CMT.cpp 11 KB

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