CObjectHandler.h 55 KB

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