CObjectHandler.h 53 KB

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