CCreatureHandler.h 10 KB

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