CCreatureHandler.cpp 31 KB

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