CMT.cpp 21 KB

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