MiscObjects.h 14 KB

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