MiscObjects.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. #pragma once
  2. #include "CObjectHandler.h"
  3. #include "CArmedInstance.h"
  4. #include "../ResourceSet.h"
  5. /*
  6. * MiscObjects.h, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. class CMap;
  15. class DLL_LINKAGE CPlayersVisited: public CGObjectInstance
  16. {
  17. public:
  18. std::set<PlayerColor> players; //players that visited this object
  19. bool wasVisited(PlayerColor player) const override;
  20. bool wasVisited(TeamID team) const;
  21. void setPropertyDer(ui8 what, ui32 val) override;
  22. template <typename Handler> void serialize(Handler &h, const int version)
  23. {
  24. h & static_cast<CGObjectInstance&>(*this);
  25. h & players;
  26. }
  27. static const int OBJPROP_VISITED = 10;
  28. };
  29. class DLL_LINKAGE CGCreature : public CArmedInstance //creatures on map
  30. {
  31. public:
  32. enum Action {
  33. FIGHT = -2, FLEE = -1, JOIN_FOR_FREE = 0 //values > 0 mean gold price
  34. };
  35. enum Character {
  36. COMPLIANT = 0, FRIENDLY = 1, AGRESSIVE = 2, HOSTILE = 3, SAVAGE = 4
  37. };
  38. ui32 identifier; //unique code for this monster (used in missions)
  39. 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)
  40. std::string message; //message printed for attacking hero
  41. TResources resources; // resources given to hero that has won with monsters
  42. ArtifactID gainedArtifact; //ID of artifact gained to hero, -1 if none
  43. bool neverFlees; //if true, the troops will never flee
  44. bool notGrowingTeam; //if true, number of units won't grow
  45. ui64 temppower; //used to handle fractional stack growth for tiny stacks
  46. bool refusedJoining;
  47. void onHeroVisit(const CGHeroInstance * h) const override;
  48. std::string getHoverText(PlayerColor player) const override;
  49. std::string getHoverText(const CGHeroInstance * hero) const override;
  50. void initObj(CRandomGenerator & rand) override;
  51. void newTurn(CRandomGenerator & rand) const override;
  52. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  53. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  54. //stack formation depends on position,
  55. bool containsUpgradedStack() const;
  56. int getNumberOfStacks(const CGHeroInstance *hero) const;
  57. struct DLL_LINKAGE formationInfo // info about merging stacks after battle back into one
  58. {
  59. si32 basicType;
  60. ui8 upgrade; //random seed used to determine number of stacks and is there's upgraded stack
  61. template <typename Handler> void serialize(Handler &h, const int version)
  62. {
  63. h & basicType & upgrade;
  64. }
  65. } formation;
  66. template <typename Handler> void serialize(Handler &h, const int version)
  67. {
  68. h & static_cast<CArmedInstance&>(*this);
  69. h & identifier & character & message & resources & gainedArtifact & neverFlees & notGrowingTeam & temppower;
  70. h & refusedJoining & formation;
  71. }
  72. protected:
  73. void setPropertyDer(ui8 what, ui32 val) override;
  74. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  75. private:
  76. void fight(const CGHeroInstance *h) const;
  77. void flee( const CGHeroInstance * h ) const;
  78. void fleeDecision(const CGHeroInstance *h, ui32 pursue) const;
  79. void joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const;
  80. 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)
  81. void giveReward(const CGHeroInstance * h) const;
  82. };
  83. class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  84. {
  85. public:
  86. std::string message;
  87. void onHeroVisit(const CGHeroInstance * h) const override;
  88. void initObj(CRandomGenerator & rand) override;
  89. template <typename Handler> void serialize(Handler &h, const int version)
  90. {
  91. h & static_cast<CGObjectInstance&>(*this);
  92. h & message;
  93. }
  94. protected:
  95. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  96. };
  97. class DLL_LINKAGE CGWitchHut : public CPlayersVisited
  98. {
  99. public:
  100. std::vector<si32> allowedAbilities;
  101. ui32 ability;
  102. std::string getHoverText(PlayerColor player) const override;
  103. std::string getHoverText(const CGHeroInstance * hero) const override;
  104. void onHeroVisit(const CGHeroInstance * h) const override;
  105. void initObj(CRandomGenerator & rand) override;
  106. template <typename Handler> void serialize(Handler &h, const int version)
  107. {
  108. h & static_cast<CPlayersVisited&>(*this);
  109. h & allowedAbilities & ability;
  110. }
  111. protected:
  112. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  113. };
  114. class DLL_LINKAGE CGScholar : public CGObjectInstance
  115. {
  116. public:
  117. enum EBonusType {PRIM_SKILL, SECONDARY_SKILL, SPELL, RANDOM = 255};
  118. EBonusType bonusType;
  119. ui16 bonusID; //ID of skill/spell
  120. CGScholar() : bonusType(EBonusType::RANDOM){};
  121. void onHeroVisit(const CGHeroInstance * h) const override;
  122. void initObj(CRandomGenerator & rand) override;
  123. template <typename Handler> void serialize(Handler &h, const int version)
  124. {
  125. h & static_cast<CGObjectInstance&>(*this);
  126. h & bonusType & bonusID;
  127. }
  128. protected:
  129. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  130. };
  131. class DLL_LINKAGE CGGarrison : public CArmedInstance
  132. {
  133. public:
  134. bool removableUnits;
  135. bool passableFor(PlayerColor color) const override;
  136. void onHeroVisit(const CGHeroInstance * h) const override;
  137. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  138. template <typename Handler> void serialize(Handler &h, const int version)
  139. {
  140. h & static_cast<CArmedInstance&>(*this);
  141. h & removableUnits;
  142. }
  143. protected:
  144. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  145. };
  146. class DLL_LINKAGE CGArtifact : public CArmedInstance
  147. {
  148. public:
  149. CArtifactInstance *storedArtifact;
  150. std::string message;
  151. CGArtifact() : CArmedInstance() {storedArtifact = nullptr;};
  152. void onHeroVisit(const CGHeroInstance * h) const override;
  153. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  154. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  155. std::string getObjectName() const override;
  156. void pick( const CGHeroInstance * h ) const;
  157. void initObj(CRandomGenerator & rand) override;
  158. template <typename Handler> void serialize(Handler &h, const int version)
  159. {
  160. h & static_cast<CArmedInstance&>(*this);
  161. h & message & storedArtifact;
  162. }
  163. protected:
  164. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  165. };
  166. class DLL_LINKAGE CGResource : public CArmedInstance
  167. {
  168. public:
  169. ui32 amount; //0 if random
  170. std::string message;
  171. CGResource();
  172. void onHeroVisit(const CGHeroInstance * h) const override;
  173. void initObj(CRandomGenerator & rand) override;
  174. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  175. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  176. std::string getHoverText(PlayerColor player) const override;
  177. void collectRes(PlayerColor player) const;
  178. template <typename Handler> void serialize(Handler &h, const int version)
  179. {
  180. h & static_cast<CArmedInstance&>(*this);
  181. h & amount & message;
  182. }
  183. protected:
  184. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  185. };
  186. class DLL_LINKAGE CGShrine : public CPlayersVisited
  187. {
  188. public:
  189. SpellID spell; //id of spell or NONE if random
  190. void onHeroVisit(const CGHeroInstance * h) const override;
  191. void initObj(CRandomGenerator & rand) override;
  192. std::string getHoverText(PlayerColor player) const override;
  193. std::string getHoverText(const CGHeroInstance * hero) const override;
  194. template <typename Handler> void serialize(Handler &h, const int version)
  195. {
  196. h & static_cast<CPlayersVisited&>(*this);;
  197. h & spell;
  198. }
  199. protected:
  200. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  201. };
  202. class DLL_LINKAGE CGMine : public CArmedInstance
  203. {
  204. public:
  205. Res::ERes producedResource;
  206. ui32 producedQuantity;
  207. private:
  208. void onHeroVisit(const CGHeroInstance * h) const override;
  209. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  210. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  211. void flagMine(PlayerColor player) const;
  212. void newTurn(CRandomGenerator & rand) const override;
  213. void initObj(CRandomGenerator & rand) override;
  214. std::string getObjectName() const override;
  215. std::string getHoverText(PlayerColor player) const override;
  216. bool isAbandoned() const;
  217. public:
  218. template <typename Handler> void serialize(Handler &h, const int version)
  219. {
  220. h & static_cast<CArmedInstance&>(*this);
  221. h & producedResource & producedQuantity;
  222. }
  223. ui32 defaultResProduction();
  224. protected:
  225. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  226. };
  227. struct DLL_LINKAGE TeleportChannel
  228. {
  229. enum EPassability {UNKNOWN, IMPASSABLE, PASSABLE};
  230. TeleportChannel() : passability(UNKNOWN) {}
  231. std::vector<ObjectInstanceID> entrances;
  232. std::vector<ObjectInstanceID> exits;
  233. EPassability passability;
  234. template <typename Handler> void serialize(Handler &h, const int version)
  235. {
  236. h & entrances & exits & passability;
  237. }
  238. };
  239. class DLL_LINKAGE CGTeleport : public CGObjectInstance
  240. {
  241. bool isChannelEntrance(ObjectInstanceID id) const;
  242. bool isChannelExit(ObjectInstanceID id) const;
  243. std::vector<ObjectInstanceID> getAllEntrances(bool excludeCurrent = false) const;
  244. protected:
  245. enum EType {UNKNOWN, ENTRANCE, EXIT, BOTH};
  246. EType type;
  247. CGTeleport();
  248. ObjectInstanceID getRandomExit(const CGHeroInstance * h) const;
  249. std::vector<ObjectInstanceID> getAllExits(bool excludeCurrent = false) const;
  250. public:
  251. TeleportChannelID channel;
  252. bool isEntrance() const;
  253. bool isExit() const;
  254. virtual void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const = 0;
  255. static bool isTeleport(const CGObjectInstance * dst);
  256. static bool isConnected(const CGTeleport * src, const CGTeleport * dst);
  257. static bool isConnected(const CGObjectInstance * src, const CGObjectInstance * dst);
  258. static void addToChannel(std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > &channelsList, const CGTeleport * obj);
  259. static std::vector<ObjectInstanceID> getPassableExits(CGameState * gs, const CGHeroInstance * h, std::vector<ObjectInstanceID> exits);
  260. static bool isExitPassable(CGameState * gs, const CGHeroInstance * h, const CGObjectInstance * obj);
  261. template <typename Handler> void serialize(Handler &h, const int version)
  262. {
  263. h & type & channel & static_cast<CGObjectInstance&>(*this);
  264. }
  265. };
  266. class DLL_LINKAGE CGMonolith : public CGTeleport
  267. {
  268. TeleportChannelID findMeChannel(std::vector<Obj> IDs, int SubID) const;
  269. protected:
  270. void onHeroVisit(const CGHeroInstance * h) const override;
  271. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  272. void initObj(CRandomGenerator & rand) override;
  273. public:
  274. template <typename Handler> void serialize(Handler &h, const int version)
  275. {
  276. h & static_cast<CGTeleport&>(*this);
  277. }
  278. };
  279. class DLL_LINKAGE CGSubterraneanGate : public CGMonolith
  280. {
  281. void onHeroVisit(const CGHeroInstance * h) const override;
  282. void initObj(CRandomGenerator & rand) override;
  283. public:
  284. static void postInit();
  285. template <typename Handler> void serialize(Handler &h, const int version)
  286. {
  287. h & static_cast<CGMonolith&>(*this);
  288. }
  289. };
  290. class DLL_LINKAGE CGWhirlpool : public CGMonolith
  291. {
  292. void onHeroVisit(const CGHeroInstance * h) const override;
  293. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  294. static bool isProtected( const CGHeroInstance * h );
  295. public:
  296. template <typename Handler> void serialize(Handler &h, const int version)
  297. {
  298. h & static_cast<CGMonolith&>(*this);
  299. }
  300. };
  301. class DLL_LINKAGE CGMagicWell : public CGObjectInstance //objects giving bonuses to luck/morale/movement
  302. {
  303. public:
  304. void onHeroVisit(const CGHeroInstance * h) const override;
  305. std::string getHoverText(const CGHeroInstance * hero) const override;
  306. template <typename Handler> void serialize(Handler &h, const int version)
  307. {
  308. h & static_cast<CGObjectInstance&>(*this);
  309. }
  310. };
  311. class DLL_LINKAGE CGSirens : public CGObjectInstance
  312. {
  313. public:
  314. void onHeroVisit(const CGHeroInstance * h) const override;
  315. std::string getHoverText(const CGHeroInstance * hero) const override;
  316. void initObj(CRandomGenerator & rand) override;
  317. template <typename Handler> void serialize(Handler &h, const int version)
  318. {
  319. h & static_cast<CGObjectInstance&>(*this);
  320. }
  321. };
  322. class DLL_LINKAGE CGObservatory : public CGObjectInstance //Redwood observatory
  323. {
  324. public:
  325. void onHeroVisit(const CGHeroInstance * h) 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 CGBoat : public CGObjectInstance
  332. {
  333. public:
  334. ui8 direction;
  335. const CGHeroInstance *hero; //hero on board
  336. void initObj(CRandomGenerator & rand) override;
  337. CGBoat()
  338. {
  339. hero = nullptr;
  340. direction = 4;
  341. }
  342. template <typename Handler> void serialize(Handler &h, const int version)
  343. {
  344. h & static_cast<CGObjectInstance&>(*this) & direction & hero;
  345. }
  346. };
  347. class CGShipyard : public CGObjectInstance, public IShipyard
  348. {
  349. public:
  350. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed
  351. CGShipyard();
  352. void onHeroVisit(const CGHeroInstance * h) const override;
  353. template <typename Handler> void serialize(Handler &h, const int version)
  354. {
  355. h & static_cast<CGObjectInstance&>(*this);
  356. h & static_cast<IShipyard&>(*this);
  357. }
  358. protected:
  359. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  360. };
  361. class DLL_LINKAGE CGMagi : public CGObjectInstance
  362. {
  363. public:
  364. static std::map <si32, std::vector<ObjectInstanceID> > eyelist; //[subID][id], supports multiple sets as in H5
  365. static void reset();
  366. void initObj(CRandomGenerator & rand) override;
  367. void onHeroVisit(const CGHeroInstance * h) const override;
  368. template <typename Handler> void serialize(Handler &h, const int version)
  369. {
  370. h & static_cast<CGObjectInstance&>(*this);
  371. }
  372. };
  373. class DLL_LINKAGE CCartographer : public CPlayersVisited
  374. {
  375. ///behaviour varies depending on surface and floor
  376. public:
  377. void onHeroVisit(const CGHeroInstance * h) const override;
  378. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  379. template <typename Handler> void serialize(Handler &h, const int version)
  380. {
  381. h & static_cast<CPlayersVisited&>(*this);
  382. }
  383. };
  384. class DLL_LINKAGE CGDenOfthieves : public CGObjectInstance
  385. {
  386. void onHeroVisit(const CGHeroInstance * h) const override;
  387. };
  388. class DLL_LINKAGE CGObelisk : public CPlayersVisited
  389. {
  390. public:
  391. static const int OBJPROP_INC = 20;
  392. static ui8 obeliskCount; //how many obelisks are on map
  393. static std::map<TeamID, ui8> visited; //map: team_id => how many obelisks has been visited
  394. void onHeroVisit(const CGHeroInstance * h) const override;
  395. void initObj(CRandomGenerator & rand) override;
  396. std::string getHoverText(PlayerColor player) const override;
  397. static void reset();
  398. template <typename Handler> void serialize(Handler &h, const int version)
  399. {
  400. h & static_cast<CPlayersVisited&>(*this);
  401. }
  402. protected:
  403. void setPropertyDer(ui8 what, ui32 val) override;
  404. };
  405. class DLL_LINKAGE CGLighthouse : public CGObjectInstance
  406. {
  407. public:
  408. void onHeroVisit(const CGHeroInstance * h) const override;
  409. void initObj(CRandomGenerator & rand) override;
  410. std::string getHoverText(PlayerColor player) const override;
  411. template <typename Handler> void serialize(Handler &h, const int version)
  412. {
  413. h & static_cast<CGObjectInstance&>(*this);
  414. }
  415. void giveBonusTo( PlayerColor player ) const;
  416. protected:
  417. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  418. };