CCreatureHandler.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. std::string abilities = parser.readString();
  249. loadBonuses(ncre, parser.readString());
  250. { //adding abilities from ZCRTRAIT.TXT
  251. static const std::map < std::string,Bonus::BonusType> abilityMap = boost::assign::map_list_of
  252. ("FLYING_ARMY", Bonus::FLYING)
  253. ("SHOOTING_ARMY", Bonus::SHOOTER)
  254. ("SIEGE_WEAPON", Bonus::SIEGE_WEAPON)
  255. ("const_free_attack", Bonus::BLOCKS_RETALIATION)
  256. ("IS_UNDEAD", Bonus::UNDEAD)
  257. ("const_no_melee_penalty",Bonus::NO_MELEE_PENALTY)
  258. ("const_jousting",Bonus::JOUSTING)
  259. ("KING_1",Bonus::KING1)
  260. ("KING_2",Bonus::KING2)
  261. ("KING_3",Bonus::KING3)
  262. ("const_no_wall_penalty",Bonus::NO_WALL_PENALTY)
  263. ("CATAPULT",Bonus::CATAPULT)
  264. ("MULTI_HEADED",Bonus::ATTACKS_ALL_ADJACENT)
  265. ("IMMUNE_TO_MIND_SPELLS",Bonus::MIND_IMMUNITY)
  266. ("IMMUNE_TO_FIRE_SPELLS",Bonus::FIRE_IMMUNITY)
  267. ("IMMUNE_TO_FIRE_SPELLS",Bonus::FIRE_IMMUNITY)
  268. ("HAS_EXTENDED_ATTACK",Bonus::TWO_HEX_ATTACK_BREATH);
  269. auto hasAbility = [&](const std::string name) -> bool
  270. {
  271. return boost::algorithm::find_first(abilities, name);
  272. };
  273. BOOST_FOREACH(auto a, abilityMap)
  274. {
  275. if(hasAbility(a.first))
  276. ncre.addBonus(0, a.second);
  277. }
  278. if(hasAbility("DOUBLE_WIDE"))
  279. ncre.doubleWide = true;
  280. if(hasAbility("const_raises_morale"))
  281. {
  282. ncre.addBonus(+1, Bonus::MORALE);;
  283. ncre.getBonusList().back()->addPropagator(make_shared<CPropagatorNodeType>(CBonusSystemNode::HERO));
  284. }
  285. if(hasAbility("const_lowers_morale"))
  286. {
  287. ncre.addBonus(-1, Bonus::MORALE);;
  288. ncre.getBonusList().back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  289. }
  290. }
  291. creatures.push_back(&ncre);
  292. }
  293. while (parser.endLine());
  294. // loading creatures properties
  295. tlog5 << "\t\tReading creatures json configs" << std::endl;
  296. const JsonNode gameConf(ResourceID("config/gameConfig.json"));
  297. const JsonNode config(JsonUtils::assembleFromFiles(gameConf["creatures"].convertTo<std::vector<std::string> >()));
  298. BOOST_FOREACH(auto & node, config.Struct())
  299. {
  300. int creatureID = node.second["id"].Float();
  301. CCreature *c = creatures[creatureID];
  302. loadCreatureJson(c, node.second);
  303. // Main reference name, e.g. royalGriffin
  304. c->nameRef = node.first;
  305. registerCreature(node.first, c->idNumber);
  306. // Alternative names, if any
  307. BOOST_FOREACH(const JsonNode &name, node.second["extraNames"].Vector())
  308. {
  309. registerCreature(name.String(), c->idNumber);
  310. }
  311. }
  312. loadAnimationInfo();
  313. if (VLC->modh->modules.STACK_EXP) //reading default stack experience bonuses
  314. {
  315. CLegacyConfigParser parser("DATA/CREXPBON.TXT");
  316. Bonus b; //prototype with some default properties
  317. b.source = Bonus::STACK_EXPERIENCE;
  318. b.duration = Bonus::PERMANENT;
  319. b.valType = Bonus::ADDITIVE_VALUE;
  320. b.effectRange = Bonus::NO_LIMIT;
  321. b.additionalInfo = 0;
  322. b.turnsRemain = 0;
  323. BonusList bl;
  324. parser.endLine();
  325. parser.readString(); //ignore index
  326. loadStackExp(b, bl, parser);
  327. BOOST_FOREACH(Bonus * b, bl)
  328. addBonusForAllCreatures(b); //health bonus is common for all
  329. parser.endLine();
  330. for (int i = 1; i < 7; ++i)
  331. {
  332. for (int j = 0; j < 4; ++j) //four modifiers common for tiers
  333. {
  334. parser.readString(); //ignore index
  335. bl.clear();
  336. loadStackExp(b, bl, parser);
  337. BOOST_FOREACH(Bonus * b, bl)
  338. addBonusForTier(i, b);
  339. parser.endLine();
  340. }
  341. }
  342. for (int j = 0; j < 4; ++j) //tier 7
  343. {
  344. parser.readString(); //ignore index
  345. bl.clear();
  346. loadStackExp(b, bl, parser);
  347. BOOST_FOREACH(Bonus * b, bl)
  348. {
  349. addBonusForTier(7, b);
  350. creaturesOfLevel[0].addNewBonus(b); //bonuses from level 7 are given to high-level creatures
  351. }
  352. parser.endLine();
  353. }
  354. do //parse everything that's left
  355. {
  356. b.sid = parser.readNumber(); //id = this particular creature ID
  357. loadStackExp(b, creatures[b.sid]->getBonusList(), parser); //add directly to CCreature Node
  358. }
  359. while (parser.endLine());
  360. //Calculate rank exp values, formula appears complicated bu no parsing needed
  361. expRanks.resize(8);
  362. int dif = 0;
  363. int it = 8000; //ignore name of this variable
  364. expRanks[0].push_back(it);
  365. for (int j = 1; j < 10; ++j) //used for tiers 8-10, and all other probably
  366. {
  367. expRanks[0].push_back(expRanks[0][j-1] + it + dif);
  368. dif += it/5;
  369. }
  370. for (int i = 1; i < 8; ++i)
  371. {
  372. dif = 0;
  373. it = 1000 * i;
  374. expRanks[i].push_back(it);
  375. for (int j = 1; j < 10; ++j)
  376. {
  377. expRanks[i].push_back(expRanks[i][j-1] + it + dif);
  378. dif += it/5;
  379. }
  380. }
  381. CLegacyConfigParser expBonParser("DATA/CREXPMOD.TXT");
  382. expBonParser.endLine(); //header
  383. maxExpPerBattle.resize(8);
  384. for (int i = 1; i < 8; ++i)
  385. {
  386. expBonParser.readString(); //index
  387. expBonParser.readString(); //float multiplier -> hardcoded
  388. expBonParser.readString(); //ignore upgrade mod? ->hardcoded
  389. expBonParser.readString(); //already calculated
  390. maxExpPerBattle[i] = expBonParser.readNumber();
  391. expRanks[i].push_back(expRanks[i].back() + expBonParser.readNumber());
  392. expBonParser.endLine();
  393. }
  394. //skeleton gets exp penalty
  395. creatures[56].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
  396. creatures[57].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
  397. //exp for tier >7, rank 11
  398. expRanks[0].push_back(147000);
  399. expAfterUpgrade = 75; //percent
  400. maxExpPerBattle[0] = maxExpPerBattle[7];
  401. }//end of Stack Experience
  402. tlog5 << "\t\tReading config/commanders.json" << std::endl;
  403. const JsonNode config3(ResourceID("config/commanders.json"));
  404. BOOST_FOREACH (auto bonus, config3["bonusPerLevel"].Vector())
  405. {
  406. commanderLevelPremy.push_back(JsonUtils::parseBonus (bonus.Vector()));
  407. }
  408. int i = 0;
  409. BOOST_FOREACH (auto skill, config3["skillLevels"].Vector())
  410. {
  411. skillLevels.push_back (std::vector<ui8>());
  412. BOOST_FOREACH (auto skillLevel, skill["levels"].Vector())
  413. {
  414. skillLevels[i].push_back (skillLevel.Float());
  415. }
  416. ++i;
  417. }
  418. BOOST_FOREACH (auto ability, config3["abilityRequirements"].Vector())
  419. {
  420. std::pair <Bonus, std::pair <ui8, ui8> > a;
  421. a.first = *JsonUtils::parseBonus (ability["ability"].Vector());
  422. a.second.first = ability["skills"].Vector()[0].Float();
  423. a.second.second = ability["skills"].Vector()[1].Float();
  424. skillRequirements.push_back (a);
  425. }
  426. }
  427. void CCreatureHandler::loadAnimationInfo()
  428. {
  429. CLegacyConfigParser parser("DATA/CRANIM.TXT");
  430. parser.endLine(); // header
  431. parser.endLine();
  432. for(int dd=0; dd<creatures.size(); ++dd)
  433. {
  434. while (parser.isNextEntryEmpty() && parser.endLine()) // skip empty lines
  435. ;
  436. loadUnitAnimInfo(*creatures[dd], parser);
  437. }
  438. }
  439. void CCreatureHandler::loadUnitAnimInfo(CCreature & unit, CLegacyConfigParser & parser)
  440. {
  441. unit.animation.timeBetweenFidgets = parser.readNumber();
  442. unit.animation.walkAnimationTime = parser.readNumber();
  443. unit.animation.attackAnimationTime = parser.readNumber();
  444. unit.animation.flightAnimationDistance = parser.readNumber();
  445. ///////////////////////
  446. unit.animation.upperRightMissleOffsetX = parser.readNumber();
  447. unit.animation.upperRightMissleOffsetY = parser.readNumber();
  448. unit.animation.rightMissleOffsetX = parser.readNumber();
  449. unit.animation.rightMissleOffsetY = parser.readNumber();
  450. unit.animation.lowerRightMissleOffsetX = parser.readNumber();
  451. unit.animation.lowerRightMissleOffsetY = parser.readNumber();
  452. ///////////////////////
  453. for(int jjj=0; jjj<12; ++jjj)
  454. {
  455. unit.animation.missleFrameAngles[jjj] = parser.readNumber();
  456. }
  457. unit.animation.troopCountLocationOffset= parser.readNumber();
  458. unit.animation.attackClimaxFrame = parser.readNumber();
  459. parser.endLine();
  460. }
  461. void CCreatureHandler::load(std::string creatureID, const JsonNode & node)
  462. {
  463. CCreature * creature = loadCreature(node);
  464. creature->nameRef = creatureID;
  465. creature->idNumber = CreatureID(creatures.size());
  466. creatures.push_back(creature);
  467. tlog5 << "Added creature: " << creatureID << "\n";
  468. registerCreature(creature->nameRef, creature->idNumber);
  469. }
  470. CCreature * CCreatureHandler::loadCreature(const JsonNode & node)
  471. {
  472. CCreature * cre = new CCreature();
  473. const JsonNode & name = node["name"];
  474. cre->nameSing = name["singular"].String();
  475. cre->namePl = name["plural"].String();
  476. cre->cost = Res::ResourceSet(node["cost"]);
  477. cre->fightValue = node["fightValue"].Float();
  478. cre->AIValue = node["aiValue"].Float();
  479. cre->growth = node["growth"].Float();
  480. cre->hordeGrowth = node["horde"].Float(); // Needed at least until configurable buildings
  481. cre->addBonus(node["hitPoints"].Float(), Bonus::STACK_HEALTH);
  482. cre->addBonus(node["speed"].Float(), Bonus::STACKS_SPEED);
  483. cre->addBonus(node["attack"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  484. cre->addBonus(node["defense"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  485. const JsonNode & vec = node["damage"];
  486. cre->addBonus(vec["min"].Float(), Bonus::CREATURE_DAMAGE, 1);
  487. cre->addBonus(vec["max"].Float(), Bonus::CREATURE_DAMAGE, 2);
  488. auto & amounts = node ["advMapAmount"];
  489. cre->ammMin = amounts["min"].Float();
  490. cre->ammMax = amounts["max"].Float();
  491. if (!node["shots"].isNull())
  492. cre->addBonus(node["shots"].Float(), Bonus::SHOTS);
  493. if (node["spellPoints"].isNull())
  494. cre->addBonus(node["spellPoints"].Float(), Bonus::CASTS);
  495. cre->doubleWide = node["doubleWide"].Bool();
  496. //graphics
  497. loadStackExperience(cre, node["stackExperience"]);
  498. const JsonNode & graphics = node["graphics"];
  499. cre->animation.timeBetweenFidgets = graphics["timeBetweenFidgets"].Float();
  500. cre->animation.troopCountLocationOffset = graphics["troopCountLocationOffset"].Float();
  501. cre->animation.attackClimaxFrame = graphics["attackClimaxFrame"].Float();
  502. const JsonNode & animationTime = graphics["animationTime"];
  503. cre->animation.walkAnimationTime = animationTime["walk"].Float();
  504. cre->animation.attackAnimationTime = animationTime["attack"].Float();
  505. cre->animation.flightAnimationDistance = animationTime["flight"].Float(); //?
  506. const JsonNode & missile = graphics["missile"];
  507. const JsonNode & offsets = missile["offset"];
  508. cre->animation.upperRightMissleOffsetX = offsets["upperX"].Float();
  509. cre->animation.upperRightMissleOffsetY = offsets["upperY"].Float();
  510. cre->animation.rightMissleOffsetX = offsets["middleX"].Float();
  511. cre->animation.rightMissleOffsetY = offsets["middleY"].Float();
  512. cre->animation.lowerRightMissleOffsetX = offsets["lowerX"].Float();
  513. cre->animation.lowerRightMissleOffsetY = offsets["lowerY"].Float();
  514. int i = 0;
  515. BOOST_FOREACH (auto & angle, missile["frameAngles"].Vector())
  516. {
  517. cre->animation.missleFrameAngles[i++] = angle.Float();
  518. }
  519. cre->advMapDef = graphics["map"].String();
  520. cre->iconIndex = graphics["iconIndex"].Float();
  521. loadCreatureJson(cre, node);
  522. return cre;
  523. }
  524. void CCreatureHandler::loadCreatureJson(CCreature * creature, const JsonNode & config)
  525. {
  526. creature->level = config["level"].Float();
  527. creature->animDefName = config["graphics"]["animation"].String();
  528. BOOST_FOREACH(const JsonNode &ability, config["ability_remove"].Vector())
  529. {
  530. RemoveAbility(creature, ability);
  531. }
  532. BOOST_FOREACH(const JsonNode &ability, config["abilities"].Vector())
  533. {
  534. if (ability.getType() == JsonNode::DATA_VECTOR)
  535. AddAbility(creature, ability.Vector()); // used only for H3 creatures
  536. else
  537. {
  538. auto b = JsonUtils::parseBonus(ability);
  539. b->source = Bonus::CREATURE_ABILITY;
  540. b->duration = Bonus::PERMANENT;
  541. creature->addNewBonus(b);
  542. }
  543. }
  544. VLC->modh->identifiers.requestIdentifier(std::string("faction.") + config["faction"].String(), [=](si32 faction)
  545. {
  546. creature->faction = faction;
  547. });
  548. BOOST_FOREACH(const JsonNode &value, config["upgrades"].Vector())
  549. {
  550. VLC->modh->identifiers.requestIdentifier(std::string("creature.") + value.String(), [=](si32 identifier)
  551. {
  552. creature->upgrades.insert(CreatureID(identifier));
  553. });
  554. }
  555. if(config["hasDoubleWeek"].Bool())
  556. doubledCreatures.insert(creature->idNumber);
  557. creature->animation.projectileImageName = config["graphics"]["missile"]["projectile"].String();
  558. creature->animation.projectileSpin = config["graphics"]["missile"]["spinning"].Bool();
  559. creature->special = config["special"].Bool();
  560. const JsonNode & sounds = config["sound"];
  561. #define GET_SOUND_VALUE(value_name) creature->sounds.value_name = sounds[#value_name].String()
  562. GET_SOUND_VALUE(attack);
  563. GET_SOUND_VALUE(defend);
  564. GET_SOUND_VALUE(killed);
  565. GET_SOUND_VALUE(move);
  566. GET_SOUND_VALUE(shoot);
  567. GET_SOUND_VALUE(wince);
  568. GET_SOUND_VALUE(ext1);
  569. GET_SOUND_VALUE(ext2);
  570. GET_SOUND_VALUE(startMoving);
  571. GET_SOUND_VALUE(endMoving);
  572. #undef GET_SOUND_VALUE
  573. }
  574. void CCreatureHandler::loadStackExperience(CCreature * creature, const JsonNode & input)
  575. {
  576. BOOST_FOREACH (const JsonNode &exp, input.Vector())
  577. {
  578. auto bonus = JsonUtils::parseBonus (exp["bonus"]); // FIXME: memory leak? Only copies of bonus is added to creature
  579. bonus->source = Bonus::STACK_EXPERIENCE;
  580. bonus->duration = Bonus::PERMANENT;
  581. const JsonVector &values = exp["values"].Vector();
  582. int lowerLimit = 1;//, upperLimit = 255;
  583. if (values[0].getType() == JsonNode::JsonType::DATA_BOOL)
  584. {
  585. BOOST_FOREACH (const JsonNode &val, values)
  586. {
  587. if (val.Bool() == true)
  588. {
  589. bonus->limiter = make_shared<RankRangeLimiter>(RankRangeLimiter(lowerLimit));
  590. creature->addNewBonus (new Bonus(*bonus)); //bonuses must be unique objects
  591. break; //TODO: allow bonuses to turn off?
  592. }
  593. ++lowerLimit;
  594. }
  595. }
  596. else
  597. {
  598. int lastVal = 0;
  599. BOOST_FOREACH (const JsonNode &val, values)
  600. {
  601. if (val.Float() != lastVal)
  602. {
  603. bonus->val = val.Float() - lastVal;
  604. bonus->limiter.reset (new RankRangeLimiter(lowerLimit));
  605. creature->addNewBonus (new Bonus(*bonus));
  606. }
  607. lastVal = val.Float();
  608. ++lowerLimit;
  609. }
  610. }
  611. }
  612. }
  613. void CCreatureHandler::loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser & parser) //help function for parsing CREXPBON.txt
  614. {
  615. bool enable = false; //some bonuses are activated with values 2 or 1
  616. std::string buf = parser.readString();
  617. std::string mod = parser.readString();
  618. switch (buf[0])
  619. {
  620. case 'H':
  621. b.type = Bonus::STACK_HEALTH;
  622. b.valType = Bonus::PERCENT_TO_BASE;
  623. break;
  624. case 'A':
  625. b.type = Bonus::PRIMARY_SKILL;
  626. b.subtype = PrimarySkill::ATTACK;
  627. break;
  628. case 'D':
  629. b.type = Bonus::PRIMARY_SKILL;
  630. b.subtype = PrimarySkill::DEFENSE;
  631. break;
  632. case 'M': //Max damage
  633. b.type = Bonus::CREATURE_DAMAGE;
  634. b.subtype = 2;
  635. break;
  636. case 'm': //Min damage
  637. b.type = Bonus::CREATURE_DAMAGE;
  638. b.subtype = 1;
  639. break;
  640. case 'S':
  641. b.type = Bonus::STACKS_SPEED; break;
  642. case 'O':
  643. b.type = Bonus::SHOTS; break;
  644. case 'b':
  645. b.type = Bonus::ENEMY_DEFENCE_REDUCTION; break;
  646. case 'C':
  647. b.type = Bonus::CHANGES_SPELL_COST_FOR_ALLY; break;
  648. case 'd':
  649. b.type = Bonus::DEFENSIVE_STANCE; break;
  650. case 'e':
  651. b.type = Bonus::DOUBLE_DAMAGE_CHANCE;
  652. b.subtype = 0;
  653. break;
  654. case 'E':
  655. b.type = Bonus::DEATH_STARE;
  656. b.subtype = 0; //Gorgon
  657. break;
  658. case 'F':
  659. b.type = Bonus::FEAR; break;
  660. case 'g':
  661. b.type = Bonus::SPELL_DAMAGE_REDUCTION;
  662. b.subtype = -1; //all magic schools
  663. break;
  664. case 'P':
  665. b.type = Bonus::CASTS; break;
  666. case 'R':
  667. b.type = Bonus::ADDITIONAL_RETALIATION; break;
  668. case 'W':
  669. b.type = Bonus::MAGIC_RESISTANCE;
  670. b.subtype = 0; //otherwise creature window goes crazy
  671. break;
  672. case 'f': //on-off skill
  673. enable = true; //sometimes format is: 2 -> 0, 1 -> 1
  674. switch (mod[0])
  675. {
  676. case 'A':
  677. b.type = Bonus::ATTACKS_ALL_ADJACENT; break;
  678. case 'b':
  679. b.type = Bonus::RETURN_AFTER_STRIKE; break;
  680. case 'B':
  681. b.type = Bonus::TWO_HEX_ATTACK_BREATH; break;
  682. case 'c':
  683. b.type = Bonus::JOUSTING; break;
  684. case 'D':
  685. b.type = Bonus::ADDITIONAL_ATTACK; break;
  686. case 'f':
  687. b.type = Bonus::FEARLESS; break;
  688. case 'F':
  689. b.type = Bonus::FLYING; break;
  690. case 'm':
  691. b.type = Bonus::SELF_MORALE; break;
  692. case 'M':
  693. b.type = Bonus::NO_MORALE; break;
  694. case 'p': //Mind spells
  695. case 'P':
  696. b.type = Bonus::MIND_IMMUNITY; break;
  697. case 'r':
  698. b.type = Bonus::REBIRTH; //on/off? makes sense?
  699. b.subtype = 0;
  700. b.val = 20; //arbitrary value
  701. break;
  702. case 'R':
  703. b.type = Bonus::BLOCKS_RETALIATION; break;
  704. case 's':
  705. b.type = Bonus::FREE_SHOOTING; break;
  706. case 'u':
  707. b.type = Bonus::SPELL_RESISTANCE_AURA; break;
  708. case 'U':
  709. b.type = Bonus::UNDEAD; break;
  710. default:
  711. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  712. return;
  713. break;
  714. }
  715. break;
  716. case 'w': //specific spell immunities, enabled/disabled
  717. enable = true;
  718. switch (mod[0])
  719. {
  720. case 'B': //Blind
  721. b.type = Bonus::SPELL_IMMUNITY;
  722. b.subtype = SpellID::BLIND;
  723. break;
  724. case 'H': //Hypnotize
  725. b.type = Bonus::SPELL_IMMUNITY;
  726. b.subtype = SpellID::HYPNOTIZE;
  727. break;
  728. case 'I': //Implosion
  729. b.type = Bonus::SPELL_IMMUNITY;
  730. b.subtype = SpellID::IMPLOSION;
  731. break;
  732. case 'K': //Berserk
  733. b.type = Bonus::SPELL_IMMUNITY;
  734. b.subtype = SpellID::BERSERK;
  735. break;
  736. case 'M': //Meteor Shower
  737. b.type = Bonus::SPELL_IMMUNITY;
  738. b.subtype = SpellID::METEOR_SHOWER;
  739. break;
  740. case 'N': //dispell beneficial spells
  741. b.type = Bonus::SPELL_IMMUNITY;
  742. b.subtype = SpellID::DISPEL_HELPFUL_SPELLS;
  743. break;
  744. case 'R': //Armageddon
  745. b.type = Bonus::SPELL_IMMUNITY;
  746. b.subtype = SpellID::ARMAGEDDON;
  747. break;
  748. case 'S': //Slow
  749. b.type = Bonus::SPELL_IMMUNITY;
  750. b.subtype = SpellID::SLOW;
  751. break;
  752. case '6':
  753. case '7':
  754. case '8':
  755. case '9':
  756. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  757. b.val = std::atoi(mod.c_str()) - 5;
  758. break;
  759. case ':':
  760. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  761. b.val = GameConstants::SPELL_LEVELS; //in case someone adds higher level spells?
  762. break;
  763. case 'F':
  764. b.type = Bonus::FIRE_IMMUNITY;
  765. b.subtype = 1; //not positive
  766. break;
  767. case 'O':
  768. b.type = Bonus::FIRE_IMMUNITY;
  769. b.subtype = 2; //only direct damage
  770. break;
  771. case 'f':
  772. b.type = Bonus::FIRE_IMMUNITY;
  773. b.subtype = 0; //all
  774. break;
  775. case 'C':
  776. b.type = Bonus::WATER_IMMUNITY;
  777. b.subtype = 1; //not positive
  778. break;
  779. case 'W':
  780. b.type = Bonus::WATER_IMMUNITY;
  781. b.subtype = 2; //only direct damage
  782. break;
  783. case 'w':
  784. b.type = Bonus::WATER_IMMUNITY;
  785. b.subtype = 0; //all
  786. break;
  787. case 'E':
  788. b.type = Bonus::EARTH_IMMUNITY;
  789. b.subtype = 2; //only direct damage
  790. break;
  791. case 'e':
  792. b.type = Bonus::EARTH_IMMUNITY;
  793. b.subtype = 0; //all
  794. break;
  795. case 'A':
  796. b.type = Bonus::AIR_IMMUNITY;
  797. b.subtype = 2; //only direct damage
  798. break;
  799. case 'a':
  800. b.type = Bonus::AIR_IMMUNITY;
  801. b.subtype = 0; //all
  802. break;
  803. case 'D':
  804. b.type = Bonus::DIRECT_DAMAGE_IMMUNITY;
  805. break;
  806. case '0':
  807. b.type = Bonus::RECEPTIVE;
  808. break;
  809. case 'm':
  810. b.type = Bonus::MIND_IMMUNITY;
  811. break;
  812. default:
  813. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  814. return;
  815. }
  816. break;
  817. case 'i':
  818. enable = true;
  819. b.type = Bonus::NO_DISTANCE_PENALTY;
  820. break;
  821. case 'o':
  822. enable = true;
  823. b.type = Bonus::NO_WALL_PENALTY;
  824. break;
  825. case 'a':
  826. case 'c':
  827. case 'K':
  828. case 'k':
  829. b.type = Bonus::SPELL_AFTER_ATTACK;
  830. b.subtype = stringToNumber(mod);
  831. break;
  832. case 'h':
  833. b.type= Bonus::HATE;
  834. b.subtype = stringToNumber(mod);
  835. break;
  836. case 'p':
  837. case 'J':
  838. b.type = Bonus::SPELL_BEFORE_ATTACK;
  839. b.subtype = stringToNumber(mod);
  840. b.additionalInfo = 3; //always expert?
  841. break;
  842. case 'r':
  843. b.type = Bonus::HP_REGENERATION;
  844. b.val = stringToNumber(mod);
  845. break;
  846. case 's':
  847. b.type = Bonus::ENCHANTED;
  848. b.subtype = stringToNumber(mod);
  849. b.valType = Bonus::INDEPENDENT_MAX;
  850. break;
  851. default:
  852. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  853. return;
  854. break;
  855. }
  856. switch (mod[0])
  857. {
  858. case '+':
  859. case '=': //should we allow percent values to stack or pick highest?
  860. b.valType = Bonus::ADDITIVE_VALUE;
  861. break;
  862. }
  863. //limiters, range
  864. si32 lastVal, curVal, lastLev = 0;
  865. if (enable) //0 and 2 means non-active, 1 - active
  866. {
  867. if (b.type != Bonus::REBIRTH)
  868. b.val = 0; //on-off ability, no value specified
  869. curVal = parser.readNumber();// 0 level is never active
  870. for (int i = 1; i < 11; ++i)
  871. {
  872. curVal = parser.readNumber();
  873. if (curVal == 1)
  874. {
  875. b.limiter.reset (new RankRangeLimiter(i));
  876. bl.push_back(new Bonus(b));
  877. break; //never turned off it seems
  878. }
  879. }
  880. }
  881. else
  882. {
  883. lastVal = parser.readNumber(); //basic value, not particularly useful but existent
  884. for (int i = 1; i < 11; ++i)
  885. {
  886. curVal = parser.readNumber();
  887. if (b.type == Bonus::HATE)
  888. curVal *= 10; //odd fix
  889. if (curVal > lastVal) //threshold, add new bonus
  890. {
  891. b.val = curVal - lastVal;
  892. lastVal = curVal;
  893. b.limiter.reset (new RankRangeLimiter(i));
  894. bl.push_back(new Bonus(b));
  895. lastLev = i; //start new range from here, i = previous rank
  896. }
  897. else if (curVal < lastVal)
  898. {
  899. b.val = lastVal;
  900. b.limiter.reset (new RankRangeLimiter(lastLev, i));
  901. }
  902. }
  903. }
  904. }
  905. int CCreatureHandler::stringToNumber(std::string & s)
  906. {
  907. boost::algorithm::replace_first(s,"#",""); //drop hash character
  908. return std::atoi(s.c_str());
  909. }
  910. CCreatureHandler::~CCreatureHandler()
  911. {
  912. }
  913. CreatureID CCreatureHandler::pickRandomMonster(const boost::function<int()> &randGen, int tier) const
  914. {
  915. int r = 0;
  916. if(tier == -1) //pick any allowed creature
  917. {
  918. do
  919. {
  920. r = vstd::pickRandomElementOf(creatures, randGen)->idNumber;
  921. } while (VLC->creh->creatures[r] && VLC->creh->creatures[r]->special); // find first "not special" creature
  922. }
  923. else
  924. {
  925. assert(vstd::iswithin(tier, 1, 7));
  926. std::vector<CreatureID> allowed;
  927. BOOST_FOREACH(const CBonusSystemNode *b, creaturesOfLevel[tier].getChildrenNodes())
  928. {
  929. assert(b->getNodeType() == CBonusSystemNode::CREATURE);
  930. const CCreature * crea = dynamic_cast<const CCreature*>(b);
  931. if(crea && !crea->special)
  932. allowed.push_back(crea->idNumber);
  933. }
  934. if(!allowed.size())
  935. {
  936. tlog2 << "Cannot pick a random creature of tier " << tier << "!\n";
  937. return CreatureID::NONE;
  938. }
  939. return vstd::pickRandomElementOf(allowed, randGen);
  940. }
  941. return CreatureID(r);
  942. }
  943. void CCreatureHandler::addBonusForTier(int tier, Bonus *b)
  944. {
  945. assert(vstd::iswithin(tier, 1, 7));
  946. creaturesOfLevel[tier].addNewBonus(b);
  947. }
  948. void CCreatureHandler::addBonusForAllCreatures(Bonus *b)
  949. {
  950. allCreatures.addNewBonus(b);
  951. }
  952. void CCreatureHandler::buildBonusTreeForTiers()
  953. {
  954. BOOST_FOREACH(CCreature *c, creatures)
  955. {
  956. if(vstd::isbetween(c->level, 0, ARRAY_COUNT(creaturesOfLevel)))
  957. c->attachTo(&creaturesOfLevel[c->level]);
  958. else
  959. c->attachTo(&creaturesOfLevel[0]);
  960. }
  961. BOOST_FOREACH(CBonusSystemNode &b, creaturesOfLevel)
  962. b.attachTo(&allCreatures);
  963. }
  964. void CCreatureHandler::deserializationFix()
  965. {
  966. buildBonusTreeForTiers();
  967. }