CHeroHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. #include "../stdafx.h"
  2. #include "CHeroHandler.h"
  3. #include "../CGameInfo.h"
  4. #include <sstream>
  5. #include "../CGameInfo.h"
  6. #include "CGeneralTextHandler.h"
  7. #include "CLodHandler.h"
  8. #include "CAbilityHandler.h"
  9. #include "../SDL_Extensions.h"
  10. #include <cmath>
  11. #include <iomanip>
  12. CHeroHandler::~CHeroHandler()
  13. {
  14. for (int j=0;j<heroes.size();j++)
  15. {
  16. if (heroes[j]->portraitSmall)
  17. SDL_FreeSurface(heroes[j]->portraitSmall);
  18. delete heroes[j];
  19. }
  20. }
  21. void CHeroHandler::loadPortraits()
  22. {
  23. std::ifstream of("config/portrety.txt");
  24. for (int j=0;j<heroes.size();j++)
  25. {
  26. int ID;
  27. of>>ID;
  28. std::string path;
  29. of>>path;
  30. heroes[ID]->portraitSmall=CGI->bitmaph->loadBitmap(path);
  31. if (!heroes[ID]->portraitSmall)
  32. std::cout<<"Can't read small portrait for "<<ID<<" ("<<path<<")\n";
  33. for(int ff=0; ff<path.size(); ++ff) //size letter is usually third one, but there are exceptions an it should fix the problem
  34. {
  35. if(path[ff]=='S')
  36. {
  37. path[ff]='L';
  38. break;
  39. }
  40. }
  41. heroes[ID]->portraitLarge=CGI->bitmaph->loadBitmap(path);
  42. if (!heroes[ID]->portraitLarge)
  43. std::cout<<"Can't read large portrait for "<<ID<<" ("<<path<<")\n";
  44. SDL_SetColorKey(heroes[ID]->portraitLarge,SDL_SRCCOLORKEY,SDL_MapRGB(heroes[ID]->portraitLarge->format,0,255,255));
  45. }
  46. of.close();
  47. pskillsb = CGI->spriteh->giveDef("PSKILL.DEF");
  48. resources = CGI->spriteh->giveDef("RESOUR82.DEF");
  49. un44 = CGI->spriteh->giveDef("UN44.DEF");
  50. std::string strs = CGI->bitmaph->getTextFile("PRISKILL.TXT");
  51. int itr=0;
  52. for (int i=0; i<PRIMARY_SKILLS; i++)
  53. {
  54. std::string tmp;
  55. CGeneralTextHandler::loadToIt(tmp, strs, itr, 3);
  56. pskillsn.push_back(tmp);
  57. }
  58. }
  59. void CHeroHandler::loadHeroes()
  60. {
  61. int ID=0;
  62. std::string buf = CGameInfo::mainObj->bitmaph->getTextFile("HOTRAITS.TXT");
  63. int it=0;
  64. std::string dump;
  65. for(int i=0; i<2; ++i)
  66. {
  67. CGeneralTextHandler::loadToIt(dump,buf,it,3);
  68. }
  69. int numberOfCurrentClassHeroes = 0;
  70. int currentClass = 0;
  71. int additHero = 0;
  72. EHeroClasses addTab[12];
  73. addTab[0] = HERO_KNIGHT;
  74. addTab[1] = HERO_WITCH;
  75. addTab[2] = HERO_KNIGHT;
  76. addTab[3] = HERO_WIZARD;
  77. addTab[4] = HERO_RANGER;
  78. addTab[5] = HERO_BARBARIAN;
  79. addTab[6] = HERO_DEATHKNIGHT;
  80. addTab[7] = HERO_WARLOCK;
  81. addTab[8] = HERO_KNIGHT;
  82. addTab[9] = HERO_WARLOCK;
  83. addTab[10] = HERO_BARBARIAN;
  84. addTab[11] = HERO_DEMONIAC;
  85. for (int i=0; i<HEROES_QUANTITY; i++)
  86. {
  87. CHero * nher = new CHero;
  88. if(currentClass<18)
  89. {
  90. nher->heroType = (EHeroClasses)currentClass;
  91. ++numberOfCurrentClassHeroes;
  92. if(numberOfCurrentClassHeroes==8)
  93. {
  94. numberOfCurrentClassHeroes = 0;
  95. ++currentClass;
  96. }
  97. }
  98. else
  99. {
  100. nher->heroType = addTab[additHero++];
  101. }
  102. std::string pom ;
  103. CGeneralTextHandler::loadToIt(nher->name,buf,it,4);
  104. CGeneralTextHandler::loadToIt(pom,buf,it,4);
  105. nher->low1stack = atoi(pom.c_str());
  106. CGeneralTextHandler::loadToIt(pom,buf,it,4);
  107. nher->high1stack = atoi(pom.c_str());
  108. CGeneralTextHandler::loadToIt(nher->refType1stack,buf,it,4);
  109. CGeneralTextHandler::loadToIt(pom,buf,it,4);
  110. nher->low2stack = atoi(pom.c_str());
  111. CGeneralTextHandler::loadToIt(pom,buf,it,4);
  112. nher->high2stack = atoi(pom.c_str());
  113. CGeneralTextHandler::loadToIt(nher->refType2stack,buf,it,4);
  114. CGeneralTextHandler::loadToIt(pom,buf,it,4);
  115. nher->low3stack = atoi(pom.c_str());
  116. CGeneralTextHandler::loadToIt(pom,buf,it,4);
  117. nher->high3stack = atoi(pom.c_str());
  118. CGeneralTextHandler::loadToIt(nher->refType3stack,buf,it,3);
  119. nher->ID = heroes.size();
  120. heroes.push_back(nher);
  121. }
  122. loadSpecialAbilities();
  123. loadBiographies();
  124. loadHeroClasses();
  125. initHeroClasses();
  126. expPerLevel.push_back(0);
  127. expPerLevel.push_back(1000);
  128. expPerLevel.push_back(2000);
  129. expPerLevel.push_back(3200);
  130. expPerLevel.push_back(4500);
  131. expPerLevel.push_back(6000);
  132. expPerLevel.push_back(7700);
  133. expPerLevel.push_back(9000);
  134. expPerLevel.push_back(11000);
  135. expPerLevel.push_back(13200);
  136. expPerLevel.push_back(15500);
  137. expPerLevel.push_back(18500);
  138. expPerLevel.push_back(22100);
  139. expPerLevel.push_back(26420);
  140. expPerLevel.push_back(31604);
  141. return;
  142. }
  143. void CHeroHandler::loadSpecialAbilities()
  144. {
  145. std::string buf = CGameInfo::mainObj->bitmaph->getTextFile("HEROSPEC.TXT");
  146. int it=0;
  147. std::string dump;
  148. for(int i=0; i<2; ++i)
  149. {
  150. CGeneralTextHandler::loadToIt(dump,buf,it,3);
  151. }
  152. for (int i=0;i<heroes.size();i++)
  153. {
  154. CGeneralTextHandler::loadToIt(heroes[i]->bonusName,buf,it,4);
  155. CGeneralTextHandler::loadToIt(heroes[i]->shortBonus,buf,it,4);
  156. CGeneralTextHandler::loadToIt(heroes[i]->longBonus,buf,it,3);
  157. }
  158. }
  159. void CHeroHandler::loadBiographies()
  160. {
  161. std::string buf = CGameInfo::mainObj->bitmaph->getTextFile("HEROBIOS.TXT");
  162. int it=0;
  163. for (int i=0;i<heroes.size();i++)
  164. {
  165. CGeneralTextHandler::loadToIt(heroes[i]->biography,buf,it,3);
  166. }
  167. }
  168. void CHeroHandler::loadHeroClasses()
  169. {
  170. std::string buf = CGameInfo::mainObj->bitmaph->getTextFile("HCTRAITS.TXT");
  171. int andame = buf.size();
  172. for(int y=0; y<andame; ++y)
  173. if(buf[y]==',')
  174. buf[y]='.';
  175. int i = 0; //buf iterator
  176. int hmcr = 0;
  177. for(i; i<andame; ++i) //omitting rubbish
  178. {
  179. if(buf[i]=='\r')
  180. ++hmcr;
  181. if(hmcr==2)
  182. break;
  183. }
  184. i+=2;
  185. for(int ss=0; ss<18; ++ss) //18 classes of hero (including conflux)
  186. {
  187. CHeroClass * hc = new CHeroClass;
  188. int befi=i;
  189. for(i; i<andame; ++i)
  190. {
  191. if(buf[i]=='\t')
  192. break;
  193. }
  194. hc->name = buf.substr(befi, i-befi);
  195. ++i;
  196. befi=i;
  197. for(i; i<andame; ++i)
  198. {
  199. if(buf[i]=='\t')
  200. break;
  201. }
  202. hc->aggression = atof(buf.substr(befi, i-befi).c_str());
  203. ++i;
  204. befi=i;
  205. for(i; i<andame; ++i)
  206. {
  207. if(buf[i]=='\t')
  208. break;
  209. }
  210. hc->initialAttack = atoi(buf.substr(befi, i-befi).c_str());
  211. ++i;
  212. befi=i;
  213. for(i; i<andame; ++i)
  214. {
  215. if(buf[i]=='\t')
  216. break;
  217. }
  218. hc->initialDefence = atoi(buf.substr(befi, i-befi).c_str());
  219. ++i;
  220. befi=i;
  221. for(i; i<andame; ++i)
  222. {
  223. if(buf[i]=='\t')
  224. break;
  225. }
  226. hc->initialPower = atoi(buf.substr(befi, i-befi).c_str());
  227. ++i;
  228. befi=i;
  229. for(i; i<andame; ++i)
  230. {
  231. if(buf[i]=='\t')
  232. break;
  233. }
  234. hc->initialKnowledge = atoi(buf.substr(befi, i-befi).c_str());
  235. ++i;
  236. befi=i;
  237. for(i; i<andame; ++i)
  238. {
  239. if(buf[i]=='\t')
  240. break;
  241. }
  242. hc->proAttack[0] = atoi(buf.substr(befi, i-befi).c_str());
  243. ++i;
  244. befi=i;
  245. for(i; i<andame; ++i)
  246. {
  247. if(buf[i]=='\t')
  248. break;
  249. }
  250. hc->proDefence[0] = atoi(buf.substr(befi, i-befi).c_str());
  251. ++i;
  252. befi=i;
  253. for(i; i<andame; ++i)
  254. {
  255. if(buf[i]=='\t')
  256. break;
  257. }
  258. hc->proPower[0] = atoi(buf.substr(befi, i-befi).c_str());
  259. ++i;
  260. befi=i;
  261. for(i; i<andame; ++i)
  262. {
  263. if(buf[i]=='\t')
  264. break;
  265. }
  266. hc->proKnowledge[0] = atoi(buf.substr(befi, i-befi).c_str());
  267. ++i;
  268. befi=i;
  269. for(i; i<andame; ++i)
  270. {
  271. if(buf[i]=='\t')
  272. break;
  273. }
  274. hc->proAttack[1] = atoi(buf.substr(befi, i-befi).c_str());
  275. ++i;
  276. befi=i;
  277. for(i; i<andame; ++i)
  278. {
  279. if(buf[i]=='\t')
  280. break;
  281. }
  282. hc->proDefence[1] = atoi(buf.substr(befi, i-befi).c_str());
  283. ++i;
  284. befi=i;
  285. for(i; i<andame; ++i)
  286. {
  287. if(buf[i]=='\t')
  288. break;
  289. }
  290. hc->proPower[1] = atoi(buf.substr(befi, i-befi).c_str());
  291. ++i;
  292. befi=i;
  293. for(i; i<andame; ++i)
  294. {
  295. if(buf[i]=='\t')
  296. break;
  297. }
  298. hc->proKnowledge[1] = atoi(buf.substr(befi, i-befi).c_str());
  299. ++i;
  300. //CHero kkk = heroes[0];
  301. for(int dd=0; dd<CGameInfo::mainObj->abilh->abilities.size(); ++dd)
  302. {
  303. befi=i;
  304. for(i; i<andame; ++i)
  305. {
  306. if(buf[i]=='\t')
  307. break;
  308. }
  309. int buff = atoi(buf.substr(befi, i-befi).c_str());
  310. ++i;
  311. hc->proSec.push_back(buff);
  312. }
  313. for(int dd=0; dd<9; ++dd)
  314. {
  315. befi=i;
  316. for(i; i<andame; ++i)
  317. {
  318. if(buf[i]=='\t' || buf[i]=='\r')
  319. break;
  320. }
  321. hc->selectionProbability[dd] = atoi(buf.substr(befi, i-befi).c_str());
  322. ++i;
  323. }
  324. ++i;
  325. std::stringstream nm;
  326. nm<<"AH";
  327. nm<<std::setw(2);
  328. nm<<std::setfill('0');
  329. nm<<heroClasses.size();
  330. nm<<"_.DEF";
  331. hc->moveAnim = CGI->spriteh->giveDef(nm.str());
  332. for(int o=0; o<hc->moveAnim->ourImages.size(); ++o)
  333. {
  334. if(hc->moveAnim->ourImages[o].groupNumber==6)
  335. {
  336. for(int e=0; e<8; ++e)
  337. {
  338. Cimage nci;
  339. nci.bitmap = CSDL_Ext::rotate01(hc->moveAnim->ourImages[o+e].bitmap);
  340. nci.groupNumber = 10;
  341. nci.imName = std::string();
  342. hc->moveAnim->ourImages.push_back(nci);
  343. }
  344. o+=8;
  345. }
  346. if(hc->moveAnim->ourImages[o].groupNumber==7)
  347. {
  348. for(int e=0; e<8; ++e)
  349. {
  350. Cimage nci;
  351. nci.bitmap = CSDL_Ext::rotate01(hc->moveAnim->ourImages[o+e].bitmap);
  352. nci.groupNumber = 11;
  353. nci.imName = std::string();
  354. hc->moveAnim->ourImages.push_back(nci);
  355. }
  356. o+=8;
  357. }
  358. if(hc->moveAnim->ourImages[o].groupNumber==8)
  359. {
  360. for(int e=0; e<8; ++e)
  361. {
  362. Cimage nci;
  363. nci.bitmap = CSDL_Ext::rotate01(hc->moveAnim->ourImages[o+e].bitmap);
  364. nci.groupNumber = 12;
  365. nci.imName = std::string();
  366. hc->moveAnim->ourImages.push_back(nci);
  367. }
  368. o+=8;
  369. }
  370. }
  371. for(int o=0; o<hc->moveAnim->ourImages.size(); ++o)
  372. {
  373. if(hc->moveAnim->ourImages[o].groupNumber==1)
  374. {
  375. Cimage nci;
  376. nci.bitmap = CSDL_Ext::rotate01(hc->moveAnim->ourImages[o].bitmap);
  377. nci.groupNumber = 13;
  378. nci.imName = std::string();
  379. hc->moveAnim->ourImages.push_back(nci);
  380. //o+=1;
  381. }
  382. if(hc->moveAnim->ourImages[o].groupNumber==2)
  383. {
  384. Cimage nci;
  385. nci.bitmap = CSDL_Ext::rotate01(hc->moveAnim->ourImages[o].bitmap);
  386. nci.groupNumber = 14;
  387. nci.imName = std::string();
  388. hc->moveAnim->ourImages.push_back(nci);
  389. //o+=1;
  390. }
  391. if(hc->moveAnim->ourImages[o].groupNumber==3)
  392. {
  393. Cimage nci;
  394. nci.bitmap = CSDL_Ext::rotate01(hc->moveAnim->ourImages[o].bitmap);
  395. nci.groupNumber = 15;
  396. nci.imName = std::string();
  397. hc->moveAnim->ourImages.push_back(nci);
  398. //o+=1;
  399. }
  400. }
  401. for(int ff=0; ff<hc->moveAnim->ourImages.size(); ++ff)
  402. {
  403. CSDL_Ext::fullAlphaTransform(hc->moveAnim->ourImages[ff].bitmap);
  404. }
  405. hc->moveAnim->alphaTransformed = true;
  406. heroClasses.push_back(hc);
  407. }
  408. }
  409. void CHeroHandler::initHeroClasses()
  410. {
  411. for(int gg=0; gg<heroes.size(); ++gg)
  412. {
  413. heroes[gg]->heroClass = heroClasses[heroes[gg]->heroType];
  414. }
  415. initTerrainCosts();
  416. }
  417. unsigned int CHeroHandler::level(unsigned int experience)
  418. {
  419. int add=0;
  420. while(experience>=expPerLevel[expPerLevel.size()-1])
  421. {
  422. experience/=1.2;
  423. add+=1;
  424. }
  425. for(int i=expPerLevel.size()-1; i>=0; --i)
  426. {
  427. if(experience>=expPerLevel[i])
  428. return i+add;
  429. }
  430. return -1;
  431. }
  432. unsigned int CHeroHandler::reqExp(unsigned int level)
  433. {
  434. level-=1;
  435. if(level<=expPerLevel.size())
  436. return expPerLevel[level];
  437. else
  438. {
  439. unsigned int exp = expPerLevel[expPerLevel.size()-1];
  440. level-=expPerLevel.size();
  441. while(level>0)
  442. {
  443. --level;
  444. exp*=1.2;
  445. }
  446. }
  447. return -1;
  448. }
  449. void CHeroHandler::initTerrainCosts()
  450. {
  451. std::ifstream inp;
  452. inp.open("config\\TERCOSTS.TXT", std::ios_base::in|std::ios_base::binary);
  453. int tynum;
  454. inp>>tynum;
  455. for(int i=0; i<2*tynum; i+=2)
  456. {
  457. int catNum;
  458. inp>>catNum;
  459. for(int k=0; k<catNum; ++k)
  460. {
  461. int curCost;
  462. inp>>curCost;
  463. heroClasses[i]->terrCosts.push_back(curCost);
  464. heroClasses[i+1]->terrCosts.push_back(curCost);
  465. }
  466. }
  467. inp.close();
  468. }