CCreatureHandler.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. unit.animation.missleFrameAngles.push_back(parser.readNumber());
  414. unit.animation.troopCountLocationOffset= parser.readNumber();
  415. unit.animation.attackClimaxFrame = parser.readNumber();
  416. parser.endLine();
  417. }
  418. void CCreatureHandler::load(std::string creatureID, const JsonNode & node)
  419. {
  420. CCreature * creature = loadCreature(node);
  421. creature->nameRef = creatureID;
  422. creature->idNumber = CreatureID(creatures.size());
  423. creatures.push_back(creature);
  424. tlog5 << "Added creature: " << creatureID << "\n";
  425. registerCreature(creature->nameRef, creature->idNumber);
  426. }
  427. CCreature * CCreatureHandler::loadCreature(const JsonNode & node)
  428. {
  429. CCreature * cre = new CCreature();
  430. const JsonNode & name = node["name"];
  431. cre->nameSing = name["singular"].String();
  432. cre->namePl = name["plural"].String();
  433. cre->cost = Res::ResourceSet(node["cost"]);
  434. cre->fightValue = node["fightValue"].Float();
  435. cre->AIValue = node["aiValue"].Float();
  436. cre->growth = node["growth"].Float();
  437. cre->hordeGrowth = node["horde"].Float(); // Needed at least until configurable buildings
  438. cre->addBonus(node["hitPoints"].Float(), Bonus::STACK_HEALTH);
  439. cre->addBonus(node["speed"].Float(), Bonus::STACKS_SPEED);
  440. cre->addBonus(node["attack"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  441. cre->addBonus(node["defense"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  442. const JsonNode & vec = node["damage"];
  443. cre->addBonus(vec["min"].Float(), Bonus::CREATURE_DAMAGE, 1);
  444. cre->addBonus(vec["max"].Float(), Bonus::CREATURE_DAMAGE, 2);
  445. auto & amounts = node ["advMapAmount"];
  446. cre->ammMin = amounts["min"].Float();
  447. cre->ammMax = amounts["max"].Float();
  448. if (!node["shots"].isNull())
  449. cre->addBonus(node["shots"].Float(), Bonus::SHOTS);
  450. if (node["spellPoints"].isNull())
  451. cre->addBonus(node["spellPoints"].Float(), Bonus::CASTS);
  452. cre->doubleWide = node["doubleWide"].Bool();
  453. //graphics
  454. loadStackExperience(cre, node["stackExperience"]);
  455. const JsonNode & graphics = node["graphics"];
  456. cre->animation.timeBetweenFidgets = graphics["timeBetweenFidgets"].Float();
  457. cre->animation.troopCountLocationOffset = graphics["troopCountLocationOffset"].Float();
  458. cre->animation.attackClimaxFrame = graphics["attackClimaxFrame"].Float();
  459. const JsonNode & animationTime = graphics["animationTime"];
  460. cre->animation.walkAnimationTime = animationTime["walk"].Float();
  461. cre->animation.attackAnimationTime = animationTime["attack"].Float();
  462. cre->animation.flightAnimationDistance = animationTime["flight"].Float(); //?
  463. const JsonNode & missile = graphics["missile"];
  464. const JsonNode & offsets = missile["offset"];
  465. cre->animation.upperRightMissleOffsetX = offsets["upperX"].Float();
  466. cre->animation.upperRightMissleOffsetY = offsets["upperY"].Float();
  467. cre->animation.rightMissleOffsetX = offsets["middleX"].Float();
  468. cre->animation.rightMissleOffsetY = offsets["middleY"].Float();
  469. cre->animation.lowerRightMissleOffsetX = offsets["lowerX"].Float();
  470. cre->animation.lowerRightMissleOffsetY = offsets["lowerY"].Float();
  471. cre->animation.missleFrameAngles = missile["frameAngles"].convertTo<std::vector<double>>();
  472. cre->advMapDef = graphics["map"].String();
  473. cre->iconIndex = graphics["iconIndex"].Float();
  474. loadCreatureJson(cre, node);
  475. return cre;
  476. }
  477. void CCreatureHandler::loadCreatureJson(CCreature * creature, const JsonNode & config)
  478. {
  479. creature->level = config["level"].Float();
  480. creature->animDefName = config["graphics"]["animation"].String();
  481. BOOST_FOREACH(const JsonNode &ability, config["ability_remove"].Vector())
  482. {
  483. RemoveAbility(creature, ability);
  484. }
  485. BOOST_FOREACH(const JsonNode &ability, config["abilities"].Vector())
  486. {
  487. if (ability.getType() == JsonNode::DATA_VECTOR)
  488. AddAbility(creature, ability.Vector()); // used only for H3 creatures
  489. else
  490. {
  491. auto b = JsonUtils::parseBonus(ability);
  492. b->source = Bonus::CREATURE_ABILITY;
  493. b->duration = Bonus::PERMANENT;
  494. creature->addNewBonus(b);
  495. }
  496. }
  497. VLC->modh->identifiers.requestIdentifier(std::string("faction.") + config["faction"].String(), [=](si32 faction)
  498. {
  499. creature->faction = faction;
  500. });
  501. BOOST_FOREACH(const JsonNode &value, config["upgrades"].Vector())
  502. {
  503. VLC->modh->identifiers.requestIdentifier(std::string("creature.") + value.String(), [=](si32 identifier)
  504. {
  505. creature->upgrades.insert(CreatureID(identifier));
  506. });
  507. }
  508. if(config["hasDoubleWeek"].Bool())
  509. doubledCreatures.insert(creature->idNumber);
  510. creature->animation.projectileImageName = config["graphics"]["missile"]["projectile"].String();
  511. //creature->animation.projectileSpin = config["graphics"]["missile"]["spinning"].Bool();
  512. creature->special = config["special"].Bool();
  513. const JsonNode & sounds = config["sound"];
  514. #define GET_SOUND_VALUE(value_name) creature->sounds.value_name = sounds[#value_name].String()
  515. GET_SOUND_VALUE(attack);
  516. GET_SOUND_VALUE(defend);
  517. GET_SOUND_VALUE(killed);
  518. GET_SOUND_VALUE(move);
  519. GET_SOUND_VALUE(shoot);
  520. GET_SOUND_VALUE(wince);
  521. GET_SOUND_VALUE(ext1);
  522. GET_SOUND_VALUE(ext2);
  523. GET_SOUND_VALUE(startMoving);
  524. GET_SOUND_VALUE(endMoving);
  525. #undef GET_SOUND_VALUE
  526. }
  527. void CCreatureHandler::loadStackExperience(CCreature * creature, const JsonNode & input)
  528. {
  529. BOOST_FOREACH (const JsonNode &exp, input.Vector())
  530. {
  531. auto bonus = JsonUtils::parseBonus (exp["bonus"]);
  532. bonus->source = Bonus::STACK_EXPERIENCE;
  533. bonus->duration = Bonus::PERMANENT;
  534. const JsonVector &values = exp["values"].Vector();
  535. int lowerLimit = 1;//, upperLimit = 255;
  536. if (values[0].getType() == JsonNode::JsonType::DATA_BOOL)
  537. {
  538. BOOST_FOREACH (const JsonNode &val, values)
  539. {
  540. if (val.Bool() == true)
  541. {
  542. bonus->limiter = make_shared<RankRangeLimiter>(RankRangeLimiter(lowerLimit));
  543. creature->addNewBonus (new Bonus(*bonus)); //bonuses must be unique objects
  544. break; //TODO: allow bonuses to turn off?
  545. }
  546. ++lowerLimit;
  547. }
  548. }
  549. else
  550. {
  551. int lastVal = 0;
  552. BOOST_FOREACH (const JsonNode &val, values)
  553. {
  554. if (val.Float() != lastVal)
  555. {
  556. bonus->val = val.Float() - lastVal;
  557. bonus->limiter.reset (new RankRangeLimiter(lowerLimit));
  558. creature->addNewBonus (new Bonus(*bonus));
  559. }
  560. lastVal = val.Float();
  561. ++lowerLimit;
  562. }
  563. }
  564. delete bonus;
  565. }
  566. }
  567. void CCreatureHandler::loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser & parser) //help function for parsing CREXPBON.txt
  568. {
  569. bool enable = false; //some bonuses are activated with values 2 or 1
  570. std::string buf = parser.readString();
  571. std::string mod = parser.readString();
  572. switch (buf[0])
  573. {
  574. case 'H':
  575. b.type = Bonus::STACK_HEALTH;
  576. b.valType = Bonus::PERCENT_TO_BASE;
  577. break;
  578. case 'A':
  579. b.type = Bonus::PRIMARY_SKILL;
  580. b.subtype = PrimarySkill::ATTACK;
  581. break;
  582. case 'D':
  583. b.type = Bonus::PRIMARY_SKILL;
  584. b.subtype = PrimarySkill::DEFENSE;
  585. break;
  586. case 'M': //Max damage
  587. b.type = Bonus::CREATURE_DAMAGE;
  588. b.subtype = 2;
  589. break;
  590. case 'm': //Min damage
  591. b.type = Bonus::CREATURE_DAMAGE;
  592. b.subtype = 1;
  593. break;
  594. case 'S':
  595. b.type = Bonus::STACKS_SPEED; break;
  596. case 'O':
  597. b.type = Bonus::SHOTS; break;
  598. case 'b':
  599. b.type = Bonus::ENEMY_DEFENCE_REDUCTION; break;
  600. case 'C':
  601. b.type = Bonus::CHANGES_SPELL_COST_FOR_ALLY; break;
  602. case 'd':
  603. b.type = Bonus::DEFENSIVE_STANCE; break;
  604. case 'e':
  605. b.type = Bonus::DOUBLE_DAMAGE_CHANCE;
  606. b.subtype = 0;
  607. break;
  608. case 'E':
  609. b.type = Bonus::DEATH_STARE;
  610. b.subtype = 0; //Gorgon
  611. break;
  612. case 'F':
  613. b.type = Bonus::FEAR; 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 = SpellID::BLIND;
  677. break;
  678. case 'H': //Hypnotize
  679. b.type = Bonus::SPELL_IMMUNITY;
  680. b.subtype = SpellID::HYPNOTIZE;
  681. break;
  682. case 'I': //Implosion
  683. b.type = Bonus::SPELL_IMMUNITY;
  684. b.subtype = SpellID::IMPLOSION;
  685. break;
  686. case 'K': //Berserk
  687. b.type = Bonus::SPELL_IMMUNITY;
  688. b.subtype = SpellID::BERSERK;
  689. break;
  690. case 'M': //Meteor Shower
  691. b.type = Bonus::SPELL_IMMUNITY;
  692. b.subtype = SpellID::METEOR_SHOWER;
  693. break;
  694. case 'N': //dispell beneficial spells
  695. b.type = Bonus::SPELL_IMMUNITY;
  696. b.subtype = SpellID::DISPEL_HELPFUL_SPELLS;
  697. break;
  698. case 'R': //Armageddon
  699. b.type = Bonus::SPELL_IMMUNITY;
  700. b.subtype = SpellID::ARMAGEDDON;
  701. break;
  702. case 'S': //Slow
  703. b.type = Bonus::SPELL_IMMUNITY;
  704. b.subtype = SpellID::SLOW;
  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. case 'm':
  764. b.type = Bonus::MIND_IMMUNITY;
  765. break;
  766. default:
  767. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  768. return;
  769. }
  770. break;
  771. case 'i':
  772. enable = true;
  773. b.type = Bonus::NO_DISTANCE_PENALTY;
  774. break;
  775. case 'o':
  776. enable = true;
  777. b.type = Bonus::NO_WALL_PENALTY;
  778. break;
  779. case 'a':
  780. case 'c':
  781. case 'K':
  782. case 'k':
  783. b.type = Bonus::SPELL_AFTER_ATTACK;
  784. b.subtype = stringToNumber(mod);
  785. break;
  786. case 'h':
  787. b.type= Bonus::HATE;
  788. b.subtype = stringToNumber(mod);
  789. break;
  790. case 'p':
  791. case 'J':
  792. b.type = Bonus::SPELL_BEFORE_ATTACK;
  793. b.subtype = stringToNumber(mod);
  794. b.additionalInfo = 3; //always expert?
  795. break;
  796. case 'r':
  797. b.type = Bonus::HP_REGENERATION;
  798. b.val = stringToNumber(mod);
  799. break;
  800. case 's':
  801. b.type = Bonus::ENCHANTED;
  802. b.subtype = stringToNumber(mod);
  803. b.valType = Bonus::INDEPENDENT_MAX;
  804. break;
  805. default:
  806. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  807. return;
  808. break;
  809. }
  810. switch (mod[0])
  811. {
  812. case '+':
  813. case '=': //should we allow percent values to stack or pick highest?
  814. b.valType = Bonus::ADDITIVE_VALUE;
  815. break;
  816. }
  817. //limiters, range
  818. si32 lastVal, curVal, lastLev = 0;
  819. if (enable) //0 and 2 means non-active, 1 - active
  820. {
  821. if (b.type != Bonus::REBIRTH)
  822. b.val = 0; //on-off ability, no value specified
  823. curVal = parser.readNumber();// 0 level is never active
  824. for (int i = 1; i < 11; ++i)
  825. {
  826. curVal = parser.readNumber();
  827. if (curVal == 1)
  828. {
  829. b.limiter.reset (new RankRangeLimiter(i));
  830. bl.push_back(new Bonus(b));
  831. break; //never turned off it seems
  832. }
  833. }
  834. }
  835. else
  836. {
  837. lastVal = parser.readNumber(); //basic value, not particularly useful but existent
  838. for (int i = 1; i < 11; ++i)
  839. {
  840. curVal = parser.readNumber();
  841. if (b.type == Bonus::HATE)
  842. curVal *= 10; //odd fix
  843. if (curVal > lastVal) //threshold, add new bonus
  844. {
  845. b.val = curVal - lastVal;
  846. lastVal = curVal;
  847. b.limiter.reset (new RankRangeLimiter(i));
  848. bl.push_back(new Bonus(b));
  849. lastLev = i; //start new range from here, i = previous rank
  850. }
  851. else if (curVal < lastVal)
  852. {
  853. b.val = lastVal;
  854. b.limiter.reset (new RankRangeLimiter(lastLev, i));
  855. }
  856. }
  857. }
  858. }
  859. int CCreatureHandler::stringToNumber(std::string & s)
  860. {
  861. boost::algorithm::replace_first(s,"#",""); //drop hash character
  862. return std::atoi(s.c_str());
  863. }
  864. CCreatureHandler::~CCreatureHandler()
  865. {
  866. }
  867. CreatureID CCreatureHandler::pickRandomMonster(const boost::function<int()> &randGen, int tier) const
  868. {
  869. int r = 0;
  870. if(tier == -1) //pick any allowed creature
  871. {
  872. do
  873. {
  874. r = vstd::pickRandomElementOf(creatures, randGen)->idNumber;
  875. } while (VLC->creh->creatures[r] && VLC->creh->creatures[r]->special); // find first "not special" creature
  876. }
  877. else
  878. {
  879. assert(vstd::iswithin(tier, 1, 7));
  880. std::vector<CreatureID> allowed;
  881. BOOST_FOREACH(const CBonusSystemNode *b, creaturesOfLevel[tier].getChildrenNodes())
  882. {
  883. assert(b->getNodeType() == CBonusSystemNode::CREATURE);
  884. const CCreature * crea = dynamic_cast<const CCreature*>(b);
  885. if(crea && !crea->special)
  886. allowed.push_back(crea->idNumber);
  887. }
  888. if(!allowed.size())
  889. {
  890. tlog2 << "Cannot pick a random creature of tier " << tier << "!\n";
  891. return CreatureID::NONE;
  892. }
  893. return vstd::pickRandomElementOf(allowed, randGen);
  894. }
  895. return CreatureID(r);
  896. }
  897. void CCreatureHandler::addBonusForTier(int tier, Bonus *b)
  898. {
  899. assert(vstd::iswithin(tier, 1, 7));
  900. creaturesOfLevel[tier].addNewBonus(b);
  901. }
  902. void CCreatureHandler::addBonusForAllCreatures(Bonus *b)
  903. {
  904. allCreatures.addNewBonus(b);
  905. }
  906. void CCreatureHandler::buildBonusTreeForTiers()
  907. {
  908. BOOST_FOREACH(CCreature *c, creatures)
  909. {
  910. if(vstd::isbetween(c->level, 0, ARRAY_COUNT(creaturesOfLevel)))
  911. c->attachTo(&creaturesOfLevel[c->level]);
  912. else
  913. c->attachTo(&creaturesOfLevel[0]);
  914. }
  915. BOOST_FOREACH(CBonusSystemNode &b, creaturesOfLevel)
  916. b.attachTo(&allCreatures);
  917. }
  918. void CCreatureHandler::deserializationFix()
  919. {
  920. buildBonusTreeForTiers();
  921. }