MiscObjects.h 16 KB

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