CMT.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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\CSemiLodHandler.h"
  30. #include "hch\CLodHandler.h"
  31. #include "hch\CDefHandler.h"
  32. #include "hch\CSndHandler.h"
  33. #include "hch\CTownHandler.h"
  34. #include "hch\CDefObjInfoHandler.h"
  35. #include "hch\CAmbarCendamo.h"
  36. #include "mapHandler.h"
  37. #include "global.h"
  38. #include "CPreGame.h"
  39. #include "hch\CGeneralTextHandler.h"
  40. #include "CConsoleHandler.h"
  41. #include "CCursorHandler.h"
  42. #include "CScreenHandler.h"
  43. #include "CPathfinder.h"
  44. #include "CGameState.h"
  45. #include "CCallback.h"
  46. #include "CLuaHandler.h"
  47. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  48. # include <fcntl.h>
  49. # include <io.h>
  50. # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  51. #else
  52. # define SET_BINARY_MODE(file)
  53. #endif
  54. #define CHUNK 16384
  55. const char * NAME = "VCMI 0.3 \"Tol Galen\"";
  56. SDL_Surface * ekran, * screen, * screen2;
  57. TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM;
  58. void initGameState(CGameInfo * cgi)
  59. {
  60. cgi->state->day=1;
  61. /*********creating players entries in gs****************************************/
  62. for (int i=0; i<cgi->scenarioOps.playerInfos.size();i++)
  63. {
  64. std::pair<int,PlayerState> ins(cgi->scenarioOps.playerInfos[i].color,PlayerState());
  65. cgi->state->players.insert(ins);
  66. }
  67. /******************RESOURCES****************************************************/
  68. //TODO: zeby komputer dostawal inaczej niz gracz
  69. std::vector<int> startres;
  70. std::ifstream tis("config/startres.txt");
  71. int k;
  72. for (int j=0;j<cgi->scenarioOps.difficulty;j++)
  73. {
  74. tis >> k;
  75. for (int z=0;z<RESOURCE_QUANTITY;z++)
  76. tis>>k;
  77. }
  78. tis >> k;
  79. for (int i=0;i<RESOURCE_QUANTITY;i++)
  80. {
  81. tis >> k;
  82. startres.push_back(k);
  83. }
  84. tis.close();
  85. for (std::map<int,PlayerState>::iterator i = cgi->state->players.begin(); i!=cgi->state->players.end(); i++)
  86. {
  87. (*i).second.resources.resize(RESOURCE_QUANTITY);
  88. for (int x=0;x<RESOURCE_QUANTITY;x++)
  89. (*i).second.resources[x] = startres[x];
  90. }
  91. /*************************HEROES************************************************/
  92. for (int i=0; i<cgi->heroh->heroInstances.size();i++) //heroes instances
  93. {
  94. if (!cgi->heroh->heroInstances[i]->type || cgi->heroh->heroInstances[i]->owner<0)
  95. continue;
  96. CHeroInstance * vhi = new CHeroInstance();
  97. *vhi=*(cgi->heroh->heroInstances[i]);
  98. if (!vhi->level)
  99. {
  100. vhi->exp=40+rand()%50;
  101. vhi->level = 1;
  102. }
  103. if (vhi->level>1) ;//TODO dodac um dr, ale potrzebne los
  104. if ((!vhi->primSkills.size()) || (vhi->primSkills[0]<0))
  105. {
  106. if (vhi->primSkills.size()<PRIMARY_SKILLS)
  107. vhi->primSkills.resize(PRIMARY_SKILLS);
  108. vhi->primSkills[0] = vhi->type->heroClass->initialAttack;
  109. vhi->primSkills[1] = vhi->type->heroClass->initialDefence;
  110. vhi->primSkills[2] = vhi->type->heroClass->initialPower;
  111. vhi->primSkills[3] = vhi->type->heroClass->initialKnowledge;
  112. vhi->mana = vhi->primSkills[3]*10;
  113. }
  114. if (!vhi->name.length())
  115. {
  116. vhi->name = vhi->type->name;
  117. }
  118. if (!vhi->biography.length())
  119. {
  120. vhi->biography = vhi->type->biography;
  121. }
  122. if (vhi->portrait < 0)
  123. vhi->portrait = vhi->type->ID;
  124. //initial army
  125. if (!vhi->army.slots.size())
  126. {
  127. vhi->army.slots[0].first = &(cgi->creh->creatures[(cgi->creh->nameToID[vhi->type->refType1stack])]);
  128. vhi->army.slots[0].second = (rand()%(vhi->type->high1stack-vhi->type->low1stack))+vhi->type->low1stack;
  129. vhi->army.slots[1].first = &(cgi->creh->creatures[(cgi->creh->nameToID[vhi->type->refType2stack])]);
  130. vhi->army.slots[1].second = (rand()%(vhi->type->high2stack-vhi->type->low2stack))+vhi->type->low2stack;
  131. vhi->army.slots[2].first = &(cgi->creh->creatures[(cgi->creh->nameToID[vhi->type->refType3stack])]);
  132. vhi->army.slots[2].second = (rand()%(vhi->type->high3stack-vhi->type->low3stack))+vhi->type->low3stack;
  133. }
  134. cgi->state->players[vhi->owner].heroes.push_back(vhi);
  135. }
  136. /*************************FOG**OF**WAR******************************************/
  137. for(std::map<int, PlayerState>::iterator k=cgi->state->players.begin(); k!=cgi->state->players.end(); ++k)
  138. {
  139. k->second.fogOfWarMap.resize(cgi->ac->map.width, Woff);
  140. for(int g=-Woff; g<cgi->ac->map.width+Woff; ++g)
  141. k->second.fogOfWarMap[g].resize(cgi->ac->map.height, Hoff);
  142. for(int g=-Woff; g<cgi->ac->map.width+Woff; ++g)
  143. for(int h=-Hoff; h<cgi->ac->map.height+Hoff; ++h)
  144. k->second.fogOfWarMap[g][h].resize(cgi->ac->map.twoLevel+1, 0);
  145. for(int g=-Woff; g<cgi->ac->map.width+Woff; ++g)
  146. for(int h=-Hoff; h<cgi->ac->map.height+Hoff; ++h)
  147. for(int v=0; v<cgi->ac->map.twoLevel+1; ++v)
  148. k->second.fogOfWarMap[g][h][v] = 0;
  149. for(int xd=0; xd<cgi->ac->map.width; ++xd) //revealing part of map around heroes
  150. {
  151. for(int yd=0; yd<cgi->ac->map.height; ++yd)
  152. {
  153. for(int ch=0; ch<k->second.heroes.size(); ++ch)
  154. {
  155. int deltaX = (k->second.heroes[ch]->getPosition(false).x-xd)*(k->second.heroes[ch]->getPosition(false).x-xd);
  156. int deltaY = (k->second.heroes[ch]->getPosition(false).y-yd)*(k->second.heroes[ch]->getPosition(false).y-yd);
  157. if(deltaX+deltaY<=k->second.heroes[ch]->getSightDistance()*k->second.heroes[ch]->getSightDistance())
  158. k->second.fogOfWarMap[xd][yd][k->second.heroes[ch]->getPosition(false).z] = 1;
  159. }
  160. }
  161. }
  162. }
  163. /****************************TOWNS************************************************/
  164. for (int i=0;i<cgi->townh->townInstances.size();i++)
  165. {
  166. CTownInstance * vti = new CTownInstance();
  167. (*vti)=*(cgi->townh->townInstances[i]);
  168. if (vti->name.length()==0) // if town hasn't name we draw it
  169. vti->name=vti->town->names[rand()%vti->town->names.size()];
  170. cgi->state->players[vti->owner].towns.push_back(vti);
  171. }
  172. for(std::map<int, PlayerState>::iterator k=cgi->state->players.begin(); k!=cgi->state->players.end(); ++k)
  173. {
  174. if(k->first==-1 || k->first==255)
  175. continue;
  176. for(int xd=0; xd<cgi->ac->map.width; ++xd) //revealing part of map around towns
  177. {
  178. for(int yd=0; yd<cgi->ac->map.height; ++yd)
  179. {
  180. for(int ch=0; ch<k->second.towns.size(); ++ch)
  181. {
  182. int deltaX = (k->second.towns[ch]->pos.x-xd)*(k->second.towns[ch]->pos.x-xd);
  183. int deltaY = (k->second.towns[ch]->pos.y-yd)*(k->second.towns[ch]->pos.y-yd);
  184. if(deltaX+deltaY<=k->second.towns[ch]->getSightDistance()*k->second.towns[ch]->getSightDistance())
  185. k->second.fogOfWarMap[xd][yd][k->second.towns[ch]->pos.z] = 1;
  186. }
  187. }
  188. }
  189. }
  190. }
  191. int _tmain(int argc, _TCHAR* argv[])
  192. {
  193. CLuaHandler luatest;
  194. luatest.test();
  195. //CBIKHandler cb;
  196. //cb.open("CSECRET.BIK");
  197. THC timeHandler tmh;
  198. THC tmh.getDif();
  199. int xx=0, yy=0, zz=0;
  200. SDL_Event sEvent;
  201. srand ( time(NULL) );
  202. SDL_Surface *temp;
  203. std::vector<SDL_Surface*> Sprites;
  204. float i;
  205. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO/*|SDL_INIT_EVENTTHREAD*/)==0)
  206. {
  207. CPG=NULL;
  208. TTF_Init();
  209. atexit(TTF_Quit);
  210. atexit(SDL_Quit);
  211. //TNRB = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  212. TNRB16 = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  213. //TNR = TTF_OpenFont("Fonts\\tnr.ttf",10);
  214. GEOR13 = TTF_OpenFont("Fonts\\georgia.ttf",13);
  215. GEORXX = TTF_OpenFont("Fonts\\tnrb.ttf",22);
  216. GEORM = TTF_OpenFont("Fonts\\georgia.ttf",10);
  217. CMusicHandler * mush = new CMusicHandler; //initializing audio
  218. mush->initMusics();
  219. //audio initialized
  220. /*if(Mix_PlayMusic(mush->mainMenuWoG, -1)==-1) //uncomment this fragment to have music
  221. {
  222. printf("Mix_PlayMusic: %s\n", Mix_GetError());
  223. // well, there's no music, but most games don't break without music...
  224. }*/
  225. screen2 = SDL_SetVideoMode(800,600,24,SDL_SWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/);
  226. screen = SDL_ConvertSurface(screen2, screen2->format, SDL_SWSURFACE);
  227. ekran = screen;
  228. SDL_WM_SetCaption(NAME,""); //set window title
  229. CGameInfo * cgi = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  230. CGameInfo::mainObj = cgi;
  231. cgi->consoleh = new CConsoleHandler;
  232. cgi->mush = mush;
  233. cgi->curh = new CCursorHandler;
  234. THC std::cout<<"Initializing screen, fonts and sound handling: "<<tmh.getDif()<<std::endl;
  235. cgi->spriteh = new CLodHandler;
  236. cgi->spriteh->init(std::string("Data\\H3sprite.lod"));
  237. cgi->bitmaph = new CLodHandler;
  238. cgi->bitmaph->init(std::string("Data\\H3bitmap.lod"));
  239. THC std::cout<<"Loading .lod files: "<<tmh.getDif()<<std::endl;
  240. cgi->curh->initCursor();
  241. cgi->curh->showGraphicCursor();
  242. cgi->screenh = new CScreenHandler;
  243. cgi->screenh->initScreen();
  244. //colors initialization
  245. SDL_Color p;
  246. p.unused = 0;
  247. p.r = 0xff; p.g = 0x0; p.b = 0x0; //red
  248. cgi->playerColors.push_back(p); //red
  249. p.r = 0x31; p.g = 0x52; p.b = 0xff; //blue
  250. cgi->playerColors.push_back(p); //blue
  251. p.r = 0x9c; p.g = 0x73; p.b = 0x52;//tan
  252. cgi->playerColors.push_back(p);//tan
  253. p.r = 0x42; p.g = 0x94; p.b = 0x29; //green
  254. cgi->playerColors.push_back(p); //green
  255. p.r = 0xff; p.g = 0x84; p.b = 0x0; //orange
  256. cgi->playerColors.push_back(p); //orange
  257. p.r = 0x8c; p.g = 0x29; p.b = 0xa5; //purple
  258. cgi->playerColors.push_back(p); //purple
  259. p.r = 0x09; p.g = 0x9c; p.b = 0xa5;//teal
  260. cgi->playerColors.push_back(p);//teal
  261. p.r = 0xc6; p.g = 0x7b; p.b = 0x8c;//pink
  262. cgi->playerColors.push_back(p);//pink
  263. p.r = 0x84; p.g = 0x84; p.b = 0x84;//gray
  264. cgi->neutralColor = p;//gray
  265. //colors initialized
  266. CMessage::init();
  267. cgi->townh = new CTownHandler;
  268. cgi->townh->loadNames();
  269. CAbilityHandler * abilh = new CAbilityHandler;
  270. abilh->loadAbilities();
  271. cgi->abilh = abilh;
  272. CHeroHandler * heroh = new CHeroHandler;
  273. heroh->loadHeroes();
  274. heroh->loadPortraits();
  275. cgi->heroh = heroh;
  276. cgi->generaltexth = new CGeneralTextHandler;
  277. cgi->generaltexth->load();
  278. //initializing hero flags
  279. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01L.DEF")); //red
  280. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01G.DEF")); //blue
  281. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01R.DEF")); //tan
  282. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01D.DEF")); //green
  283. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01B.DEF")); //orange
  284. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01P.DEF")); //purple
  285. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01W.DEF")); //teal
  286. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01K.DEF")); //pink
  287. for(int q=0; q<8; ++q)
  288. {
  289. for(int o=0; o<cgi->heroh->flags1[q]->ourImages.size(); ++o)
  290. {
  291. if(cgi->heroh->flags1[q]->ourImages[o].groupNumber==6)
  292. {
  293. for(int e=0; e<8; ++e)
  294. {
  295. Cimage nci;
  296. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags1[q]->ourImages[o+e].bitmap);
  297. nci.groupNumber = 10;
  298. nci.imName = std::string();
  299. cgi->heroh->flags1[q]->ourImages.push_back(nci);
  300. }
  301. o+=8;
  302. }
  303. if(cgi->heroh->flags1[q]->ourImages[o].groupNumber==7)
  304. {
  305. for(int e=0; e<8; ++e)
  306. {
  307. Cimage nci;
  308. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags1[q]->ourImages[o+e].bitmap);
  309. nci.groupNumber = 11;
  310. nci.imName = std::string();
  311. cgi->heroh->flags1[q]->ourImages.push_back(nci);
  312. }
  313. o+=8;
  314. }
  315. if(cgi->heroh->flags1[q]->ourImages[o].groupNumber==8)
  316. {
  317. for(int e=0; e<8; ++e)
  318. {
  319. Cimage nci;
  320. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags1[q]->ourImages[o+e].bitmap);
  321. nci.groupNumber = 12;
  322. nci.imName = std::string();
  323. cgi->heroh->flags1[q]->ourImages.push_back(nci);
  324. }
  325. o+=8;
  326. }
  327. }
  328. for(int ff=80; ff<cgi->heroh->flags1[q]->ourImages.size(); ++ff)
  329. {
  330. CSDL_Ext::fullAlphaTransform(cgi->heroh->flags1[q]->ourImages[ff].bitmap);
  331. }
  332. cgi->heroh->flags1[q]->alphaTransformed = true;
  333. }
  334. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02L.DEF")); //red
  335. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02G.DEF")); //blue
  336. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02R.DEF")); //tan
  337. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02D.DEF")); //green
  338. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02B.DEF")); //orange
  339. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02P.DEF")); //purple
  340. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02W.DEF")); //teal
  341. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02K.DEF")); //pink
  342. for(int q=0; q<8; ++q)
  343. {
  344. for(int o=0; o<cgi->heroh->flags2[q]->ourImages.size(); ++o)
  345. {
  346. if(cgi->heroh->flags2[q]->ourImages[o].groupNumber==6)
  347. {
  348. for(int e=0; e<8; ++e)
  349. {
  350. Cimage nci;
  351. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags2[q]->ourImages[o+e].bitmap);
  352. nci.groupNumber = 10;
  353. nci.imName = std::string();
  354. cgi->heroh->flags2[q]->ourImages.push_back(nci);
  355. }
  356. o+=8;
  357. }
  358. if(cgi->heroh->flags2[q]->ourImages[o].groupNumber==7)
  359. {
  360. for(int e=0; e<8; ++e)
  361. {
  362. Cimage nci;
  363. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags2[q]->ourImages[o+e].bitmap);
  364. nci.groupNumber = 11;
  365. nci.imName = std::string();
  366. cgi->heroh->flags2[q]->ourImages.push_back(nci);
  367. }
  368. o+=8;
  369. }
  370. if(cgi->heroh->flags2[q]->ourImages[o].groupNumber==8)
  371. {
  372. for(int e=0; e<8; ++e)
  373. {
  374. Cimage nci;
  375. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags2[q]->ourImages[o+e].bitmap);
  376. nci.groupNumber = 12;
  377. nci.imName = std::string();
  378. cgi->heroh->flags2[q]->ourImages.push_back(nci);
  379. }
  380. o+=8;
  381. }
  382. }
  383. for(int ff=80; ff<cgi->heroh->flags2[q]->ourImages.size(); ++ff)
  384. {
  385. CSDL_Ext::fullAlphaTransform(cgi->heroh->flags2[q]->ourImages[ff].bitmap);
  386. }
  387. cgi->heroh->flags2[q]->alphaTransformed = true;
  388. }
  389. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03L.DEF")); //red
  390. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03G.DEF")); //blue
  391. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03R.DEF")); //tan
  392. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03D.DEF")); //green
  393. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03B.DEF")); //orange
  394. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03P.DEF")); //purple
  395. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03W.DEF")); //teal
  396. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03K.DEF")); //pink
  397. for(int q=0; q<8; ++q)
  398. {
  399. for(int o=0; o<cgi->heroh->flags3[q]->ourImages.size(); ++o)
  400. {
  401. if(cgi->heroh->flags3[q]->ourImages[o].groupNumber==6)
  402. {
  403. for(int e=0; e<8; ++e)
  404. {
  405. Cimage nci;
  406. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags3[q]->ourImages[o+e].bitmap);
  407. nci.groupNumber = 10;
  408. nci.imName = std::string();
  409. cgi->heroh->flags3[q]->ourImages.push_back(nci);
  410. }
  411. o+=8;
  412. }
  413. if(cgi->heroh->flags3[q]->ourImages[o].groupNumber==7)
  414. {
  415. for(int e=0; e<8; ++e)
  416. {
  417. Cimage nci;
  418. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags3[q]->ourImages[o+e].bitmap);
  419. nci.groupNumber = 11;
  420. nci.imName = std::string();
  421. cgi->heroh->flags3[q]->ourImages.push_back(nci);
  422. }
  423. o+=8;
  424. }
  425. if(cgi->heroh->flags3[q]->ourImages[o].groupNumber==8)
  426. {
  427. for(int e=0; e<8; ++e)
  428. {
  429. Cimage nci;
  430. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags3[q]->ourImages[o+e].bitmap);
  431. nci.groupNumber = 12;
  432. nci.imName = std::string();
  433. cgi->heroh->flags3[q]->ourImages.push_back(nci);
  434. }
  435. o+=8;
  436. }
  437. }
  438. for(int ff=80; ff<cgi->heroh->flags3[q]->ourImages.size(); ++ff)
  439. {
  440. CSDL_Ext::fullAlphaTransform(cgi->heroh->flags3[q]->ourImages[ff].bitmap);
  441. }
  442. cgi->heroh->flags3[q]->alphaTransformed = true;
  443. }
  444. cgi->heroh->flags4.push_back(cgi->spriteh->giveDef("AF00.DEF")); //red
  445. cgi->heroh->flags4.push_back(cgi->spriteh->giveDef("AF01.DEF")); //blue
  446. cgi->heroh->flags4.push_back(cgi->spriteh->giveDef("AF02.DEF")); //tan
  447. cgi->heroh->flags4.push_back(cgi->spriteh->giveDef("AF03.DEF")); //green
  448. cgi->heroh->flags4.push_back(cgi->spriteh->giveDef("AF04.DEF")); //orange
  449. cgi->heroh->flags4.push_back(cgi->spriteh->giveDef("AF05.DEF")); //purple
  450. cgi->heroh->flags4.push_back(cgi->spriteh->giveDef("AF06.DEF")); //teal
  451. cgi->heroh->flags4.push_back(cgi->spriteh->giveDef("AF07.DEF")); //pink
  452. for(int q=0; q<8; ++q)
  453. {
  454. for(int o=0; o<cgi->heroh->flags4[q]->ourImages.size(); ++o)
  455. {
  456. if(cgi->heroh->flags4[q]->ourImages[o].groupNumber==6)
  457. {
  458. for(int e=0; e<8; ++e)
  459. {
  460. Cimage nci;
  461. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags4[q]->ourImages[o+e].bitmap);
  462. nci.groupNumber = 10;
  463. nci.imName = std::string();
  464. cgi->heroh->flags4[q]->ourImages.push_back(nci);
  465. }
  466. o+=8;
  467. }
  468. if(cgi->heroh->flags4[q]->ourImages[o].groupNumber==7)
  469. {
  470. for(int e=0; e<8; ++e)
  471. {
  472. Cimage nci;
  473. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags4[q]->ourImages[o+e].bitmap);
  474. nci.groupNumber = 11;
  475. nci.imName = std::string();
  476. cgi->heroh->flags4[q]->ourImages.push_back(nci);
  477. }
  478. o+=8;
  479. }
  480. if(cgi->heroh->flags4[q]->ourImages[o].groupNumber==8)
  481. {
  482. for(int e=0; e<8; ++e)
  483. {
  484. Cimage nci;
  485. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags4[q]->ourImages[o+e].bitmap);
  486. nci.groupNumber = 12;
  487. nci.imName = std::string();
  488. cgi->heroh->flags4[q]->ourImages.push_back(nci);
  489. }
  490. o+=8;
  491. }
  492. }
  493. for(int o=0; o<cgi->heroh->flags4[q]->ourImages.size(); ++o)
  494. {
  495. if(cgi->heroh->flags4[q]->ourImages[o].groupNumber==1)
  496. {
  497. for(int e=0; e<8; ++e)
  498. {
  499. Cimage nci;
  500. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags4[q]->ourImages[o+e].bitmap);
  501. nci.groupNumber = 13;
  502. nci.imName = std::string();
  503. cgi->heroh->flags4[q]->ourImages.push_back(nci);
  504. }
  505. o+=8;
  506. }
  507. if(cgi->heroh->flags4[q]->ourImages[o].groupNumber==2)
  508. {
  509. for(int e=0; e<8; ++e)
  510. {
  511. Cimage nci;
  512. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags4[q]->ourImages[o+e].bitmap);
  513. nci.groupNumber = 14;
  514. nci.imName = std::string();
  515. cgi->heroh->flags4[q]->ourImages.push_back(nci);
  516. }
  517. o+=8;
  518. }
  519. if(cgi->heroh->flags4[q]->ourImages[o].groupNumber==3)
  520. {
  521. for(int e=0; e<8; ++e)
  522. {
  523. Cimage nci;
  524. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags4[q]->ourImages[o+e].bitmap);
  525. nci.groupNumber = 15;
  526. nci.imName = std::string();
  527. cgi->heroh->flags4[q]->ourImages.push_back(nci);
  528. }
  529. o+=8;
  530. }
  531. }
  532. for(int ff=80; ff<cgi->heroh->flags4[q]->ourImages.size(); ++ff)
  533. {
  534. CSDL_Ext::fullAlphaTransform(cgi->heroh->flags4[q]->ourImages[ff].bitmap);
  535. }
  536. cgi->heroh->flags4[q]->alphaTransformed = true;
  537. }
  538. //hero flags initialized
  539. THC std::cout<<"Preparing first handlers: "<<tmh.getDif()<<std::endl;
  540. CPreGame * cpg = new CPreGame(); //main menu and submenus
  541. THC std::cout<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  542. cpg->mush = mush;
  543. cgi->scenarioOps = cpg->runLoop();
  544. THC tmh.getDif();
  545. cgi->sspriteh = new CSemiLodHandler();
  546. cgi->sspriteh->openLod("H3sprite.lod");
  547. CArtHandler * arth = new CArtHandler;
  548. arth->loadArtifacts();
  549. cgi->arth = arth;
  550. CCreatureHandler * creh = new CCreatureHandler;
  551. creh->loadCreatures();
  552. cgi->creh = creh;
  553. CSpellHandler * spellh = new CSpellHandler;
  554. spellh->loadSpells();
  555. cgi->spellh = spellh;
  556. CBuildingHandler * buildh = new CBuildingHandler;
  557. buildh->loadBuildings();
  558. cgi->buildh = buildh;
  559. CObjectHandler * objh = new CObjectHandler;
  560. objh->loadObjects();
  561. cgi->objh = objh;
  562. cgi->dobjinfo = new CDefObjInfoHandler;
  563. cgi->dobjinfo->load();
  564. cgi->state = new CGameState();
  565. cgi->state->players = std::map<int, PlayerState>();
  566. cgi->pathf = new CPathfinder();
  567. cgi->consoleh->cb = new CCallback(cgi->state,-1);
  568. cgi->consoleh->runConsole();
  569. THC std::cout<<"Handlers initailization: "<<tmh.getDif()<<std::endl;
  570. std::string mapname;
  571. if(CPG->ourScenSel->mapsel.selected==0) CPG->ourScenSel->mapsel.selected = 1; //only for tests
  572. if (CPG) mapname = CPG->ourScenSel->mapsel.ourMaps[CPG->ourScenSel->mapsel.selected].filename;
  573. gzFile map = gzopen(mapname.c_str(),"rb");
  574. std::string mapstr;int pom;
  575. while((pom=gzgetc(map))>=0)
  576. {
  577. mapstr+=pom;
  578. }
  579. gzclose(map);
  580. unsigned char *initTable = new unsigned char[mapstr.size()];
  581. for(int ss=0; ss<mapstr.size(); ++ss)
  582. {
  583. initTable[ss] = mapstr[ss];
  584. }
  585. #define CHOOSE
  586. #ifdef CHOOSE
  587. CAmbarCendamo * ac = new CAmbarCendamo(initTable); //4gryf
  588. #else
  589. CAmbarCendamo * ac = new CAmbarCendamo("RoEtest"); //4gryf
  590. #endif
  591. CMapHeader * mmhh = new CMapHeader(ac->bufor); //czytanie nag³ówka
  592. cgi->ac = ac;
  593. THC std::cout<<"Reading file: "<<tmh.getDif()<<std::endl;
  594. ac->deh3m();
  595. THC std::cout<<"Detecting file (together): "<<tmh.getDif()<<std::endl;
  596. ac->loadDefs();
  597. THC std::cout<<"Reading terrain defs: "<<tmh.getDif()<<std::endl;
  598. CMapHandler * mh = new CMapHandler();
  599. cgi->mh = mh;
  600. mh->reader = ac;
  601. THC std::cout<<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  602. mh->init();
  603. THC std::cout<<"Initializing mapHandler: "<<tmh.getDif()<<std::endl;
  604. initGameState(cgi);
  605. THC std::cout<<"Initializing GameState: "<<tmh.getDif()<<std::endl;
  606. for (int i=0; i<cgi->scenarioOps.playerInfos.size();i++) //initializing interfaces
  607. {
  608. //TODO: uncomment when AI will be done
  609. //if(cgi->scenarioOps.playerInfos[i].name=="AI")
  610. // cgi->playerint.push_back(new CGlobalAI());
  611. //else
  612. {
  613. cgi->state->currentPlayer=cgi->scenarioOps.playerInfos[i].color;
  614. cgi->playerint.push_back(new CPlayerInterface(cgi->scenarioOps.playerInfos[i].color,i));
  615. ((CPlayerInterface*)(cgi->playerint[i]))->init(new CCallback(cgi->state,cgi->scenarioOps.playerInfos[i].color));
  616. }
  617. }
  618. while(1) //main game loop, one execution per turn
  619. {
  620. cgi->consoleh->cb->newTurn();
  621. for (int i=0;i<cgi->playerint.size();i++)
  622. {
  623. cgi->state->currentPlayer=cgi->playerint[i]->playerID;
  624. cgi->playerint[i]->yourTurn();
  625. }
  626. }
  627. }
  628. else
  629. {
  630. printf("Something was wrong: %s/n", SDL_GetError());
  631. return -1;
  632. }
  633. }