CHeroHandler.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CHeroHandler.h"
  4. #include <sstream>
  5. #include "CLodHandler.h"
  6. #include "../lib/VCMI_Lib.h"
  7. extern CLodHandler * bitmaph;
  8. void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  9. CHeroHandler::~CHeroHandler()
  10. {}
  11. void CHeroHandler::loadPortraits()
  12. {
  13. std::string strs = bitmaph->getTextFile("PRISKILL.TXT");
  14. int itr=0;
  15. for (int i=0; i<PRIMARY_SKILLS; i++)
  16. {
  17. std::string tmp;
  18. loadToIt(tmp, strs, itr, 3);
  19. pskillsn.push_back(tmp);
  20. }
  21. }
  22. void CHeroHandler::loadHeroes()
  23. {
  24. VLC->heroh = this;
  25. int ID=0;
  26. std::string buf = bitmaph->getTextFile("HOTRAITS.TXT");
  27. int it=0;
  28. std::string dump;
  29. for(int i=0; i<2; ++i)
  30. {
  31. loadToIt(dump,buf,it,3);
  32. }
  33. int numberOfCurrentClassHeroes = 0;
  34. int currentClass = 0;
  35. int additHero = 0;
  36. EHeroClasses addTab[12];
  37. addTab[0] = HERO_KNIGHT;
  38. addTab[1] = HERO_WITCH;
  39. addTab[2] = HERO_KNIGHT;
  40. addTab[3] = HERO_WIZARD;
  41. addTab[4] = HERO_RANGER;
  42. addTab[5] = HERO_BARBARIAN;
  43. addTab[6] = HERO_DEATHKNIGHT;
  44. addTab[7] = HERO_WARLOCK;
  45. addTab[8] = HERO_KNIGHT;
  46. addTab[9] = HERO_WARLOCK;
  47. addTab[10] = HERO_BARBARIAN;
  48. addTab[11] = HERO_DEMONIAC;
  49. for (int i=0; i<HEROES_QUANTITY; i++)
  50. {
  51. CHero * nher = new CHero;
  52. if(currentClass<18)
  53. {
  54. nher->heroType = (EHeroClasses)currentClass;
  55. ++numberOfCurrentClassHeroes;
  56. if(numberOfCurrentClassHeroes==8)
  57. {
  58. numberOfCurrentClassHeroes = 0;
  59. ++currentClass;
  60. }
  61. }
  62. else
  63. {
  64. nher->heroType = addTab[additHero++];
  65. }
  66. std::string pom ;
  67. loadToIt(nher->name,buf,it,4);
  68. for(int x=0;x<3;x++)
  69. {
  70. loadToIt(pom,buf,it,4);
  71. nher->lowStack[x] = atoi(pom.c_str());
  72. loadToIt(pom,buf,it,4);
  73. nher->highStack[x] = atoi(pom.c_str());
  74. loadToIt(nher->refTypeStack[x],buf,it,(x==2) ? (3) : (4));
  75. int hlp = nher->refTypeStack[x].find_first_of(' ',0);
  76. if(hlp>=0)
  77. nher->refTypeStack[x].replace(hlp,1,"");
  78. }
  79. nher->ID = heroes.size();
  80. heroes.push_back(nher);
  81. }
  82. loadSpecialAbilities();
  83. loadBiographies();
  84. loadHeroClasses();
  85. initHeroClasses();
  86. expPerLevel.push_back(0);
  87. expPerLevel.push_back(1000);
  88. expPerLevel.push_back(2000);
  89. expPerLevel.push_back(3200);
  90. expPerLevel.push_back(4500);
  91. expPerLevel.push_back(6000);
  92. expPerLevel.push_back(7700);
  93. expPerLevel.push_back(9000);
  94. expPerLevel.push_back(11000);
  95. expPerLevel.push_back(13200);
  96. expPerLevel.push_back(15500);
  97. expPerLevel.push_back(18500);
  98. expPerLevel.push_back(22100);
  99. expPerLevel.push_back(26420);
  100. expPerLevel.push_back(31604);
  101. return;
  102. }
  103. void CHeroHandler::loadSpecialAbilities()
  104. {
  105. std::string buf = bitmaph->getTextFile("HEROSPEC.TXT");
  106. int it=0;
  107. std::string dump;
  108. for(int i=0; i<2; ++i)
  109. {
  110. loadToIt(dump,buf,it,3);
  111. }
  112. for (int i=0;i<heroes.size();i++)
  113. {
  114. loadToIt(heroes[i]->bonusName,buf,it,4);
  115. loadToIt(heroes[i]->shortBonus,buf,it,4);
  116. loadToIt(heroes[i]->longBonus,buf,it,3);
  117. }
  118. }
  119. void CHeroHandler::loadBiographies()
  120. {
  121. std::string buf = bitmaph->getTextFile("HEROBIOS.TXT");
  122. int it=0;
  123. for (int i=0;i<heroes.size();i++)
  124. {
  125. loadToIt(heroes[i]->biography,buf,it,3);
  126. }
  127. }
  128. void CHeroHandler::loadHeroClasses()
  129. {
  130. std::string buf = bitmaph->getTextFile("HCTRAITS.TXT");
  131. int andame = buf.size();
  132. for(int y=0; y<andame; ++y)
  133. if(buf[y]==',')
  134. buf[y]='.';
  135. int i = 0; //buf iterator
  136. int hmcr = 0;
  137. for(i; i<andame; ++i) //omitting rubbish
  138. {
  139. if(buf[i]=='\r')
  140. ++hmcr;
  141. if(hmcr==2)
  142. break;
  143. }
  144. i+=2;
  145. for(int ss=0; ss<18; ++ss) //18 classes of hero (including conflux)
  146. {
  147. CHeroClass * hc = new CHeroClass;
  148. int befi=i;
  149. for(i; i<andame; ++i)
  150. {
  151. if(buf[i]=='\t')
  152. break;
  153. }
  154. hc->name = buf.substr(befi, i-befi);
  155. ++i;
  156. befi=i;
  157. for(i; i<andame; ++i)
  158. {
  159. if(buf[i]=='\t')
  160. break;
  161. }
  162. hc->aggression = atof(buf.substr(befi, i-befi).c_str());
  163. ++i;
  164. befi=i;
  165. for(i; i<andame; ++i)
  166. {
  167. if(buf[i]=='\t')
  168. break;
  169. }
  170. hc->initialAttack = atoi(buf.substr(befi, i-befi).c_str());
  171. ++i;
  172. befi=i;
  173. for(i; i<andame; ++i)
  174. {
  175. if(buf[i]=='\t')
  176. break;
  177. }
  178. hc->initialDefence = atoi(buf.substr(befi, i-befi).c_str());
  179. ++i;
  180. befi=i;
  181. for(i; i<andame; ++i)
  182. {
  183. if(buf[i]=='\t')
  184. break;
  185. }
  186. hc->initialPower = atoi(buf.substr(befi, i-befi).c_str());
  187. ++i;
  188. befi=i;
  189. for(i; i<andame; ++i)
  190. {
  191. if(buf[i]=='\t')
  192. break;
  193. }
  194. hc->initialKnowledge = atoi(buf.substr(befi, i-befi).c_str());
  195. ++i;
  196. hc->primChance.resize(PRIMARY_SKILLS);
  197. for(int x=0;x<PRIMARY_SKILLS;x++)
  198. {
  199. befi=i;
  200. for(i; i<andame; ++i)
  201. {
  202. if(buf[i]=='\t')
  203. break;
  204. }
  205. hc->primChance[x].first = atoi(buf.substr(befi, i-befi).c_str());
  206. ++i;
  207. }
  208. for(int x=0;x<PRIMARY_SKILLS;x++)
  209. {
  210. befi=i;
  211. for(i; i<andame; ++i)
  212. {
  213. if(buf[i]=='\t')
  214. break;
  215. }
  216. hc->primChance[x].second = atoi(buf.substr(befi, i-befi).c_str());
  217. ++i;
  218. }
  219. //CHero kkk = heroes[0];
  220. for(int dd=0; dd<SKILL_QUANTITY; ++dd)
  221. {
  222. befi=i;
  223. for(i; i<andame; ++i)
  224. {
  225. if(buf[i]=='\t')
  226. break;
  227. }
  228. int buff = atoi(buf.substr(befi, i-befi).c_str());
  229. ++i;
  230. hc->proSec.push_back(buff);
  231. }
  232. for(int dd=0; dd<9; ++dd)
  233. {
  234. befi=i;
  235. for(i; i<andame; ++i)
  236. {
  237. if(buf[i]=='\t' || buf[i]=='\r')
  238. break;
  239. }
  240. hc->selectionProbability[dd] = atoi(buf.substr(befi, i-befi).c_str());
  241. ++i;
  242. }
  243. ++i;
  244. heroClasses.push_back(hc);
  245. }
  246. }
  247. void CHeroHandler::initHeroClasses()
  248. {
  249. for(int gg=0; gg<heroes.size(); ++gg)
  250. {
  251. heroes[gg]->heroClass = heroClasses[heroes[gg]->heroType];
  252. }
  253. initTerrainCosts();
  254. }
  255. unsigned int CHeroHandler::level(unsigned int experience)
  256. {
  257. int add=0;
  258. while(experience>=expPerLevel[expPerLevel.size()-1])
  259. {
  260. experience/=1.2;
  261. add+=1;
  262. }
  263. for(int i=expPerLevel.size()-1; i>=0; --i)
  264. {
  265. if(experience>=expPerLevel[i])
  266. return i+add;
  267. }
  268. return -1;
  269. }
  270. unsigned int CHeroHandler::reqExp(unsigned int level)
  271. {
  272. level-=1;
  273. if(level<=expPerLevel.size())
  274. return expPerLevel[level];
  275. else
  276. {
  277. unsigned int exp = expPerLevel[expPerLevel.size()-1];
  278. level-=expPerLevel.size();
  279. while(level>0)
  280. {
  281. --level;
  282. exp*=1.2;
  283. }
  284. }
  285. return -1;
  286. }
  287. void CHeroHandler::initTerrainCosts()
  288. {
  289. std::ifstream inp;
  290. inp.open("config\\TERCOSTS.TXT", std::ios_base::in|std::ios_base::binary);
  291. int tynum;
  292. inp>>tynum;
  293. for(int i=0; i<2*tynum; i+=2)
  294. {
  295. int catNum;
  296. inp>>catNum;
  297. for(int k=0; k<catNum; ++k)
  298. {
  299. int curCost;
  300. inp>>curCost;
  301. heroClasses[i]->terrCosts.push_back(curCost);
  302. heroClasses[i+1]->terrCosts.push_back(curCost);
  303. }
  304. }
  305. inp.close();
  306. }