CCreatureHandler.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #pragma once
  2. #include "HeroBonus.h"
  3. #include "ConstTransitivePtr.h"
  4. #include "ResourceSet.h"
  5. #include "GameConstants.h"
  6. #include "JsonNode.h"
  7. /*
  8. * CCreatureHandler.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class CLegacyConfigParser;
  17. class CCreatureHandler;
  18. class CCreature;
  19. struct CreaturesBattleSounds;
  20. class DLL_LINKAGE CCreature : public CBonusSystemNode
  21. {
  22. public:
  23. std::string namePl, nameSing, nameRef; //name in singular and plural form; and reference name
  24. TResources cost; //cost[res_id] - amount of that resource
  25. std::set<std::string> upgradeNames; //for reference, they are later transformed info ui32 upgrades
  26. std::set<ui32> upgrades; // IDs of creatures to which this creature can be upgraded
  27. //damage, hp. etc are handled by Bonuses
  28. ui32 fightValue, AIValue, growth, hordeGrowth;
  29. ui32 ammMin, ammMax;
  30. ui8 level; // 0 - unknown
  31. std::string abilityText; //description of abilities
  32. std::string abilityRefs; //references to abilities, in text format
  33. std::string animDefName;
  34. std::string advMapDef; //for new creatures only
  35. si32 idNumber;
  36. si32 iconIndex; // index of icon in files like twcrport
  37. TFaction faction; //-1 = neutral
  38. ui8 doubleWide;
  39. ///animation info
  40. double timeBetweenFidgets, walkAnimationTime, attackAnimationTime, flightAnimationDistance;
  41. int upperRightMissleOffsetX, rightMissleOffsetX, lowerRightMissleOffsetX, upperRightMissleOffsetY, rightMissleOffsetY, lowerRightMissleOffsetY;
  42. double missleFrameAngles[12];
  43. int troopCountLocationOffset, attackClimaxFrame;
  44. std::string projectile;
  45. ui8 projectileSpin; //if true, appropriate projectile is spinning during flight
  46. ///end of anim info
  47. //sound info
  48. struct CreaturesBattleSounds
  49. {
  50. std::string attack;
  51. std::string defend;
  52. std::string killed; // was killed or died
  53. std::string move;
  54. std::string shoot; // range attack
  55. std::string wince; // attacked but did not die
  56. std::string ext1; // creature specific extension
  57. std::string ext2; // creature specific extension
  58. std::string startMoving; // usually same as ext1
  59. std::string endMoving; // usually same as ext2
  60. template <typename Handler> void serialize(Handler &h, const int version)
  61. {
  62. h & attack & defend & killed & move & shoot & wince & ext1 & ext2 & startMoving & endMoving;
  63. }
  64. } sounds;
  65. bool isItNativeTerrain(int terrain) const;
  66. bool isDoubleWide() const; //returns true if unit is double wide on battlefield
  67. bool isFlying() const; //returns true if it is a flying unit
  68. bool isShooting() const; //returns true if unit can shoot
  69. bool isUndead() const; //returns true if unit is undead
  70. bool isGood () const;
  71. bool isEvil () const;
  72. si32 maxAmount(const std::vector<si32> &res) const; //how many creatures can be bought
  73. 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
  74. static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range
  75. bool isMyUpgrade(const CCreature *anotherCre) const;
  76. bool valid() const;
  77. void addBonus(int val, int type, int subtype = -1);
  78. std::string nodeName() const override;
  79. //void getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const;
  80. template<typename RanGen>
  81. int getRandomAmount(RanGen ranGen) const
  82. {
  83. if(ammMax == ammMin)
  84. return ammMax;
  85. else
  86. return ammMin + (ranGen() % (ammMax - ammMin));
  87. }
  88. template <typename Handler> void serialize(Handler &h, const int version)
  89. {
  90. h & static_cast<CBonusSystemNode&>(*this);
  91. h & namePl & nameSing & nameRef
  92. & cost & upgrades
  93. & fightValue & AIValue & growth & hordeGrowth
  94. & ammMin & ammMax & level
  95. & abilityText & abilityRefs & animDefName & advMapDef;
  96. h & iconIndex;
  97. h & idNumber & faction
  98. & timeBetweenFidgets & walkAnimationTime & attackAnimationTime & flightAnimationDistance
  99. & upperRightMissleOffsetX & rightMissleOffsetX & lowerRightMissleOffsetX & upperRightMissleOffsetY & rightMissleOffsetY & lowerRightMissleOffsetY
  100. & missleFrameAngles & troopCountLocationOffset & attackClimaxFrame;
  101. h & sounds & projectile & projectileSpin;
  102. h & doubleWide;
  103. }
  104. CCreature();
  105. friend class CCreatureHandler;
  106. };
  107. class DLL_LINKAGE CCreatureHandler
  108. {
  109. private: //?
  110. CBonusSystemNode allCreatures;
  111. CBonusSystemNode creaturesOfLevel[GameConstants::CREATURES_PER_TOWN + 1];//index 0 is used for creatures of unknown tier or outside <1-7> range
  112. public:
  113. std::set<int> notUsedMonsters;
  114. std::set<TCreature> doubledCreatures; //they get double week
  115. std::vector<ConstTransitivePtr<CCreature> > creatures; //creature ID -> creature info
  116. //stack exp
  117. std::map<TBonusType, std::pair<std::string, std::string> > stackBonuses; // bonus => name, description
  118. std::vector<std::vector<ui32> > expRanks; // stack experience needed for certain rank, index 0 for other tiers (?)
  119. std::vector<ui32> maxExpPerBattle; //%, tiers same as above
  120. si8 expAfterUpgrade;//multiplier in %
  121. //Commanders
  122. std::map <TFaction, TCreature> factionCommanders;
  123. BonusList commanderLevelPremy; //bonus values added with each level-up
  124. 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
  125. std::vector <std::pair <Bonus, std::pair <ui8, ui8> > > skillRequirements; // first - Bonus, second - which two skills are needed to use it
  126. /// loading functions
  127. /// load all creatures from H3 files
  128. void loadCreatures();
  129. /// load all creatures from json structure
  130. void load(const JsonNode & node);
  131. /// load one creature from json config
  132. CCreature * loadCreature(const JsonNode & node);
  133. /// generates tier-specific bonus tree entries
  134. void buildBonusTreeForTiers();
  135. /// read cranim.txt file from H3
  136. void loadAnimationInfo();
  137. /// read one line from cranim.txt
  138. void loadUnitAnimInfo(CCreature & unit, CLegacyConfigParser &parser);
  139. /// load cr_sounds.json config
  140. void loadCreatureSounds(JsonNode node, si32 creaID);
  141. void loadSoundsInfo();
  142. /// parse crexpbon.txt file from H3
  143. void loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser &parser);
  144. /// help function for parsing CREXPBON.txt
  145. int stringToNumber(std::string & s);
  146. CCreatureHandler();
  147. ~CCreatureHandler();
  148. void deserializationFix();
  149. int pickRandomMonster(const boost::function<int()> &randGen = 0, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
  150. void addBonusForTier(int tier, Bonus *b); //tier must be <1-7>
  151. void addBonusForAllCreatures(Bonus *b);
  152. template <typename Handler> void serialize(Handler &h, const int version)
  153. {
  154. //TODO: should be optimized, not all these informations needs to be serialized (same for ccreature)
  155. h & notUsedMonsters & creatures;
  156. h & stackBonuses & expRanks & maxExpPerBattle & expAfterUpgrade;
  157. h & factionCommanders & skillLevels & skillRequirements & commanderLevelPremy;
  158. h & allCreatures;
  159. h & creaturesOfLevel;
  160. BONUS_TREE_DESERIALIZATION_FIX
  161. }
  162. };