CObjectHandler.h 52 KB

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