MiscObjects.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * MiscObjects.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 "IOwnableObject.h"
  12. #include "army/CArmedInstance.h"
  13. #include "../entities/artifact/CArtifactInstance.h"
  14. #include "../texts/MetaString.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class CMap;
  17. class UpgradeInfo;
  18. class MineInstanceConstructor;
  19. // This one teleport-specific, but has to be available everywhere in callbacks and netpacks
  20. // For now it's will be there till teleports code refactored and moved into own file
  21. using TTeleportExitsList = std::vector<std::pair<ObjectInstanceID, int3>>;
  22. /// Legacy class, use CRewardableObject instead
  23. class DLL_LINKAGE CTeamVisited: public CGObjectInstance
  24. {
  25. public:
  26. using CGObjectInstance::CGObjectInstance;
  27. std::set<PlayerColor> players; //players that visited this object
  28. bool wasVisited (const CGHeroInstance * h) const override;
  29. bool wasVisited(PlayerColor player) const override;
  30. bool wasVisited(const TeamID & team) const;
  31. void setPropertyDer(ObjProperty what, ObjPropertyID identifier) override;
  32. template <typename Handler> void serialize(Handler &h)
  33. {
  34. h & static_cast<CGObjectInstance&>(*this);
  35. h & players;
  36. }
  37. };
  38. class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  39. {
  40. public:
  41. using CGObjectInstance::CGObjectInstance;
  42. MetaString message;
  43. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  44. void initObj(IGameRandomizer & gameRandomizer) override;
  45. template <typename Handler> void serialize(Handler &h)
  46. {
  47. h & static_cast<CGObjectInstance&>(*this);
  48. h & message;
  49. }
  50. protected:
  51. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  52. };
  53. class DLL_LINKAGE CGGarrison : public CArmedInstance, public IOwnableObject
  54. {
  55. public:
  56. using CArmedInstance::CArmedInstance;
  57. bool removableUnits;
  58. void initObj(IGameRandomizer & gameRandomizer) override;
  59. bool passableFor(PlayerColor color) const override;
  60. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  61. void battleFinished(IGameEventCallback & gameEvents, const CGHeroInstance *hero, const BattleResult &result) const override;
  62. const IOwnableObject * asOwnable() const final;
  63. ResourceSet dailyIncome() const override;
  64. std::vector<CreatureID> providedCreatures() const override;
  65. template <typename Handler> void serialize(Handler &h)
  66. {
  67. h & static_cast<CArmedInstance&>(*this);
  68. h & removableUnits;
  69. }
  70. protected:
  71. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  72. void addAntimagicGarrisonBonus();
  73. };
  74. class DLL_LINKAGE CGArtifact : public CArmedInstance
  75. {
  76. ArtifactInstanceID storedArtifact;
  77. public:
  78. using CArmedInstance::CArmedInstance;
  79. MetaString message;
  80. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  81. void battleFinished(IGameEventCallback & gameEvents, const CGHeroInstance *hero, const BattleResult &result) const override;
  82. void blockingDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, int32_t answer) const override;
  83. std::string getObjectName() const override;
  84. std::string getPopupText(PlayerColor player) const override;
  85. std::string getPopupText(const CGHeroInstance * hero) const override;
  86. std::vector<Component> getPopupComponents(PlayerColor player) const override;
  87. void pick(IGameEventCallback & gameEvents, const CGHeroInstance * h) const;
  88. void initObj(IGameRandomizer & gameRandomizer) override;
  89. void pickRandomObject(IGameRandomizer & gameRandomizer) override;
  90. BattleField getBattlefield() const override;
  91. ArtifactID getArtifactType() const;
  92. const CArtifactInstance * getArtifactInstance() const;
  93. void setArtifactInstance(const CArtifactInstance *);
  94. template <typename Handler> void serialize(Handler &h)
  95. {
  96. h & static_cast<CArmedInstance&>(*this);
  97. h & message;
  98. if (h.saving || h.hasFeature(Handler::Version::NO_RAW_POINTERS_IN_SERIALIZER))
  99. {
  100. h & storedArtifact;
  101. }
  102. else
  103. {
  104. std::shared_ptr<CArtifactInstance> pointer;
  105. h & pointer;
  106. if (pointer->getId() == ArtifactInstanceID())
  107. CArtifactInstance::saveCompatibilityFixArtifactID(pointer);
  108. storedArtifact = pointer->getId();
  109. }
  110. }
  111. protected:
  112. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  113. };
  114. class DLL_LINKAGE CGMine : public CArmedInstance, public IOwnableObject
  115. {
  116. public:
  117. struct InitialGuards
  118. {
  119. CreatureID creature = CreatureID::TROGLODYTES;
  120. int minAmount = 100;
  121. int maxAmount = 199;
  122. template <typename Handler> void serialize(Handler &h)
  123. {
  124. h & creature;
  125. h & minAmount;
  126. h & maxAmount;
  127. }
  128. };
  129. GameResID producedResource;
  130. ui32 producedQuantity;
  131. std::set<GameResID> abandonedMineResources;
  132. InitialGuards abandonedMineGuards;
  133. bool isAbandoned() const;
  134. std::shared_ptr<MineInstanceConstructor> getResourceHandler() const;
  135. private:
  136. using CArmedInstance::CArmedInstance;
  137. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  138. void battleFinished(IGameEventCallback & gameEvents, const CGHeroInstance *hero, const BattleResult &result) const override;
  139. void blockingDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, int32_t answer) const override;
  140. void flagMine(IGameEventCallback & gameEvents, const PlayerColor & player) const;
  141. void initObj(IGameRandomizer & gameRandomizer) override;
  142. std::string getObjectName() const override;
  143. std::string getHoverText(PlayerColor player) const override;
  144. public:
  145. template <typename Handler> void serialize(Handler &h)
  146. {
  147. h & static_cast<CArmedInstance&>(*this);
  148. h & producedResource;
  149. h & producedQuantity;
  150. h & abandonedMineResources;
  151. if(h.version >= Handler::Version::HOTA_MAP_FORMAT_EXTENSIONS_2)
  152. h & abandonedMineGuards;
  153. }
  154. ui32 defaultResProduction() const;
  155. ui32 getProducedQuantity() const;
  156. const IOwnableObject * asOwnable() const final;
  157. ResourceSet dailyIncome() const override;
  158. std::vector<CreatureID> providedCreatures() const override;
  159. protected:
  160. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  161. };
  162. struct DLL_LINKAGE TeleportChannel : public Serializeable
  163. {
  164. enum EPassability {UNKNOWN, IMPASSABLE, PASSABLE};
  165. std::vector<ObjectInstanceID> entrances;
  166. std::vector<ObjectInstanceID> exits;
  167. EPassability passability = EPassability::UNKNOWN;
  168. template <typename Handler> void serialize(Handler &h)
  169. {
  170. h & entrances;
  171. h & exits;
  172. h & passability;
  173. }
  174. };
  175. class DLL_LINKAGE CGTeleport : public CGObjectInstance
  176. {
  177. bool isChannelEntrance(const ObjectInstanceID & id) const;
  178. bool isChannelExit(const ObjectInstanceID & id) const;
  179. protected:
  180. enum EType {UNKNOWN, ENTRANCE, EXIT, BOTH};
  181. EType type = EType::UNKNOWN;
  182. ObjectInstanceID getRandomExit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const;
  183. public:
  184. using CGObjectInstance::CGObjectInstance;
  185. TeleportChannelID channel;
  186. bool isEntrance() const;
  187. bool isExit() const;
  188. std::vector<ObjectInstanceID> getAllEntrances(bool excludeCurrent = false) const;
  189. std::vector<ObjectInstanceID> getAllExits(bool excludeCurrent = false) const;
  190. virtual void teleportDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const = 0;
  191. static bool isTeleport(const CGObjectInstance * dst);
  192. static bool isConnected(const CGTeleport * src, const CGTeleport * dst);
  193. static bool isConnected(const CGObjectInstance * src, const CGObjectInstance * dst);
  194. static void addToChannel(std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > &channelsList, const CGTeleport * obj);
  195. static std::vector<ObjectInstanceID> getPassableExits(const IGameInfoCallback & gameInfo, const CGHeroInstance * h, std::vector<ObjectInstanceID> exits);
  196. static bool isExitPassable(const IGameInfoCallback & gameInfo, const CGHeroInstance * h, const CGObjectInstance * obj);
  197. template <typename Handler> void serialize(Handler &h)
  198. {
  199. h & type;
  200. h & channel;
  201. h & static_cast<CGObjectInstance&>(*this);
  202. }
  203. };
  204. class DLL_LINKAGE CGMonolith : public CGTeleport
  205. {
  206. TeleportChannelID findMeChannel(const std::vector<Obj> & IDs, MapObjectSubID SubID) const;
  207. protected:
  208. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  209. void teleportDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  210. void initObj(IGameRandomizer & gameRandomizer) override;
  211. public:
  212. using CGTeleport::CGTeleport;
  213. template <typename Handler> void serialize(Handler &h)
  214. {
  215. h & static_cast<CGTeleport&>(*this);
  216. }
  217. };
  218. class DLL_LINKAGE CGSubterraneanGate : public CGMonolith
  219. {
  220. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  221. void initObj(IGameRandomizer & gameRandomizer) override;
  222. public:
  223. using CGMonolith::CGMonolith;
  224. static void postInit(IGameInfoCallback * cb);
  225. template <typename Handler> void serialize(Handler &h)
  226. {
  227. h & static_cast<CGMonolith&>(*this);
  228. }
  229. };
  230. class DLL_LINKAGE CGWhirlpool : public CGMonolith
  231. {
  232. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  233. void teleportDialogAnswered(IGameEventCallback & gameEvents, const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  234. static bool isProtected( const CGHeroInstance * h );
  235. public:
  236. using CGMonolith::CGMonolith;
  237. template <typename Handler> void serialize(Handler &h)
  238. {
  239. h & static_cast<CGMonolith&>(*this);
  240. }
  241. };
  242. class DLL_LINKAGE CGSirens : public CGObjectInstance
  243. {
  244. public:
  245. using CGObjectInstance::CGObjectInstance;
  246. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  247. std::string getHoverText(const CGHeroInstance * hero) const override;
  248. void initObj(IGameRandomizer & gameRandomizer) override;
  249. template <typename Handler> void serialize(Handler &h)
  250. {
  251. h & static_cast<CGObjectInstance&>(*this);
  252. }
  253. };
  254. class DLL_LINKAGE CGBoat : public CGObjectInstance, public CBonusSystemNode
  255. {
  256. ObjectInstanceID boardedHeroID;
  257. public:
  258. using CGObjectInstance::CGObjectInstance;
  259. ui8 direction;
  260. bool onboardAssaultAllowed; //if true, hero can attack units from transport
  261. bool onboardVisitAllowed; //if true, hero can visit objects from transport
  262. EPathfindingLayer layer;
  263. //animation filenames. If empty - animations won't be used
  264. AnimationPath actualAnimation; //for OH3 boats those have actual animations
  265. AnimationPath overlayAnimation; //waves animations
  266. std::array<AnimationPath, PlayerColor::PLAYER_LIMIT_I> flagAnimations;
  267. CGBoat(IGameInfoCallback * cb);
  268. bool isCoastVisitable() const override;
  269. void setBoardedHero(const CGHeroInstance * hero);
  270. const CGHeroInstance * getBoardedHero() const;
  271. template <typename Handler> void serialize(Handler &h)
  272. {
  273. h & static_cast<CGObjectInstance&>(*this);
  274. h & static_cast<CBonusSystemNode&>(*this);
  275. h & direction;
  276. if (h.hasFeature(Handler::Version::NO_RAW_POINTERS_IN_SERIALIZER))
  277. {
  278. h & boardedHeroID;
  279. }
  280. else
  281. {
  282. std::shared_ptr<CGObjectInstance> ptr;
  283. h & ptr;
  284. boardedHeroID = ptr ? ptr->id : ObjectInstanceID();
  285. }
  286. h & layer;
  287. h & onboardAssaultAllowed;
  288. h & onboardVisitAllowed;
  289. h & actualAnimation;
  290. h & overlayAnimation;
  291. h & flagAnimations;
  292. }
  293. };
  294. class DLL_LINKAGE CGShipyard : public CGObjectInstance, public IShipyard, public IOwnableObject
  295. {
  296. friend class ShipyardInstanceConstructor;
  297. BoatId createdBoat;
  298. protected:
  299. void getOutOffsets(std::vector<int3> & offsets) const override;
  300. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  301. const IObjectInterface * getObject() const override;
  302. BoatId getBoatType() const override;
  303. const IOwnableObject * asOwnable() const final;
  304. ResourceSet dailyIncome() const override;
  305. std::vector<CreatureID> providedCreatures() const override;
  306. public:
  307. using CGObjectInstance::CGObjectInstance;
  308. template<typename Handler> void serialize(Handler & h)
  309. {
  310. h & static_cast<CGObjectInstance&>(*this);
  311. h & createdBoat;
  312. }
  313. protected:
  314. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  315. };
  316. class DLL_LINKAGE CGMagi : public CGObjectInstance
  317. {
  318. public:
  319. using CGObjectInstance::CGObjectInstance;
  320. void initObj(IGameRandomizer & gameRandomizer) override;
  321. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  322. template <typename Handler> void serialize(Handler &h)
  323. {
  324. h & static_cast<CGObjectInstance&>(*this);
  325. }
  326. };
  327. class DLL_LINKAGE CGDenOfthieves : public CGObjectInstance
  328. {
  329. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  330. public:
  331. using CGObjectInstance::CGObjectInstance;
  332. };
  333. class DLL_LINKAGE CGObelisk : public CTeamVisited
  334. {
  335. public:
  336. using CTeamVisited::CTeamVisited;
  337. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  338. void initObj(IGameRandomizer & gameRandomizer) override;
  339. std::string getHoverText(PlayerColor player) const override;
  340. std::string getObjectDescription(PlayerColor player) const;
  341. template <typename Handler> void serialize(Handler &h)
  342. {
  343. h & static_cast<CTeamVisited&>(*this);
  344. }
  345. protected:
  346. void setPropertyDer(ObjProperty what, ObjPropertyID identifier) override;
  347. };
  348. class DLL_LINKAGE CGTerrainPatch : public CGObjectInstance
  349. {
  350. public:
  351. using CGObjectInstance::CGObjectInstance;
  352. bool isTile2Terrain() const override
  353. {
  354. return true;
  355. }
  356. };
  357. class DLL_LINKAGE HillFort : public CGObjectInstance, public ICreatureUpgrader
  358. {
  359. friend class HillFortInstanceConstructor;
  360. std::vector<int> upgradeCostPercentage;
  361. protected:
  362. void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
  363. void fillUpgradeInfo(UpgradeInfo & info, const CStackInstance &stack) const override;
  364. public:
  365. using CGObjectInstance::CGObjectInstance;
  366. std::string getPopupText(PlayerColor player) const override;
  367. std::string getPopupText(const CGHeroInstance * hero) const override;
  368. std::string getDescriptionToolTip() const;
  369. std::string getUnavailableUpgradeMessage() const;
  370. template <typename Handler> void serialize(Handler &h)
  371. {
  372. h & static_cast<CGObjectInstance&>(*this);
  373. h & upgradeCostPercentage;
  374. }
  375. };
  376. VCMI_LIB_NAMESPACE_END