CMT.cpp 9.8 KB

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