CHeroHandler.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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(const std::set<int> & possibles) const //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>::const_iterator i=possibles.begin(); i!=possibles.end(); i++)
  22. {
  23. totalProb += proSec[*i];
  24. }
  25. int ran = rand()%totalProb;
  26. for(std::set<int>::const_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. //loading initial secondary skills
  107. std::ifstream inp;
  108. inp.open("config" PATHSEPARATOR "heroes_sec_skills.txt", std::ios_base::in|std::ios_base::binary);
  109. if(!inp.is_open())
  110. {
  111. tlog1<<"missing file: config/heroes_sec_skills.txt"<<std::endl;
  112. }
  113. else
  114. {
  115. inp>>dump;
  116. int hid; //ID of currently read hero
  117. int secQ; //number of secondary abilities
  118. while(true)
  119. {
  120. inp>>hid;
  121. if(hid == -1)
  122. break;
  123. inp>>secQ;
  124. for(int g=0; g<secQ; ++g)
  125. {
  126. int a, b;
  127. inp>>a; inp>>b;
  128. heroes[hid]->secSkillsInit.push_back(std::make_pair(a, b));
  129. }
  130. }
  131. inp.close();
  132. }
  133. //initial skills loaded
  134. loadSpecialAbilities();
  135. loadBiographies();
  136. loadHeroClasses();
  137. initHeroClasses();
  138. expPerLevel.push_back(0);
  139. expPerLevel.push_back(1000);
  140. expPerLevel.push_back(2000);
  141. expPerLevel.push_back(3200);
  142. expPerLevel.push_back(4500);
  143. expPerLevel.push_back(6000);
  144. expPerLevel.push_back(7700);
  145. expPerLevel.push_back(9000);
  146. expPerLevel.push_back(11000);
  147. expPerLevel.push_back(13200);
  148. expPerLevel.push_back(15500);
  149. expPerLevel.push_back(18500);
  150. expPerLevel.push_back(22100);
  151. expPerLevel.push_back(26420);
  152. expPerLevel.push_back(31604);
  153. return;
  154. }
  155. void CHeroHandler::loadSpecialAbilities()
  156. {
  157. std::string buf = bitmaph->getTextFile("HEROSPEC.TXT");
  158. int it=0;
  159. std::string dump;
  160. for(int i=0; i<2; ++i)
  161. {
  162. loadToIt(dump,buf,it,3);
  163. }
  164. for (int i=0;i<heroes.size();i++)
  165. {
  166. loadToIt(heroes[i]->bonusName,buf,it,4);
  167. loadToIt(heroes[i]->shortBonus,buf,it,4);
  168. loadToIt(heroes[i]->longBonus,buf,it,3);
  169. }
  170. }
  171. void CHeroHandler::loadBiographies()
  172. {
  173. std::string buf = bitmaph->getTextFile("HEROBIOS.TXT");
  174. int it=0;
  175. for (int i=0;i<heroes.size();i++)
  176. {
  177. loadToIt(heroes[i]->biography,buf,it,3);
  178. }
  179. }
  180. void CHeroHandler::loadHeroClasses()
  181. {
  182. std::string buf = bitmaph->getTextFile("HCTRAITS.TXT");
  183. int andame = buf.size();
  184. for(int y=0; y<andame; ++y)
  185. if(buf[y]==',')
  186. buf[y]='.';
  187. int i = 0; //buf iterator
  188. int hmcr = 0;
  189. for(i; i<andame; ++i) //omitting rubbish
  190. {
  191. if(buf[i]=='\r')
  192. ++hmcr;
  193. if(hmcr==2)
  194. break;
  195. }
  196. i+=2;
  197. for(int ss=0; ss<18; ++ss) //18 classes of hero (including conflux)
  198. {
  199. CHeroClass * hc = new CHeroClass;
  200. int befi=i;
  201. for(i; i<andame; ++i)
  202. {
  203. if(buf[i]=='\t')
  204. break;
  205. }
  206. hc->name = buf.substr(befi, i-befi);
  207. ++i;
  208. befi=i;
  209. for(i; i<andame; ++i)
  210. {
  211. if(buf[i]=='\t')
  212. break;
  213. }
  214. hc->aggression = atof(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->initialAttack = 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->initialDefence = atoi(buf.substr(befi, i-befi).c_str());
  231. ++i;
  232. befi=i;
  233. for(i; i<andame; ++i)
  234. {
  235. if(buf[i]=='\t')
  236. break;
  237. }
  238. hc->initialPower = atoi(buf.substr(befi, i-befi).c_str());
  239. ++i;
  240. befi=i;
  241. for(i; i<andame; ++i)
  242. {
  243. if(buf[i]=='\t')
  244. break;
  245. }
  246. hc->initialKnowledge = atoi(buf.substr(befi, i-befi).c_str());
  247. ++i;
  248. hc->primChance.resize(PRIMARY_SKILLS);
  249. for(int x=0;x<PRIMARY_SKILLS;x++)
  250. {
  251. befi=i;
  252. for(i; i<andame; ++i)
  253. {
  254. if(buf[i]=='\t')
  255. break;
  256. }
  257. hc->primChance[x].first = atoi(buf.substr(befi, i-befi).c_str());
  258. ++i;
  259. }
  260. for(int x=0;x<PRIMARY_SKILLS;x++)
  261. {
  262. befi=i;
  263. for(i; i<andame; ++i)
  264. {
  265. if(buf[i]=='\t')
  266. break;
  267. }
  268. hc->primChance[x].second = atoi(buf.substr(befi, i-befi).c_str());
  269. ++i;
  270. }
  271. //CHero kkk = heroes[0];
  272. for(int dd=0; dd<SKILL_QUANTITY; ++dd)
  273. {
  274. befi=i;
  275. for(i; i<andame; ++i)
  276. {
  277. if(buf[i]=='\t')
  278. break;
  279. }
  280. int buff = atoi(buf.substr(befi, i-befi).c_str());
  281. ++i;
  282. hc->proSec.push_back(buff);
  283. }
  284. for(int dd=0; dd<9; ++dd)
  285. {
  286. befi=i;
  287. for(i; i<andame; ++i)
  288. {
  289. if(buf[i]=='\t' || buf[i]=='\r')
  290. break;
  291. }
  292. hc->selectionProbability[dd] = atoi(buf.substr(befi, i-befi).c_str());
  293. ++i;
  294. }
  295. ++i;
  296. heroClasses.push_back(hc);
  297. }
  298. }
  299. void CHeroHandler::initHeroClasses()
  300. {
  301. for(int gg=0; gg<heroes.size(); ++gg)
  302. {
  303. heroes[gg]->heroClass = heroClasses[heroes[gg]->heroType];
  304. }
  305. initTerrainCosts();
  306. }
  307. unsigned int CHeroHandler::level(unsigned int experience)
  308. {
  309. int add=0;
  310. while(experience>=expPerLevel[expPerLevel.size()-1])
  311. {
  312. experience/=1.2;
  313. add+=1;
  314. }
  315. for(int i=expPerLevel.size()-1; i>=0; --i)
  316. {
  317. if(experience>=expPerLevel[i])
  318. return i+add;
  319. }
  320. return -1;
  321. }
  322. unsigned int CHeroHandler::reqExp(unsigned int level)
  323. {
  324. level-=1;
  325. if(level<expPerLevel.size())
  326. return expPerLevel[level];
  327. else
  328. {
  329. unsigned int exp = expPerLevel[expPerLevel.size()-1];
  330. level-=(expPerLevel.size()-1);
  331. while(level>0)
  332. {
  333. --level;
  334. exp*=1.2;
  335. }
  336. return exp;
  337. }
  338. return -1;
  339. }
  340. void CHeroHandler::initTerrainCosts()
  341. {
  342. std::ifstream inp;
  343. inp.open("config" PATHSEPARATOR "TERCOSTS.TXT", std::ios_base::in|std::ios_base::binary);
  344. int tynum;
  345. inp>>tynum;
  346. for(int i=0; i<2*tynum; i+=2)
  347. {
  348. int catNum;
  349. inp>>catNum;
  350. for(int k=0; k<catNum; ++k)
  351. {
  352. int curCost;
  353. inp>>curCost;
  354. heroClasses[i]->terrCosts.push_back(curCost);
  355. heroClasses[i+1]->terrCosts.push_back(curCost);
  356. }
  357. }
  358. inp.close();
  359. }