CObjectHandler.h 51 KB

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