MiscObjects.h 14 KB

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