MiscObjects.h 14 KB

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