CMT.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
  47. # include <fcntl.h>
  48. # include <io.h>
  49. # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  50. #else
  51. # define SET_BINARY_MODE(file)
  52. #endif
  53. #define CHUNK 16384
  54. const char * NAME = "VCMI 0.3 \"Tol Galen\"";
  55. SDL_Surface * ekran, * screen, * screen2;
  56. TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM;
  57. //#include "lua.h"
  58. //#include "lualib.h"
  59. //#include "lauxlib.h"
  60. //#include <luabind/luabind.hpp>
  61. //#include <luabind/function.hpp>
  62. //#include <luabind/class.hpp>
  63. //
  64. //#if (LUA_VERSION_NUM < 500)
  65. //# define LUA_OPEN_LIB(L, lib) lib(L)
  66. //#else
  67. //# define LUA_OPEN_LIB(L, lib) \
  68. // lua_pushcfunction((L), lib); \
  69. // lua_pcall((L), 0, 0, 0);
  70. //#endif
  71. //void piszpowitanie2(std::string i)
  72. //{
  73. // std::cout<<"powitanie2zc++. Liczba dnia to " << i;
  74. //}
  75. void initGameState(CGameInfo * cgi)
  76. {
  77. cgi->state->day=1;
  78. /*********creating players entries in gs****************************************/
  79. for (int i=0; i<cgi->scenarioOps.playerInfos.size();i++)
  80. {
  81. std::pair<int,PlayerState> ins(cgi->scenarioOps.playerInfos[i].color,PlayerState());
  82. cgi->state->players.insert(ins);
  83. }
  84. /******************RESOURCES****************************************************/
  85. //TODO: zeby komputer dostawal inaczej niz gracz
  86. std::vector<int> startres;
  87. std::ifstream tis("config/startres.txt");
  88. int k;
  89. for (int j=0;j<cgi->scenarioOps.difficulty;j++)
  90. {
  91. tis >> k;
  92. for (int z=0;z<RESOURCE_QUANTITY;z++)
  93. tis>>k;
  94. }
  95. tis >> k;
  96. for (int i=0;i<RESOURCE_QUANTITY;i++)
  97. {
  98. tis >> k;
  99. startres.push_back(k);
  100. }
  101. tis.close();
  102. for (std::map<int,PlayerState>::iterator i = cgi->state->players.begin(); i!=cgi->state->players.end(); i++)
  103. {
  104. (*i).second.resources.resize(RESOURCE_QUANTITY);
  105. for (int x=0;x<RESOURCE_QUANTITY;x++)
  106. (*i).second.resources[x] = startres[x];
  107. }
  108. /*************************HEROES************************************************/
  109. for (int i=0; i<cgi->heroh->heroInstances.size();i++) //heroes instances
  110. {
  111. if (!cgi->heroh->heroInstances[i]->type || cgi->heroh->heroInstances[i]->owner<0)
  112. continue;
  113. CHeroInstance * vhi = new CHeroInstance();
  114. *vhi=*(cgi->heroh->heroInstances[i]);
  115. if (!vhi->level)
  116. {
  117. vhi->exp=40+rand()%50;
  118. vhi->level = 1;
  119. }
  120. if (vhi->level>1) ;//TODO dodac um dr, ale potrzebne los
  121. if ((!vhi->primSkills.size()) || (vhi->primSkills[0]<0))
  122. {
  123. if (vhi->primSkills.size()<PRIMARY_SKILLS)
  124. vhi->primSkills.resize(PRIMARY_SKILLS);
  125. vhi->primSkills[0] = vhi->type->heroClass->initialAttack;
  126. vhi->primSkills[1] = vhi->type->heroClass->initialDefence;
  127. vhi->primSkills[2] = vhi->type->heroClass->initialPower;
  128. vhi->primSkills[3] = vhi->type->heroClass->initialKnowledge;
  129. vhi->mana = vhi->primSkills[3]*10;
  130. }
  131. if (!vhi->name.length())
  132. {
  133. vhi->name = vhi->type->name;
  134. }
  135. if (!vhi->biography.length())
  136. {
  137. vhi->biography = vhi->type->biography;
  138. }
  139. if (vhi->portrait < 0)
  140. vhi->portrait = vhi->type->ID;
  141. cgi->state->players[vhi->owner].heroes.push_back(vhi);
  142. }
  143. /*************************FOG**OF**WAR******************************************/
  144. for(std::map<int, PlayerState>::iterator k=cgi->state->players.begin(); k!=cgi->state->players.end(); ++k)
  145. {
  146. k->second.fogOfWarMap.resize(cgi->ac->map.width);
  147. for(int g=0; g<cgi->ac->map.width; ++g)
  148. k->second.fogOfWarMap[g].resize(cgi->ac->map.height);
  149. for(int g=0; g<cgi->ac->map.width; ++g)
  150. for(int h=0; h<cgi->ac->map.height; ++h)
  151. k->second.fogOfWarMap[g][h].resize(cgi->ac->map.twoLevel+1);
  152. for(int g=0; g<cgi->ac->map.width; ++g)
  153. for(int h=0; h<cgi->ac->map.height; ++h)
  154. for(int v=0; v<cgi->ac->map.twoLevel+1; ++v)
  155. k->second.fogOfWarMap[g][h][v] = 1;
  156. }
  157. /****************************TOWNS************************************************/
  158. for (int i=0;i<cgi->townh->townInstances.size();i++)
  159. {
  160. CTownInstance * vti = new CTownInstance();
  161. (*vti)=*(cgi->townh->townInstances[i]);
  162. cgi->state->players[vti->owner].towns.push_back(vti);
  163. }
  164. }
  165. int _tmain(int argc, _TCHAR* argv[])
  166. {
  167. //int iErr = 0;
  168. //lua_State *lua = lua_open (); // Open Lua
  169. // LUA_OPEN_LIB(lua, luaopen_base);
  170. // LUA_OPEN_LIB(lua, luaopen_io);
  171. //if ((iErr = luaL_loadfile (lua, "test.lua")) == 0)
  172. //{
  173. //
  174. // // Call main...
  175. // if ((iErr = lua_pcall (lua, 0, LUA_MULTRET, 0)) == 0)
  176. // {
  177. // luabind::open(lua);
  178. // luabind::module(lua)
  179. // [
  180. // luabind::def("powitanie",&piszpowitanie2)
  181. // ];
  182. // //int ret = luabind::call_function<int>(lua, "helloWorld2");
  183. // lua_pushstring (lua, "helloWorld2");
  184. // lua_gettable (lua, LUA_GLOBALSINDEX);
  185. // lua_pcall (lua, 0, 0, 0);
  186. // // Push the function name onto the stack
  187. // lua_pushstring (lua, "helloWorld");
  188. // lua_gettable (lua, LUA_GLOBALSINDEX);
  189. // lua_pcall (lua, 0, 0, 0);
  190. // }
  191. //}
  192. //lua_close (lua);
  193. //CBIKHandler cb;
  194. //cb.open("CSECRET.BIK");
  195. THC timeHandler tmh;
  196. THC tmh.getDif();
  197. int xx=0, yy=0, zz=0;
  198. SDL_Event sEvent;
  199. srand ( time(NULL) );
  200. SDL_Surface *temp;
  201. std::vector<SDL_Surface*> Sprites;
  202. float i;
  203. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO/*|SDL_INIT_EVENTTHREAD*/)==0)
  204. {
  205. CPG=NULL;
  206. TTF_Init();
  207. atexit(TTF_Quit);
  208. atexit(SDL_Quit);
  209. //TNRB = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  210. TNRB16 = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  211. //TNR = TTF_OpenFont("Fonts\\tnr.ttf",10);
  212. GEOR13 = TTF_OpenFont("Fonts\\georgia.ttf",13);
  213. GEORXX = TTF_OpenFont("Fonts\\tnrb.ttf",22);
  214. GEORM = TTF_OpenFont("Fonts\\georgia.ttf",10);
  215. CMusicHandler * mush = new CMusicHandler; //initializing audio
  216. mush->initMusics();
  217. //audio initialized
  218. /*if(Mix_PlayMusic(mush->mainMenuWoG, -1)==-1) //uncomment this fragment to have music
  219. {
  220. printf("Mix_PlayMusic: %s\n", Mix_GetError());
  221. // well, there's no music, but most games don't break without music...
  222. }*/
  223. screen2 = SDL_SetVideoMode(800,600,24,SDL_SWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/);
  224. screen = SDL_ConvertSurface(screen2, screen2->format, SDL_SWSURFACE);
  225. ekran = screen;
  226. SDL_WM_SetCaption(NAME,""); //set window title
  227. CGameInfo * cgi = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  228. CGameInfo::mainObj = cgi;
  229. cgi->consoleh = new CConsoleHandler;
  230. cgi->mush = mush;
  231. cgi->curh = new CCursorHandler;
  232. THC std::cout<<"Initializing screen, fonts and sound handling: "<<tmh.getDif()<<std::endl;
  233. cgi->spriteh = new CLodHandler;
  234. cgi->spriteh->init(std::string("Data\\H3sprite.lod"));
  235. cgi->bitmaph = new CLodHandler;
  236. cgi->bitmaph->init(std::string("Data\\H3bitmap.lod"));
  237. THC std::cout<<"Loading .lod files: "<<tmh.getDif()<<std::endl;
  238. cgi->curh->initCursor();
  239. cgi->curh->showGraphicCursor();
  240. cgi->screenh = new CScreenHandler;
  241. cgi->screenh->initScreen();
  242. //colors initialization
  243. SDL_Color p;
  244. p.unused = 0;
  245. p.r = 0xff; p.g = 0x0; p.b = 0x0; //red
  246. cgi->playerColors.push_back(p); //red
  247. p.r = 0x31; p.g = 0x52; p.b = 0xff; //blue
  248. cgi->playerColors.push_back(p); //blue
  249. p.r = 0x9c; p.g = 0x73; p.b = 0x52;//tan
  250. cgi->playerColors.push_back(p);//tan
  251. p.r = 0x42; p.g = 0x94; p.b = 0x29; //green
  252. cgi->playerColors.push_back(p); //green
  253. p.r = 0xff; p.g = 0x84; p.b = 0x0; //orange
  254. cgi->playerColors.push_back(p); //orange
  255. p.r = 0x8c; p.g = 0x29; p.b = 0xa5; //purple
  256. cgi->playerColors.push_back(p); //purple
  257. p.r = 0x09; p.g = 0x9c; p.b = 0xa5;//teal
  258. cgi->playerColors.push_back(p);//teal
  259. p.r = 0xc6; p.g = 0x7b; p.b = 0x8c;//pink
  260. cgi->playerColors.push_back(p);//pink
  261. p.r = 0x84; p.g = 0x84; p.b = 0x84;//gray
  262. cgi->neutralColor = p;//gray
  263. //colors initialized
  264. cgi->townh = new CTownHandler;
  265. cgi->townh->loadNames();
  266. CAbilityHandler * abilh = new CAbilityHandler;
  267. abilh->loadAbilities();
  268. cgi->abilh = abilh;
  269. CHeroHandler * heroh = new CHeroHandler;
  270. heroh->loadHeroes();
  271. heroh->loadPortraits();
  272. cgi->heroh = heroh;
  273. cgi->generaltexth = new CGeneralTextHandler;
  274. cgi->generaltexth->load();
  275. //initializing hero flags
  276. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01L.DEF")); //red
  277. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01G.DEF")); //blue
  278. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01R.DEF")); //tan
  279. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01D.DEF")); //green
  280. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01B.DEF")); //orange
  281. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01P.DEF")); //purple
  282. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01W.DEF")); //teal
  283. cgi->heroh->flags1.push_back(cgi->spriteh->giveDef("ABF01K.DEF")); //pink
  284. for(int q=0; q<8; ++q)
  285. {
  286. for(int o=0; o<cgi->heroh->flags1[q]->ourImages.size(); ++o)
  287. {
  288. if(cgi->heroh->flags1[q]->ourImages[o].groupNumber==6)
  289. {
  290. for(int e=0; e<8; ++e)
  291. {
  292. Cimage nci;
  293. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags1[q]->ourImages[o+e].bitmap);
  294. nci.groupNumber = 10;
  295. nci.imName = std::string();
  296. cgi->heroh->flags1[q]->ourImages.push_back(nci);
  297. }
  298. o+=8;
  299. }
  300. if(cgi->heroh->flags1[q]->ourImages[o].groupNumber==7)
  301. {
  302. for(int e=0; e<8; ++e)
  303. {
  304. Cimage nci;
  305. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags1[q]->ourImages[o+e].bitmap);
  306. nci.groupNumber = 11;
  307. nci.imName = std::string();
  308. cgi->heroh->flags1[q]->ourImages.push_back(nci);
  309. }
  310. o+=8;
  311. }
  312. if(cgi->heroh->flags1[q]->ourImages[o].groupNumber==8)
  313. {
  314. for(int e=0; e<8; ++e)
  315. {
  316. Cimage nci;
  317. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags1[q]->ourImages[o+e].bitmap);
  318. nci.groupNumber = 12;
  319. nci.imName = std::string();
  320. cgi->heroh->flags1[q]->ourImages.push_back(nci);
  321. }
  322. o+=8;
  323. }
  324. }
  325. for(int ff=0; ff<cgi->heroh->flags1[q]->ourImages.size(); ++ff)
  326. {
  327. CSDL_Ext::fullAlphaTransform(cgi->heroh->flags1[q]->ourImages[ff].bitmap);
  328. }
  329. cgi->heroh->flags1[q]->alphaTransformed = true;
  330. }
  331. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02L.DEF")); //red
  332. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02G.DEF")); //blue
  333. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02R.DEF")); //tan
  334. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02D.DEF")); //green
  335. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02B.DEF")); //orange
  336. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02P.DEF")); //purple
  337. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02W.DEF")); //teal
  338. cgi->heroh->flags2.push_back(cgi->spriteh->giveDef("ABF02K.DEF")); //pink
  339. for(int q=0; q<8; ++q)
  340. {
  341. for(int o=0; o<cgi->heroh->flags2[q]->ourImages.size(); ++o)
  342. {
  343. if(cgi->heroh->flags2[q]->ourImages[o].groupNumber==6)
  344. {
  345. for(int e=0; e<8; ++e)
  346. {
  347. Cimage nci;
  348. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags2[q]->ourImages[o+e].bitmap);
  349. nci.groupNumber = 10;
  350. nci.imName = std::string();
  351. cgi->heroh->flags2[q]->ourImages.push_back(nci);
  352. }
  353. o+=8;
  354. }
  355. if(cgi->heroh->flags2[q]->ourImages[o].groupNumber==7)
  356. {
  357. for(int e=0; e<8; ++e)
  358. {
  359. Cimage nci;
  360. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags2[q]->ourImages[o+e].bitmap);
  361. nci.groupNumber = 11;
  362. nci.imName = std::string();
  363. cgi->heroh->flags2[q]->ourImages.push_back(nci);
  364. }
  365. o+=8;
  366. }
  367. if(cgi->heroh->flags2[q]->ourImages[o].groupNumber==8)
  368. {
  369. for(int e=0; e<8; ++e)
  370. {
  371. Cimage nci;
  372. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags2[q]->ourImages[o+e].bitmap);
  373. nci.groupNumber = 12;
  374. nci.imName = std::string();
  375. cgi->heroh->flags2[q]->ourImages.push_back(nci);
  376. }
  377. o+=8;
  378. }
  379. }
  380. for(int ff=0; ff<cgi->heroh->flags2[q]->ourImages.size(); ++ff)
  381. {
  382. CSDL_Ext::fullAlphaTransform(cgi->heroh->flags2[q]->ourImages[ff].bitmap);
  383. }
  384. cgi->heroh->flags2[q]->alphaTransformed = true;
  385. }
  386. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03L.DEF")); //red
  387. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03G.DEF")); //blue
  388. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03R.DEF")); //tan
  389. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03D.DEF")); //green
  390. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03B.DEF")); //orange
  391. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03P.DEF")); //purple
  392. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03W.DEF")); //teal
  393. cgi->heroh->flags3.push_back(cgi->spriteh->giveDef("ABF03K.DEF")); //pink
  394. for(int q=0; q<8; ++q)
  395. {
  396. for(int o=0; o<cgi->heroh->flags3[q]->ourImages.size(); ++o)
  397. {
  398. if(cgi->heroh->flags3[q]->ourImages[o].groupNumber==6)
  399. {
  400. for(int e=0; e<8; ++e)
  401. {
  402. Cimage nci;
  403. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags3[q]->ourImages[o+e].bitmap);
  404. nci.groupNumber = 10;
  405. nci.imName = std::string();
  406. cgi->heroh->flags3[q]->ourImages.push_back(nci);
  407. }
  408. o+=8;
  409. }
  410. if(cgi->heroh->flags3[q]->ourImages[o].groupNumber==7)
  411. {
  412. for(int e=0; e<8; ++e)
  413. {
  414. Cimage nci;
  415. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags3[q]->ourImages[o+e].bitmap);
  416. nci.groupNumber = 11;
  417. nci.imName = std::string();
  418. cgi->heroh->flags3[q]->ourImages.push_back(nci);
  419. }
  420. o+=8;
  421. }
  422. if(cgi->heroh->flags3[q]->ourImages[o].groupNumber==8)
  423. {
  424. for(int e=0; e<8; ++e)
  425. {
  426. Cimage nci;
  427. nci.bitmap = CSDL_Ext::rotate01(cgi->heroh->flags3[q]->ourImages[o+e].bitmap);
  428. nci.groupNumber = 12;
  429. nci.imName = std::string();
  430. cgi->heroh->flags3[q]->ourImages.push_back(nci);
  431. }
  432. o+=8;
  433. }
  434. }
  435. for(int ff=0; ff<cgi->heroh->flags3[q]->ourImages.size(); ++ff)
  436. {
  437. CSDL_Ext::fullAlphaTransform(cgi->heroh->flags3[q]->ourImages[ff].bitmap);
  438. }
  439. cgi->heroh->flags3[q]->alphaTransformed = true;
  440. }
  441. //hero flags initialized
  442. THC std::cout<<"Preparing first handlers: "<<tmh.getDif()<<std::endl;
  443. CPreGame * cpg = new CPreGame(); //main menu and submenus
  444. THC std::cout<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  445. cpg->mush = mush;
  446. cgi->scenarioOps = cpg->runLoop();
  447. THC tmh.getDif();
  448. cgi->sspriteh = new CSemiLodHandler();
  449. cgi->sspriteh->openLod("H3sprite.lod");
  450. CArtHandler * arth = new CArtHandler;
  451. arth->loadArtifacts();
  452. cgi->arth = arth;
  453. CCreatureHandler * creh = new CCreatureHandler;
  454. creh->loadCreatures();
  455. cgi->creh = creh;
  456. CSpellHandler * spellh = new CSpellHandler;
  457. spellh->loadSpells();
  458. cgi->spellh = spellh;
  459. CBuildingHandler * buildh = new CBuildingHandler;
  460. buildh->loadBuildings();
  461. cgi->buildh = buildh;
  462. CObjectHandler * objh = new CObjectHandler;
  463. objh->loadObjects();
  464. cgi->objh = objh;
  465. cgi->dobjinfo = new CDefObjInfoHandler;
  466. cgi->dobjinfo->load();
  467. cgi->state = new CGameState();
  468. cgi->state->players = std::map<int, PlayerState>();
  469. cgi->pathf = new CPathfinder();
  470. cgi->consoleh->cb = new CCallback(cgi->state,-1);
  471. cgi->consoleh->runConsole();
  472. THC std::cout<<"Handlers initailization: "<<tmh.getDif()<<std::endl;
  473. std::string mapname;
  474. if(CPG->ourScenSel->mapsel.selected==0) CPG->ourScenSel->mapsel.selected = 1; //only for tests
  475. if (CPG) mapname = CPG->ourScenSel->mapsel.ourMaps[CPG->ourScenSel->mapsel.selected].filename;
  476. gzFile map = gzopen(mapname.c_str(),"rb");
  477. std::string mapstr;int pom;
  478. while((pom=gzgetc(map))>=0)
  479. {
  480. mapstr+=pom;
  481. }
  482. gzclose(map);
  483. unsigned char *initTable = new unsigned char[mapstr.size()];
  484. for(int ss=0; ss<mapstr.size(); ++ss)
  485. {
  486. initTable[ss] = mapstr[ss];
  487. }
  488. #define CHOOSE
  489. #ifdef CHOOSE
  490. CAmbarCendamo * ac = new CAmbarCendamo(initTable); //4gryf
  491. #else
  492. CAmbarCendamo * ac = new CAmbarCendamo("RoEtest"); //4gryf
  493. #endif
  494. CMapHeader * mmhh = new CMapHeader(ac->bufor); //czytanie nag³ówka
  495. cgi->ac = ac;
  496. THC std::cout<<"Reading file: "<<tmh.getDif()<<std::endl;
  497. ac->deh3m();
  498. THC std::cout<<"Detecting file (together): "<<tmh.getDif()<<std::endl;
  499. ac->loadDefs();
  500. THC std::cout<<"Reading terrain defs: "<<tmh.getDif()<<std::endl;
  501. CMapHandler * mh = new CMapHandler();
  502. cgi->mh = mh;
  503. mh->reader = ac;
  504. THC std::cout<<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  505. mh->init();
  506. THC std::cout<<"Initializing mapHandler: "<<tmh.getDif()<<std::endl;
  507. initGameState(cgi);
  508. THC std::cout<<"Initializing GameState: "<<tmh.getDif()<<std::endl;
  509. for (int i=0; i<cgi->scenarioOps.playerInfos.size();i++) //initializing interfaces
  510. {
  511. //TODO: uncomment when AI will be done
  512. //if(cgi->scenarioOps.playerInfos[i].name=="AI")
  513. // cgi->playerint.push_back(new CGlobalAI());
  514. //else
  515. {
  516. cgi->state->currentPlayer=cgi->scenarioOps.playerInfos[i].color;
  517. cgi->playerint.push_back(new CPlayerInterface(cgi->scenarioOps.playerInfos[i].color,i));
  518. ((CPlayerInterface*)(cgi->playerint[i]))->init(new CCallback(cgi->state,cgi->scenarioOps.playerInfos[i].color));
  519. }
  520. }
  521. while(1) //main game loop, one execution per turn
  522. {
  523. cgi->consoleh->cb->newTurn();
  524. for (int i=0;i<cgi->playerint.size();i++)
  525. {
  526. cgi->state->currentPlayer=cgi->playerint[i]->playerID;
  527. cgi->playerint[i]->yourTurn();
  528. }
  529. }
  530. }
  531. else
  532. {
  533. printf("Something was wrong: %s/n", SDL_GetError());
  534. return -1;
  535. }
  536. }