CCreatureHandler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * CCreatureHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include <vcmi/Creature.h>
  12. #include <vcmi/CreatureService.h>
  13. #include "HeroBonus.h"
  14. #include "ConstTransitivePtr.h"
  15. #include "ResourceSet.h"
  16. #include "GameConstants.h"
  17. #include "JsonNode.h"
  18. #include "IHandlerBase.h"
  19. #include "CRandomGenerator.h"
  20. #include "Terrain.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. class CLegacyConfigParser;
  23. class CCreatureHandler;
  24. class CCreature;
  25. class JsonSerializeFormat;
  26. class DLL_LINKAGE CCreature : public Creature, public CBonusSystemNode
  27. {
  28. public:
  29. std::string identifier;
  30. std::string nameRef; // reference name, stringID
  31. std::string nameSing;// singular name, e.g. Centaur
  32. std::string namePl; // plural name, e.g. Centaurs
  33. std::string abilityText; //description of abilities
  34. CreatureID idNumber;
  35. TFaction faction;
  36. ui8 level; // 0 - unknown; 1-7 for "usual" creatures
  37. //stats that are not handled by bonus system
  38. ui32 fightValue, AIValue, growth, hordeGrowth;
  39. ui32 ammMin, ammMax; // initial size of stack of these creatures on adventure map (if not set in editor)
  40. bool doubleWide;
  41. bool special; // Creature is not available normally (war machines, commanders, several unused creatures, etc
  42. TResources cost; //cost[res_id] - amount of that resource required to buy creature from dwelling
  43. std::set<CreatureID> upgrades; // IDs of creatures to which this creature can be upgraded
  44. std::string animDefName; // creature animation used during battles
  45. std::string advMapDef; //for new creatures only, image for adventure map
  46. si32 iconIndex; // index of icon in files like twcrport
  47. /// names of files with appropriate icons. Used only during loading
  48. std::string smallIconName;
  49. std::string largeIconName;
  50. struct CreatureAnimation
  51. {
  52. double timeBetweenFidgets, idleAnimationTime,
  53. walkAnimationTime, attackAnimationTime, flightAnimationDistance;
  54. int upperRightMissleOffsetX, rightMissleOffsetX, lowerRightMissleOffsetX,
  55. upperRightMissleOffsetY, rightMissleOffsetY, lowerRightMissleOffsetY;
  56. std::vector<double> missleFrameAngles;
  57. int troopCountLocationOffset, attackClimaxFrame;
  58. std::string projectileImageName;
  59. //bool projectileSpin; //if true, appropriate projectile is spinning during flight
  60. template <typename Handler> void serialize(Handler &h, const int version)
  61. {
  62. h & timeBetweenFidgets;
  63. h & idleAnimationTime;
  64. h & walkAnimationTime;
  65. h & attackAnimationTime;
  66. h & flightAnimationDistance;
  67. h & upperRightMissleOffsetX;
  68. h & rightMissleOffsetX;
  69. h & lowerRightMissleOffsetX;
  70. h & upperRightMissleOffsetY;
  71. h & rightMissleOffsetY;
  72. h & lowerRightMissleOffsetY;
  73. h & missleFrameAngles;
  74. h & troopCountLocationOffset;
  75. h & attackClimaxFrame;
  76. h & projectileImageName;
  77. }
  78. } animation;
  79. //sound info
  80. struct CreatureBattleSounds
  81. {
  82. std::string attack;
  83. std::string defend;
  84. std::string killed; // was killed or died
  85. std::string move;
  86. std::string shoot; // range attack
  87. std::string wince; // attacked but did not die
  88. std::string startMoving;
  89. std::string endMoving;
  90. template <typename Handler> void serialize(Handler &h, const int version)
  91. {
  92. h & attack;
  93. h & defend;
  94. h & killed;
  95. h & move;
  96. h & shoot;
  97. h & wince;
  98. h & startMoving;
  99. h & endMoving;
  100. }
  101. } sounds;
  102. ArtifactID warMachine;
  103. bool isItNativeTerrain(TerrainId terrain) const;
  104. /**
  105. Returns creature native terrain considering some terrain bonuses.
  106. @param considerBonus is used to avoid Dead Lock when this method is called inside getAllBonuses
  107. considerBonus = true is called from Pathfinder and fills actual nativeTerrain considering bonus(es).
  108. considerBonus = false is called on Battle init and returns already prepared nativeTerrain without Bonus system calling.
  109. */
  110. TerrainId getNativeTerrain() const;
  111. int32_t getIndex() const override;
  112. int32_t getIconIndex() const override;
  113. const std::string & getName() const override;
  114. const std::string & getJsonKey() const override;
  115. void registerIcons(const IconRegistar & cb) const override;
  116. CreatureID getId() const override;
  117. virtual const IBonusBearer * accessBonuses() const override;
  118. const std::string & getPluralName() const override;
  119. const std::string & getSingularName() const override;
  120. uint32_t getMaxHealth() const override;
  121. int32_t getAdvMapAmountMin() const override;
  122. int32_t getAdvMapAmountMax() const override;
  123. int32_t getAIValue() const override;
  124. int32_t getFightValue() const override;
  125. int32_t getLevel() const override;
  126. int32_t getGrowth() const override;
  127. int32_t getHorde() const override;
  128. int32_t getFactionIndex() const override;
  129. int32_t getBaseAttack() const override;
  130. int32_t getBaseDefense() const override;
  131. int32_t getBaseDamageMin() const override;
  132. int32_t getBaseDamageMax() const override;
  133. int32_t getBaseHitPoints() const override;
  134. int32_t getBaseSpellPoints() const override;
  135. int32_t getBaseSpeed() const override;
  136. int32_t getBaseShots() const override;
  137. int32_t getCost(int32_t resIndex) const override;
  138. bool isDoubleWide() const override; //returns true if unit is double wide on battlefield
  139. bool isGood () const;
  140. bool isEvil () const;
  141. si32 maxAmount(const std::vector<si32> &res) const; //how many creatures can be bought
  142. static int getQuantityID(const int & quantity); //1 - a few, 2 - several, 3 - pack, 4 - lots, 5 - horde, 6 - throng, 7 - swarm, 8 - zounds, 9 - legion
  143. static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range
  144. bool isMyUpgrade(const CCreature *anotherCre) const;
  145. bool valid() const;
  146. void addBonus(int val, Bonus::BonusType type, int subtype = -1);
  147. std::string nodeName() const override;
  148. template<typename RanGen>
  149. int getRandomAmount(RanGen ranGen) const
  150. {
  151. if(ammMax == ammMin)
  152. return ammMax;
  153. else
  154. return ammMin + (ranGen() % (ammMax - ammMin));
  155. }
  156. void updateFrom(const JsonNode & data);
  157. void serializeJson(JsonSerializeFormat & handler);
  158. template <typename Handler> void serialize(Handler &h, const int version)
  159. {
  160. h & static_cast<CBonusSystemNode&>(*this);
  161. h & namePl;
  162. h & nameSing;
  163. h & nameRef;
  164. h & cost;
  165. h & upgrades;
  166. h & fightValue;
  167. h & AIValue;
  168. h & growth;
  169. h & hordeGrowth;
  170. h & ammMin;
  171. h & ammMax;
  172. h & level;
  173. h & abilityText;
  174. h & animDefName;
  175. h & advMapDef;
  176. h & iconIndex;
  177. h & smallIconName;
  178. h & largeIconName;
  179. h & idNumber;
  180. h & faction;
  181. h & sounds;
  182. h & animation;
  183. h & doubleWide;
  184. h & special;
  185. h & identifier;
  186. h & warMachine;
  187. }
  188. CCreature();
  189. private:
  190. void fillWarMachine();
  191. };
  192. class DLL_LINKAGE CCreatureHandler : public CHandlerBase<CreatureID, Creature, CCreature, CreatureService>
  193. {
  194. private:
  195. CBonusSystemNode allCreatures;
  196. CBonusSystemNode creaturesOfLevel[GameConstants::CREATURES_PER_TOWN + 1];//index 0 is used for creatures of unknown tier or outside <1-7> range
  197. void loadJsonAnimation(CCreature * creature, const JsonNode & graphics);
  198. void loadStackExperience(CCreature * creature, const JsonNode &input);
  199. void loadCreatureJson(CCreature * creature, const JsonNode & config);
  200. /// adding abilities from ZCRTRAIT.TXT
  201. void loadBonuses(JsonNode & creature, std::string bonuses);
  202. /// load all creatures from H3 files
  203. void load();
  204. void loadCommanders();
  205. /// load creature from json structure
  206. void load(std::string creatureID, const JsonNode & node);
  207. /// read cranim.txt file from H3
  208. void loadAnimationInfo(std::vector<JsonNode> & h3Data);
  209. /// read one line from cranim.txt
  210. void loadUnitAnimInfo(JsonNode & unit, CLegacyConfigParser &parser);
  211. /// parse crexpbon.txt file from H3
  212. void loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser &parser);
  213. /// help function for parsing CREXPBON.txt
  214. int stringToNumber(std::string & s);
  215. protected:
  216. const std::vector<std::string> & getTypeNames() const override;
  217. CCreature * loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override;
  218. public:
  219. std::set<CreatureID> doubledCreatures; //they get double week
  220. //stack exp
  221. std::vector<std::vector<ui32> > expRanks; // stack experience needed for certain rank, index 0 for other tiers (?)
  222. std::vector<ui32> maxExpPerBattle; //%, tiers same as above
  223. si8 expAfterUpgrade;//multiplier in %
  224. //Commanders
  225. BonusList commanderLevelPremy; //bonus values added with each level-up
  226. std::vector< std::vector <ui8> > skillLevels; //how much of a bonus will be given to commander with every level. SPELL_POWER also gives CASTS and RESISTANCE
  227. std::vector <std::pair <std::shared_ptr<Bonus>, std::pair <ui8, ui8> > > skillRequirements; // first - Bonus, second - which two skills are needed to use it
  228. const CCreature * getCreature(const std::string & scope, const std::string & identifier) const;
  229. void deserializationFix();
  230. CreatureID pickRandomMonster(CRandomGenerator & rand, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
  231. void addBonusForTier(int tier, const std::shared_ptr<Bonus> & b); //tier must be <1-7>
  232. void addBonusForAllCreatures(const std::shared_ptr<Bonus> & b); //due to CBonusSystem::addNewBonus(const std::shared_ptr<Bonus>& b);
  233. void removeBonusesFromAllCreatures();
  234. CCreatureHandler();
  235. ~CCreatureHandler();
  236. /// load all creatures from H3 files
  237. void loadCrExpBon();
  238. /// generates tier-specific bonus tree entries
  239. void buildBonusTreeForTiers();
  240. void afterLoadFinalization() override;
  241. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  242. std::vector<bool> getDefaultAllowed() const override;
  243. template <typename Handler> void serialize(Handler &h, const int version)
  244. {
  245. //TODO: should be optimized, not all these informations needs to be serialized (same for ccreature)
  246. h & doubledCreatures;
  247. h & objects;
  248. h & expRanks;
  249. h & maxExpPerBattle;
  250. h & expAfterUpgrade;
  251. h & skillLevels;
  252. h & skillRequirements;
  253. h & commanderLevelPremy;
  254. h & allCreatures;
  255. h & creaturesOfLevel;
  256. BONUS_TREE_DESERIALIZATION_FIX
  257. }
  258. };
  259. VCMI_LIB_NAMESPACE_END