CObjectHandler.h 53 KB

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