CObjectHandler.h 7.9 KB

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