2
0

MiscObjects.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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(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 const 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 const ui32 RANDOM_AMOUNT = 0;
  187. ui32 amount; //0 if random
  188. std::string message;
  189. CGResource();
  190. void onHeroVisit(const CGHeroInstance * h) const override;
  191. void initObj(CRandomGenerator & rand) override;
  192. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  193. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  194. std::string getHoverText(PlayerColor player) const override;
  195. void collectRes(PlayerColor player) const;
  196. template <typename Handler> void serialize(Handler &h, const int version)
  197. {
  198. h & static_cast<CArmedInstance&>(*this);
  199. h & amount;
  200. h & message;
  201. }
  202. protected:
  203. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  204. };
  205. class DLL_LINKAGE CGShrine : public CTeamVisited
  206. {
  207. public:
  208. SpellID spell; //id of spell or NONE if random
  209. void onHeroVisit(const CGHeroInstance * h) const override;
  210. void initObj(CRandomGenerator & rand) override;
  211. std::string getHoverText(PlayerColor player) const override;
  212. std::string getHoverText(const CGHeroInstance * hero) const override;
  213. template <typename Handler> void serialize(Handler &h, const int version)
  214. {
  215. h & static_cast<CTeamVisited&>(*this);;
  216. h & spell;
  217. }
  218. protected:
  219. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  220. };
  221. class DLL_LINKAGE CGMine : public CArmedInstance
  222. {
  223. public:
  224. Res::ERes producedResource;
  225. ui32 producedQuantity;
  226. private:
  227. void onHeroVisit(const CGHeroInstance * h) const override;
  228. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  229. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  230. void flagMine(PlayerColor player) const;
  231. void newTurn(CRandomGenerator & rand) const override;
  232. void initObj(CRandomGenerator & rand) override;
  233. std::string getObjectName() const override;
  234. std::string getHoverText(PlayerColor player) const override;
  235. bool isAbandoned() const;
  236. public:
  237. template <typename Handler> void serialize(Handler &h, const int version)
  238. {
  239. h & static_cast<CArmedInstance&>(*this);
  240. h & producedResource;
  241. h & producedQuantity;
  242. }
  243. ui32 defaultResProduction();
  244. protected:
  245. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  246. };
  247. struct DLL_LINKAGE TeleportChannel
  248. {
  249. enum EPassability {UNKNOWN, IMPASSABLE, PASSABLE};
  250. TeleportChannel() : passability(UNKNOWN) {}
  251. std::vector<ObjectInstanceID> entrances;
  252. std::vector<ObjectInstanceID> exits;
  253. EPassability passability;
  254. template <typename Handler> void serialize(Handler &h, const int version)
  255. {
  256. h & entrances;
  257. h & exits;
  258. h & passability;
  259. }
  260. };
  261. class DLL_LINKAGE CGTeleport : public CGObjectInstance
  262. {
  263. bool isChannelEntrance(ObjectInstanceID id) const;
  264. bool isChannelExit(ObjectInstanceID id) const;
  265. std::vector<ObjectInstanceID> getAllEntrances(bool excludeCurrent = false) const;
  266. protected:
  267. enum EType {UNKNOWN, ENTRANCE, EXIT, BOTH};
  268. EType type;
  269. CGTeleport();
  270. ObjectInstanceID getRandomExit(const CGHeroInstance * h) const;
  271. std::vector<ObjectInstanceID> getAllExits(bool excludeCurrent = false) const;
  272. public:
  273. TeleportChannelID channel;
  274. bool isEntrance() const;
  275. bool isExit() const;
  276. virtual void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const = 0;
  277. static bool isTeleport(const CGObjectInstance * dst);
  278. static bool isConnected(const CGTeleport * src, const CGTeleport * dst);
  279. static bool isConnected(const CGObjectInstance * src, const CGObjectInstance * dst);
  280. static void addToChannel(std::map<TeleportChannelID, std::shared_ptr<TeleportChannel> > &channelsList, const CGTeleport * obj);
  281. static std::vector<ObjectInstanceID> getPassableExits(CGameState * gs, const CGHeroInstance * h, std::vector<ObjectInstanceID> exits);
  282. static bool isExitPassable(CGameState * gs, const CGHeroInstance * h, const CGObjectInstance * obj);
  283. template <typename Handler> void serialize(Handler &h, const int version)
  284. {
  285. h & type;
  286. h & channel;
  287. h & static_cast<CGObjectInstance&>(*this);
  288. }
  289. };
  290. class DLL_LINKAGE CGMonolith : public CGTeleport
  291. {
  292. TeleportChannelID findMeChannel(std::vector<Obj> IDs, int SubID) const;
  293. protected:
  294. void onHeroVisit(const CGHeroInstance * h) const override;
  295. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  296. void initObj(CRandomGenerator & rand) override;
  297. public:
  298. template <typename Handler> void serialize(Handler &h, const int version)
  299. {
  300. h & static_cast<CGTeleport&>(*this);
  301. }
  302. };
  303. class DLL_LINKAGE CGSubterraneanGate : public CGMonolith
  304. {
  305. void onHeroVisit(const CGHeroInstance * h) const override;
  306. void initObj(CRandomGenerator & rand) override;
  307. public:
  308. static void postInit();
  309. template <typename Handler> void serialize(Handler &h, const int version)
  310. {
  311. h & static_cast<CGMonolith&>(*this);
  312. }
  313. };
  314. class DLL_LINKAGE CGWhirlpool : public CGMonolith
  315. {
  316. void onHeroVisit(const CGHeroInstance * h) const override;
  317. void teleportDialogAnswered(const CGHeroInstance *hero, ui32 answer, TTeleportExitsList exits) const override;
  318. static bool isProtected( const CGHeroInstance * h );
  319. public:
  320. template <typename Handler> void serialize(Handler &h, const int version)
  321. {
  322. h & static_cast<CGMonolith&>(*this);
  323. }
  324. };
  325. class DLL_LINKAGE CGSirens : public CGObjectInstance
  326. {
  327. public:
  328. void onHeroVisit(const CGHeroInstance * h) const override;
  329. std::string getHoverText(const CGHeroInstance * hero) const override;
  330. void initObj(CRandomGenerator & rand) override;
  331. template <typename Handler> void serialize(Handler &h, const int version)
  332. {
  333. h & static_cast<CGObjectInstance&>(*this);
  334. }
  335. };
  336. class DLL_LINKAGE CGObservatory : public CGObjectInstance //Redwood observatory
  337. {
  338. public:
  339. void onHeroVisit(const CGHeroInstance * h) const override;
  340. template <typename Handler> void serialize(Handler &h, const int version)
  341. {
  342. h & static_cast<CGObjectInstance&>(*this);
  343. }
  344. };
  345. class DLL_LINKAGE CGBoat : public CGObjectInstance
  346. {
  347. public:
  348. ui8 direction;
  349. const CGHeroInstance *hero; //hero on board
  350. void initObj(CRandomGenerator & rand) override;
  351. CGBoat()
  352. {
  353. hero = nullptr;
  354. direction = 4;
  355. }
  356. template <typename Handler> void serialize(Handler &h, const int version)
  357. {
  358. h & static_cast<CGObjectInstance&>(*this);
  359. h & direction;
  360. h & hero;
  361. }
  362. };
  363. class DLL_LINKAGE CGShipyard : public CGObjectInstance, public IShipyard
  364. {
  365. public:
  366. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed
  367. CGShipyard();
  368. void onHeroVisit(const CGHeroInstance * h) const override;
  369. template <typename Handler> void serialize(Handler &h, const int version)
  370. {
  371. h & static_cast<CGObjectInstance&>(*this);
  372. h & static_cast<IShipyard&>(*this);
  373. }
  374. protected:
  375. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  376. };
  377. class DLL_LINKAGE CGMagi : public CGObjectInstance
  378. {
  379. public:
  380. static std::map <si32, std::vector<ObjectInstanceID> > eyelist; //[subID][id], supports multiple sets as in H5
  381. static void reset();
  382. void initObj(CRandomGenerator & rand) override;
  383. void onHeroVisit(const CGHeroInstance * h) const override;
  384. template <typename Handler> void serialize(Handler &h, const int version)
  385. {
  386. h & static_cast<CGObjectInstance&>(*this);
  387. }
  388. };
  389. class DLL_LINKAGE CCartographer : public CTeamVisited
  390. {
  391. ///behaviour varies depending on surface and floor
  392. public:
  393. void onHeroVisit(const CGHeroInstance * h) const override;
  394. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  395. template <typename Handler> void serialize(Handler &h, const int version)
  396. {
  397. h & static_cast<CTeamVisited&>(*this);
  398. }
  399. };
  400. class DLL_LINKAGE CGDenOfthieves : public CGObjectInstance
  401. {
  402. void onHeroVisit(const CGHeroInstance * h) const override;
  403. };
  404. class DLL_LINKAGE CGObelisk : public CTeamVisited
  405. {
  406. public:
  407. static const int OBJPROP_INC = 20;
  408. static ui8 obeliskCount; //how many obelisks are on map
  409. static std::map<TeamID, ui8> visited; //map: team_id => how many obelisks has been visited
  410. void onHeroVisit(const CGHeroInstance * h) const override;
  411. void initObj(CRandomGenerator & rand) override;
  412. std::string getHoverText(PlayerColor player) const override;
  413. static void reset();
  414. template <typename Handler> void serialize(Handler &h, const int version)
  415. {
  416. h & static_cast<CTeamVisited&>(*this);
  417. }
  418. protected:
  419. void setPropertyDer(ui8 what, ui32 val) override;
  420. };
  421. class DLL_LINKAGE CGLighthouse : public CGObjectInstance
  422. {
  423. public:
  424. void onHeroVisit(const CGHeroInstance * h) const override;
  425. void initObj(CRandomGenerator & rand) override;
  426. std::string getHoverText(PlayerColor player) const override;
  427. template <typename Handler> void serialize(Handler &h, const int version)
  428. {
  429. h & static_cast<CGObjectInstance&>(*this);
  430. }
  431. void giveBonusTo(PlayerColor player, bool onInit = false) const;
  432. protected:
  433. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  434. };
  435. class DLL_LINKAGE CGTerrainPatch : public CGObjectInstance
  436. {
  437. public:
  438. CGTerrainPatch() = default;
  439. virtual bool isTile2Terrain() const override
  440. {
  441. return true;
  442. }
  443. };
  444. VCMI_LIB_NAMESPACE_END