CCreatureHandler.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. class DLL_LINKAGE CCreature : public CBonusSystemNode
  20. {
  21. public:
  22. std::string nameRef; // reference name, stringID
  23. std::string nameSing;// singular name, e.g. Centaur
  24. std::string namePl; // plural name, e.g. Centaurs
  25. std::string abilityText; //description of abilities
  26. CreatureID idNumber;
  27. TFaction faction;
  28. ui8 level; // 0 - unknown
  29. //stats that are not handled by bonus system
  30. ui32 fightValue, AIValue, growth, hordeGrowth;
  31. ui32 ammMin, ammMax; // initial size of stack of these creatures on adventure map (if not set in editor)
  32. bool doubleWide;
  33. bool special; // Creature is not available normally (war machines, commanders, several unused creatures, etc
  34. TResources cost; //cost[res_id] - amount of that resource required to buy creature from dwelling
  35. std::set<CreatureID> upgrades; // IDs of creatures to which this creature can be upgraded
  36. std::string animDefName; // creature animation used during battles
  37. std::string advMapDef; //for new creatures only, image for adventure map
  38. si32 iconIndex; // index of icon in files like twcrport
  39. struct CreatureAnimation
  40. {
  41. double timeBetweenFidgets, walkAnimationTime, attackAnimationTime, flightAnimationDistance;
  42. int upperRightMissleOffsetX, rightMissleOffsetX, lowerRightMissleOffsetX,
  43. upperRightMissleOffsetY, rightMissleOffsetY, lowerRightMissleOffsetY;
  44. std::vector<double> missleFrameAngles;
  45. int troopCountLocationOffset, attackClimaxFrame;
  46. std::string projectileImageName;
  47. //bool projectileSpin; //if true, appropriate projectile is spinning during flight
  48. template <typename Handler> void serialize(Handler &h, const int version)
  49. {
  50. h & timeBetweenFidgets & walkAnimationTime & attackAnimationTime & flightAnimationDistance;
  51. h & upperRightMissleOffsetX & rightMissleOffsetX & lowerRightMissleOffsetX;
  52. h & upperRightMissleOffsetY & rightMissleOffsetY & lowerRightMissleOffsetY;
  53. h & missleFrameAngles & troopCountLocationOffset & attackClimaxFrame;
  54. h & projectileImageName;
  55. }
  56. } animation;
  57. //sound info
  58. struct CreatureBattleSounds
  59. {
  60. std::string attack;
  61. std::string defend;
  62. std::string killed; // was killed or died
  63. std::string move;
  64. std::string shoot; // range attack
  65. std::string wince; // attacked but did not die
  66. std::string ext1; // creature specific extension
  67. std::string ext2; // creature specific extension
  68. std::string startMoving; // usually same as ext1
  69. std::string endMoving; // usually same as ext2
  70. template <typename Handler> void serialize(Handler &h, const int version)
  71. {
  72. h & attack & defend & killed & move & shoot & wince & ext1 & ext2 & startMoving & endMoving;
  73. }
  74. } sounds;
  75. bool isItNativeTerrain(int terrain) const;
  76. bool isDoubleWide() const; //returns true if unit is double wide on battlefield
  77. bool isFlying() const; //returns true if it is a flying unit
  78. bool isShooting() const; //returns true if unit can shoot
  79. bool isUndead() const; //returns true if unit is undead
  80. bool isGood () const;
  81. bool isEvil () const;
  82. si32 maxAmount(const std::vector<si32> &res) const; //how many creatures can be bought
  83. 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
  84. static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range
  85. bool isMyUpgrade(const CCreature *anotherCre) const;
  86. bool valid() const;
  87. void addBonus(int val, Bonus::BonusType type, int subtype = -1);
  88. std::string nodeName() const override;
  89. template<typename RanGen>
  90. int getRandomAmount(RanGen ranGen) const
  91. {
  92. if(ammMax == ammMin)
  93. return ammMax;
  94. else
  95. return ammMin + (ranGen() % (ammMax - ammMin));
  96. }
  97. template <typename Handler> void serialize(Handler &h, const int version)
  98. {
  99. h & static_cast<CBonusSystemNode&>(*this);
  100. h & namePl & nameSing & nameRef
  101. & cost & upgrades
  102. & fightValue & AIValue & growth & hordeGrowth
  103. & ammMin & ammMax & level
  104. & abilityText & animDefName & advMapDef;
  105. h & iconIndex;
  106. h & idNumber & faction & sounds & animation;
  107. h & doubleWide & special;
  108. }
  109. CCreature();
  110. };
  111. class DLL_LINKAGE CCreatureHandler
  112. {
  113. private:
  114. CBonusSystemNode allCreatures;
  115. CBonusSystemNode creaturesOfLevel[GameConstants::CREATURES_PER_TOWN + 1];//index 0 is used for creatures of unknown tier or outside <1-7> range
  116. void loadStackExperience(CCreature * creature, const JsonNode &input);
  117. void loadCreatureJson(CCreature * creature, const JsonNode & config);
  118. public:
  119. std::set<CreatureID> doubledCreatures; //they get double week
  120. std::vector<ConstTransitivePtr<CCreature> > creatures; //creature ID -> creature info.
  121. //stack exp
  122. std::vector<std::vector<ui32> > expRanks; // stack experience needed for certain rank, index 0 for other tiers (?)
  123. std::vector<ui32> maxExpPerBattle; //%, tiers same as above
  124. si8 expAfterUpgrade;//multiplier in %
  125. //Commanders
  126. BonusList commanderLevelPremy; //bonus values added with each level-up
  127. 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
  128. std::vector <std::pair <Bonus, std::pair <ui8, ui8> > > skillRequirements; // first - Bonus, second - which two skills are needed to use it
  129. /// loading functions
  130. /// adding abilities from ZCRTRAIT.TXT
  131. void loadBonuses(CCreature & creature, std::string bonuses);
  132. /// load all creatures from H3 files
  133. void load();
  134. /// load creature from json structure
  135. void load(std::string creatureID, const JsonNode & node);
  136. /// load one creature from json config
  137. CCreature * loadCreature(const JsonNode & node);
  138. /// generates tier-specific bonus tree entries
  139. void buildBonusTreeForTiers();
  140. /// read cranim.txt file from H3
  141. void loadAnimationInfo();
  142. /// read one line from cranim.txt
  143. void loadUnitAnimInfo(CCreature & 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. CCreatureHandler();
  149. ~CCreatureHandler();
  150. void deserializationFix();
  151. CreatureID pickRandomMonster(const boost::function<int()> &randGen = 0, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
  152. void addBonusForTier(int tier, Bonus *b); //tier must be <1-7>
  153. void addBonusForAllCreatures(Bonus *b);
  154. template <typename Handler> void serialize(Handler &h, const int version)
  155. {
  156. //TODO: should be optimized, not all these informations needs to be serialized (same for ccreature)
  157. h & doubledCreatures & creatures;
  158. h & expRanks & maxExpPerBattle & expAfterUpgrade;
  159. h & skillLevels & skillRequirements & commanderLevelPremy;
  160. h & allCreatures;
  161. h & creaturesOfLevel;
  162. BONUS_TREE_DESERIALIZATION_FIX
  163. }
  164. };