CMT.cpp 11 KB

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