CMT.cpp 28 KB

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