CMT.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // CMT.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <cmath>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <cmath>
  9. #include <boost/algorithm/string.hpp>
  10. #include "boost/filesystem/operations.hpp"
  11. #include <boost/interprocess/mapped_region.hpp>
  12. #include <boost/interprocess/shared_memory_object.hpp>
  13. #include <boost/thread.hpp>
  14. #include "SDL_ttf.h"
  15. #include "SDL_mixer.h"
  16. #include "SDL_Extensions.h"
  17. #include "SDL_framerate.h"
  18. #include "CGameInfo.h"
  19. #include "mapHandler.h"
  20. #include "global.h"
  21. #include "CPreGame.h"
  22. #include "CCastleInterface.h"
  23. #include "CConsoleHandler.h"
  24. #include "CCursorHandler.h"
  25. #include "CPathfinder.h"
  26. #include "CGameState.h"
  27. #include "CCallback.h"
  28. #include "CPlayerInterface.h"
  29. #include "CLuaHandler.h"
  30. #include "CAdvmapInterface.h"
  31. #include "hch/CBuildingHandler.h"
  32. #include "hch/CVideoHandler.h"
  33. #include "hch/CAbilityHandler.h"
  34. #include "hch/CHeroHandler.h"
  35. #include "hch/CCreatureHandler.h"
  36. #include "hch/CSpellHandler.h"
  37. #include "hch/CMusicHandler.h"
  38. #include "hch/CLodHandler.h"
  39. #include "hch/CDefHandler.h"
  40. #include "hch/CAmbarCendamo.h"
  41. #include "hch/CGeneralTextHandler.h"
  42. #include "client/Graphics.h"
  43. #include "client/Client.h"
  44. #include "lib/Connection.h"
  45. #include "lib/Interprocess.h"
  46. #include "lib/VCMI_Lib.h"
  47. std::string NAME = NAME_VER + std::string(" (client)");
  48. DLL_EXPORT void initDLL(CLodHandler *b);
  49. SDL_Surface * screen, * screen2;
  50. extern SDL_Surface * CSDL_Ext::std32bppSurface;
  51. std::queue<SDL_Event> events;
  52. boost::mutex eventsM;
  53. TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM, *GEOR16;
  54. namespace intpr = boost::interprocess;
  55. void processCommand(const std::string &message, CClient *&client);
  56. #ifndef __GNUC__
  57. int _tmain(int argc, _TCHAR* argv[])
  58. #else
  59. int main(int argc, char** argv)
  60. #endif
  61. {
  62. CClient *client = NULL;
  63. boost::thread *console = NULL;
  64. if(argc>2)
  65. {
  66. std::cout << "Special mode without new support for console!" << std::endl;
  67. }
  68. else
  69. {
  70. logfile = new std::ofstream("VCMI_Client_log.txt");
  71. ::console = new CConsoleHandler;
  72. *::console->cb = boost::bind(processCommand,_1,boost::ref(client));
  73. console = new boost::thread(boost::bind(&CConsoleHandler::run,::console));
  74. }
  75. tlog0 << "\tConsole and logifle ready!" << std::endl;
  76. int port;
  77. if(argc > 1)
  78. {
  79. #ifdef _MSC_VER
  80. port = _tstoi(argv[1]);
  81. #else
  82. port = _ttoi(argv[1]);
  83. #endif
  84. }
  85. else
  86. {
  87. port = 3030;
  88. tlog0 << "Port " << port << " will be used." << std::endl;
  89. }
  90. std::cout.flags(ios::unitbuf);
  91. tlog0 << NAME << std::endl;
  92. srand ( time(NULL) );
  93. CPG=NULL;
  94. atexit(SDL_Quit);
  95. CGameInfo * cgi = CGI = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  96. //CLuaHandler luatest;
  97. //luatest.test();
  98. //CBIKHandler cb;
  99. //cb.open("CSECRET.BIK");
  100. tlog0 << "Starting... " << std::endl;
  101. THC timeHandler tmh, total, pomtime;
  102. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO)==0)
  103. {
  104. screen = SDL_SetVideoMode(800,600,24,SDL_SWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/); //initializing important global surface
  105. tlog0 <<"\tInitializing screen: "<<pomtime.getDif();
  106. tlog0 << std::endl;
  107. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  108. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  109. int rmask = 0xff000000;int gmask = 0x00ff0000;int bmask = 0x0000ff00;int amask = 0x000000ff;
  110. #else
  111. int rmask = 0x000000ff; int gmask = 0x0000ff00; int bmask = 0x00ff0000; int amask = 0xff000000;
  112. #endif
  113. CSDL_Ext::std32bppSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 1, 32, rmask, gmask, bmask, amask);
  114. tlog0 << "\tInitializing minors: " << pomtime.getDif() << std::endl;
  115. TTF_Init();
  116. TNRB16 = TTF_OpenFont("Fonts" PATHSEPARATOR "tnrb.ttf",16);
  117. GEOR13 = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",13);
  118. GEOR16 = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",16);
  119. GEORXX = TTF_OpenFont("Fonts" PATHSEPARATOR "tnrb.ttf",22);
  120. GEORM = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",10);
  121. atexit(TTF_Quit);
  122. THC tlog0<<"\tInitializing fonts: "<<pomtime.getDif()<<std::endl;
  123. CMusicHandler * mush = new CMusicHandler; //initializing audio
  124. mush->initMusics();
  125. //audio initialized
  126. cgi->mush = mush;
  127. THC tlog0<<"\tInitializing sound: "<<pomtime.getDif()<<std::endl;
  128. THC tlog0<<"Initializing screen, fonts and sound handling: "<<tmh.getDif()<<std::endl;
  129. CDefHandler::Spriteh = cgi->spriteh = new CLodHandler();
  130. cgi->spriteh->init("Data" PATHSEPARATOR "H3sprite.lod","Sprites");
  131. BitmapHandler::bitmaph = cgi->bitmaph = new CLodHandler;
  132. cgi->bitmaph->init("Data" PATHSEPARATOR "H3bitmap.lod","Data");
  133. THC tlog0<<"Loading .lod files: "<<tmh.getDif()<<std::endl;
  134. initDLL(cgi->bitmaph,::console,logfile);
  135. CGI->arth = VLC->arth;
  136. CGI->creh = VLC->creh;
  137. CGI->townh = VLC->townh;
  138. CGI->heroh = VLC->heroh;
  139. CGI->objh = VLC->objh;
  140. CGI->spellh = VLC->spellh;
  141. CGI->dobjinfo = VLC->dobjinfo;
  142. CGI->buildh = VLC->buildh;
  143. tlog0<<"Initializing VCMI_Lib: "<<tmh.getDif()<<std::endl;
  144. pomtime.getDif();
  145. cgi->curh = new CCursorHandler;
  146. cgi->curh->initCursor();
  147. tlog0<<"\tScreen handler: "<<pomtime.getDif()<<std::endl;
  148. CAbilityHandler * abilh = new CAbilityHandler;
  149. abilh->loadAbilities();
  150. cgi->abilh = abilh;
  151. tlog0<<"\tAbility handler: "<<pomtime.getDif()<<std::endl;
  152. tlog0<<"Preparing first handlers: "<<tmh.getDif()<<std::endl;
  153. pomtime.getDif();
  154. graphics = new Graphics();
  155. tlog0<<"\tMain graphics: "<<tmh.getDif()<<std::endl;
  156. std::vector<CDefHandler **> animacje;
  157. for(std::vector<CHeroClass *>::iterator i = cgi->heroh->heroClasses.begin();i!=cgi->heroh->heroClasses.end();i++)
  158. animacje.push_back(&((*i)->*(&CHeroClass::moveAnim)));
  159. graphics->loadHeroAnim(animacje);
  160. tlog0<<"\tHero animations: "<<tmh.getDif()<<std::endl;
  161. tlog0<<"Initializing game graphics: "<<tmh.getDif()<<std::endl;
  162. CMessage::init();
  163. cgi->generaltexth = new CGeneralTextHandler;
  164. cgi->generaltexth->load();
  165. tlog0<<"Preparing more handlers: "<<tmh.getDif()<<std::endl;
  166. CPreGame * cpg = new CPreGame(); //main menu and submenus
  167. tlog0<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  168. tlog0<<"Initialization of VCMI (togeter): "<<total.getDif()<<std::endl;
  169. cpg->mush = mush;
  170. StartInfo *options = new StartInfo(cpg->runLoop());
  171. tmh.getDif();
  172. ////////////////////////SERVER STARTING/////////////////////////////////////////////////
  173. char portc[10]; SDL_itoa(port,portc,10);
  174. intpr::shared_memory_object smo(intpr::open_or_create,"vcmi_memory",intpr::read_write);
  175. smo.truncate(sizeof(ServerReady));
  176. intpr::mapped_region mr(smo,intpr::read_write);
  177. ServerReady *sr = new(mr.get_address())ServerReady();
  178. std::string comm = std::string(SERVER_NAME) + " " + portc + " > server_log.txt";
  179. boost::thread servthr(boost::bind(system,comm.c_str())); //runs server executable; //TODO: will it work on non-windows platforms?
  180. tlog0<<"Preparing shared memory and starting server: "<<tmh.getDif()<<std::endl;
  181. ///////////////////////////////////////////////////////////////////////////////////////
  182. tmh.getDif();pomtime.getDif();//reset timers
  183. cgi->pathf = new CPathfinder();
  184. tlog0<<"\tPathfinder: "<<pomtime.getDif()<<std::endl;
  185. tlog0<<"Handlers initialization (together): "<<tmh.getDif()<<std::endl;
  186. std::ofstream logs("client_log.txt");
  187. CConnection *c=NULL;
  188. //wait until server is ready
  189. tlog0<<"Waiting for server... ";
  190. {
  191. intpr::scoped_lock<intpr::interprocess_mutex> slock(sr->mutex);
  192. while(!sr->ready)
  193. {
  194. sr->cond.wait(slock);
  195. }
  196. }
  197. intpr::shared_memory_object::remove("vcmi_memory");
  198. tlog0 << tmh.getDif()<<std::endl;
  199. while(!c)
  200. {
  201. try
  202. {
  203. tlog0 << "Establishing connection...\n";
  204. c = new CConnection("127.0.0.1",portc,NAME,logs);
  205. }
  206. catch(...)
  207. {
  208. tlog1 << "\nCannot establish connection! Retrying within 2 seconds" <<std::endl;
  209. SDL_Delay(2000);
  210. }
  211. }
  212. THC tlog0<<"\tConnecting to the server: "<<tmh.getDif()<<std::endl;
  213. CClient cl(c,options);
  214. client = &cl;
  215. boost::thread t(boost::bind(&CClient::run,&cl));
  216. SDL_Event ev;
  217. while(1) //main SDL events loop
  218. {
  219. SDL_WaitEvent(&ev);
  220. if((ev.type==SDL_QUIT) || (ev.type == SDL_KEYDOWN && ev.key.keysym.sym==SDLK_F4 && (ev.key.keysym.mod & KMOD_ALT)))
  221. {
  222. cl.close();
  223. #ifndef __unix__
  224. ::console->killConsole(console->native_handle());
  225. #endif
  226. SDL_Delay(750);
  227. tlog0 << "Ending...\n";
  228. exit(0);
  229. }
  230. eventsM.lock();
  231. events.push(ev);
  232. eventsM.unlock();
  233. }
  234. }
  235. else
  236. {
  237. tlog1<<"Something was wrong: "<<SDL_GetError()<<std::endl;
  238. return -1;
  239. }
  240. }
  241. void processCommand(const std::string &message, CClient *&client)
  242. {
  243. std::istringstream readed;
  244. readed.str(message);
  245. std::string cn; //command name
  246. readed >> cn;
  247. int3 src, dst;
  248. int heronum;
  249. int3 dest;
  250. if(message==std::string("die, fool"))
  251. exit(0);
  252. else if(cn==std::string("activate"))
  253. {
  254. int what;
  255. readed >> what;
  256. switch (what)
  257. {
  258. case 0:
  259. LOCPLINT->curint->activate();
  260. break;
  261. case 1:
  262. LOCPLINT->adventureInt->activate();
  263. break;
  264. case 2:
  265. LOCPLINT->castleInt->activate();
  266. break;
  267. }
  268. }
  269. else if(message=="get txt")
  270. {
  271. boost::filesystem::create_directory("Extracted_txts");
  272. tlog0<<"Command accepted. Opening .lod file...\t";
  273. CLodHandler * txth = new CLodHandler;
  274. txth->init(std::string(DATA_DIR "Data" PATHSEPARATOR "H3bitmap.lod"),"data");
  275. tlog0<<"done.\nScanning .lod file\n";
  276. int curp=0;
  277. std::string pattern = ".TXT", pom;
  278. for(int i=0;i<txth->entries.size(); i++)
  279. {
  280. pom = txth->entries[i].nameStr;
  281. if(boost::algorithm::find_last(pom,pattern))
  282. {
  283. txth->extractFile(std::string("Extracted_txts\\")+pom,pom);
  284. }
  285. if(i%8) continue;
  286. int p2 = ((float)i/(float)txth->entries.size())*(float)100;
  287. if(p2!=curp)
  288. {
  289. curp = p2;
  290. tlog0<<"\r"<<curp<<"%";
  291. }
  292. }
  293. tlog0<<"\rExtracting done :)\n";
  294. }
  295. else if(client && client->serv && client->serv->connected) //send to server
  296. {
  297. *client->serv << ui16(513) << message;
  298. }
  299. }