MiscObjects.h 15 KB

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