CCreatureHandler.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #pragma once
  2. #include "HeroBonus.h"
  3. #include "ConstTransitivePtr.h"
  4. #include "ResourceSet.h"
  5. #include "GameConstants.h"
  6. #include "JsonNode.h"
  7. #include "IHandlerBase.h"
  8. #include "CRandomGenerator.h"
  9. /*
  10. * CCreatureHandler.h, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. class CLegacyConfigParser;
  19. class CCreatureHandler;
  20. class CCreature;
  21. class DLL_LINKAGE CCreature : public CBonusSystemNode
  22. {
  23. public:
  24. std::string identifier;
  25. std::string nameRef; // reference name, stringID
  26. std::string nameSing;// singular name, e.g. Centaur
  27. std::string namePl; // plural name, e.g. Centaurs
  28. std::string abilityText; //description of abilities
  29. CreatureID idNumber;
  30. TFaction faction;
  31. ui8 level; // 0 - unknown; 1-7 for "usual" creatures
  32. //stats that are not handled by bonus system
  33. ui32 fightValue, AIValue, growth, hordeGrowth;
  34. ui32 ammMin, ammMax; // initial size of stack of these creatures on adventure map (if not set in editor)
  35. bool doubleWide;
  36. bool special; // Creature is not available normally (war machines, commanders, several unused creatures, etc
  37. TResources cost; //cost[res_id] - amount of that resource required to buy creature from dwelling
  38. std::set<CreatureID> upgrades; // IDs of creatures to which this creature can be upgraded
  39. std::string animDefName; // creature animation used during battles
  40. std::string advMapDef; //for new creatures only, image for adventure map
  41. si32 iconIndex; // index of icon in files like twcrport
  42. /// names of files with appropriate icons. Used only during loading
  43. std::string smallIconName;
  44. std::string largeIconName;
  45. struct CreatureAnimation
  46. {
  47. double timeBetweenFidgets, idleAnimationTime,
  48. walkAnimationTime, attackAnimationTime, flightAnimationDistance;
  49. int upperRightMissleOffsetX, rightMissleOffsetX, lowerRightMissleOffsetX,
  50. upperRightMissleOffsetY, rightMissleOffsetY, lowerRightMissleOffsetY;
  51. std::vector<double> missleFrameAngles;
  52. int troopCountLocationOffset, attackClimaxFrame;
  53. std::string projectileImageName;
  54. //bool projectileSpin; //if true, appropriate projectile is spinning during flight
  55. template <typename Handler> void serialize(Handler &h, const int version)
  56. {
  57. h & timeBetweenFidgets & idleAnimationTime;
  58. h & walkAnimationTime & attackAnimationTime & flightAnimationDistance;
  59. h & upperRightMissleOffsetX & rightMissleOffsetX & lowerRightMissleOffsetX;
  60. h & upperRightMissleOffsetY & rightMissleOffsetY & lowerRightMissleOffsetY;
  61. h & missleFrameAngles & troopCountLocationOffset & attackClimaxFrame;
  62. h & projectileImageName;
  63. }
  64. } animation;
  65. //sound info
  66. struct CreatureBattleSounds
  67. {
  68. std::string attack;
  69. std::string defend;
  70. std::string killed; // was killed or died
  71. std::string move;
  72. std::string shoot; // range attack
  73. std::string wince; // attacked but did not die
  74. std::string startMoving;
  75. std::string endMoving;
  76. template <typename Handler> void serialize(Handler &h, const int version)
  77. {
  78. h & attack & defend & killed & move & shoot & wince & startMoving & endMoving;
  79. }
  80. } sounds;
  81. bool isItNativeTerrain(int terrain) const;
  82. bool isDoubleWide() const; //returns true if unit is double wide on battlefield
  83. bool isFlying() const; //returns true if it is a flying unit
  84. bool isShooting() const; //returns true if unit can shoot
  85. bool isUndead() const; //returns true if unit is undead
  86. bool isGood () const;
  87. bool isEvil () const;
  88. si32 maxAmount(const std::vector<si32> &res) const; //how many creatures can be bought
  89. static int getQuantityID(const int & quantity); //0 - a few, 1 - several, 2 - pack, 3 - lots, 4 - horde, 5 - throng, 6 - swarm, 7 - zounds, 8 - legion
  90. static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range
  91. bool isMyUpgrade(const CCreature *anotherCre) const;
  92. bool valid() const;
  93. void setId(CreatureID ID); //assigns idNumber and updates bonuses to reference it
  94. void addBonus(int val, Bonus::BonusType type, int subtype = -1);
  95. std::string nodeName() const override;
  96. template<typename RanGen>
  97. int getRandomAmount(RanGen ranGen) const
  98. {
  99. if(ammMax == ammMin)
  100. return ammMax;
  101. else
  102. return ammMin + (ranGen() % (ammMax - ammMin));
  103. }
  104. template <typename Handler> void serialize(Handler &h, const int version)
  105. {
  106. h & static_cast<CBonusSystemNode&>(*this);
  107. h & namePl & nameSing & nameRef
  108. & cost & upgrades
  109. & fightValue & AIValue & growth & hordeGrowth
  110. & ammMin & ammMax & level
  111. & abilityText & animDefName & advMapDef;
  112. h & iconIndex & smallIconName & largeIconName;
  113. h & idNumber & faction & sounds & animation;
  114. h & doubleWide & special;
  115. if(version>=759)
  116. {
  117. h & identifier;
  118. }
  119. }
  120. CCreature();
  121. };
  122. class DLL_LINKAGE CCreatureHandler : public IHandlerBase
  123. {
  124. private:
  125. CBonusSystemNode allCreatures;
  126. CBonusSystemNode creaturesOfLevel[GameConstants::CREATURES_PER_TOWN + 1];//index 0 is used for creatures of unknown tier or outside <1-7> range
  127. /// load one creature from json config
  128. CCreature * loadFromJson(const JsonNode & node, const std::string & identifier);
  129. void loadJsonAnimation(CCreature * creature, const JsonNode & graphics);
  130. void loadStackExperience(CCreature * creature, const JsonNode &input);
  131. void loadCreatureJson(CCreature * creature, const JsonNode & config);
  132. /// loading functions
  133. /// adding abilities from ZCRTRAIT.TXT
  134. void loadBonuses(JsonNode & creature, std::string bonuses);
  135. /// load all creatures from H3 files
  136. void load();
  137. void loadCommanders();
  138. /// load creature from json structure
  139. void load(std::string creatureID, const JsonNode & node);
  140. /// read cranim.txt file from H3
  141. void loadAnimationInfo(std::vector<JsonNode> & h3Data);
  142. /// read one line from cranim.txt
  143. void loadUnitAnimInfo(JsonNode & unit, CLegacyConfigParser &parser);
  144. /// parse crexpbon.txt file from H3
  145. void loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser &parser);
  146. /// help function for parsing CREXPBON.txt
  147. int stringToNumber(std::string & s);
  148. public:
  149. std::set<CreatureID> doubledCreatures; //they get double week
  150. std::vector<ConstTransitivePtr<CCreature> > creatures; //creature ID -> creature info.
  151. //stack exp
  152. std::vector<std::vector<ui32> > expRanks; // stack experience needed for certain rank, index 0 for other tiers (?)
  153. std::vector<ui32> maxExpPerBattle; //%, tiers same as above
  154. si8 expAfterUpgrade;//multiplier in %
  155. //Commanders
  156. BonusList commanderLevelPremy; //bonus values added with each level-up
  157. 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
  158. std::vector <std::pair <std::shared_ptr<Bonus>, std::pair <ui8, ui8> > > skillRequirements; // first - Bonus, second - which two skills are needed to use it
  159. const CCreature * getCreature(const std::string & scope, const std::string & identifier) const;
  160. void deserializationFix();
  161. CreatureID pickRandomMonster(CRandomGenerator & rand, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
  162. void addBonusForTier(int tier, std::shared_ptr<Bonus> b); //tier must be <1-7>
  163. void addBonusForAllCreatures(std::shared_ptr<Bonus> b);
  164. CCreatureHandler();
  165. ~CCreatureHandler();
  166. /// load all creatures from H3 files
  167. void loadCrExpBon();
  168. /// generates tier-specific bonus tree entries
  169. void buildBonusTreeForTiers();
  170. void afterLoadFinalization() override;
  171. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  172. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  173. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  174. std::vector<bool> getDefaultAllowed() const override;
  175. template <typename Handler> void serialize(Handler &h, const int version)
  176. {
  177. //TODO: should be optimized, not all these informations needs to be serialized (same for ccreature)
  178. h & doubledCreatures & creatures;
  179. h & expRanks & maxExpPerBattle & expAfterUpgrade;
  180. h & skillLevels & skillRequirements & commanderLevelPremy;
  181. h & allCreatures;
  182. h & creaturesOfLevel;
  183. BONUS_TREE_DESERIALIZATION_FIX
  184. }
  185. };