CCreatureHandler.cpp 29 KB

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