CMT.cpp 26 KB

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