CMT.cpp 29 KB

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