CObjectHandler.h 6.8 KB

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