MiscObjects.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. class CMap;
  15. /// Legacy class, use CRewardableObject instead
  16. class DLL_LINKAGE CTeamVisited: public CGObjectInstance
  17. {
  18. public:
  19. std::set<PlayerColor> players; //players that visited this object
  20. bool wasVisited(PlayerColor player) const override;
  21. bool wasVisited(TeamID team) const;
  22. void setPropertyDer(ui8 what, ui32 val) override;
  23. template <typename Handler> void serialize(Handler &h, const int version)
  24. {
  25. h & static_cast<CGObjectInstance&>(*this);
  26. h & players;
  27. }
  28. static const int OBJPROP_VISITED = 10;
  29. };
  30. class DLL_LINKAGE CGCreature : public CArmedInstance //creatures on map
  31. {
  32. public:
  33. enum Action {
  34. FIGHT = -2, FLEE = -1, JOIN_FOR_FREE = 0 //values > 0 mean gold price
  35. };
  36. enum Character {
  37. COMPLIANT = 0, FRIENDLY = 1, AGRESSIVE = 2, HOSTILE = 3, SAVAGE = 4
  38. };
  39. ui32 identifier; //unique code for this monster (used in missions)
  40. 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)
  41. std::string message; //message printed for attacking hero
  42. TResources resources; // resources given to hero that has won with monsters
  43. ArtifactID gainedArtifact; //ID of artifact gained to hero, -1 if none
  44. bool neverFlees; //if true, the troops will never flee
  45. bool notGrowingTeam; //if true, number of units won't grow
  46. ui64 temppower; //used to handle fractional stack growth for tiny stacks
  47. bool refusedJoining;
  48. void onHeroVisit(const CGHeroInstance * h) const override;
  49. std::string getHoverText(PlayerColor player) const override;
  50. std::string getHoverText(const CGHeroInstance * hero) const override;
  51. void initObj(CRandomGenerator & rand) override;
  52. void newTurn(CRandomGenerator & rand) const override;
  53. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  54. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  55. //stack formation depends on position,
  56. bool containsUpgradedStack() const;
  57. int getNumberOfStacks(const CGHeroInstance *hero) const;
  58. struct DLL_LINKAGE formationInfo // info about merging stacks after battle back into one
  59. {
  60. si32 basicType;
  61. ui8 upgrade; //random seed used to determine number of stacks and is there's upgraded stack
  62. template <typename Handler> void serialize(Handler &h, const int version)
  63. {
  64. h & basicType;
  65. h & upgrade;
  66. }
  67. } formation;
  68. template <typename Handler> void serialize(Handler &h, const int version)
  69. {
  70. h & static_cast<CArmedInstance&>(*this);
  71. h & identifier;
  72. h & character;
  73. h & message;
  74. h & resources;
  75. h & gainedArtifact;
  76. h & neverFlees;
  77. h & notGrowingTeam;
  78. h & temppower;
  79. h & refusedJoining;
  80. h & formation;
  81. }
  82. protected:
  83. void setPropertyDer(ui8 what, ui32 val) override;
  84. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  85. private:
  86. void fight(const CGHeroInstance *h) const;
  87. void flee( const CGHeroInstance * h ) const;
  88. void fleeDecision(const CGHeroInstance *h, ui32 pursue) const;
  89. void joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const;
  90. 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)
  91. void giveReward(const CGHeroInstance * h) const;
  92. };
  93. class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  94. {
  95. public:
  96. std::string message;
  97. void onHeroVisit(const CGHeroInstance * h) const override;
  98. void initObj(CRandomGenerator & rand) override;
  99. template <typename Handler> void serialize(Handler &h, const int version)
  100. {
  101. h & static_cast<CGObjectInstance&>(*this);
  102. h & message;
  103. }
  104. protected:
  105. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  106. };
  107. class DLL_LINKAGE CGWitchHut : public CTeamVisited
  108. {
  109. public:
  110. std::vector<si32> allowedAbilities;
  111. ui32 ability;
  112. std::string getHoverText(PlayerColor player) const override;
  113. std::string getHoverText(const CGHeroInstance * hero) const override;
  114. void onHeroVisit(const CGHeroInstance * h) const override;
  115. void initObj(CRandomGenerator & rand) override;
  116. template <typename Handler> void serialize(Handler &h, const int version)
  117. {
  118. h & static_cast<CTeamVisited&>(*this);
  119. h & allowedAbilities;
  120. h & ability;
  121. }
  122. protected:
  123. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  124. };
  125. class DLL_LINKAGE CGScholar : public CGObjectInstance
  126. {
  127. public:
  128. enum EBonusType {PRIM_SKILL, SECONDARY_SKILL, SPELL, RANDOM = 255};
  129. EBonusType bonusType;
  130. ui16 bonusID; //ID of skill/spell
  131. CGScholar() : bonusType(EBonusType::RANDOM),bonusID(0){};
  132. void onHeroVisit(const CGHeroInstance * h) const override;
  133. void initObj(CRandomGenerator & rand) override;
  134. template <typename Handler> void serialize(Handler &h, const int version)
  135. {
  136. h & static_cast<CGObjectInstance&>(*this);
  137. h & bonusType;
  138. h & bonusID;
  139. }
  140. protected:
  141. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  142. };
  143. class DLL_LINKAGE CGGarrison : public CArmedInstance
  144. {
  145. public:
  146. bool removableUnits;
  147. bool passableFor(PlayerColor color) const override;
  148. void onHeroVisit(const CGHeroInstance * h) const override;
  149. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  150. template <typename Handler> void serialize(Handler &h, const int version)
  151. {
  152. h & static_cast<CArmedInstance&>(*this);
  153. h & removableUnits;
  154. }
  155. protected:
  156. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  157. };
  158. class DLL_LINKAGE CGArtifact : public CArmedInstance
  159. {
  160. public:
  161. CArtifactInstance *storedArtifact;
  162. std::string message;
  163. CGArtifact() : CArmedInstance() {storedArtifact = nullptr;};
  164. void onHeroVisit(const CGHeroInstance * h) const override;
  165. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  166. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  167. std::string getObjectName() const override;
  168. void pick( const CGHeroInstance * h ) const;
  169. void initObj(CRandomGenerator & rand) override;
  170. void afterAddToMap(CMap * map) override;
  171. BattleField getBattlefield() const override;
  172. template <typename Handler> void serialize(Handler &h, const int version)
  173. {
  174. h & static_cast<CArmedInstance&>(*this);
  175. h & message;
  176. h & storedArtifact;
  177. }
  178. protected:
  179. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  180. };
  181. class DLL_LINKAGE CGResource : public CArmedInstance
  182. {
  183. public:
  184. static const ui32 RANDOM_AMOUNT = 0;
  185. ui32 amount; //0 if random
  186. std::string message;
  187. CGResource();
  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(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. Res::ERes producedResource;
  223. ui32 producedQuantity;
  224. private:
  225. void onHeroVisit(const CGHeroInstance * h) const override;
  226. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  227. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  228. void flagMine(PlayerColor player) const;
  229. void newTurn(CRandomGenerator & rand) const override;
  230. void initObj(CRandomGenerator & rand) override;
  231. std::string getObjectName() const override;
  232. std::string getHoverText(PlayerColor player) const override;
  233. bool isAbandoned() const;
  234. public:
  235. template <typename Handler> void serialize(Handler &h, const int version)
  236. {
  237. h & static_cast<CArmedInstance&>(*this);
  238. h & producedResource;
  239. h & producedQuantity;
  240. }
  241. ui32 defaultResProduction();
  242. protected:
  243. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  244. };
  245. struct DLL_LINKAGE TeleportChannel
  246. {
  247. enum EPassability {UNKNOWN, IMPASSABLE, PASSABLE};
  248. TeleportChannel() : passability(UNKNOWN) {}
  249. std::vector<ObjectInstanceID> entrances;
  250. std::vector<ObjectInstanceID> exits;
  251. EPassability passability;
  252. template <typename Handler> void serialize(Handler &h, const int version)
  253. {
  254. h & entrances;
  255. h & exits;
  256. h & passability;
  257. }
  258. };
  259. class DLL_LINKAGE CGTeleport : public CGObjectInstance
  260. {
  261. bool isChannelEntrance(ObjectInstanceID id) const;
  262. bool isChannelExit(ObjectInstanceID id) const;
  263. std::vector<ObjectInstanceID> getAllEntrances(bool excludeCurrent = false) const;
  264. protected:
  265. enum EType {UNKNOWN, ENTRANCE, EXIT, BOTH};
  266. EType type;
  267. CGTeleport();
  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(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 CGMagicWell : public CGObjectInstance //objects giving bonuses to luck/morale/movement
  324. {
  325. public:
  326. void onHeroVisit(const CGHeroInstance * h) const override;
  327. std::string getHoverText(const CGHeroInstance * hero) const override;
  328. template <typename Handler> void serialize(Handler &h, const int version)
  329. {
  330. h & static_cast<CGObjectInstance&>(*this);
  331. }
  332. };
  333. class DLL_LINKAGE CGSirens : public CGObjectInstance
  334. {
  335. public:
  336. void onHeroVisit(const CGHeroInstance * h) const override;
  337. std::string getHoverText(const CGHeroInstance * hero) const override;
  338. void initObj(CRandomGenerator & rand) 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 CGObservatory : public CGObjectInstance //Redwood observatory
  345. {
  346. public:
  347. void onHeroVisit(const CGHeroInstance * h) const override;
  348. template <typename Handler> void serialize(Handler &h, const int version)
  349. {
  350. h & static_cast<CGObjectInstance&>(*this);
  351. }
  352. };
  353. class DLL_LINKAGE CGBoat : public CGObjectInstance
  354. {
  355. public:
  356. ui8 direction;
  357. const CGHeroInstance *hero; //hero on board
  358. void initObj(CRandomGenerator & rand) override;
  359. CGBoat()
  360. {
  361. hero = nullptr;
  362. direction = 4;
  363. }
  364. template <typename Handler> void serialize(Handler &h, const int version)
  365. {
  366. h & static_cast<CGObjectInstance&>(*this);
  367. h & direction;
  368. h & hero;
  369. }
  370. };
  371. class CGShipyard : public CGObjectInstance, public IShipyard
  372. {
  373. public:
  374. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed
  375. CGShipyard();
  376. void onHeroVisit(const CGHeroInstance * h) const override;
  377. template <typename Handler> void serialize(Handler &h, const int version)
  378. {
  379. h & static_cast<CGObjectInstance&>(*this);
  380. h & static_cast<IShipyard&>(*this);
  381. }
  382. protected:
  383. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  384. };
  385. class DLL_LINKAGE CGMagi : public CGObjectInstance
  386. {
  387. public:
  388. static std::map <si32, std::vector<ObjectInstanceID> > eyelist; //[subID][id], supports multiple sets as in H5
  389. static void reset();
  390. void initObj(CRandomGenerator & rand) override;
  391. void onHeroVisit(const CGHeroInstance * h) const override;
  392. template <typename Handler> void serialize(Handler &h, const int version)
  393. {
  394. h & static_cast<CGObjectInstance&>(*this);
  395. }
  396. };
  397. class DLL_LINKAGE CCartographer : public CTeamVisited
  398. {
  399. ///behaviour varies depending on surface and floor
  400. public:
  401. void onHeroVisit(const CGHeroInstance * h) const override;
  402. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  403. template <typename Handler> void serialize(Handler &h, const int version)
  404. {
  405. h & static_cast<CTeamVisited&>(*this);
  406. }
  407. };
  408. class DLL_LINKAGE CGDenOfthieves : public CGObjectInstance
  409. {
  410. void onHeroVisit(const CGHeroInstance * h) const override;
  411. };
  412. class DLL_LINKAGE CGObelisk : public CTeamVisited
  413. {
  414. public:
  415. static const int OBJPROP_INC = 20;
  416. static ui8 obeliskCount; //how many obelisks are on map
  417. static std::map<TeamID, ui8> visited; //map: team_id => how many obelisks has been visited
  418. void onHeroVisit(const CGHeroInstance * h) const override;
  419. void initObj(CRandomGenerator & rand) override;
  420. std::string getHoverText(PlayerColor player) const override;
  421. static void reset();
  422. template <typename Handler> void serialize(Handler &h, const int version)
  423. {
  424. h & static_cast<CTeamVisited&>(*this);
  425. }
  426. protected:
  427. void setPropertyDer(ui8 what, ui32 val) override;
  428. };
  429. class DLL_LINKAGE CGLighthouse : public CGObjectInstance
  430. {
  431. public:
  432. void onHeroVisit(const CGHeroInstance * h) const override;
  433. void initObj(CRandomGenerator & rand) override;
  434. std::string getHoverText(PlayerColor player) const override;
  435. template <typename Handler> void serialize(Handler &h, const int version)
  436. {
  437. h & static_cast<CGObjectInstance&>(*this);
  438. }
  439. void giveBonusTo(PlayerColor player, bool onInit = false) const;
  440. protected:
  441. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  442. };
  443. class DLL_LINKAGE CGTerrainPatch : public CGObjectInstance
  444. {
  445. public:
  446. CGTerrainPatch() = default;
  447. virtual bool isTile2Terrain() const override
  448. {
  449. return true;
  450. }
  451. };