MiscObjects.h 14 KB

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