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. VCMI_LIB_NAMESPACE_BEGIN
  13. class CMap;
  14. // This one teleport-specific, but has to be available everywhere in callbacks and netpacks
  15. // For now it's will be there till teleports code refactored and moved into own file
  16. using TTeleportExitsList = std::vector<std::pair<ObjectInstanceID, int3>>;
  17. /// Legacy class, use CRewardableObject instead
  18. class DLL_LINKAGE CTeamVisited: public CGObjectInstance
  19. {
  20. public:
  21. std::set<PlayerColor> players; //players that visited this object
  22. bool wasVisited (const CGHeroInstance * h) const override;
  23. bool wasVisited(PlayerColor player) const override;
  24. bool wasVisited(const TeamID & team) const;
  25. void setPropertyDer(ui8 what, ui32 val) override;
  26. template <typename Handler> void serialize(Handler &h, const int version)
  27. {
  28. h & static_cast<CGObjectInstance&>(*this);
  29. h & players;
  30. }
  31. static constexpr int OBJPROP_VISITED = 10;
  32. };
  33. class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  34. {
  35. public:
  36. std::string message;
  37. void onHeroVisit(const CGHeroInstance * h) const override;
  38. void initObj(CRandomGenerator & rand) override;
  39. template <typename Handler> void serialize(Handler &h, const int version)
  40. {
  41. h & static_cast<CGObjectInstance&>(*this);
  42. h & message;
  43. }
  44. protected:
  45. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  46. };
  47. class DLL_LINKAGE CGWitchHut : public CTeamVisited
  48. {
  49. public:
  50. std::set<SecondarySkill> allowedAbilities;
  51. SecondarySkill ability;
  52. std::string getHoverText(PlayerColor player) const override;
  53. std::string getHoverText(const CGHeroInstance * hero) const override;
  54. void onHeroVisit(const CGHeroInstance * h) const override;
  55. void initObj(CRandomGenerator & rand) override;
  56. template <typename Handler> void serialize(Handler &h, const int version)
  57. {
  58. h & static_cast<CTeamVisited&>(*this);
  59. h & allowedAbilities;
  60. h & ability;
  61. }
  62. protected:
  63. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  64. };
  65. class DLL_LINKAGE CGScholar : public CGObjectInstance
  66. {
  67. public:
  68. enum EBonusType {PRIM_SKILL, SECONDARY_SKILL, SPELL, RANDOM = 255};
  69. EBonusType bonusType;
  70. ui16 bonusID; //ID of skill/spell
  71. CGScholar() : bonusType(EBonusType::RANDOM),bonusID(0){};
  72. void onHeroVisit(const CGHeroInstance * h) const override;
  73. void initObj(CRandomGenerator & rand) override;
  74. template <typename Handler> void serialize(Handler &h, const int version)
  75. {
  76. h & static_cast<CGObjectInstance&>(*this);
  77. h & bonusType;
  78. h & bonusID;
  79. }
  80. protected:
  81. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  82. };
  83. class DLL_LINKAGE CGGarrison : public CArmedInstance
  84. {
  85. public:
  86. bool removableUnits;
  87. bool passableFor(PlayerColor color) const override;
  88. void onHeroVisit(const CGHeroInstance * h) const override;
  89. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  90. template <typename Handler> void serialize(Handler &h, const int version)
  91. {
  92. h & static_cast<CArmedInstance&>(*this);
  93. h & removableUnits;
  94. }
  95. protected:
  96. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  97. };
  98. class DLL_LINKAGE CGArtifact : public CArmedInstance
  99. {
  100. public:
  101. CArtifactInstance * storedArtifact = nullptr;
  102. std::string message;
  103. void onHeroVisit(const CGHeroInstance * h) const override;
  104. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  105. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  106. std::string getObjectName() const override;
  107. void pick( const CGHeroInstance * h ) const;
  108. void initObj(CRandomGenerator & rand) override;
  109. void afterAddToMap(CMap * map) override;
  110. BattleField getBattlefield() const override;
  111. template <typename Handler> void serialize(Handler &h, const int version)
  112. {
  113. h & static_cast<CArmedInstance&>(*this);
  114. h & message;
  115. h & storedArtifact;
  116. }
  117. protected:
  118. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  119. };
  120. class DLL_LINKAGE CGResource : public CArmedInstance
  121. {
  122. public:
  123. static constexpr ui32 RANDOM_AMOUNT = 0;
  124. ui32 amount = RANDOM_AMOUNT; //0 if random
  125. std::string message;
  126. void onHeroVisit(const CGHeroInstance * h) const override;
  127. void initObj(CRandomGenerator & rand) override;
  128. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  129. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  130. std::string getHoverText(PlayerColor player) const override;
  131. void collectRes(const PlayerColor & player) const;
  132. template <typename Handler> void serialize(Handler &h, const int version)
  133. {
  134. h & static_cast<CArmedInstance&>(*this);
  135. h & amount;
  136. h & message;
  137. }
  138. protected:
  139. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  140. };
  141. class DLL_LINKAGE CGShrine : public CTeamVisited
  142. {
  143. public:
  144. MetaString visitText;
  145. SpellID spell; //id of spell or NONE if random
  146. void onHeroVisit(const CGHeroInstance * h) const override;
  147. void initObj(CRandomGenerator & rand) override;
  148. std::string getHoverText(PlayerColor player) const override;
  149. std::string getHoverText(const CGHeroInstance * hero) const override;
  150. template <typename Handler> void serialize(Handler &h, const int version)
  151. {
  152. h & static_cast<CTeamVisited&>(*this);;
  153. h & spell;
  154. h & visitText;
  155. }
  156. protected:
  157. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  158. };
  159. class DLL_LINKAGE CGMine : public CArmedInstance
  160. {
  161. public:
  162. GameResID producedResource;
  163. ui32 producedQuantity;
  164. std::set<GameResID> abandonedMineResources;
  165. private:
  166. void onHeroVisit(const CGHeroInstance * h) const override;
  167. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  168. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  169. void flagMine(const PlayerColor & player) const;
  170. void newTurn(CRandomGenerator & rand) const override;
  171. void initObj(CRandomGenerator & rand) override;
  172. std::string getObjectName() const override;
  173. std::string getHoverText(PlayerColor player) const override;
  174. bool isAbandoned() const;
  175. public:
  176. template <typename Handler> void serialize(Handler &h, const int version)
  177. {
  178. h & static_cast<CArmedInstance&>(*this);
  179. h & producedResource;
  180. h & producedQuantity;
  181. h & abandonedMineResources;
  182. }
  183. ui32 defaultResProduction() const;
  184. protected:
  185. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  186. };
  187. struct DLL_LINKAGE TeleportChannel
  188. {
  189. enum EPassability {UNKNOWN, IMPASSABLE, PASSABLE};
  190. std::vector<ObjectInstanceID> entrances;
  191. std::vector<ObjectInstanceID> exits;
  192. EPassability passability = EPassability::UNKNOWN;
  193. template <typename Handler> void serialize(Handler &h, const int version)
  194. {
  195. h & entrances;
  196. h & exits;
  197. h & passability;
  198. }
  199. };
  200. class DLL_LINKAGE CGTeleport : public CGObjectInstance
  201. {
  202. bool isChannelEntrance(const ObjectInstanceID & id) const;
  203. bool isChannelExit(const ObjectInstanceID & id) const;
  204. std::vector<ObjectInstanceID> getAllEntrances(bool excludeCurrent = false) const;
  205. protected:
  206. enum EType {UNKNOWN, ENTRANCE, EXIT, BOTH};
  207. EType type = EType::UNKNOWN;
  208. ObjectInstanceID getRandomExit(const CGHeroInstance * h) const;
  209. std::vector<ObjectInstanceID> getAllExits(bool excludeCurrent = false) const;
  210. public:
  211. TeleportChannelID channel;
  212. bool isEntrance() const;
  213. bool isExit() const;
  214. virtual void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const = 0;
  215. static bool isTeleport(const CGObjectInstance * dst);
  216. static bool isConnected(const CGTeleport * src, const CGTeleport * dst);
  217. static bool isConnected(const CGObjectInstance * src, const CGObjectInstance * dst);
  218. static void addToChannel(std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > &channelsList, const CGTeleport * obj);
  219. static std::vector<ObjectInstanceID> getPassableExits(CGameState * gs, const CGHeroInstance * h, std::vector<ObjectInstanceID> exits);
  220. static bool isExitPassable(CGameState * gs, const CGHeroInstance * h, const CGObjectInstance * obj);
  221. template <typename Handler> void serialize(Handler &h, const int version)
  222. {
  223. h & type;
  224. h & channel;
  225. h & static_cast<CGObjectInstance&>(*this);
  226. }
  227. };
  228. class DLL_LINKAGE CGMonolith : public CGTeleport
  229. {
  230. TeleportChannelID findMeChannel(const std::vector<Obj> & IDs, int SubID) const;
  231. protected:
  232. void onHeroVisit(const CGHeroInstance * h) const override;
  233. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  234. void initObj(CRandomGenerator & rand) override;
  235. public:
  236. template <typename Handler> void serialize(Handler &h, const int version)
  237. {
  238. h & static_cast<CGTeleport&>(*this);
  239. }
  240. };
  241. class DLL_LINKAGE CGSubterraneanGate : public CGMonolith
  242. {
  243. void onHeroVisit(const CGHeroInstance * h) const override;
  244. void initObj(CRandomGenerator & rand) override;
  245. public:
  246. static void postInit();
  247. template <typename Handler> void serialize(Handler &h, const int version)
  248. {
  249. h & static_cast<CGMonolith&>(*this);
  250. }
  251. };
  252. class DLL_LINKAGE CGWhirlpool : public CGMonolith
  253. {
  254. void onHeroVisit(const CGHeroInstance * h) const override;
  255. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  256. static bool isProtected( const CGHeroInstance * h );
  257. public:
  258. template <typename Handler> void serialize(Handler &h, const int version)
  259. {
  260. h & static_cast<CGMonolith&>(*this);
  261. }
  262. };
  263. class DLL_LINKAGE CGSirens : public CGObjectInstance
  264. {
  265. public:
  266. void onHeroVisit(const CGHeroInstance * h) const override;
  267. std::string getHoverText(const CGHeroInstance * hero) const override;
  268. void initObj(CRandomGenerator & rand) override;
  269. template <typename Handler> void serialize(Handler &h, const int version)
  270. {
  271. h & static_cast<CGObjectInstance&>(*this);
  272. }
  273. };
  274. class DLL_LINKAGE CGObservatory : public CGObjectInstance //Redwood observatory
  275. {
  276. public:
  277. void onHeroVisit(const CGHeroInstance * h) const override;
  278. template <typename Handler> void serialize(Handler &h, const int version)
  279. {
  280. h & static_cast<CGObjectInstance&>(*this);
  281. }
  282. };
  283. class DLL_LINKAGE CGBoat : public CGObjectInstance, public CBonusSystemNode
  284. {
  285. public:
  286. ui8 direction;
  287. const CGHeroInstance *hero; //hero on board
  288. bool onboardAssaultAllowed; //if true, hero can attack units from transport
  289. bool onboardVisitAllowed; //if true, hero can visit objects from transport
  290. EPathfindingLayer::EEPathfindingLayer layer;
  291. //animation filenames. If empty - animations won't be used
  292. std::string actualAnimation; //for OH3 boats those have actual animations
  293. std::string overlayAnimation; //waves animations
  294. std::array<std::string, PlayerColor::PLAYER_LIMIT_I> flagAnimations;
  295. CGBoat();
  296. void initObj(CRandomGenerator & rand) override;
  297. static int3 translatePos(const int3 &pos, bool reverse = false);
  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