CObjectHandler.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * CObjectHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "ObjectTemplate.h"
  12. #include "../int3.h"
  13. #include "../NetPacksBase.h"
  14. #include "../ResourceSet.h"
  15. #include "../bonuses/HeroBonus.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class CGHeroInstance;
  18. class IGameCallback;
  19. class CGObjectInstance;
  20. struct MetaString;
  21. struct BattleResult;
  22. class JsonSerializeFormat;
  23. class CRandomGenerator;
  24. class CMap;
  25. class JsonNode;
  26. // This one teleport-specific, but has to be available everywhere in callbacks and netpacks
  27. // For now it's will be there till teleports code refactored and moved into own file
  28. using TTeleportExitsList = std::vector<std::pair<ObjectInstanceID, int3>>;
  29. class DLL_LINKAGE IObjectInterface
  30. {
  31. public:
  32. static IGameCallback *cb;
  33. virtual ~IObjectInterface() = default;
  34. virtual int32_t getObjGroupIndex() const = 0;
  35. virtual int32_t getObjTypeIndex() const = 0;
  36. virtual PlayerColor getOwner() const = 0;
  37. virtual int3 visitablePos() const = 0;
  38. virtual int3 getPosition() const = 0;
  39. virtual void onHeroVisit(const CGHeroInstance * h) const;
  40. virtual void onHeroLeave(const CGHeroInstance * h) const;
  41. virtual void newTurn(CRandomGenerator & rand) const;
  42. virtual void initObj(CRandomGenerator & rand); //synchr
  43. virtual void setProperty(ui8 what, ui32 val);//synchr
  44. //Called when queries created DURING HERO VISIT are resolved
  45. //First parameter is always hero that visited object and triggered the query
  46. virtual void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const;
  47. virtual void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const;
  48. virtual void garrisonDialogClosed(const CGHeroInstance *hero) const;
  49. virtual void heroLevelUpDone(const CGHeroInstance *hero) const;
  50. //unified helper to show info dialog for object owner
  51. virtual void showInfoDialog(const ui32 txtID, const ui16 soundID = 0, EInfoWindowMode mode = EInfoWindowMode::AUTO) const;
  52. //unified helper to show a specific window
  53. static void openWindow(const EOpenWindowMode type, const int id1, const int id2 = -1);
  54. //unified interface, AI helpers
  55. virtual bool wasVisited (PlayerColor player) const;
  56. virtual bool wasVisited (const CGHeroInstance * h) const;
  57. static void preInit(); //called before objs receive their initObj
  58. static void postInit();//called after objs receive their initObj
  59. template <typename Handler> void serialize(Handler &h, const int version)
  60. {
  61. logGlobal->error("IObjectInterface serialized, unexpected, should not happen!");
  62. }
  63. };
  64. class DLL_LINKAGE IBoatGenerator
  65. {
  66. public:
  67. const CGObjectInstance *o;
  68. IBoatGenerator(const CGObjectInstance *O);
  69. virtual ~IBoatGenerator() = default;
  70. virtual BoatId getBoatType() const; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  71. virtual void getOutOffsets(std::vector<int3> &offsets) const =0; //offsets to obj pos when we boat can be placed
  72. int3 bestLocation() const; //returns location when the boat should be placed
  73. enum EGeneratorState {GOOD, BOAT_ALREADY_BUILT, TILE_BLOCKED, NO_WATER};
  74. EGeneratorState shipyardStatus() const; //0 - can buid, 1 - there is already a boat at dest tile, 2 - dest tile is blocked, 3 - no water
  75. void getProblemText(MetaString &out, const CGHeroInstance *visitor = nullptr) const;
  76. template <typename Handler> void serialize(Handler &h, const int version)
  77. {
  78. h & o;
  79. }
  80. };
  81. class DLL_LINKAGE IShipyard : public IBoatGenerator
  82. {
  83. public:
  84. IShipyard(const CGObjectInstance *O);
  85. virtual void getBoatCost(TResources & cost) const;
  86. static const IShipyard *castFrom(const CGObjectInstance *obj);
  87. static IShipyard *castFrom(CGObjectInstance *obj);
  88. template <typename Handler> void serialize(Handler &h, const int version)
  89. {
  90. h & static_cast<IBoatGenerator&>(*this);
  91. }
  92. };
  93. class DLL_LINKAGE CGObjectInstance : public IObjectInterface
  94. {
  95. public:
  96. /// Position of bottom-right corner of object on map
  97. int3 pos;
  98. /// Type of object, e.g. town, hero, creature.
  99. Obj ID;
  100. /// Subtype of object, depends on type
  101. si32 subID;
  102. /// Current owner of an object (when below PLAYER_LIMIT)
  103. PlayerColor tempOwner;
  104. /// Index of object in map's list of objects
  105. ObjectInstanceID id;
  106. /// Defines appearance of object on map (animation, blocked tiles, blit order, etc)
  107. std::shared_ptr<const ObjectTemplate> appearance;
  108. /// If true hero can visit this object only from neighbouring tiles and can't stand on this object
  109. bool blockVisit;
  110. std::string instanceName;
  111. std::string typeName;
  112. std::string subTypeName;
  113. CGObjectInstance(); //TODO: remove constructor
  114. ~CGObjectInstance() override;
  115. int32_t getObjGroupIndex() const override;
  116. int32_t getObjTypeIndex() const override;
  117. /// "center" tile from which the sight distance is calculated
  118. int3 getSightCenter() const;
  119. PlayerColor getOwner() const override
  120. {
  121. return this->tempOwner;
  122. }
  123. void setOwner(const PlayerColor & ow);
  124. /** APPEARANCE ACCESSORS **/
  125. int getWidth() const; //returns width of object graphic in tiles
  126. int getHeight() const; //returns height of object graphic in tiles
  127. bool visitableAt(int x, int y) const; //returns true if object is visitable at location (x, y) (h3m pos)
  128. int3 visitablePos() const override;
  129. int3 getPosition() const override;
  130. int3 getTopVisiblePos() const;
  131. bool blockingAt(int x, int y) const; //returns true if object is blocking location (x, y) (h3m pos)
  132. bool coveringAt(int x, int y) const; //returns true if object covers with picture location (x, y) (h3m pos)
  133. std::set<int3> getBlockedPos() const; //returns set of positions blocked by this object
  134. std::set<int3> getBlockedOffsets() const; //returns set of relative positions blocked by this object
  135. bool isVisitable() const; //returns true if object is visitable
  136. virtual BattleField getBattlefield() const;
  137. virtual bool isTile2Terrain() const { return false; }
  138. std::optional<std::string> getAmbientSound() const;
  139. std::optional<std::string> getVisitSound() const;
  140. std::optional<std::string> getRemovalSound() const;
  141. /** VIRTUAL METHODS **/
  142. /// Returns true if player can pass through visitable tiles of this object
  143. virtual bool passableFor(PlayerColor color) const;
  144. /// Range of revealed map around this object, counting from getSightCenter()
  145. virtual int getSightRadius() const;
  146. /// returns (x,y,0) offset to a visitable tile of object
  147. virtual int3 getVisitableOffset() const;
  148. /// Called mostly during map randomization to turn random object into a regular one (e.g. "Random Monster" into "Pikeman")
  149. virtual void setType(si32 ID, si32 subID);
  150. /// returns text visible in status bar with specific hero/player active.
  151. /// Returns generic name of object, without any player-specific info
  152. virtual std::string getObjectName() const;
  153. /// Returns hover name for situation when there are no selected heroes. Default = object name
  154. virtual std::string getHoverText(PlayerColor player) const;
  155. /// Returns hero-specific hover name, including visited/not visited info. Default = player-specific name
  156. virtual std::string getHoverText(const CGHeroInstance * hero) const;
  157. /** OVERRIDES OF IObjectInterface **/
  158. void initObj(CRandomGenerator & rand) override;
  159. void onHeroVisit(const CGHeroInstance * h) const override;
  160. /// method for synchronous update. Note: For new properties classes should override setPropertyDer instead
  161. void setProperty(ui8 what, ui32 val) final;
  162. virtual void afterAddToMap(CMap * map);
  163. virtual void afterRemoveFromMap(CMap * map);
  164. ///Entry point of binary (de-)serialization
  165. template <typename Handler> void serialize(Handler &h, const int version)
  166. {
  167. h & instanceName;
  168. h & typeName;
  169. h & subTypeName;
  170. h & pos;
  171. h & ID;
  172. h & subID;
  173. h & id;
  174. h & tempOwner;
  175. h & blockVisit;
  176. h & appearance;
  177. //definfo is handled by map serializer
  178. }
  179. ///Entry point of Json (de-)serialization
  180. void serializeJson(JsonSerializeFormat & handler);
  181. virtual void updateFrom(const JsonNode & data);
  182. protected:
  183. /// virtual method that allows synchronously update object state on server and all clients
  184. virtual void setPropertyDer(ui8 what, ui32 val);
  185. /// Gives dummy bonus from this object to hero. Can be used to track visited state
  186. void giveDummyBonus(const ObjectInstanceID & heroID, ui8 duration = Bonus::ONE_DAY) const;
  187. ///Serialize object-type specific options
  188. virtual void serializeJsonOptions(JsonSerializeFormat & handler);
  189. void serializeJsonOwner(JsonSerializeFormat & handler);
  190. };
  191. /// function object which can be used to find an object with an specific sub ID
  192. class CGObjectInstanceBySubIdFinder
  193. {
  194. public:
  195. CGObjectInstanceBySubIdFinder(CGObjectInstance * obj);
  196. bool operator()(CGObjectInstance * obj) const;
  197. private:
  198. CGObjectInstance * obj;
  199. };
  200. class DLL_LINKAGE CObjectHandler
  201. {
  202. public:
  203. std::vector<ui32> resVals; //default values of resources in gold
  204. CObjectHandler();
  205. template <typename Handler> void serialize(Handler &h, const int version)
  206. {
  207. h & resVals;
  208. }
  209. };
  210. VCMI_LIB_NAMESPACE_END