2
0

CObjectHandler.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. 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. bool isTile2Terrain() const
  132. {
  133. return ID.num == Obj::CLOVER_FIELD
  134. || ID.num == Obj::CURSED_GROUND1
  135. || ID.num == Obj::CURSED_GROUND2
  136. || ID.num == Obj::EVIL_FOG
  137. || ID.num == Obj::FAVORABLE_WINDS
  138. || ID.num == Obj::FIERY_FIELDS
  139. || ID.num == Obj::HOLY_GROUNDS
  140. || ID.num == Obj::LUCID_POOLS
  141. || ID.num == Obj::MAGIC_CLOUDS
  142. || ID.num == Obj::MAGIC_PLAINS1
  143. || ID.num == Obj::MAGIC_PLAINS2
  144. || ID.num == Obj::ROCKLANDS;
  145. }
  146. boost::optional<std::string> getAmbientSound() const;
  147. boost::optional<std::string> getVisitSound() const;
  148. boost::optional<std::string> getRemovalSound() const;
  149. /** VIRTUAL METHODS **/
  150. /// Returns true if player can pass through visitable tiles of this object
  151. virtual bool passableFor(PlayerColor color) const;
  152. /// Range of revealed map around this object, counting from getSightCenter()
  153. virtual int getSightRadius() const;
  154. /// returns (x,y,0) offset to a visitable tile of object
  155. virtual int3 getVisitableOffset() const;
  156. /// Called mostly during map randomization to turn random object into a regular one (e.g. "Random Monster" into "Pikeman")
  157. virtual void setType(si32 ID, si32 subID);
  158. /// returns text visible in status bar with specific hero/player active.
  159. /// Returns generic name of object, without any player-specific info
  160. virtual std::string getObjectName() const;
  161. /// Returns hover name for situation when there are no selected heroes. Default = object name
  162. virtual std::string getHoverText(PlayerColor player) const;
  163. /// Returns hero-specific hover name, including visited/not visited info. Default = player-specific name
  164. virtual std::string getHoverText(const CGHeroInstance * hero) const;
  165. /** OVERRIDES OF IObjectInterface **/
  166. void initObj(CRandomGenerator & rand) override;
  167. void onHeroVisit(const CGHeroInstance * h) const override;
  168. /// method for synchronous update. Note: For new properties classes should override setPropertyDer instead
  169. void setProperty(ui8 what, ui32 val) override final;
  170. virtual void afterAddToMap(CMap * map);
  171. ///Entry point of binary (de-)serialization
  172. template <typename Handler> void serialize(Handler &h, const int version)
  173. {
  174. if(version >= 759)
  175. {
  176. h & instanceName;
  177. h & typeName;
  178. h & subTypeName;
  179. }
  180. h & pos;
  181. h & ID;
  182. h & subID;
  183. h & id;
  184. h & tempOwner;
  185. h & blockVisit;
  186. h & appearance;
  187. //definfo is handled by map serializer
  188. }
  189. ///Entry point of Json (de-)serialization
  190. void serializeJson(JsonSerializeFormat & handler);
  191. virtual void updateFrom(const JsonNode & data);
  192. protected:
  193. /// virtual method that allows synchronously update object state on server and all clients
  194. virtual void setPropertyDer(ui8 what, ui32 val);
  195. /// Gives dummy bonus from this object to hero. Can be used to track visited state
  196. void giveDummyBonus(ObjectInstanceID heroID, ui8 duration = Bonus::ONE_DAY) const;
  197. ///Serialize object-type specific options
  198. virtual void serializeJsonOptions(JsonSerializeFormat & handler);
  199. void serializeJsonOwner(JsonSerializeFormat & handler);
  200. };
  201. /// function object which can be used to find an object with an specific sub ID
  202. class CGObjectInstanceBySubIdFinder
  203. {
  204. public:
  205. CGObjectInstanceBySubIdFinder(CGObjectInstance * obj);
  206. bool operator()(CGObjectInstance * obj) const;
  207. private:
  208. CGObjectInstance * obj;
  209. };
  210. class DLL_LINKAGE CObjectHandler
  211. {
  212. public:
  213. std::vector<ui32> resVals; //default values of resources in gold
  214. CObjectHandler();
  215. template <typename Handler> void serialize(Handler &h, const int version)
  216. {
  217. h & resVals;
  218. }
  219. };