MiscObjects.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 & upgrade;
  65. }
  66. } formation;
  67. template <typename Handler> void serialize(Handler &h, const int version)
  68. {
  69. h & static_cast<CArmedInstance&>(*this);
  70. h & identifier & character & message & resources & gainedArtifact & neverFlees & notGrowingTeam & temppower;
  71. h & refusedJoining & formation;
  72. }
  73. protected:
  74. void setPropertyDer(ui8 what, ui32 val) override;
  75. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  76. private:
  77. void fight(const CGHeroInstance *h) const;
  78. void flee( const CGHeroInstance * h ) const;
  79. void fleeDecision(const CGHeroInstance *h, ui32 pursue) const;
  80. void joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const;
  81. 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)
  82. void giveReward(const CGHeroInstance * h) const;
  83. };
  84. class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  85. {
  86. public:
  87. std::string message;
  88. void onHeroVisit(const CGHeroInstance * h) const override;
  89. void initObj(CRandomGenerator & rand) override;
  90. template <typename Handler> void serialize(Handler &h, const int version)
  91. {
  92. h & static_cast<CGObjectInstance&>(*this);
  93. h & message;
  94. }
  95. protected:
  96. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  97. };
  98. class DLL_LINKAGE CGWitchHut : public CTeamVisited
  99. {
  100. public:
  101. std::vector<si32> allowedAbilities;
  102. ui32 ability;
  103. std::string getHoverText(PlayerColor player) const override;
  104. std::string getHoverText(const CGHeroInstance * hero) const override;
  105. void onHeroVisit(const CGHeroInstance * h) const override;
  106. void initObj(CRandomGenerator & rand) override;
  107. template <typename Handler> void serialize(Handler &h, const int version)
  108. {
  109. h & static_cast<CTeamVisited&>(*this);
  110. h & allowedAbilities & ability;
  111. }
  112. protected:
  113. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  114. };
  115. class DLL_LINKAGE CGScholar : public CGObjectInstance
  116. {
  117. public:
  118. enum EBonusType {PRIM_SKILL, SECONDARY_SKILL, SPELL, RANDOM = 255};
  119. EBonusType bonusType;
  120. ui16 bonusID; //ID of skill/spell
  121. CGScholar() : bonusType(EBonusType::RANDOM),bonusID(0){};
  122. void onHeroVisit(const CGHeroInstance * h) const override;
  123. void initObj(CRandomGenerator & rand) override;
  124. template <typename Handler> void serialize(Handler &h, const int version)
  125. {
  126. h & static_cast<CGObjectInstance&>(*this);
  127. h & bonusType & bonusID;
  128. }
  129. protected:
  130. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  131. };
  132. class DLL_LINKAGE CGGarrison : public CArmedInstance
  133. {
  134. public:
  135. bool removableUnits;
  136. bool passableFor(PlayerColor color) const override;
  137. void onHeroVisit(const CGHeroInstance * h) const override;
  138. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  139. template <typename Handler> void serialize(Handler &h, const int version)
  140. {
  141. h & static_cast<CArmedInstance&>(*this);
  142. h & removableUnits;
  143. }
  144. protected:
  145. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  146. };
  147. class DLL_LINKAGE CGArtifact : public CArmedInstance
  148. {
  149. public:
  150. CArtifactInstance *storedArtifact;
  151. std::string message;
  152. CGArtifact() : CArmedInstance() {storedArtifact = nullptr;};
  153. void onHeroVisit(const CGHeroInstance * h) const override;
  154. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  155. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  156. std::string getObjectName() const override;
  157. void pick( const CGHeroInstance * h ) const;
  158. void initObj(CRandomGenerator & rand) override;
  159. void afterAddToMap(CMap * map) override;
  160. template <typename Handler> void serialize(Handler &h, const int version)
  161. {
  162. h & static_cast<CArmedInstance&>(*this);
  163. h & message & storedArtifact;
  164. }
  165. protected:
  166. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  167. };
  168. class DLL_LINKAGE CGResource : public CArmedInstance
  169. {
  170. public:
  171. ui32 amount; //0 if random
  172. std::string message;
  173. CGResource();
  174. void onHeroVisit(const CGHeroInstance * h) const override;
  175. void initObj(CRandomGenerator & rand) override;
  176. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  177. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  178. std::string getHoverText(PlayerColor player) const override;
  179. void collectRes(PlayerColor player) const;
  180. template <typename Handler> void serialize(Handler &h, const int version)
  181. {
  182. h & static_cast<CArmedInstance&>(*this);
  183. h & amount & message;
  184. }
  185. protected:
  186. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  187. };
  188. class DLL_LINKAGE CGShrine : public CTeamVisited
  189. {
  190. public:
  191. SpellID spell; //id of spell or NONE if random
  192. void onHeroVisit(const CGHeroInstance * h) const override;
  193. void initObj(CRandomGenerator & rand) override;
  194. std::string getHoverText(PlayerColor player) const override;
  195. std::string getHoverText(const CGHeroInstance * hero) const override;
  196. template <typename Handler> void serialize(Handler &h, const int version)
  197. {
  198. h & static_cast<CTeamVisited&>(*this);;
  199. h & spell;
  200. }
  201. protected:
  202. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  203. };
  204. class DLL_LINKAGE CGMine : public CArmedInstance
  205. {
  206. public:
  207. Res::ERes producedResource;
  208. ui32 producedQuantity;
  209. private:
  210. void onHeroVisit(const CGHeroInstance * h) const override;
  211. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  212. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  213. void flagMine(PlayerColor player) const;
  214. void newTurn(CRandomGenerator & rand) const override;
  215. void initObj(CRandomGenerator & rand) override;
  216. std::string getObjectName() const override;
  217. std::string getHoverText(PlayerColor player) const override;
  218. bool isAbandoned() const;
  219. public:
  220. template <typename Handler> void serialize(Handler &h, const int version)
  221. {
  222. h & static_cast<CArmedInstance&>(*this);
  223. h & producedResource & producedQuantity;
  224. }
  225. ui32 defaultResProduction();
  226. protected:
  227. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  228. };
  229. struct DLL_LINKAGE TeleportChannel
  230. {
  231. enum EPassability {UNKNOWN, IMPASSABLE, PASSABLE};
  232. TeleportChannel() : passability(UNKNOWN) {}
  233. std::vector<ObjectInstanceID> entrances;
  234. std::vector<ObjectInstanceID> exits;
  235. EPassability passability;
  236. template <typename Handler> void serialize(Handler &h, const int version)
  237. {
  238. h & entrances & exits & passability;
  239. }
  240. };
  241. class DLL_LINKAGE CGTeleport : public CGObjectInstance
  242. {
  243. bool isChannelEntrance(ObjectInstanceID id) const;
  244. bool isChannelExit(ObjectInstanceID id) const;
  245. std::vector<ObjectInstanceID> getAllEntrances(bool excludeCurrent = false) const;
  246. protected:
  247. enum EType {UNKNOWN, ENTRANCE, EXIT, BOTH};
  248. EType type;
  249. CGTeleport();
  250. ObjectInstanceID getRandomExit(const CGHeroInstance * h) const;
  251. std::vector<ObjectInstanceID> getAllExits(bool excludeCurrent = false) const;
  252. public:
  253. TeleportChannelID channel;
  254. bool isEntrance() const;
  255. bool isExit() const;
  256. virtual void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const = 0;
  257. static bool isTeleport(const CGObjectInstance * dst);
  258. static bool isConnected(const CGTeleport * src, const CGTeleport * dst);
  259. static bool isConnected(const CGObjectInstance * src, const CGObjectInstance * dst);
  260. static void addToChannel(std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > &channelsList, const CGTeleport * obj);
  261. static std::vector<ObjectInstanceID> getPassableExits(CGameState * gs, const CGHeroInstance * h, std::vector<ObjectInstanceID> exits);
  262. static bool isExitPassable(CGameState * gs, const CGHeroInstance * h, const CGObjectInstance * obj);
  263. template <typename Handler> void serialize(Handler &h, const int version)
  264. {
  265. h & type & channel & static_cast<CGObjectInstance&>(*this);
  266. }
  267. };
  268. class DLL_LINKAGE CGMonolith : public CGTeleport
  269. {
  270. TeleportChannelID findMeChannel(std::vector<Obj> IDs, int SubID) const;
  271. protected:
  272. void onHeroVisit(const CGHeroInstance * h) const override;
  273. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  274. void initObj(CRandomGenerator & rand) override;
  275. public:
  276. template <typename Handler> void serialize(Handler &h, const int version)
  277. {
  278. h & static_cast<CGTeleport&>(*this);
  279. }
  280. };
  281. class DLL_LINKAGE CGSubterraneanGate : public CGMonolith
  282. {
  283. void onHeroVisit(const CGHeroInstance * h) const override;
  284. void initObj(CRandomGenerator & rand) override;
  285. public:
  286. static void postInit();
  287. template <typename Handler> void serialize(Handler &h, const int version)
  288. {
  289. h & static_cast<CGMonolith&>(*this);
  290. }
  291. };
  292. class DLL_LINKAGE CGWhirlpool : public CGMonolith
  293. {
  294. void onHeroVisit(const CGHeroInstance * h) const override;
  295. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  296. static bool isProtected( const CGHeroInstance * h );
  297. public:
  298. template <typename Handler> void serialize(Handler &h, const int version)
  299. {
  300. h & static_cast<CGMonolith&>(*this);
  301. }
  302. };
  303. class DLL_LINKAGE CGMagicWell : public CGObjectInstance //objects giving bonuses to luck/morale/movement
  304. {
  305. public:
  306. void onHeroVisit(const CGHeroInstance * h) const override;
  307. std::string getHoverText(const CGHeroInstance * hero) const override;
  308. template <typename Handler> void serialize(Handler &h, const int version)
  309. {
  310. h & static_cast<CGObjectInstance&>(*this);
  311. }
  312. };
  313. class DLL_LINKAGE CGSirens : public CGObjectInstance
  314. {
  315. public:
  316. void onHeroVisit(const CGHeroInstance * h) const override;
  317. std::string getHoverText(const CGHeroInstance * hero) const override;
  318. void initObj(CRandomGenerator & rand) override;
  319. template <typename Handler> void serialize(Handler &h, const int version)
  320. {
  321. h & static_cast<CGObjectInstance&>(*this);
  322. }
  323. };
  324. class DLL_LINKAGE CGObservatory : public CGObjectInstance //Redwood observatory
  325. {
  326. public:
  327. void onHeroVisit(const CGHeroInstance * h) 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 CGBoat : public CGObjectInstance
  334. {
  335. public:
  336. ui8 direction;
  337. const CGHeroInstance *hero; //hero on board
  338. void initObj(CRandomGenerator & rand) override;
  339. CGBoat()
  340. {
  341. hero = nullptr;
  342. direction = 4;
  343. }
  344. template <typename Handler> void serialize(Handler &h, const int version)
  345. {
  346. h & static_cast<CGObjectInstance&>(*this) & direction & hero;
  347. }
  348. };
  349. class CGShipyard : public CGObjectInstance, public IShipyard
  350. {
  351. public:
  352. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed
  353. CGShipyard();
  354. void onHeroVisit(const CGHeroInstance * h) const override;
  355. template <typename Handler> void serialize(Handler &h, const int version)
  356. {
  357. h & static_cast<CGObjectInstance&>(*this);
  358. h & static_cast<IShipyard&>(*this);
  359. }
  360. protected:
  361. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  362. };
  363. class DLL_LINKAGE CGMagi : public CGObjectInstance
  364. {
  365. public:
  366. static std::map <si32, std::vector<ObjectInstanceID> > eyelist; //[subID][id], supports multiple sets as in H5
  367. static void reset();
  368. void initObj(CRandomGenerator & rand) override;
  369. void onHeroVisit(const CGHeroInstance * h) const override;
  370. template <typename Handler> void serialize(Handler &h, const int version)
  371. {
  372. h & static_cast<CGObjectInstance&>(*this);
  373. }
  374. };
  375. class DLL_LINKAGE CCartographer : public CTeamVisited
  376. {
  377. ///behaviour varies depending on surface and floor
  378. public:
  379. void onHeroVisit(const CGHeroInstance * h) const override;
  380. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  381. template <typename Handler> void serialize(Handler &h, const int version)
  382. {
  383. h & static_cast<CTeamVisited&>(*this);
  384. }
  385. };
  386. class DLL_LINKAGE CGDenOfthieves : public CGObjectInstance
  387. {
  388. void onHeroVisit(const CGHeroInstance * h) const override;
  389. };
  390. class DLL_LINKAGE CGObelisk : public CTeamVisited
  391. {
  392. public:
  393. static const int OBJPROP_INC = 20;
  394. static ui8 obeliskCount; //how many obelisks are on map
  395. static std::map<TeamID, ui8> visited; //map: team_id => how many obelisks has been visited
  396. void onHeroVisit(const CGHeroInstance * h) const override;
  397. void initObj(CRandomGenerator & rand) override;
  398. std::string getHoverText(PlayerColor player) const override;
  399. static void reset();
  400. template <typename Handler> void serialize(Handler &h, const int version)
  401. {
  402. h & static_cast<CTeamVisited&>(*this);
  403. }
  404. protected:
  405. void setPropertyDer(ui8 what, ui32 val) override;
  406. };
  407. class DLL_LINKAGE CGLighthouse : public CGObjectInstance
  408. {
  409. public:
  410. void onHeroVisit(const CGHeroInstance * h) const override;
  411. void initObj(CRandomGenerator & rand) override;
  412. std::string getHoverText(PlayerColor player) const override;
  413. template <typename Handler> void serialize(Handler &h, const int version)
  414. {
  415. h & static_cast<CGObjectInstance&>(*this);
  416. }
  417. void giveBonusTo( PlayerColor player ) const;
  418. protected:
  419. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  420. };