CCreatureHandler.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. };
  69. double timeBetweenFidgets, idleAnimationTime,
  70. walkAnimationTime, attackAnimationTime;
  71. int upperRightMissleOffsetX, rightMissleOffsetX, lowerRightMissleOffsetX,
  72. upperRightMissleOffsetY, rightMissleOffsetY, lowerRightMissleOffsetY;
  73. std::vector<double> missleFrameAngles;
  74. int attackClimaxFrame;
  75. AnimationPath projectileImageName;
  76. std::vector<RayColor> projectileRay;
  77. } animation;
  78. //sound info
  79. struct CreatureBattleSounds
  80. {
  81. AudioPath attack;
  82. AudioPath defend;
  83. AudioPath killed; // was killed or died
  84. AudioPath move;
  85. AudioPath shoot; // range attack
  86. AudioPath wince; // attacked but did not die
  87. AudioPath startMoving;
  88. AudioPath endMoving;
  89. } sounds;
  90. ArtifactID warMachine;
  91. std::string getNamePluralTranslated() const override;
  92. std::string getNameSingularTranslated() const override;
  93. std::string getNamePluralTextID() const override;
  94. std::string getNameSingularTextID() const override;
  95. FactionID getFaction() const override;
  96. int32_t getIndex() const override;
  97. int32_t getIconIndex() const override;
  98. std::string getJsonKey() const override;
  99. void registerIcons(const IconRegistar & cb) const override;
  100. CreatureID getId() const override;
  101. virtual const IBonusBearer * getBonusBearer() const override;
  102. int32_t getAdvMapAmountMin() const override;
  103. int32_t getAdvMapAmountMax() const override;
  104. int32_t getAIValue() const override;
  105. int32_t getFightValue() const override;
  106. int32_t getLevel() const override;
  107. int32_t getGrowth() const override;
  108. int32_t getHorde() const override;
  109. int32_t getBaseAttack() const override;
  110. int32_t getBaseDefense() const override;
  111. int32_t getBaseDamageMin() const override;
  112. int32_t getBaseDamageMax() const override;
  113. int32_t getBaseHitPoints() const override;
  114. int32_t getBaseSpellPoints() const override;
  115. int32_t getBaseSpeed() const override;
  116. int32_t getBaseShots() const override;
  117. int32_t getRecruitCost(GameResID resIndex) const override;
  118. TResources getFullRecruitCost() const override;
  119. bool isDoubleWide() const override; //returns true if unit is double wide on battlefield
  120. bool hasUpgrades() const override;
  121. bool isGood () const;
  122. bool isEvil () const;
  123. si32 maxAmount(const TResources &res) const; //how many creatures can be bought
  124. static CCreature::CreatureQuantityId getQuantityID(const int & quantity);
  125. static std::string getQuantityRangeStringForId(const CCreature::CreatureQuantityId & quantityId);
  126. static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range
  127. bool isMyUpgrade(const CCreature *anotherCre) const;
  128. bool valid() const;
  129. void addBonus(int val, BonusType type);
  130. void addBonus(int val, BonusType type, BonusSubtypeID subtype);
  131. std::string nodeName() const override;
  132. template<typename RanGen>
  133. int getRandomAmount(RanGen ranGen) const
  134. {
  135. if(ammMax == ammMin)
  136. return ammMax;
  137. else
  138. return ammMin + (ranGen() % (ammMax - ammMin));
  139. }
  140. void updateFrom(const JsonNode & data);
  141. void serializeJson(JsonSerializeFormat & handler);
  142. CCreature();
  143. private:
  144. static const std::map<CreatureQuantityId, std::string> creatureQuantityRanges;
  145. };
  146. class DLL_LINKAGE CCreatureHandler : public CHandlerBase<CreatureID, Creature, CCreature, CreatureService>
  147. {
  148. private:
  149. void loadJsonAnimation(CCreature * creature, const JsonNode & graphics) const;
  150. void loadStackExperience(CCreature * creature, const JsonNode & input) const;
  151. void loadCreatureJson(CCreature * creature, const JsonNode & config) const;
  152. /// adding abilities from ZCRTRAIT.TXT
  153. void loadBonuses(JsonNode & creature, std::string bonuses) const;
  154. /// load all creatures from H3 files
  155. void load();
  156. void loadCommanders();
  157. /// load creature from json structure
  158. void load(std::string creatureID, const JsonNode & node);
  159. /// read cranim.txt file from H3
  160. void loadAnimationInfo(std::vector<JsonNode> & h3Data) const;
  161. /// read one line from cranim.txt
  162. void loadUnitAnimInfo(JsonNode & unit, CLegacyConfigParser & parser) const;
  163. /// parse crexpbon.txt file from H3
  164. void loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser & parser) const;
  165. /// help function for parsing CREXPBON.txt
  166. int stringToNumber(std::string & s) const;
  167. protected:
  168. const std::vector<std::string> & getTypeNames() const override;
  169. CCreature * loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override;
  170. public:
  171. std::set<CreatureID> doubledCreatures; //they get double week
  172. //stack exp
  173. std::vector<std::vector<ui32> > expRanks; // stack experience needed for certain rank, index 0 for other tiers (?)
  174. std::vector<ui32> maxExpPerBattle; //%, tiers same as above
  175. si8 expAfterUpgrade;//multiplier in %
  176. //Commanders
  177. BonusList commanderLevelPremy; //bonus values added with each level-up
  178. 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
  179. std::vector <std::pair <std::shared_ptr<Bonus>, std::pair <ui8, ui8> > > skillRequirements; // first - Bonus, second - which two skills are needed to use it
  180. const CCreature * getCreature(const std::string & scope, const std::string & identifier) const;
  181. CreatureID pickRandomMonster(CRandomGenerator & rand, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
  182. CCreatureHandler();
  183. ~CCreatureHandler();
  184. /// load all stack experience bonuses from H3 files
  185. void loadCrExpBon(CBonusSystemNode & globalEffects);
  186. /// load all stack modifier bonuses from H3 files. TODO: move this to json
  187. void loadCrExpMod();
  188. void afterLoadFinalization() override;
  189. std::vector<JsonNode> loadLegacyData() override;
  190. std::vector<bool> getDefaultAllowed() const override;
  191. };
  192. VCMI_LIB_NAMESPACE_END