2
0

CMT.cpp 11 KB

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