CObjectHandler.h 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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. class CGHeroPlaceholder : public CGObjectInstance
  196. {
  197. public:
  198. //subID stores id of hero type. If it's 0xff then following field is used
  199. ui8 power;
  200. template <typename Handler> void serialize(Handler &h, const int version)
  201. {
  202. h & static_cast<CGObjectInstance&>(*this);
  203. h & power;
  204. }
  205. };
  206. class DLL_LINKAGE CPlayersVisited: public CGObjectInstance
  207. {
  208. public:
  209. std::set<PlayerColor> players; //players that visited this object
  210. bool wasVisited(PlayerColor player) const;
  211. bool wasVisited(TeamID team) const;
  212. void setPropertyDer(ui8 what, ui32 val) override;
  213. template <typename Handler> void serialize(Handler &h, const int version)
  214. {
  215. h & static_cast<CGObjectInstance&>(*this);
  216. h & players;
  217. }
  218. };
  219. class DLL_LINKAGE CArmedInstance: public CGObjectInstance, public CBonusSystemNode, public CCreatureSet
  220. {
  221. public:
  222. BattleInfo *battle; //set to the current battle, if engaged
  223. void randomizeArmy(int type);
  224. virtual void updateMoraleBonusFromArmy();
  225. void armyChanged() override;
  226. //////////////////////////////////////////////////////////////////////////
  227. // int valOfGlobalBonuses(CSelector selector) const; //used only for castle interface ???
  228. virtual CBonusSystemNode *whereShouldBeAttached(CGameState *gs);
  229. virtual CBonusSystemNode *whatShouldBeAttached();
  230. //////////////////////////////////////////////////////////////////////////
  231. CArmedInstance();
  232. template <typename Handler> void serialize(Handler &h, const int version)
  233. {
  234. h & static_cast<CGObjectInstance&>(*this);
  235. h & static_cast<CBonusSystemNode&>(*this);
  236. h & static_cast<CCreatureSet&>(*this);
  237. }
  238. };
  239. class DLL_LINKAGE CGHeroInstance : public CArmedInstance, public IBoatGenerator, public CArtifactSet
  240. {
  241. public:
  242. enum ECanDig
  243. {
  244. CAN_DIG, LACK_OF_MOVEMENT, WRONG_TERRAIN, TILE_OCCUPIED
  245. };
  246. //////////////////////////////////////////////////////////////////////////
  247. ui8 moveDir; //format: 123
  248. // 8 4
  249. // 765
  250. mutable ui8 isStanding, tacticFormationEnabled;
  251. //////////////////////////////////////////////////////////////////////////
  252. ConstTransitivePtr<CHero> type;
  253. TExpType exp; //experience points
  254. ui32 level; //current level of hero
  255. std::string name; //may be custom
  256. std::string biography; //if custom
  257. si32 portrait; //may be custom
  258. si32 mana; // remaining spell points
  259. 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
  260. ui32 movement; //remaining movement points
  261. ui8 sex;
  262. bool inTownGarrison; // if hero is in town garrison
  263. ConstTransitivePtr<CGTownInstance> visitedTown; //set if hero is visiting town or in the town garrison
  264. ConstTransitivePtr<CCommanderInstance> commander;
  265. const CGBoat *boat; //set to CGBoat when sailing
  266. //std::vector<const CArtifact*> artifacts; //hero's artifacts from bag
  267. //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
  268. std::set<SpellID> spells; //known spells (spell IDs)
  269. struct DLL_LINKAGE Patrol
  270. {
  271. Patrol(){patrolling=false;patrolRadious=-1;};
  272. bool patrolling;
  273. ui32 patrolRadious;
  274. template <typename Handler> void serialize(Handler &h, const int version)
  275. {
  276. h & patrolling & patrolRadious;
  277. }
  278. } patrol;
  279. struct DLL_LINKAGE HeroSpecial : CBonusSystemNode
  280. {
  281. bool growsWithLevel;
  282. HeroSpecial(){growsWithLevel = false;};
  283. template <typename Handler> void serialize(Handler &h, const int version)
  284. {
  285. h & static_cast<CBonusSystemNode&>(*this);
  286. h & growsWithLevel;
  287. }
  288. };
  289. std::vector<HeroSpecial*> specialty;
  290. struct DLL_LINKAGE SecondarySkillsInfo
  291. {
  292. //skills are determined, initialized at map start
  293. //FIXME: remove mutable?
  294. mutable std::minstd_rand distribution;
  295. ui8 magicSchoolCounter;
  296. ui8 wisdomCounter;
  297. void resetMagicSchoolCounter();
  298. void resetWisdomCounter();
  299. template <typename Handler> void serialize(Handler &h, const int version)
  300. {
  301. h & magicSchoolCounter & wisdomCounter;
  302. if (h.saving)
  303. {
  304. std::ostringstream stream;
  305. stream << distribution;
  306. std::string str = stream.str();
  307. h & str;
  308. }
  309. else
  310. {
  311. std::string str;
  312. h & str;
  313. std::istringstream stream(str);
  314. stream >> distribution;
  315. }
  316. }
  317. } skillsInfo;
  318. //////////////////////////////////////////////////////////////////////////
  319. template <typename Handler> void serialize(Handler &h, const int version)
  320. {
  321. h & static_cast<CArmedInstance&>(*this);
  322. h & static_cast<CArtifactSet&>(*this);
  323. h & exp & level & name & biography & portrait & mana & secSkills & movement
  324. & sex & inTownGarrison & spells & patrol & moveDir & skillsInfo;
  325. h & visitedTown & boat;
  326. h & type & specialty & commander;
  327. BONUS_TREE_DESERIALIZATION_FIX
  328. //visitied town pointer will be restored by map serialization method
  329. }
  330. //////////////////////////////////////////////////////////////////////////
  331. int3 getSightCenter() const; //"center" tile from which the sight distance is calculated
  332. int getSightRadious() const; //sight distance (should be used if player-owned structure)
  333. //////////////////////////////////////////////////////////////////////////
  334. int getBoatType() const; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  335. void getOutOffsets(std::vector<int3> &offsets) const; //offsets to obj pos when we boat can be placed
  336. //////////////////////////////////////////////////////////////////////////
  337. bool hasSpellbook() const;
  338. EAlignment::EAlignment getAlignment() const;
  339. const std::string &getBiography() const;
  340. bool needsLastStack()const;
  341. 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
  342. ui32 getLowestCreatureSpeed() const;
  343. int3 getPosition(bool h3m = false) const; //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  344. si32 manaRegain() const; //how many points of mana can hero regain "naturally" in one day
  345. bool canWalkOnSea() const;
  346. int getCurrentLuck(int stack=-1, bool town=false) const;
  347. int getSpellCost(const CSpell *sp) const; //do not use during battles -> bonuses from army would be ignored
  348. ui8 getSecSkillLevel(SecondarySkill skill) const; //0 - no skill
  349. void setSecSkillLevel(SecondarySkill which, int val, bool abs);// abs == 0 - changes by value; 1 - sets to value
  350. bool canLearnSkill() const; ///true if hero has free secondary skill slot
  351. int maxMovePoints(bool onLand) const;
  352. int movementPointsAfterEmbark(int MPsBefore, int basicCost, bool disembark = false) const;
  353. //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
  354. static int3 convertPosition(int3 src, bool toh3m); //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  355. double getFightingStrength() const; // takes attack / defense skill into account
  356. double getMagicStrength() const; // takes knowledge / spell power skill into account
  357. double getHeroStrength() const; // includes fighting and magic strength
  358. ui64 getTotalStrength() const; // includes fighting strength and army strength
  359. TExpType calculateXp(TExpType exp) const; //apply learning skill
  360. 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,
  361. 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
  362. CStackBasicDescriptor calculateNecromancy (const BattleResult &battleResult) const;
  363. void showNecromancyDialog(const CStackBasicDescriptor &raisedStack) const;
  364. ECanDig diggingStatus() const; //0 - can dig; 1 - lack of movement; 2 -
  365. std::vector<SecondarySkill> levelUpProposedSkills() const;
  366. bool gainsLevel() const; //true if hero has lower level than should upon his experience
  367. //////////////////////////////////////////////////////////////////////////
  368. void initHero();
  369. void initHero(HeroTypeID SUBID);
  370. void putArtifact(ArtifactPosition pos, CArtifactInstance *art);
  371. void putInBackpack(CArtifactInstance *art);
  372. void initExp();
  373. void initArmy(IArmyDescriptor *dst = nullptr);
  374. //void giveArtifact (ui32 aid);
  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. void updateAppearance();
  582. //////////////////////////////////////////////////////////////////////////
  583. bool needsLastStack() const;
  584. CGTownInstance::EFortLevel fortLevel() const;
  585. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  586. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  587. int creatureDwellingLevel(int dwelling) const;
  588. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  589. int creatureGrowth(const int & level) const;
  590. GrowthInfo getGrowthInfo(int level) const;
  591. bool hasFort() const;
  592. bool hasCapitol() const;
  593. //checks if building is constructed and town has same subID
  594. bool hasBuilt(BuildingID buildingID) const;
  595. bool hasBuilt(BuildingID buildingID, int townID) const;
  596. int dailyIncome() const; //calculates daily income of this town
  597. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  598. bool armedGarrison() const; //true if town has creatures in garrison or garrisoned hero
  599. int getTownLevel() const;
  600. void removeCapitols (PlayerColor owner) const;
  601. void addHeroToStructureVisitors(const CGHeroInstance *h, si32 structureInstanceID) const; //hero must be visiting or garrisoned in town
  602. CGTownInstance();
  603. virtual ~CGTownInstance();
  604. ///IObjectInterface overrides
  605. void newTurn() const override;
  606. void onHeroVisit(const CGHeroInstance * h) const override;
  607. void onHeroLeave(const CGHeroInstance * h) const override;
  608. void initObj() override;
  609. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  610. protected:
  611. void setPropertyDer(ui8 what, ui32 val) override;
  612. };
  613. class DLL_LINKAGE CGPandoraBox : public CArmedInstance
  614. {
  615. public:
  616. std::string message;
  617. bool hasGuardians; //helper - after battle even though we have no stacks, allows us to know that there was battle
  618. //gained things:
  619. ui32 gainedExp;
  620. si32 manaDiff; //amount of gained / lost mana
  621. si32 moraleDiff; //morale modifier
  622. si32 luckDiff; //luck modifier
  623. TResources resources;//gained / lost resources
  624. std::vector<si32> primskills;//gained / lost prim skills
  625. std::vector<SecondarySkill> abilities; //gained abilities
  626. std::vector<si32> abilityLevels; //levels of gained abilities
  627. std::vector<ArtifactID> artifacts; //gained artifacts
  628. std::vector<SpellID> spells; //gained spells
  629. CCreatureSet creatures; //gained creatures
  630. void initObj() override;
  631. void onHeroVisit(const CGHeroInstance * h) const override;
  632. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  633. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  634. void heroLevelUpDone(const CGHeroInstance *hero) const override;
  635. template <typename Handler> void serialize(Handler &h, const int version)
  636. {
  637. h & static_cast<CArmedInstance&>(*this);
  638. h & message & hasGuardians & gainedExp & manaDiff & moraleDiff & luckDiff & resources & primskills
  639. & abilities & abilityLevels & artifacts & spells & creatures;
  640. }
  641. protected:
  642. void giveContentsUpToExp(const CGHeroInstance *h) const;
  643. void giveContentsAfterExp(const CGHeroInstance *h) const;
  644. private:
  645. void getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const;
  646. void getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const;
  647. };
  648. class DLL_LINKAGE CGEvent : public CGPandoraBox //event objects
  649. {
  650. public:
  651. bool removeAfterVisit; //true if event is removed after occurring
  652. ui8 availableFor; //players whom this event is available for
  653. bool computerActivate; //true if computer player can activate this event
  654. bool humanActivate; //true if human player can activate this event
  655. template <typename Handler> void serialize(Handler &h, const int version)
  656. {
  657. h & static_cast<CGPandoraBox &>(*this);
  658. h & removeAfterVisit & availableFor & computerActivate & humanActivate;
  659. }
  660. void onHeroVisit(const CGHeroInstance * h) const override;
  661. private:
  662. void activated(const CGHeroInstance * h) const;
  663. };
  664. class DLL_LINKAGE CGCreature : public CArmedInstance //creatures on map
  665. {
  666. enum Action {
  667. FIGHT = -2, FLEE = -1, JOIN_FOR_FREE = 0 //values > 0 mean gold price
  668. };
  669. public:
  670. ui32 identifier; //unique code for this monster (used in missions)
  671. 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)
  672. std::string message; //message printed for attacking hero
  673. TResources resources; // resources given to hero that has won with monsters
  674. ArtifactID gainedArtifact; //ID of artifact gained to hero, -1 if none
  675. bool neverFlees; //if true, the troops will never flee
  676. bool notGrowingTeam; //if true, number of units won't grow
  677. ui64 temppower; //used to handle fractional stack growth for tiny stacks
  678. bool refusedJoining;
  679. void onHeroVisit(const CGHeroInstance * h) const override;
  680. const std::string & getHoverText() const override;
  681. void initObj() override;
  682. void newTurn() const override;
  683. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  684. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  685. struct DLL_LINKAGE formationInfo // info about merging stacks after battle back into one
  686. {
  687. si32 basicType;
  688. ui32 randomFormation; //random seed used to determine number of stacks and is there's upgraded stack
  689. template <typename Handler> void serialize(Handler &h, const int version)
  690. {
  691. h & basicType & randomFormation;
  692. }
  693. } formation;
  694. template <typename Handler> void serialize(Handler &h, const int version)
  695. {
  696. h & static_cast<CArmedInstance&>(*this);
  697. h & identifier & character & message & resources & gainedArtifact & neverFlees & notGrowingTeam & temppower;
  698. h & refusedJoining & formation;
  699. }
  700. protected:
  701. void setPropertyDer(ui8 what, ui32 val) override;
  702. private:
  703. void fight(const CGHeroInstance *h) const;
  704. void flee( const CGHeroInstance * h ) const;
  705. void fleeDecision(const CGHeroInstance *h, ui32 pursue) const;
  706. void joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const;
  707. 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)
  708. };
  709. class DLL_LINKAGE CGSignBottle : public CGObjectInstance //signs and ocean bottles
  710. {
  711. public:
  712. std::string message;
  713. void onHeroVisit(const CGHeroInstance * h) const override;
  714. void initObj() override;
  715. template <typename Handler> void serialize(Handler &h, const int version)
  716. {
  717. h & static_cast<CGObjectInstance&>(*this);
  718. h & message;
  719. }
  720. };
  721. class DLL_LINKAGE IQuestObject
  722. {
  723. public:
  724. CQuest * quest;
  725. IQuestObject(): quest(new CQuest()){};
  726. virtual ~IQuestObject() {};
  727. virtual void getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  728. virtual bool checkQuest (const CGHeroInstance * h) const;
  729. template <typename Handler> void serialize(Handler &h, const int version)
  730. {
  731. h & quest;
  732. }
  733. };
  734. class DLL_LINKAGE CGSeerHut : public CArmedInstance, public IQuestObject //army is used when giving reward
  735. {
  736. public:
  737. enum ERewardType {NOTHING, EXPERIENCE, MANA_POINTS, MORALE_BONUS, LUCK_BONUS, RESOURCES, PRIMARY_SKILL, SECONDARY_SKILL, ARTIFACT, SPELL, CREATURE};
  738. ERewardType rewardType;
  739. si32 rID; //reward ID
  740. si32 rVal; //reward value
  741. std::string seerName;
  742. CGSeerHut() : IQuestObject(){};
  743. void initObj() override;
  744. const std::string & getHoverText() const override;
  745. void newTurn() const override;
  746. void onHeroVisit(const CGHeroInstance * h) const override;
  747. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  748. virtual void init();
  749. int checkDirection() const; //calculates the region of map where monster is placed
  750. void setObjToKill(); //remember creatures / heroes to kill after they are initialized
  751. const CGHeroInstance *getHeroToKill(bool allowNull = false) const;
  752. const CGCreature *getCreatureToKill(bool allowNull = false) const;
  753. void getRolloverText (MetaString &text, bool onHover) const;
  754. void getCompletionText(MetaString &text, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h = nullptr) const;
  755. void finishQuest (const CGHeroInstance * h, ui32 accept) const; //common for both objects
  756. virtual void completeQuest (const CGHeroInstance * h) const;
  757. template <typename Handler> void serialize(Handler &h, const int version)
  758. {
  759. h & static_cast<CArmedInstance&>(*this) & static_cast<IQuestObject&>(*this);
  760. h & rewardType & rID & rVal & seerName;
  761. }
  762. protected:
  763. void setPropertyDer(ui8 what, ui32 val) override;
  764. };
  765. class DLL_LINKAGE CGQuestGuard : public CGSeerHut
  766. {
  767. public:
  768. CGQuestGuard() : CGSeerHut(){};
  769. void init() override;
  770. void completeQuest (const CGHeroInstance * h) const override;
  771. template <typename Handler> void serialize(Handler &h, const int version)
  772. {
  773. h & static_cast<CGSeerHut&>(*this);
  774. }
  775. };
  776. class DLL_LINKAGE CGWitchHut : public CPlayersVisited
  777. {
  778. public:
  779. std::vector<si32> allowedAbilities;
  780. ui32 ability;
  781. const std::string & getHoverText() const override;
  782. void onHeroVisit(const CGHeroInstance * h) const override;
  783. void initObj() override;
  784. template <typename Handler> void serialize(Handler &h, const int version)
  785. {
  786. h & static_cast<CPlayersVisited&>(*this);
  787. h & allowedAbilities & ability;
  788. }
  789. };
  790. class DLL_LINKAGE CGScholar : public CGObjectInstance
  791. {
  792. public:
  793. enum EBonusType {PRIM_SKILL, SECONDARY_SKILL, SPELL, RANDOM = 255};
  794. EBonusType bonusType;
  795. ui16 bonusID; //ID of skill/spell
  796. // void giveAnyBonus(const CGHeroInstance * h) const; //TODO: remove
  797. void onHeroVisit(const CGHeroInstance * h) const override;
  798. void initObj() override;
  799. template <typename Handler> void serialize(Handler &h, const int version)
  800. {
  801. h & static_cast<CGObjectInstance&>(*this);
  802. h & bonusType & bonusID;
  803. }
  804. };
  805. class DLL_LINKAGE CGGarrison : public CArmedInstance
  806. {
  807. public:
  808. bool removableUnits;
  809. ui8 getPassableness() const;
  810. void onHeroVisit(const CGHeroInstance * h) const override;
  811. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  812. template <typename Handler> void serialize(Handler &h, const int version)
  813. {
  814. h & static_cast<CArmedInstance&>(*this);
  815. h & removableUnits;
  816. }
  817. };
  818. class DLL_LINKAGE CGArtifact : public CArmedInstance
  819. {
  820. public:
  821. CArtifactInstance *storedArtifact;
  822. std::string message;
  823. void onHeroVisit(const CGHeroInstance * h) const override;
  824. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  825. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  826. void pick( const CGHeroInstance * h ) const;
  827. void initObj() override;
  828. template <typename Handler> void serialize(Handler &h, const int version)
  829. {
  830. h & static_cast<CArmedInstance&>(*this);
  831. h & message & storedArtifact;
  832. }
  833. };
  834. class DLL_LINKAGE CGResource : public CArmedInstance
  835. {
  836. public:
  837. ui32 amount; //0 if random
  838. std::string message;
  839. void onHeroVisit(const CGHeroInstance * h) const override;
  840. void initObj() override;
  841. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  842. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  843. void collectRes(PlayerColor player) const;
  844. template <typename Handler> void serialize(Handler &h, const int version)
  845. {
  846. h & static_cast<CArmedInstance&>(*this);
  847. h & amount & message;
  848. }
  849. };
  850. class DLL_LINKAGE CGPickable : public CGObjectInstance //campfire, treasure chest, Flotsam, Shipwreck Survivor, Sea Chest
  851. {
  852. public:
  853. ui32 type, val1, val2;
  854. void onHeroVisit(const CGHeroInstance * h) const override;
  855. void initObj() override;
  856. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  857. template <typename Handler> void serialize(Handler &h, const int version)
  858. {
  859. h & static_cast<CGObjectInstance&>(*this);
  860. h & type & val1 & val2;
  861. }
  862. };
  863. class DLL_LINKAGE CGShrine : public CPlayersVisited
  864. {
  865. public:
  866. SpellID spell; //id of spell or NONE if random
  867. void onHeroVisit(const CGHeroInstance * h) const override;
  868. void initObj() override;
  869. const std::string & getHoverText() const override;
  870. template <typename Handler> void serialize(Handler &h, const int version)
  871. {
  872. h & static_cast<CPlayersVisited&>(*this);;
  873. h & spell;
  874. }
  875. };
  876. class DLL_LINKAGE CGMine : public CArmedInstance
  877. {
  878. public:
  879. Res::ERes producedResource;
  880. ui32 producedQuantity;
  881. void onHeroVisit(const CGHeroInstance * h) const override;
  882. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  883. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  884. void flagMine(PlayerColor player) const;
  885. void newTurn() const override;
  886. void initObj() override;
  887. template <typename Handler> void serialize(Handler &h, const int version)
  888. {
  889. h & static_cast<CArmedInstance&>(*this);
  890. h & producedResource & producedQuantity;
  891. }
  892. ui32 defaultResProduction();
  893. };
  894. class DLL_LINKAGE CGVisitableOPW : public CGObjectInstance //objects visitable OPW
  895. {
  896. public:
  897. ui8 visited; //true if object has been visited this week
  898. bool wasVisited(PlayerColor player) const;
  899. void onHeroVisit(const CGHeroInstance * h) const override;
  900. virtual void newTurn() const override;
  901. template <typename Handler> void serialize(Handler &h, const int version)
  902. {
  903. h & static_cast<CGObjectInstance&>(*this);
  904. h & visited;
  905. }
  906. protected:
  907. void setPropertyDer(ui8 what, ui32 val) override;
  908. };
  909. class DLL_LINKAGE CGTeleport : public CGObjectInstance //teleports and subterranean gates
  910. {
  911. public:
  912. static std::map<Obj, std::map<int, std::vector<ObjectInstanceID> > > objs; //teleports: map[ID][subID] => vector of ids
  913. static std::vector<std::pair<ObjectInstanceID, ObjectInstanceID> > gates; //subterranean gates: pairs of ids
  914. void onHeroVisit(const CGHeroInstance * h) const override;
  915. void initObj() override;
  916. static void postInit();
  917. static ObjectInstanceID getMatchingGate(ObjectInstanceID id); //receives id of one subterranean gate and returns id of the paired one, -1 if none
  918. template <typename Handler> void serialize(Handler &h, const int version)
  919. {
  920. h & static_cast<CGObjectInstance&>(*this);
  921. }
  922. };
  923. class DLL_LINKAGE CGBonusingObject : public CGObjectInstance //objects giving bonuses to luck/morale/movement
  924. {
  925. public:
  926. bool wasVisited (const CGHeroInstance * h) const;
  927. void onHeroVisit(const CGHeroInstance * h) const override;
  928. const std::string & getHoverText() const override;
  929. void initObj() override;
  930. template <typename Handler> void serialize(Handler &h, const int version)
  931. {
  932. h & static_cast<CGObjectInstance&>(*this);
  933. }
  934. };
  935. class DLL_LINKAGE CGMagicSpring : public CGVisitableOPW
  936. {///unfortunately, this one is quite different than others
  937. enum EVisitedEntrance
  938. {
  939. CLEAR = 0, LEFT = 1, RIGHT
  940. };
  941. public:
  942. EVisitedEntrance visitedTile; //only one entrance was visited - there are two
  943. std::vector<int3> getVisitableOffsets() const;
  944. int3 getVisitableOffset() const override;
  945. void setPropertyDer(ui8 what, ui32 val) override;
  946. void newTurn() const override;
  947. void onHeroVisit(const CGHeroInstance * h) const override;
  948. const std::string & getHoverText() const override;
  949. template <typename Handler> void serialize(Handler &h, const int version)
  950. {
  951. h & static_cast<CGObjectInstance&>(*this);
  952. h & visitedTile & visited;
  953. }
  954. };
  955. class DLL_LINKAGE CGMagicWell : public CGObjectInstance //objects giving bonuses to luck/morale/movement
  956. {
  957. public:
  958. void onHeroVisit(const CGHeroInstance * h) const override;
  959. const std::string & getHoverText() const override;
  960. template <typename Handler> void serialize(Handler &h, const int version)
  961. {
  962. h & static_cast<CGObjectInstance&>(*this);
  963. }
  964. };
  965. class DLL_LINKAGE CGSirens : public CGObjectInstance
  966. {
  967. public:
  968. void onHeroVisit(const CGHeroInstance * h) const override;
  969. const std::string & getHoverText() const override;
  970. void initObj() override;
  971. template <typename Handler> void serialize(Handler &h, const int version)
  972. {
  973. h & static_cast<CGObjectInstance&>(*this);
  974. }
  975. };
  976. class DLL_LINKAGE CGObservatory : public CGObjectInstance //Redwood observatory
  977. {
  978. public:
  979. void onHeroVisit(const CGHeroInstance * h) const 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 CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  986. {
  987. public:
  988. static std::map <PlayerColor, std::set <ui8> > playerKeyMap; //[players][keysowned]
  989. //SubID 0 - lightblue, 1 - green, 2 - red, 3 - darkblue, 4 - brown, 5 - purple, 6 - white, 7 - black
  990. const std::string getName() const; //depending on color
  991. bool wasMyColorVisited (PlayerColor player) const;
  992. const std::string & getHoverText() const override;
  993. template <typename Handler> void serialize(Handler &h, const int version)
  994. {
  995. h & static_cast<CGObjectInstance&>(*this);
  996. }
  997. protected:
  998. void setPropertyDer(ui8 what, ui32 val) override;
  999. };
  1000. class DLL_LINKAGE CGKeymasterTent : public CGKeys
  1001. {
  1002. public:
  1003. bool wasVisited (PlayerColor player) const;
  1004. void onHeroVisit(const CGHeroInstance * h) const override;
  1005. template <typename Handler> void serialize(Handler &h, const int version)
  1006. {
  1007. h & static_cast<CGObjectInstance&>(*this);
  1008. }
  1009. };
  1010. class DLL_LINKAGE CGBorderGuard : public CGKeys, public IQuestObject
  1011. {
  1012. public:
  1013. CGBorderGuard() : IQuestObject(){};
  1014. void initObj() override;
  1015. void onHeroVisit(const CGHeroInstance * h) const override;
  1016. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  1017. void getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h = nullptr) const;
  1018. void getRolloverText (MetaString &text, bool onHover) const;
  1019. bool checkQuest (const CGHeroInstance * h) const;
  1020. template <typename Handler> void serialize(Handler &h, const int version)
  1021. {
  1022. h & static_cast<IQuestObject&>(*this);
  1023. h & static_cast<CGObjectInstance&>(*this);
  1024. h & blockVisit;
  1025. }
  1026. };
  1027. class DLL_LINKAGE CGBorderGate : public CGBorderGuard
  1028. {
  1029. public:
  1030. CGBorderGate() : CGBorderGuard(){};
  1031. void onHeroVisit(const CGHeroInstance * h) const override;
  1032. ui8 getPassableness() const override;
  1033. template <typename Handler> void serialize(Handler &h, const int version)
  1034. {
  1035. h & static_cast<CGBorderGuard&>(*this); //need to serialize or object will be empty
  1036. }
  1037. };
  1038. class DLL_LINKAGE CGBoat : public CGObjectInstance
  1039. {
  1040. public:
  1041. ui8 direction;
  1042. const CGHeroInstance *hero; //hero on board
  1043. void initObj() override;
  1044. CGBoat()
  1045. {
  1046. hero = nullptr;
  1047. direction = 4;
  1048. }
  1049. template <typename Handler> void serialize(Handler &h, const int version)
  1050. {
  1051. h & static_cast<CGObjectInstance&>(*this) & direction & hero;
  1052. }
  1053. };
  1054. class DLL_LINKAGE CGOnceVisitable : public CPlayersVisited
  1055. ///wagon, corpse, lean to, warriors tomb
  1056. {
  1057. public:
  1058. ui8 artOrRes; //0 - nothing; 1 - artifact; 2 - resource
  1059. ui32 bonusType, //id of res or artifact
  1060. bonusVal; //resource amount (or not used)
  1061. void onHeroVisit(const CGHeroInstance * h) const override;
  1062. const std::string & getHoverText() const override;
  1063. void initObj() override;
  1064. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  1065. template <typename Handler> void serialize(Handler &h, const int version)
  1066. {
  1067. h & static_cast<CPlayersVisited&>(*this);;
  1068. h & artOrRes & bonusType & bonusVal;
  1069. }
  1070. };
  1071. class DLL_LINKAGE CBank : public CArmedInstance
  1072. {
  1073. public:
  1074. int index; //banks have unusal numbering - see ZCRBANK.txt and initObj()
  1075. BankConfig *bc;
  1076. double multiplier; //for improved banks script
  1077. std::vector<ui32> artifacts; //fixed and deterministic
  1078. ui32 daycounter;
  1079. void initObj() override;
  1080. const std::string & getHoverText() const override;
  1081. void initialize() const;
  1082. void reset(ui16 var1);
  1083. void newTurn() const override;
  1084. bool wasVisited (PlayerColor player) const override;
  1085. void onHeroVisit(const CGHeroInstance * h) const override;
  1086. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  1087. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  1088. template <typename Handler> void serialize(Handler &h, const int version)
  1089. {
  1090. h & static_cast<CArmedInstance&>(*this);
  1091. h & index & multiplier & artifacts & daycounter & bc;
  1092. }
  1093. protected:
  1094. void setPropertyDer(ui8 what, ui32 val) override;
  1095. };
  1096. class DLL_LINKAGE CGPyramid : public CBank
  1097. {
  1098. public:
  1099. ui16 spell;
  1100. void initObj() override;
  1101. const std::string & getHoverText() const override;
  1102. void newTurn() const override {}; //empty, no reset
  1103. void onHeroVisit(const CGHeroInstance * h) const override;
  1104. void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const override;
  1105. template <typename Handler> void serialize(Handler &h, const int version)
  1106. {
  1107. h & static_cast<CBank&>(*this);
  1108. h & spell;
  1109. }
  1110. };
  1111. class CGShipyard : public CGObjectInstance, public IShipyard
  1112. {
  1113. public:
  1114. void getOutOffsets(std::vector<int3> &offsets) const; //offsets to obj pos when we boat can be placed
  1115. CGShipyard();
  1116. void onHeroVisit(const CGHeroInstance * h) const override;
  1117. };
  1118. class DLL_LINKAGE CGMagi : public CGObjectInstance
  1119. {
  1120. public:
  1121. static std::map <si32, std::vector<ObjectInstanceID> > eyelist; //[subID][id], supports multiple sets as in H5
  1122. void initObj() override;
  1123. void onHeroVisit(const CGHeroInstance * h) const override;
  1124. template <typename Handler> void serialize(Handler &h, const int version)
  1125. {
  1126. h & static_cast<CGObjectInstance&>(*this);
  1127. }
  1128. };
  1129. class DLL_LINKAGE CCartographer : public CPlayersVisited
  1130. {
  1131. ///behaviour varies depending on surface and floor
  1132. public:
  1133. void onHeroVisit(const CGHeroInstance * h) const override;
  1134. void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const override;
  1135. template <typename Handler> void serialize(Handler &h, const int version)
  1136. {
  1137. h & static_cast<CPlayersVisited&>(*this);
  1138. }
  1139. };
  1140. class DLL_LINKAGE CGDenOfthieves : public CGObjectInstance
  1141. {
  1142. void onHeroVisit(const CGHeroInstance * h) const override;
  1143. };
  1144. class DLL_LINKAGE CGObelisk : public CPlayersVisited
  1145. {
  1146. public:
  1147. static ui8 obeliskCount; //how many obelisks are on map
  1148. static std::map<TeamID, ui8> visited; //map: team_id => how many obelisks has been visited
  1149. void onHeroVisit(const CGHeroInstance * h) const override;
  1150. void initObj() override;
  1151. const std::string & getHoverText() const override;
  1152. template <typename Handler> void serialize(Handler &h, const int version)
  1153. {
  1154. h & static_cast<CPlayersVisited&>(*this);
  1155. }
  1156. protected:
  1157. void setPropertyDer(ui8 what, ui32 val) override;
  1158. };
  1159. class DLL_LINKAGE CGLighthouse : public CGObjectInstance
  1160. {
  1161. public:
  1162. void onHeroVisit(const CGHeroInstance * h) const override;
  1163. void initObj() override;
  1164. const std::string & getHoverText() const override;
  1165. template <typename Handler> void serialize(Handler &h, const int version)
  1166. {
  1167. h & static_cast<CGObjectInstance&>(*this);
  1168. }
  1169. void giveBonusTo( PlayerColor player ) const;
  1170. };
  1171. class DLL_LINKAGE CGMarket : public CGObjectInstance, public IMarket
  1172. {
  1173. public:
  1174. CGMarket();
  1175. ///IObjectIntercae
  1176. void onHeroVisit(const CGHeroInstance * h) const override; //open trading window
  1177. ///IMarket
  1178. int getMarketEfficiency() const override;
  1179. bool allowsTrade(EMarketMode::EMarketMode mode) const override;
  1180. int availableUnits(EMarketMode::EMarketMode mode, int marketItemSerial) const override; //-1 if unlimited
  1181. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  1182. template <typename Handler> void serialize(Handler &h, const int version)
  1183. {
  1184. h & static_cast<CGObjectInstance&>(*this);
  1185. }
  1186. };
  1187. class DLL_LINKAGE CGBlackMarket : public CGMarket
  1188. {
  1189. public:
  1190. std::vector<const CArtifact *> artifacts; //available artifacts
  1191. void newTurn() const override; //reset artifacts for black market every month
  1192. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const override;
  1193. template <typename Handler> void serialize(Handler &h, const int version)
  1194. {
  1195. h & static_cast<CGMarket&>(*this);
  1196. h & artifacts;
  1197. }
  1198. };
  1199. class DLL_LINKAGE CGUniversity : public CGMarket
  1200. {
  1201. public:
  1202. std::vector<int> skills; //available skills
  1203. std::vector<int> availableItemsIds(EMarketMode::EMarketMode mode) const;
  1204. void initObj() override;//set skills for trade
  1205. void onHeroVisit(const CGHeroInstance * h) const override; //open window
  1206. template <typename Handler> void serialize(Handler &h, const int version)
  1207. {
  1208. h & static_cast<CGMarket&>(*this);
  1209. h & skills;
  1210. }
  1211. };
  1212. struct BankConfig
  1213. {
  1214. BankConfig() {level = chance = upgradeChance = combatValue = value = rewardDifficulty = easiest = 0; };
  1215. ui8 level; //1 - 4, how hard the battle will be
  1216. ui8 chance; //chance for this level being chosen
  1217. ui8 upgradeChance; //chance for creatures to be in upgraded versions
  1218. std::vector< std::pair <CreatureID, ui32> > guards; //creature ID, amount
  1219. ui32 combatValue; //how hard are guards of this level
  1220. Res::ResourceSet resources; //resources given in case of victory
  1221. std::vector< std::pair <CreatureID, ui32> > creatures; //creatures granted in case of victory (creature ID, amount)
  1222. std::vector<ui16> artifacts; //number of artifacts given in case of victory [0] -> treasure, [1] -> minor [2] -> major [3] -> relic
  1223. ui32 value; //overall value of given things
  1224. ui32 rewardDifficulty; //proportion of reward value to difficulty of guards; how profitable is this creature Bank config
  1225. ui16 easiest; //?!?
  1226. template <typename Handler> void serialize(Handler &h, const int version)
  1227. {
  1228. h & level & chance & upgradeChance & guards & combatValue & resources & creatures & artifacts & value & rewardDifficulty & easiest;
  1229. }
  1230. };
  1231. class DLL_LINKAGE CObjectHandler
  1232. {
  1233. public:
  1234. std::map<si32, CreatureID> cregens; //type 17. dwelling subid -> creature ID
  1235. std::map <ui32, std::vector < ConstTransitivePtr<BankConfig> > > banksInfo; //[index][preset]
  1236. std::map <ui32, std::string> creBanksNames; //[crebank index] -> name of this creature bank
  1237. std::vector<ui32> resVals; //default values of resources in gold
  1238. CObjectHandler();
  1239. ~CObjectHandler();
  1240. int bankObjToIndex (const CGObjectInstance * obj);
  1241. template <typename Handler> void serialize(Handler &h, const int version)
  1242. {
  1243. h & cregens & banksInfo & creBanksNames & resVals;
  1244. }
  1245. };