CCreatureHandler.h 7.4 KB

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