CCreatureHandler.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. /*
  2. * CCreatureHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CCreatureHandler.h"
  12. #include "CGeneralTextHandler.h"
  13. #include "filesystem/Filesystem.h"
  14. #include "VCMI_Lib.h"
  15. #include "CGameState.h"
  16. #include "CTownHandler.h"
  17. #include "CModHandler.h"
  18. #include "StringConstants.h"
  19. #include "mapObjects/CObjectClassesHandler.h"
  20. int CCreature::getQuantityID(const int & quantity)
  21. {
  22. if (quantity<5)
  23. return 1;
  24. if (quantity<10)
  25. return 2;
  26. if (quantity<20)
  27. return 3;
  28. if (quantity<50)
  29. return 4;
  30. if (quantity<100)
  31. return 5;
  32. if (quantity<250)
  33. return 6;
  34. if (quantity<500)
  35. return 7;
  36. if (quantity<1000)
  37. return 8;
  38. return 9;
  39. }
  40. int CCreature::estimateCreatureCount(ui32 countID)
  41. {
  42. static const int creature_count[] = { 0, 3, 8, 15, 35, 75, 175, 375, 750, 2500 };
  43. if(countID > 9)
  44. {
  45. logGlobal->error("Wrong countID %d!", countID);
  46. return 0;
  47. }
  48. else
  49. return creature_count[countID];
  50. }
  51. bool CCreature::isDoubleWide() const
  52. {
  53. return doubleWide;
  54. }
  55. bool CCreature::isFlying() const
  56. {
  57. return hasBonusOfType(Bonus::FLYING);
  58. }
  59. bool CCreature::isShooting() const
  60. {
  61. return hasBonusOfType(Bonus::SHOOTER);
  62. }
  63. bool CCreature::isUndead() const
  64. {
  65. return hasBonusOfType(Bonus::UNDEAD);
  66. }
  67. /**
  68. * Determines if the creature is of a good alignment.
  69. * @return true if the creture is good, false otherwise.
  70. */
  71. bool CCreature::isGood () const
  72. {
  73. return VLC->townh->factions[faction]->alignment == EAlignment::GOOD;
  74. }
  75. /**
  76. * Determines if the creature is of an evil alignment.
  77. * @return true if the creature is evil, false otherwise.
  78. */
  79. bool CCreature::isEvil () const
  80. {
  81. return VLC->townh->factions[faction]->alignment == EAlignment::EVIL;
  82. }
  83. si32 CCreature::maxAmount(const std::vector<si32> &res) const //how many creatures can be bought
  84. {
  85. int ret = 2147483645;
  86. int resAmnt = static_cast<int>(std::min(res.size(),cost.size()));
  87. for(int i=0;i<resAmnt;i++)
  88. if(cost[i])
  89. ret = std::min(ret,(int)(res[i]/cost[i]));
  90. return ret;
  91. }
  92. CCreature::CCreature()
  93. {
  94. setNodeType(CBonusSystemNode::CREATURE);
  95. faction = 0;
  96. level = 0;
  97. fightValue = AIValue = growth = hordeGrowth = ammMin = ammMax = 0;
  98. doubleWide = false;
  99. special = true;
  100. iconIndex = -1;
  101. }
  102. void CCreature::addBonus(int val, Bonus::BonusType type, int subtype)
  103. {
  104. auto added = std::make_shared<Bonus>(Bonus::PERMANENT, type, Bonus::CREATURE_ABILITY, val, idNumber, subtype, Bonus::BASE_NUMBER);
  105. addNewBonus(added);
  106. }
  107. bool CCreature::isMyUpgrade(const CCreature *anotherCre) const
  108. {
  109. //TODO upgrade of upgrade?
  110. return vstd::contains(upgrades, anotherCre->idNumber);
  111. }
  112. bool CCreature::valid() const
  113. {
  114. return this == VLC->creh->creatures[idNumber];
  115. }
  116. std::string CCreature::nodeName() const
  117. {
  118. return "\"" + namePl + "\"";
  119. }
  120. bool CCreature::isItNativeTerrain(ETerrainType::EETerrainType terrain) const
  121. {
  122. auto native = getNativeTerrain();
  123. return native == terrain || native == ETerrainType::ANY_TERRAIN;
  124. }
  125. ETerrainType::EETerrainType CCreature::getNativeTerrain() const
  126. {
  127. //this code is used in the CreatureTerrainLimiter::limit to setup battle bonuses
  128. //and in the CGHeroInstance::getNativeTerrain() to setup mevement bonuses or/and penalties.
  129. return hasBonusOfType(Bonus::NO_TERRAIN_PENALTY) ?
  130. ETerrainType::ANY_TERRAIN
  131. : (ETerrainType::EETerrainType)VLC->townh->factions[faction]->nativeTerrain;
  132. }
  133. void CCreature::setId(CreatureID ID)
  134. {
  135. idNumber = ID;
  136. for(auto bonus : getExportedBonusList())
  137. {
  138. if(bonus->source == Bonus::CREATURE_ABILITY)
  139. bonus->sid = ID;
  140. }
  141. CBonusSystemNode::treeHasChanged();
  142. }
  143. void CCreature::fillWarMachine()
  144. {
  145. switch (idNumber)
  146. {
  147. case CreatureID::CATAPULT: //Catapult
  148. warMachine = ArtifactID::CATAPULT;
  149. break;
  150. case CreatureID::BALLISTA: //Ballista
  151. warMachine = ArtifactID::BALLISTA;
  152. break;
  153. case CreatureID::FIRST_AID_TENT: //First Aid tent
  154. warMachine = ArtifactID::FIRST_AID_TENT;
  155. break;
  156. case CreatureID::AMMO_CART: //Ammo cart
  157. warMachine = ArtifactID::AMMO_CART;
  158. break;
  159. }
  160. warMachine = ArtifactID::NONE; //this creature is not artifact
  161. }
  162. static void AddAbility(CCreature *cre, const JsonVector &ability_vec)
  163. {
  164. auto nsf = std::make_shared<Bonus>();
  165. std::string type = ability_vec[0].String();
  166. auto it = bonusNameMap.find(type);
  167. if (it == bonusNameMap.end()) {
  168. if (type == "DOUBLE_WIDE")
  169. cre->doubleWide = true;
  170. else if (type == "ENEMY_MORALE_DECREASING") {
  171. cre->addBonus(-1, Bonus::MORALE);
  172. cre->getBonusList().back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  173. }
  174. else if (type == "ENEMY_LUCK_DECREASING") {
  175. cre->addBonus(-1, Bonus::LUCK);
  176. cre->getBonusList().back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  177. } else
  178. logGlobal->error("Error: invalid ability type %s in creatures config", type);
  179. return;
  180. }
  181. nsf->type = it->second;
  182. JsonUtils::parseTypedBonusShort(ability_vec,nsf);
  183. nsf->source = Bonus::CREATURE_ABILITY;
  184. nsf->sid = cre->idNumber;
  185. cre->addNewBonus(nsf);
  186. }
  187. CCreatureHandler::CCreatureHandler()
  188. : expAfterUpgrade(0)
  189. {
  190. VLC->creh = this;
  191. allCreatures.setDescription("All creatures");
  192. allCreatures.setNodeType(CBonusSystemNode::ENodeTypes::ALL_CREATURES);
  193. creaturesOfLevel[0].setDescription("Creatures of unnormalized tier");
  194. for(int i = 1; i < ARRAY_COUNT(creaturesOfLevel); i++)
  195. creaturesOfLevel[i].setDescription("Creatures of tier " + boost::lexical_cast<std::string>(i));
  196. loadCommanders();
  197. }
  198. const CCreature * CCreatureHandler::getCreature(const std::string & scope, const std::string & identifier) const
  199. {
  200. boost::optional<si32> index = VLC->modh->identifiers.getIdentifier(scope, "creature", identifier);
  201. if(!index)
  202. throw std::runtime_error("Creature not found "+identifier);
  203. return creatures[*index];
  204. }
  205. void CCreatureHandler::loadCommanders()
  206. {
  207. JsonNode data(ResourceID("config/commanders.json"));
  208. data.setMeta("core"); // assume that commanders are in core mod (for proper bonuses resolution)
  209. const JsonNode & config = data; // switch to const data accessors
  210. for (auto bonus : config["bonusPerLevel"].Vector())
  211. {
  212. commanderLevelPremy.push_back(JsonUtils::parseBonus(bonus.Vector()));
  213. }
  214. int i = 0;
  215. for (auto skill : config["skillLevels"].Vector())
  216. {
  217. skillLevels.push_back (std::vector<ui8>());
  218. for (auto skillLevel : skill["levels"].Vector())
  219. {
  220. skillLevels[i].push_back ((ui8)skillLevel.Float());
  221. }
  222. ++i;
  223. }
  224. for (auto ability : config["abilityRequirements"].Vector())
  225. {
  226. std::pair <std::shared_ptr<Bonus>, std::pair <ui8, ui8> > a;
  227. a.first = JsonUtils::parseBonus (ability["ability"].Vector());
  228. a.second.first = static_cast<ui8>(ability["skills"].Vector()[0].Float());
  229. a.second.second = static_cast<ui8>(ability["skills"].Vector()[1].Float());
  230. skillRequirements.push_back (a);
  231. }
  232. }
  233. void CCreatureHandler::loadBonuses(JsonNode & creature, std::string bonuses)
  234. {
  235. auto makeBonusNode = [&](std::string type) -> JsonNode
  236. {
  237. JsonNode ret;
  238. ret["type"].String() = type;
  239. return ret;
  240. };
  241. static const std::map<std::string, JsonNode> abilityMap =
  242. {
  243. {"FLYING_ARMY", makeBonusNode("FLYING")},
  244. {"SHOOTING_ARMY", makeBonusNode("SHOOTER")},
  245. {"SIEGE_WEAPON", makeBonusNode("SIEGE_WEAPON")},
  246. {"const_free_attack", makeBonusNode("BLOCKS_RETALIATION")},
  247. {"IS_UNDEAD", makeBonusNode("UNDEAD")},
  248. {"const_no_melee_penalty", makeBonusNode("NO_MELEE_PENALTY")},
  249. {"const_jousting", makeBonusNode("JOUSTING")},
  250. {"KING_1", makeBonusNode("KING1")},
  251. {"KING_2", makeBonusNode("KING2")},
  252. {"KING_3", makeBonusNode("KING3")},
  253. {"const_no_wall_penalty", makeBonusNode("NO_WALL_PENALTY")},
  254. {"CATAPULT", makeBonusNode("CATAPULT")},
  255. {"MULTI_HEADED", makeBonusNode("ATTACKS_ALL_ADJACENT")},
  256. {"IMMUNE_TO_MIND_SPELLS", makeBonusNode("MIND_IMMUNITY")},
  257. {"HAS_EXTENDED_ATTACK", makeBonusNode("TWO_HEX_ATTACK_BREATH")}
  258. };
  259. auto hasAbility = [&](const std::string name) -> bool
  260. {
  261. return boost::algorithm::find_first(bonuses, name);
  262. };
  263. for(auto a : abilityMap)
  264. {
  265. if(hasAbility(a.first))
  266. creature["abilities"][a.first] = a.second;
  267. }
  268. if(hasAbility("DOUBLE_WIDE"))
  269. creature["doubleWide"].Bool() = true;
  270. if(hasAbility("const_raises_morale"))
  271. {
  272. JsonNode node = makeBonusNode("MORALE");
  273. node["val"].Float() = 1;
  274. node["propagator"].String() = "HERO";
  275. creature["abilities"]["const_raises_morale"] = node;
  276. }
  277. if(hasAbility("const_lowers_morale"))
  278. {
  279. JsonNode node = makeBonusNode("MORALE");
  280. node["val"].Float() = -1;
  281. node["effectRange"].String() = "ONLY_ENEMY_ARMY";
  282. creature["abilities"]["const_lowers_morale"] = node;
  283. }
  284. }
  285. std::vector<JsonNode> CCreatureHandler::loadLegacyData(size_t dataSize)
  286. {
  287. creatures.resize(dataSize);
  288. std::vector<JsonNode> h3Data;
  289. h3Data.reserve(dataSize);
  290. CLegacyConfigParser parser("DATA/CRTRAITS.TXT");
  291. parser.endLine(); // header
  292. // this file is a bit different in some of Russian localisations:
  293. //ENG: Singular Plural Wood ...
  294. //RUS: Singular Plural Plural2 Wood ...
  295. // Try to detect which version this is by header
  296. // TODO: use 3rd name? Stand for "whose", e.g. pikemans'
  297. size_t namesCount;
  298. {
  299. if ( parser.readString() != "Singular" || parser.readString() != "Plural" )
  300. throw std::runtime_error("Incorrect format of CrTraits.txt");
  301. if (parser.readString() == "Plural2")
  302. namesCount = 3;
  303. else
  304. namesCount = 2;
  305. parser.endLine();
  306. }
  307. for (size_t i=0; i<dataSize; i++)
  308. {
  309. //loop till non-empty line
  310. while (parser.isNextEntryEmpty())
  311. parser.endLine();
  312. JsonNode data;
  313. data["name"]["singular"].String() = parser.readString();
  314. if (namesCount == 3)
  315. parser.readString();
  316. data["name"]["plural"].String() = parser.readString();
  317. for(int v=0; v<7; ++v)
  318. data["cost"][GameConstants::RESOURCE_NAMES[v]].Float() = parser.readNumber();
  319. data["fightValue"].Float() = parser.readNumber();
  320. data["aiValue"].Float() = parser.readNumber();
  321. data["growth"].Float() = parser.readNumber();
  322. data["horde"].Float() = parser.readNumber();
  323. data["hitPoints"].Float() = parser.readNumber();
  324. data["speed"].Float() = parser.readNumber();
  325. data["attack"].Float() = parser.readNumber();
  326. data["defense"].Float() = parser.readNumber();
  327. data["damage"]["min"].Float() = parser.readNumber();
  328. data["damage"]["max"].Float() = parser.readNumber();
  329. if (float shots = parser.readNumber())
  330. data["shots"].Float() = shots;
  331. if (float spells = parser.readNumber())
  332. data["spellPoints"].Float() = spells;
  333. data["advMapAmount"]["min"].Float() = parser.readNumber();
  334. data["advMapAmount"]["max"].Float() = parser.readNumber();
  335. data["abilityText"].String() = parser.readString();
  336. loadBonuses(data, parser.readString()); //Attributes
  337. h3Data.push_back(data);
  338. }
  339. loadAnimationInfo(h3Data);
  340. return h3Data;
  341. }
  342. void CCreatureHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  343. {
  344. auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name));
  345. object->setId(CreatureID((si32)creatures.size()));
  346. object->iconIndex = object->idNumber + 2;
  347. creatures.push_back(object);
  348. VLC->modh->identifiers.requestIdentifier(scope, "object", "monster", [=](si32 index)
  349. {
  350. JsonNode conf;
  351. conf.setMeta(scope);
  352. VLC->objtypeh->loadSubObject(object->identifier, conf, Obj::MONSTER, object->idNumber.num);
  353. if (!object->advMapDef.empty())
  354. {
  355. JsonNode templ;
  356. templ["animation"].String() = object->advMapDef;
  357. VLC->objtypeh->getHandlerFor(Obj::MONSTER, object->idNumber.num)->addTemplate(templ);
  358. }
  359. // object does not have any templates - this is not usable object (e.g. pseudo-creature like Arrow Tower)
  360. if (VLC->objtypeh->getHandlerFor(Obj::MONSTER, object->idNumber.num)->getTemplates().empty())
  361. VLC->objtypeh->removeSubObject(Obj::MONSTER, object->idNumber.num);
  362. });
  363. registerObject(scope, "creature", name, object->idNumber);
  364. for(auto node : data["extraNames"].Vector())
  365. {
  366. registerObject(scope, "creature", node.String(), object->idNumber);
  367. }
  368. }
  369. void CCreatureHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  370. {
  371. auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name));
  372. object->setId(CreatureID((si32)index));
  373. object->iconIndex = object->idNumber + 2;
  374. if(data["hasDoubleWeek"].Bool()) //
  375. {
  376. doubledCreatures.insert (object->idNumber); //we need to have id (or identifier) before it is inserted
  377. }
  378. assert(creatures[index] == nullptr); // ensure that this id was not loaded before
  379. creatures[index] = object;
  380. VLC->modh->identifiers.requestIdentifier(scope, "object", "monster", [=](si32 index)
  381. {
  382. JsonNode conf;
  383. conf.setMeta(scope);
  384. VLC->objtypeh->loadSubObject(object->identifier, conf, Obj::MONSTER, object->idNumber.num);
  385. if (!object->advMapDef.empty())
  386. {
  387. JsonNode templ;
  388. templ["animation"].String() = object->advMapDef;
  389. VLC->objtypeh->getHandlerFor(Obj::MONSTER, object->idNumber.num)->addTemplate(templ);
  390. }
  391. // object does not have any templates - this is not usable object (e.g. pseudo-creature like Arrow Tower)
  392. if (VLC->objtypeh->getHandlerFor(Obj::MONSTER, object->idNumber.num)->getTemplates().empty())
  393. VLC->objtypeh->removeSubObject(Obj::MONSTER, object->idNumber.num);
  394. });
  395. registerObject(scope, "creature", name, object->idNumber);
  396. for(auto & node : data["extraNames"].Vector())
  397. {
  398. registerObject(scope, "creature", node.String(), object->idNumber);
  399. }
  400. }
  401. std::vector<bool> CCreatureHandler::getDefaultAllowed() const
  402. {
  403. std::vector<bool> ret;
  404. for(const CCreature * crea : creatures)
  405. {
  406. ret.push_back(crea ? !crea->special : false);
  407. }
  408. return ret;
  409. }
  410. void CCreatureHandler::loadCrExpBon()
  411. {
  412. if (VLC->modh->modules.STACK_EXP) //reading default stack experience bonuses
  413. {
  414. CLegacyConfigParser parser("DATA/CREXPBON.TXT");
  415. Bonus b; //prototype with some default properties
  416. b.source = Bonus::STACK_EXPERIENCE;
  417. b.duration = Bonus::PERMANENT;
  418. b.valType = Bonus::ADDITIVE_VALUE;
  419. b.effectRange = Bonus::NO_LIMIT;
  420. b.additionalInfo = 0;
  421. b.turnsRemain = 0;
  422. BonusList bl;
  423. parser.endLine();
  424. parser.readString(); //ignore index
  425. loadStackExp(b, bl, parser);
  426. for(auto b : bl)
  427. addBonusForAllCreatures(b); //health bonus is common for all
  428. parser.endLine();
  429. for (int i = 1; i < 7; ++i)
  430. {
  431. for (int j = 0; j < 4; ++j) //four modifiers common for tiers
  432. {
  433. parser.readString(); //ignore index
  434. bl.clear();
  435. loadStackExp(b, bl, parser);
  436. for(auto b : bl)
  437. addBonusForTier(i, b);
  438. parser.endLine();
  439. }
  440. }
  441. for (int j = 0; j < 4; ++j) //tier 7
  442. {
  443. parser.readString(); //ignore index
  444. bl.clear();
  445. loadStackExp(b, bl, parser);
  446. for(auto b : bl)
  447. {
  448. addBonusForTier(7, b);
  449. creaturesOfLevel[0].addNewBonus(b); //bonuses from level 7 are given to high-level creatures
  450. }
  451. parser.endLine();
  452. }
  453. do //parse everything that's left
  454. {
  455. auto sid = static_cast<ui32>(parser.readNumber()); //id = this particular creature ID
  456. b.sid = sid;
  457. bl.clear();
  458. loadStackExp(b, bl, parser);
  459. for(auto b : bl)
  460. {
  461. creatures[sid]->addNewBonus(b); //add directly to CCreature Node
  462. }
  463. }
  464. while (parser.endLine());
  465. //Calculate rank exp values, formula appears complicated bu no parsing needed
  466. expRanks.resize(8);
  467. int dif = 0;
  468. int it = 8000; //ignore name of this variable
  469. expRanks[0].push_back(it);
  470. for (int j = 1; j < 10; ++j) //used for tiers 8-10, and all other probably
  471. {
  472. expRanks[0].push_back(expRanks[0][j-1] + it + dif);
  473. dif += it/5;
  474. }
  475. for (int i = 1; i < 8; ++i)
  476. {
  477. dif = 0;
  478. it = 1000 * i;
  479. expRanks[i].push_back(it);
  480. for (int j = 1; j < 10; ++j)
  481. {
  482. expRanks[i].push_back(expRanks[i][j-1] + it + dif);
  483. dif += it/5;
  484. }
  485. }
  486. CLegacyConfigParser expBonParser("DATA/CREXPMOD.TXT");
  487. expBonParser.endLine(); //header
  488. maxExpPerBattle.resize(8);
  489. for (int i = 1; i < 8; ++i)
  490. {
  491. expBonParser.readString(); //index
  492. expBonParser.readString(); //float multiplier -> hardcoded
  493. expBonParser.readString(); //ignore upgrade mod? ->hardcoded
  494. expBonParser.readString(); //already calculated
  495. maxExpPerBattle[i] = static_cast<ui32>(expBonParser.readNumber());
  496. expRanks[i].push_back(expRanks[i].back() + (ui32)expBonParser.readNumber());
  497. expBonParser.endLine();
  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. }
  508. void CCreatureHandler::loadAnimationInfo(std::vector<JsonNode> &h3Data)
  509. {
  510. CLegacyConfigParser parser("DATA/CRANIM.TXT");
  511. parser.endLine(); // header
  512. parser.endLine();
  513. for(int dd=0; dd<VLC->modh->settings.data["textData"]["creature"].Float(); ++dd)
  514. {
  515. while (parser.isNextEntryEmpty() && parser.endLine()) // skip empty lines
  516. ;
  517. loadUnitAnimInfo(h3Data[dd]["graphics"], parser);
  518. parser.endLine();
  519. }
  520. }
  521. void CCreatureHandler::loadUnitAnimInfo(JsonNode & graphics, CLegacyConfigParser & parser)
  522. {
  523. graphics["timeBetweenFidgets"].Float() = parser.readNumber();
  524. JsonNode & animationTime = graphics["animationTime"];
  525. animationTime["walk"].Float() = parser.readNumber();
  526. animationTime["attack"].Float() = parser.readNumber();
  527. animationTime["flight"].Float() = parser.readNumber();
  528. animationTime["idle"].Float() = 10.0;
  529. JsonNode & missile = graphics["missile"];
  530. JsonNode & offsets = missile["offset"];
  531. offsets["upperX"].Float() = parser.readNumber();
  532. offsets["upperY"].Float() = parser.readNumber();
  533. offsets["middleX"].Float() = parser.readNumber();
  534. offsets["middleY"].Float() = parser.readNumber();
  535. offsets["lowerX"].Float() = parser.readNumber();
  536. offsets["lowerY"].Float() = parser.readNumber();
  537. for(int i=0; i<12; i++)
  538. {
  539. JsonNode entry;
  540. entry.Float() = parser.readNumber();
  541. missile["frameAngles"].Vector().push_back(entry);
  542. }
  543. graphics["troopCountLocationOffset"].Float() = parser.readNumber();
  544. missile["attackClimaxFrame"].Float() = parser.readNumber();
  545. // assume that creature is not a shooter and should not have whole missile field
  546. if (missile["frameAngles"].Vector()[0].Float() == 0 &&
  547. missile["attackClimaxFrame"].Float() == 0)
  548. graphics.Struct().erase("missile");
  549. }
  550. CCreature * CCreatureHandler::loadFromJson(const JsonNode & node, const std::string & identifier)
  551. {
  552. auto cre = new CCreature();
  553. const JsonNode & name = node["name"];
  554. cre->identifier = identifier;
  555. cre->nameSing = name["singular"].String();
  556. cre->namePl = name["plural"].String();
  557. cre->cost = Res::ResourceSet(node["cost"]);
  558. cre->fightValue = static_cast<ui32>(node["fightValue"].Float());
  559. cre->AIValue = static_cast<ui32>(node["aiValue"].Float());
  560. cre->growth = static_cast<ui32>(node["growth"].Float());
  561. cre->hordeGrowth = static_cast<ui32>(node["horde"].Float()); // Needed at least until configurable buildings
  562. cre->addBonus((int)node["hitPoints"].Float(), Bonus::STACK_HEALTH);
  563. cre->addBonus((int)node["speed"].Float(), Bonus::STACKS_SPEED);
  564. cre->addBonus((int)node["attack"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  565. cre->addBonus((int)node["defense"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  566. cre->addBonus((int)node["damage"]["min"].Float(), Bonus::CREATURE_DAMAGE, 1);
  567. cre->addBonus((int)node["damage"]["max"].Float(), Bonus::CREATURE_DAMAGE, 2);
  568. assert(node["damage"]["min"].Float() <= node["damage"]["max"].Float());
  569. cre->ammMin = static_cast<ui32>(node["advMapAmount"]["min"].Float());
  570. cre->ammMax = static_cast<ui32>(node["advMapAmount"]["max"].Float());
  571. assert(cre->ammMin <= cre->ammMax);
  572. if (!node["shots"].isNull())
  573. cre->addBonus((int)node["shots"].Float(), Bonus::SHOTS);
  574. if (node["spellPoints"].isNull())
  575. cre->addBonus((int)node["spellPoints"].Float(), Bonus::CASTS);
  576. cre->doubleWide = node["doubleWide"].Bool();
  577. loadStackExperience(cre, node["stackExperience"]);
  578. loadJsonAnimation(cre, node["graphics"]);
  579. loadCreatureJson(cre, node);
  580. return cre;
  581. }
  582. void CCreatureHandler::loadJsonAnimation(CCreature * cre, const JsonNode & graphics)
  583. {
  584. cre->animation.timeBetweenFidgets = graphics["timeBetweenFidgets"].Float();
  585. cre->animation.troopCountLocationOffset = static_cast<int>(graphics["troopCountLocationOffset"].Float());
  586. const JsonNode & animationTime = graphics["animationTime"];
  587. cre->animation.walkAnimationTime = animationTime["walk"].Float();
  588. cre->animation.idleAnimationTime = animationTime["idle"].Float();
  589. cre->animation.attackAnimationTime = animationTime["attack"].Float();
  590. cre->animation.flightAnimationDistance = animationTime["flight"].Float(); //?
  591. const JsonNode & missile = graphics["missile"];
  592. const JsonNode & offsets = missile["offset"];
  593. cre->animation.upperRightMissleOffsetX = static_cast<int>(offsets["upperX"].Float());
  594. cre->animation.upperRightMissleOffsetY = static_cast<int>(offsets["upperY"].Float());
  595. cre->animation.rightMissleOffsetX = static_cast<int>(offsets["middleX"].Float());
  596. cre->animation.rightMissleOffsetY = static_cast<int>(offsets["middleY"].Float());
  597. cre->animation.lowerRightMissleOffsetX = static_cast<int>(offsets["lowerX"].Float());
  598. cre->animation.lowerRightMissleOffsetY = static_cast<int>(offsets["lowerY"].Float());
  599. cre->animation.attackClimaxFrame = static_cast<int>(missile["attackClimaxFrame"].Float());
  600. cre->animation.missleFrameAngles = missile["frameAngles"].convertTo<std::vector<double> >();
  601. cre->advMapDef = graphics["map"].String();
  602. cre->smallIconName = graphics["iconSmall"].String();
  603. cre->largeIconName = graphics["iconLarge"].String();
  604. }
  605. void CCreatureHandler::loadCreatureJson(CCreature * creature, const JsonNode & config)
  606. {
  607. creature->level = static_cast<ui8>(config["level"].Float());
  608. creature->animDefName = config["graphics"]["animation"].String();
  609. //FIXME: MOD COMPATIBILITY
  610. if (config["abilities"].getType() == JsonNode::JsonType::DATA_STRUCT)
  611. {
  612. for(auto &ability : config["abilities"].Struct())
  613. {
  614. if (!ability.second.isNull())
  615. {
  616. auto b = JsonUtils::parseBonus(ability.second);
  617. b->source = Bonus::CREATURE_ABILITY;
  618. b->duration = Bonus::PERMANENT;
  619. creature->addNewBonus(b);
  620. }
  621. }
  622. }
  623. else
  624. {
  625. for(const JsonNode &ability : config["abilities"].Vector())
  626. {
  627. if (ability.getType() == JsonNode::JsonType::DATA_VECTOR)
  628. {
  629. assert(0); // should be unused now
  630. AddAbility(creature, ability.Vector()); // used only for H3 creatures
  631. }
  632. else
  633. {
  634. auto b = JsonUtils::parseBonus(ability);
  635. b->source = Bonus::CREATURE_ABILITY;
  636. b->duration = Bonus::PERMANENT;
  637. creature->addNewBonus(b);
  638. }
  639. }
  640. }
  641. VLC->modh->identifiers.requestIdentifier("faction", config["faction"], [=](si32 faction)
  642. {
  643. creature->faction = faction;
  644. });
  645. for(const JsonNode &value : config["upgrades"].Vector())
  646. {
  647. VLC->modh->identifiers.requestIdentifier("creature", value, [=](si32 identifier)
  648. {
  649. creature->upgrades.insert(CreatureID(identifier));
  650. });
  651. }
  652. creature->animation.projectileImageName = config["graphics"]["missile"]["projectile"].String();
  653. creature->special = config["special"].Bool() || config["disabled"].Bool();
  654. const JsonNode & sounds = config["sound"];
  655. #define GET_SOUND_VALUE(value_name) creature->sounds.value_name = sounds[#value_name].String()
  656. GET_SOUND_VALUE(attack);
  657. GET_SOUND_VALUE(defend);
  658. GET_SOUND_VALUE(killed);
  659. GET_SOUND_VALUE(move);
  660. GET_SOUND_VALUE(shoot);
  661. GET_SOUND_VALUE(wince);
  662. GET_SOUND_VALUE(startMoving);
  663. GET_SOUND_VALUE(endMoving);
  664. #undef GET_SOUND_VALUE
  665. }
  666. void CCreatureHandler::loadStackExperience(CCreature * creature, const JsonNode & input)
  667. {
  668. for (const JsonNode &exp : input.Vector())
  669. {
  670. auto bonus = JsonUtils::parseBonus (exp["bonus"]);
  671. bonus->source = Bonus::STACK_EXPERIENCE;
  672. bonus->duration = Bonus::PERMANENT;
  673. const JsonVector &values = exp["values"].Vector();
  674. int lowerLimit = 1;//, upperLimit = 255;
  675. if (values[0].getType() == JsonNode::JsonType::DATA_BOOL)
  676. {
  677. for (const JsonNode &val : values)
  678. {
  679. if (val.Bool() == true)
  680. {
  681. bonus->limiter = std::make_shared<RankRangeLimiter>(RankRangeLimiter(lowerLimit));
  682. creature->addNewBonus (std::make_shared<Bonus>(*bonus)); //bonuses must be unique objects
  683. break; //TODO: allow bonuses to turn off?
  684. }
  685. ++lowerLimit;
  686. }
  687. }
  688. else
  689. {
  690. int lastVal = 0;
  691. for (const JsonNode &val : values)
  692. {
  693. if (val.Float() != lastVal)
  694. {
  695. bonus->val = (int)val.Float() - lastVal;
  696. bonus->limiter.reset (new RankRangeLimiter(lowerLimit));
  697. creature->addNewBonus (std::make_shared<Bonus>(*bonus));
  698. }
  699. lastVal = static_cast<int>(val.Float());
  700. ++lowerLimit;
  701. }
  702. }
  703. }
  704. }
  705. void CCreatureHandler::loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser & parser) //help function for parsing CREXPBON.txt
  706. {
  707. bool enable = false; //some bonuses are activated with values 2 or 1
  708. std::string buf = parser.readString();
  709. std::string mod = parser.readString();
  710. switch (buf[0])
  711. {
  712. case 'H':
  713. b.type = Bonus::STACK_HEALTH;
  714. b.valType = Bonus::PERCENT_TO_BASE;
  715. break;
  716. case 'A':
  717. b.type = Bonus::PRIMARY_SKILL;
  718. b.subtype = PrimarySkill::ATTACK;
  719. break;
  720. case 'D':
  721. b.type = Bonus::PRIMARY_SKILL;
  722. b.subtype = PrimarySkill::DEFENSE;
  723. break;
  724. case 'M': //Max damage
  725. b.type = Bonus::CREATURE_DAMAGE;
  726. b.subtype = 2;
  727. break;
  728. case 'm': //Min damage
  729. b.type = Bonus::CREATURE_DAMAGE;
  730. b.subtype = 1;
  731. break;
  732. case 'S':
  733. b.type = Bonus::STACKS_SPEED; break;
  734. case 'O':
  735. b.type = Bonus::SHOTS; break;
  736. case 'b':
  737. b.type = Bonus::ENEMY_DEFENCE_REDUCTION; break;
  738. case 'C':
  739. b.type = Bonus::CHANGES_SPELL_COST_FOR_ALLY; break;
  740. case 'd':
  741. b.type = Bonus::DEFENSIVE_STANCE; break;
  742. case 'e':
  743. b.type = Bonus::DOUBLE_DAMAGE_CHANCE;
  744. b.subtype = 0;
  745. break;
  746. case 'E':
  747. b.type = Bonus::DEATH_STARE;
  748. b.subtype = 0; //Gorgon
  749. break;
  750. case 'F':
  751. b.type = Bonus::FEAR; break;
  752. case 'g':
  753. b.type = Bonus::SPELL_DAMAGE_REDUCTION;
  754. b.subtype = -1; //all magic schools
  755. break;
  756. case 'P':
  757. b.type = Bonus::CASTS; break;
  758. case 'R':
  759. b.type = Bonus::ADDITIONAL_RETALIATION; break;
  760. case 'W':
  761. b.type = Bonus::MAGIC_RESISTANCE;
  762. b.subtype = 0; //otherwise creature window goes crazy
  763. break;
  764. case 'f': //on-off skill
  765. enable = true; //sometimes format is: 2 -> 0, 1 -> 1
  766. switch (mod[0])
  767. {
  768. case 'A':
  769. b.type = Bonus::ATTACKS_ALL_ADJACENT; break;
  770. case 'b':
  771. b.type = Bonus::RETURN_AFTER_STRIKE; break;
  772. case 'B':
  773. b.type = Bonus::TWO_HEX_ATTACK_BREATH; break;
  774. case 'c':
  775. b.type = Bonus::JOUSTING; break;
  776. case 'D':
  777. b.type = Bonus::ADDITIONAL_ATTACK; break;
  778. case 'f':
  779. b.type = Bonus::FEARLESS; break;
  780. case 'F':
  781. b.type = Bonus::FLYING; break;
  782. case 'm':
  783. b.type = Bonus::SELF_MORALE; break;
  784. case 'M':
  785. b.type = Bonus::NO_MORALE; break;
  786. case 'p': //Mind spells
  787. case 'P':
  788. b.type = Bonus::MIND_IMMUNITY; break;
  789. case 'r':
  790. b.type = Bonus::REBIRTH; //on/off? makes sense?
  791. b.subtype = 0;
  792. b.val = 20; //arbitrary value
  793. break;
  794. case 'R':
  795. b.type = Bonus::BLOCKS_RETALIATION; break;
  796. case 's':
  797. b.type = Bonus::FREE_SHOOTING; break;
  798. case 'u':
  799. b.type = Bonus::SPELL_RESISTANCE_AURA; break;
  800. case 'U':
  801. b.type = Bonus::UNDEAD; break;
  802. default:
  803. logGlobal->trace("Not parsed bonus %s %s", buf, mod);
  804. return;
  805. break;
  806. }
  807. break;
  808. case 'w': //specific spell immunities, enabled/disabled
  809. enable = true;
  810. switch (mod[0])
  811. {
  812. case 'B': //Blind
  813. b.type = Bonus::SPELL_IMMUNITY;
  814. b.subtype = SpellID::BLIND;
  815. b.additionalInfo = 0;//normal immunity
  816. break;
  817. case 'H': //Hypnotize
  818. b.type = Bonus::SPELL_IMMUNITY;
  819. b.subtype = SpellID::HYPNOTIZE;
  820. b.additionalInfo = 0;//normal immunity
  821. break;
  822. case 'I': //Implosion
  823. b.type = Bonus::SPELL_IMMUNITY;
  824. b.subtype = SpellID::IMPLOSION;
  825. b.additionalInfo = 0;//normal immunity
  826. break;
  827. case 'K': //Berserk
  828. b.type = Bonus::SPELL_IMMUNITY;
  829. b.subtype = SpellID::BERSERK;
  830. b.additionalInfo = 0;//normal immunity
  831. break;
  832. case 'M': //Meteor Shower
  833. b.type = Bonus::SPELL_IMMUNITY;
  834. b.subtype = SpellID::METEOR_SHOWER;
  835. b.additionalInfo = 0;//normal immunity
  836. break;
  837. case 'N': //dispell beneficial spells
  838. b.type = Bonus::SPELL_IMMUNITY;
  839. b.subtype = SpellID::DISPEL_HELPFUL_SPELLS;
  840. b.additionalInfo = 0;//normal immunity
  841. break;
  842. case 'R': //Armageddon
  843. b.type = Bonus::SPELL_IMMUNITY;
  844. b.subtype = SpellID::ARMAGEDDON;
  845. b.additionalInfo = 0;//normal immunity
  846. break;
  847. case 'S': //Slow
  848. b.type = Bonus::SPELL_IMMUNITY;
  849. b.subtype = SpellID::SLOW;
  850. b.additionalInfo = 0;//normal immunity
  851. break;
  852. case '6':
  853. case '7':
  854. case '8':
  855. case '9':
  856. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  857. b.val = std::atoi(mod.c_str()) - 5;
  858. break;
  859. case ':':
  860. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  861. b.val = GameConstants::SPELL_LEVELS; //in case someone adds higher level spells?
  862. break;
  863. case 'F':
  864. b.type = Bonus::FIRE_IMMUNITY;
  865. b.subtype = 1; //not positive
  866. break;
  867. case 'O':
  868. b.type = Bonus::FIRE_IMMUNITY;
  869. b.subtype = 2; //only direct damage
  870. break;
  871. case 'f':
  872. b.type = Bonus::FIRE_IMMUNITY;
  873. b.subtype = 0; //all
  874. break;
  875. case 'C':
  876. b.type = Bonus::WATER_IMMUNITY;
  877. b.subtype = 1; //not positive
  878. break;
  879. case 'W':
  880. b.type = Bonus::WATER_IMMUNITY;
  881. b.subtype = 2; //only direct damage
  882. break;
  883. case 'w':
  884. b.type = Bonus::WATER_IMMUNITY;
  885. b.subtype = 0; //all
  886. break;
  887. case 'E':
  888. b.type = Bonus::EARTH_IMMUNITY;
  889. b.subtype = 2; //only direct damage
  890. break;
  891. case 'e':
  892. b.type = Bonus::EARTH_IMMUNITY;
  893. b.subtype = 0; //all
  894. break;
  895. case 'A':
  896. b.type = Bonus::AIR_IMMUNITY;
  897. b.subtype = 2; //only direct damage
  898. break;
  899. case 'a':
  900. b.type = Bonus::AIR_IMMUNITY;
  901. b.subtype = 0; //all
  902. break;
  903. case 'D':
  904. b.type = Bonus::DIRECT_DAMAGE_IMMUNITY;
  905. break;
  906. case '0':
  907. b.type = Bonus::RECEPTIVE;
  908. break;
  909. case 'm':
  910. b.type = Bonus::MIND_IMMUNITY;
  911. break;
  912. default:
  913. logGlobal->trace("Not parsed bonus %s %s", buf, mod);
  914. return;
  915. }
  916. break;
  917. case 'i':
  918. enable = true;
  919. b.type = Bonus::NO_DISTANCE_PENALTY;
  920. break;
  921. case 'o':
  922. enable = true;
  923. b.type = Bonus::NO_WALL_PENALTY;
  924. break;
  925. case 'a':
  926. case 'c':
  927. case 'K':
  928. case 'k':
  929. b.type = Bonus::SPELL_AFTER_ATTACK;
  930. b.subtype = stringToNumber(mod);
  931. break;
  932. case 'h':
  933. b.type= Bonus::HATE;
  934. b.subtype = stringToNumber(mod);
  935. break;
  936. case 'p':
  937. case 'J':
  938. b.type = Bonus::SPELL_BEFORE_ATTACK;
  939. b.subtype = stringToNumber(mod);
  940. b.additionalInfo = 3; //always expert?
  941. break;
  942. case 'r':
  943. b.type = Bonus::HP_REGENERATION;
  944. b.val = stringToNumber(mod);
  945. break;
  946. case 's':
  947. b.type = Bonus::ENCHANTED;
  948. b.subtype = stringToNumber(mod);
  949. b.valType = Bonus::INDEPENDENT_MAX;
  950. break;
  951. default:
  952. logGlobal->trace("Not parsed bonus %s %s", buf, mod);
  953. return;
  954. break;
  955. }
  956. switch (mod[0])
  957. {
  958. case '+':
  959. case '=': //should we allow percent values to stack or pick highest?
  960. b.valType = Bonus::ADDITIVE_VALUE;
  961. break;
  962. }
  963. //limiters, range
  964. si32 lastVal, curVal, lastLev = 0;
  965. if (enable) //0 and 2 means non-active, 1 - active
  966. {
  967. if (b.type != Bonus::REBIRTH)
  968. b.val = 0; //on-off ability, no value specified
  969. curVal = static_cast<si32>(parser.readNumber());// 0 level is never active
  970. for (int i = 1; i < 11; ++i)
  971. {
  972. curVal = static_cast<si32>(parser.readNumber());
  973. if (curVal == 1)
  974. {
  975. b.limiter.reset (new RankRangeLimiter(i));
  976. bl.push_back(std::make_shared<Bonus>(b));
  977. break; //never turned off it seems
  978. }
  979. }
  980. }
  981. else
  982. {
  983. lastVal = static_cast<si32>(parser.readNumber());
  984. if (b.type == Bonus::HATE)
  985. lastVal *= 10; //odd fix
  986. //FIXME: value for zero level should be stored in our config files (independent of stack exp)
  987. for (int i = 1; i < 11; ++i)
  988. {
  989. curVal = static_cast<si32>(parser.readNumber());
  990. if (b.type == Bonus::HATE)
  991. curVal *= 10; //odd fix
  992. if (curVal > lastVal) //threshold, add new bonus
  993. {
  994. b.val = curVal - lastVal;
  995. lastVal = curVal;
  996. b.limiter.reset (new RankRangeLimiter(i));
  997. bl.push_back(std::make_shared<Bonus>(b));
  998. lastLev = i; //start new range from here, i = previous rank
  999. }
  1000. else if (curVal < lastVal)
  1001. {
  1002. b.val = lastVal;
  1003. b.limiter.reset (new RankRangeLimiter(lastLev, i));
  1004. }
  1005. }
  1006. }
  1007. }
  1008. int CCreatureHandler::stringToNumber(std::string & s)
  1009. {
  1010. boost::algorithm::replace_first(s,"#",""); //drop hash character
  1011. return std::atoi(s.c_str());
  1012. }
  1013. CCreatureHandler::~CCreatureHandler()
  1014. {
  1015. for(auto & creature : creatures)
  1016. creature.dellNull();
  1017. for(auto & p : skillRequirements)
  1018. p.first = nullptr;
  1019. }
  1020. CreatureID CCreatureHandler::pickRandomMonster(CRandomGenerator & rand, int tier) const
  1021. {
  1022. int r = 0;
  1023. if(tier == -1) //pick any allowed creature
  1024. {
  1025. do
  1026. {
  1027. r = (*RandomGeneratorUtil::nextItem(creatures, rand))->idNumber;
  1028. } while (VLC->creh->creatures[r] && VLC->creh->creatures[r]->special); // find first "not special" creature
  1029. }
  1030. else
  1031. {
  1032. assert(vstd::iswithin(tier, 1, 7));
  1033. std::vector<CreatureID> allowed;
  1034. for(const CBonusSystemNode *b : creaturesOfLevel[tier].getChildrenNodes())
  1035. {
  1036. assert(b->getNodeType() == CBonusSystemNode::CREATURE);
  1037. const CCreature * crea = dynamic_cast<const CCreature*>(b);
  1038. if(crea && !crea->special)
  1039. allowed.push_back(crea->idNumber);
  1040. }
  1041. if(!allowed.size())
  1042. {
  1043. logGlobal->warn("Cannot pick a random creature of tier %d!", tier);
  1044. return CreatureID::NONE;
  1045. }
  1046. return *RandomGeneratorUtil::nextItem(allowed, rand);
  1047. }
  1048. assert (r >= 0); //should always be, but it crashed
  1049. return CreatureID(r);
  1050. }
  1051. void CCreatureHandler::addBonusForTier(int tier, std::shared_ptr<Bonus> b)
  1052. {
  1053. assert(vstd::iswithin(tier, 1, 7));
  1054. creaturesOfLevel[tier].addNewBonus(b);
  1055. }
  1056. void CCreatureHandler::addBonusForAllCreatures(std::shared_ptr<Bonus> b)
  1057. {
  1058. allCreatures.addNewBonus(b);
  1059. }
  1060. void CCreatureHandler::removeBonusesFromAllCreatures()
  1061. {
  1062. allCreatures.removeBonuses(Selector::all);
  1063. }
  1064. void CCreatureHandler::buildBonusTreeForTiers()
  1065. {
  1066. for(CCreature *c : creatures)
  1067. {
  1068. if(vstd::isbetween(c->level, 0, ARRAY_COUNT(creaturesOfLevel)))
  1069. c->attachTo(&creaturesOfLevel[c->level]);
  1070. else
  1071. c->attachTo(&creaturesOfLevel[0]);
  1072. }
  1073. for(CBonusSystemNode &b : creaturesOfLevel)
  1074. b.attachTo(&allCreatures);
  1075. }
  1076. void CCreatureHandler::afterLoadFinalization()
  1077. {
  1078. }
  1079. void CCreatureHandler::deserializationFix()
  1080. {
  1081. buildBonusTreeForTiers();
  1082. }