CCreatureHandler.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma once
  2. #include "../lib/HeroBonus.h"
  3. #include "../lib/ConstTransitivePtr.h"
  4. #include "ResourceSet.h"
  5. #include "GameConstants.h"
  6. /*
  7. * CCreatureHandler.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. #define ALLCREATURESGETDOUBLEMONTHS false
  16. class CLodHandler;
  17. class CCreatureHandler;
  18. class CCreature;
  19. class DLL_LINKAGE CCreature : public CBonusSystemNode
  20. {
  21. public:
  22. std::string namePl, nameSing, nameRef; //name in singular and plural form; and reference name
  23. TResources cost; //cost[res_id] - amount of that resource
  24. std::set<ui32> upgrades; // IDs of creatures to which this creature can be upgraded
  25. ui32 hitPoints, speed, attack, defence;
  26. ui32 fightValue, AIValue, growth, hordeGrowth, shots, spells;
  27. ui32 damageMin, damageMax;
  28. ui32 ammMin, ammMax;
  29. ui8 level; // 0 - unknown
  30. std::string abilityText; //description of abilities
  31. std::string abilityRefs; //references to abilities, in text format
  32. std::string animDefName;
  33. si32 idNumber;
  34. si8 faction; //-1 = neutral
  35. ui8 doubleWide;
  36. ///animation info
  37. double timeBetweenFidgets, walkAnimationTime, attackAnimationTime, flightAnimationDistance;
  38. int upperRightMissleOffsetX, rightMissleOffsetX, lowerRightMissleOffsetX, upperRightMissleOffsetY, rightMissleOffsetY, lowerRightMissleOffsetY;
  39. double missleFrameAngles[12];
  40. int troopCountLocationOffset, attackClimaxFrame;
  41. ///end of anim info
  42. bool isItNativeTerrain(int terrain) const;
  43. bool isDoubleWide() const; //returns true if unit is double wide on battlefield
  44. bool isFlying() const; //returns true if it is a flying unit
  45. bool isShooting() const; //returns true if unit can shoot
  46. bool isUndead() const; //returns true if unit is undead
  47. bool isGood () const;
  48. bool isEvil () const;
  49. si32 maxAmount(const std::vector<si32> &res) const; //how many creatures can be bought
  50. 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
  51. static int estimateCreatureCount(ui32 countID); //reverse version of above function, returns middle of range
  52. bool isMyUpgrade(const CCreature *anotherCre) const;
  53. bool valid() const;
  54. void addBonus(int val, int type, int subtype = -1);
  55. std::string nodeName() const OVERRIDE;
  56. //void getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const;
  57. template<typename RanGen>
  58. int getRandomAmount(RanGen ranGen) const
  59. {
  60. if(ammMax == ammMin)
  61. return ammMax;
  62. else
  63. return ammMin + (ranGen() % (ammMax - ammMin));
  64. }
  65. template <typename Handler> void serialize(Handler &h, const int version)
  66. {
  67. h & static_cast<CBonusSystemNode&>(*this);
  68. h & namePl & nameSing & nameRef
  69. & cost & upgrades
  70. & fightValue & AIValue & growth & hordeGrowth & hitPoints & speed & attack & defence & shots & spells
  71. & damageMin & damageMax & ammMin & ammMax & level
  72. & abilityText & abilityRefs & animDefName
  73. & idNumber & faction
  74. & timeBetweenFidgets & walkAnimationTime & attackAnimationTime & flightAnimationDistance
  75. & upperRightMissleOffsetX & rightMissleOffsetX & lowerRightMissleOffsetX & upperRightMissleOffsetY & rightMissleOffsetY & lowerRightMissleOffsetY
  76. & missleFrameAngles & troopCountLocationOffset & attackClimaxFrame;
  77. h & doubleWide;
  78. }
  79. CCreature();
  80. friend class CCreatureHandler;
  81. };
  82. class DLL_LINKAGE CCreatureHandler
  83. {
  84. private: //?
  85. CBonusSystemNode allCreatures;
  86. CBonusSystemNode creaturesOfLevel[GameConstants::CREATURES_PER_TOWN + 1];//index 0 is used for creatures of unknown tier or outside <1-7> range
  87. public:
  88. std::set<int> notUsedMonsters;
  89. std::set<TCreature> doubledCreatures; //they get double week
  90. std::vector<ConstTransitivePtr<CCreature> > creatures; //creature ID -> creature info
  91. bmap<std::string,int> nameToID;
  92. bmap<int,std::string> idToProjectile;
  93. bmap<int,bool> idToProjectileSpin; //if true, appropriate projectile is spinning during flight
  94. std::vector<si8> factionAlignments; //1 for good, 0 for neutral and -1 for evil with faction ID as index
  95. int factionToTurretCreature[GameConstants::F_NUMBER]; //which creature's animation should be used to dispaly creature in turret while siege
  96. //stack exp
  97. std::map<TBonusType, std::pair<std::string, std::string> > stackBonuses; // bonus => name, description
  98. std::vector<std::vector<ui32> > expRanks; // stack experience needed for certain rank, index 0 for other tiers (?)
  99. std::vector<ui32> maxExpPerBattle; //%, tiers same as above
  100. si8 expAfterUpgrade;//multiplier in %
  101. //Commanders
  102. std::map <ui8, ui32> factionCommanders;
  103. BonusList commanderLevelPremy; //bonus values added with each level-up
  104. 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
  105. std::vector <std::pair <Bonus, std::pair <ui8, ui8> > > skillRequirements; // first - Bonus, second - which two skills are needed to use it
  106. void deserializationFix();
  107. void loadCreatures();
  108. void buildBonusTreeForTiers();
  109. void loadAnimationInfo();
  110. void loadUnitAnimInfo(CCreature & unit, std::string & src, int & i);
  111. void loadStackExp(Bonus & b, BonusList & bl, std::string & src, int & it);
  112. int stringToNumber(std::string & s);//help function for parsing CREXPBON.txt
  113. bool isGood (si8 faction) const;
  114. bool isEvil (si8 faction) const;
  115. int pickRandomMonster(const boost::function<int()> &randGen = 0, int tier = -1) const; //tier <1 - CREATURES_PER_TOWN> or -1 for any
  116. void addBonusForTier(int tier, Bonus *b); //tier must be <1-7>
  117. void addBonusForAllCreatures(Bonus *b);
  118. CCreatureHandler();
  119. ~CCreatureHandler();
  120. template <typename Handler> void serialize(Handler &h, const int version)
  121. {
  122. //TODO: should be optimized, not all these informations needs to be serialized (same for ccreature)
  123. h & notUsedMonsters & creatures & nameToID & idToProjectile & idToProjectileSpin & factionToTurretCreature;
  124. h & stackBonuses & expRanks & maxExpPerBattle & expAfterUpgrade;
  125. h & factionCommanders & skillLevels & skillRequirements & commanderLevelPremy;
  126. h & allCreatures;
  127. h & creaturesOfLevel;
  128. BONUS_TREE_DESERIALIZATION_FIX
  129. }
  130. };