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