CCreatureHandler.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. #include "StdInc.h"
  2. #include "CCreatureHandler.h"
  3. #include "CLodHandler.h"
  4. #include "../lib/VCMI_Lib.h"
  5. #include "../lib/CGameState.h"
  6. #include "../lib/JsonNode.h"
  7. using namespace boost::assign;
  8. extern CLodHandler * bitmaph;
  9. /*
  10. * CCreatureHandler.cpp, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. CCreatureHandler::CCreatureHandler()
  19. {
  20. VLC->creh = this;
  21. // Set the faction alignments to the defaults:
  22. // Good: Castle, Rampart, Tower // Evil: Inferno, Necropolis, Dungeon
  23. // Neutral: Stronghold, Fortess, Conflux
  24. factionAlignments += 1, 1, 1, -1, -1, -1, 0, 0, 0;
  25. doubledCreatures += 4, 14, 20, 28, 44, 60, 70, 72, 85, 86, 100, 104; //according to Strategija
  26. allCreatures.setDescription("All creatures");
  27. creaturesOfLevel[0].setDescription("Creatures of unnormalized tier");
  28. for(int i = 1; i < ARRAY_COUNT(creaturesOfLevel); i++)
  29. creaturesOfLevel[i].setDescription("Creatures of tier " + boost::lexical_cast<std::string>(i));
  30. }
  31. int CCreature::getQuantityID(const int & quantity)
  32. {
  33. if (quantity<5)
  34. return 0;
  35. if (quantity<10)
  36. return 1;
  37. if (quantity<20)
  38. return 2;
  39. if (quantity<50)
  40. return 3;
  41. if (quantity<100)
  42. return 4;
  43. if (quantity<250)
  44. return 5;
  45. if (quantity<500)
  46. return 6;
  47. if (quantity<1000)
  48. return 7;
  49. return 8;
  50. }
  51. int CCreature::estimateCreatureCount(ui32 countID)
  52. {
  53. static const int creature_count[] = { 3, 8, 15, 35, 75, 175, 375, 750, 2500 };
  54. if (countID > 8)
  55. assert("Wrong countID!");
  56. return creature_count[countID];
  57. }
  58. bool CCreature::isDoubleWide() const
  59. {
  60. return doubleWide;
  61. }
  62. bool CCreature::isFlying() const
  63. {
  64. return hasBonusOfType(Bonus::FLYING);
  65. }
  66. bool CCreature::isShooting() const
  67. {
  68. return hasBonusOfType(Bonus::SHOOTER);
  69. }
  70. bool CCreature::isUndead() const
  71. {
  72. return hasBonusOfType(Bonus::UNDEAD);
  73. }
  74. /**
  75. * Determines if the creature is of a good alignment.
  76. * @return true if the creture is good, false otherwise.
  77. */
  78. bool CCreature::isGood () const
  79. {
  80. return VLC->creh->isGood(faction);
  81. }
  82. /**
  83. * Determines if the creature is of an evil alignment.
  84. * @return true if the creature is evil, false otherwise.
  85. */
  86. bool CCreature::isEvil () const
  87. {
  88. return VLC->creh->isEvil(faction);
  89. }
  90. si32 CCreature::maxAmount(const std::vector<si32> &res) const //how many creatures can be bought
  91. {
  92. int ret = 2147483645;
  93. int resAmnt = std::min(res.size(),cost.size());
  94. for(int i=0;i<resAmnt;i++)
  95. if(cost[i])
  96. ret = std::min(ret,(int)(res[i]/cost[i]));
  97. return ret;
  98. }
  99. CCreature::CCreature()
  100. {
  101. doubleWide = false;
  102. setNodeType(CBonusSystemNode::CREATURE);
  103. }
  104. void CCreature::addBonus(int val, int type, int subtype /*= -1*/)
  105. {
  106. Bonus *added = new Bonus(Bonus::PERMANENT, type, Bonus::CREATURE_ABILITY, val, idNumber, subtype, Bonus::BASE_NUMBER);
  107. addNewBonus(added);
  108. }
  109. // void CCreature::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  110. // {
  111. // out.insert (VLC->creh->globalEffects);
  112. // }
  113. bool CCreature::isMyUpgrade(const CCreature *anotherCre) const
  114. {
  115. //TODO upgrade of upgrade?
  116. return vstd::contains(upgrades, anotherCre->idNumber);
  117. }
  118. bool CCreature::valid() const
  119. {
  120. return this == VLC->creh->creatures[idNumber];
  121. }
  122. std::string CCreature::nodeName() const
  123. {
  124. return "\"" + namePl + "\"";
  125. }
  126. int readNumber(int & befi, int & i, int andame, std::string & buf) //helper function for void CCreatureHandler::loadCreatures() and loadUnitAnimInfo()
  127. {
  128. befi=i;
  129. for(; i<andame; ++i)
  130. {
  131. if(buf[i]=='\t')
  132. break;
  133. }
  134. std::string tmp = buf.substr(befi, i-befi);
  135. int ret = atoi(buf.substr(befi, i-befi).c_str());
  136. ++i;
  137. return ret;
  138. }
  139. double readFloat(int & befi, int & i, int andame, std::string & buf) //helper function for void CCreatureHandler::loadUnitAnimInfo()
  140. {
  141. befi=i;
  142. for(; i<andame; ++i)
  143. {
  144. if(buf[i]=='\t')
  145. break;
  146. }
  147. std::string tmp = buf.substr(befi, i-befi);
  148. double ret = atof(buf.substr(befi, i-befi).c_str());
  149. ++i;
  150. return ret;
  151. }
  152. /**
  153. * Determines if a faction is good.
  154. * @param ID of the faction.
  155. * @return true if the faction is good, false otherwise.
  156. */
  157. bool CCreatureHandler::isGood (si8 faction) const
  158. {
  159. return faction != -1 && factionAlignments[faction] == 1;
  160. }
  161. /**
  162. * Determines if a faction is evil.
  163. * @param ID of the faction.
  164. * @return true if the faction is evil, false otherwise.
  165. */
  166. bool CCreatureHandler::isEvil (si8 faction) const
  167. {
  168. return faction != -1 && factionAlignments[faction] == -1;
  169. }
  170. static void AddAbility(CCreature *cre, const JsonVector &ability_vec)
  171. {
  172. Bonus *nsf = new Bonus();
  173. std::string type = ability_vec[0].String();
  174. std::map<std::string, int>::const_iterator it = bonusNameMap.find(type);
  175. if (it == bonusNameMap.end()) {
  176. if (type == "DOUBLE_WIDE")
  177. cre->doubleWide = true;
  178. else if (type == "ENEMY_MORALE_DECREASING") {
  179. cre->addBonus(-1, Bonus::MORALE);
  180. cre->getBonusList().back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  181. }
  182. else if (type == "ENEMY_LUCK_DECREASING") {
  183. cre->addBonus(-1, Bonus::LUCK);
  184. cre->getBonusList().back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  185. } else
  186. tlog1 << "Error: invalid ability type " << type << " in creatures.txt" << std::endl;
  187. return;
  188. }
  189. nsf->type = it->second;
  190. nsf->val = ability_vec[1].Float();
  191. nsf->subtype = ability_vec[2].Float();
  192. nsf->additionalInfo = ability_vec[3].Float();
  193. nsf->source = Bonus::CREATURE_ABILITY;
  194. nsf->sid = cre->idNumber;
  195. //nsf->duration = Bonus::ONE_BATTLE; //what the?
  196. nsf->duration = Bonus::PERMANENT;
  197. nsf->turnsRemain = 0;
  198. cre->addNewBonus(nsf);
  199. }
  200. static void RemoveAbility(CCreature *cre, const JsonNode &ability)
  201. {
  202. std::string type = ability.String();
  203. std::map<std::string, int>::const_iterator it = bonusNameMap.find(type);
  204. if (it == bonusNameMap.end()) {
  205. if (type == "DOUBLE_WIDE")
  206. cre->doubleWide = false;
  207. else
  208. tlog1 << "Error: invalid ability type " << type << " in creatures.json" << std::endl;
  209. return;
  210. }
  211. int typeNo = it->second;
  212. Bonus::BonusType ecf = static_cast<Bonus::BonusType>(typeNo);
  213. Bonus *b = cre->getBonus(Selector::type(ecf));
  214. cre->removeBonus(b);
  215. }
  216. void CCreatureHandler::loadCreatures()
  217. {
  218. tlog5 << "\t\tReading config/cr_abils.json and ZCRTRAIT.TXT" << std::endl;
  219. ////////////reading ZCRTRAIT.TXT ///////////////////
  220. std::string buf = bitmaph->getTextFile("ZCRTRAIT.TXT");
  221. int andame = buf.size();
  222. int i=0; //buf iterator
  223. int hmcr=0;
  224. for(; i<andame; ++i)
  225. {
  226. if(buf[i]=='\r')
  227. ++hmcr;
  228. if(hmcr==2)
  229. break;
  230. }
  231. i+=2;
  232. while(i<buf.size())
  233. {
  234. CCreature &ncre = *new CCreature;
  235. ncre.idNumber = creatures.size();
  236. ncre.cost.resize(GameConstants::RESOURCE_QUANTITY);
  237. ncre.level=0;
  238. int befi=i;
  239. for(; i<andame; ++i)
  240. {
  241. if(buf[i]=='\t')
  242. break;
  243. }
  244. ncre.nameSing = buf.substr(befi, i-befi);
  245. ++i;
  246. befi=i;
  247. for(; i<andame; ++i)
  248. {
  249. if(buf[i]=='\t')
  250. break;
  251. }
  252. ncre.namePl = buf.substr(befi, i-befi);
  253. ++i;
  254. for(int v=0; v<7; ++v)
  255. {
  256. ncre.cost[v] = readNumber(befi, i, andame, buf);
  257. }
  258. ncre.fightValue = readNumber(befi, i, andame, buf);
  259. ncre.AIValue = readNumber(befi, i, andame, buf);
  260. ncre.growth = readNumber(befi, i, andame, buf);
  261. ncre.hordeGrowth = readNumber(befi, i, andame, buf);
  262. ncre.hitPoints = readNumber(befi, i, andame, buf);
  263. ncre.addBonus(ncre.hitPoints, Bonus::STACK_HEALTH);
  264. ncre.speed = readNumber(befi, i, andame, buf);
  265. ncre.addBonus(ncre.speed, Bonus::STACKS_SPEED);
  266. ncre.attack = readNumber(befi, i, andame, buf);
  267. ncre.addBonus(ncre.attack, Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  268. ncre.defence = readNumber(befi, i, andame, buf);
  269. ncre.addBonus(ncre.defence, Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  270. ncre.damageMin = readNumber(befi, i, andame, buf); //not used anymore?
  271. ncre.addBonus(ncre.damageMin, Bonus::CREATURE_DAMAGE, 1);
  272. ncre.damageMax = readNumber(befi, i, andame, buf);
  273. ncre.addBonus(ncre.damageMax, Bonus::CREATURE_DAMAGE, 2);
  274. ncre.shots = readNumber(befi, i, andame, buf);
  275. ncre.addBonus(ncre.shots, Bonus::SHOTS);
  276. ncre.spells = readNumber(befi, i, andame, buf);
  277. ncre.ammMin = readNumber(befi, i, andame, buf);
  278. ncre.ammMax = readNumber(befi, i, andame, buf);
  279. befi=i;
  280. for(; i<andame; ++i)
  281. {
  282. if(buf[i]=='\t')
  283. break;
  284. }
  285. ncre.abilityText = buf.substr(befi, i-befi);
  286. ++i;
  287. befi=i;
  288. for(; i<andame; ++i)
  289. {
  290. if(buf[i]=='\r')
  291. break;
  292. }
  293. ncre.abilityRefs = buf.substr(befi, i-befi);
  294. i+=2;
  295. if(true)
  296. { //adding abilities from ZCRTRAIT.TXT
  297. if(boost::algorithm::find_first(ncre.abilityRefs, "DOUBLE_WIDE"))
  298. ncre.doubleWide = true;
  299. if(boost::algorithm::find_first(ncre.abilityRefs, "FLYING_ARMY"))
  300. ncre.addBonus(0, Bonus::FLYING);
  301. if(boost::algorithm::find_first(ncre.abilityRefs, "SHOOTING_ARMY"))
  302. ncre.addBonus(0, Bonus::SHOOTER);
  303. if(boost::algorithm::find_first(ncre.abilityRefs, "SIEGE_WEAPON"))
  304. ncre.addBonus(0, Bonus::SIEGE_WEAPON);
  305. if(boost::algorithm::find_first(ncre.abilityRefs, "const_two_attacks"))
  306. ncre.addBonus(1, Bonus::ADDITIONAL_ATTACK);
  307. if(boost::algorithm::find_first(ncre.abilityRefs, "const_free_attack"))
  308. ncre.addBonus(0, Bonus::BLOCKS_RETALIATION);
  309. if(boost::algorithm::find_first(ncre.abilityRefs, "IS_UNDEAD"))
  310. ncre.addBonus(0, Bonus::UNDEAD);
  311. if(boost::algorithm::find_first(ncre.abilityRefs, "const_no_melee_penalty"))
  312. ncre.addBonus(0, Bonus::NO_MELEE_PENALTY);
  313. if(boost::algorithm::find_first(ncre.abilityRefs, "const_jousting"))
  314. ncre.addBonus(0, Bonus::JOUSTING);
  315. if(boost::algorithm::find_first(ncre.abilityRefs, "const_raises_morale"))
  316. {
  317. ncre.addBonus(+1, Bonus::MORALE);;
  318. ncre.getBonusList().back()->addPropagator(make_shared<CPropagatorNodeType>(CBonusSystemNode::HERO));
  319. }
  320. if(boost::algorithm::find_first(ncre.abilityRefs, "const_lowers_morale"))
  321. {
  322. ncre.addBonus(-1, Bonus::MORALE);;
  323. ncre.getBonusList().back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  324. }
  325. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_1"))
  326. ncre.addBonus(0, Bonus::KING1);
  327. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_2"))
  328. ncre.addBonus(0, Bonus::KING2);
  329. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_3"))
  330. ncre.addBonus(0, Bonus::KING3);
  331. if(boost::algorithm::find_first(ncre.abilityRefs, "const_no_wall_penalty"))
  332. ncre.addBonus(0, Bonus::NO_WALL_PENALTY);
  333. if(boost::algorithm::find_first(ncre.abilityRefs, "CATAPULT"))
  334. ncre.addBonus(0, Bonus::CATAPULT);
  335. if(boost::algorithm::find_first(ncre.abilityRefs, "MULTI_HEADED"))
  336. ncre.addBonus(0, Bonus::ATTACKS_ALL_ADJACENT);
  337. if(boost::algorithm::find_first(ncre.abilityRefs, "IMMUNE_TO_MIND_SPELLS"))
  338. ncre.addBonus(0, Bonus::MIND_IMMUNITY); //giants are immune to mind spells
  339. if(boost::algorithm::find_first(ncre.abilityRefs, "IMMUNE_TO_FIRE_SPELLS"))
  340. ncre.addBonus(0, Bonus::FIRE_IMMUNITY);
  341. if(boost::algorithm::find_first(ncre.abilityRefs, "HAS_EXTENDED_ATTACK"))
  342. ncre.addBonus(0, Bonus::TWO_HEX_ATTACK_BREATH);;
  343. }
  344. if(ncre.nameSing!=std::string("") && ncre.namePl!=std::string(""))
  345. {
  346. ncre.idNumber = creatures.size();
  347. creatures.push_back(&ncre);
  348. }
  349. }
  350. // loading creatures properties
  351. tlog5 << "\t\tReading config/creatures.json" << std::endl;
  352. const JsonNode config(GameConstants::DATA_DIR + "/config/creatures.json");
  353. BOOST_FOREACH(const JsonNode &creature, config["creatures"].Vector()) {
  354. int creatureID = creature["id"].Float();
  355. const JsonNode *value;
  356. /* A creature can have several names. */
  357. BOOST_FOREACH(const JsonNode &name, creature["name"].Vector()) {
  358. boost::assign::insert(nameToID)(name.String(), creatureID);
  359. }
  360. // Set various creature properties
  361. CCreature *c = creatures[creatureID];
  362. c->level = creature["level"].Float();
  363. c->faction = creature["faction"].Float();
  364. c->animDefName = creature["defname"].String();
  365. value = &creature["upgrade"];
  366. if (!value->isNull())
  367. c->upgrades.insert(value->Float());
  368. value = &creature["projectile_defname"];
  369. if (!value->isNull()) {
  370. idToProjectile[creatureID] = value->String();
  371. value = &creature["projectile_spin"];
  372. idToProjectileSpin[creatureID] = value->Bool();
  373. }
  374. value = &creature["turret_shooter"];
  375. if (!value->isNull() && value->Bool())
  376. factionToTurretCreature[c->faction] = creatureID;
  377. value = &creature["ability_add"];
  378. if (!value->isNull()) {
  379. BOOST_FOREACH(const JsonNode &ability, value->Vector()) {
  380. AddAbility(c, ability.Vector());
  381. }
  382. }
  383. value = &creature["ability_remove"];
  384. if (!value->isNull()) {
  385. BOOST_FOREACH(const JsonNode &ability, value->Vector()) {
  386. RemoveAbility(c, ability);
  387. }
  388. }
  389. }
  390. BOOST_FOREACH(const JsonNode &creature, config["unused_creatures"].Vector()) {
  391. notUsedMonsters += creature.Float();
  392. }
  393. buildBonusTreeForTiers();
  394. loadAnimationInfo();
  395. //reading creature ability names
  396. const JsonNode config2(GameConstants::DATA_DIR + "/config/bonusnames.json");
  397. BOOST_FOREACH(const JsonNode &bonus, config2["bonuses"].Vector()) {
  398. std::map<std::string,int>::const_iterator it_map;
  399. std::string bonusID = bonus["id"].String();
  400. it_map = bonusNameMap.find(bonusID);
  401. if (it_map != bonusNameMap.end()) {
  402. stackBonuses[it_map->second] = std::pair<std::string, std::string>(bonus["name"].String(), bonus["description"].String());
  403. } else
  404. tlog2 << "Bonus " << bonusID << " not recognized, ignoring\n";
  405. }
  406. //handle magic resistance secondary skill premy, potentialy may be buggy
  407. //std::map<TBonusType, std::pair<std::string, std::string> >::iterator it = stackBonuses.find(Bonus::MAGIC_RESISTANCE);
  408. //stackBonuses[Bonus::SECONDARY_SKILL_PREMY] = std::pair<std::string, std::string>(it->second.first, it->second.second);
  409. if (GameConstants::STACK_EXP) //reading default stack experience bonuses
  410. {
  411. buf = bitmaph->getTextFile("CREXPBON.TXT");
  412. int it = 0;
  413. si32 creid = -1;
  414. Bonus b; //prototype with some default properties
  415. b.source = Bonus::STACK_EXPERIENCE;
  416. b.duration = Bonus::PERMANENT;
  417. b.valType = Bonus::ADDITIVE_VALUE;
  418. b.effectRange = Bonus::NO_LIMIT;
  419. b.additionalInfo = 0;
  420. b.turnsRemain = 0;
  421. BonusList bl;
  422. std::string dump2;
  423. loadToIt (dump2, buf, it, 3); //ignore first line
  424. loadToIt (dump2, buf, it, 4); //ignore index
  425. loadStackExp(b, bl, buf, it);
  426. BOOST_FOREACH(Bonus * b, bl)
  427. addBonusForAllCreatures(b); //health bonus is common for all
  428. loadToIt (dump2, buf, it, 3); //crop comment
  429. for (i = 1; i < 7; ++i)
  430. {
  431. for (int j = 0; j < 4; ++j) //four modifiers common for tiers
  432. {
  433. loadToIt (dump2, buf, it, 4); //ignore index
  434. bl.clear();
  435. loadStackExp(b, bl, buf, it);
  436. BOOST_FOREACH(Bonus * b, bl)
  437. addBonusForTier(i, b);
  438. loadToIt (dump2, buf, it, 3); //crop comment
  439. }
  440. }
  441. for (int j = 0; j < 4; ++j) //tier 7
  442. {
  443. loadToIt (dump2, buf, it, 4); //ignore index
  444. bl.clear();
  445. loadStackExp(b, bl, buf, it);
  446. BOOST_FOREACH(Bonus * b, bl)
  447. {
  448. addBonusForTier(7, b);
  449. creaturesOfLevel[0].addNewBonus(b); //bonuses from level 7 are given to high-level creatures
  450. }
  451. loadToIt (dump2, buf, it, 3); //crop comment
  452. }
  453. do //parse everything that's left
  454. {
  455. loadToIt(creid, buf, it, 4); //get index
  456. b.sid = creid; //id = this particular creature ID
  457. loadStackExp(b, creatures[creid]->getBonusList(), buf, it); //add directly to CCreature Node
  458. loadToIt (dump2, buf, it, 3); //crop comment
  459. } while (it < buf.size());
  460. //Calculate rank exp values, formula appears complicated bu no parsing needed
  461. expRanks.resize(8);
  462. int dif = 0;
  463. it = 8000; //ignore name of this variable
  464. expRanks[0].push_back(it);
  465. for (int j = 1; j < 10; ++j) //used for tiers 8-10, and all other probably
  466. {
  467. expRanks[0].push_back(expRanks[0][j-1] + it + dif);
  468. dif += it/5;
  469. }
  470. for (i = 1; i < 8; ++i)
  471. {
  472. dif = 0;
  473. it = 1000 * i;
  474. expRanks[i].push_back(it);
  475. for (int j = 1; j < 10; ++j)
  476. {
  477. expRanks[i].push_back(expRanks[i][j-1] + it + dif);
  478. dif += it/5;
  479. }
  480. }
  481. buf = bitmaph->getTextFile("CREXPMOD.TXT"); //could be hardcoded though, lots of useless info
  482. it = 0;
  483. loadToIt (dump2, buf, it, 3); //ignore first line
  484. maxExpPerBattle.resize(8);
  485. si32 val;
  486. for (i = 1; i < 8; ++i)
  487. {
  488. loadToIt (dump2, buf, it, 4); //index
  489. loadToIt (dump2, buf, it, 4); //float multiplier -> hardcoded
  490. loadToIt (dump2, buf, it, 4); //ignore upgrade mod? ->hardcoded
  491. loadToIt (dump2, buf, it, 4); //already calculated
  492. loadToIt (val, buf, it, 4);
  493. maxExpPerBattle[i] = (ui32)val;
  494. loadToIt (val, buf, it, 4); //11th level
  495. val += (si32)expRanks[i].back();
  496. expRanks[i].push_back((ui32)val);
  497. loadToIt (dump2, buf, it, 3); //crop comment
  498. }
  499. //skeleton gets exp penalty
  500. creatures[56].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
  501. creatures[57].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
  502. //exp for tier >7, rank 11
  503. expRanks[0].push_back(147000);
  504. expAfterUpgrade = 75; //percent
  505. maxExpPerBattle[0] = maxExpPerBattle[7];
  506. }//end of Stack Experience
  507. //experiment - add 100 to attack for creatures of tier 1
  508. // Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::OTHER, +100, 0, 0);
  509. // addBonusForTier(1, b);
  510. }
  511. void CCreatureHandler::loadAnimationInfo()
  512. {
  513. std::string buf = bitmaph->getTextFile("CRANIM.TXT");
  514. int andame = buf.size();
  515. int i=0; //buf iterator
  516. int hmcr=0;
  517. for(; i<andame; ++i)
  518. {
  519. if(buf[i]=='\r')
  520. ++hmcr;
  521. if(hmcr==2)
  522. break;
  523. }
  524. i+=2;
  525. for(int dd=0; dd<creatures.size(); ++dd)
  526. {
  527. //tlog5 << "\t\t\tReading animation info for creature " << dd << std::endl;
  528. loadUnitAnimInfo(*creatures[dd], buf, i);
  529. }
  530. return;
  531. }
  532. void CCreatureHandler::loadUnitAnimInfo(CCreature & unit, std::string & src, int & i)
  533. {
  534. int befi=i;
  535. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  536. while(unit.timeBetweenFidgets == 0.0)
  537. {
  538. for(; i<src.size(); ++i)
  539. {
  540. if(src[i]=='\r')
  541. break;
  542. }
  543. i+=2;
  544. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  545. }
  546. unit.walkAnimationTime = readFloat(befi, i, src.size(), src);
  547. unit.attackAnimationTime = readFloat(befi, i, src.size(), src);
  548. unit.flightAnimationDistance = readFloat(befi, i, src.size(), src);
  549. ///////////////////////
  550. unit.upperRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  551. unit.upperRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  552. unit.rightMissleOffsetX = readNumber(befi, i, src.size(), src);
  553. unit.rightMissleOffsetY = readNumber(befi, i, src.size(), src);
  554. unit.lowerRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  555. unit.lowerRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  556. ///////////////////////
  557. for(int jjj=0; jjj<12; ++jjj)
  558. {
  559. unit.missleFrameAngles[jjj] = readFloat(befi, i, src.size(), src);
  560. }
  561. unit.troopCountLocationOffset= readNumber(befi, i, src.size(), src);
  562. unit.attackClimaxFrame = readNumber(befi, i, src.size(), src);
  563. for(; i<src.size(); ++i)
  564. {
  565. if(src[i]=='\r')
  566. break;
  567. }
  568. i+=2;
  569. }
  570. void CCreatureHandler::loadStackExp(Bonus & b, BonusList & bl, std::string & src, int & it) //help function for parsing CREXPBON.txt
  571. {
  572. std::string buf, mod;
  573. bool enable = false; //some bonuses are activated with values 2 or 1
  574. loadToIt(buf, src, it, 4);
  575. loadToIt(mod, src, it, 4);
  576. switch (buf[0])
  577. {
  578. case 'H':
  579. b.type = Bonus::STACK_HEALTH;
  580. b.valType = Bonus::PERCENT_TO_BASE;
  581. break;
  582. case 'A':
  583. b.type = Bonus::PRIMARY_SKILL;
  584. b.subtype = PrimarySkill::ATTACK;
  585. break;
  586. case 'D':
  587. b.type = Bonus::PRIMARY_SKILL;
  588. b.subtype = PrimarySkill::DEFENSE;
  589. break;
  590. case 'M': //Max damage
  591. b.type = Bonus::CREATURE_DAMAGE;
  592. b.subtype = 2;
  593. break;
  594. case 'm': //Min damage
  595. b.type = Bonus::CREATURE_DAMAGE;
  596. b.subtype = 1;
  597. break;
  598. case 'S':
  599. b.type = Bonus::STACKS_SPEED; break;
  600. case 'O':
  601. b.type = Bonus::SHOTS; break;
  602. case 'b':
  603. b.type = Bonus::ENEMY_DEFENCE_REDUCTION; break;
  604. case 'C':
  605. b.type = Bonus::CHANGES_SPELL_COST_FOR_ALLY; break;
  606. case 'd':
  607. b.type = Bonus::DEFENSIVE_STANCE; break;
  608. case 'e':
  609. b.type = Bonus::DOUBLE_DAMAGE_CHANCE; break;
  610. case 'E':
  611. b.type = Bonus::DEATH_STARE;
  612. b.subtype = 0; //Gorgon
  613. break;
  614. case 'g':
  615. b.type = Bonus::SPELL_DAMAGE_REDUCTION;
  616. b.subtype = -1; //all magic schools
  617. break;
  618. case 'P':
  619. b.type = Bonus::CASTS; break;
  620. case 'R':
  621. b.type = Bonus::ADDITIONAL_RETALIATION; break;
  622. case 'W':
  623. b.type = Bonus::MAGIC_RESISTANCE;
  624. b.subtype = 0; //otherwise creature window goes crazy
  625. break;
  626. case 'f': //on-off skill
  627. enable = true; //sometimes format is: 2 -> 0, 1 -> 1
  628. switch (mod[0])
  629. {
  630. case 'A':
  631. b.type = Bonus::ATTACKS_ALL_ADJACENT; break;
  632. case 'b':
  633. b.type = Bonus::RETURN_AFTER_STRIKE; break;
  634. case 'B':
  635. b.type = Bonus::TWO_HEX_ATTACK_BREATH; break;
  636. case 'c':
  637. b.type = Bonus::JOUSTING; break;
  638. case 'D':
  639. b.type = Bonus::ADDITIONAL_ATTACK; break;
  640. case 'f':
  641. b.type = Bonus::FEARLESS; break;
  642. case 'F':
  643. b.type = Bonus::FLYING; break;
  644. case 'm':
  645. b.type = Bonus::SELF_MORALE; break;
  646. case 'M':
  647. b.type = Bonus::NO_MORALE; break;
  648. case 'p': //Mind spells
  649. case 'P':
  650. b.type = Bonus::MIND_IMMUNITY; break;
  651. case 'r':
  652. b.type = Bonus::REBIRTH; //on/off? makes sense?
  653. b.subtype = 0;
  654. b.val = 20; //arbitrary value
  655. break;
  656. case 'R':
  657. b.type = Bonus::BLOCKS_RETALIATION; break;
  658. case 's':
  659. b.type = Bonus::FREE_SHOOTING; break;
  660. case 'u':
  661. b.type = Bonus::SPELL_RESISTANCE_AURA; break;
  662. case 'U':
  663. b.type = Bonus::UNDEAD; break;
  664. default:
  665. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  666. return;
  667. break;
  668. }
  669. break;
  670. case 'w': //specific spell immunities, enabled/disabled
  671. enable = true;
  672. switch (mod[0])
  673. {
  674. case 'B': //Blind
  675. b.type = Bonus::SPELL_IMMUNITY;
  676. b.subtype = 74;
  677. break;
  678. case 'H': //Hypnotize
  679. b.type = Bonus::SPELL_IMMUNITY;
  680. b.subtype = 60;
  681. break;
  682. case 'I': //Implosion
  683. b.type = Bonus::SPELL_IMMUNITY;
  684. b.subtype = 18;
  685. break;
  686. case 'K': //Berserk
  687. b.type = Bonus::SPELL_IMMUNITY;
  688. b.subtype = 59;
  689. break;
  690. case 'M': //Meteor Shower
  691. b.type = Bonus::SPELL_IMMUNITY;
  692. b.subtype = 23;
  693. break;
  694. case 'N': //dispell beneficial spells
  695. b.type = Bonus::SPELL_IMMUNITY;
  696. b.subtype = 78;
  697. break;
  698. case 'R': //Armageddon
  699. b.type = Bonus::SPELL_IMMUNITY;
  700. b.subtype = 26;
  701. break;
  702. case 'S': //Slow
  703. b.type = Bonus::SPELL_IMMUNITY;
  704. b.subtype = 54;
  705. break;
  706. case '6':
  707. case '7':
  708. case '8':
  709. case '9':
  710. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  711. b.val = std::atoi(mod.c_str()) - 5;
  712. break;
  713. case ':':
  714. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  715. b.val = GameConstants::SPELL_LEVELS; //in case someone adds higher level spells?
  716. break;
  717. case 'F':
  718. b.type = Bonus::FIRE_IMMUNITY;
  719. b.subtype = 1; //not positive
  720. break;
  721. case 'O':
  722. b.type = Bonus::FIRE_IMMUNITY;
  723. b.subtype = 2; //only direct damage
  724. break;
  725. case 'f':
  726. b.type = Bonus::FIRE_IMMUNITY;
  727. b.subtype = 0; //all
  728. break;
  729. case 'C':
  730. b.type = Bonus::WATER_IMMUNITY;
  731. b.subtype = 1; //not positive
  732. break;
  733. case 'W':
  734. b.type = Bonus::WATER_IMMUNITY;
  735. b.subtype = 2; //only direct damage
  736. break;
  737. case 'w':
  738. b.type = Bonus::WATER_IMMUNITY;
  739. b.subtype = 0; //all
  740. break;
  741. case 'E':
  742. b.type = Bonus::EARTH_IMMUNITY;
  743. b.subtype = 2; //only direct damage
  744. break;
  745. case 'e':
  746. b.type = Bonus::EARTH_IMMUNITY;
  747. b.subtype = 0; //all
  748. break;
  749. case 'A':
  750. b.type = Bonus::AIR_IMMUNITY;
  751. b.subtype = 2; //only direct damage
  752. break;
  753. case 'a':
  754. b.type = Bonus::AIR_IMMUNITY;
  755. b.subtype = 0; //all
  756. break;
  757. case 'D':
  758. b.type = Bonus::DIRECT_DAMAGE_IMMUNITY;
  759. break;
  760. case '0':
  761. b.type = Bonus::RECEPTIVE;
  762. break;
  763. default:
  764. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  765. return;
  766. }
  767. break;
  768. case 'i':
  769. enable = true;
  770. b.type = Bonus::NO_DISTANCE_PENALTY;
  771. break;
  772. case 'o':
  773. enable = true;
  774. b.type = Bonus::NO_WALL_PENALTY;
  775. break;
  776. case 'a':
  777. case 'c': //some special abilities are threated as spells, work in progress
  778. b.type = Bonus::SPELL_AFTER_ATTACK;
  779. b.subtype = stringToNumber(mod);
  780. break;
  781. case 'h':
  782. b.type= Bonus::HATE;
  783. b.subtype = stringToNumber(mod);
  784. break;
  785. case 'p':
  786. b.type = Bonus::SPELL_BEFORE_ATTACK;
  787. b.subtype = stringToNumber(mod);
  788. b.additionalInfo = 3; //always expert?
  789. break;
  790. default:
  791. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  792. return;
  793. break;
  794. }
  795. switch (mod[0])
  796. {
  797. case '+':
  798. case '=': //should we allow percent values to stack or pick highest?
  799. b.valType = Bonus::ADDITIVE_VALUE;
  800. break;
  801. }
  802. //limiters, range
  803. si32 lastVal, curVal, lastLev = 0;
  804. if (enable) //0 and 2 means non-active, 1 - active
  805. {
  806. if (b.type != Bonus::REBIRTH)
  807. b.val = 0; //on-off ability, no value specified
  808. loadToIt (curVal, src, it, 4); // 0 level is never active
  809. for (int i = 1; i < 11; ++i)
  810. {
  811. loadToIt (curVal, src, it, 4);
  812. if (curVal == 1)
  813. {
  814. b.limiter.reset (new RankRangeLimiter(i));
  815. bl.push_back(new Bonus(b));
  816. break; //never turned off it seems
  817. }
  818. }
  819. }
  820. else
  821. {
  822. loadToIt (lastVal, src, it, 4); //basic value, not particularly useful but existent
  823. for (int i = 1; i < 11; ++i)
  824. {
  825. loadToIt (curVal, src, it, 4);
  826. if (b.type == Bonus::HATE)
  827. curVal *= 10; //odd fix
  828. if (curVal > lastVal) //threshold, add new bonus
  829. {
  830. b.val = curVal - lastVal;
  831. lastVal = curVal;
  832. b.limiter.reset (new RankRangeLimiter(i));
  833. bl.push_back(new Bonus(b));
  834. lastLev = i; //start new range from here, i = previous rank
  835. }
  836. else if (curVal < lastVal)
  837. {
  838. b.val = lastVal;
  839. b.limiter.reset (new RankRangeLimiter(lastLev, i));
  840. }
  841. }
  842. }
  843. }
  844. int CCreatureHandler::stringToNumber(std::string & s)
  845. {
  846. boost::algorithm::replace_first(s,"#",""); //drop hash character
  847. return std::atoi(s.c_str());
  848. }
  849. CCreatureHandler::~CCreatureHandler()
  850. {
  851. }
  852. static int retreiveRandNum(const boost::function<int()> &randGen)
  853. {
  854. if(randGen)
  855. return randGen();
  856. else
  857. return rand();
  858. }
  859. template <typename T> const T & pickRandomElementOf(const std::vector<T> &v, const boost::function<int()> &randGen)
  860. {
  861. return v[retreiveRandNum(randGen) % v.size()];
  862. }
  863. int CCreatureHandler::pickRandomMonster(const boost::function<int()> &randGen, int tier) const
  864. {
  865. int r = 0;
  866. if(tier == -1) //pick any allowed creature
  867. {
  868. do
  869. {
  870. r = pickRandomElementOf(creatures, randGen)->idNumber;
  871. } while (vstd::contains(VLC->creh->notUsedMonsters,r));
  872. }
  873. else
  874. {
  875. assert(vstd::iswithin(tier, 1, 7));
  876. std::vector<int> allowed;
  877. BOOST_FOREACH(const CBonusSystemNode *b, creaturesOfLevel[tier].getChildrenNodes())
  878. {
  879. assert(b->getNodeType() == CBonusSystemNode::CREATURE);
  880. int creid = static_cast<const CCreature*>(b)->idNumber;
  881. if(!vstd::contains(notUsedMonsters, creid))
  882. allowed.push_back(creid);
  883. }
  884. if(!allowed.size())
  885. {
  886. tlog2 << "Cannot pick a random creature of tier " << tier << "!\n";
  887. return 0;
  888. }
  889. return pickRandomElementOf(allowed, randGen);
  890. }
  891. return r;
  892. }
  893. void CCreatureHandler::addBonusForTier(int tier, Bonus *b)
  894. {
  895. assert(vstd::iswithin(tier, 1, 7));
  896. creaturesOfLevel[tier].addNewBonus(b);
  897. }
  898. void CCreatureHandler::addBonusForAllCreatures(Bonus *b)
  899. {
  900. allCreatures.addNewBonus(b);
  901. }
  902. void CCreatureHandler::buildBonusTreeForTiers()
  903. {
  904. BOOST_FOREACH(CCreature *c, creatures)
  905. {
  906. if(vstd::isbetween(c->level, 0, ARRAY_COUNT(creaturesOfLevel)))
  907. c->attachTo(&creaturesOfLevel[c->level]);
  908. else
  909. c->attachTo(&creaturesOfLevel[0]);
  910. }
  911. BOOST_FOREACH(CBonusSystemNode &b, creaturesOfLevel)
  912. b.attachTo(&allCreatures);
  913. }
  914. void CCreatureHandler::deserializationFix()
  915. {
  916. buildBonusTreeForTiers();
  917. }