CMT.cpp 27 KB

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