2
0

CHeroHandler.cpp 7.2 KB

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