CObjectHandler.h 53 KB

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