CMT.cpp 21 KB

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