MiscObjects.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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;
  164. std::string message;
  165. CGArtifact() : CArmedInstance() {storedArtifact = nullptr;};
  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. std::string getObjectName() const override;
  170. void pick( const CGHeroInstance * h ) const;
  171. void initObj(CRandomGenerator & rand) override;
  172. void afterAddToMap(CMap * map) override;
  173. BattleField getBattlefield() const override;
  174. template <typename Handler> void serialize(Handler &h, const int version)
  175. {
  176. h & static_cast<CArmedInstance&>(*this);
  177. h & message;
  178. h & storedArtifact;
  179. }
  180. protected:
  181. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  182. };
  183. class DLL_LINKAGE CGResource : public CArmedInstance
  184. {
  185. public:
  186. static constexpr ui32 RANDOM_AMOUNT = 0;
  187. ui32 amount = RANDOM_AMOUNT; //0 if random
  188. std::string message;
  189. void onHeroVisit(const CGHeroInstance * h) const override;
  190. void initObj(CRandomGenerator & rand) override;
  191. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  192. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  193. std::string getHoverText(PlayerColor player) const override;
  194. void collectRes(const PlayerColor & player) const;
  195. template <typename Handler> void serialize(Handler &h, const int version)
  196. {
  197. h & static_cast<CArmedInstance&>(*this);
  198. h & amount;
  199. h & message;
  200. }
  201. protected:
  202. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  203. };
  204. class DLL_LINKAGE CGShrine : public CTeamVisited
  205. {
  206. public:
  207. SpellID spell; //id of spell or NONE if random
  208. void onHeroVisit(const CGHeroInstance * h) const override;
  209. void initObj(CRandomGenerator & rand) override;
  210. std::string getHoverText(PlayerColor player) const override;
  211. std::string getHoverText(const CGHeroInstance * hero) const override;
  212. template <typename Handler> void serialize(Handler &h, const int version)
  213. {
  214. h & static_cast<CTeamVisited&>(*this);;
  215. h & spell;
  216. }
  217. protected:
  218. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  219. };
  220. class DLL_LINKAGE CGMine : public CArmedInstance
  221. {
  222. public:
  223. GameResID producedResource;
  224. ui32 producedQuantity;
  225. std::set<GameResID> abandonedMineResources;
  226. private:
  227. void onHeroVisit(const CGHeroInstance * h) const override;
  228. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  229. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  230. void flagMine(const PlayerColor & player) const;
  231. void newTurn(CRandomGenerator & rand) const override;
  232. void initObj(CRandomGenerator & rand) override;
  233. std::string getObjectName() const override;
  234. std::string getHoverText(PlayerColor player) const override;
  235. bool isAbandoned() const;
  236. public:
  237. template <typename Handler> void serialize(Handler &h, const int version)
  238. {
  239. h & static_cast<CArmedInstance&>(*this);
  240. h & producedResource;
  241. h & producedQuantity;
  242. h & abandonedMineResources;
  243. }
  244. ui32 defaultResProduction() const;
  245. protected:
  246. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  247. };
  248. struct DLL_LINKAGE TeleportChannel
  249. {
  250. enum EPassability {UNKNOWN, IMPASSABLE, PASSABLE};
  251. std::vector<ObjectInstanceID> entrances;
  252. std::vector<ObjectInstanceID> exits;
  253. EPassability passability = EPassability::UNKNOWN;
  254. template <typename Handler> void serialize(Handler &h, const int version)
  255. {
  256. h & entrances;
  257. h & exits;
  258. h & passability;
  259. }
  260. };
  261. class DLL_LINKAGE CGTeleport : public CGObjectInstance
  262. {
  263. bool isChannelEntrance(const ObjectInstanceID & id) const;
  264. bool isChannelExit(const ObjectInstanceID & id) const;
  265. std::vector<ObjectInstanceID> getAllEntrances(bool excludeCurrent = false) const;
  266. protected:
  267. enum EType {UNKNOWN, ENTRANCE, EXIT, BOTH};
  268. EType type = EType::UNKNOWN;
  269. ObjectInstanceID getRandomExit(const CGHeroInstance * h) const;
  270. std::vector<ObjectInstanceID> getAllExits(bool excludeCurrent = false) const;
  271. public:
  272. TeleportChannelID channel;
  273. bool isEntrance() const;
  274. bool isExit() const;
  275. virtual void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const = 0;
  276. static bool isTeleport(const CGObjectInstance * dst);
  277. static bool isConnected(const CGTeleport * src, const CGTeleport * dst);
  278. static bool isConnected(const CGObjectInstance * src, const CGObjectInstance * dst);
  279. static void addToChannel(std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > &channelsList, const CGTeleport * obj);
  280. static std::vector<ObjectInstanceID> getPassableExits(CGameState * gs, const CGHeroInstance * h, std::vector<ObjectInstanceID> exits);
  281. static bool isExitPassable(CGameState * gs, const CGHeroInstance * h, const CGObjectInstance * obj);
  282. template <typename Handler> void serialize(Handler &h, const int version)
  283. {
  284. h & type;
  285. h & channel;
  286. h & static_cast<CGObjectInstance&>(*this);
  287. }
  288. };
  289. class DLL_LINKAGE CGMonolith : public CGTeleport
  290. {
  291. TeleportChannelID findMeChannel(const std::vector<Obj> & IDs, int SubID) const;
  292. protected:
  293. void onHeroVisit(const CGHeroInstance * h) const override;
  294. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  295. void initObj(CRandomGenerator & rand) override;
  296. public:
  297. template <typename Handler> void serialize(Handler &h, const int version)
  298. {
  299. h & static_cast<CGTeleport&>(*this);
  300. }
  301. };
  302. class DLL_LINKAGE CGSubterraneanGate : public CGMonolith
  303. {
  304. void onHeroVisit(const CGHeroInstance * h) const override;
  305. void initObj(CRandomGenerator & rand) override;
  306. public:
  307. static void postInit();
  308. template <typename Handler> void serialize(Handler &h, const int version)
  309. {
  310. h & static_cast<CGMonolith&>(*this);
  311. }
  312. };
  313. class DLL_LINKAGE CGWhirlpool : public CGMonolith
  314. {
  315. void onHeroVisit(const CGHeroInstance * h) const override;
  316. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  317. static bool isProtected( const CGHeroInstance * h );
  318. public:
  319. template <typename Handler> void serialize(Handler &h, const int version)
  320. {
  321. h & static_cast<CGMonolith&>(*this);
  322. }
  323. };
  324. class DLL_LINKAGE CGSirens : public CGObjectInstance
  325. {
  326. public:
  327. void onHeroVisit(const CGHeroInstance * h) const override;
  328. std::string getHoverText(const CGHeroInstance * hero) const override;
  329. void initObj(CRandomGenerator & rand) override;
  330. template <typename Handler> void serialize(Handler &h, const int version)
  331. {
  332. h & static_cast<CGObjectInstance&>(*this);
  333. }
  334. };
  335. class DLL_LINKAGE CGObservatory : public CGObjectInstance //Redwood observatory
  336. {
  337. public:
  338. void onHeroVisit(const CGHeroInstance * h) const override;
  339. template <typename Handler> void serialize(Handler &h, const int version)
  340. {
  341. h & static_cast<CGObjectInstance&>(*this);
  342. }
  343. };
  344. class DLL_LINKAGE CGBoat : public CGObjectInstance, public CBonusSystemNode
  345. {
  346. public:
  347. ui8 direction;
  348. const CGHeroInstance *hero; //hero on board
  349. EPathfindingLayer::EEPathfindingLayer layer;
  350. //animation filenames. If empty - animations won't be used
  351. std::string actualAnimation; //for OH3 boats those have actual animations
  352. std::string overlayAnimation; //waves animations
  353. std::array<std::string, PlayerColor::PLAYER_LIMIT_I> flagAnimations;
  354. void initObj(CRandomGenerator & rand) override;
  355. CGBoat()
  356. {
  357. hero = nullptr;
  358. direction = 4;
  359. layer = EPathfindingLayer::EEPathfindingLayer::SAIL;
  360. }
  361. template <typename Handler> void serialize(Handler &h, const int version)
  362. {
  363. h & static_cast<CGObjectInstance&>(*this);
  364. h & static_cast<CBonusSystemNode&>(*this);
  365. h & direction;
  366. h & hero;
  367. h & layer;
  368. h & actualAnimation;
  369. h & overlayAnimation;
  370. h & flagAnimations;
  371. }
  372. };
  373. class DLL_LINKAGE CGShipyard : public CGObjectInstance, public IShipyard
  374. {
  375. public:
  376. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed
  377. CGShipyard();
  378. void onHeroVisit(const CGHeroInstance * h) const override;
  379. template <typename Handler> void serialize(Handler &h, const int version)
  380. {
  381. h & static_cast<CGObjectInstance&>(*this);
  382. h & static_cast<IShipyard&>(*this);
  383. }
  384. protected:
  385. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  386. };
  387. class DLL_LINKAGE CGMagi : public CGObjectInstance
  388. {
  389. public:
  390. static std::map <si32, std::vector<ObjectInstanceID> > eyelist; //[subID][id], supports multiple sets as in H5
  391. static void reset();
  392. void initObj(CRandomGenerator & rand) override;
  393. void onHeroVisit(const CGHeroInstance * h) const override;
  394. template <typename Handler> void serialize(Handler &h, const int version)
  395. {
  396. h & static_cast<CGObjectInstance&>(*this);
  397. }
  398. };
  399. class DLL_LINKAGE CCartographer : public CTeamVisited
  400. {
  401. ///behaviour varies depending on surface and floor
  402. public:
  403. void onHeroVisit(const CGHeroInstance * h) const override;
  404. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  405. template <typename Handler> void serialize(Handler &h, const int version)
  406. {
  407. h & static_cast<CTeamVisited&>(*this);
  408. }
  409. };
  410. class DLL_LINKAGE CGDenOfthieves : public CGObjectInstance
  411. {
  412. void onHeroVisit(const CGHeroInstance * h) const override;
  413. };
  414. class DLL_LINKAGE CGObelisk : public CTeamVisited
  415. {
  416. public:
  417. static constexpr int OBJPROP_INC = 20;
  418. static ui8 obeliskCount; //how many obelisks are on map
  419. static std::map<TeamID, ui8> visited; //map: team_id => how many obelisks has been visited
  420. void onHeroVisit(const CGHeroInstance * h) const override;
  421. void initObj(CRandomGenerator & rand) override;
  422. std::string getHoverText(PlayerColor player) const override;
  423. static void reset();
  424. template <typename Handler> void serialize(Handler &h, const int version)
  425. {
  426. h & static_cast<CTeamVisited&>(*this);
  427. }
  428. protected:
  429. void setPropertyDer(ui8 what, ui32 val) override;
  430. };
  431. class DLL_LINKAGE CGLighthouse : public CGObjectInstance
  432. {
  433. public:
  434. void onHeroVisit(const CGHeroInstance * h) const override;
  435. void initObj(CRandomGenerator & rand) override;
  436. template <typename Handler> void serialize(Handler &h, const int version)
  437. {
  438. h & static_cast<CGObjectInstance&>(*this);
  439. }
  440. void giveBonusTo(const PlayerColor & player, bool onInit = false) const;
  441. protected:
  442. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  443. };
  444. class DLL_LINKAGE CGTerrainPatch : public CGObjectInstance
  445. {
  446. public:
  447. CGTerrainPatch() = default;
  448. virtual bool isTile2Terrain() const override
  449. {
  450. return true;
  451. }
  452. };
  453. VCMI_LIB_NAMESPACE_END