CObjectHandler.h 8.8 KB

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