MiscObjects.h 17 KB

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