CMT.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. THC std::cout<<"Preparing first handlers: "<<tmh.getDif()<<std::endl;
  276. CPreGame * cpg = new CPreGame(); //main menu and submenus
  277. THC std::cout<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl;
  278. cpg->mush = mush;
  279. cgi->scenarioOps = cpg->runLoop();
  280. THC tmh.getDif();
  281. cgi->sspriteh = new CSemiLodHandler();
  282. cgi->sspriteh->openLod("H3sprite.lod");
  283. CArtHandler * arth = new CArtHandler;
  284. arth->loadArtifacts();
  285. cgi->arth = arth;
  286. CCreatureHandler * creh = new CCreatureHandler;
  287. creh->loadCreatures();
  288. cgi->creh = creh;
  289. CSpellHandler * spellh = new CSpellHandler;
  290. spellh->loadSpells();
  291. cgi->spellh = spellh;
  292. CBuildingHandler * buildh = new CBuildingHandler;
  293. buildh->loadBuildings();
  294. cgi->buildh = buildh;
  295. CObjectHandler * objh = new CObjectHandler;
  296. objh->loadObjects();
  297. cgi->objh = objh;
  298. cgi->dobjinfo = new CDefObjInfoHandler;
  299. cgi->dobjinfo->load();
  300. cgi->state = new CGameState();
  301. cgi->state->players = std::map<int, PlayerState>();
  302. cgi->pathf = new CPathfinder();
  303. cgi->consoleh->cb = new CCallback(cgi->state,-1);
  304. cgi->consoleh->runConsole();
  305. THC std::cout<<"Handlers initailization: "<<tmh.getDif()<<std::endl;
  306. std::string mapname;
  307. if(CPG->ourScenSel->mapsel.selected==0) CPG->ourScenSel->mapsel.selected = 1; //only for tests
  308. if (CPG) mapname = CPG->ourScenSel->mapsel.ourMaps[CPG->ourScenSel->mapsel.selected].filename;
  309. gzFile map = gzopen(mapname.c_str(),"rb");
  310. std::string mapstr;int pom;
  311. while((pom=gzgetc(map))>=0)
  312. {
  313. mapstr+=pom;
  314. }
  315. gzclose(map);
  316. unsigned char *initTable = new unsigned char[mapstr.size()];
  317. for(int ss=0; ss<mapstr.size(); ++ss)
  318. {
  319. initTable[ss] = mapstr[ss];
  320. }
  321. #define CHOOSE
  322. #ifdef CHOOSE
  323. CAmbarCendamo * ac = new CAmbarCendamo(initTable); //4gryf
  324. #else
  325. CAmbarCendamo * ac = new CAmbarCendamo("RoEtest"); //4gryf
  326. #endif
  327. CMapHeader * mmhh = new CMapHeader(ac->bufor); //czytanie nag³ówka
  328. cgi->ac = ac;
  329. THC std::cout<<"Reading file: "<<tmh.getDif()<<std::endl;
  330. ac->deh3m();
  331. THC std::cout<<"Detecting file (together): "<<tmh.getDif()<<std::endl;
  332. ac->loadDefs();
  333. THC std::cout<<"Reading terrain defs: "<<tmh.getDif()<<std::endl;
  334. CMapHandler * mh = new CMapHandler();
  335. cgi->mh = mh;
  336. mh->reader = ac;
  337. THC std::cout<<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  338. mh->init();
  339. THC std::cout<<"Initializing mapHandler: "<<tmh.getDif()<<std::endl;
  340. initGameState(cgi);
  341. THC std::cout<<"Initializing GameState: "<<tmh.getDif()<<std::endl;
  342. for (int i=0; i<cgi->scenarioOps.playerInfos.size();i++) //initializing interfaces
  343. {
  344. //TODO: uncomment when AI will be done
  345. //if(cgi->scenarioOps.playerInfos[i].name=="AI")
  346. // cgi->playerint.push_back(new CGlobalAI());
  347. //else
  348. {
  349. cgi->state->currentPlayer=cgi->scenarioOps.playerInfos[i].color;
  350. cgi->playerint.push_back(new CPlayerInterface(cgi->scenarioOps.playerInfos[i].color,i));
  351. ((CPlayerInterface*)(cgi->playerint[i]))->init(new CCallback(cgi->state,cgi->scenarioOps.playerInfos[i].color));
  352. }
  353. }
  354. while(1) //main game loop, one execution per turn
  355. {
  356. for (int i=0;i<cgi->playerint.size();i++)
  357. {
  358. cgi->state->currentPlayer=cgi->playerint[i]->playerID;
  359. cgi->playerint[i]->yourTurn();
  360. }
  361. }
  362. }
  363. else
  364. {
  365. printf("Something was wrong: %s/n", SDL_GetError());
  366. return -1;
  367. }
  368. }