CMT.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/thread.hpp>
  12. #include <SDL_ttf.h>
  13. #include <SDL_mixer.h>
  14. #include "SDL_Extensions.h"
  15. #include "SDL_framerate.h"
  16. #include "CGameInfo.h"
  17. #include "mapHandler.h"
  18. #include "global.h"
  19. #include "CPreGame.h"
  20. #include "CCastleInterface.h"
  21. #include "CConsoleHandler.h"
  22. #include "CCursorHandler.h"
  23. #include "CGameState.h"
  24. #include "CCallback.h"
  25. #include "CPlayerInterface.h"
  26. #include "CAdvmapInterface.h"
  27. #include "hch/CBuildingHandler.h"
  28. #include "hch/CVideoHandler.h"
  29. #include "hch/CHeroHandler.h"
  30. #include "hch/CCreatureHandler.h"
  31. #include "hch/CSpellHandler.h"
  32. #include "hch/CMusicHandler.h"
  33. #include "hch/CLodHandler.h"
  34. #include "hch/CDefHandler.h"
  35. #include "hch/CAmbarCendamo.h"
  36. #include "hch/CGeneralTextHandler.h"
  37. #include "client/Graphics.h"
  38. #include "client/Client.h"
  39. #include "client/CConfigHandler.h"
  40. #include "lib/Connection.h"
  41. #include "lib/VCMI_Lib.h"
  42. #include <cstdlib>
  43. #include "lib/NetPacks.h"
  44. #if __MINGW32__
  45. #undef main
  46. #endif
  47. std::string NAME = NAME_VER + std::string(" (client)");
  48. SDL_Surface * screen, * screen2;
  49. std::queue<SDL_Event*> events;
  50. boost::mutex eventsM;
  51. TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM, *GEOR16;
  52. void processCommand(const std::string &message, CClient *&client);
  53. #ifndef __GNUC__
  54. int _tmain(int argc, _TCHAR* argv[])
  55. #else
  56. int main(int argc, char** argv)
  57. #endif
  58. {
  59. tlog0 << "Starting... " << std::endl;
  60. THC timeHandler tmh, total, pomtime;
  61. CClient *client = NULL;
  62. boost::thread *console = NULL;
  63. std::cout.flags(std::ios::unitbuf);
  64. logfile = new std::ofstream("VCMI_Client_log.txt");
  65. ::console = new CConsoleHandler;
  66. *::console->cb = boost::bind(processCommand,_1,boost::ref(client));
  67. console = new boost::thread(boost::bind(&CConsoleHandler::run,::console));
  68. tlog0 <<"Creating console and logfile: "<<pomtime.getDif() << std::endl;
  69. conf.init();
  70. tlog0 <<"Loading settings: "<<pomtime.getDif() << std::endl;
  71. tlog0 << NAME << std::endl;
  72. srand ( time(NULL) );
  73. CPG=NULL;
  74. atexit(SDL_Quit);
  75. CGameInfo * cgi = CGI = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  76. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO)==0)
  77. {
  78. screen = SDL_SetVideoMode(800,600,conf.cc.bpp,SDL_SWSURFACE|SDL_DOUBLEBUF|(conf.cc.fullscreen?SDL_FULLSCREEN:0)); //initializing important global surface
  79. tlog0 <<"\tInitializing screen: "<<pomtime.getDif();
  80. tlog0 << std::endl;
  81. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  82. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  83. int rmask = 0xff000000;int gmask = 0x00ff0000;int bmask = 0x0000ff00;int amask = 0x000000ff;
  84. #else
  85. int rmask = 0x000000ff; int gmask = 0x0000ff00; int bmask = 0x00ff0000; int amask = 0xff000000;
  86. #endif
  87. CSDL_Ext::std32bppSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 1, 32, rmask, gmask, bmask, amask);
  88. tlog0 << "\tInitializing minors: " << pomtime.getDif() << std::endl;
  89. TTF_Init();
  90. TNRB16 = TTF_OpenFont("Fonts" PATHSEPARATOR "tnrb.ttf",16);
  91. GEOR13 = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",13);
  92. GEOR16 = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",16);
  93. GEORXX = TTF_OpenFont("Fonts" PATHSEPARATOR "tnrb.ttf",22);
  94. GEORM = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",10);
  95. if(! (TNRB16 && GEOR16 && GEORXX && GEORM))
  96. {
  97. tlog1 << "One of the fonts couldn't be loaded!\n";
  98. throw "One of the fonts couldn't be loaded!\n";
  99. }
  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->setFromLib();
  115. tlog0<<"Initializing VCMI_Lib: "<<tmh.getDif()<<std::endl;
  116. pomtime.getDif();
  117. cgi->curh = new CCursorHandler;
  118. cgi->curh->initCursor();
  119. cgi->curh->show();
  120. tlog0<<"Screen handler: "<<pomtime.getDif()<<std::endl;
  121. pomtime.getDif();
  122. graphics = new Graphics();
  123. graphics->loadHeroAnim();
  124. tlog0<<"\tMain graphics: "<<tmh.getDif()<<std::endl;
  125. tlog0<<"Initializing game graphics: "<<tmh.getDif()<<std::endl;
  126. CMessage::init();
  127. tlog0<<"Message handler: "<<tmh.getDif()<<std::endl;
  128. CPreGame * cpg = new CPreGame(); //main menu and submenus
  129. tlog0<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  130. tlog0<<"Initialization of VCMI (together): "<<total.getDif()<<std::endl;
  131. cpg->mush = mush;
  132. StartInfo *options = new StartInfo(cpg->runLoop());
  133. if(screen->w != conf.cc.resx || screen->h != conf.cc.resy)
  134. {
  135. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  136. SDL_InitSubSystem(SDL_INIT_VIDEO);
  137. 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
  138. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  139. SDL_ShowCursor(SDL_DISABLE);
  140. }
  141. CClient cl;
  142. if(options->mode == 0) //new game
  143. {
  144. tmh.getDif();
  145. char portc[10];
  146. SDL_itoa(conf.cc.port,portc,10);
  147. CClient::runServer(portc);
  148. tlog0<<"Preparing shared memory and starting server: "<<tmh.getDif()<<std::endl;
  149. tmh.getDif();pomtime.getDif();//reset timers
  150. CConnection *c=NULL;
  151. //wait until server is ready
  152. tlog0<<"Waiting for server... ";
  153. cl.waitForServer();
  154. tlog0 << tmh.getDif()<<std::endl;
  155. while(!c)
  156. {
  157. try
  158. {
  159. tlog0 << "Establishing connection...\n";
  160. c = new CConnection(conf.cc.server,portc,NAME);
  161. }
  162. catch(...)
  163. {
  164. tlog1 << "\nCannot establish connection! Retrying within 2 seconds" <<std::endl;
  165. SDL_Delay(2000);
  166. }
  167. }
  168. THC tlog0<<"\tConnecting to the server: "<<tmh.getDif()<<std::endl;
  169. cl.newGame(c,options);
  170. client = &cl;
  171. boost::thread t(boost::bind(&CClient::run,&cl));
  172. }
  173. else //load game
  174. {
  175. std::string fname = options->mapname;
  176. boost::algorithm::erase_last(fname,".vlgm1");
  177. cl.load(fname);
  178. client = &cl;
  179. boost::thread t(boost::bind(&CClient::run,&cl));
  180. }
  181. SDL_Event *ev = NULL;
  182. while(1) //main SDL events loop
  183. {
  184. ev = new SDL_Event();
  185. SDL_WaitEvent(ev);
  186. if((ev->type==SDL_QUIT) || (ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4 && (ev->key.keysym.mod & KMOD_ALT)))
  187. {
  188. cl.close();
  189. #ifndef __unix__
  190. ::console->killConsole(console->native_handle());
  191. #endif
  192. LOCPLINT->pim->lock();
  193. SDL_Delay(750);
  194. tlog0 << "Ending...\n";
  195. exit(EXIT_SUCCESS);
  196. }
  197. else if(ev->type == SDL_KEYDOWN && ev->key.keysym.sym==SDLK_F4)
  198. {
  199. LOCPLINT->pim->lock();
  200. bool full = !(screen->flags&SDL_FULLSCREEN);
  201. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  202. SDL_InitSubSystem(SDL_INIT_VIDEO);
  203. screen = SDL_SetVideoMode(conf.cc.resx,conf.cc.resy,conf.cc.bpp,SDL_SWSURFACE|SDL_DOUBLEBUF|(full?SDL_FULLSCREEN:0)); //initializing important global surface
  204. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  205. SDL_ShowCursor(SDL_DISABLE);
  206. LOCPLINT->curint->show();
  207. if(LOCPLINT->curint != LOCPLINT->adventureInt)
  208. LOCPLINT->adventureInt->show();
  209. if(LOCPLINT->curint == LOCPLINT->castleInt)
  210. LOCPLINT->castleInt->showAll(0,true);
  211. if(LOCPLINT->curint->subInt)
  212. LOCPLINT->curint->subInt->show();
  213. LOCPLINT->pim->unlock();
  214. }
  215. eventsM.lock();
  216. events.push(ev);
  217. eventsM.unlock();
  218. }
  219. }
  220. else
  221. {
  222. tlog1<<"Something was wrong: "<<SDL_GetError()<<std::endl;
  223. return -1;
  224. }
  225. }
  226. void processCommand(const std::string &message, CClient *&client)
  227. {
  228. std::istringstream readed;
  229. readed.str(message);
  230. std::string cn; //command name
  231. readed >> cn;
  232. int3 src, dst;
  233. // int heronum;//TODO use me
  234. int3 dest;
  235. if(message==std::string("die, fool"))
  236. exit(EXIT_SUCCESS);
  237. else if(cn==std::string("activate"))
  238. {
  239. int what;
  240. readed >> what;
  241. switch (what)
  242. {
  243. case 0:
  244. LOCPLINT->curint->activate();
  245. break;
  246. case 1:
  247. LOCPLINT->adventureInt->activate();
  248. break;
  249. case 2:
  250. LOCPLINT->castleInt->activate();
  251. break;
  252. }
  253. }
  254. else if(cn=="save")
  255. {
  256. std::string fname;
  257. readed >> fname;
  258. client->save(fname);
  259. }
  260. else if(cn=="load")
  261. {
  262. std::string fname;
  263. readed >> fname;
  264. client->load(fname);
  265. }
  266. else if(cn=="resolution")
  267. {
  268. std::map<std::pair<int,int>, config::GUIOptions >::iterator j;
  269. int i=1, hlp=1;
  270. tlog4 << "Available screen resolutions:\n";
  271. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end(); j++)
  272. tlog4 << i++ <<". " << j->first.first << " x " << j->first.second << std::endl;
  273. tlog4 << "Type number from 1 to " << i-1 << " to set appropriate resolution or 0 to cancel.\n";
  274. std::cin >> i;
  275. if(!i)
  276. return;
  277. else if(i < 0 || i > conf.guiOptions.size())
  278. {
  279. tlog1 << "Invalid resolution ID! Not a number between 0 and " << conf.guiOptions.size() << ". No settings changed.\n";
  280. }
  281. else
  282. {
  283. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end() && hlp++<i; j++); //move j to the i-th resolution info
  284. conf.cc.resx = j->first.first;
  285. conf.cc.resy = j->first.second;
  286. tlog0 << "Screen resolution set to " << conf.cc.resx << " x " << conf.cc.resy <<". It will be aplied when the game starts.\n";
  287. }
  288. }
  289. else if(message=="get txt")
  290. {
  291. boost::filesystem::create_directory("Extracted_txts");
  292. tlog0<<"Command accepted. Opening .lod file...\t";
  293. CLodHandler * txth = new CLodHandler;
  294. txth->init(std::string(DATA_DIR "Data" PATHSEPARATOR "H3bitmap.lod"),"");
  295. tlog0<<"done.\nScanning .lod file\n";
  296. int curp=0;
  297. std::string pattern = ".TXT", pom;
  298. for(int i=0;i<txth->entries.size(); i++)
  299. {
  300. pom = txth->entries[i].nameStr;
  301. if(boost::algorithm::find_last(pom,pattern))
  302. {
  303. txth->extractFile(std::string("Extracted_txts\\")+pom,pom);
  304. }
  305. if(i%8) continue;
  306. int p2 = ((float)i/(float)txth->entries.size())*(float)100;
  307. if(p2!=curp)
  308. {
  309. curp = p2;
  310. tlog0<<"\r"<<curp<<"%";
  311. }
  312. }
  313. tlog0<<"\rExtracting done :)\n";
  314. }
  315. else if(client && client->serv && client->serv->connected) //send to server
  316. {
  317. *client->serv << &PlayerMessage(LOCPLINT->playerID,message);
  318. }
  319. }