CMT.cpp 32 KB

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