CMT.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 "CPathfinder.h"
  24. #include "CGameState.h"
  25. #include "CCallback.h"
  26. #include "CPlayerInterface.h"
  27. #include "CAdvmapInterface.h"
  28. #include "hch/CBuildingHandler.h"
  29. #include "hch/CVideoHandler.h"
  30. #include "hch/CAbilityHandler.h"
  31. #include "hch/CHeroHandler.h"
  32. #include "hch/CCreatureHandler.h"
  33. #include "hch/CSpellHandler.h"
  34. #include "hch/CMusicHandler.h"
  35. #include "hch/CLodHandler.h"
  36. #include "hch/CDefHandler.h"
  37. #include "hch/CAmbarCendamo.h"
  38. #include "hch/CGeneralTextHandler.h"
  39. #include "client/Graphics.h"
  40. #include "client/Client.h"
  41. #include "client/CConfigHandler.h"
  42. #include "lib/Connection.h"
  43. #include "lib/VCMI_Lib.h"
  44. #include <cstdlib>
  45. #if __MINGW32__
  46. #undef main
  47. #endif
  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. 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. tlog0 << "Starting... " << std::endl;
  63. THC timeHandler tmh, total, pomtime;
  64. CClient *client = NULL;
  65. boost::thread *console = NULL;
  66. std::cout.flags(std::ios::unitbuf);
  67. logfile = new std::ofstream("VCMI_Client_log.txt");
  68. ::console = new CConsoleHandler;
  69. *::console->cb = boost::bind(processCommand,_1,boost::ref(client));
  70. console = new boost::thread(boost::bind(&CConsoleHandler::run,::console));
  71. tlog0 <<"Creating console and logfile: "<<pomtime.getDif() << std::endl;
  72. conf.init();
  73. tlog0 <<"Loading settings: "<<pomtime.getDif() << std::endl;
  74. tlog0 << NAME << std::endl;
  75. srand ( time(NULL) );
  76. CPG=NULL;
  77. atexit(SDL_Quit);
  78. CGameInfo * cgi = CGI = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  79. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO)==0)
  80. {
  81. screen = SDL_SetVideoMode(800,600,conf.cc.bpp,SDL_SWSURFACE|SDL_DOUBLEBUF|(conf.cc.fullscreen?SDL_FULLSCREEN:0)); //initializing important global surface
  82. tlog0 <<"\tInitializing screen: "<<pomtime.getDif();
  83. tlog0 << std::endl;
  84. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  85. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  86. int rmask = 0xff000000;int gmask = 0x00ff0000;int bmask = 0x0000ff00;int amask = 0x000000ff;
  87. #else
  88. int rmask = 0x000000ff; int gmask = 0x0000ff00; int bmask = 0x00ff0000; int amask = 0xff000000;
  89. #endif
  90. CSDL_Ext::std32bppSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 1, 32, rmask, gmask, bmask, amask);
  91. tlog0 << "\tInitializing minors: " << pomtime.getDif() << std::endl;
  92. TTF_Init();
  93. TNRB16 = TTF_OpenFont("Fonts" PATHSEPARATOR "tnrb.ttf",16);
  94. GEOR13 = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",13);
  95. GEOR16 = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",16);
  96. GEORXX = TTF_OpenFont("Fonts" PATHSEPARATOR "tnrb.ttf",22);
  97. GEORM = TTF_OpenFont("Fonts" PATHSEPARATOR "georgia.ttf",10);
  98. if(! (TNRB16 && GEOR16 && GEORXX && GEORM))
  99. {
  100. tlog1 << "One of the fonts couldn't be loaded!\n";
  101. throw "One of the fonts couldn't be loaded!\n";
  102. }
  103. atexit(TTF_Quit);
  104. THC tlog0<<"\tInitializing fonts: "<<pomtime.getDif()<<std::endl;
  105. CMusicHandler * mush = new CMusicHandler; //initializing audio
  106. mush->initMusics();
  107. //audio initialized
  108. cgi->mush = mush;
  109. tlog0<<"\tInitializing sound: "<<pomtime.getDif()<<std::endl;
  110. tlog0<<"Initializing screen, fonts and sound handling: "<<tmh.getDif()<<std::endl;
  111. CDefHandler::Spriteh = cgi->spriteh = new CLodHandler();
  112. cgi->spriteh->init("Data" PATHSEPARATOR "H3sprite.lod","Sprites");
  113. BitmapHandler::bitmaph = cgi->bitmaph = new CLodHandler;
  114. cgi->bitmaph->init("Data" PATHSEPARATOR "H3bitmap.lod","Data");
  115. tlog0<<"Loading .lod files: "<<tmh.getDif()<<std::endl;
  116. initDLL(cgi->bitmaph,::console,logfile);
  117. CGI->setFromLib();
  118. tlog0<<"Initializing VCMI_Lib: "<<tmh.getDif()<<std::endl;
  119. pomtime.getDif();
  120. cgi->curh = new CCursorHandler;
  121. cgi->curh->initCursor();
  122. cgi->curh->show();
  123. tlog0<<"\tScreen handler: "<<pomtime.getDif()<<std::endl;
  124. CAbilityHandler * abilh = new CAbilityHandler;
  125. abilh->loadAbilities();
  126. cgi->abilh = abilh;
  127. tlog0<<"\tAbility handler: "<<pomtime.getDif()<<std::endl;
  128. cgi->pathf = new CPathfinder();
  129. tlog0<<"\tPathfinder: "<<pomtime.getDif()<<std::endl;
  130. tlog0<<"Preparing first handlers: "<<tmh.getDif()<<std::endl;
  131. pomtime.getDif();
  132. graphics = new Graphics();
  133. graphics->loadHeroAnim();
  134. tlog0<<"\tMain graphics: "<<tmh.getDif()<<std::endl;
  135. tlog0<<"Initializing game graphics: "<<tmh.getDif()<<std::endl;
  136. CMessage::init();
  137. tlog0<<"Message handler: "<<tmh.getDif()<<std::endl;
  138. CPreGame * cpg = new CPreGame(); //main menu and submenus
  139. tlog0<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  140. tlog0<<"Initialization of VCMI (together): "<<total.getDif()<<std::endl;
  141. cpg->mush = mush;
  142. StartInfo *options = new StartInfo(cpg->runLoop());
  143. if(screen->w != conf.cc.resx || screen->h != conf.cc.resy)
  144. {
  145. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  146. SDL_InitSubSystem(SDL_INIT_VIDEO);
  147. 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
  148. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  149. SDL_ShowCursor(SDL_DISABLE);
  150. }
  151. CClient cl;
  152. if(options->mode == 0) //new game
  153. {
  154. tmh.getDif();
  155. char portc[10];
  156. SDL_itoa(conf.cc.port,portc,10);
  157. CClient::runServer(portc);
  158. tlog0<<"Preparing shared memory and starting server: "<<tmh.getDif()<<std::endl;
  159. tmh.getDif();pomtime.getDif();//reset timers
  160. CConnection *c=NULL;
  161. //wait until server is ready
  162. tlog0<<"Waiting for server... ";
  163. cl.waitForServer();
  164. tlog0 << tmh.getDif()<<std::endl;
  165. while(!c)
  166. {
  167. try
  168. {
  169. tlog0 << "Establishing connection...\n";
  170. c = new CConnection(conf.cc.server,portc,NAME);
  171. }
  172. catch(...)
  173. {
  174. tlog1 << "\nCannot establish connection! Retrying within 2 seconds" <<std::endl;
  175. SDL_Delay(2000);
  176. }
  177. }
  178. THC tlog0<<"\tConnecting to the server: "<<tmh.getDif()<<std::endl;
  179. cl.newGame(c,options);
  180. client = &cl;
  181. boost::thread t(boost::bind(&CClient::run,&cl));
  182. }
  183. else //load game
  184. {
  185. std::string fname = options->mapname;
  186. boost::algorithm::erase_last(fname,".vlgm1");
  187. cl.load(fname);
  188. client = &cl;
  189. boost::thread t(boost::bind(&CClient::run,&cl));
  190. }
  191. SDL_Event ev;
  192. while(1) //main SDL events loop
  193. {
  194. SDL_WaitEvent(&ev);
  195. if((ev.type==SDL_QUIT) || (ev.type == SDL_KEYDOWN && ev.key.keysym.sym==SDLK_F4 && (ev.key.keysym.mod & KMOD_ALT)))
  196. {
  197. cl.close();
  198. #ifndef __unix__
  199. ::console->killConsole(console->native_handle());
  200. #endif
  201. LOCPLINT->pim->lock();
  202. SDL_Delay(750);
  203. tlog0 << "Ending...\n";
  204. exit(EXIT_SUCCESS);
  205. }
  206. else if(ev.type == SDL_KEYDOWN && ev.key.keysym.sym==SDLK_F4)
  207. {
  208. LOCPLINT->pim->lock();
  209. bool full = !(screen->flags&SDL_FULLSCREEN);
  210. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  211. SDL_InitSubSystem(SDL_INIT_VIDEO);
  212. screen = SDL_SetVideoMode(conf.cc.resx,conf.cc.resy,conf.cc.bpp,SDL_SWSURFACE|SDL_DOUBLEBUF|(full?SDL_FULLSCREEN:0)); //initializing important global surface
  213. SDL_WM_SetCaption(NAME.c_str(),""); //set window title
  214. SDL_ShowCursor(SDL_DISABLE);
  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(cn=="load")
  270. {
  271. std::string fname;
  272. readed >> fname;
  273. client->load(fname);
  274. }
  275. else if(cn=="resolution")
  276. {
  277. std::map<std::pair<int,int>, config::GUIOptions >::iterator j;
  278. int i=1, hlp=1;
  279. tlog4 << "Available screen resolutions:\n";
  280. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end(); j++)
  281. tlog4 << i++ <<". " << j->first.first << " x " << j->first.second << std::endl;
  282. tlog4 << "Type number from 1 to " << i-1 << " to set appropriate resolution or 0 to cancel.\n";
  283. std::cin >> i;
  284. if(!i)
  285. return;
  286. else if(i < 0 || i > conf.guiOptions.size())
  287. {
  288. tlog1 << "Invalid resolution ID! Not a number between 0 and " << conf.guiOptions.size() << ". No settings changed.\n";
  289. }
  290. else
  291. {
  292. for(j=conf.guiOptions.begin(); j!=conf.guiOptions.end() && hlp++<i; j++); //move j to the i-th resolution info
  293. conf.cc.resx = j->first.first;
  294. conf.cc.resy = j->first.second;
  295. tlog0 << "Screen resolution set to " << conf.cc.resx << " x " << conf.cc.resy <<". It will be aplied when the game starts.\n";
  296. }
  297. }
  298. else if(message=="get txt")
  299. {
  300. boost::filesystem::create_directory("Extracted_txts");
  301. tlog0<<"Command accepted. Opening .lod file...\t";
  302. CLodHandler * txth = new CLodHandler;
  303. txth->init(std::string(DATA_DIR "Data" PATHSEPARATOR "H3bitmap.lod"),"");
  304. tlog0<<"done.\nScanning .lod file\n";
  305. int curp=0;
  306. std::string pattern = ".TXT", pom;
  307. for(int i=0;i<txth->entries.size(); i++)
  308. {
  309. pom = txth->entries[i].nameStr;
  310. if(boost::algorithm::find_last(pom,pattern))
  311. {
  312. txth->extractFile(std::string("Extracted_txts\\")+pom,pom);
  313. }
  314. if(i%8) continue;
  315. int p2 = ((float)i/(float)txth->entries.size())*(float)100;
  316. if(p2!=curp)
  317. {
  318. curp = p2;
  319. tlog0<<"\r"<<curp<<"%";
  320. }
  321. }
  322. tlog0<<"\rExtracting done :)\n";
  323. }
  324. else if(client && client->serv && client->serv->connected) //send to server
  325. {
  326. *client->serv << ui16(513) << message;
  327. }
  328. }