2
0

MiscObjects.h 16 KB

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