MiscObjects.h 15 KB

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