CMT.cpp 10 KB

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