MiscObjects.h 15 KB

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