CHeroHandler.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. throw std::string("Cannot pick secondary skill!");
  33. }
  34. CHeroHandler::~CHeroHandler()
  35. {
  36. for (int i = 0; i < heroes.size(); i++)
  37. delete heroes[i];
  38. for (int i = 0; i < heroClasses.size(); i++)
  39. delete heroClasses[i];
  40. }
  41. CHeroHandler::CHeroHandler()
  42. {}
  43. void CHeroHandler::loadHeroes()
  44. {
  45. VLC->heroh = this;
  46. int ID=0;
  47. std::string buf = bitmaph->getTextFile("HOTRAITS.TXT");
  48. int it=0;
  49. std::string dump;
  50. for(int i=0; i<2; ++i)
  51. {
  52. loadToIt(dump,buf,it,3);
  53. }
  54. int numberOfCurrentClassHeroes = 0;
  55. int currentClass = 0;
  56. int additHero = 0;
  57. EHeroClasses addTab[12];
  58. addTab[0] = HERO_KNIGHT;
  59. addTab[1] = HERO_WITCH;
  60. addTab[2] = HERO_KNIGHT;
  61. addTab[3] = HERO_WIZARD;
  62. addTab[4] = HERO_RANGER;
  63. addTab[5] = HERO_BARBARIAN;
  64. addTab[6] = HERO_DEATHKNIGHT;
  65. addTab[7] = HERO_WARLOCK;
  66. addTab[8] = HERO_KNIGHT;
  67. addTab[9] = HERO_WARLOCK;
  68. addTab[10] = HERO_BARBARIAN;
  69. addTab[11] = HERO_DEMONIAC;
  70. for (int i=0; i<HEROES_QUANTITY; i++)
  71. {
  72. CHero * nher = new CHero;
  73. if(currentClass<18)
  74. {
  75. nher->heroType = (EHeroClasses)currentClass;
  76. ++numberOfCurrentClassHeroes;
  77. if(numberOfCurrentClassHeroes==8)
  78. {
  79. numberOfCurrentClassHeroes = 0;
  80. ++currentClass;
  81. }
  82. }
  83. else
  84. {
  85. nher->heroType = addTab[additHero++];
  86. }
  87. std::string pom ;
  88. loadToIt(nher->name,buf,it,4);
  89. for(int x=0;x<3;x++)
  90. {
  91. loadToIt(pom,buf,it,4);
  92. nher->lowStack[x] = atoi(pom.c_str());
  93. loadToIt(pom,buf,it,4);
  94. nher->highStack[x] = atoi(pom.c_str());
  95. loadToIt(nher->refTypeStack[x],buf,it,(x==2) ? (3) : (4));
  96. int hlp = nher->refTypeStack[x].find_first_of(' ',0);
  97. if(hlp>=0)
  98. nher->refTypeStack[x].replace(hlp,1,"");
  99. }
  100. nher->ID = heroes.size();
  101. heroes.push_back(nher);
  102. }
  103. //loading initial secondary skills
  104. std::ifstream inp;
  105. inp.open("config" PATHSEPARATOR "heroes_sec_skills.txt", std::ios_base::in|std::ios_base::binary);
  106. if(!inp.is_open())
  107. {
  108. tlog1<<"missing file: config/heroes_sec_skills.txt"<<std::endl;
  109. }
  110. else
  111. {
  112. inp>>dump;
  113. int hid; //ID of currently read hero
  114. int secQ; //number of secondary abilities
  115. while(true)
  116. {
  117. inp>>hid;
  118. if(hid == -1)
  119. break;
  120. inp>>secQ;
  121. for(int g=0; g<secQ; ++g)
  122. {
  123. int a, b;
  124. inp>>a; inp>>b;
  125. heroes[hid]->secSkillsInit.push_back(std::make_pair(a, b));
  126. }
  127. }
  128. inp.close();
  129. }
  130. //initial skills loaded
  131. loadHeroClasses();
  132. initHeroClasses();
  133. expPerLevel.push_back(0);
  134. expPerLevel.push_back(1000);
  135. expPerLevel.push_back(2000);
  136. expPerLevel.push_back(3200);
  137. expPerLevel.push_back(4500);
  138. expPerLevel.push_back(6000);
  139. expPerLevel.push_back(7700);
  140. expPerLevel.push_back(9000);
  141. expPerLevel.push_back(11000);
  142. expPerLevel.push_back(13200);
  143. expPerLevel.push_back(15500);
  144. expPerLevel.push_back(18500);
  145. expPerLevel.push_back(22100);
  146. expPerLevel.push_back(26420);
  147. expPerLevel.push_back(31604);
  148. //ballistics info
  149. buf = bitmaph->getTextFile("BALLIST.TXT");
  150. it = 0;
  151. for(int i=0; i<22; ++i)
  152. {
  153. loadToIt(dump,buf,it,4);
  154. }
  155. for(int lvl=0; lvl<4; ++lvl)
  156. {
  157. CHeroHandler::SBallisticsLevelInfo bli;
  158. si32 tempNum;
  159. loadToIt(tempNum,buf,it,4);
  160. bli.keep = tempNum;
  161. loadToIt(tempNum,buf,it,4);
  162. bli.tower = tempNum;
  163. loadToIt(tempNum,buf,it,4);
  164. bli.gate = tempNum;
  165. loadToIt(tempNum,buf,it,4);
  166. bli.wall = tempNum;
  167. loadToIt(tempNum,buf,it,4);
  168. bli.shots = tempNum;
  169. loadToIt(tempNum,buf,it,4);
  170. bli.noDmg = tempNum;
  171. loadToIt(tempNum,buf,it,4);
  172. bli.oneDmg = tempNum;
  173. loadToIt(tempNum,buf,it,4);
  174. bli.twoDmg = tempNum;
  175. loadToIt(tempNum,buf,it,4);
  176. bli.sum = tempNum;
  177. if(lvl!=3)
  178. {
  179. loadToIt(dump,buf,it,4);
  180. }
  181. ballistics.push_back(bli);
  182. }
  183. }
  184. void CHeroHandler::loadHeroClasses()
  185. {
  186. std::string buf = bitmaph->getTextFile("HCTRAITS.TXT");
  187. int andame = buf.size();
  188. for(int y=0; y<andame; ++y)
  189. if(buf[y]==',')
  190. buf[y]='.';
  191. int i = 0; //buf iterator
  192. int hmcr = 0;
  193. for(i; i<andame; ++i) //omitting rubbish
  194. {
  195. if(buf[i]=='\r')
  196. ++hmcr;
  197. if(hmcr==2)
  198. break;
  199. }
  200. i+=2;
  201. for(int ss=0; ss<18; ++ss) //18 classes of hero (including conflux)
  202. {
  203. CHeroClass * hc = new CHeroClass;
  204. int befi=i;
  205. for(i; i<andame; ++i)
  206. {
  207. if(buf[i]=='\t')
  208. break;
  209. }
  210. hc->name = buf.substr(befi, i-befi);
  211. ++i;
  212. befi=i;
  213. for(i; i<andame; ++i)
  214. {
  215. if(buf[i]=='\t')
  216. break;
  217. }
  218. hc->aggression = atof(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->initialAttack = 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->initialDefence = 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->initialPower = 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->initialKnowledge = atoi(buf.substr(befi, i-befi).c_str());
  251. ++i;
  252. hc->primChance.resize(PRIMARY_SKILLS);
  253. for(int x=0;x<PRIMARY_SKILLS;x++)
  254. {
  255. befi=i;
  256. for(i; i<andame; ++i)
  257. {
  258. if(buf[i]=='\t')
  259. break;
  260. }
  261. hc->primChance[x].first = atoi(buf.substr(befi, i-befi).c_str());
  262. ++i;
  263. }
  264. for(int x=0;x<PRIMARY_SKILLS;x++)
  265. {
  266. befi=i;
  267. for(i; i<andame; ++i)
  268. {
  269. if(buf[i]=='\t')
  270. break;
  271. }
  272. hc->primChance[x].second = atoi(buf.substr(befi, i-befi).c_str());
  273. ++i;
  274. }
  275. //CHero kkk = heroes[0];
  276. for(int dd=0; dd<SKILL_QUANTITY; ++dd)
  277. {
  278. befi=i;
  279. for(i; i<andame; ++i)
  280. {
  281. if(buf[i]=='\t')
  282. break;
  283. }
  284. int buff = atoi(buf.substr(befi, i-befi).c_str());
  285. ++i;
  286. hc->proSec.push_back(buff);
  287. }
  288. for(int dd=0; dd<9; ++dd)
  289. {
  290. befi=i;
  291. for(i; i<andame; ++i)
  292. {
  293. if(buf[i]=='\t' || buf[i]=='\r')
  294. break;
  295. }
  296. hc->selectionProbability[dd] = atoi(buf.substr(befi, i-befi).c_str());
  297. ++i;
  298. }
  299. ++i;
  300. heroClasses.push_back(hc);
  301. }
  302. }
  303. void CHeroHandler::initHeroClasses()
  304. {
  305. for(int gg=0; gg<heroes.size(); ++gg)
  306. {
  307. heroes[gg]->heroClass = heroClasses[heroes[gg]->heroType];
  308. }
  309. initTerrainCosts();
  310. }
  311. unsigned int CHeroHandler::level(unsigned int experience)
  312. {
  313. int add=0;
  314. while(experience>=expPerLevel[expPerLevel.size()-1])
  315. {
  316. experience/=1.2;
  317. add+=1;
  318. }
  319. for(int i=expPerLevel.size()-1; i>=0; --i)
  320. {
  321. if(experience>=expPerLevel[i])
  322. return 1+i+add;
  323. }
  324. return -1;
  325. }
  326. unsigned int CHeroHandler::reqExp(unsigned int level)
  327. {
  328. level-=1;
  329. if(level<expPerLevel.size())
  330. return expPerLevel[level];
  331. else
  332. {
  333. unsigned int exp = expPerLevel[expPerLevel.size()-1];
  334. level-=(expPerLevel.size()-1);
  335. while(level>0)
  336. {
  337. --level;
  338. exp*=1.2;
  339. }
  340. return exp;
  341. }
  342. return -1;
  343. }
  344. void CHeroHandler::initTerrainCosts()
  345. {
  346. std::ifstream inp;
  347. inp.open("config" PATHSEPARATOR "TERCOSTS.TXT", std::ios_base::in|std::ios_base::binary);
  348. int tynum;
  349. inp>>tynum;
  350. for(int i=0; i<2*tynum; i+=2)
  351. {
  352. int catNum;
  353. inp>>catNum;
  354. for(int k=0; k<catNum; ++k)
  355. {
  356. int curCost;
  357. inp>>curCost;
  358. heroClasses[i]->terrCosts.push_back(curCost);
  359. heroClasses[i+1]->terrCosts.push_back(curCost);
  360. }
  361. }
  362. inp.close();
  363. }