CObjectHandler.h 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. #pragma once
  2. #include "../lib/CCreatureSet.h"
  3. #include "../lib/CTownHandler.h"
  4. #include "../lib/CDefObjInfoHandler.h"
  5. #include "CArtHandler.h"
  6. #include "../lib/ConstTransitivePtr.h"
  7. #include "int3.h"
  8. #include "GameConstants.h"
  9. #include "ResourceSet.h"
  10. #include "CRandomGenerator.h"
  11. /*
  12. * CObjectHandler.h, part of VCMI engine
  13. *
  14. * Authors: listed in file AUTHORS in main folder
  15. *
  16. * License: GNU General Public License v2.0 or later
  17. * Full text of license available in license.txt file, in main folder
  18. *
  19. */
  20. class CGameState;
  21. class CArtifactInstance;
  22. struct MetaString;
  23. struct BattleInfo;
  24. struct QuestInfo;
  25. class IGameCallback;
  26. struct BattleResult;
  27. class CGObjectInstance;
  28. class CScript;
  29. class CObjectScript;
  30. class CGHeroInstance;
  31. class CTown;
  32. class CHero;
  33. class CBuilding;
  34. class CSpell;
  35. class CGTownInstance;
  36. class CGTownBuilding;
  37. class CArtifact;
  38. class CSpecObjInfo;
  39. class CCastleEvent;
  40. struct TerrainTile;
  41. struct InfoWindow;
  42. struct Component;
  43. struct BankConfig;
  44. struct UpdateHerospecialty;
  45. struct NewArtifact;
  46. class CGBoat;
  47. class CArtifactSet;
  48. class CCommanderInstance;
  49. class DLL_LINKAGE CQuest
  50. {
  51. public:
  52. enum Emission {MISSION_NONE = 0, MISSION_LEVEL = 1, MISSION_PRIMARY_STAT = 2, MISSION_KILL_HERO = 3, MISSION_KILL_CREATURE = 4,
  53. MISSION_ART = 5, MISSION_ARMY = 6, MISSION_RESOURCES = 7, MISSION_HERO = 8, MISSION_PLAYER = 9, MISSION_KEYMASTER = 10};
  54. enum Eprogress {NOT_ACTIVE, IN_PROGRESS, COMPLETE};
  55. si32 qid; //unique quest id for serialization / identification
  56. Emission missionType;
  57. Eprogress progress;
  58. si32 lastDay; //after this day (first day is 0) mission cannot be completed; if -1 - no limit
  59. ui32 m13489val;
  60. std::vector<ui32> m2stats;
  61. std::vector<ui16> m5arts; //artifacts id
  62. std::vector<CStackBasicDescriptor> m6creatures; //pair[cre id, cre count], CreatureSet info irrelevant
  63. std::vector<ui32> m7resources; //TODO: use resourceset?
  64. //following field are used only for kill creature/hero missions, the original objects became inaccessible after their removal, so we need to store info needed for messages / hover text
  65. ui8 textOption;
  66. CStackBasicDescriptor stackToKill;
  67. ui8 stackDirection;
  68. std::string heroName; //backup of hero name
  69. si32 heroPortrait;
  70. std::string firstVisitText, nextVisitText, completedText;
  71. bool isCustomFirst, isCustomNext, isCustomComplete;
  72. CQuest(){missionType = MISSION_NONE;}; //default constructor
  73. virtual ~CQuest(){};
  74. virtual bool checkQuest (const CGHeroInstance * h) const; //determines whether the quest is complete or not
  75. virtual void getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  76. virtual void getCompletionText (MetaString &text, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h = nullptr) const;
  77. virtual void getRolloverText (MetaString &text, bool onHover) const; //hover or quest log entry
  78. virtual void completeQuest (const CGHeroInstance * h) const {};
  79. virtual void addReplacements(MetaString &out, const std::string &base) const;
  80. bool operator== (const CQuest & quest) const
  81. {
  82. return (quest.qid == qid);
  83. }
  84. template <typename Handler> void serialize(Handler &h, const int version)
  85. {
  86. h & qid & missionType & progress & lastDay & m13489val & m2stats & m5arts & m6creatures & m7resources
  87. & textOption & stackToKill & stackDirection & heroName & heroPortrait
  88. & firstVisitText & nextVisitText & completedText & isCustomFirst & isCustomNext & isCustomComplete;
  89. }
  90. };
  91. class DLL_LINKAGE IObjectInterface
  92. {
  93. public:
  94. static IGameCallback *cb;
  95. IObjectInterface();
  96. virtual ~IObjectInterface();
  97. virtual void onHeroVisit(const CGHeroInstance * h) const;
  98. virtual void onHeroLeave(const CGHeroInstance * h) const;
  99. virtual void newTurn() const;
  100. virtual void initObj(); //synchr
  101. virtual void setProperty(ui8 what, ui32 val);//synchr
  102. //Called when queries created DURING HERO VISIT are resolved
  103. //First parameter is always hero that visited object and triggered the query
  104. virtual void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const;
  105. virtual void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const;
  106. virtual void garrisonDialogClosed(const CGHeroInstance *hero) const;
  107. virtual void heroLevelUpDone(const CGHeroInstance *hero) const;
  108. //unified interface, AI helpers
  109. virtual bool wasVisited (PlayerColor player) const;
  110. virtual bool wasVisited (const CGHeroInstance * h) const;
  111. static void preInit(); //called before objs receive their initObj
  112. static void postInit();//called after objs receive their initObj
  113. template <typename Handler> void serialize(Handler &h, const int version)
  114. {
  115. logGlobal->errorStream() << "IObjectInterface serialized, unexpected, should not happen!";
  116. }
  117. };
  118. class DLL_LINKAGE IBoatGenerator
  119. {
  120. public:
  121. const CGObjectInstance *o;
  122. IBoatGenerator(const CGObjectInstance *O);
  123. virtual ~IBoatGenerator() {}
  124. virtual int getBoatType() const; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  125. virtual void getOutOffsets(std::vector<int3> &offsets) const =0; //offsets to obj pos when we boat can be placed
  126. int3 bestLocation() const; //returns location when the boat should be placed
  127. enum EGeneratorState {GOOD, BOAT_ALREADY_BUILT, TILE_BLOCKED, NO_WATER};
  128. EGeneratorState shipyardStatus() const; //0 - can buid, 1 - there is already a boat at dest tile, 2 - dest tile is blocked, 3 - no water
  129. void getProblemText(MetaString &out, const CGHeroInstance *visitor = nullptr) const;
  130. template <typename Handler> void serialize(Handler &h, const int version)
  131. {
  132. h & o;
  133. }
  134. };
  135. class DLL_LINKAGE IShipyard : public IBoatGenerator
  136. {
  137. public:
  138. IShipyard(const CGObjectInstance *O);
  139. virtual ~IShipyard() {}
  140. virtual void getBoatCost(std::vector<si32> &cost) const;
  141. static const IShipyard *castFrom(const CGObjectInstance *obj);
  142. static IShipyard *castFrom(CGObjectInstance *obj);
  143. template <typename Handler> void serialize(Handler &h, const int version)
  144. {
  145. h & static_cast<IBoatGenerator&>(*this);
  146. }
  147. };
  148. class DLL_LINKAGE IMarket
  149. {
  150. public:
  151. const CGObjectInstance *o;
  152. IMarket(const CGObjectInstance *O);
  153. virtual ~IMarket() {}
  154. virtual int getMarketEfficiency() const =0;
  155. virtual bool allowsTrade(EMarketMode::EMarketMode mode) const;
  156. virtual int availableUnits(EMarketMode::EMarketMode mode, int marketItemSerial) const; //-1 if unlimited
  157. virtual std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const;
  158. bool getOffer(int id1, int id2, int &val1, int &val2, EMarketMode::EMarketMode mode) const; //val1 - how many units of id1 player has to give to receive val2 units
  159. std::vector<EMarketMode::EMarketMode> availableModes() const;
  160. static const IMarket *castFrom(const CGObjectInstance *obj, bool verbose = true);
  161. template <typename Handler> void serialize(Handler &h, const int version)
  162. {
  163. h & o;
  164. }
  165. };
  166. class DLL_LINKAGE CGObjectInstance : public IObjectInterface
  167. {
  168. public:
  169. mutable std::string hoverName;
  170. int3 pos; //h3m pos
  171. Obj ID;
  172. si32 subID; //normal subID (this one from OH3 maps ;])
  173. ObjectInstanceID id;//number of object in map's vector
  174. ObjectTemplate appearance;
  175. PlayerColor tempOwner;
  176. bool blockVisit; //if non-zero then blocks the tile but is visitable from neighbouring tile
  177. virtual ui8 getPassableness() const; //bitmap - if the bit is set the corresponding player can pass through the visitable tiles of object, even if it's blockvis; if not set - default properties from definfo are used
  178. virtual int3 getSightCenter() const; //"center" tile from which the sight distance is calculated
  179. virtual int getSightRadious() const; //sight distance (should be used if player-owned structure)
  180. bool passableFor(PlayerColor color) const;
  181. void getSightTiles(std::unordered_set<int3, ShashInt3> &tiles) const; //returns reference to the set
  182. PlayerColor getOwner() const;
  183. void setOwner(PlayerColor ow);
  184. int getWidth() const; //returns width of object graphic in tiles
  185. int getHeight() const; //returns height of object graphic in tiles
  186. virtual bool visitableAt(int x, int y) const; //returns true if object is visitable at location (x, y) (h3m pos)
  187. virtual int3 getVisitableOffset() const; //returns (x,y,0) offset to first visitable tile from bottom right obj tile (0,0,0) (h3m pos)
  188. int3 visitablePos() const;
  189. bool blockingAt(int x, int y) const; //returns true if object is blocking location (x, y) (h3m pos)
  190. bool coveringAt(int x, int y) const; //returns true if object covers with picture location (x, y) (h3m pos)
  191. std::set<int3> getBlockedPos() const; //returns set of positions blocked by this object
  192. std::set<int3> getBlockedOffsets() const; //returns set of relative positions blocked by this object
  193. bool isVisitable() const; //returns true if object is visitable
  194. bool operator<(const CGObjectInstance & cmp) const; //screen printing priority comparing
  195. void hideTiles(PlayerColor ourplayer, int radius) const;
  196. CGObjectInstance();
  197. virtual ~CGObjectInstance();
  198. //CGObjectInstance(const CGObjectInstance & right);
  199. //CGObjectInstance& operator=(const CGObjectInstance & right);
  200. virtual const std::string & getHoverText() const;
  201. ///IObjectInterface
  202. void initObj() override;
  203. void onHeroVisit(const CGHeroInstance * h) const override;
  204. void setProperty(ui8 what, ui32 val) override;//synchr
  205. friend class CGameHandler;
  206. template <typename Handler> void serialize(Handler &h, const int version)
  207. {
  208. h & hoverName & pos & ID & subID & id & tempOwner & blockVisit & appearance;
  209. //definfo is handled by map serializer
  210. }
  211. protected:
  212. virtual void setPropertyDer(ui8 what, ui32 val);//synchr
  213. void getNameVis(std::string &hname) const;
  214. void giveDummyBonus(ObjectInstanceID heroID, ui8 duration = Bonus::ONE_DAY) const;
  215. };
  216. /// function object which can be used to find an object with an specific sub ID
  217. class CGObjectInstanceBySubIdFinder
  218. {
  219. public:
  220. CGObjectInstanceBySubIdFinder(CGObjectInstance * obj);
  221. bool operator()(CGObjectInstance * obj) const;
  222. private:
  223. CGObjectInstance * obj;
  224. };
  225. class CGHeroPlaceholder : public CGObjectInstance
  226. {
  227. public:
  228. //subID stores id of hero type. If it's 0xff then following field is used
  229. ui8 power;
  230. template <typename Handler> void serialize(Handler &h, const int version)
  231. {
  232. h & static_cast<CGObjectInstance&>(*this);
  233. h & power;
  234. }
  235. };
  236. class DLL_LINKAGE CPlayersVisited: public CGObjectInstance
  237. {
  238. public:
  239. std::set<PlayerColor> players; //players that visited this object
  240. bool wasVisited(PlayerColor player) const;
  241. bool wasVisited(TeamID team) const;
  242. void setPropertyDer(ui8 what, ui32 val) override;
  243. template <typename Handler> void serialize(Handler &h, const int version)
  244. {
  245. h & static_cast<CGObjectInstance&>(*this);
  246. h & players;
  247. }
  248. };
  249. class DLL_LINKAGE CArmedInstance: public CGObjectInstance, public CBonusSystemNode, public CCreatureSet
  250. {
  251. public:
  252. BattleInfo *battle; //set to the current battle, if engaged
  253. void randomizeArmy(int type);
  254. virtual void updateMoraleBonusFromArmy();
  255. void armyChanged() override;
  256. //////////////////////////////////////////////////////////////////////////
  257. // int valOfGlobalBonuses(CSelector selector) const; //used only for castle interface ???
  258. virtual CBonusSystemNode *whereShouldBeAttached(CGameState *gs);
  259. virtual CBonusSystemNode *whatShouldBeAttached();
  260. //////////////////////////////////////////////////////////////////////////
  261. CArmedInstance();
  262. template <typename Handler> void serialize(Handler &h, const int version)
  263. {
  264. h & static_cast<CGObjectInstance&>(*this);
  265. h & static_cast<CBonusSystemNode&>(*this);
  266. h & static_cast<CCreatureSet&>(*this);
  267. }
  268. };
  269. class DLL_LINKAGE CGHeroInstance : public CArmedInstance, public IBoatGenerator, public CArtifactSet
  270. {
  271. public:
  272. enum ECanDig
  273. {
  274. CAN_DIG, LACK_OF_MOVEMENT, WRONG_TERRAIN, TILE_OCCUPIED
  275. };
  276. //////////////////////////////////////////////////////////////////////////
  277. ui8 moveDir; //format: 123
  278. // 8 4
  279. // 765
  280. mutable ui8 isStanding, tacticFormationEnabled;
  281. //////////////////////////////////////////////////////////////////////////
  282. ConstTransitivePtr<CHero> type;
  283. TExpType exp; //experience points
  284. ui32 level; //current level of hero
  285. std::string name; //may be custom
  286. std::string biography; //if custom
  287. si32 portrait; //may be custom
  288. si32 mana; // remaining spell points
  289. std::vector<std::pair<SecondarySkill,ui8> > secSkills; //first - ID of skill, second - level of skill (1 - basic, 2 - adv., 3 - expert); if hero has ability (-1, -1) it meansthat it should have default secondary abilities
  290. ui32 movement; //remaining movement points
  291. ui8 sex;
  292. bool inTownGarrison; // if hero is in town garrison
  293. ConstTransitivePtr<CGTownInstance> visitedTown; //set if hero is visiting town or in the town garrison
  294. ConstTransitivePtr<CCommanderInstance> commander;
  295. const CGBoat *boat; //set to CGBoat when sailing
  296. //std::vector<const CArtifact*> artifacts; //hero's artifacts from bag
  297. //std::map<ui16, const CArtifact*> artifWorn; //map<position,artifact_id>; positions: 0 - head; 1 - shoulders; 2 - neck; 3 - right hand; 4 - left hand; 5 - torso; 6 - right ring; 7 - left ring; 8 - feet; 9 - misc1; 10 - misc2; 11 - misc3; 12 - misc4; 13 - mach1; 14 - mach2; 15 - mach3; 16 - mach4; 17 - spellbook; 18 - misc5
  298. std::set<SpellID> spells; //known spells (spell IDs)
  299. struct DLL_LINKAGE Patrol
  300. {
  301. Patrol(){patrolling=false;patrolRadious=-1;};
  302. bool patrolling;
  303. ui32 patrolRadious;
  304. template <typename Handler> void serialize(Handler &h, const int version)
  305. {
  306. h & patrolling & patrolRadious;
  307. }
  308. } patrol;
  309. struct DLL_LINKAGE HeroSpecial : CBonusSystemNode
  310. {
  311. bool growsWithLevel;
  312. HeroSpecial(){growsWithLevel = false;};
  313. template <typename Handler> void serialize(Handler &h, const int version)
  314. {
  315. h & static_cast<CBonusSystemNode&>(*this);
  316. h & growsWithLevel;
  317. }
  318. };
  319. std::vector<HeroSpecial*> specialty;
  320. struct DLL_LINKAGE SecondarySkillsInfo
  321. {
  322. //skills are determined, initialized at map start
  323. //FIXME remove mutable
  324. mutable CRandomGenerator rand;
  325. ui8 magicSchoolCounter;
  326. ui8 wisdomCounter;
  327. void resetMagicSchoolCounter();
  328. void resetWisdomCounter();
  329. template <typename Handler> void serialize(Handler &h, const int version)
  330. {
  331. h & magicSchoolCounter & wisdomCounter & rand;
  332. }
  333. } skillsInfo;
  334. int3 getSightCenter() const; //"center" tile from which the sight distance is calculated
  335. int getSightRadious() const; //sight distance (should be used if player-owned structure)
  336. //////////////////////////////////////////////////////////////////////////
  337. int getBoatType() const; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  338. void getOutOffsets(std::vector<int3> &offsets) const; //offsets to obj pos when we boat can be placed
  339. //////////////////////////////////////////////////////////////////////////
  340. bool hasSpellbook() const;
  341. EAlignment::EAlignment getAlignment() const;
  342. const std::string &getBiography() const;
  343. bool needsLastStack()const;
  344. ui32 getTileCost(const TerrainTile &dest, const TerrainTile &from) const; //move cost - applying pathfinding skill, road and terrain modifiers. NOT includes diagonal move penalty, last move levelling
  345. ui32 getLowestCreatureSpeed() const;
  346. int3 getPosition(bool h3m = false) const; //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  347. si32 manaRegain() const; //how many points of mana can hero regain "naturally" in one day
  348. bool canWalkOnSea() const;
  349. int getCurrentLuck(int stack=-1, bool town=false) const;
  350. int getSpellCost(const CSpell *sp) const; //do not use during battles -> bonuses from army would be ignored
  351. // ----- primary and secondary skill, experience, level handling -----
  352. /// Returns true if hero has lower level than should upon his experience.
  353. bool gainsLevel() const;
  354. /// Returns the next primary skill on level up. Can only be called if hero can gain a level up.
  355. PrimarySkill::PrimarySkill nextPrimarySkill() const;
  356. /// Returns the next secondary skill randomly on level up. Can only be called if hero can gain a level up.
  357. boost::optional<SecondarySkill> nextSecondarySkill() const;
  358. /// Gets 0, 1 or 2 secondary skills which are proposed on hero level up.
  359. std::vector<SecondarySkill> getLevelUpProposedSecondarySkills() const;
  360. ui8 getSecSkillLevel(SecondarySkill skill) const; //0 - no skill
  361. /// Returns true if hero has free secondary skill slot.
  362. bool canLearnSkill() const;
  363. void setPrimarySkill(PrimarySkill::PrimarySkill primarySkill, si64 value, ui8 abs);
  364. void setSecSkillLevel(SecondarySkill which, int val, bool abs);// abs == 0 - changes by value; 1 - sets to value
  365. void levelUp(std::vector<SecondarySkill> skills);
  366. int maxMovePoints(bool onLand) const;
  367. int movementPointsAfterEmbark(int MPsBefore, int basicCost, bool disembark = false) const;
  368. //int getSpellSecLevel(int spell) const; //returns level of secondary ability (fire, water, earth, air magic) known to this hero and applicable to given spell; -1 if error
  369. static int3 convertPosition(int3 src, bool toh3m); //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  370. double getFightingStrength() const; // takes attack / defense skill into account
  371. double getMagicStrength() const; // takes knowledge / spell power skill into account
  372. double getHeroStrength() const; // includes fighting and magic strength
  373. ui64 getTotalStrength() const; // includes fighting strength and army strength
  374. TExpType calculateXp(TExpType exp) const; //apply learning skill
  375. ui8 getSpellSchoolLevel(const CSpell * spell, int *outSelectedSchool = nullptr) const; //returns level on which given spell would be cast by this hero (0 - none, 1 - basic etc); optionally returns number of selected school by arg - 0 - air magic, 1 - fire magic, 2 - water magic, 3 - earth magic,
  376. bool canCastThisSpell(const CSpell * spell) const; //determines if this hero can cast given spell; takes into account existing spell in spellbook, existing spellbook and artifact bonuses
  377. CStackBasicDescriptor calculateNecromancy (const BattleResult &battleResult) const;
  378. void showNecromancyDialog(const CStackBasicDescriptor &raisedStack) const;
  379. ECanDig diggingStatus() const; //0 - can dig; 1 - lack of movement; 2 -
  380. //////////////////////////////////////////////////////////////////////////
  381. void initHero();
  382. void initHero(HeroTypeID SUBID);
  383. void putArtifact(ArtifactPosition pos, CArtifactInstance *art);
  384. void putInBackpack(CArtifactInstance *art);
  385. void initExp();
  386. void initArmy(IArmyDescriptor *dst = nullptr);
  387. //void giveArtifact (ui32 aid);
  388. void pushPrimSkill(PrimarySkill::PrimarySkill which, int val);
  389. ui8 maxlevelsToMagicSchool() const;
  390. ui8 maxlevelsToWisdom() const;
  391. void Updatespecialty();
  392. void recreateSecondarySkillsBonuses();
  393. void updateSkill(SecondarySkill which, int val);
  394. CGHeroInstance();
  395. virtual ~CGHeroInstance();
  396. //////////////////////////////////////////////////////////////////////////
  397. //
  398. ArtBearer::ArtBearer bearerType() const override;
  399. //////////////////////////////////////////////////////////////////////////
  400. CBonusSystemNode *whereShouldBeAttached(CGameState *gs) override;
  401. std::string nodeName() const override;
  402. void deserializationFix();
  403. void initObj() override;
  404. void onHeroVisit(const CGHeroInstance * h) const override;
  405. const std::string & getHoverText() const override;
  406. protected:
  407. void setPropertyDer(ui8 what, ui32 val) override;//synchr
  408. private:
  409. void levelUpAutomatically();
  410. public:
  411. template <typename Handler> void serialize(Handler &h, const int version)
  412. {
  413. h & static_cast<CArmedInstance&>(*this);
  414. h & static_cast<CArtifactSet&>(*this);
  415. h & exp & level & name & biography & portrait & mana & secSkills & movement
  416. & sex & inTownGarrison & spells & patrol & moveDir & skillsInfo;
  417. h & visitedTown & boat;
  418. h & type & specialty & commander;
  419. BONUS_TREE_DESERIALIZATION_FIX
  420. //visitied town pointer will be restored by map serialization method
  421. }
  422. };
  423. class DLL_LINKAGE CSpecObjInfo
  424. {
  425. public:
  426. virtual ~CSpecObjInfo(){};
  427. PlayerColor player; //owner
  428. };
  429. class DLL_LINKAGE CCreGenAsCastleInfo : public virtual CSpecObjInfo
  430. {
  431. public:
  432. bool asCastle;
  433. ui32 identifier;
  434. ui8 castles[2]; //allowed castles
  435. };
  436. class DLL_LINKAGE CCreGenLeveledInfo : public virtual CSpecObjInfo
  437. {
  438. public:
  439. ui8 minLevel, maxLevel; //minimal and maximal level of creature in dwelling: <0, 6>
  440. };
  441. class DLL_LINKAGE CCreGenLeveledCastleInfo : public CCreGenAsCastleInfo, public CCreGenLeveledInfo
  442. {
  443. };
  444. class DLL_LINKAGE CGDwelling : public CArmedInstance
  445. {
  446. public:
  447. typedef std::vector<std::pair<ui32, std::vector<CreatureID> > > TCreaturesSet;
  448. CSpecObjInfo * info; //h3m info about dewlling
  449. TCreaturesSet creatures; //creatures[level] -> <vector of alternative ids (base creature and upgrades, creatures amount>
  450. template <typename Handler> void serialize(Handler &h, const int version)
  451. {
  452. h & static_cast<CArmedInstance&>(*this) & creatures;
  453. }
  454. void initObj() override;
  455. void onHeroVisit(const CGHeroInstance * h) const override;
  456. void newTurn() const override;
  457. void setProperty(ui8 what, ui32 val) override;
  458. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  459. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  460. private:
  461. void heroAcceptsCreatures(const CGHeroInstance *h) const;
  462. };
  463. class DLL_LINKAGE CGVisitableOPH : public CGObjectInstance //objects visitable only once per hero
  464. {
  465. public:
  466. std::set<ObjectInstanceID> visitors; //ids of heroes who have visited this obj
  467. TResources treePrice; //used only by trees of knowledge: empty, 2000 gold, 10 gems
  468. const std::string & getHoverText() const override;
  469. void onHeroVisit(const CGHeroInstance * h) const override;
  470. void initObj() override;
  471. bool wasVisited (const CGHeroInstance * h) const override;
  472. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  473. template <typename Handler> void serialize(Handler &h, const int version)
  474. {
  475. h & static_cast<CGObjectInstance&>(*this);
  476. h & visitors & treePrice;
  477. }
  478. protected:
  479. void setPropertyDer(ui8 what, ui32 val) override;//synchr
  480. private:
  481. void onNAHeroVisit(const CGHeroInstance * h, bool alreadyVisited) const;
  482. ///dialog callbacks
  483. void treeSelected(const CGHeroInstance * h, ui32 result) const;
  484. void schoolSelected(const CGHeroInstance * h, ui32 which) const;
  485. void arenaSelected(const CGHeroInstance * h, int primSkill) const;
  486. };
  487. class DLL_LINKAGE CGTownBuilding : public IObjectInterface
  488. {
  489. ///basic class for town structures handled as map objects
  490. public:
  491. BuildingID ID; //from buildig list
  492. si32 id; //identifies its index on towns vector
  493. CGTownInstance *town;
  494. template <typename Handler> void serialize(Handler &h, const int version)
  495. {
  496. h & ID & id;
  497. }
  498. };
  499. class DLL_LINKAGE COPWBonus : public CGTownBuilding
  500. {///used for OPW bonusing structures
  501. public:
  502. std::set<si32> visitors;
  503. void setProperty(ui8 what, ui32 val) override;
  504. void onHeroVisit (const CGHeroInstance * h) const override;
  505. COPWBonus (BuildingID index, CGTownInstance *TOWN);
  506. COPWBonus (){ID = BuildingID::NONE; town = nullptr;};
  507. template <typename Handler> void serialize(Handler &h, const int version)
  508. {
  509. h & static_cast<CGTownBuilding&>(*this);
  510. h & visitors;
  511. }
  512. };
  513. class DLL_LINKAGE CTownBonus : public CGTownBuilding
  514. {
  515. ///used for one-time bonusing structures
  516. ///feel free to merge inheritance tree
  517. public:
  518. std::set<ObjectInstanceID> visitors;
  519. void setProperty(ui8 what, ui32 val) override;
  520. void onHeroVisit (const CGHeroInstance * h) const override;
  521. CTownBonus (BuildingID index, CGTownInstance *TOWN);
  522. CTownBonus (){ID = BuildingID::NONE; town = nullptr;};
  523. template <typename Handler> void serialize(Handler &h, const int version)
  524. {
  525. h & static_cast<CGTownBuilding&>(*this);
  526. h & visitors;
  527. }
  528. };
  529. class DLL_LINKAGE CTownAndVisitingHero : public CBonusSystemNode
  530. {
  531. public:
  532. CTownAndVisitingHero();
  533. };
  534. struct DLL_LINKAGE GrowthInfo
  535. {
  536. struct Entry
  537. {
  538. int count;
  539. std::string description;
  540. Entry(const std::string &format, int _count);
  541. Entry(int subID, BuildingID building, int _count);
  542. };
  543. std::vector<Entry> entries;
  544. int totalGrowth() const;
  545. };
  546. class DLL_LINKAGE CGTownInstance : public CGDwelling, public IShipyard, public IMarket
  547. {
  548. public:
  549. enum EFortLevel {NONE = 0, FORT = 1, CITADEL = 2, CASTLE = 3};
  550. CTownAndVisitingHero townAndVis;
  551. const CTown * town;
  552. std::string name; // name of town
  553. si32 builded; //how many buildings has been built this turn
  554. si32 destroyed; //how many buildings has been destroyed this turn
  555. ConstTransitivePtr<CGHeroInstance> garrisonHero, visitingHero;
  556. ui32 identifier; //special identifier from h3m (only > RoE maps)
  557. si32 alignment;
  558. std::set<BuildingID> forbiddenBuildings, builtBuildings;
  559. std::vector<CGTownBuilding*> bonusingBuildings;
  560. std::vector<SpellID> possibleSpells, obligatorySpells;
  561. std::vector<std::vector<SpellID> > spells; //spells[level] -> vector of spells, first will be available in guild
  562. std::list<CCastleEvent> events;
  563. std::pair<si32, si32> bonusValue;//var to store town bonuses (rampart = resources from mystic pond);
  564. //////////////////////////////////////////////////////////////////////////
  565. static std::vector<const CArtifact *> merchantArtifacts; //vector of artifacts available at Artifact merchant, NULLs possible (for making empty space when artifact is bought)
  566. static std::vector<int> universitySkills;//skills for university of magic
  567. template <typename Handler> void serialize(Handler &h, const int version)
  568. {
  569. h & static_cast<CGDwelling&>(*this);
  570. h & static_cast<IShipyard&>(*this);
  571. h & static_cast<IMarket&>(*this);
  572. h & name & builded & destroyed & identifier;
  573. h & garrisonHero & visitingHero;
  574. h & alignment & forbiddenBuildings & builtBuildings & bonusValue
  575. & possibleSpells & obligatorySpells & spells & /*strInfo & */events & bonusingBuildings;
  576. for (std::vector<CGTownBuilding*>::iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  577. (*i)->town = this;
  578. h & town & townAndVis;
  579. BONUS_TREE_DESERIALIZATION_FIX
  580. vstd::erase_if(builtBuildings, [this](BuildingID building) -> bool
  581. {
  582. if(!town->buildings.count(building) || !town->buildings.at(building))
  583. {
  584. logGlobal->errorStream() << boost::format("#1444-like issue in CGTownInstance::serialize. From town %s at %s removing the bogus builtBuildings item %s")
  585. % name % pos % building;
  586. return true;
  587. }
  588. return false;
  589. });
  590. }
  591. //////////////////////////////////////////////////////////////////////////
  592. CBonusSystemNode *whatShouldBeAttached() override;
  593. std::string nodeName() const override;
  594. void updateMoraleBonusFromArmy() override;
  595. void deserializationFix();
  596. void recreateBuildingsBonuses();
  597. bool addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, TPropagatorPtr &prop, int subtype = -1); //returns true if building is built and bonus has been added
  598. bool addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, int subtype = -1); //convienence version of above
  599. void setVisitingHero(CGHeroInstance *h);
  600. void setGarrisonedHero(CGHeroInstance *h);
  601. const CArmedInstance *getUpperArmy() const; //garrisoned hero if present or the town itself
  602. //////////////////////////////////////////////////////////////////////////
  603. ui8 getPassableness() const; //bitmap - if the bit is set the corresponding player can pass through the visitable tiles of object, even if it's blockvis; if not set - default properties from definfo are used
  604. int3 getSightCenter() const override; //"center" tile from which the sight distance is calculated
  605. int getSightRadious() const override; //returns sight distance
  606. int getBoatType() const; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  607. void getOutOffsets(std::vector<int3> &offsets) const; //offsets to obj pos when we boat can be placed
  608. int getMarketEfficiency() const override; //=market count
  609. bool allowsTrade(EMarketMode::EMarketMode mode) const;
  610. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const;
  611. void updateAppearance();
  612. //////////////////////////////////////////////////////////////////////////
  613. bool needsLastStack() const;
  614. CGTownInstance::EFortLevel fortLevel() const;
  615. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  616. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  617. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  618. int creatureGrowth(const int & level) const;
  619. GrowthInfo getGrowthInfo(int level) const;
  620. bool hasFort() const;
  621. bool hasCapitol() const;
  622. //checks if building is constructed and town has same subID
  623. bool hasBuilt(BuildingID buildingID) const;
  624. bool hasBuilt(BuildingID buildingID, int townID) const;
  625. TResources dailyIncome() const; //calculates daily income of this town
  626. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  627. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  628. int getTownLevel() const;
  629. void removeCapitols (PlayerColor owner) const;
  630. void addHeroToStructureVisitors(const CGHeroInstance *h, si32 structureInstanceID) const; //hero must be visiting or garrisoned in town
  631. CGTownInstance();
  632. virtual ~CGTownInstance();
  633. ///IObjectInterface overrides
  634. void newTurn() const override;
  635. void onHeroVisit(const CGHeroInstance * h) const override;
  636. void onHeroLeave(const CGHeroInstance * h) const override;
  637. void initObj() override;
  638. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  639. protected:
  640. void setPropertyDer(ui8 what, ui32 val) override;
  641. };
  642. class DLL_LINKAGE CGPandoraBox : public CArmedInstance
  643. {
  644. public:
  645. std::string message;
  646. bool hasGuardians; //helper - after battle even though we have no stacks, allows us to know that there was battle
  647. //gained things:
  648. ui32 gainedExp;
  649. si32 manaDiff; //amount of gained / lost mana
  650. si32 moraleDiff; //morale modifier
  651. si32 luckDiff; //luck modifier
  652. TResources resources;//gained / lost resources
  653. std::vector<si32> primskills;//gained / lost prim skills
  654. std::vector<SecondarySkill> abilities; //gained abilities
  655. std::vector<si32> abilityLevels; //levels of gained abilities
  656. std::vector<ArtifactID> artifacts; //gained artifacts
  657. std::vector<SpellID> spells; //gained spells
  658. CCreatureSet creatures; //gained creatures
  659. void initObj() override;
  660. void onHeroVisit(const CGHeroInstance * h) const override;
  661. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  662. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  663. void heroLevelUpDone(const CGHeroInstance *hero) const override;
  664. template <typename Handler> void serialize(Handler &h, const int version)
  665. {
  666. h & static_cast<CArmedInstance&>(*this);
  667. h & message & hasGuardians & gainedExp & manaDiff & moraleDiff & luckDiff & resources & primskills
  668. & abilities & abilityLevels & artifacts & spells & creatures;
  669. }
  670. protected:
  671. void giveContentsUpToExp(const CGHeroInstance *h) const;
  672. void giveContentsAfterExp(const CGHeroInstance *h) const;
  673. private:
  674. void getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const;
  675. void getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const;
  676. };
  677. class DLL_LINKAGE CGEvent : public CGPandoraBox //event objects
  678. {
  679. public:
  680. bool removeAfterVisit; //true if event is removed after occurring
  681. ui8 availableFor; //players whom this event is available for
  682. bool computerActivate; //true if computer player can activate this event
  683. bool humanActivate; //true if human player can activate this event
  684. template <typename Handler> void serialize(Handler &h, const int version)
  685. {
  686. h & static_cast<CGPandoraBox &>(*this);
  687. h & removeAfterVisit & availableFor & computerActivate & humanActivate;
  688. }
  689. void onHeroVisit(const CGHeroInstance * h) const override;
  690. private:
  691. void activated(const CGHeroInstance * h) const;
  692. };
  693. class DLL_LINKAGE CGCreature : public CArmedInstance //creatures on map
  694. {
  695. enum Action {
  696. FIGHT = -2, FLEE = -1, JOIN_FOR_FREE = 0 //values > 0 mean gold price
  697. };
  698. public:
  699. ui32 identifier; //unique code for this monster (used in missions)
  700. 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)
  701. std::string message; //message printed for attacking hero
  702. TResources resources; // resources given to hero that has won with monsters
  703. ArtifactID gainedArtifact; //ID of artifact gained to hero, -1 if none
  704. bool neverFlees; //if true, the troops will never flee
  705. bool notGrowingTeam; //if true, number of units won't grow
  706. ui64 temppower; //used to handle fractional stack growth for tiny stacks
  707. bool refusedJoining;
  708. void onHeroVisit(const CGHeroInstance * h) const override;
  709. const std::string & getHoverText() const override;
  710. void initObj() override;
  711. void newTurn() const override;
  712. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  713. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  714. struct DLL_LINKAGE formationInfo // info about merging stacks after battle back into one
  715. {
  716. si32 basicType;
  717. ui32 randomFormation; //random seed used to determine number of stacks and is there's upgraded stack
  718. template <typename Handler> void serialize(Handler &h, const int version)
  719. {
  720. h & basicType & randomFormation;
  721. }
  722. } formation;
  723. template <typename Handler> void serialize(Handler &h, const int version)
  724. {
  725. h & static_cast<CArmedInstance&>(*this);
  726. h & identifier & character & message & resources & gainedArtifact & neverFlees & notGrowingTeam & temppower;
  727. h & refusedJoining & formation;
  728. }
  729. protected:
  730. void setPropertyDer(ui8 what, ui32 val) override;
  731. private:
  732. void fight(const CGHeroInstance *h) const;
  733. void flee( const CGHeroInstance * h ) const;
  734. void fleeDecision(const CGHeroInstance *h, ui32 pursue) const;
  735. void joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const;
  736. 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)
  737. };
  738. class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  739. {
  740. public:
  741. std::string message;
  742. void onHeroVisit(const CGHeroInstance * h) const override;
  743. void initObj() override;
  744. template <typename Handler> void serialize(Handler &h, const int version)
  745. {
  746. h & static_cast<CGObjectInstance&>(*this);
  747. h & message;
  748. }
  749. };
  750. class DLL_LINKAGE IQuestObject
  751. {
  752. public:
  753. CQuest * quest;
  754. IQuestObject(): quest(new CQuest()){};
  755. virtual ~IQuestObject() {};
  756. virtual void getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  757. virtual bool checkQuest (const CGHeroInstance * h) const;
  758. template <typename Handler> void serialize(Handler &h, const int version)
  759. {
  760. h & quest;
  761. }
  762. };
  763. class DLL_LINKAGE CGSeerHut : public CArmedInstance, public IQuestObject //army is used when giving reward
  764. {
  765. public:
  766. enum ERewardType {NOTHING, EXPERIENCE, MANA_POINTS, MORALE_BONUS, LUCK_BONUS, RESOURCES, PRIMARY_SKILL, SECONDARY_SKILL, ARTIFACT, SPELL, CREATURE};
  767. ERewardType rewardType;
  768. si32 rID; //reward ID
  769. si32 rVal; //reward value
  770. std::string seerName;
  771. CGSeerHut() : IQuestObject(){};
  772. void initObj() override;
  773. const std::string & getHoverText() const override;
  774. void newTurn() const override;
  775. void onHeroVisit(const CGHeroInstance * h) const override;
  776. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  777. virtual void init();
  778. int checkDirection() const; //calculates the region of map where monster is placed
  779. void setObjToKill(); //remember creatures / heroes to kill after they are initialized
  780. const CGHeroInstance *getHeroToKill(bool allowNull = false) const;
  781. const CGCreature *getCreatureToKill(bool allowNull = false) const;
  782. void getRolloverText (MetaString &text, bool onHover) const;
  783. void getCompletionText(MetaString &text, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h = nullptr) const;
  784. void finishQuest (const CGHeroInstance * h, ui32 accept) const; //common for both objects
  785. virtual void completeQuest (const CGHeroInstance * h) const;
  786. template <typename Handler> void serialize(Handler &h, const int version)
  787. {
  788. h & static_cast<CArmedInstance&>(*this) & static_cast<IQuestObject&>(*this);
  789. h & rewardType & rID & rVal & seerName;
  790. }
  791. protected:
  792. void setPropertyDer(ui8 what, ui32 val) override;
  793. };
  794. class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  795. {
  796. public:
  797. CGQuestGuard() : CGSeerHut(){};
  798. void init() override;
  799. void completeQuest (const CGHeroInstance * h) const override;
  800. template <typename Handler> void serialize(Handler &h, const int version)
  801. {
  802. h & static_cast<CGSeerHut&>(*this);
  803. }
  804. };
  805. class DLL_LINKAGE CGWitchHut : public CPlayersVisited
  806. {
  807. public:
  808. std::vector<si32> allowedAbilities;
  809. ui32 ability;
  810. const std::string & getHoverText() const override;
  811. void onHeroVisit(const CGHeroInstance * h) const override;
  812. void initObj() override;
  813. template <typename Handler> void serialize(Handler &h, const int version)
  814. {
  815. h & static_cast<CPlayersVisited&>(*this);
  816. h & allowedAbilities & ability;
  817. }
  818. };
  819. class DLL_LINKAGE CGScholar : public CGObjectInstance
  820. {
  821. public:
  822. enum EBonusType {PRIM_SKILL, SECONDARY_SKILL, SPELL, RANDOM = 255};
  823. EBonusType bonusType;
  824. ui16 bonusID; //ID of skill/spell
  825. // void giveAnyBonus(const CGHeroInstance * h) const; //TODO: remove
  826. void onHeroVisit(const CGHeroInstance * h) const override;
  827. void initObj() override;
  828. template <typename Handler> void serialize(Handler &h, const int version)
  829. {
  830. h & static_cast<CGObjectInstance&>(*this);
  831. h & bonusType & bonusID;
  832. }
  833. };
  834. class DLL_LINKAGE CGGarrison : public CArmedInstance
  835. {
  836. public:
  837. bool removableUnits;
  838. ui8 getPassableness() const;
  839. void onHeroVisit(const CGHeroInstance * h) const override;
  840. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  841. template <typename Handler> void serialize(Handler &h, const int version)
  842. {
  843. h & static_cast<CArmedInstance&>(*this);
  844. h & removableUnits;
  845. }
  846. };
  847. class DLL_LINKAGE CGArtifact : public CArmedInstance
  848. {
  849. public:
  850. CArtifactInstance *storedArtifact;
  851. std::string message;
  852. void onHeroVisit(const CGHeroInstance * h) const override;
  853. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  854. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  855. void pick( const CGHeroInstance * h ) const;
  856. void initObj() override;
  857. template <typename Handler> void serialize(Handler &h, const int version)
  858. {
  859. h & static_cast<CArmedInstance&>(*this);
  860. h & message & storedArtifact;
  861. }
  862. };
  863. class DLL_LINKAGE CGResource : public CArmedInstance
  864. {
  865. public:
  866. ui32 amount; //0 if random
  867. std::string message;
  868. void onHeroVisit(const CGHeroInstance * h) const override;
  869. void initObj() override;
  870. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  871. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  872. void collectRes(PlayerColor player) const;
  873. template <typename Handler> void serialize(Handler &h, const int version)
  874. {
  875. h & static_cast<CArmedInstance&>(*this);
  876. h & amount & message;
  877. }
  878. };
  879. class DLL_LINKAGE CGPickable : public CGObjectInstance //campfire, treasure chest, Flotsam, Shipwreck Survivor, Sea Chest
  880. {
  881. public:
  882. ui32 type, val1, val2;
  883. void onHeroVisit(const CGHeroInstance * h) const override;
  884. void initObj() override;
  885. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  886. template <typename Handler> void serialize(Handler &h, const int version)
  887. {
  888. h & static_cast<CGObjectInstance&>(*this);
  889. h & type & val1 & val2;
  890. }
  891. };
  892. class DLL_LINKAGE CGShrine : public CPlayersVisited
  893. {
  894. public:
  895. SpellID spell; //id of spell or NONE if random
  896. void onHeroVisit(const CGHeroInstance * h) const override;
  897. void initObj() override;
  898. const std::string & getHoverText() const override;
  899. template <typename Handler> void serialize(Handler &h, const int version)
  900. {
  901. h & static_cast<CPlayersVisited&>(*this);;
  902. h & spell;
  903. }
  904. };
  905. class DLL_LINKAGE CGMine : public CArmedInstance
  906. {
  907. public:
  908. Res::ERes producedResource;
  909. ui32 producedQuantity;
  910. void onHeroVisit(const CGHeroInstance * h) const override;
  911. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  912. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  913. void flagMine(PlayerColor player) const;
  914. void newTurn() const override;
  915. void initObj() override;
  916. template <typename Handler> void serialize(Handler &h, const int version)
  917. {
  918. h & static_cast<CArmedInstance&>(*this);
  919. h & producedResource & producedQuantity;
  920. }
  921. ui32 defaultResProduction();
  922. };
  923. class DLL_LINKAGE CGVisitableOPW : public CGObjectInstance //objects visitable OPW
  924. {
  925. public:
  926. ui8 visited; //true if object has been visited this week
  927. bool wasVisited(PlayerColor player) const;
  928. void onHeroVisit(const CGHeroInstance * h) const override;
  929. virtual void newTurn() const override;
  930. template <typename Handler> void serialize(Handler &h, const int version)
  931. {
  932. h & static_cast<CGObjectInstance&>(*this);
  933. h & visited;
  934. }
  935. protected:
  936. void setPropertyDer(ui8 what, ui32 val) override;
  937. };
  938. class DLL_LINKAGE CGTeleport : public CGObjectInstance //teleports and subterranean gates
  939. {
  940. public:
  941. static std::map<Obj, std::map<int, std::vector<ObjectInstanceID> > > objs; //teleports: map[ID][subID] => vector of ids
  942. static std::vector<std::pair<ObjectInstanceID, ObjectInstanceID> > gates; //subterranean gates: pairs of ids
  943. void onHeroVisit(const CGHeroInstance * h) const override;
  944. void initObj() override;
  945. static void postInit();
  946. static ObjectInstanceID getMatchingGate(ObjectInstanceID id); //receives id of one subterranean gate and returns id of the paired one, -1 if none
  947. template <typename Handler> void serialize(Handler &h, const int version)
  948. {
  949. h & static_cast<CGObjectInstance&>(*this);
  950. }
  951. };
  952. class DLL_LINKAGE CGBonusingObject : public CGObjectInstance //objects giving bonuses to luck/morale/movement
  953. {
  954. public:
  955. bool wasVisited (const CGHeroInstance * h) const;
  956. void onHeroVisit(const CGHeroInstance * h) const override;
  957. const std::string & getHoverText() const override;
  958. void initObj() override;
  959. template <typename Handler> void serialize(Handler &h, const int version)
  960. {
  961. h & static_cast<CGObjectInstance&>(*this);
  962. }
  963. };
  964. class DLL_LINKAGE CGMagicSpring : public CGVisitableOPW
  965. {///unfortunately, this one is quite different than others
  966. enum EVisitedEntrance
  967. {
  968. CLEAR = 0, LEFT = 1, RIGHT
  969. };
  970. public:
  971. EVisitedEntrance visitedTile; //only one entrance was visited - there are two
  972. std::vector<int3> getVisitableOffsets() const;
  973. int3 getVisitableOffset() const override;
  974. void setPropertyDer(ui8 what, ui32 val) override;
  975. void newTurn() const override;
  976. void onHeroVisit(const CGHeroInstance * h) const override;
  977. const std::string & getHoverText() const override;
  978. template <typename Handler> void serialize(Handler &h, const int version)
  979. {
  980. h & static_cast<CGObjectInstance&>(*this);
  981. h & visitedTile & visited;
  982. }
  983. };
  984. class DLL_LINKAGE CGMagicWell : public CGObjectInstance //objects giving bonuses to luck/morale/movement
  985. {
  986. public:
  987. void onHeroVisit(const CGHeroInstance * h) const override;
  988. const std::string & getHoverText() const override;
  989. template <typename Handler> void serialize(Handler &h, const int version)
  990. {
  991. h & static_cast<CGObjectInstance&>(*this);
  992. }
  993. };
  994. class DLL_LINKAGE CGSirens : public CGObjectInstance
  995. {
  996. public:
  997. void onHeroVisit(const CGHeroInstance * h) const override;
  998. const std::string & getHoverText() const override;
  999. void initObj() override;
  1000. template <typename Handler> void serialize(Handler &h, const int version)
  1001. {
  1002. h & static_cast<CGObjectInstance&>(*this);
  1003. }
  1004. };
  1005. class DLL_LINKAGE CGObservatory : public CGObjectInstance //Redwood observatory
  1006. {
  1007. public:
  1008. void onHeroVisit(const CGHeroInstance * h) const override;
  1009. template <typename Handler> void serialize(Handler &h, const int version)
  1010. {
  1011. h & static_cast<CGObjectInstance&>(*this);
  1012. }
  1013. };
  1014. class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  1015. {
  1016. public:
  1017. static std::map <PlayerColor, std::set <ui8> > playerKeyMap; //[players][keysowned]
  1018. //SubID 0 - lightblue, 1 - green, 2 - red, 3 - darkblue, 4 - brown, 5 - purple, 6 - white, 7 - black
  1019. const std::string getName() const; //depending on color
  1020. bool wasMyColorVisited (PlayerColor player) const;
  1021. const std::string & getHoverText() const override;
  1022. template <typename Handler> void serialize(Handler &h, const int version)
  1023. {
  1024. h & static_cast<CGObjectInstance&>(*this);
  1025. }
  1026. protected:
  1027. void setPropertyDer(ui8 what, ui32 val) override;
  1028. };
  1029. class DLL_LINKAGE CGKeymasterTent : public CGKeys
  1030. {
  1031. public:
  1032. bool wasVisited (PlayerColor player) const;
  1033. void onHeroVisit(const CGHeroInstance * h) const override;
  1034. template <typename Handler> void serialize(Handler &h, const int version)
  1035. {
  1036. h & static_cast<CGObjectInstance&>(*this);
  1037. }
  1038. };
  1039. class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  1040. {
  1041. public:
  1042. CGBorderGuard() : IQuestObject(){};
  1043. void initObj() override;
  1044. void onHeroVisit(const CGHeroInstance * h) const override;
  1045. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  1046. void getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  1047. void getRolloverText (MetaString &text, bool onHover) const;
  1048. bool checkQuest (const CGHeroInstance * h) const;
  1049. template <typename Handler> void serialize(Handler &h, const int version)
  1050. {
  1051. h & static_cast<IQuestObject&>(*this);
  1052. h & static_cast<CGObjectInstance&>(*this);
  1053. h & blockVisit;
  1054. }
  1055. };
  1056. class DLL_LINKAGE CGBorderGate : public CGBorderGuard
  1057. {
  1058. public:
  1059. CGBorderGate() : CGBorderGuard(){};
  1060. void onHeroVisit(const CGHeroInstance * h) const override;
  1061. ui8 getPassableness() const override;
  1062. template <typename Handler> void serialize(Handler &h, const int version)
  1063. {
  1064. h & static_cast<CGBorderGuard&>(*this); //need to serialize or object will be empty
  1065. }
  1066. };
  1067. class DLL_LINKAGE CGBoat : public CGObjectInstance
  1068. {
  1069. public:
  1070. ui8 direction;
  1071. const CGHeroInstance *hero; //hero on board
  1072. void initObj() override;
  1073. CGBoat()
  1074. {
  1075. hero = nullptr;
  1076. direction = 4;
  1077. }
  1078. template <typename Handler> void serialize(Handler &h, const int version)
  1079. {
  1080. h & static_cast<CGObjectInstance&>(*this) & direction & hero;
  1081. }
  1082. };
  1083. class DLL_LINKAGE CGOnceVisitable : public CPlayersVisited
  1084. ///wagon, corpse, lean to, warriors tomb
  1085. {
  1086. public:
  1087. ui8 artOrRes; //0 - nothing; 1 - artifact; 2 - resource
  1088. ui32 bonusType, //id of res or artifact
  1089. bonusVal; //resource amount (or not used)
  1090. void onHeroVisit(const CGHeroInstance * h) const override;
  1091. const std::string & getHoverText() const override;
  1092. void initObj() override;
  1093. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  1094. template <typename Handler> void serialize(Handler &h, const int version)
  1095. {
  1096. h & static_cast<CPlayersVisited&>(*this);;
  1097. h & artOrRes & bonusType & bonusVal;
  1098. }
  1099. };
  1100. class DLL_LINKAGE CBank : public CArmedInstance
  1101. {
  1102. public:
  1103. int index; //banks have unusal numbering - see ZCRBANK.txt and initObj()
  1104. BankConfig *bc;
  1105. double multiplier; //for improved banks script
  1106. std::vector<ui32> artifacts; //fixed and deterministic
  1107. ui32 daycounter;
  1108. void initObj() override;
  1109. const std::string & getHoverText() const override;
  1110. void initialize() const;
  1111. void reset(ui16 var1);
  1112. void newTurn() const override;
  1113. bool wasVisited (PlayerColor player) const override;
  1114. void onHeroVisit(const CGHeroInstance * h) const override;
  1115. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  1116. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  1117. template <typename Handler> void serialize(Handler &h, const int version)
  1118. {
  1119. h & static_cast<CArmedInstance&>(*this);
  1120. h & index & multiplier & artifacts & daycounter & bc;
  1121. }
  1122. protected:
  1123. void setPropertyDer(ui8 what, ui32 val) override;
  1124. };
  1125. class DLL_LINKAGE CGPyramid : public CBank
  1126. {
  1127. public:
  1128. ui16 spell;
  1129. void initObj() override;
  1130. const std::string & getHoverText() const override;
  1131. void newTurn() const override {}; //empty, no reset
  1132. void onHeroVisit(const CGHeroInstance * h) const override;
  1133. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  1134. template <typename Handler> void serialize(Handler &h, const int version)
  1135. {
  1136. h & static_cast<CBank&>(*this);
  1137. h & spell;
  1138. }
  1139. };
  1140. class CGShipyard : public CGObjectInstance, public IShipyard
  1141. {
  1142. public:
  1143. void getOutOffsets(std::vector<int3> &offsets) const; //offsets to obj pos when we boat can be placed
  1144. CGShipyard();
  1145. void onHeroVisit(const CGHeroInstance * h) const override;
  1146. template <typename Handler> void serialize(Handler &h, const int version)
  1147. {
  1148. h & static_cast<CGObjectInstance&>(*this);
  1149. h & static_cast<IShipyard&>(*this);
  1150. }
  1151. };
  1152. class DLL_LINKAGE CGMagi : public CGObjectInstance
  1153. {
  1154. public:
  1155. static std::map <si32, std::vector<ObjectInstanceID> > eyelist; //[subID][id], supports multiple sets as in H5
  1156. void initObj() override;
  1157. void onHeroVisit(const CGHeroInstance * h) const override;
  1158. template <typename Handler> void serialize(Handler &h, const int version)
  1159. {
  1160. h & static_cast<CGObjectInstance&>(*this);
  1161. }
  1162. };
  1163. class DLL_LINKAGE CCartographer : public CPlayersVisited
  1164. {
  1165. ///behaviour varies depending on surface and floor
  1166. public:
  1167. void onHeroVisit(const CGHeroInstance * h) const override;
  1168. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  1169. template <typename Handler> void serialize(Handler &h, const int version)
  1170. {
  1171. h & static_cast<CPlayersVisited&>(*this);
  1172. }
  1173. };
  1174. class DLL_LINKAGE CGDenOfthieves : public CGObjectInstance
  1175. {
  1176. void onHeroVisit(const CGHeroInstance * h) const override;
  1177. };
  1178. class DLL_LINKAGE CGObelisk : public CPlayersVisited
  1179. {
  1180. public:
  1181. static ui8 obeliskCount; //how many obelisks are on map
  1182. static std::map<TeamID, ui8> visited; //map: team_id => how many obelisks has been visited
  1183. void onHeroVisit(const CGHeroInstance * h) const override;
  1184. void initObj() override;
  1185. const std::string & getHoverText() const override;
  1186. template <typename Handler> void serialize(Handler &h, const int version)
  1187. {
  1188. h & static_cast<CPlayersVisited&>(*this);
  1189. }
  1190. protected:
  1191. void setPropertyDer(ui8 what, ui32 val) override;
  1192. };
  1193. class DLL_LINKAGE CGLighthouse : public CGObjectInstance
  1194. {
  1195. public:
  1196. void onHeroVisit(const CGHeroInstance * h) const override;
  1197. void initObj() override;
  1198. const std::string & getHoverText() const override;
  1199. template <typename Handler> void serialize(Handler &h, const int version)
  1200. {
  1201. h & static_cast<CGObjectInstance&>(*this);
  1202. }
  1203. void giveBonusTo( PlayerColor player ) const;
  1204. };
  1205. class DLL_LINKAGE CGMarket : public CGObjectInstance, public IMarket
  1206. {
  1207. public:
  1208. CGMarket();
  1209. ///IObjectIntercae
  1210. void onHeroVisit(const CGHeroInstance * h) const override; //open trading window
  1211. ///IMarket
  1212. int getMarketEfficiency() const override;
  1213. bool allowsTrade(EMarketMode::EMarketMode mode) const override;
  1214. int availableUnits(EMarketMode::EMarketMode mode, int marketItemSerial) const override; //-1 if unlimited
  1215. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  1216. template <typename Handler> void serialize(Handler &h, const int version)
  1217. {
  1218. h & static_cast<CGObjectInstance&>(*this);
  1219. h & static_cast<IMarket&>(*this);
  1220. }
  1221. };
  1222. class DLL_LINKAGE CGBlackMarket : public CGMarket
  1223. {
  1224. public:
  1225. std::vector<const CArtifact *> artifacts; //available artifacts
  1226. void newTurn() const override; //reset artifacts for black market every month
  1227. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  1228. template <typename Handler> void serialize(Handler &h, const int version)
  1229. {
  1230. h & static_cast<CGMarket&>(*this);
  1231. h & artifacts;
  1232. }
  1233. };
  1234. class DLL_LINKAGE CGUniversity : public CGMarket
  1235. {
  1236. public:
  1237. std::vector<int> skills; //available skills
  1238. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const;
  1239. void initObj() override;//set skills for trade
  1240. void onHeroVisit(const CGHeroInstance * h) const override; //open window
  1241. template <typename Handler> void serialize(Handler &h, const int version)
  1242. {
  1243. h & static_cast<CGMarket&>(*this);
  1244. h & skills;
  1245. }
  1246. };
  1247. struct BankConfig
  1248. {
  1249. BankConfig() {level = chance = upgradeChance = combatValue = value = rewardDifficulty = easiest = 0; };
  1250. ui8 level; //1 - 4, how hard the battle will be
  1251. ui8 chance; //chance for this level being chosen
  1252. ui8 upgradeChance; //chance for creatures to be in upgraded versions
  1253. std::vector< std::pair <CreatureID, ui32> > guards; //creature ID, amount
  1254. ui32 combatValue; //how hard are guards of this level
  1255. Res::ResourceSet resources; //resources given in case of victory
  1256. std::vector< std::pair <CreatureID, ui32> > creatures; //creatures granted in case of victory (creature ID, amount)
  1257. std::vector<ui16> artifacts; //number of artifacts given in case of victory [0] -> treasure, [1] -> minor [2] -> major [3] -> relic
  1258. ui32 value; //overall value of given things
  1259. ui32 rewardDifficulty; //proportion of reward value to difficulty of guards; how profitable is this creature Bank config
  1260. ui16 easiest; //?!?
  1261. template <typename Handler> void serialize(Handler &h, const int version)
  1262. {
  1263. h & level & chance & upgradeChance & guards & combatValue & resources & creatures & artifacts & value & rewardDifficulty & easiest;
  1264. }
  1265. };
  1266. class DLL_LINKAGE CObjectHandler
  1267. {
  1268. public:
  1269. std::map<si32, CreatureID> cregens; //type 17. dwelling subid -> creature ID
  1270. std::map <ui32, std::vector < ConstTransitivePtr<BankConfig> > > banksInfo; //[index][preset]
  1271. std::map <ui32, std::string> creBanksNames; //[crebank index] -> name of this creature bank
  1272. std::vector<ui32> resVals; //default values of resources in gold
  1273. CObjectHandler();
  1274. ~CObjectHandler();
  1275. int bankObjToIndex (const CGObjectInstance * obj);
  1276. template <typename Handler> void serialize(Handler &h, const int version)
  1277. {
  1278. h & cregens & banksInfo & creBanksNames & resVals;
  1279. }
  1280. };