MiscObjects.h 15 KB

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