CHeroHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. for(int x=0;x<3;x++)
  105. {
  106. CGeneralTextHandler::loadToIt(pom,buf,it,4);
  107. nher->lowStack[x] = atoi(pom.c_str());
  108. CGeneralTextHandler::loadToIt(pom,buf,it,4);
  109. nher->highStack[x] = atoi(pom.c_str());
  110. CGeneralTextHandler::loadToIt(nher->refTypeStack[x],buf,it,(x==2) ? (3) : (4));
  111. int hlp = nher->refTypeStack[x].find_first_of(' ',0);
  112. if(hlp>=0)
  113. nher->refTypeStack[x].replace(hlp,1,"");
  114. }
  115. nher->ID = heroes.size();
  116. heroes.push_back(nher);
  117. }
  118. loadSpecialAbilities();
  119. loadBiographies();
  120. loadHeroClasses();
  121. initHeroClasses();
  122. expPerLevel.push_back(0);
  123. expPerLevel.push_back(1000);
  124. expPerLevel.push_back(2000);
  125. expPerLevel.push_back(3200);
  126. expPerLevel.push_back(4500);
  127. expPerLevel.push_back(6000);
  128. expPerLevel.push_back(7700);
  129. expPerLevel.push_back(9000);
  130. expPerLevel.push_back(11000);
  131. expPerLevel.push_back(13200);
  132. expPerLevel.push_back(15500);
  133. expPerLevel.push_back(18500);
  134. expPerLevel.push_back(22100);
  135. expPerLevel.push_back(26420);
  136. expPerLevel.push_back(31604);
  137. return;
  138. }
  139. void CHeroHandler::loadSpecialAbilities()
  140. {
  141. std::string buf = CGameInfo::mainObj->bitmaph->getTextFile("HEROSPEC.TXT");
  142. int it=0;
  143. std::string dump;
  144. for(int i=0; i<2; ++i)
  145. {
  146. CGeneralTextHandler::loadToIt(dump,buf,it,3);
  147. }
  148. for (int i=0;i<heroes.size();i++)
  149. {
  150. CGeneralTextHandler::loadToIt(heroes[i]->bonusName,buf,it,4);
  151. CGeneralTextHandler::loadToIt(heroes[i]->shortBonus,buf,it,4);
  152. CGeneralTextHandler::loadToIt(heroes[i]->longBonus,buf,it,3);
  153. }
  154. }
  155. void CHeroHandler::loadBiographies()
  156. {
  157. std::string buf = CGameInfo::mainObj->bitmaph->getTextFile("HEROBIOS.TXT");
  158. int it=0;
  159. for (int i=0;i<heroes.size();i++)
  160. {
  161. CGeneralTextHandler::loadToIt(heroes[i]->biography,buf,it,3);
  162. }
  163. }
  164. void CHeroHandler::loadHeroClasses()
  165. {
  166. std::string buf = CGameInfo::mainObj->bitmaph->getTextFile("HCTRAITS.TXT");
  167. int andame = buf.size();
  168. for(int y=0; y<andame; ++y)
  169. if(buf[y]==',')
  170. buf[y]='.';
  171. int i = 0; //buf iterator
  172. int hmcr = 0;
  173. for(i; i<andame; ++i) //omitting rubbish
  174. {
  175. if(buf[i]=='\r')
  176. ++hmcr;
  177. if(hmcr==2)
  178. break;
  179. }
  180. i+=2;
  181. for(int ss=0; ss<18; ++ss) //18 classes of hero (including conflux)
  182. {
  183. CHeroClass * hc = new CHeroClass;
  184. int befi=i;
  185. for(i; i<andame; ++i)
  186. {
  187. if(buf[i]=='\t')
  188. break;
  189. }
  190. hc->name = buf.substr(befi, i-befi);
  191. ++i;
  192. befi=i;
  193. for(i; i<andame; ++i)
  194. {
  195. if(buf[i]=='\t')
  196. break;
  197. }
  198. hc->aggression = atof(buf.substr(befi, i-befi).c_str());
  199. ++i;
  200. befi=i;
  201. for(i; i<andame; ++i)
  202. {
  203. if(buf[i]=='\t')
  204. break;
  205. }
  206. hc->initialAttack = atoi(buf.substr(befi, i-befi).c_str());
  207. ++i;
  208. befi=i;
  209. for(i; i<andame; ++i)
  210. {
  211. if(buf[i]=='\t')
  212. break;
  213. }
  214. hc->initialDefence = atoi(buf.substr(befi, i-befi).c_str());
  215. ++i;
  216. befi=i;
  217. for(i; i<andame; ++i)
  218. {
  219. if(buf[i]=='\t')
  220. break;
  221. }
  222. hc->initialPower = atoi(buf.substr(befi, i-befi).c_str());
  223. ++i;
  224. befi=i;
  225. for(i; i<andame; ++i)
  226. {
  227. if(buf[i]=='\t')
  228. break;
  229. }
  230. hc->initialKnowledge = atoi(buf.substr(befi, i-befi).c_str());
  231. ++i;
  232. hc->primChance.resize(PRIMARY_SKILLS);
  233. for(int x=0;x<PRIMARY_SKILLS;x++)
  234. {
  235. befi=i;
  236. for(i; i<andame; ++i)
  237. {
  238. if(buf[i]=='\t')
  239. break;
  240. }
  241. hc->primChance[x].first = atoi(buf.substr(befi, i-befi).c_str());
  242. ++i;
  243. }
  244. for(int x=0;x<PRIMARY_SKILLS;x++)
  245. {
  246. befi=i;
  247. for(i; i<andame; ++i)
  248. {
  249. if(buf[i]=='\t')
  250. break;
  251. }
  252. hc->primChance[x].second = atoi(buf.substr(befi, i-befi).c_str());
  253. ++i;
  254. }
  255. //CHero kkk = heroes[0];
  256. for(int dd=0; dd<CGameInfo::mainObj->abilh->abilities.size(); ++dd)
  257. {
  258. befi=i;
  259. for(i; i<andame; ++i)
  260. {
  261. if(buf[i]=='\t')
  262. break;
  263. }
  264. int buff = atoi(buf.substr(befi, i-befi).c_str());
  265. ++i;
  266. hc->proSec.push_back(buff);
  267. }
  268. for(int dd=0; dd<9; ++dd)
  269. {
  270. befi=i;
  271. for(i; i<andame; ++i)
  272. {
  273. if(buf[i]=='\t' || buf[i]=='\r')
  274. break;
  275. }
  276. hc->selectionProbability[dd] = atoi(buf.substr(befi, i-befi).c_str());
  277. ++i;
  278. }
  279. ++i;
  280. std::stringstream nm;
  281. nm<<"AH";
  282. nm<<std::setw(2);
  283. nm<<std::setfill('0');
  284. nm<<heroClasses.size();
  285. nm<<"_.DEF";
  286. hc->moveAnim = CGI->spriteh->giveDef(nm.str());
  287. for(int o=0; o<hc->moveAnim->ourImages.size(); ++o)
  288. {
  289. if(hc->moveAnim->ourImages[o].groupNumber==6)
  290. {
  291. for(int e=0; e<8; ++e)
  292. {
  293. Cimage nci;
  294. nci.bitmap = CSDL_Ext::rotate01(hc->moveAnim->ourImages[o+e].bitmap);
  295. nci.groupNumber = 10;
  296. nci.imName = std::string();
  297. hc->moveAnim->ourImages.push_back(nci);
  298. }
  299. o+=8;
  300. }
  301. if(hc->moveAnim->ourImages[o].groupNumber==7)
  302. {
  303. for(int e=0; e<8; ++e)
  304. {
  305. Cimage nci;
  306. nci.bitmap = CSDL_Ext::rotate01(hc->moveAnim->ourImages[o+e].bitmap);
  307. nci.groupNumber = 11;
  308. nci.imName = std::string();
  309. hc->moveAnim->ourImages.push_back(nci);
  310. }
  311. o+=8;
  312. }
  313. if(hc->moveAnim->ourImages[o].groupNumber==8)
  314. {
  315. for(int e=0; e<8; ++e)
  316. {
  317. Cimage nci;
  318. nci.bitmap = CSDL_Ext::rotate01(hc->moveAnim->ourImages[o+e].bitmap);
  319. nci.groupNumber = 12;
  320. nci.imName = std::string();
  321. hc->moveAnim->ourImages.push_back(nci);
  322. }
  323. o+=8;
  324. }
  325. }
  326. for(int o=0; o<hc->moveAnim->ourImages.size(); ++o)
  327. {
  328. if(hc->moveAnim->ourImages[o].groupNumber==1)
  329. {
  330. Cimage nci;
  331. nci.bitmap = CSDL_Ext::rotate01(hc->moveAnim->ourImages[o].bitmap);
  332. nci.groupNumber = 13;
  333. nci.imName = std::string();
  334. hc->moveAnim->ourImages.push_back(nci);
  335. //o+=1;
  336. }
  337. if(hc->moveAnim->ourImages[o].groupNumber==2)
  338. {
  339. Cimage nci;
  340. nci.bitmap = CSDL_Ext::rotate01(hc->moveAnim->ourImages[o].bitmap);
  341. nci.groupNumber = 14;
  342. nci.imName = std::string();
  343. hc->moveAnim->ourImages.push_back(nci);
  344. //o+=1;
  345. }
  346. if(hc->moveAnim->ourImages[o].groupNumber==3)
  347. {
  348. Cimage nci;
  349. nci.bitmap = CSDL_Ext::rotate01(hc->moveAnim->ourImages[o].bitmap);
  350. nci.groupNumber = 15;
  351. nci.imName = std::string();
  352. hc->moveAnim->ourImages.push_back(nci);
  353. //o+=1;
  354. }
  355. }
  356. for(int ff=0; ff<hc->moveAnim->ourImages.size(); ++ff)
  357. {
  358. CSDL_Ext::alphaTransform(hc->moveAnim->ourImages[ff].bitmap);
  359. }
  360. hc->moveAnim->alphaTransformed = true;
  361. heroClasses.push_back(hc);
  362. }
  363. }
  364. void CHeroHandler::initHeroClasses()
  365. {
  366. for(int gg=0; gg<heroes.size(); ++gg)
  367. {
  368. heroes[gg]->heroClass = heroClasses[heroes[gg]->heroType];
  369. }
  370. initTerrainCosts();
  371. }
  372. unsigned int CHeroHandler::level(unsigned int experience)
  373. {
  374. int add=0;
  375. while(experience>=expPerLevel[expPerLevel.size()-1])
  376. {
  377. experience/=1.2;
  378. add+=1;
  379. }
  380. for(int i=expPerLevel.size()-1; i>=0; --i)
  381. {
  382. if(experience>=expPerLevel[i])
  383. return i+add;
  384. }
  385. return -1;
  386. }
  387. unsigned int CHeroHandler::reqExp(unsigned int level)
  388. {
  389. level-=1;
  390. if(level<=expPerLevel.size())
  391. return expPerLevel[level];
  392. else
  393. {
  394. unsigned int exp = expPerLevel[expPerLevel.size()-1];
  395. level-=expPerLevel.size();
  396. while(level>0)
  397. {
  398. --level;
  399. exp*=1.2;
  400. }
  401. }
  402. return -1;
  403. }
  404. void CHeroHandler::initTerrainCosts()
  405. {
  406. std::ifstream inp;
  407. inp.open("config\\TERCOSTS.TXT", std::ios_base::in|std::ios_base::binary);
  408. int tynum;
  409. inp>>tynum;
  410. for(int i=0; i<2*tynum; i+=2)
  411. {
  412. int catNum;
  413. inp>>catNum;
  414. for(int k=0; k<catNum; ++k)
  415. {
  416. int curCost;
  417. inp>>curCost;
  418. heroClasses[i]->terrCosts.push_back(curCost);
  419. heroClasses[i+1]->terrCosts.push_back(curCost);
  420. }
  421. }
  422. inp.close();
  423. }