CObjectHandler.h 53 KB

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