CObjectHandler.h 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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 recreateSecondarySkillsBonuses();
  389. void updateSkill(SecondarySkill which, int val);
  390. CGHeroInstance();
  391. virtual ~CGHeroInstance();
  392. //////////////////////////////////////////////////////////////////////////
  393. //
  394. ArtBearer::ArtBearer bearerType() const override;
  395. //////////////////////////////////////////////////////////////////////////
  396. CBonusSystemNode *whereShouldBeAttached(CGameState *gs) override;
  397. std::string nodeName() const override;
  398. void deserializationFix();
  399. void initObj() override;
  400. void onHeroVisit(const CGHeroInstance * h) const override;
  401. const std::string & getHoverText() const override;
  402. protected:
  403. void setPropertyDer(ui8 what, ui32 val) override;//synchr
  404. };
  405. class DLL_LINKAGE CSpecObjInfo
  406. {
  407. public:
  408. virtual ~CSpecObjInfo(){};
  409. PlayerColor player; //owner
  410. };
  411. class DLL_LINKAGE CCreGenAsCastleInfo : public virtual CSpecObjInfo
  412. {
  413. public:
  414. bool asCastle;
  415. ui32 identifier;
  416. ui8 castles[2]; //allowed castles
  417. };
  418. class DLL_LINKAGE CCreGenLeveledInfo : public virtual CSpecObjInfo
  419. {
  420. public:
  421. ui8 minLevel, maxLevel; //minimal and maximal level of creature in dwelling: <0, 6>
  422. };
  423. class DLL_LINKAGE CCreGenLeveledCastleInfo : public CCreGenAsCastleInfo, public CCreGenLeveledInfo
  424. {
  425. };
  426. class DLL_LINKAGE CGDwelling : public CArmedInstance
  427. {
  428. public:
  429. typedef std::vector<std::pair<ui32, std::vector<CreatureID> > > TCreaturesSet;
  430. CSpecObjInfo * info; //h3m info about dewlling
  431. TCreaturesSet creatures; //creatures[level] -> <vector of alternative ids (base creature and upgrades, creatures amount>
  432. template <typename Handler> void serialize(Handler &h, const int version)
  433. {
  434. h & static_cast<CArmedInstance&>(*this) & creatures;
  435. }
  436. void initObj() override;
  437. void onHeroVisit(const CGHeroInstance * h) const override;
  438. void newTurn() const override;
  439. void setProperty(ui8 what, ui32 val) override;
  440. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  441. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  442. private:
  443. void heroAcceptsCreatures(const CGHeroInstance *h) const;
  444. };
  445. class DLL_LINKAGE CGVisitableOPH : public CGObjectInstance //objects visitable only once per hero
  446. {
  447. public:
  448. std::set<ObjectInstanceID> visitors; //ids of heroes who have visited this obj
  449. TResources treePrice; //used only by trees of knowledge: empty, 2000 gold, 10 gems
  450. const std::string & getHoverText() const override;
  451. void onHeroVisit(const CGHeroInstance * h) const override;
  452. void initObj() override;
  453. bool wasVisited (const CGHeroInstance * h) const override;
  454. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  455. template <typename Handler> void serialize(Handler &h, const int version)
  456. {
  457. h & static_cast<CGObjectInstance&>(*this);
  458. h & visitors & treePrice;
  459. }
  460. protected:
  461. void setPropertyDer(ui8 what, ui32 val) override;//synchr
  462. private:
  463. void onNAHeroVisit(const CGHeroInstance * h, bool alreadyVisited) const;
  464. ///dialog callbacks
  465. void treeSelected(const CGHeroInstance * h, ui32 result) const;
  466. void schoolSelected(const CGHeroInstance * h, ui32 which) const;
  467. void arenaSelected(const CGHeroInstance * h, int primSkill) const;
  468. };
  469. class DLL_LINKAGE CGTownBuilding : public IObjectInterface
  470. {
  471. ///basic class for town structures handled as map objects
  472. public:
  473. BuildingID ID; //from buildig list
  474. si32 id; //identifies its index on towns vector
  475. CGTownInstance *town;
  476. template <typename Handler> void serialize(Handler &h, const int version)
  477. {
  478. h & ID & id;
  479. }
  480. };
  481. class DLL_LINKAGE COPWBonus : public CGTownBuilding
  482. {///used for OPW bonusing structures
  483. public:
  484. std::set<si32> visitors;
  485. void setProperty(ui8 what, ui32 val) override;
  486. void onHeroVisit (const CGHeroInstance * h) const override;
  487. COPWBonus (BuildingID index, CGTownInstance *TOWN);
  488. COPWBonus (){ID = BuildingID::NONE; town = nullptr;};
  489. template <typename Handler> void serialize(Handler &h, const int version)
  490. {
  491. h & static_cast<CGTownBuilding&>(*this);
  492. h & visitors;
  493. }
  494. };
  495. class DLL_LINKAGE CTownBonus : public CGTownBuilding
  496. {
  497. ///used for one-time bonusing structures
  498. ///feel free to merge inheritance tree
  499. public:
  500. std::set<ObjectInstanceID> visitors;
  501. void setProperty(ui8 what, ui32 val) override;
  502. void onHeroVisit (const CGHeroInstance * h) const override;
  503. CTownBonus (BuildingID index, CGTownInstance *TOWN);
  504. CTownBonus (){ID = BuildingID::NONE; town = nullptr;};
  505. template <typename Handler> void serialize(Handler &h, const int version)
  506. {
  507. h & static_cast<CGTownBuilding&>(*this);
  508. h & visitors;
  509. }
  510. };
  511. class DLL_LINKAGE CTownAndVisitingHero : public CBonusSystemNode
  512. {
  513. public:
  514. CTownAndVisitingHero();
  515. };
  516. struct DLL_LINKAGE GrowthInfo
  517. {
  518. struct Entry
  519. {
  520. int count;
  521. std::string description;
  522. Entry(const std::string &format, int _count);
  523. Entry(int subID, BuildingID building, int _count);
  524. };
  525. std::vector<Entry> entries;
  526. int totalGrowth() const;
  527. };
  528. class DLL_LINKAGE CGTownInstance : public CGDwelling, public IShipyard, public IMarket
  529. {
  530. public:
  531. enum EFortLevel {NONE = 0, FORT = 1, CITADEL = 2, CASTLE = 3};
  532. CTownAndVisitingHero townAndVis;
  533. const CTown * town;
  534. std::string name; // name of town
  535. si32 builded; //how many buildings has been built this turn
  536. si32 destroyed; //how many buildings has been destroyed this turn
  537. ConstTransitivePtr<CGHeroInstance> garrisonHero, visitingHero;
  538. ui32 identifier; //special identifier from h3m (only > RoE maps)
  539. si32 alignment;
  540. std::set<BuildingID> forbiddenBuildings, builtBuildings;
  541. std::vector<CGTownBuilding*> bonusingBuildings;
  542. std::vector<SpellID> possibleSpells, obligatorySpells;
  543. std::vector<std::vector<SpellID> > spells; //spells[level] -> vector of spells, first will be available in guild
  544. std::list<CCastleEvent> events;
  545. std::pair<si32, si32> bonusValue;//var to store town bonuses (rampart = resources from mystic pond);
  546. //////////////////////////////////////////////////////////////////////////
  547. static std::vector<const CArtifact *> merchantArtifacts; //vector of artifacts available at Artifact merchant, NULLs possible (for making empty space when artifact is bought)
  548. static std::vector<int> universitySkills;//skills for university of magic
  549. template <typename Handler> void serialize(Handler &h, const int version)
  550. {
  551. h & static_cast<CGDwelling&>(*this);
  552. h & name & builded & destroyed & identifier;
  553. h & garrisonHero & visitingHero;
  554. h & alignment & forbiddenBuildings & builtBuildings & bonusValue
  555. & possibleSpells & obligatorySpells & spells & /*strInfo & */events & bonusingBuildings;
  556. for (std::vector<CGTownBuilding*>::iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  557. (*i)->town = this;
  558. h & town & townAndVis;
  559. BONUS_TREE_DESERIALIZATION_FIX
  560. vstd::erase_if(builtBuildings, [this](BuildingID building) -> bool
  561. {
  562. if(!town->buildings.count(building) || !town->buildings.at(building))
  563. {
  564. logGlobal->errorStream() << boost::format("#1444-like issue in CGTownInstance::serialize. From town %s at %s removing the bogus builtBuildings item %s")
  565. % name % pos % building;
  566. return true;
  567. }
  568. return false;
  569. });
  570. }
  571. //////////////////////////////////////////////////////////////////////////
  572. CBonusSystemNode *whatShouldBeAttached() override;
  573. std::string nodeName() const override;
  574. void updateMoraleBonusFromArmy() override;
  575. void deserializationFix();
  576. void recreateBuildingsBonuses();
  577. 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
  578. bool addBonusIfBuilt(BuildingID building, Bonus::BonusType type, int val, int subtype = -1); //convienence version of above
  579. void setVisitingHero(CGHeroInstance *h);
  580. void setGarrisonedHero(CGHeroInstance *h);
  581. const CArmedInstance *getUpperArmy() const; //garrisoned hero if present or the town itself
  582. //////////////////////////////////////////////////////////////////////////
  583. 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
  584. int3 getSightCenter() const override; //"center" tile from which the sight distance is calculated
  585. int getSightRadious() const override; //returns sight distance
  586. int getBoatType() const; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  587. void getOutOffsets(std::vector<int3> &offsets) const; //offsets to obj pos when we boat can be placed
  588. int getMarketEfficiency() const override; //=market count
  589. bool allowsTrade(EMarketMode::EMarketMode mode) const;
  590. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const;
  591. void updateAppearance();
  592. //////////////////////////////////////////////////////////////////////////
  593. bool needsLastStack() const;
  594. CGTownInstance::EFortLevel fortLevel() const;
  595. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  596. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  597. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  598. int creatureGrowth(const int & level) const;
  599. GrowthInfo getGrowthInfo(int level) const;
  600. bool hasFort() const;
  601. bool hasCapitol() const;
  602. //checks if building is constructed and town has same subID
  603. bool hasBuilt(BuildingID buildingID) const;
  604. bool hasBuilt(BuildingID buildingID, int townID) const;
  605. int dailyIncome() const; //calculates daily income of this town
  606. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  607. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  608. int getTownLevel() const;
  609. void removeCapitols (PlayerColor owner) const;
  610. void addHeroToStructureVisitors(const CGHeroInstance *h, si32 structureInstanceID) const; //hero must be visiting or garrisoned in town
  611. CGTownInstance();
  612. virtual ~CGTownInstance();
  613. ///IObjectInterface overrides
  614. void newTurn() const override;
  615. void onHeroVisit(const CGHeroInstance * h) const override;
  616. void onHeroLeave(const CGHeroInstance * h) const override;
  617. void initObj() override;
  618. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  619. protected:
  620. void setPropertyDer(ui8 what, ui32 val) override;
  621. };
  622. class DLL_LINKAGE CGPandoraBox : public CArmedInstance
  623. {
  624. public:
  625. std::string message;
  626. bool hasGuardians; //helper - after battle even though we have no stacks, allows us to know that there was battle
  627. //gained things:
  628. ui32 gainedExp;
  629. si32 manaDiff; //amount of gained / lost mana
  630. si32 moraleDiff; //morale modifier
  631. si32 luckDiff; //luck modifier
  632. TResources resources;//gained / lost resources
  633. std::vector<si32> primskills;//gained / lost prim skills
  634. std::vector<SecondarySkill> abilities; //gained abilities
  635. std::vector<si32> abilityLevels; //levels of gained abilities
  636. std::vector<ArtifactID> artifacts; //gained artifacts
  637. std::vector<SpellID> spells; //gained spells
  638. CCreatureSet creatures; //gained creatures
  639. void initObj() override;
  640. void onHeroVisit(const CGHeroInstance * h) const override;
  641. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  642. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  643. void heroLevelUpDone(const CGHeroInstance *hero) const override;
  644. template <typename Handler> void serialize(Handler &h, const int version)
  645. {
  646. h & static_cast<CArmedInstance&>(*this);
  647. h & message & hasGuardians & gainedExp & manaDiff & moraleDiff & luckDiff & resources & primskills
  648. & abilities & abilityLevels & artifacts & spells & creatures;
  649. }
  650. protected:
  651. void giveContentsUpToExp(const CGHeroInstance *h) const;
  652. void giveContentsAfterExp(const CGHeroInstance *h) const;
  653. private:
  654. void getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const;
  655. void getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const;
  656. };
  657. class DLL_LINKAGE CGEvent : public CGPandoraBox //event objects
  658. {
  659. public:
  660. bool removeAfterVisit; //true if event is removed after occurring
  661. ui8 availableFor; //players whom this event is available for
  662. bool computerActivate; //true if computer player can activate this event
  663. bool humanActivate; //true if human player can activate this event
  664. template <typename Handler> void serialize(Handler &h, const int version)
  665. {
  666. h & static_cast<CGPandoraBox &>(*this);
  667. h & removeAfterVisit & availableFor & computerActivate & humanActivate;
  668. }
  669. void onHeroVisit(const CGHeroInstance * h) const override;
  670. private:
  671. void activated(const CGHeroInstance * h) const;
  672. };
  673. class DLL_LINKAGE CGCreature : public CArmedInstance //creatures on map
  674. {
  675. enum Action {
  676. FIGHT = -2, FLEE = -1, JOIN_FOR_FREE = 0 //values > 0 mean gold price
  677. };
  678. public:
  679. ui32 identifier; //unique code for this monster (used in missions)
  680. 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)
  681. std::string message; //message printed for attacking hero
  682. TResources resources; // resources given to hero that has won with monsters
  683. ArtifactID gainedArtifact; //ID of artifact gained to hero, -1 if none
  684. bool neverFlees; //if true, the troops will never flee
  685. bool notGrowingTeam; //if true, number of units won't grow
  686. ui64 temppower; //used to handle fractional stack growth for tiny stacks
  687. bool refusedJoining;
  688. void onHeroVisit(const CGHeroInstance * h) const override;
  689. const std::string & getHoverText() const override;
  690. void initObj() override;
  691. void newTurn() const override;
  692. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  693. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  694. struct DLL_LINKAGE formationInfo // info about merging stacks after battle back into one
  695. {
  696. si32 basicType;
  697. ui32 randomFormation; //random seed used to determine number of stacks and is there's upgraded stack
  698. template <typename Handler> void serialize(Handler &h, const int version)
  699. {
  700. h & basicType & randomFormation;
  701. }
  702. } formation;
  703. template <typename Handler> void serialize(Handler &h, const int version)
  704. {
  705. h & static_cast<CArmedInstance&>(*this);
  706. h & identifier & character & message & resources & gainedArtifact & neverFlees & notGrowingTeam & temppower;
  707. h & refusedJoining & formation;
  708. }
  709. protected:
  710. void setPropertyDer(ui8 what, ui32 val) override;
  711. private:
  712. void fight(const CGHeroInstance *h) const;
  713. void flee( const CGHeroInstance * h ) const;
  714. void fleeDecision(const CGHeroInstance *h, ui32 pursue) const;
  715. void joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const;
  716. 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)
  717. };
  718. class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  719. {
  720. public:
  721. std::string message;
  722. void onHeroVisit(const CGHeroInstance * h) const override;
  723. void initObj() override;
  724. template <typename Handler> void serialize(Handler &h, const int version)
  725. {
  726. h & static_cast<CGObjectInstance&>(*this);
  727. h & message;
  728. }
  729. };
  730. class DLL_LINKAGE IQuestObject
  731. {
  732. public:
  733. CQuest * quest;
  734. IQuestObject(): quest(new CQuest()){};
  735. virtual ~IQuestObject() {};
  736. virtual void getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  737. virtual bool checkQuest (const CGHeroInstance * h) const;
  738. template <typename Handler> void serialize(Handler &h, const int version)
  739. {
  740. h & quest;
  741. }
  742. };
  743. class DLL_LINKAGE CGSeerHut : public CArmedInstance, public IQuestObject //army is used when giving reward
  744. {
  745. public:
  746. enum ERewardType {NOTHING, EXPERIENCE, MANA_POINTS, MORALE_BONUS, LUCK_BONUS, RESOURCES, PRIMARY_SKILL, SECONDARY_SKILL, ARTIFACT, SPELL, CREATURE};
  747. ERewardType rewardType;
  748. si32 rID; //reward ID
  749. si32 rVal; //reward value
  750. std::string seerName;
  751. CGSeerHut() : IQuestObject(){};
  752. void initObj() override;
  753. const std::string & getHoverText() const override;
  754. void newTurn() const override;
  755. void onHeroVisit(const CGHeroInstance * h) const override;
  756. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  757. virtual void init();
  758. int checkDirection() const; //calculates the region of map where monster is placed
  759. void setObjToKill(); //remember creatures / heroes to kill after they are initialized
  760. const CGHeroInstance *getHeroToKill(bool allowNull = false) const;
  761. const CGCreature *getCreatureToKill(bool allowNull = false) const;
  762. void getRolloverText (MetaString &text, bool onHover) const;
  763. void getCompletionText(MetaString &text, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h = nullptr) const;
  764. void finishQuest (const CGHeroInstance * h, ui32 accept) const; //common for both objects
  765. virtual void completeQuest (const CGHeroInstance * h) const;
  766. template <typename Handler> void serialize(Handler &h, const int version)
  767. {
  768. h & static_cast<CArmedInstance&>(*this) & static_cast<IQuestObject&>(*this);
  769. h & rewardType & rID & rVal & seerName;
  770. }
  771. protected:
  772. void setPropertyDer(ui8 what, ui32 val) override;
  773. };
  774. class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  775. {
  776. public:
  777. CGQuestGuard() : CGSeerHut(){};
  778. void init() override;
  779. void completeQuest (const CGHeroInstance * h) const override;
  780. template <typename Handler> void serialize(Handler &h, const int version)
  781. {
  782. h & static_cast<CGSeerHut&>(*this);
  783. }
  784. };
  785. class DLL_LINKAGE CGWitchHut : public CPlayersVisited
  786. {
  787. public:
  788. std::vector<si32> allowedAbilities;
  789. ui32 ability;
  790. const std::string & getHoverText() const override;
  791. void onHeroVisit(const CGHeroInstance * h) const override;
  792. void initObj() override;
  793. template <typename Handler> void serialize(Handler &h, const int version)
  794. {
  795. h & static_cast<CPlayersVisited&>(*this);
  796. h & allowedAbilities & ability;
  797. }
  798. };
  799. class DLL_LINKAGE CGScholar : public CGObjectInstance
  800. {
  801. public:
  802. enum EBonusType {PRIM_SKILL, SECONDARY_SKILL, SPELL, RANDOM = 255};
  803. EBonusType bonusType;
  804. ui16 bonusID; //ID of skill/spell
  805. // void giveAnyBonus(const CGHeroInstance * h) const; //TODO: remove
  806. void onHeroVisit(const CGHeroInstance * h) const override;
  807. void initObj() override;
  808. template <typename Handler> void serialize(Handler &h, const int version)
  809. {
  810. h & static_cast<CGObjectInstance&>(*this);
  811. h & bonusType & bonusID;
  812. }
  813. };
  814. class DLL_LINKAGE CGGarrison : public CArmedInstance
  815. {
  816. public:
  817. bool removableUnits;
  818. ui8 getPassableness() const;
  819. void onHeroVisit(const CGHeroInstance * h) const override;
  820. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  821. template <typename Handler> void serialize(Handler &h, const int version)
  822. {
  823. h & static_cast<CArmedInstance&>(*this);
  824. h & removableUnits;
  825. }
  826. };
  827. class DLL_LINKAGE CGArtifact : public CArmedInstance
  828. {
  829. public:
  830. CArtifactInstance *storedArtifact;
  831. std::string message;
  832. void onHeroVisit(const CGHeroInstance * h) const override;
  833. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  834. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  835. void pick( const CGHeroInstance * h ) const;
  836. void initObj() override;
  837. template <typename Handler> void serialize(Handler &h, const int version)
  838. {
  839. h & static_cast<CArmedInstance&>(*this);
  840. h & message & storedArtifact;
  841. }
  842. };
  843. class DLL_LINKAGE CGResource : public CArmedInstance
  844. {
  845. public:
  846. ui32 amount; //0 if random
  847. std::string message;
  848. void onHeroVisit(const CGHeroInstance * h) const override;
  849. void initObj() override;
  850. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  851. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  852. void collectRes(PlayerColor player) const;
  853. template <typename Handler> void serialize(Handler &h, const int version)
  854. {
  855. h & static_cast<CArmedInstance&>(*this);
  856. h & amount & message;
  857. }
  858. };
  859. class DLL_LINKAGE CGPickable : public CGObjectInstance //campfire, treasure chest, Flotsam, Shipwreck Survivor, Sea Chest
  860. {
  861. public:
  862. ui32 type, val1, val2;
  863. void onHeroVisit(const CGHeroInstance * h) const override;
  864. void initObj() override;
  865. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  866. template <typename Handler> void serialize(Handler &h, const int version)
  867. {
  868. h & static_cast<CGObjectInstance&>(*this);
  869. h & type & val1 & val2;
  870. }
  871. };
  872. class DLL_LINKAGE CGShrine : public CPlayersVisited
  873. {
  874. public:
  875. SpellID spell; //id of spell or NONE if random
  876. void onHeroVisit(const CGHeroInstance * h) const override;
  877. void initObj() override;
  878. const std::string & getHoverText() const override;
  879. template <typename Handler> void serialize(Handler &h, const int version)
  880. {
  881. h & static_cast<CPlayersVisited&>(*this);;
  882. h & spell;
  883. }
  884. };
  885. class DLL_LINKAGE CGMine : public CArmedInstance
  886. {
  887. public:
  888. Res::ERes producedResource;
  889. ui32 producedQuantity;
  890. void onHeroVisit(const CGHeroInstance * h) const override;
  891. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  892. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  893. void flagMine(PlayerColor player) const;
  894. void newTurn() const override;
  895. void initObj() override;
  896. template <typename Handler> void serialize(Handler &h, const int version)
  897. {
  898. h & static_cast<CArmedInstance&>(*this);
  899. h & producedResource & producedQuantity;
  900. }
  901. ui32 defaultResProduction();
  902. };
  903. class DLL_LINKAGE CGVisitableOPW : public CGObjectInstance //objects visitable OPW
  904. {
  905. public:
  906. ui8 visited; //true if object has been visited this week
  907. bool wasVisited(PlayerColor player) const;
  908. void onHeroVisit(const CGHeroInstance * h) const override;
  909. virtual void newTurn() const override;
  910. template <typename Handler> void serialize(Handler &h, const int version)
  911. {
  912. h & static_cast<CGObjectInstance&>(*this);
  913. h & visited;
  914. }
  915. protected:
  916. void setPropertyDer(ui8 what, ui32 val) override;
  917. };
  918. class DLL_LINKAGE CGTeleport : public CGObjectInstance //teleports and subterranean gates
  919. {
  920. public:
  921. static std::map<Obj, std::map<int, std::vector<ObjectInstanceID> > > objs; //teleports: map[ID][subID] => vector of ids
  922. static std::vector<std::pair<ObjectInstanceID, ObjectInstanceID> > gates; //subterranean gates: pairs of ids
  923. void onHeroVisit(const CGHeroInstance * h) const override;
  924. void initObj() override;
  925. static void postInit();
  926. static ObjectInstanceID getMatchingGate(ObjectInstanceID id); //receives id of one subterranean gate and returns id of the paired one, -1 if none
  927. template <typename Handler> void serialize(Handler &h, const int version)
  928. {
  929. h & static_cast<CGObjectInstance&>(*this);
  930. }
  931. };
  932. class DLL_LINKAGE CGBonusingObject : public CGObjectInstance //objects giving bonuses to luck/morale/movement
  933. {
  934. public:
  935. bool wasVisited (const CGHeroInstance * h) const;
  936. void onHeroVisit(const CGHeroInstance * h) const override;
  937. const std::string & getHoverText() const override;
  938. void initObj() override;
  939. template <typename Handler> void serialize(Handler &h, const int version)
  940. {
  941. h & static_cast<CGObjectInstance&>(*this);
  942. }
  943. };
  944. class DLL_LINKAGE CGMagicSpring : public CGVisitableOPW
  945. {///unfortunately, this one is quite different than others
  946. enum EVisitedEntrance
  947. {
  948. CLEAR = 0, LEFT = 1, RIGHT
  949. };
  950. public:
  951. EVisitedEntrance visitedTile; //only one entrance was visited - there are two
  952. std::vector<int3> getVisitableOffsets() const;
  953. int3 getVisitableOffset() const override;
  954. void setPropertyDer(ui8 what, ui32 val) override;
  955. void newTurn() const override;
  956. void onHeroVisit(const CGHeroInstance * h) const override;
  957. const std::string & getHoverText() const override;
  958. template <typename Handler> void serialize(Handler &h, const int version)
  959. {
  960. h & static_cast<CGObjectInstance&>(*this);
  961. h & visitedTile & visited;
  962. }
  963. };
  964. class DLL_LINKAGE CGMagicWell : public CGObjectInstance //objects giving bonuses to luck/morale/movement
  965. {
  966. public:
  967. void onHeroVisit(const CGHeroInstance * h) const override;
  968. const std::string & getHoverText() const override;
  969. template <typename Handler> void serialize(Handler &h, const int version)
  970. {
  971. h & static_cast<CGObjectInstance&>(*this);
  972. }
  973. };
  974. class DLL_LINKAGE CGSirens : public CGObjectInstance
  975. {
  976. public:
  977. void onHeroVisit(const CGHeroInstance * h) const override;
  978. const std::string & getHoverText() const override;
  979. void initObj() override;
  980. template <typename Handler> void serialize(Handler &h, const int version)
  981. {
  982. h & static_cast<CGObjectInstance&>(*this);
  983. }
  984. };
  985. class DLL_LINKAGE CGObservatory : public CGObjectInstance //Redwood observatory
  986. {
  987. public:
  988. void onHeroVisit(const CGHeroInstance * h) const override;
  989. template <typename Handler> void serialize(Handler &h, const int version)
  990. {
  991. h & static_cast<CGObjectInstance&>(*this);
  992. }
  993. };
  994. class DLL_LINKAGE CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  995. {
  996. public:
  997. static std::map <PlayerColor, std::set <ui8> > playerKeyMap; //[players][keysowned]
  998. //SubID 0 - lightblue, 1 - green, 2 - red, 3 - darkblue, 4 - brown, 5 - purple, 6 - white, 7 - black
  999. const std::string getName() const; //depending on color
  1000. bool wasMyColorVisited (PlayerColor player) const;
  1001. const std::string & getHoverText() const override;
  1002. template <typename Handler> void serialize(Handler &h, const int version)
  1003. {
  1004. h & static_cast<CGObjectInstance&>(*this);
  1005. }
  1006. protected:
  1007. void setPropertyDer(ui8 what, ui32 val) override;
  1008. };
  1009. class DLL_LINKAGE CGKeymasterTent : public CGKeys
  1010. {
  1011. public:
  1012. bool wasVisited (PlayerColor player) const;
  1013. void onHeroVisit(const CGHeroInstance * h) const override;
  1014. template <typename Handler> void serialize(Handler &h, const int version)
  1015. {
  1016. h & static_cast<CGObjectInstance&>(*this);
  1017. }
  1018. };
  1019. class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  1020. {
  1021. public:
  1022. CGBorderGuard() : IQuestObject(){};
  1023. void initObj() override;
  1024. void onHeroVisit(const CGHeroInstance * h) const override;
  1025. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  1026. void getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  1027. void getRolloverText (MetaString &text, bool onHover) const;
  1028. bool checkQuest (const CGHeroInstance * h) const;
  1029. template <typename Handler> void serialize(Handler &h, const int version)
  1030. {
  1031. h & static_cast<IQuestObject&>(*this);
  1032. h & static_cast<CGObjectInstance&>(*this);
  1033. h & blockVisit;
  1034. }
  1035. };
  1036. class DLL_LINKAGE CGBorderGate : public CGBorderGuard
  1037. {
  1038. public:
  1039. CGBorderGate() : CGBorderGuard(){};
  1040. void onHeroVisit(const CGHeroInstance * h) const override;
  1041. ui8 getPassableness() const override;
  1042. template <typename Handler> void serialize(Handler &h, const int version)
  1043. {
  1044. h & static_cast<CGBorderGuard&>(*this); //need to serialize or object will be empty
  1045. }
  1046. };
  1047. class DLL_LINKAGE CGBoat : public CGObjectInstance
  1048. {
  1049. public:
  1050. ui8 direction;
  1051. const CGHeroInstance *hero; //hero on board
  1052. void initObj() override;
  1053. CGBoat()
  1054. {
  1055. hero = nullptr;
  1056. direction = 4;
  1057. }
  1058. template <typename Handler> void serialize(Handler &h, const int version)
  1059. {
  1060. h & static_cast<CGObjectInstance&>(*this) & direction & hero;
  1061. }
  1062. };
  1063. class DLL_LINKAGE CGOnceVisitable : public CPlayersVisited
  1064. ///wagon, corpse, lean to, warriors tomb
  1065. {
  1066. public:
  1067. ui8 artOrRes; //0 - nothing; 1 - artifact; 2 - resource
  1068. ui32 bonusType, //id of res or artifact
  1069. bonusVal; //resource amount (or not used)
  1070. void onHeroVisit(const CGHeroInstance * h) const override;
  1071. const std::string & getHoverText() const override;
  1072. void initObj() override;
  1073. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  1074. template <typename Handler> void serialize(Handler &h, const int version)
  1075. {
  1076. h & static_cast<CPlayersVisited&>(*this);;
  1077. h & artOrRes & bonusType & bonusVal;
  1078. }
  1079. };
  1080. class DLL_LINKAGE CBank : public CArmedInstance
  1081. {
  1082. public:
  1083. int index; //banks have unusal numbering - see ZCRBANK.txt and initObj()
  1084. BankConfig *bc;
  1085. double multiplier; //for improved banks script
  1086. std::vector<ui32> artifacts; //fixed and deterministic
  1087. ui32 daycounter;
  1088. void initObj() override;
  1089. const std::string & getHoverText() const override;
  1090. void initialize() const;
  1091. void reset(ui16 var1);
  1092. void newTurn() const override;
  1093. bool wasVisited (PlayerColor player) const override;
  1094. void onHeroVisit(const CGHeroInstance * h) const override;
  1095. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  1096. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  1097. template <typename Handler> void serialize(Handler &h, const int version)
  1098. {
  1099. h & static_cast<CArmedInstance&>(*this);
  1100. h & index & multiplier & artifacts & daycounter & bc;
  1101. }
  1102. protected:
  1103. void setPropertyDer(ui8 what, ui32 val) override;
  1104. };
  1105. class DLL_LINKAGE CGPyramid : public CBank
  1106. {
  1107. public:
  1108. ui16 spell;
  1109. void initObj() override;
  1110. const std::string & getHoverText() const override;
  1111. void newTurn() const override {}; //empty, no reset
  1112. void onHeroVisit(const CGHeroInstance * h) const override;
  1113. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  1114. template <typename Handler> void serialize(Handler &h, const int version)
  1115. {
  1116. h & static_cast<CBank&>(*this);
  1117. h & spell;
  1118. }
  1119. };
  1120. class CGShipyard : public CGObjectInstance, public IShipyard
  1121. {
  1122. public:
  1123. void getOutOffsets(std::vector<int3> &offsets) const; //offsets to obj pos when we boat can be placed
  1124. CGShipyard();
  1125. void onHeroVisit(const CGHeroInstance * h) const override;
  1126. };
  1127. class DLL_LINKAGE CGMagi : public CGObjectInstance
  1128. {
  1129. public:
  1130. static std::map <si32, std::vector<ObjectInstanceID> > eyelist; //[subID][id], supports multiple sets as in H5
  1131. void initObj() override;
  1132. void onHeroVisit(const CGHeroInstance * h) const override;
  1133. template <typename Handler> void serialize(Handler &h, const int version)
  1134. {
  1135. h & static_cast<CGObjectInstance&>(*this);
  1136. }
  1137. };
  1138. class DLL_LINKAGE CCartographer : public CPlayersVisited
  1139. {
  1140. ///behaviour varies depending on surface and floor
  1141. public:
  1142. void onHeroVisit(const CGHeroInstance * h) const override;
  1143. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  1144. template <typename Handler> void serialize(Handler &h, const int version)
  1145. {
  1146. h & static_cast<CPlayersVisited&>(*this);
  1147. }
  1148. };
  1149. class DLL_LINKAGE CGDenOfthieves : public CGObjectInstance
  1150. {
  1151. void onHeroVisit(const CGHeroInstance * h) const override;
  1152. };
  1153. class DLL_LINKAGE CGObelisk : public CPlayersVisited
  1154. {
  1155. public:
  1156. static ui8 obeliskCount; //how many obelisks are on map
  1157. static std::map<TeamID, ui8> visited; //map: team_id => how many obelisks has been visited
  1158. void onHeroVisit(const CGHeroInstance * h) const override;
  1159. void initObj() override;
  1160. const std::string & getHoverText() const override;
  1161. template <typename Handler> void serialize(Handler &h, const int version)
  1162. {
  1163. h & static_cast<CPlayersVisited&>(*this);
  1164. }
  1165. protected:
  1166. void setPropertyDer(ui8 what, ui32 val) override;
  1167. };
  1168. class DLL_LINKAGE CGLighthouse : public CGObjectInstance
  1169. {
  1170. public:
  1171. void onHeroVisit(const CGHeroInstance * h) const override;
  1172. void initObj() override;
  1173. const std::string & getHoverText() const override;
  1174. template <typename Handler> void serialize(Handler &h, const int version)
  1175. {
  1176. h & static_cast<CGObjectInstance&>(*this);
  1177. }
  1178. void giveBonusTo( PlayerColor player ) const;
  1179. };
  1180. class DLL_LINKAGE CGMarket : public CGObjectInstance, public IMarket
  1181. {
  1182. public:
  1183. CGMarket();
  1184. ///IObjectIntercae
  1185. void onHeroVisit(const CGHeroInstance * h) const override; //open trading window
  1186. ///IMarket
  1187. int getMarketEfficiency() const override;
  1188. bool allowsTrade(EMarketMode::EMarketMode mode) const override;
  1189. int availableUnits(EMarketMode::EMarketMode mode, int marketItemSerial) const override; //-1 if unlimited
  1190. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  1191. template <typename Handler> void serialize(Handler &h, const int version)
  1192. {
  1193. h & static_cast<CGObjectInstance&>(*this);
  1194. }
  1195. };
  1196. class DLL_LINKAGE CGBlackMarket : public CGMarket
  1197. {
  1198. public:
  1199. std::vector<const CArtifact *> artifacts; //available artifacts
  1200. void newTurn() const override; //reset artifacts for black market every month
  1201. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  1202. template <typename Handler> void serialize(Handler &h, const int version)
  1203. {
  1204. h & static_cast<CGMarket&>(*this);
  1205. h & artifacts;
  1206. }
  1207. };
  1208. class DLL_LINKAGE CGUniversity : public CGMarket
  1209. {
  1210. public:
  1211. std::vector<int> skills; //available skills
  1212. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const;
  1213. void initObj() override;//set skills for trade
  1214. void onHeroVisit(const CGHeroInstance * h) const override; //open window
  1215. template <typename Handler> void serialize(Handler &h, const int version)
  1216. {
  1217. h & static_cast<CGMarket&>(*this);
  1218. h & skills;
  1219. }
  1220. };
  1221. struct BankConfig
  1222. {
  1223. BankConfig() {level = chance = upgradeChance = combatValue = value = rewardDifficulty = easiest = 0; };
  1224. ui8 level; //1 - 4, how hard the battle will be
  1225. ui8 chance; //chance for this level being chosen
  1226. ui8 upgradeChance; //chance for creatures to be in upgraded versions
  1227. std::vector< std::pair <CreatureID, ui32> > guards; //creature ID, amount
  1228. ui32 combatValue; //how hard are guards of this level
  1229. Res::ResourceSet resources; //resources given in case of victory
  1230. std::vector< std::pair <CreatureID, ui32> > creatures; //creatures granted in case of victory (creature ID, amount)
  1231. std::vector<ui16> artifacts; //number of artifacts given in case of victory [0] -> treasure, [1] -> minor [2] -> major [3] -> relic
  1232. ui32 value; //overall value of given things
  1233. ui32 rewardDifficulty; //proportion of reward value to difficulty of guards; how profitable is this creature Bank config
  1234. ui16 easiest; //?!?
  1235. template <typename Handler> void serialize(Handler &h, const int version)
  1236. {
  1237. h & level & chance & upgradeChance & guards & combatValue & resources & creatures & artifacts & value & rewardDifficulty & easiest;
  1238. }
  1239. };
  1240. class DLL_LINKAGE CObjectHandler
  1241. {
  1242. public:
  1243. std::map<si32, CreatureID> cregens; //type 17. dwelling subid -> creature ID
  1244. std::map <ui32, std::vector < ConstTransitivePtr<BankConfig> > > banksInfo; //[index][preset]
  1245. std::map <ui32, std::string> creBanksNames; //[crebank index] -> name of this creature bank
  1246. std::vector<ui32> resVals; //default values of resources in gold
  1247. CObjectHandler();
  1248. ~CObjectHandler();
  1249. int bankObjToIndex (const CGObjectInstance * obj);
  1250. template <typename Handler> void serialize(Handler &h, const int version)
  1251. {
  1252. h & cregens & banksInfo & creBanksNames & resVals;
  1253. }
  1254. };