CHeroHandler.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #include "stdafx.h"
  2. #include "CHeroHandler.h"
  3. #include "CGameInfo.h"
  4. #include <sstream>
  5. #define CGI (CGameInfo::mainObj)
  6. CHeroHandler::~CHeroHandler()
  7. {
  8. for (int j=0;j<heroes.size();j++)
  9. {
  10. if (heroes[j]->portraitSmall)
  11. SDL_FreeSurface(heroes[j]->portraitSmall);
  12. delete heroes[j];
  13. }
  14. }
  15. void CHeroHandler::loadPortraits()
  16. {
  17. std::ifstream of("portrety.txt");
  18. for (int j=0;j<heroes.size();j++)
  19. {
  20. int ID;
  21. of>>ID;
  22. std::string path;
  23. of>>path;
  24. heroes[ID]->portraitSmall=CGI->bitmaph->loadBitmap(path);
  25. if (!heroes[ID]->portraitSmall)
  26. std::cout<<"Can't read portrait for "<<ID<<" ("<<path<<")\n";
  27. }
  28. of.close();
  29. }
  30. void CHeroHandler::loadHeroes()
  31. {
  32. int ID=0;
  33. std::ifstream inp("H3bitmap.lod\\HOTRAITS.TXT", std::ios::in);
  34. std::string dump;
  35. for(int i=0; i<25; ++i)
  36. {
  37. inp>>dump;
  38. }
  39. inp.ignore();
  40. int numberOfCurrentClassHeroes = 0;
  41. int currentClass = 0;
  42. int additHero = 0;
  43. EHeroClasses addTab[12];
  44. addTab[0] = HERO_KNIGHT;
  45. addTab[1] = HERO_WITCH;
  46. addTab[2] = HERO_KNIGHT;
  47. addTab[3] = HERO_WIZARD;
  48. addTab[4] = HERO_RANGER;
  49. addTab[5] = HERO_BARBARIAN;
  50. addTab[6] = HERO_DEATHKNIGHT;
  51. addTab[7] = HERO_WARLOCK;
  52. addTab[8] = HERO_KNIGHT;
  53. addTab[9] = HERO_WARLOCK;
  54. addTab[10] = HERO_BARBARIAN;
  55. addTab[11] = HERO_DEMONIAC;
  56. while(!inp.eof())
  57. {
  58. CHero * nher = new CHero;
  59. if(currentClass<18)
  60. {
  61. nher->heroType = (EHeroClasses)currentClass;
  62. ++numberOfCurrentClassHeroes;
  63. if(numberOfCurrentClassHeroes==8)
  64. {
  65. numberOfCurrentClassHeroes = 0;
  66. ++currentClass;
  67. }
  68. }
  69. else
  70. {
  71. nher->heroType = addTab[additHero++];
  72. }
  73. std::string base;
  74. char * tab = new char[500];
  75. int iit = 0;
  76. inp.getline(tab, 500);
  77. base = std::string(tab);
  78. if(base.size()<2) //ended, but some rubbish could still stay end we have something useless
  79. {
  80. loadSpecialAbilities();
  81. loadBiographies();
  82. loadHeroClasses();
  83. initHeroClasses();
  84. inp.close();
  85. return;
  86. }
  87. while(base[iit]!='\t')
  88. {
  89. ++iit;
  90. }
  91. nher->name = base.substr(0, iit);
  92. ++iit;
  93. for(int i=iit; i<iit+100; ++i)
  94. {
  95. if(base[i]==(char)(10) || base[i]==(char)(9))
  96. {
  97. nher->low1stack = atoi(base.substr(iit, i).c_str());
  98. iit=i+1;
  99. break;
  100. }
  101. }
  102. for(int i=iit; i<iit+100; ++i)
  103. {
  104. if(base[i]==(char)(10) || base[i]==(char)(9))
  105. {
  106. nher->high1stack = atoi(base.substr(iit, i).c_str());
  107. iit=i+1;
  108. break;
  109. }
  110. }
  111. int ipom=iit;
  112. while(base[ipom]!='\t')
  113. {
  114. ++ipom;
  115. }
  116. nher->refType1stack = base.substr(iit, ipom-iit);
  117. iit=ipom+1;
  118. for(int i=iit; i<iit+100; ++i)
  119. {
  120. if(base[i]==(char)(10) || base[i]==(char)(9))
  121. {
  122. nher->low2stack = atoi(base.substr(iit, i-iit).c_str());
  123. iit=i+1;
  124. break;
  125. }
  126. }
  127. for(int i=iit; i<iit+100; ++i)
  128. {
  129. if(base[i]==(char)(10) || base[i]==(char)(9))
  130. {
  131. nher->high2stack = atoi(base.substr(iit, i-iit).c_str());
  132. iit=i+1;
  133. break;
  134. }
  135. }
  136. ipom=iit;
  137. while(base[ipom]!='\t')
  138. {
  139. ++ipom;
  140. }
  141. nher->refType2stack = base.substr(iit, ipom-iit);
  142. iit=ipom+1;
  143. for(int i=iit; i<iit+100; ++i)
  144. {
  145. if(base[i]==(char)(10) || base[i]==(char)(9))
  146. {
  147. nher->low3stack = atoi(base.substr(iit, i-iit).c_str());
  148. iit=i+1;
  149. break;
  150. }
  151. }
  152. for(int i=iit; i<iit+100; ++i)
  153. {
  154. if(base[i]==(char)(10) || base[i]==(char)(9))
  155. {
  156. nher->high3stack = atoi(base.substr(iit, i-iit).c_str());
  157. iit=i+1;
  158. break;
  159. }
  160. }
  161. nher->refType3stack = base.substr(iit, base.size()-iit);
  162. nher->ID=ID++;
  163. heroes.push_back(nher);
  164. delete[500] tab;
  165. }
  166. loadSpecialAbilities();
  167. }
  168. void CHeroHandler::loadSpecialAbilities()
  169. {
  170. std::ifstream inp("H3bitmap.lod\\HEROSPEC.txt", std::ios::in);
  171. std::string dump;
  172. for(int i=0; i<7; ++i)
  173. {
  174. inp>>dump;
  175. }
  176. inp.ignore();
  177. int whHero=0;
  178. while(!inp.eof() && whHero<heroes.size())
  179. {
  180. std::string base;
  181. char * tab = new char[500];
  182. int iitBef = 0;
  183. int iit = 0;
  184. inp.getline(tab, 500);
  185. base = std::string(tab);
  186. if(base.size()<2) //ended, but some rubbish could still stay end we have something useless
  187. {
  188. inp.close();
  189. return; //add counter
  190. }
  191. while(base[iit]!='\t')
  192. {
  193. ++iit;
  194. }
  195. heroes[whHero]->bonusName = base.substr(0, iit);
  196. ++iit;
  197. iitBef=iit;
  198. if(heroes[whHero]->bonusName == std::string("Ogry"))
  199. {
  200. char * tab2 = new char[500];
  201. inp.getline(tab2, 500);
  202. base += std::string(tab2);
  203. delete [500] tab2;
  204. }
  205. while(base[iit]!='\t')
  206. {
  207. ++iit;
  208. }
  209. heroes[whHero]->shortBonus = base.substr(iitBef, iit-iitBef);
  210. ++iit;
  211. iitBef=iit;
  212. while(base[iit]!='\t' && iit<base.size())
  213. {
  214. ++iit;
  215. }
  216. heroes[whHero]->longBonus = base.substr(iitBef, iit-iitBef);
  217. ++whHero;
  218. delete [500] tab;
  219. }
  220. inp.close();
  221. }
  222. void CHeroHandler::loadBiographies()
  223. {
  224. std::ifstream inp("H3bitmap.lod\\HEROBIOS.TXT", std::ios::in | std::ios::binary);
  225. inp.seekg(0,std::ios::end); // na koniec
  226. int andame = inp.tellg(); // read length
  227. inp.seekg(0,std::ios::beg); // wracamy na poczatek
  228. char * bufor = new char[andame]; // allocate memory
  229. inp.read((char*)bufor, andame); // read map file to buffer
  230. inp.close();
  231. std::string buf = std::string(bufor);
  232. delete [andame] bufor;
  233. int i = 0; //buf iterator
  234. for(int q=0; q<heroes.size(); ++q)
  235. {
  236. int befi=i;
  237. for(i; i<andame; ++i)
  238. {
  239. if(buf[i]=='\r')
  240. break;
  241. }
  242. heroes[q]->biography = buf.substr(befi, i-befi);
  243. i+=2;
  244. }
  245. }
  246. void CHeroHandler::loadHeroClasses()
  247. {
  248. //std::ifstream inp("H3bitmap.lod\\HCTRAITS.TXT", std::ios::in | std::ios::binary);
  249. //inp.seekg(0,std::ios::end); // na koniec
  250. //int andame = inp.tellg(); // read length
  251. //inp.seekg(0,std::ios::beg); // wracamy na poczatek
  252. //char * bufor = new char[andame]; // allocate memory
  253. //inp.read((char*)bufor, andame); // read map file to buffer
  254. //inp.close();
  255. //std::string buf = std::string(bufor);
  256. //delete [andame] bufor;
  257. std::string buf = CGameInfo::mainObj->bitmaph->getTextFile("HCTRAITS.TXT");
  258. int andame = buf.size();
  259. for(int y=0; y<andame; ++y)
  260. if(buf[y]==',')
  261. buf[y]='.';
  262. int i = 0; //buf iterator
  263. int hmcr = 0;
  264. for(i; i<andame; ++i) //omitting rubbish
  265. {
  266. if(buf[i]=='\r')
  267. ++hmcr;
  268. if(hmcr==2)
  269. break;
  270. }
  271. i+=2;
  272. for(int ss=0; ss<18; ++ss) //18 classes of hero (including conflux)
  273. {
  274. CHeroClass * hc = new CHeroClass;
  275. int befi=i;
  276. for(i; i<andame; ++i)
  277. {
  278. if(buf[i]=='\t')
  279. break;
  280. }
  281. hc->name = buf.substr(befi, i-befi);
  282. ++i;
  283. befi=i;
  284. for(i; i<andame; ++i)
  285. {
  286. if(buf[i]=='\t')
  287. break;
  288. }
  289. hc->aggression = atof(buf.substr(befi, i-befi).c_str());
  290. ++i;
  291. befi=i;
  292. for(i; i<andame; ++i)
  293. {
  294. if(buf[i]=='\t')
  295. break;
  296. }
  297. hc->initialAttack = atoi(buf.substr(befi, i-befi).c_str());
  298. ++i;
  299. befi=i;
  300. for(i; i<andame; ++i)
  301. {
  302. if(buf[i]=='\t')
  303. break;
  304. }
  305. hc->initialDefence = atoi(buf.substr(befi, i-befi).c_str());
  306. ++i;
  307. befi=i;
  308. for(i; i<andame; ++i)
  309. {
  310. if(buf[i]=='\t')
  311. break;
  312. }
  313. hc->initialPower = atoi(buf.substr(befi, i-befi).c_str());
  314. ++i;
  315. befi=i;
  316. for(i; i<andame; ++i)
  317. {
  318. if(buf[i]=='\t')
  319. break;
  320. }
  321. hc->initialKnowledge = atoi(buf.substr(befi, i-befi).c_str());
  322. ++i;
  323. befi=i;
  324. for(i; i<andame; ++i)
  325. {
  326. if(buf[i]=='\t')
  327. break;
  328. }
  329. hc->proAttack[0] = atoi(buf.substr(befi, i-befi).c_str());
  330. ++i;
  331. befi=i;
  332. for(i; i<andame; ++i)
  333. {
  334. if(buf[i]=='\t')
  335. break;
  336. }
  337. hc->proDefence[0] = atoi(buf.substr(befi, i-befi).c_str());
  338. ++i;
  339. befi=i;
  340. for(i; i<andame; ++i)
  341. {
  342. if(buf[i]=='\t')
  343. break;
  344. }
  345. hc->proPower[0] = atoi(buf.substr(befi, i-befi).c_str());
  346. ++i;
  347. befi=i;
  348. for(i; i<andame; ++i)
  349. {
  350. if(buf[i]=='\t')
  351. break;
  352. }
  353. hc->proKnowledge[0] = atoi(buf.substr(befi, i-befi).c_str());
  354. ++i;
  355. befi=i;
  356. for(i; i<andame; ++i)
  357. {
  358. if(buf[i]=='\t')
  359. break;
  360. }
  361. hc->proAttack[1] = atoi(buf.substr(befi, i-befi).c_str());
  362. ++i;
  363. befi=i;
  364. for(i; i<andame; ++i)
  365. {
  366. if(buf[i]=='\t')
  367. break;
  368. }
  369. hc->proDefence[1] = atoi(buf.substr(befi, i-befi).c_str());
  370. ++i;
  371. befi=i;
  372. for(i; i<andame; ++i)
  373. {
  374. if(buf[i]=='\t')
  375. break;
  376. }
  377. hc->proPower[1] = atoi(buf.substr(befi, i-befi).c_str());
  378. ++i;
  379. befi=i;
  380. for(i; i<andame; ++i)
  381. {
  382. if(buf[i]=='\t')
  383. break;
  384. }
  385. hc->proKnowledge[1] = atoi(buf.substr(befi, i-befi).c_str());
  386. ++i;
  387. //CHero kkk = heroes[0];
  388. for(int dd=0; dd<CGameInfo::mainObj->abilh->abilities.size(); ++dd)
  389. {
  390. befi=i;
  391. for(i; i<andame; ++i)
  392. {
  393. if(buf[i]=='\t')
  394. break;
  395. }
  396. int buff = atoi(buf.substr(befi, i-befi).c_str());
  397. ++i;
  398. hc->proSec.push_back(buff);
  399. }
  400. for(int dd=0; dd<9; ++dd)
  401. {
  402. befi=i;
  403. for(i; i<andame; ++i)
  404. {
  405. if(buf[i]=='\t' || buf[i]=='\r')
  406. break;
  407. }
  408. hc->selectionProbability[dd] = atoi(buf.substr(befi, i-befi).c_str());
  409. ++i;
  410. }
  411. ++i;
  412. heroClasses.push_back(hc);
  413. }
  414. }
  415. void CHeroHandler::initHeroClasses()
  416. {
  417. for(int gg=0; gg<heroes.size(); ++gg)
  418. {
  419. heroes[gg]->heroClass = heroClasses[heroes[gg]->heroType];
  420. }
  421. }