CMT.cpp 20 KB

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