MiscObjects.h 13 KB

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