CMT.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. /* Compress from file source to file dest until EOF on source.
  56. def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
  57. allocated for processing, Z_STREAM_ERROR if an invalid compression
  58. level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
  59. version of the library linked do not match, or Z_ERRNO if there is
  60. an error reading or writing the files. */
  61. SDL_Surface * ekran, * screen, * screen2;
  62. TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM;
  63. //#include "lua.h"
  64. //#include "lualib.h"
  65. //#include "lauxlib.h"
  66. //#include <luabind/luabind.hpp>
  67. //#include <luabind/function.hpp>
  68. //#include <luabind/class.hpp>
  69. //
  70. //#if (LUA_VERSION_NUM < 500)
  71. //# define LUA_OPEN_LIB(L, lib) lib(L)
  72. //#else
  73. //# define LUA_OPEN_LIB(L, lib) \
  74. // lua_pushcfunction((L), lib); \
  75. // lua_pcall((L), 0, 0, 0);
  76. //#endif
  77. //void piszpowitanie2(std::string i)
  78. //{
  79. // std::cout<<"powitanie2zc++. Liczba dnia to " << i;
  80. //}
  81. void initGameState(CGameInfo * cgi)
  82. {
  83. /******************RESOURCES****************************************************/
  84. //TODO: zeby komputer dostawal inaczej niz gracz
  85. std::vector<int> startres;
  86. std::ifstream tis("config/startres.txt");
  87. int k;
  88. for (int j=0;j<cgi->scenarioOps.difficulty;j++)
  89. {
  90. tis >> k;
  91. for (int z=0;z<RESOURCE_QUANTITY;z++)
  92. tis>>k;
  93. }
  94. tis >> k;
  95. for (int i=0;i<RESOURCE_QUANTITY;i++)
  96. {
  97. tis >> k;
  98. startres.push_back(k);
  99. }
  100. tis.close();
  101. for (std::map<int,PlayerState>::iterator i = cgi->state->players.begin(); i!=cgi->state->players.end(); i++)
  102. {
  103. (*i).second.resources.resize(RESOURCE_QUANTITY);
  104. for (int x=0;x<RESOURCE_QUANTITY;x++)
  105. (*i).second.resources[x] = startres[x];
  106. }
  107. /*************************HEROES************************************************/
  108. for (int i=0; i<cgi->heroh->heroInstances.size();i++) //heroes instances
  109. {
  110. if (!cgi->heroh->heroInstances[i]->type || cgi->heroh->heroInstances[i]->owner<0)
  111. continue;
  112. CHeroInstance * vhi = new CHeroInstance();
  113. *vhi=*(cgi->heroh->heroInstances[i]);
  114. if (!vhi->level)
  115. {
  116. vhi->exp=40+rand()%50;
  117. vhi->level = 1;
  118. }
  119. if (vhi->level>1) ;//TODO dodac um dr, ale potrzebne los
  120. if ((!vhi->primSkills.size()) || (vhi->primSkills[0]<0))
  121. {
  122. if (vhi->primSkills.size()<PRIMARY_SKILLS)
  123. vhi->primSkills.resize(PRIMARY_SKILLS);
  124. vhi->primSkills[0] = vhi->type->heroClass->initialAttack;
  125. vhi->primSkills[1] = vhi->type->heroClass->initialDefence;
  126. vhi->primSkills[2] = vhi->type->heroClass->initialPower;
  127. vhi->primSkills[3] = vhi->type->heroClass->initialKnowledge;
  128. vhi->mana = vhi->primSkills[3]*10;
  129. }
  130. if (!vhi->name.length())
  131. {
  132. vhi->name = vhi->type->name;
  133. }
  134. if (!vhi->biography.length())
  135. {
  136. vhi->biography = vhi->type->biography;
  137. }
  138. if (vhi->portrait < 0)
  139. vhi->portrait = vhi->type->ID;
  140. cgi->state->players[vhi->owner].heroes.push_back(vhi);
  141. }
  142. /*************************FOG**OF**WAR******************************************/
  143. for(std::map<int, PlayerState>::iterator k=cgi->state->players.begin(); k!=cgi->state->players.end(); ++k)
  144. {
  145. k->second.fogOfWarMap.resize(cgi->ac->map.width);
  146. for(int g=0; g<cgi->ac->map.width; ++g)
  147. k->second.fogOfWarMap[g].resize(cgi->ac->map.height);
  148. for(int g=0; g<cgi->ac->map.width; ++g)
  149. for(int h=0; h<cgi->ac->map.height; ++h)
  150. k->second.fogOfWarMap[g][h].resize(cgi->ac->map.twoLevel+1);
  151. for(int g=0; g<cgi->ac->map.width; ++g)
  152. for(int h=0; h<cgi->ac->map.height; ++h)
  153. for(int v=0; v<cgi->ac->map.twoLevel+1; ++v)
  154. k->second.fogOfWarMap[g][h][v] = 1;
  155. }
  156. /****************************TOWNS************************************************/
  157. }
  158. int _tmain(int argc, _TCHAR* argv[])
  159. {
  160. //int iErr = 0;
  161. //lua_State *lua = lua_open (); // Open Lua
  162. // LUA_OPEN_LIB(lua, luaopen_base);
  163. // LUA_OPEN_LIB(lua, luaopen_io);
  164. //if ((iErr = luaL_loadfile (lua, "test.lua")) == 0)
  165. //{
  166. //
  167. // // Call main...
  168. // if ((iErr = lua_pcall (lua, 0, LUA_MULTRET, 0)) == 0)
  169. // {
  170. // luabind::open(lua);
  171. // luabind::module(lua)
  172. // [
  173. // luabind::def("powitanie",&piszpowitanie2)
  174. // ];
  175. // //int ret = luabind::call_function<int>(lua, "helloWorld2");
  176. // lua_pushstring (lua, "helloWorld2");
  177. // lua_gettable (lua, LUA_GLOBALSINDEX);
  178. // lua_pcall (lua, 0, 0, 0);
  179. // // Push the function name onto the stack
  180. // lua_pushstring (lua, "helloWorld");
  181. // lua_gettable (lua, LUA_GLOBALSINDEX);
  182. // lua_pcall (lua, 0, 0, 0);
  183. // }
  184. //}
  185. //lua_close (lua);
  186. //CBIKHandler cb;
  187. //cb.open("CSECRET.BIK");
  188. THC timeHandler tmh;
  189. THC tmh.getDif();
  190. int xx=0, yy=0, zz=0;
  191. SDL_Event sEvent;
  192. srand ( time(NULL) );
  193. SDL_Surface *temp;
  194. std::vector<SDL_Surface*> Sprites;
  195. float i;
  196. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO/*|SDL_INIT_EVENTTHREAD*/)==0)
  197. {
  198. CPG=NULL;
  199. TTF_Init();
  200. atexit(TTF_Quit);
  201. atexit(SDL_Quit);
  202. //TNRB = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  203. TNRB16 = TTF_OpenFont("Fonts\\tnrb.ttf",16);
  204. //TNR = TTF_OpenFont("Fonts\\tnr.ttf",10);
  205. GEOR13 = TTF_OpenFont("Fonts\\georgia.ttf",13);
  206. GEORXX = TTF_OpenFont("Fonts\\tnrb.ttf",22);
  207. GEORM = TTF_OpenFont("Fonts\\georgia.ttf",10);
  208. //initializing audio
  209. CMusicHandler * mush = new CMusicHandler;
  210. mush->initMusics();
  211. //CSndHandler snd("Heroes3.snd");
  212. //snd.extract("AELMMOVE.wav","snddd.wav");
  213. //audio initialized
  214. /*if(Mix_PlayMusic(mush->mainMenuWoG, -1)==-1) //uncomment this fragment to have music
  215. {
  216. printf("Mix_PlayMusic: %s\n", Mix_GetError());
  217. // well, there's no music, but most games don't break without music...
  218. }*/
  219. screen2 = SDL_SetVideoMode(800,600,24,SDL_SWSURFACE|SDL_DOUBLEBUF/*|SDL_FULLSCREEN*/);
  220. screen = SDL_ConvertSurface(screen2, screen2->format, SDL_SWSURFACE);
  221. ekran = screen;
  222. SDL_WM_SetCaption(NAME,""); //set window title
  223. CGameInfo * cgi = new CGameInfo; //contains all global informations about game (texts, lodHandlers, map handler itp.)
  224. CGameInfo::mainObj = cgi;
  225. cgi->consoleh = new CConsoleHandler;
  226. cgi->consoleh->runConsole();
  227. cgi->mush = mush;
  228. cgi->curh = new CCursorHandler;
  229. THC std::cout<<"Initializing screen, fonts and sound handling: "<<tmh.getDif()<<std::endl;
  230. cgi->spriteh = new CLodHandler;
  231. cgi->spriteh->init(std::string("Data\\H3sprite.lod"));
  232. cgi->bitmaph = new CLodHandler;
  233. cgi->bitmaph->init(std::string("Data\\H3bitmap.lod"));
  234. THC std::cout<<"Loading .lod files: "<<tmh.getDif()<<std::endl;
  235. cgi->curh->initCursor();
  236. cgi->curh->showGraphicCursor();
  237. cgi->screenh = new CScreenHandler;
  238. cgi->screenh->initScreen();
  239. //colors initialization
  240. SDL_Color p;
  241. p.unused = 0;
  242. p.r = 0xff; p.g = 0x0; p.b = 0x0; //red
  243. cgi->playerColors.push_back(p); //red
  244. p.r = 0x31; p.g = 0x52; p.b = 0xff; //blue
  245. cgi->playerColors.push_back(p); //blue
  246. p.r = 0x9c; p.g = 0x73; p.b = 0x52;//tan
  247. cgi->playerColors.push_back(p);//tan
  248. p.r = 0x42; p.g = 0x94; p.b = 0x29; //green
  249. cgi->playerColors.push_back(p); //green
  250. p.r = 0xff; p.g = 0x84; p.b = 0x0; //orange
  251. cgi->playerColors.push_back(p); //orange
  252. p.r = 0x8c; p.g = 0x29; p.b = 0xa5; //purple
  253. cgi->playerColors.push_back(p); //purple
  254. p.r = 0x09; p.g = 0x9c; p.b = 0xa5;//teal
  255. cgi->playerColors.push_back(p);//teal
  256. p.r = 0xc6; p.g = 0x7b; p.b = 0x8c;//pink
  257. cgi->playerColors.push_back(p);//pink
  258. p.r = 0x84; p.g = 0x84; p.b = 0x84;//gray
  259. cgi->neutralColor = p;//gray
  260. //colors initialized
  261. cgi->townh = new CTownHandler;
  262. cgi->townh->loadNames();
  263. CAbilityHandler * abilh = new CAbilityHandler;
  264. abilh->loadAbilities();
  265. cgi->abilh = abilh;
  266. CHeroHandler * heroh = new CHeroHandler;
  267. heroh->loadHeroes();
  268. heroh->loadPortraits();
  269. cgi->heroh = heroh;
  270. cgi->generaltexth = new CGeneralTextHandler;
  271. cgi->generaltexth->load();
  272. THC std::cout<<"Preparing first handlers: "<<tmh.getDif()<<std::endl;
  273. CPreGame * cpg = new CPreGame(); //main menu and submenus
  274. THC std::cout<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  275. cpg->mush = mush;
  276. cgi->scenarioOps = cpg->runLoop();
  277. THC tmh.getDif();
  278. cgi->sspriteh = new CSemiLodHandler();
  279. cgi->sspriteh->openLod("H3sprite.lod");
  280. CArtHandler * arth = new CArtHandler;
  281. arth->loadArtifacts();
  282. cgi->arth = arth;
  283. CCreatureHandler * creh = new CCreatureHandler;
  284. creh->loadCreatures();
  285. cgi->creh = creh;
  286. CSpellHandler * spellh = new CSpellHandler;
  287. spellh->loadSpells();
  288. cgi->spellh = spellh;
  289. CBuildingHandler * buildh = new CBuildingHandler;
  290. buildh->loadBuildings();
  291. cgi->buildh = buildh;
  292. CObjectHandler * objh = new CObjectHandler;
  293. objh->loadObjects();
  294. cgi->objh = objh;
  295. cgi->dobjinfo = new CDefObjInfoHandler;
  296. cgi->dobjinfo->load();
  297. cgi->state = new CGameState();
  298. cgi->state->players = std::map<int, PlayerState>();
  299. cgi->state->cb = new CCallback(cgi->state);
  300. cgi->pathf = new CPathfinder();
  301. THC std::cout<<"Handlers initailization: "<<tmh.getDif()<<std::endl;
  302. std::string mapname;
  303. if(CPG->ourScenSel->mapsel.selected==0) CPG->ourScenSel->mapsel.selected = 1; //only for tests
  304. if (CPG) mapname = CPG->ourScenSel->mapsel.ourMaps[CPG->ourScenSel->mapsel.selected].filename;
  305. gzFile map = gzopen(mapname.c_str(),"rb");
  306. std::string mapstr;int pom;
  307. while((pom=gzgetc(map))>=0)
  308. {
  309. mapstr+=pom;
  310. }
  311. gzclose(map);
  312. unsigned char *initTable = new unsigned char[mapstr.size()];
  313. for(int ss=0; ss<mapstr.size(); ++ss)
  314. {
  315. initTable[ss] = mapstr[ss];
  316. }
  317. #define CHOOSE
  318. #ifdef CHOOSE
  319. CAmbarCendamo * ac = new CAmbarCendamo(initTable); //4gryf
  320. #else
  321. CAmbarCendamo * ac = new CAmbarCendamo("RoEtest"); //4gryf
  322. #endif
  323. CMapHeader * mmhh = new CMapHeader(ac->bufor); //czytanie nag³ówka
  324. cgi->ac = ac;
  325. THC std::cout<<"Reading file: "<<tmh.getDif()<<std::endl;
  326. ac->deh3m();
  327. THC std::cout<<"Detecting file (together): "<<tmh.getDif()<<std::endl;
  328. ac->loadDefs();
  329. THC std::cout<<"Reading terrain defs: "<<tmh.getDif()<<std::endl;
  330. CMapHandler * mh = new CMapHandler();
  331. cgi->mh = mh;
  332. mh->reader = ac;
  333. THC std::cout<<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  334. mh->init();
  335. THC std::cout<<"Initializing mapHandler: "<<tmh.getDif()<<std::endl;
  336. initGameState(cgi);
  337. THC std::cout<<"Initializing GameState: "<<tmh.getDif()<<std::endl;
  338. for (int i=0; i<cgi->scenarioOps.playerInfos.size();i++) //initializing interfaces
  339. {
  340. //TODO: uncomment when AI will be done
  341. //if(cgi->scenarioOps.playerInfos[i].name=="AI")
  342. // cgi->playerint.push_back(new CGlobalAI());
  343. //else
  344. {
  345. cgi->state->currentPlayer=i;
  346. cgi->playerint.push_back(new CPlayerInterface(cgi->scenarioOps.playerInfos[i].color,i));
  347. ((CPlayerInterface*)(cgi->playerint[i]))->init(cgi->state->cb);
  348. }
  349. }
  350. while(1) //main game loop, one execution per turn
  351. {
  352. for (int i=0;i<cgi->playerint.size();i++)
  353. {
  354. cgi->state->currentPlayer=cgi->playerint[i]->playerID;
  355. cgi->playerint[i]->yourTurn();
  356. }
  357. }
  358. }
  359. else
  360. {
  361. printf("Something was wrong: %s/n", SDL_GetError());
  362. return -1;
  363. }
  364. }