MiscObjects.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 "CObjectHandler.h"
  12. #include "CArmedInstance.h"
  13. #include "../ResourceSet.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CMap;
  16. /// Legacy class, use CRewardableObject instead
  17. class DLL_LINKAGE CTeamVisited: public CGObjectInstance
  18. {
  19. public:
  20. std::set<PlayerColor> players; //players that visited this object
  21. bool wasVisited (const CGHeroInstance * h) const override;
  22. bool wasVisited(PlayerColor player) const override;
  23. bool wasVisited(const TeamID & team) const;
  24. void setPropertyDer(ui8 what, ui32 val) override;
  25. template <typename Handler> void serialize(Handler &h, const int version)
  26. {
  27. h & static_cast<CGObjectInstance&>(*this);
  28. h & players;
  29. }
  30. static constexpr int OBJPROP_VISITED = 10;
  31. };
  32. class DLL_LINKAGE CGCreature : public CArmedInstance //creatures on map
  33. {
  34. public:
  35. enum Action {
  36. FIGHT = -2, FLEE = -1, JOIN_FOR_FREE = 0 //values > 0 mean gold price
  37. };
  38. enum Character {
  39. COMPLIANT = 0, FRIENDLY = 1, AGRESSIVE = 2, HOSTILE = 3, SAVAGE = 4
  40. };
  41. ui32 identifier; //unique code for this monster (used in missions)
  42. si8 character; //character of this set of creatures (0 - the most friendly, 4 - the most hostile) => on init changed to -4 (compliant) ... 10 value (savage)
  43. std::string message; //message printed for attacking hero
  44. TResources resources; // resources given to hero that has won with monsters
  45. ArtifactID gainedArtifact; //ID of artifact gained to hero, -1 if none
  46. bool neverFlees; //if true, the troops will never flee
  47. bool notGrowingTeam; //if true, number of units won't grow
  48. ui64 temppower; //used to handle fractional stack growth for tiny stacks
  49. bool refusedJoining;
  50. void onHeroVisit(const CGHeroInstance * h) const override;
  51. std::string getHoverText(PlayerColor player) const override;
  52. std::string getHoverText(const CGHeroInstance * hero) const override;
  53. void initObj(CRandomGenerator & rand) override;
  54. void newTurn(CRandomGenerator & rand) const override;
  55. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  56. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  57. //stack formation depends on position,
  58. bool containsUpgradedStack() const;
  59. int getNumberOfStacks(const CGHeroInstance *hero) const;
  60. struct DLL_LINKAGE formationInfo // info about merging stacks after battle back into one
  61. {
  62. si32 basicType;
  63. ui8 upgrade; //random seed used to determine number of stacks and is there's upgraded stack
  64. template <typename Handler> void serialize(Handler &h, const int version)
  65. {
  66. h & basicType;
  67. h & upgrade;
  68. }
  69. } formation;
  70. template <typename Handler> void serialize(Handler &h, const int version)
  71. {
  72. h & static_cast<CArmedInstance&>(*this);
  73. h & identifier;
  74. h & character;
  75. h & message;
  76. h & resources;
  77. h & gainedArtifact;
  78. h & neverFlees;
  79. h & notGrowingTeam;
  80. h & temppower;
  81. h & refusedJoining;
  82. h & formation;
  83. }
  84. protected:
  85. void setPropertyDer(ui8 what, ui32 val) override;
  86. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  87. private:
  88. void fight(const CGHeroInstance *h) const;
  89. void flee( const CGHeroInstance * h ) const;
  90. void fleeDecision(const CGHeroInstance *h, ui32 pursue) const;
  91. void joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const;
  92. int takenAction(const CGHeroInstance *h, bool allowJoin=true) const; //action on confrontation: -2 - fight, -1 - flee, >=0 - will join for given value of gold (may be 0)
  93. void giveReward(const CGHeroInstance * h) const;
  94. };
  95. class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  96. {
  97. public:
  98. std::string message;
  99. void onHeroVisit(const CGHeroInstance * h) const override;
  100. void initObj(CRandomGenerator & rand) override;
  101. template <typename Handler> void serialize(Handler &h, const int version)
  102. {
  103. h & static_cast<CGObjectInstance&>(*this);
  104. h & message;
  105. }
  106. protected:
  107. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  108. };
  109. class DLL_LINKAGE CGWitchHut : public CTeamVisited
  110. {
  111. public:
  112. std::set<si32> allowedAbilities;
  113. ui32 ability;
  114. std::string getHoverText(PlayerColor player) const override;
  115. std::string getHoverText(const CGHeroInstance * hero) const override;
  116. void onHeroVisit(const CGHeroInstance * h) const override;
  117. void initObj(CRandomGenerator & rand) override;
  118. template <typename Handler> void serialize(Handler &h, const int version)
  119. {
  120. h & static_cast<CTeamVisited&>(*this);
  121. h & allowedAbilities;
  122. h & ability;
  123. }
  124. protected:
  125. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  126. };
  127. class DLL_LINKAGE CGScholar : public CGObjectInstance
  128. {
  129. public:
  130. enum EBonusType {PRIM_SKILL, SECONDARY_SKILL, SPELL, RANDOM = 255};
  131. EBonusType bonusType;
  132. ui16 bonusID; //ID of skill/spell
  133. CGScholar() : bonusType(EBonusType::RANDOM),bonusID(0){};
  134. void onHeroVisit(const CGHeroInstance * h) const override;
  135. void initObj(CRandomGenerator & rand) override;
  136. template <typename Handler> void serialize(Handler &h, const int version)
  137. {
  138. h & static_cast<CGObjectInstance&>(*this);
  139. h & bonusType;
  140. h & bonusID;
  141. }
  142. protected:
  143. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  144. };
  145. class DLL_LINKAGE CGGarrison : public CArmedInstance
  146. {
  147. public:
  148. bool removableUnits;
  149. bool passableFor(PlayerColor color) const override;
  150. void onHeroVisit(const CGHeroInstance * h) const override;
  151. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  152. template <typename Handler> void serialize(Handler &h, const int version)
  153. {
  154. h & static_cast<CArmedInstance&>(*this);
  155. h & removableUnits;
  156. }
  157. protected:
  158. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  159. };
  160. class DLL_LINKAGE CGArtifact : public CArmedInstance
  161. {
  162. public:
  163. CArtifactInstance * storedArtifact = nullptr;
  164. std::string message;
  165. void onHeroVisit(const CGHeroInstance * h) const override;
  166. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  167. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  168. std::string getObjectName() const override;
  169. void pick( const CGHeroInstance * h ) const;
  170. void initObj(CRandomGenerator & rand) override;
  171. void afterAddToMap(CMap * map) override;
  172. BattleField getBattlefield() const override;
  173. template <typename Handler> void serialize(Handler &h, const int version)
  174. {
  175. h & static_cast<CArmedInstance&>(*this);
  176. h & message;
  177. h & storedArtifact;
  178. }
  179. protected:
  180. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  181. };
  182. class DLL_LINKAGE CGResource : public CArmedInstance
  183. {
  184. public:
  185. static constexpr ui32 RANDOM_AMOUNT = 0;
  186. ui32 amount = RANDOM_AMOUNT; //0 if random
  187. std::string message;
  188. void onHeroVisit(const CGHeroInstance * h) const override;
  189. void initObj(CRandomGenerator & rand) override;
  190. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  191. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  192. std::string getHoverText(PlayerColor player) const override;
  193. void collectRes(const PlayerColor & player) const;
  194. template <typename Handler> void serialize(Handler &h, const int version)
  195. {
  196. h & static_cast<CArmedInstance&>(*this);
  197. h & amount;
  198. h & message;
  199. }
  200. protected:
  201. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  202. };
  203. class DLL_LINKAGE CGShrine : public CTeamVisited
  204. {
  205. public:
  206. SpellID spell; //id of spell or NONE if random
  207. void onHeroVisit(const CGHeroInstance * h) const override;
  208. void initObj(CRandomGenerator & rand) override;
  209. std::string getHoverText(PlayerColor player) const override;
  210. std::string getHoverText(const CGHeroInstance * hero) const override;
  211. template <typename Handler> void serialize(Handler &h, const int version)
  212. {
  213. h & static_cast<CTeamVisited&>(*this);;
  214. h & spell;
  215. }
  216. protected:
  217. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  218. };
  219. class DLL_LINKAGE CGMine : public CArmedInstance
  220. {
  221. public:
  222. GameResID producedResource;
  223. ui32 producedQuantity;
  224. std::set<GameResID> abandonedMineResources;
  225. private:
  226. void onHeroVisit(const CGHeroInstance * h) const override;
  227. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  228. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  229. void flagMine(const PlayerColor & player) const;
  230. void newTurn(CRandomGenerator & rand) const override;
  231. void initObj(CRandomGenerator & rand) override;
  232. std::string getObjectName() const override;
  233. std::string getHoverText(PlayerColor player) const override;
  234. bool isAbandoned() const;
  235. public:
  236. template <typename Handler> void serialize(Handler &h, const int version)
  237. {
  238. h & static_cast<CArmedInstance&>(*this);
  239. h & producedResource;
  240. h & producedQuantity;
  241. h & abandonedMineResources;
  242. }
  243. ui32 defaultResProduction() const;
  244. protected:
  245. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  246. };
  247. struct DLL_LINKAGE TeleportChannel
  248. {
  249. enum EPassability {UNKNOWN, IMPASSABLE, PASSABLE};
  250. std::vector<ObjectInstanceID> entrances;
  251. std::vector<ObjectInstanceID> exits;
  252. EPassability passability = EPassability::UNKNOWN;
  253. template <typename Handler> void serialize(Handler &h, const int version)
  254. {
  255. h & entrances;
  256. h & exits;
  257. h & passability;
  258. }
  259. };
  260. class DLL_LINKAGE CGTeleport : public CGObjectInstance
  261. {
  262. bool isChannelEntrance(const ObjectInstanceID & id) const;
  263. bool isChannelExit(const ObjectInstanceID & id) const;
  264. std::vector<ObjectInstanceID> getAllEntrances(bool excludeCurrent = false) const;
  265. protected:
  266. enum EType {UNKNOWN, ENTRANCE, EXIT, BOTH};
  267. EType type = EType::UNKNOWN;
  268. ObjectInstanceID getRandomExit(const CGHeroInstance * h) const;
  269. std::vector<ObjectInstanceID> getAllExits(bool excludeCurrent = false) const;
  270. public:
  271. TeleportChannelID channel;
  272. bool isEntrance() const;
  273. bool isExit() const;
  274. virtual void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const = 0;
  275. static bool isTeleport(const CGObjectInstance * dst);
  276. static bool isConnected(const CGTeleport * src, const CGTeleport * dst);
  277. static bool isConnected(const CGObjectInstance * src, const CGObjectInstance * dst);
  278. static void addToChannel(std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > &channelsList, const CGTeleport * obj);
  279. static std::vector<ObjectInstanceID> getPassableExits(CGameState * gs, const CGHeroInstance * h, std::vector<ObjectInstanceID> exits);
  280. static bool isExitPassable(CGameState * gs, const CGHeroInstance * h, const CGObjectInstance * obj);
  281. template <typename Handler> void serialize(Handler &h, const int version)
  282. {
  283. h & type;
  284. h & channel;
  285. h & static_cast<CGObjectInstance&>(*this);
  286. }
  287. };
  288. class DLL_LINKAGE CGMonolith : public CGTeleport
  289. {
  290. TeleportChannelID findMeChannel(const std::vector<Obj> & IDs, int SubID) const;
  291. protected:
  292. void onHeroVisit(const CGHeroInstance * h) const override;
  293. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  294. void initObj(CRandomGenerator & rand) override;
  295. public:
  296. template <typename Handler> void serialize(Handler &h, const int version)
  297. {
  298. h & static_cast<CGTeleport&>(*this);
  299. }
  300. };
  301. class DLL_LINKAGE CGSubterraneanGate : public CGMonolith
  302. {
  303. void onHeroVisit(const CGHeroInstance * h) const override;
  304. void initObj(CRandomGenerator & rand) override;
  305. public:
  306. static void postInit();
  307. template <typename Handler> void serialize(Handler &h, const int version)
  308. {
  309. h & static_cast<CGMonolith&>(*this);
  310. }
  311. };
  312. class DLL_LINKAGE CGWhirlpool : public CGMonolith
  313. {
  314. void onHeroVisit(const CGHeroInstance * h) const override;
  315. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  316. static bool isProtected( const CGHeroInstance * h );
  317. public:
  318. template <typename Handler> void serialize(Handler &h, const int version)
  319. {
  320. h & static_cast<CGMonolith&>(*this);
  321. }
  322. };
  323. class DLL_LINKAGE CGSirens : public CGObjectInstance
  324. {
  325. public:
  326. void onHeroVisit(const CGHeroInstance * h) const override;
  327. std::string getHoverText(const CGHeroInstance * hero) const override;
  328. void initObj(CRandomGenerator & rand) override;
  329. template <typename Handler> void serialize(Handler &h, const int version)
  330. {
  331. h & static_cast<CGObjectInstance&>(*this);
  332. }
  333. };
  334. class DLL_LINKAGE CGObservatory : public CGObjectInstance //Redwood observatory
  335. {
  336. public:
  337. void onHeroVisit(const CGHeroInstance * h) const override;
  338. template <typename Handler> void serialize(Handler &h, const int version)
  339. {
  340. h & static_cast<CGObjectInstance&>(*this);
  341. }
  342. };
  343. class DLL_LINKAGE CGBoat : public CGObjectInstance, public CBonusSystemNode
  344. {
  345. public:
  346. ui8 direction;
  347. const CGHeroInstance *hero; //hero on board
  348. bool onboardAssaultAllowed; //if true, hero can attack units from transport
  349. bool onboardVisitAllowed; //if true, hero can visit objects from transport
  350. EPathfindingLayer::EEPathfindingLayer layer;
  351. //animation filenames. If empty - animations won't be used
  352. std::string actualAnimation; //for OH3 boats those have actual animations
  353. std::string overlayAnimation; //waves animations
  354. std::array<std::string, PlayerColor::PLAYER_LIMIT_I> flagAnimations;
  355. void initObj(CRandomGenerator & rand) override;
  356. CGBoat()
  357. {
  358. hero = nullptr;
  359. direction = 4;
  360. layer = EPathfindingLayer::EEPathfindingLayer::SAIL;
  361. }
  362. template <typename Handler> void serialize(Handler &h, const int version)
  363. {
  364. h & static_cast<CGObjectInstance&>(*this);
  365. h & static_cast<CBonusSystemNode&>(*this);
  366. h & direction;
  367. h & hero;
  368. h & layer;
  369. h & onboardAssaultAllowed;
  370. h & onboardVisitAllowed;
  371. h & actualAnimation;
  372. h & overlayAnimation;
  373. h & flagAnimations;
  374. }
  375. };
  376. class DLL_LINKAGE CGShipyard : public CGObjectInstance, public IShipyard
  377. {
  378. public:
  379. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed
  380. CGShipyard();
  381. void onHeroVisit(const CGHeroInstance * h) const override;
  382. template <typename Handler> void serialize(Handler &h, const int version)
  383. {
  384. h & static_cast<CGObjectInstance&>(*this);
  385. h & static_cast<IShipyard&>(*this);
  386. }
  387. protected:
  388. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  389. };
  390. class DLL_LINKAGE CGMagi : public CGObjectInstance
  391. {
  392. public:
  393. static std::map <si32, std::vector<ObjectInstanceID> > eyelist; //[subID][id], supports multiple sets as in H5
  394. static void reset();
  395. void initObj(CRandomGenerator & rand) override;
  396. void onHeroVisit(const CGHeroInstance * h) const override;
  397. template <typename Handler> void serialize(Handler &h, const int version)
  398. {
  399. h & static_cast<CGObjectInstance&>(*this);
  400. }
  401. };
  402. class DLL_LINKAGE CCartographer : public CTeamVisited
  403. {
  404. ///behaviour varies depending on surface and floor
  405. public:
  406. void onHeroVisit(const CGHeroInstance * h) const override;
  407. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  408. template <typename Handler> void serialize(Handler &h, const int version)
  409. {
  410. h & static_cast<CTeamVisited&>(*this);
  411. }
  412. };
  413. class DLL_LINKAGE CGDenOfthieves : public CGObjectInstance
  414. {
  415. void onHeroVisit(const CGHeroInstance * h) const override;
  416. };
  417. class DLL_LINKAGE CGObelisk : public CTeamVisited
  418. {
  419. public:
  420. static constexpr int OBJPROP_INC = 20;
  421. static ui8 obeliskCount; //how many obelisks are on map
  422. static std::map<TeamID, ui8> visited; //map: team_id => how many obelisks has been visited
  423. void onHeroVisit(const CGHeroInstance * h) const override;
  424. void initObj(CRandomGenerator & rand) override;
  425. std::string getHoverText(PlayerColor player) const override;
  426. static void reset();
  427. template <typename Handler> void serialize(Handler &h, const int version)
  428. {
  429. h & static_cast<CTeamVisited&>(*this);
  430. }
  431. protected:
  432. void setPropertyDer(ui8 what, ui32 val) override;
  433. };
  434. class DLL_LINKAGE CGLighthouse : public CGObjectInstance
  435. {
  436. public:
  437. void onHeroVisit(const CGHeroInstance * h) const override;
  438. void initObj(CRandomGenerator & rand) override;
  439. template <typename Handler> void serialize(Handler &h, const int version)
  440. {
  441. h & static_cast<CGObjectInstance&>(*this);
  442. }
  443. void giveBonusTo(const PlayerColor & player, bool onInit = false) const;
  444. protected:
  445. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  446. };
  447. class DLL_LINKAGE CGTerrainPatch : public CGObjectInstance
  448. {
  449. public:
  450. CGTerrainPatch() = default;
  451. virtual bool isTile2Terrain() const override
  452. {
  453. return true;
  454. }
  455. };
  456. VCMI_LIB_NAMESPACE_END