MiscObjects.h 16 KB

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