CObjectHandler.h 54 KB

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