CObjectHandler.h 53 KB

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