CObjectHandler.h 8.4 KB

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