CCreatureHandler.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CCreatureHandler.h"
  4. #include "CLodHandler.h"
  5. #include <sstream>
  6. #include <boost/assign/std/set.hpp>
  7. #include <boost/assign/std/vector.hpp>
  8. #include <boost/assign/std/list.hpp>
  9. #include <boost/assign/list_of.hpp>
  10. #include <boost/algorithm/string.hpp>
  11. #include <boost/algorithm/string/find.hpp>
  12. #include <boost/algorithm/string/replace.hpp>
  13. #include "../lib/VCMI_Lib.h"
  14. #include "../lib/CGameState.h"
  15. using namespace boost::assign;
  16. extern CLodHandler * bitmaph;
  17. /*
  18. * CCreatureHandler.cpp, part of VCMI engine
  19. *
  20. * Authors: listed in file AUTHORS in main folder
  21. *
  22. * License: GNU General Public License v2.0 or later
  23. * Full text of license available in license.txt file, in main folder
  24. *
  25. */
  26. static std::vector<int> getMindSpells()
  27. {
  28. std::vector<int> ret;
  29. ret.push_back(50); //sorrow
  30. ret.push_back(59); //berserk
  31. ret.push_back(60); //hypnotize
  32. ret.push_back(61); //forgetfulness
  33. ret.push_back(62); //blind
  34. return ret;
  35. }
  36. CCreatureHandler::CCreatureHandler()
  37. {
  38. VLC->creh = this;
  39. // Set the faction alignments to the defaults:
  40. // Good: Castle, Rampart, Tower // Evil: Inferno, Necropolis, Dungeon
  41. // Neutral: Stronghold, Fortess, Conflux
  42. factionAlignments += 1, 1, 1, -1, -1, -1, 0, 0, 0;
  43. doubledCreatures += 4, 14, 20, 28, 42, 44, 60, 70, 72, 85, 86, 100, 104; //according to Strategija
  44. }
  45. int CCreature::getQuantityID(const int & quantity)
  46. {
  47. if (quantity<5)
  48. return 0;
  49. if (quantity<10)
  50. return 1;
  51. if (quantity<20)
  52. return 2;
  53. if (quantity<50)
  54. return 3;
  55. if (quantity<100)
  56. return 4;
  57. if (quantity<250)
  58. return 5;
  59. if (quantity<500)
  60. return 5;
  61. if (quantity<1000)
  62. return 6;
  63. if (quantity<4000)
  64. return 7;
  65. return 8;
  66. }
  67. bool CCreature::isDoubleWide() const
  68. {
  69. return doubleWide;
  70. }
  71. bool CCreature::isFlying() const
  72. {
  73. return vstd::contains(bonuses, Bonus::FLYING);
  74. }
  75. bool CCreature::isShooting() const
  76. {
  77. return vstd::contains(bonuses, Bonus::SHOOTER);
  78. }
  79. bool CCreature::isUndead() const
  80. {
  81. return vstd::contains(bonuses, Bonus::UNDEAD);
  82. }
  83. /**
  84. * Determines if the creature is of a good alignment.
  85. * @return true if the creture is good, false otherwise.
  86. */
  87. bool CCreature::isGood () const
  88. {
  89. return VLC->creh->isGood(faction);
  90. }
  91. /**
  92. * Determines if the creature is of an evil alignment.
  93. * @return true if the creature is evil, false otherwise.
  94. */
  95. bool CCreature::isEvil () const
  96. {
  97. return VLC->creh->isEvil(faction);
  98. }
  99. si32 CCreature::maxAmount(const std::vector<si32> &res) const //how many creatures can be bought
  100. {
  101. int ret = 2147483645;
  102. int resAmnt = std::min(res.size(),cost.size());
  103. for(int i=0;i<resAmnt;i++)
  104. if(cost[i])
  105. ret = std::min(ret,(int)(res[i]/cost[i]));
  106. return ret;
  107. }
  108. CCreature::CCreature()
  109. {
  110. doubleWide = false;
  111. nodeType = CBonusSystemNode::CREATURE;
  112. }
  113. void CCreature::addBonus(int val, int type, int subtype /*= -1*/)
  114. {
  115. Bonus added(Bonus::PERMANENT, type, Bonus::CREATURE_ABILITY, val, idNumber, subtype, Bonus::BASE_NUMBER);
  116. bonuses.push_back(added);
  117. }
  118. void CCreature::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  119. {
  120. out.insert (VLC->creh->globalEffects);
  121. }
  122. bool CCreature::isMyUpgrade(const CCreature *anotherCre) const
  123. {
  124. //TODO upgrade of upgrade?
  125. return vstd::contains(upgrades, anotherCre->idNumber);
  126. }
  127. int readNumber(int & befi, int & i, int andame, std::string & buf) //helper function for void CCreatureHandler::loadCreatures() and loadUnitAnimInfo()
  128. {
  129. befi=i;
  130. for(; i<andame; ++i)
  131. {
  132. if(buf[i]=='\t')
  133. break;
  134. }
  135. std::string tmp = buf.substr(befi, i-befi);
  136. int ret = atoi(buf.substr(befi, i-befi).c_str());
  137. ++i;
  138. return ret;
  139. }
  140. float readFloat(int & befi, int & i, int andame, std::string & buf) //helper function for void CCreatureHandler::loadUnitAnimInfo()
  141. {
  142. befi=i;
  143. for(; i<andame; ++i)
  144. {
  145. if(buf[i]=='\t')
  146. break;
  147. }
  148. std::string tmp = buf.substr(befi, i-befi);
  149. float ret = atof(buf.substr(befi, i-befi).c_str());
  150. ++i;
  151. return ret;
  152. }
  153. /**
  154. * Determines if a faction is good.
  155. * @param ID of the faction.
  156. * @return true if the faction is good, false otherwise.
  157. */
  158. bool CCreatureHandler::isGood (si8 faction) const
  159. {
  160. return faction != -1 && factionAlignments[faction] == 1;
  161. }
  162. /**
  163. * Determines if a faction is evil.
  164. * @param ID of the faction.
  165. * @return true if the faction is evil, false otherwise.
  166. */
  167. bool CCreatureHandler::isEvil (si8 faction) const
  168. {
  169. return faction != -1 && factionAlignments[faction] == -1;
  170. }
  171. void CCreatureHandler::loadCreatures()
  172. {
  173. notUsedMonsters += 122,124,126,128,145,146,147,148,149,160,161,162,163,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191;
  174. tlog5 << "\t\tReading config/cr_abils.txt and ZCRTRAIT.TXT" << std::endl;
  175. bool useCreAbilsFromZCRTRAIT = true;
  176. ////////////reading cr_abils.txt ///////////////////
  177. std::ifstream abils(DATA_DIR "/config/cr_abils.txt", std::ios::in | std::ios::binary); //this file is not in lod
  178. const int MAX_LINE_SIZE = 1000;
  179. char abilLine[MAX_LINE_SIZE+1];
  180. for(int i=0; i<5; ++i) //removing 5 comment lines
  181. {
  182. abils.getline(abilLine, MAX_LINE_SIZE);
  183. }
  184. //reading first line (determining if we should use creature abilities from ZCRTRAIT.TXT)
  185. abils.getline(abilLine, MAX_LINE_SIZE);
  186. useCreAbilsFromZCRTRAIT = atoi(abilLine);
  187. ////////////reading ZCRTRAIT.TXT ///////////////////
  188. std::string buf = bitmaph->getTextFile("ZCRTRAIT.TXT");
  189. int andame = buf.size();
  190. int i=0; //buf iterator
  191. int hmcr=0;
  192. for(; i<andame; ++i)
  193. {
  194. if(buf[i]=='\r')
  195. ++hmcr;
  196. if(hmcr==2)
  197. break;
  198. }
  199. i+=2;
  200. while(i<buf.size())
  201. {
  202. CCreature &ncre = *new CCreature;
  203. ncre.idNumber = creatures.size();
  204. ncre.cost.resize(RESOURCE_QUANTITY);
  205. ncre.level=0;
  206. int befi=i;
  207. for(; i<andame; ++i)
  208. {
  209. if(buf[i]=='\t')
  210. break;
  211. }
  212. ncre.nameSing = buf.substr(befi, i-befi);
  213. ++i;
  214. befi=i;
  215. for(; i<andame; ++i)
  216. {
  217. if(buf[i]=='\t')
  218. break;
  219. }
  220. ncre.namePl = buf.substr(befi, i-befi);
  221. ++i;
  222. for(int v=0; v<7; ++v)
  223. {
  224. ncre.cost[v] = readNumber(befi, i, andame, buf);
  225. }
  226. ncre.fightValue = readNumber(befi, i, andame, buf);
  227. ncre.AIValue = readNumber(befi, i, andame, buf);
  228. ncre.growth = readNumber(befi, i, andame, buf);
  229. ncre.hordeGrowth = readNumber(befi, i, andame, buf);
  230. ncre.hitPoints = readNumber(befi, i, andame, buf);
  231. ncre.addBonus(ncre.hitPoints, Bonus::STACK_HEALTH);
  232. ncre.speed = readNumber(befi, i, andame, buf);
  233. ncre.addBonus(ncre.speed, Bonus::STACKS_SPEED);
  234. ncre.attack = readNumber(befi, i, andame, buf);
  235. ncre.addBonus(ncre.attack, Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  236. ncre.defence = readNumber(befi, i, andame, buf);
  237. ncre.addBonus(ncre.defence, Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  238. ncre.damageMin = readNumber(befi, i, andame, buf); //not used anymore?
  239. ncre.addBonus(ncre.damageMin, Bonus::CREATURE_DAMAGE, 1);
  240. ncre.damageMax = readNumber(befi, i, andame, buf);
  241. ncre.addBonus(ncre.damageMax, Bonus::CREATURE_DAMAGE, 2);
  242. ncre.shots = readNumber(befi, i, andame, buf);
  243. ncre.spells = readNumber(befi, i, andame, buf);
  244. ncre.ammMin = readNumber(befi, i, andame, buf);
  245. ncre.ammMax = readNumber(befi, i, andame, buf);
  246. befi=i;
  247. for(; i<andame; ++i)
  248. {
  249. if(buf[i]=='\t')
  250. break;
  251. }
  252. ncre.abilityText = buf.substr(befi, i-befi);
  253. ++i;
  254. befi=i;
  255. for(; i<andame; ++i)
  256. {
  257. if(buf[i]=='\r')
  258. break;
  259. }
  260. ncre.abilityRefs = buf.substr(befi, i-befi);
  261. i+=2;
  262. if(useCreAbilsFromZCRTRAIT)
  263. { //adding abilities from ZCRTRAIT.TXT
  264. if(boost::algorithm::find_first(ncre.abilityRefs, "DOUBLE_WIDE"))
  265. ncre.doubleWide = true;
  266. if(boost::algorithm::find_first(ncre.abilityRefs, "FLYING_ARMY"))
  267. ncre.addBonus(0, Bonus::FLYING);
  268. if(boost::algorithm::find_first(ncre.abilityRefs, "SHOOTING_ARMY"))
  269. ncre.addBonus(0, Bonus::SHOOTER);
  270. if(boost::algorithm::find_first(ncre.abilityRefs, "SIEGE_WEAPON"))
  271. ncre.addBonus(0, Bonus::SIEGE_WEAPON);
  272. if(boost::algorithm::find_first(ncre.abilityRefs, "const_two_attacks"))
  273. ncre.addBonus(1, Bonus::ADDITIONAL_ATTACK);
  274. if(boost::algorithm::find_first(ncre.abilityRefs, "const_free_attack"))
  275. ncre.addBonus(0, Bonus::BLOCKS_RETALIATION);
  276. if(boost::algorithm::find_first(ncre.abilityRefs, "IS_UNDEAD"))
  277. ncre.addBonus(0, Bonus::UNDEAD);
  278. if(boost::algorithm::find_first(ncre.abilityRefs, "const_no_melee_penalty"))
  279. ncre.addBonus(0, Bonus::NO_MELEE_PENALTY);
  280. if(boost::algorithm::find_first(ncre.abilityRefs, "const_jousting"))
  281. ncre.addBonus(0, Bonus::JOUSTING);
  282. if(boost::algorithm::find_first(ncre.abilityRefs, "const_raises_morale"))
  283. {
  284. ncre.addBonus(+1, Bonus::MORALE);;
  285. ncre.bonuses.back().effectRange = Bonus::ONLY_ALLIED_ARMY;
  286. }
  287. if(boost::algorithm::find_first(ncre.abilityRefs, "const_lowers_morale"))
  288. {
  289. ncre.addBonus(-1, Bonus::MORALE);;
  290. ncre.bonuses.back().effectRange = Bonus::ONLY_ENEMY_ARMY;
  291. }
  292. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_1"))
  293. ncre.addBonus(0, Bonus::KING1);
  294. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_2"))
  295. ncre.addBonus(0, Bonus::KING2);
  296. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_3"))
  297. ncre.addBonus(0, Bonus::KING3);
  298. if(boost::algorithm::find_first(ncre.abilityRefs, "const_no_wall_penalty"))
  299. ncre.addBonus(0, Bonus::NO_WALL_PENALTY);
  300. if(boost::algorithm::find_first(ncre.abilityRefs, "CATAPULT"))
  301. ncre.addBonus(0, Bonus::CATAPULT);
  302. if(boost::algorithm::find_first(ncre.abilityRefs, "MULTI_HEADED"))
  303. ncre.addBonus(0, Bonus::ATTACKS_ALL_ADJACENT);
  304. if(boost::algorithm::find_first(ncre.abilityRefs, "IMMUNE_TO_MIND_SPELLS"))
  305. {
  306. std::vector<int> mindSpells = getMindSpells();
  307. for(int g=0; g<mindSpells.size(); ++g)
  308. ncre.addBonus(0, Bonus::SPELL_IMMUNITY, mindSpells[g]); //giants are immune to mind spells
  309. }
  310. if(boost::algorithm::find_first(ncre.abilityRefs, "IMMUNE_TO_FIRE_SPELLS"))
  311. ncre.addBonus(0, Bonus::FIRE_IMMUNITY);
  312. if(boost::algorithm::find_first(ncre.abilityRefs, "HAS_EXTENDED_ATTACK"))
  313. ncre.addBonus(0, Bonus::TWO_HEX_ATTACK_BREATH);;
  314. }
  315. if(ncre.nameSing!=std::string("") && ncre.namePl!=std::string(""))
  316. {
  317. ncre.idNumber = creatures.size();
  318. creatures.push_back(&ncre);
  319. }
  320. }
  321. ////second part of reading cr_abils.txt////
  322. bool contReading = true;
  323. while(contReading) //main reading loop
  324. {
  325. abils.getline(abilLine, MAX_LINE_SIZE);
  326. std::istringstream reader(abilLine);
  327. char command;
  328. reader >> command;
  329. switch(command)
  330. {
  331. case '+': //add new ability
  332. {
  333. int creatureID;
  334. Bonus nsf;
  335. si32 buf;
  336. std::string type;
  337. reader >> creatureID;
  338. reader >> type;
  339. std::map<std::string, int>::const_iterator it = bonusNameMap.find(type);
  340. CCreature *cre = creatures[creatureID];
  341. if (it == bonusNameMap.end())
  342. {
  343. if(type == "DOUBLE_WIDE")
  344. cre->doubleWide = true;
  345. else if(type == "ENEMY_MORALE_DECREASING")
  346. {
  347. cre->addBonus(-1, Bonus::MORALE);;
  348. cre->bonuses.back().effectRange = Bonus::ONLY_ENEMY_ARMY;
  349. }
  350. else if(type == "ENEMY_LUCK_DECREASING")
  351. {
  352. cre->addBonus(-1, Bonus::LUCK);;
  353. cre->bonuses.back().effectRange = Bonus::ONLY_ENEMY_ARMY;
  354. }
  355. else
  356. tlog1 << "Error: invalid type " << type << " in cr_abils.txt" << std::endl;
  357. break;
  358. }
  359. nsf.type = it->second;
  360. reader >> buf; nsf.val = buf;
  361. reader >> buf; nsf.subtype = buf;
  362. reader >> buf; nsf.additionalInfo = buf;
  363. nsf.source = Bonus::CREATURE_ABILITY;
  364. nsf.id = cre->idNumber;
  365. nsf.duration = Bonus::ONE_BATTLE;
  366. nsf.turnsRemain = 0;
  367. cre->bonuses += nsf;
  368. break;
  369. }
  370. case '-': //remove ability
  371. {
  372. int creatureID;
  373. std::string type;
  374. reader >> creatureID;
  375. reader >> type;
  376. std::map<std::string, int>::const_iterator it = bonusNameMap.find(type);
  377. if (it == bonusNameMap.end())
  378. {
  379. if(type == "DOUBLE_WIDE")
  380. creatures[creatureID]->doubleWide = false;
  381. else
  382. tlog1 << "Error: invalid type " << type << " in cr_abils.txt" << std::endl;
  383. break;
  384. }
  385. int typeNo = it->second;
  386. Bonus::BonusType ecf = static_cast<Bonus::BonusType>(typeNo);
  387. creatures[creatureID]->bonuses -= ecf;
  388. break;
  389. }
  390. case '0': //end reading
  391. {
  392. contReading = false;
  393. break;
  394. }
  395. default: //invalid command
  396. {
  397. tlog1 << "Parse error in file config/cr_abils.txt" << std::endl;
  398. break;
  399. }
  400. }
  401. }
  402. abils.close();
  403. tlog5 << "\t\tReading config/crerefnam.txt" << std::endl;
  404. //loading reference names
  405. std::ifstream ifs(DATA_DIR "/config/crerefnam.txt");
  406. int tempi;
  407. std::string temps;
  408. for (;;)
  409. {
  410. ifs >> tempi >> temps;
  411. if (tempi>=creatures.size())
  412. break;
  413. boost::assign::insert(nameToID)(temps,tempi);
  414. creatures[tempi]->nameRef=temps;
  415. }
  416. ifs.close();
  417. ifs.clear();
  418. for(i=1;i<=10;i++)
  419. levelCreatures.insert(std::pair<int,std::vector<CCreature*> >(i,std::vector<CCreature*>()));
  420. tlog5 << "\t\tReading config/monsters.txt" << std::endl;
  421. ifs.open(DATA_DIR "/config/monsters.txt");
  422. {
  423. while(!ifs.eof())
  424. {
  425. int id, lvl;
  426. ifs >> id >> lvl;
  427. if(lvl>0)
  428. {
  429. creatures[id]->level = lvl;
  430. levelCreatures[lvl].push_back(creatures[id]);
  431. }
  432. }
  433. }
  434. ifs.close();
  435. ifs.clear();
  436. tlog5 << "\t\tReading config/cr_factions.txt" << std::endl;
  437. ifs.open(DATA_DIR "/config/cr_factions.txt");
  438. while(!ifs.eof())
  439. {
  440. int id, fact;
  441. ifs >> id >> fact;
  442. creatures[id]->faction = fact;
  443. }
  444. ifs.close();
  445. ifs.clear();
  446. tlog5 << "\t\tReading config/cr_upgrade_list.txt" << std::endl;
  447. ifs.open(DATA_DIR "/config/cr_upgrade_list.txt");
  448. while(!ifs.eof())
  449. {
  450. int id, up;
  451. ifs >> id >> up;
  452. creatures[id]->upgrades.insert(up);
  453. }
  454. ifs.close();
  455. ifs.clear();
  456. //loading unit animation def names
  457. tlog5 << "\t\tReading config/CREDEFS.TXT" << std::endl;
  458. std::ifstream inp(DATA_DIR "/config/CREDEFS.TXT", std::ios::in | std::ios::binary); //this file is not in lod
  459. inp.seekg(0,std::ios::end); // na koniec
  460. int andame2 = inp.tellg(); // read length
  461. inp.seekg(0,std::ios::beg); // wracamy na poczatek
  462. char * bufor = new char[andame2+1]; // allocate memory
  463. inp.read((char*)bufor, andame2); // read map file to buffer
  464. inp.close();
  465. bufor[andame2] = 0;
  466. buf = std::string(bufor);
  467. delete [] bufor;
  468. i = 0; //buf iterator
  469. hmcr = 0;
  470. for(; i<andame2; ++i) //omitting rubbish
  471. {
  472. if(buf[i]=='\r')
  473. break;
  474. }
  475. i+=2;
  476. tlog5 << "We have "<<creatures.size() << " creatures\n";
  477. for(int s=0; s<creatures.size(); ++s)
  478. {
  479. //tlog5 <<"\t\t\t" << s <<". Reading defname. \n";
  480. int befi=i;
  481. std::string rub;
  482. for(; i<andame2; ++i)
  483. {
  484. if(buf[i]==' ')
  485. break;
  486. }
  487. rub = buf.substr(befi, i-befi);
  488. ++i;
  489. befi=i;
  490. for(; i<andame2; ++i)
  491. {
  492. if(buf[i]=='\r')
  493. break;
  494. }
  495. std::string defName = buf.substr(befi, i-befi);
  496. creatures[s]->animDefName = defName;
  497. }
  498. tlog5 << "\t\tReading CRANIM.TXT.txt" << std::endl;
  499. loadAnimationInfo();
  500. //loading id to projectile mapping
  501. tlog5 << "\t\tReading config/cr_shots.txt" << std::endl;
  502. std::ifstream inp2(DATA_DIR "/config/cr_shots.txt", std::ios::in | std::ios::binary); //this file is not in lod
  503. char dump [200];
  504. inp2.getline(dump, 200);
  505. while(true)
  506. {
  507. int id;
  508. std::string name;
  509. bool spin;
  510. inp2>>id;
  511. if(id == -1)
  512. break;
  513. inp2>>name;
  514. idToProjectile[id] = name;
  515. inp2>>spin;
  516. idToProjectileSpin[id] = spin;
  517. }
  518. inp2.close();
  519. //reading factionToTurretCreature
  520. tlog5 << "\t\tReading config/cr_to_turret.txt" << std::endl;
  521. std::ifstream inp3(DATA_DIR "/config/cr_to_turret.txt", std::ios::in | std::ios::binary); //this file is not in lod
  522. std::string dump2;
  523. inp3 >> dump2 >> dump2;
  524. for(int g=0; g<F_NUMBER; ++g)
  525. {
  526. inp3 >> factionToTurretCreature[g];
  527. }
  528. inp3.close();
  529. //reading creature ability names
  530. ifs.open(DATA_DIR "/config/bonusnames.txt");
  531. {
  532. std::string buf2, buf3, line;
  533. int i;
  534. std::map<std::string,int>::const_iterator it;
  535. getline(ifs, line); //skip 1st line
  536. while(!ifs.eof())
  537. {
  538. getline(ifs, buf, '\t');
  539. getline(ifs, buf2, '\t');
  540. getline(ifs, buf3);
  541. it = bonusNameMap.find(buf);
  542. if (it != bonusNameMap.end())
  543. stackBonuses[it->second] = std::pair<std::string, std::string>(buf2,buf3);
  544. else
  545. tlog2 << "Bonus " << buf << " not recognized, ingoring\n";
  546. }
  547. }
  548. ifs.close();
  549. if (STACK_EXP) //reading default stack experience bonuses
  550. {
  551. buf = bitmaph->getTextFile("CREXPBON.TXT");
  552. int it = 0, creid;
  553. commonBonuses.resize(8); //8 tiers
  554. stackExperience b;
  555. b.expBonuses.resize(10);
  556. b.source = Bonus::STACK_EXPERIENCE;
  557. loadToIt (dump2, buf, it, 3); //ignore first line
  558. loadToIt (dump2, buf, it, 4); //ignore index
  559. loadStackExp(b, buf, it);
  560. loadToIt (dump2, buf, it, 4); //crop comment
  561. for (i = 0; i < 8; ++i)
  562. {
  563. commonBonuses[i].push_back(b);//health bonus common for all
  564. for (int j = 0; j < 4; ++j) //four modifiers common for tiers
  565. {
  566. loadToIt (dump2, buf, it, 4); //ignore index
  567. loadStackExp(b, buf, it);
  568. commonBonuses[i].push_back(b);
  569. loadToIt (dump2, buf, it, 3); //crop comment
  570. }
  571. }
  572. do //parse everything that's left
  573. {
  574. loadToIt(creid, buf, it, 4); //get index
  575. loadStackExp(b, buf, it);
  576. creatures[creid]->bonuses.push_back(b); //experience list is common for creatures of that type
  577. loadToIt (dump2, buf, it, 3); //crop comment
  578. }
  579. while (it < buf.size());
  580. for (std::vector<CCreature*>::iterator i = creatures.begin(); i != creatures.end(); i++)
  581. {
  582. if (it = (*i)->level < 7)
  583. std::copy(commonBonuses[it-1].begin(), commonBonuses[it-1].end(), (*i)->bonuses.begin());
  584. else
  585. std::copy(commonBonuses[7].begin(), commonBonuses[7].end(), (*i)->bonuses.begin()); //common for tiers 8+
  586. }
  587. } //end of stack experience
  588. }
  589. void CCreatureHandler::loadAnimationInfo()
  590. {
  591. std::string buf = bitmaph->getTextFile("CRANIM.TXT");
  592. int andame = buf.size();
  593. int i=0; //buf iterator
  594. int hmcr=0;
  595. for(; i<andame; ++i)
  596. {
  597. if(buf[i]=='\r')
  598. ++hmcr;
  599. if(hmcr==2)
  600. break;
  601. }
  602. i+=2;
  603. for(int dd=0; dd<creatures.size(); ++dd)
  604. {
  605. //tlog5 << "\t\t\tReading animation info for creature " << dd << std::endl;
  606. loadUnitAnimInfo(*creatures[dd], buf, i);
  607. }
  608. return;
  609. }
  610. void CCreatureHandler::loadUnitAnimInfo(CCreature & unit, std::string & src, int & i)
  611. {
  612. int befi=i;
  613. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  614. while(unit.timeBetweenFidgets == 0.0)
  615. {
  616. for(; i<src.size(); ++i)
  617. {
  618. if(src[i]=='\r')
  619. break;
  620. }
  621. i+=2;
  622. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  623. }
  624. unit.walkAnimationTime = readFloat(befi, i, src.size(), src);
  625. unit.attackAnimationTime = readFloat(befi, i, src.size(), src);
  626. unit.flightAnimationDistance = readFloat(befi, i, src.size(), src);
  627. ///////////////////////
  628. unit.upperRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  629. unit.upperRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  630. unit.rightMissleOffsetX = readNumber(befi, i, src.size(), src);
  631. unit.rightMissleOffsetY = readNumber(befi, i, src.size(), src);
  632. unit.lowerRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  633. unit.lowerRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  634. ///////////////////////
  635. for(int jjj=0; jjj<12; ++jjj)
  636. {
  637. unit.missleFrameAngles[jjj] = readFloat(befi, i, src.size(), src);
  638. }
  639. unit.troopCountLocationOffset= readNumber(befi, i, src.size(), src);
  640. unit.attackClimaxFrame = readNumber(befi, i, src.size(), src);
  641. for(; i<src.size(); ++i)
  642. {
  643. if(src[i]=='\r')
  644. break;
  645. }
  646. i+=2;
  647. }
  648. void CCreatureHandler::loadStackExp(stackExperience & b, std::string & src, int & it) //help function for parsing CREXPBON.txt, assuming all its details are already defined
  649. {
  650. std::string buf, mod;
  651. loadToIt(buf, src, it, 4);
  652. loadToIt(mod, src, it, 4);
  653. switch (buf[0])
  654. {
  655. case 'H':
  656. b.type = Bonus::STACK_HEALTH;
  657. break;
  658. case 'A':
  659. b.type = Bonus::PRIMARY_SKILL;
  660. b.subtype = PrimarySkill::ATTACK;
  661. break;
  662. case 'D':
  663. b.type = Bonus::PRIMARY_SKILL;
  664. b.subtype = PrimarySkill::DEFENSE;
  665. break;
  666. case 'M': //Max damage
  667. b.type = Bonus::CREATURE_DAMAGE;
  668. b.subtype = 2;
  669. break;
  670. case 'm': //Min damage
  671. b.type = Bonus::CREATURE_DAMAGE;
  672. b.subtype = 1;
  673. break;
  674. }
  675. loadToIt (b.val, src, it, 4); //basic value, not particularly useful but existent
  676. for (int i = 0; i < 10; ++i)
  677. {
  678. loadToIt (b.expBonuses[i], src, it, 4); //vector must have length 10
  679. }
  680. }
  681. CCreatureHandler::~CCreatureHandler()
  682. {
  683. }
  684. int CCreatureHandler::pickRandomMonster(const boost::function<int()> &randGen) const
  685. {
  686. int r = 0;
  687. do
  688. {
  689. if(randGen)
  690. r = randGen();
  691. else
  692. r = rand();
  693. r %= 197;
  694. } while (vstd::contains(VLC->creh->notUsedMonsters,r));
  695. return r;
  696. }