MiscObjects.h 15 KB

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