CObjectHandler.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. class IGameCallback;
  17. class CGObjectInstance;
  18. struct MetaString;
  19. struct BattleResult;
  20. class DLL_LINKAGE IObjectInterface
  21. {
  22. public:
  23. static IGameCallback *cb;
  24. IObjectInterface();
  25. virtual ~IObjectInterface();
  26. virtual void onHeroVisit(const CGHeroInstance * h) const;
  27. virtual void onHeroLeave(const CGHeroInstance * h) const;
  28. virtual void newTurn() const;
  29. virtual void initObj(); //synchr
  30. virtual void setProperty(ui8 what, ui32 val);//synchr
  31. //Called when queries created DURING HERO VISIT are resolved
  32. //First parameter is always hero that visited object and triggered the query
  33. virtual void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const;
  34. virtual void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const;
  35. virtual void garrisonDialogClosed(const CGHeroInstance *hero) const;
  36. virtual void heroLevelUpDone(const CGHeroInstance *hero) const;
  37. //unified interface, AI helpers
  38. virtual bool wasVisited (PlayerColor player) const;
  39. virtual bool wasVisited (const CGHeroInstance * h) const;
  40. static void preInit(); //called before objs receive their initObj
  41. static void postInit();//called after objs receive their initObj
  42. template <typename Handler> void serialize(Handler &h, const int version)
  43. {
  44. logGlobal->errorStream() << "IObjectInterface serialized, unexpected, should not happen!";
  45. }
  46. };
  47. class DLL_LINKAGE IBoatGenerator
  48. {
  49. public:
  50. const CGObjectInstance *o;
  51. IBoatGenerator(const CGObjectInstance *O);
  52. virtual ~IBoatGenerator() {}
  53. virtual int getBoatType() const; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  54. virtual void getOutOffsets(std::vector<int3> &offsets) const =0; //offsets to obj pos when we boat can be placed
  55. int3 bestLocation() const; //returns location when the boat should be placed
  56. enum EGeneratorState {GOOD, BOAT_ALREADY_BUILT, TILE_BLOCKED, NO_WATER};
  57. EGeneratorState shipyardStatus() const; //0 - can buid, 1 - there is already a boat at dest tile, 2 - dest tile is blocked, 3 - no water
  58. void getProblemText(MetaString &out, const CGHeroInstance *visitor = nullptr) const;
  59. template <typename Handler> void serialize(Handler &h, const int version)
  60. {
  61. h & o;
  62. }
  63. };
  64. class DLL_LINKAGE IShipyard : public IBoatGenerator
  65. {
  66. public:
  67. IShipyard(const CGObjectInstance *O);
  68. virtual ~IShipyard() {}
  69. virtual void getBoatCost(std::vector<si32> &cost) const;
  70. static const IShipyard *castFrom(const CGObjectInstance *obj);
  71. static IShipyard *castFrom(CGObjectInstance *obj);
  72. template <typename Handler> void serialize(Handler &h, const int version)
  73. {
  74. h & static_cast<IBoatGenerator&>(*this);
  75. }
  76. };
  77. class DLL_LINKAGE CGObjectInstance : public IObjectInterface
  78. {
  79. public:
  80. /// Position of bottom-right corner of object on map
  81. int3 pos;
  82. /// Type of object, e.g. town, hero, creature.
  83. Obj ID;
  84. /// Subtype of object, depends on type
  85. si32 subID;
  86. /// Index of object in map's list of objects
  87. ObjectInstanceID id;
  88. /// Defines appearance of object on map (animation, blocked tiles, blit order, etc)
  89. ObjectTemplate appearance;
  90. /// Current owner of an object (when below PLAYER_LIMIT)
  91. PlayerColor tempOwner;
  92. /// If true hero can visit this object only from neighbouring tiles and can't stand on this object
  93. bool blockVisit;
  94. std::string typeName;
  95. std::string subTypeName;
  96. CGObjectInstance();
  97. ~CGObjectInstance();
  98. const std::string & getStringId() const;
  99. /// "center" tile from which the sight distance is calculated
  100. int3 getSightCenter() const;
  101. PlayerColor getOwner() const;
  102. void setOwner(PlayerColor ow);
  103. /** APPEARANCE ACCESSORS **/
  104. int getWidth() const; //returns width of object graphic in tiles
  105. int getHeight() const; //returns height of object graphic in tiles
  106. bool visitableAt(int x, int y) const; //returns true if object is visitable at location (x, y) (h3m pos)
  107. int3 visitablePos() const;
  108. bool blockingAt(int x, int y) const; //returns true if object is blocking location (x, y) (h3m pos)
  109. bool coveringAt(int x, int y) const; //returns true if object covers with picture location (x, y) (h3m pos)
  110. std::set<int3> getBlockedPos() const; //returns set of positions blocked by this object
  111. std::set<int3> getBlockedOffsets() const; //returns set of relative positions blocked by this object
  112. bool isVisitable() const; //returns true if object is visitable
  113. /** VIRTUAL METHODS **/
  114. /// Returns true if player can pass through visitable tiles of this object
  115. virtual bool passableFor(PlayerColor color) const;
  116. /// Range of revealed map around this object, counting from getSightCenter()
  117. virtual int getSightRadious() const;
  118. /// returns (x,y,0) offset to a visitable tile of object
  119. virtual int3 getVisitableOffset() const;
  120. /// Called mostly during map randomization to turn random object into a regular one (e.g. "Random Monster" into "Pikeman")
  121. virtual void setType(si32 ID, si32 subID);
  122. /// returns text visible in status bar with specific hero/player active.
  123. /// Returns generic name of object, without any player-specific info
  124. virtual std::string getObjectName() const;
  125. /// Returns hover name for situation when there are no selected heroes. Default = object name
  126. virtual std::string getHoverText(PlayerColor player) const;
  127. /// Returns hero-specific hover name, including visited/not visited info. Default = player-specific name
  128. virtual std::string getHoverText(const CGHeroInstance * hero) const;
  129. /** OVERRIDES OF IObjectInterface **/
  130. void initObj() override;
  131. void onHeroVisit(const CGHeroInstance * h) const override;
  132. /// method for synchronous update. Note: For new properties classes should override setPropertyDer instead
  133. void setProperty(ui8 what, ui32 val) override final;
  134. //friend class CGameHandler;
  135. ///Entry point of binary (de-)serialization
  136. template <typename Handler> void serialize(Handler &h, const int version)
  137. {
  138. h & pos & ID & subID & id & tempOwner & blockVisit & appearance;
  139. //definfo is handled by map serializer
  140. if(version >= 755)
  141. {
  142. h & typeName & subTypeName;
  143. }
  144. }
  145. ///Entry point of Json serialization
  146. void writeJson(JsonNode & json, bool withState = false) const;
  147. ///Entry point of Json de-serialization
  148. void readJson(const JsonNode & json, bool withState = false);
  149. protected:
  150. /// virtual method that allows synchronously update object state on server and all clients
  151. virtual void setPropertyDer(ui8 what, ui32 val);
  152. /// Gives dummy bonus from this object to hero. Can be used to track visited state
  153. void giveDummyBonus(ObjectInstanceID heroID, ui8 duration = Bonus::ONE_DAY) const;
  154. ///Saves object-type specific options
  155. ///(!) do not forget to call inherited method first when overriding
  156. virtual void writeJsonOptions(JsonNode & json) const;
  157. ///Loads object-type specific options
  158. ///(!) do not forget to call inherited method first when overriding
  159. virtual void readJsonOptions(const JsonNode & json);
  160. ///Saves object-type specific state
  161. ///(!) do not forget to call inherited method first when overriding
  162. virtual void writeJsonState(JsonNode & json) const;
  163. ///Loads object-type specific state
  164. ///(!) do not forget to call inherited method first when overriding
  165. virtual void readJsonState(const JsonNode & json);
  166. private:
  167. mutable std::string stringId;///<alternate id, dynamically generated, do not serialize
  168. };
  169. /// function object which can be used to find an object with an specific sub ID
  170. class CGObjectInstanceBySubIdFinder
  171. {
  172. public:
  173. CGObjectInstanceBySubIdFinder(CGObjectInstance * obj);
  174. bool operator()(CGObjectInstance * obj) const;
  175. private:
  176. CGObjectInstance * obj;
  177. };
  178. class DLL_LINKAGE CObjectHandler
  179. {
  180. public:
  181. std::vector<ui32> resVals; //default values of resources in gold
  182. CObjectHandler();
  183. template <typename Handler> void serialize(Handler &h, const int version)
  184. {
  185. h & resVals;
  186. }
  187. };