CCreatureHandler.h 7.7 KB

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