MiscObjects.h 16 KB

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