CObjectHandler.h 50 KB

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