CMT.cpp 31 KB

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