CMT.cpp 20 KB

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