CCreatureHandler.h 10 KB

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