CMT.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. // CMT.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "SDL.h"
  5. #include "SDL_TTF.h"
  6. #include "hch\CVideoHandler.h"
  7. #include "SDL_mixer.h"
  8. #include "hch\CBuildingHandler.h"
  9. #include "SDL_Extensions.h"
  10. #include "SDL_framerate.h"
  11. #include <cmath>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <string>
  15. #include <assert.h>
  16. #include <vector>
  17. #include "zlib.h"
  18. #include <cmath>
  19. #include <ctime>
  20. #include "hch\CArtHandler.h"
  21. #include "hch\CHeroHandler.h"
  22. #include "hch\CCreatureHandler.h"
  23. #include "hch\CAbilityHandler.h"
  24. #include "hch\CSpellHandler.h"
  25. #include "hch\CBuildingHandler.h"
  26. #include "hch\CObjectHandler.h"
  27. #include "CGameInfo.h"
  28. #include "hch\CMusicHandler.h"
  29. #include "hch\CLodHandler.h"
  30. #include "hch\CDefHandler.h"
  31. #include "hch\CSndHandler.h"
  32. #include "hch\CTownHandler.h"
  33. #include "hch\CDefObjInfoHandler.h"
  34. #include "hch\CAmbarCendamo.h"
  35. #include "mapHandler.h"
  36. #include "global.h"
  37. #include "CPreGame.h"
  38. #include "hch\CGeneralTextHandler.h"
  39. #include "CConsoleHandler.h"
  40. #include "CCursorHandler.h"
  41. #include "CScreenHandler.h"
  42. #include "CPathfinder.h"
  43. #include "CGameState.h"
  44. #include "CCallback.h"
  45. #include "CPlayerInterface.h"
  46. #include "CLuaHandler.h"
  47. #include "CLua.h"
  48. #include "CAdvmapInterface.h"
  49. #include "CCastleInterface.h"
  50. #include "client\Graphics.h"
  51. #include <boost/lambda/lambda.hpp>
  52. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  53. # include <fcntl.h>
  54. # include <io.h>
  55. # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  56. #else
  57. # define SET_BINARY_MODE(file)
  58. #endif
  59. #ifdef _DEBUG
  60. #endif
  61. #define CHUNK 16384
  62. const char * NAME = "VCMI \"Altanatse\" 0.7";
  63. DLL_EXPORT void initDLL(CLodHandler *b);
  64. SDL_Color playerColorPalette[256]; //palette to make interface colors good
  65. SDL_Surface * screen, * screen2;
  66. extern SDL_Surface * CSDL_Ext::std32bppSurface;
  67. TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM, *GEOR16;
  68. void handleCPPObjS(std::map<int,CCPPObjectScript*> * mapa, CCPPObjectScript * script)
  69. {
  70. std::vector<int> tempv = script->yourObjects();
  71. for (int i=0;i<tempv.size();i++)
  72. {
  73. (*mapa)[tempv[i]]=script;
  74. }
  75. CGI->state->cppscripts.insert(script);
  76. }
  77. void initGameState(Mapa * map, CGameInfo * cgi)
  78. {
  79. cgi->state->day=0;
  80. /*********creating players entries in gs****************************************/
  81. for (int i=0; i<cgi->scenarioOps.playerInfos.size();i++)
  82. {
  83. std::pair<int,PlayerState> ins(cgi->scenarioOps.playerInfos[i].color,PlayerState());
  84. ins.second.color=ins.first;
  85. ins.second.serial=i;
  86. cgi->state->players.insert(ins);
  87. }
  88. /******************RESOURCES****************************************************/
  89. //TODO: zeby komputer dostawal inaczej niz gracz
  90. std::vector<int> startres;
  91. std::ifstream tis("config/startres.txt");
  92. int k;
  93. for (int j=0;j<cgi->scenarioOps.difficulty;j++)
  94. {
  95. tis >> k;
  96. for (int z=0;z<RESOURCE_QUANTITY;z++)
  97. tis>>k;
  98. }
  99. tis >> k;
  100. for (int i=0;i<RESOURCE_QUANTITY;i++)
  101. {
  102. tis >> k;
  103. startres.push_back(k);
  104. }
  105. tis.close();
  106. for (std::map<int,PlayerState>::iterator i = cgi->state->players.begin(); i!=cgi->state->players.end(); i++)
  107. {
  108. (*i).second.resources.resize(RESOURCE_QUANTITY);
  109. for (int x=0;x<RESOURCE_QUANTITY;x++)
  110. (*i).second.resources[x] = startres[x];
  111. }
  112. /*************************HEROES************************************************/
  113. for (int i=0; i<map->heroes.size();i++) //heroes instances
  114. {
  115. if (map->heroes[i]->getOwner()<0)
  116. continue;
  117. CGHeroInstance * vhi = (map->heroes[i]);
  118. if(!vhi->type)
  119. vhi->type = cgi->heroh->heroes[vhi->subID];
  120. //vhi->subID = vhi->type->ID;
  121. if (vhi->level<1)
  122. {
  123. vhi->exp=40+rand()%50;
  124. vhi->level = 1;
  125. }
  126. if (vhi->level>1) ;//TODO dodac um dr, ale potrzebne los
  127. if ((!vhi->primSkills.size()) || (vhi->primSkills[0]<0))
  128. {
  129. if (vhi->primSkills.size()<PRIMARY_SKILLS)
  130. vhi->primSkills.resize(PRIMARY_SKILLS);
  131. vhi->primSkills[0] = vhi->type->heroClass->initialAttack;
  132. vhi->primSkills[1] = vhi->type->heroClass->initialDefence;
  133. vhi->primSkills[2] = vhi->type->heroClass->initialPower;
  134. vhi->primSkills[3] = vhi->type->heroClass->initialKnowledge;
  135. }
  136. vhi->mana = vhi->primSkills[3]*10;
  137. if (!vhi->name.length())
  138. {
  139. vhi->name = vhi->type->name;
  140. }
  141. if (!vhi->biography.length())
  142. {
  143. vhi->biography = vhi->type->biography;
  144. }
  145. if (vhi->portrait < 0)
  146. vhi->portrait = vhi->type->ID;
  147. //initial army
  148. if (!vhi->army.slots.size()) //standard army
  149. {
  150. int pom, pom2=0;
  151. for(int x=0;x<3;x++)
  152. {
  153. pom = (cgi->creh->nameToID[vhi->type->refTypeStack[x]]);
  154. if(pom>=145 && pom<=149) //war machine
  155. {
  156. pom2++;
  157. switch (pom)
  158. {
  159. case 145: //catapult
  160. vhi->artifWorn[16] = 3;
  161. break;
  162. default:
  163. pom-=145;
  164. vhi->artifWorn[13+pom] = 4+pom;
  165. break;
  166. }
  167. continue;
  168. }
  169. vhi->army.slots[x-pom2].first = &(cgi->creh->creatures[pom]);
  170. if((pom = (vhi->type->highStack[x]-vhi->type->lowStack[x])) > 0)
  171. vhi->army.slots[x-pom2].second = (rand()%pom)+vhi->type->lowStack[x];
  172. else
  173. vhi->army.slots[x-pom2].second = +vhi->type->lowStack[x];
  174. }
  175. }
  176. cgi->state->players[vhi->getOwner()].heroes.push_back(vhi);
  177. }
  178. /*************************FOG**OF**WAR******************************************/
  179. for(std::map<int, PlayerState>::iterator k=cgi->state->players.begin(); k!=cgi->state->players.end(); ++k)
  180. {
  181. k->second.fogOfWarMap.resize(map->width, Woff);
  182. for(int g=-Woff; g<map->width+Woff; ++g)
  183. k->second.fogOfWarMap[g].resize(map->height, Hoff);
  184. for(int g=-Woff; g<map->width+Woff; ++g)
  185. for(int h=-Hoff; h<map->height+Hoff; ++h)
  186. k->second.fogOfWarMap[g][h].resize(map->twoLevel+1, 0);
  187. for(int g=-Woff; g<map->width+Woff; ++g)
  188. for(int h=-Hoff; h<map->height+Hoff; ++h)
  189. for(int v=0; v<map->twoLevel+1; ++v)
  190. k->second.fogOfWarMap[g][h][v] = 0;
  191. for(int xd=0; xd<map->width; ++xd) //revealing part of map around heroes
  192. {
  193. for(int yd=0; yd<map->height; ++yd)
  194. {
  195. for(int ch=0; ch<k->second.heroes.size(); ++ch)
  196. {
  197. int deltaX = (k->second.heroes[ch]->getPosition(false).x-xd)*(k->second.heroes[ch]->getPosition(false).x-xd);
  198. int deltaY = (k->second.heroes[ch]->getPosition(false).y-yd)*(k->second.heroes[ch]->getPosition(false).y-yd);
  199. if(deltaX+deltaY<k->second.heroes[ch]->getSightDistance()*k->second.heroes[ch]->getSightDistance())
  200. k->second.fogOfWarMap[xd][yd][k->second.heroes[ch]->getPosition(false).z] = 1;
  201. }
  202. }
  203. }
  204. }
  205. /****************************TOWNS************************************************/
  206. for (int i=0;i<map->towns.size();i++)
  207. {
  208. CGTownInstance * vti =(map->towns[i]);
  209. if (vti->name.length()==0) // if town hasn't name we draw it
  210. vti->name=vti->town->names[rand()%vti->town->names.size()];
  211. if(vti->builtBuildings.find(-50)!=vti->builtBuildings.end()) //give standard set of buildings
  212. {
  213. vti->builtBuildings.erase(-50);
  214. vti->builtBuildings.insert(10);
  215. vti->builtBuildings.insert(5);
  216. vti->builtBuildings.insert(30);
  217. if(rand()%2)
  218. vti->builtBuildings.insert(31);
  219. }
  220. cgi->state->players[vti->getOwner()].towns.push_back(vti);
  221. }
  222. for(std::map<int, PlayerState>::iterator k=cgi->state->players.begin(); k!=cgi->state->players.end(); ++k)
  223. {
  224. if(k->first==-1 || k->first==255)
  225. continue;
  226. for(int xd=0; xd<map->width; ++xd) //revealing part of map around towns
  227. {
  228. for(int yd=0; yd<map->height; ++yd)
  229. {
  230. for(int ch=0; ch<k->second.towns.size(); ++ch)
  231. {
  232. int deltaX = (k->second.towns[ch]->pos.x-xd)*(k->second.towns[ch]->pos.x-xd);
  233. int deltaY = (k->second.towns[ch]->pos.y-yd)*(k->second.towns[ch]->pos.y-yd);
  234. if(deltaX+deltaY<k->second.towns[ch]->getSightDistance()*k->second.towns[ch]->getSightDistance())
  235. k->second.fogOfWarMap[xd][yd][k->second.towns[ch]->pos.z] = 1;
  236. }
  237. }
  238. }
  239. //init visiting heroes
  240. for(int l=0; l<k->second.heroes.size();l++)
  241. {
  242. for(int m=0; m<k->second.towns.size();m++)
  243. {
  244. int3 vistile = k->second.towns[m]->pos; vistile.x--; //tile next to the entrance
  245. if(vistile == k->second.heroes[l]->pos)
  246. {
  247. k->second.towns[m]->visitingHero = k->second.heroes[l];
  248. break;
  249. }
  250. }
  251. }
  252. }
  253. /****************************SCRIPTS************************************************/
  254. std::map<int, std::map<std::string, CObjectScript*> > * skrypty = &cgi->state->objscr; //alias for easier access
  255. /****************************C++ OBJECT SCRIPTS************************************************/
  256. std::map<int,CCPPObjectScript*> scripts;
  257. CScriptCallback * csc = new CScriptCallback();
  258. csc->gs = cgi->state;
  259. handleCPPObjS(&scripts,new CVisitableOPH(csc));
  260. handleCPPObjS(&scripts,new CVisitableOPW(csc));
  261. handleCPPObjS(&scripts,new CPickable(csc));
  262. handleCPPObjS(&scripts,new CMines(csc));
  263. handleCPPObjS(&scripts,new CTownScript(csc));
  264. handleCPPObjS(&scripts,new CHeroScript(csc));
  265. handleCPPObjS(&scripts,new CMonsterS(csc));
  266. handleCPPObjS(&scripts,new CCreatureGen(csc));
  267. //created map
  268. /****************************LUA OBJECT SCRIPTS************************************************/
  269. std::vector<std::string> * lf = CLuaHandler::searchForScripts("scripts/lua/objects"); //files
  270. for (int i=0; i<lf->size(); i++)
  271. {
  272. try
  273. {
  274. std::vector<std::string> * temp = CLuaHandler::functionList((*lf)[i]);
  275. CLuaObjectScript * objs = new CLuaObjectScript((*lf)[i]);
  276. CLuaCallback::registerFuncs(objs->is);
  277. //objs
  278. for (int j=0; j<temp->size(); j++)
  279. {
  280. int obid ; //obj ID
  281. int dspos = (*temp)[j].find_first_of('_');
  282. obid = atoi((*temp)[j].substr(dspos+1,(*temp)[j].size()-dspos-1).c_str());
  283. std::string fname = (*temp)[j].substr(0,dspos);
  284. if (skrypty->find(obid)==skrypty->end())
  285. skrypty->insert(std::pair<int, std::map<std::string, CObjectScript*> >(obid,std::map<std::string,CObjectScript*>()));
  286. (*skrypty)[obid].insert(std::pair<std::string, CObjectScript*>(fname,objs));
  287. }
  288. delete temp;
  289. }HANDLE_EXCEPTION
  290. }
  291. /****************************INITIALIZING OBJECT SCRIPTS************************************************/
  292. std::string temps("newObject");
  293. for (int i=0; i<map->objects.size(); i++)
  294. {
  295. //c++ scripts
  296. if (scripts.find(map->objects[i]->ID) != scripts.end())
  297. {
  298. map->objects[i]->state = scripts[map->objects[i]->ID];
  299. map->objects[i]->state->newObject(map->objects[i]);
  300. }
  301. else
  302. {
  303. map->objects[i]->state = NULL;
  304. }
  305. // lua scripts
  306. if(cgi->state->checkFunc(map->objects[i]->ID,temps))
  307. (*skrypty)[map->objects[i]->ID][temps]->newObject(map->objects[i]);
  308. }
  309. delete lf;
  310. }
  311. int _tmain(int argc, _TCHAR* argv[])
  312. {
  313. srand ( time(NULL) );
  314. CPG=NULL;
  315. atexit(SDL_Quit);
  316. CGameInfo * cgi = CGI = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  317. //CLuaHandler luatest;
  318. //luatest.test();
  319. //CBIKHandler cb;
  320. //cb.open("CSECRET.BIK");
  321. std::cout << "Starting... " << std::endl;
  322. THC timeHandler tmh, total, pomtime;
  323. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO/*|SDL_INIT_EVENTTHREAD*/)==0)
  324. {
  325. screen = SDL_SetVideoMode(800,600,24,SDL_SWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/); //initializing important global surface
  326. THC std::cout<<"\tInitializing screen: "<<pomtime.getDif()<<std::endl;
  327. SDL_WM_SetCaption(NAME,""); //set window title
  328. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  329. int rmask = 0xff000000;int gmask = 0x00ff0000;int bmask = 0x0000ff00;int amask = 0x000000ff;
  330. #else
  331. int rmask = 0x000000ff; int gmask = 0x0000ff00; int bmask = 0x00ff0000; int amask = 0xff000000;
  332. #endif
  333. CSDL_Ext::std32bppSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 1, 32, rmask, gmask, bmask, amask);
  334. THC std::cout<<"\tInitializing minors: "<<pomtime.getDif()<<std::endl;
  335. TTF_Init();
  336. TNRB16 = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  337. GEOR13 = TTF_OpenFont("Fonts\\georgia.ttf",13);
  338. GEOR16 = TTF_OpenFont("Fonts\\georgia.ttf",16);
  339. GEORXX = TTF_OpenFont("Fonts\\tnrb.ttf",22);
  340. GEORM = TTF_OpenFont("Fonts\\georgia.ttf",10);
  341. atexit(TTF_Quit);
  342. THC std::cout<<"\tInitializing fonts: "<<pomtime.getDif()<<std::endl;
  343. CMusicHandler * mush = new CMusicHandler; //initializing audio
  344. mush->initMusics();
  345. //audio initialized
  346. cgi->consoleh = new CConsoleHandler;
  347. cgi->mush = mush;
  348. cgi->curh = new CCursorHandler;
  349. THC std::cout<<"\tInitializing sound and cursor: "<<pomtime.getDif()<<std::endl;
  350. THC std::cout<<"Initializing screen, fonts and sound handling: "<<tmh.getDif()<<std::endl;
  351. CDefHandler::Spriteh = cgi->spriteh = new CLodHandler();
  352. cgi->spriteh->init("Data\\H3sprite.lod","Sprites");
  353. BitmapHandler::bitmaph = cgi->bitmaph = new CLodHandler;
  354. cgi->bitmaph->init("Data\\H3bitmap.lod","Data");
  355. THC std::cout<<"Loading .lod files: "<<tmh.getDif()<<std::endl;
  356. initDLL(cgi->bitmaph);
  357. THC std::cout<<"Initializing VCMI_Lib: "<<tmh.getDif()<<std::endl;
  358. //cgi->curh->initCursor();
  359. //cgi->curh->showGraphicCursor();
  360. pomtime.getDif();
  361. cgi->screenh = new CScreenHandler;
  362. cgi->screenh->initScreen();
  363. THC std::cout<<"\tScreen handler: "<<pomtime.getDif()<<std::endl;
  364. cgi->townh = new CTownHandler;
  365. cgi->townh->loadNames();
  366. THC std::cout<<"\tTown handler: "<<pomtime.getDif()<<std::endl;
  367. CAbilityHandler * abilh = new CAbilityHandler;
  368. abilh->loadAbilities();
  369. cgi->abilh = abilh;
  370. THC std::cout<<"\tAbility handler: "<<pomtime.getDif()<<std::endl;
  371. CHeroHandler * heroh = new CHeroHandler;
  372. heroh->loadHeroes();
  373. heroh->loadPortraits();
  374. cgi->heroh = heroh;
  375. THC std::cout<<"\tHero handler: "<<pomtime.getDif()<<std::endl;
  376. THC std::cout<<"Preparing first handlers: "<<tmh.getDif()<<std::endl;
  377. pomtime.getDif();
  378. graphics = new Graphics();
  379. THC std::cout<<"\tMain graphics: "<<tmh.getDif()<<std::endl;
  380. std::vector<CDefHandler **> animacje;
  381. for(std::vector<CHeroClass *>::iterator i = cgi->heroh->heroClasses.begin();i!=cgi->heroh->heroClasses.end();i++)
  382. animacje.push_back(&((*i)->*(&CHeroClass::moveAnim)));
  383. graphics->loadHeroAnim(animacje);
  384. THC std::cout<<"\tHero animations: "<<tmh.getDif()<<std::endl;
  385. THC std::cout<<"Initializing game graphics: "<<tmh.getDif()<<std::endl;
  386. //colors initialization
  387. SDL_Color p;
  388. p.unused = 0;
  389. p.r = 0xff; p.g = 0x0; p.b = 0x0; //red
  390. cgi->playerColors.push_back(p); //red
  391. p.r = 0x31; p.g = 0x52; p.b = 0xff; //blue
  392. cgi->playerColors.push_back(p); //blue
  393. p.r = 0x9c; p.g = 0x73; p.b = 0x52;//tan
  394. cgi->playerColors.push_back(p);//tan
  395. p.r = 0x42; p.g = 0x94; p.b = 0x29; //green
  396. cgi->playerColors.push_back(p); //green
  397. p.r = 0xff; p.g = 0x84; p.b = 0x0; //orange
  398. cgi->playerColors.push_back(p); //orange
  399. p.r = 0x8c; p.g = 0x29; p.b = 0xa5; //purple
  400. cgi->playerColors.push_back(p); //purple
  401. p.r = 0x09; p.g = 0x9c; p.b = 0xa5;//teal
  402. cgi->playerColors.push_back(p);//teal
  403. p.r = 0xc6; p.g = 0x7b; p.b = 0x8c;//pink
  404. cgi->playerColors.push_back(p);//pink
  405. p.r = 0x84; p.g = 0x84; p.b = 0x84;//gray
  406. cgi->neutralColor = p;//gray
  407. //colors initialized
  408. //palette initialization
  409. std::string pals = cgi->bitmaph->getTextFile("PLAYERS.PAL");
  410. int startPoint = 24; //beginning byte; used to read
  411. for(int i=0; i<256; ++i)
  412. {
  413. SDL_Color col;
  414. col.r = pals[startPoint++];
  415. col.g = pals[startPoint++];
  416. col.b = pals[startPoint++];
  417. col.unused = pals[startPoint++];
  418. playerColorPalette[i] = col;
  419. }
  420. //palette initialized
  421. THC std::cout<<"Preparing players' colours: "<<tmh.getDif()<<std::endl;
  422. CMessage::init();
  423. cgi->generaltexth = new CGeneralTextHandler;
  424. cgi->generaltexth->load();
  425. THC std::cout<<"Preparing more handlers: "<<tmh.getDif()<<std::endl;
  426. CPreGame * cpg = new CPreGame(); //main menu and submenus
  427. THC std::cout<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  428. THC std::cout<<"Initialization of VCMI (togeter): "<<total.getDif()<<std::endl;
  429. cpg->mush = mush;
  430. cgi->scenarioOps = cpg->runLoop();
  431. THC tmh.getDif();pomtime.getDif();
  432. CArtHandler * arth = new CArtHandler;
  433. arth->loadArtifacts();
  434. cgi->arth = arth;
  435. THC std::cout<<"\tArtifact handler: "<<pomtime.getDif()<<std::endl;
  436. CCreatureHandler * creh = new CCreatureHandler();
  437. creh->loadCreatures();
  438. cgi->creh = creh;
  439. THC std::cout<<"\tCreature handler: "<<pomtime.getDif()<<std::endl;
  440. CSpellHandler * spellh = new CSpellHandler;
  441. spellh->loadSpells();
  442. cgi->spellh = spellh;
  443. THC std::cout<<"\tSpell handler: "<<pomtime.getDif()<<std::endl;
  444. CBuildingHandler * buildh = new CBuildingHandler;
  445. buildh->loadBuildings();
  446. cgi->buildh = buildh;
  447. THC std::cout<<"\tBuilding handler: "<<pomtime.getDif()<<std::endl;
  448. CObjectHandler * objh = new CObjectHandler;
  449. objh->loadObjects();
  450. cgi->objh = objh;
  451. THC std::cout<<"\tObject handler: "<<pomtime.getDif()<<std::endl;
  452. cgi->dobjinfo = new CDefObjInfoHandler;
  453. cgi->dobjinfo->load();
  454. THC std::cout<<"\tDef information handler: "<<pomtime.getDif()<<std::endl;
  455. cgi->state = new CGameState();
  456. cgi->state->players = std::map<int, PlayerState>();
  457. THC std::cout<<"\tGamestate: "<<pomtime.getDif()<<std::endl;
  458. cgi->pathf = new CPathfinder();
  459. THC std::cout<<"\tPathfinder: "<<pomtime.getDif()<<std::endl;
  460. cgi->consoleh->cb = new CCallback(cgi->state,-1);
  461. cgi->consoleh->runConsole();
  462. THC std::cout<<"\tCallback and console: "<<pomtime.getDif()<<std::endl;
  463. THC std::cout<<"Handlers initailization (together): "<<tmh.getDif()<<std::endl;
  464. std::string mapname = cpg->ourScenSel->mapsel.ourMaps[cpg->ourScenSel->mapsel.selected].filename;
  465. std::cout<<"Opening map file: "<<mapname<<"\t\t"<<std::flush;
  466. gzFile map = gzopen(mapname.c_str(),"rb");
  467. std::vector<unsigned char> mapstr; int pom;
  468. while((pom=gzgetc(map))>=0)
  469. {
  470. mapstr.push_back(pom);
  471. }
  472. gzclose(map);
  473. unsigned char *initTable = new unsigned char[mapstr.size()];
  474. for(int ss=0; ss<mapstr.size(); ++ss)
  475. {
  476. initTable[ss] = mapstr[ss];
  477. }
  478. std::cout<<"done."<<std::endl;
  479. CAmbarCendamo * ac = new CAmbarCendamo(initTable);
  480. THC std::cout<<"Reading file: "<<tmh.getDif()<<std::endl;
  481. ac->deh3m();
  482. THC std::cout<<"Detecting file (together): "<<tmh.getDif()<<std::endl;
  483. CMapHandler * mh = new CMapHandler();
  484. cgi->mh = mh;
  485. mh->map = &ac->map;
  486. THC std::cout<<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  487. mh->loadDefs();
  488. THC std::cout<<"Reading terrain defs: "<<tmh.getDif()<<std::endl;
  489. mh->init();
  490. THC std::cout<<"Initializing mapHandler (together): "<<tmh.getDif()<<std::endl;
  491. initGameState(&ac->map,cgi);
  492. THC std::cout<<"Initializing GameState (together): "<<tmh.getDif()<<std::endl;
  493. for (int i=0; i<cgi->scenarioOps.playerInfos.size();i++) //initializing interfaces
  494. {
  495. if(!cgi->scenarioOps.playerInfos[i].human)
  496. cgi->playerint.push_back(static_cast<CGameInterface*>(CAIHandler::getNewAI(new CCallback(cgi->state,cgi->scenarioOps.playerInfos[i].color),"EmptyAI.dll")));
  497. else
  498. {
  499. cgi->state->currentPlayer=cgi->scenarioOps.playerInfos[i].color;
  500. cgi->playerint.push_back(new CPlayerInterface(cgi->scenarioOps.playerInfos[i].color,i));
  501. ((CPlayerInterface*)(cgi->playerint[i]))->init(new CCallback(cgi->state,cgi->scenarioOps.playerInfos[i].color));
  502. }
  503. }
  504. ///claculating FoWs for minimap
  505. /****************************Minimaps' FoW******************************************/
  506. for(int g=0; g<cgi->playerint.size(); ++g)
  507. {
  508. if(!cgi->playerint[g]->human)
  509. continue;
  510. CMinimap & mm = ((CPlayerInterface*)cgi->playerint[g])->adventureInt->minimap;
  511. int mw = mm.map[0]->w, mh = mm.map[0]->h,
  512. wo = mw/CGI->mh->sizes.x, ho = mh/CGI->mh->sizes.y;
  513. for(int d=0; d<cgi->mh->map->twoLevel+1; ++d)
  514. {
  515. SDL_Surface * pt = CSDL_Ext::newSurface(mm.pos.w, mm.pos.h, CSDL_Ext::std32bppSurface);
  516. for (int i=0; i<mw; i++)
  517. {
  518. for (int j=0; j<mh; j++)
  519. {
  520. int3 pp( ((i*CGI->mh->sizes.x)/mw), ((j*CGI->mh->sizes.y)/mh), d );
  521. if ( !((CPlayerInterface*)cgi->playerint[g])->cb->isVisible(pp) )
  522. {
  523. CSDL_Ext::SDL_PutPixelWithoutRefresh(pt,i,j,0,0,0);
  524. }
  525. }
  526. }
  527. CSDL_Ext::update(pt);
  528. mm.FoW.push_back(pt);
  529. }
  530. }
  531. while(1) //main game loop, one execution per turn
  532. {
  533. cgi->consoleh->cb->newTurn();
  534. for (int i=0;i<cgi->playerint.size();i++)
  535. {
  536. cgi->state->currentPlayer=cgi->playerint[i]->playerID;
  537. try
  538. {
  539. cgi->playerint[i]->yourTurn();
  540. }HANDLE_EXCEPTION
  541. }
  542. }
  543. }
  544. else
  545. {
  546. printf("Something was wrong: %s/n", SDL_GetError());
  547. return -1;
  548. }
  549. }