CMT.cpp 9.9 KB

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