CObjectHandler.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #pragma once
  2. #include "ObjectTemplate.h"
  3. #include "../IGameCallback.h"
  4. #include "../int3.h"
  5. #include "../HeroBonus.h"
  6. /*
  7. * CObjectHandler.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. class CGHeroInstance;
  16. struct BattleResult;
  17. class DLL_LINKAGE IObjectInterface
  18. {
  19. public:
  20. static IGameCallback *cb;
  21. IObjectInterface();
  22. virtual ~IObjectInterface();
  23. virtual void onHeroVisit(const CGHeroInstance * h) const;
  24. virtual void onHeroLeave(const CGHeroInstance * h) const;
  25. virtual void newTurn() const;
  26. virtual void initObj(); //synchr
  27. virtual void setProperty(ui8 what, ui32 val);//synchr
  28. //Called when queries created DURING HERO VISIT are resolved
  29. //First parameter is always hero that visited object and triggered the query
  30. virtual void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const;
  31. virtual void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const;
  32. virtual void garrisonDialogClosed(const CGHeroInstance *hero) const;
  33. virtual void heroLevelUpDone(const CGHeroInstance *hero) const;
  34. //unified interface, AI helpers
  35. virtual bool wasVisited (PlayerColor player) const;
  36. virtual bool wasVisited (const CGHeroInstance * h) const;
  37. static void preInit(); //called before objs receive their initObj
  38. static void postInit();//called after objs receive their initObj
  39. template <typename Handler> void serialize(Handler &h, const int version)
  40. {
  41. logGlobal->errorStream() << "IObjectInterface serialized, unexpected, should not happen!";
  42. }
  43. };
  44. class DLL_LINKAGE IBoatGenerator
  45. {
  46. public:
  47. const CGObjectInstance *o;
  48. IBoatGenerator(const CGObjectInstance *O);
  49. virtual ~IBoatGenerator() {}
  50. virtual int getBoatType() const; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  51. virtual void getOutOffsets(std::vector<int3> &offsets) const =0; //offsets to obj pos when we boat can be placed
  52. int3 bestLocation() const; //returns location when the boat should be placed
  53. enum EGeneratorState {GOOD, BOAT_ALREADY_BUILT, TILE_BLOCKED, NO_WATER};
  54. EGeneratorState shipyardStatus() const; //0 - can buid, 1 - there is already a boat at dest tile, 2 - dest tile is blocked, 3 - no water
  55. void getProblemText(MetaString &out, const CGHeroInstance *visitor = nullptr) const;
  56. template <typename Handler> void serialize(Handler &h, const int version)
  57. {
  58. h & o;
  59. }
  60. };
  61. class DLL_LINKAGE IShipyard : public IBoatGenerator
  62. {
  63. public:
  64. IShipyard(const CGObjectInstance *O);
  65. virtual ~IShipyard() {}
  66. virtual void getBoatCost(std::vector<si32> &cost) const;
  67. static const IShipyard *castFrom(const CGObjectInstance *obj);
  68. static IShipyard *castFrom(CGObjectInstance *obj);
  69. template <typename Handler> void serialize(Handler &h, const int version)
  70. {
  71. h & static_cast<IBoatGenerator&>(*this);
  72. }
  73. };
  74. class DLL_LINKAGE CGObjectInstance : public IObjectInterface
  75. {
  76. public:
  77. mutable std::string hoverName;
  78. int3 pos; //h3m pos
  79. Obj ID;
  80. si32 subID; //normal subID (this one from OH3 maps ;])
  81. ObjectInstanceID id;//number of object in map's vector
  82. ObjectTemplate appearance;
  83. PlayerColor tempOwner;
  84. bool blockVisit; //if non-zero then blocks the tile but is visitable from neighbouring tile
  85. virtual ui8 getPassableness() const; //bitmap - if the bit is set the corresponding player can pass through the visitable tiles of object, even if it's blockvis; if not set - default properties from definfo are used
  86. virtual int3 getSightCenter() const; //"center" tile from which the sight distance is calculated
  87. virtual int getSightRadious() const; //sight distance (should be used if player-owned structure)
  88. bool passableFor(PlayerColor color) const;
  89. void getSightTiles(std::unordered_set<int3, ShashInt3> &tiles) const; //returns reference to the set
  90. PlayerColor getOwner() const;
  91. void setOwner(PlayerColor ow);
  92. int getWidth() const; //returns width of object graphic in tiles
  93. int getHeight() const; //returns height of object graphic in tiles
  94. virtual bool visitableAt(int x, int y) const; //returns true if object is visitable at location (x, y) (h3m pos)
  95. virtual int3 getVisitableOffset() const; //returns (x,y,0) offset to first visitable tile from bottom right obj tile (0,0,0) (h3m pos)
  96. int3 visitablePos() const;
  97. bool blockingAt(int x, int y) const; //returns true if object is blocking location (x, y) (h3m pos)
  98. bool coveringAt(int x, int y) const; //returns true if object covers with picture location (x, y) (h3m pos)
  99. std::set<int3> getBlockedPos() const; //returns set of positions blocked by this object
  100. std::set<int3> getBlockedOffsets() const; //returns set of relative positions blocked by this object
  101. bool isVisitable() const; //returns true if object is visitable
  102. bool operator<(const CGObjectInstance & cmp) const; //screen printing priority comparing
  103. void hideTiles(PlayerColor ourplayer, int radius) const;
  104. CGObjectInstance();
  105. virtual ~CGObjectInstance();
  106. //CGObjectInstance(const CGObjectInstance & right);
  107. //CGObjectInstance& operator=(const CGObjectInstance & right);
  108. virtual const std::string & getHoverText() const;
  109. void setType(si32 ID, si32 subID);
  110. ///IObjectInterface
  111. void initObj() override;
  112. void onHeroVisit(const CGHeroInstance * h) const override;
  113. void setProperty(ui8 what, ui32 val) override;//synchr
  114. friend class CGameHandler;
  115. template <typename Handler> void serialize(Handler &h, const int version)
  116. {
  117. h & hoverName & pos & ID & subID & id & tempOwner & blockVisit & appearance;
  118. //definfo is handled by map serializer
  119. }
  120. protected:
  121. virtual void setPropertyDer(ui8 what, ui32 val);//synchr
  122. void getNameVis(std::string &hname) const;
  123. void giveDummyBonus(ObjectInstanceID heroID, ui8 duration = Bonus::ONE_DAY) const;
  124. };
  125. /// function object which can be used to find an object with an specific sub ID
  126. class CGObjectInstanceBySubIdFinder
  127. {
  128. public:
  129. CGObjectInstanceBySubIdFinder(CGObjectInstance * obj);
  130. bool operator()(CGObjectInstance * obj) const;
  131. private:
  132. CGObjectInstance * obj;
  133. };
  134. struct BankConfig
  135. {
  136. BankConfig() {level = chance = upgradeChance = combatValue = value = rewardDifficulty = easiest = 0; };
  137. ui8 level; //1 - 4, how hard the battle will be
  138. ui8 chance; //chance for this level being chosen
  139. ui8 upgradeChance; //chance for creatures to be in upgraded versions
  140. std::vector< std::pair <CreatureID, ui32> > guards; //creature ID, amount
  141. ui32 combatValue; //how hard are guards of this level
  142. Res::ResourceSet resources; //resources given in case of victory
  143. std::vector< std::pair <CreatureID, ui32> > creatures; //creatures granted in case of victory (creature ID, amount)
  144. std::vector<ui16> artifacts; //number of artifacts given in case of victory [0] -> treasure, [1] -> minor [2] -> major [3] -> relic
  145. ui32 value; //overall value of given things
  146. ui32 rewardDifficulty; //proportion of reward value to difficulty of guards; how profitable is this creature Bank config
  147. ui16 easiest; //?!?
  148. template <typename Handler> void serialize(Handler &h, const int version)
  149. {
  150. h & level & chance & upgradeChance & guards & combatValue & resources & creatures & artifacts & value & rewardDifficulty & easiest;
  151. }
  152. };
  153. class DLL_LINKAGE CObjectHandler
  154. {
  155. public:
  156. std::map <ui32, std::vector < ConstTransitivePtr<BankConfig> > > banksInfo; //[index][preset]
  157. std::map <ui32, std::string> creBanksNames; //[crebank index] -> name of this creature bank
  158. std::vector<ui32> resVals; //default values of resources in gold
  159. CObjectHandler();
  160. ~CObjectHandler();
  161. int bankObjToIndex (const CGObjectInstance * obj);
  162. template <typename Handler> void serialize(Handler &h, const int version)
  163. {
  164. h & banksInfo & creBanksNames & resVals;
  165. }
  166. };