CObjectHandler.h 7.6 KB

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