CObjectHandler.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. #ifndef __COBJECTHANDLER_H__
  2. #define __COBJECTHANDLER_H__
  3. #include "../global.h"
  4. #include <string>
  5. #include <vector>
  6. #include <set>
  7. #include <map>
  8. #include <list>
  9. #include "CCreatureHandler.h"
  10. #include "../lib/HeroBonus.h"
  11. #ifndef _MSC_VER
  12. #include "CHeroHandler.h"
  13. #include "CTownHandler.h"
  14. #include "../lib/VCMI_Lib.h"
  15. #endif
  16. /*
  17. * CObjectHandler.h, part of VCMI engine
  18. *
  19. * Authors: listed in file AUTHORS in main folder
  20. *
  21. * License: GNU General Public License v2.0 or later
  22. * Full text of license available in license.txt file, in main folder
  23. *
  24. */
  25. class IGameCallback;
  26. struct BattleResult;
  27. class CCPPObjectScript;
  28. class CGObjectInstance;
  29. class CScript;
  30. class CObjectScript;
  31. class CGHeroInstance;
  32. class CTown;
  33. class CHero;
  34. class CBuilding;
  35. class CSpell;
  36. class CGTownInstance;
  37. class CGTownBuilding;
  38. class CArtifact;
  39. class CGDefInfo;
  40. class CSpecObjInfo;
  41. struct TerrainTile;
  42. struct InfoWindow;
  43. struct Component;
  44. struct BankConfig;
  45. class CGBoat;
  46. class DLL_EXPORT CCastleEvent
  47. {
  48. public:
  49. std::string name, message;
  50. std::vector<si32> resources; //gain / loss of resources
  51. ui8 players; //players for whom this event can be applied
  52. ui8 forHuman, forComputer;
  53. ui32 firstShow; //postpone of first encounter time in days
  54. ui32 forEvery; //every n days this event will occure
  55. ui8 bytes[6]; //build specific buildings (raw format, similar to town's)
  56. si32 gen[7]; //additional creatures in i-th level dwelling
  57. bool operator<(const CCastleEvent &drugie) const
  58. {
  59. return firstShow<drugie.firstShow;
  60. }
  61. template <typename Handler> void serialize(Handler &h, const int version)
  62. {
  63. h & name & message & resources & players & forHuman & forComputer & firstShow
  64. & forEvery & bytes & gen;
  65. }
  66. };
  67. class CQuest
  68. {
  69. public:
  70. enum Emission {MISSION_NONE = 0, MISSION_LEVEL = 1, MISSION_PRIMARY_STAT = 2, MISSION_KILL_HERO = 3, MISSION_KILL_CREATURE = 4,
  71. MISSION_ART = 5, MISSION_ARMY = 6, MISSION_RESOURCES = 7, MISSION_HERO = 8, MISSION_PLAYER = 9};
  72. ui8 missionType, progress;
  73. si32 lastDay; //after this day (first day is 0) mission cannot be completed; if -1 - no limit
  74. ui32 m13489val;
  75. std::vector<ui32> m2stats;
  76. std::vector<ui16> m5arts; //artifacts id
  77. TSlots m6creatures; //pair[cre id, cre count], CreatureSet info irrelevant
  78. std::vector<ui32> m7resources;
  79. std::string firstVisitText, nextVisitText, completedText;
  80. bool checkQuest (const CGHeroInstance * h) const; //determines whether the quest is complete or not
  81. virtual void completeQuest (const CGHeroInstance * h) const {};
  82. template <typename Handler> void serialize(Handler &h, const int version)
  83. {
  84. h & missionType & progress & lastDay & m13489val & m2stats & m5arts & m6creatures & m7resources
  85. & firstVisitText & nextVisitText & completedText;
  86. }
  87. };
  88. class DLL_EXPORT IObjectInterface
  89. {
  90. public:
  91. static IGameCallback *cb;
  92. IObjectInterface();
  93. virtual ~IObjectInterface();
  94. virtual void onHeroVisit(const CGHeroInstance * h) const;
  95. virtual void onHeroLeave(const CGHeroInstance * h) const;
  96. virtual void newTurn() const;
  97. virtual void initObj(); //synchr
  98. virtual void setProperty(ui8 what, ui32 val);//synchr
  99. static void preInit(); //called before objs receive their initObj
  100. static void postInit();//caleed after objs receive their initObj
  101. };
  102. class DLL_EXPORT IShipyard
  103. {
  104. public:
  105. const CGObjectInstance *o;
  106. IShipyard(const CGObjectInstance *O);
  107. void getBoatCost(std::vector<si32> &cost) const;
  108. virtual void getOutOffsets(std::vector<int3> &offsets) const =0; //offsets to obj pos when we boat can be placed
  109. //virtual bool validLocation() const; //returns true if there is a water tile near where boat can be placed
  110. int3 bestLocation() const; //returns location when the boat should be placed
  111. int state() const; //0 - can buid, 1 - there is already a boat at dest tile, 2 - dest tile is blocked, 3 - no water
  112. static const IShipyard *castFrom(const CGObjectInstance *obj);
  113. static IShipyard *castFrom(CGObjectInstance *obj);
  114. };
  115. class DLL_EXPORT CGObjectInstance : public IObjectInterface
  116. {
  117. protected:
  118. void getNameVis(std::string &hname) const;
  119. void giveDummyBonus(int heroID, ui8 duration = HeroBonus::ONE_DAY) const;
  120. public:
  121. mutable std::string hoverName;
  122. int3 pos; //h3m pos
  123. si32 ID, subID; //normal ID (this one from OH3 maps ;]) - eg. town=98; hero=34
  124. si32 id;//number of object in CObjectHandler's vector
  125. CGDefInfo * defInfo;
  126. CSpecObjInfo * info;
  127. ui8 animPhaseShift;
  128. ui8 tempOwner;
  129. ui8 blockVisit; //if non-zero then blocks the tile but is visitable from neighbouring tile
  130. 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
  131. virtual int3 getSightCenter() const; //"center" tile from which the sight distance is calculated
  132. virtual int getSightRadious() const; //sight distance (should be used if player-owned structure)
  133. void getSightTiles(std::set<int3> &tiles) const; //returns reference to the set
  134. int getOwner() const;
  135. void setOwner(int ow);
  136. int getWidth() const; //returns width of object graphic in tiles
  137. int getHeight() const; //returns height of object graphic in tiles
  138. 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)
  139. int3 getVisitableOffset() const; //returns (x,y,0) offset to first visitable tile from bottom right obj tile (0,0,0) (h3m pos)
  140. 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)
  141. 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)
  142. std::set<int3> getBlockedPos() const; //returns set of positions blocked by this object
  143. bool operator<(const CGObjectInstance & cmp) const; //screen printing priority comparing
  144. CGObjectInstance();
  145. virtual ~CGObjectInstance();
  146. //CGObjectInstance(const CGObjectInstance & right);
  147. //CGObjectInstance& operator=(const CGObjectInstance & right);
  148. virtual const std::string & getHoverText() const;
  149. //////////////////////////////////////////////////////////////////////////
  150. void initObj();
  151. void onHeroVisit(const CGHeroInstance * h) const;
  152. void setProperty(ui8 what, ui32 val);//synchr
  153. virtual void setPropertyDer(ui8 what, ui32 val);//synchr
  154. friend class CGameHandler;
  155. template <typename Handler> void serialize(Handler &h, const int version)
  156. {
  157. h & hoverName & pos & ID & subID & id & animPhaseShift & tempOwner & blockVisit;
  158. //definfo is handled by map serializer
  159. }
  160. };
  161. class CGHeroPlaceholder : public CGObjectInstance
  162. {
  163. public:
  164. //subID stores id of hero type. If it's 0xff then following field is used
  165. ui8 power;
  166. template <typename Handler> void serialize(Handler &h, const int version)
  167. {
  168. h & static_cast<CGObjectInstance&>(*this);
  169. h & power;
  170. }
  171. };
  172. class DLL_EXPORT CPlayersVisited: public CGObjectInstance
  173. {
  174. public:
  175. std::set<ui8> players; //players that visited this object
  176. bool hasVisited(ui8 player) const;
  177. virtual void setPropertyDer( ui8 what, ui32 val );
  178. template <typename Handler> void serialize(Handler &h, const int version)
  179. {
  180. h & static_cast<CGObjectInstance&>(*this);
  181. h & players;
  182. }
  183. };
  184. class DLL_EXPORT CArmedInstance: public CGObjectInstance
  185. {
  186. public:
  187. CCreatureSet army; //army
  188. virtual bool needsLastStack() const; //true if last stack cannot be taken
  189. int getArmyStrength() const; //sum of AI values of creatures
  190. ui64 getPower (TSlot slot) const; //value of specific stack
  191. std::string getRoughAmount (TSlot slot) const; //rought size of specific stack
  192. template <typename Handler> void serialize(Handler &h, const int version)
  193. {
  194. h & static_cast<CGObjectInstance&>(*this);
  195. h & army;
  196. }
  197. };
  198. class DLL_EXPORT CGHeroInstance : public CArmedInstance
  199. {
  200. public:
  201. //////////////////////////////////////////////////////////////////////////
  202. ui8 moveDir; //format: 123
  203. // 8 4
  204. // 765
  205. mutable ui8 isStanding, tacticFormationEnabled;
  206. //////////////////////////////////////////////////////////////////////////
  207. CHero * type;
  208. ui64 exp; //experience points
  209. si32 level; //current level of hero
  210. std::string name; //may be custom
  211. std::string biography; //if custom
  212. si32 portrait; //may be custom
  213. si32 mana; // remaining spell points
  214. std::vector<si32> primSkills; //0-attack, 1-defence, 2-spell power, 3-knowledge
  215. std::vector<std::pair<ui8,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
  216. si32 movement; //remaining movement points
  217. si32 identifier; //from the map file
  218. ui8 sex;
  219. ui8 inTownGarrison; // if hero is in town garrison
  220. CGTownInstance * visitedTown; //set if hero is visiting town or in the town garrison
  221. CGBoat *boat; //set to CGBoat when sailing
  222. std::vector<ui32> artifacts; //hero's artifacts from bag
  223. std::map<ui16,ui32> 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
  224. std::set<ui32> spells; //known spells (spell IDs)
  225. struct DLL_EXPORT Patrol
  226. {
  227. Patrol(){patrolling=false;patrolRadious=-1;};
  228. ui8 patrolling;
  229. si32 patrolRadious;
  230. template <typename Handler> void serialize(Handler &h, const int version)
  231. {
  232. h & patrolling & patrolRadious;
  233. }
  234. } patrol;
  235. std::list<HeroBonus> bonuses;
  236. //////////////////////////////////////////////////////////////////////////
  237. template <typename Handler> void serialize(Handler &h, const int version)
  238. {
  239. h & static_cast<CArmedInstance&>(*this);
  240. h & exp & level & name & biography & portrait & mana & primSkills & secSkills & movement
  241. & identifier & sex & inTownGarrison & artifacts & artifWorn & spells & patrol & bonuses
  242. & moveDir;
  243. ui8 standardType = (VLC->heroh->heroes[subID] == type);
  244. h & standardType;
  245. if(!standardType)
  246. h & type;
  247. else if(!h.saving)
  248. type = VLC->heroh->heroes[subID];
  249. //visitied town pointer will be restored by map serialization method
  250. }
  251. //////////////////////////////////////////////////////////////////////////
  252. int3 getSightCenter() const; //"center" tile from which the sight distance is calculated
  253. int getSightRadious() const; //sight distance (should be used if player-owned structure)
  254. //////////////////////////////////////////////////////////////////////////
  255. const HeroBonus *getBonus(int from, int id) const;
  256. int valOfBonuses(HeroBonus::BonusType type, int subtype = -1) const; //subtype -> subtype of bonus, if -1 then any
  257. bool hasBonusOfType(HeroBonus::BonusType type, int subtype = -1) const; //determines if hero has a bonus of given type (and optionally subtype)
  258. const std::string &getBiography() const;
  259. bool needsLastStack()const;
  260. unsigned int 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
  261. unsigned int getLowestCreatureSpeed() const;
  262. int3 getPosition(bool h3m) const; //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  263. si32 manaLimit() const; //maximum mana value for this hero (basically 10*knowledge)
  264. si32 manaRegain() const; //how many points of mana can hero regain "naturally" in one day
  265. bool canWalkOnSea() const;
  266. int getCurrentLuck(int stack=-1, bool town=false) const;
  267. std::vector<std::pair<int,std::string> > getCurrentLuckModifiers(int stack=-1, bool town=false) const; //args as above
  268. int getCurrentMorale(int stack=-1, bool town=false) const; //if stack - position of creature, if -1 then morale for hero is calculated; town - if bonuses from town (tavern) should be considered
  269. std::vector<std::pair<int,std::string> > getCurrentMoraleModifiers(int stack=-1, bool town=false) const; //args as above
  270. int getPrimSkillLevel(int id) const;
  271. ui8 getSecSkillLevel(const int & ID) const; //0 - no skill
  272. int maxMovePoints(bool onLand) const;
  273. ui32 getArtAtPos(ui16 pos) const; //-1 - no artifact
  274. const CArtifact * getArt(int pos) const;
  275. si32 getArtPos(int aid) const; //looks for equipped artifact with given ID and returns its slot ID or -1 if none(if more than one such artifact lower ID is returned)
  276. bool hasArt(ui32 aid) const; //checks if hero possess artifact of given id (either in backack or worn)
  277. 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
  278. static int3 convertPosition(int3 src, bool toh3m); //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  279. double getHeroStrength() const;
  280. int getTotalStrength() const;
  281. ui8 getSpellSchoolLevel(const CSpell * spell) const; //returns level on which given spell would be cast by this hero (0 - none, 1 - basic etc)
  282. 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
  283. std::pair<ui32, si32> calculateNecromancy (const BattleResult &battleResult) const;
  284. void showNecromancyDialog (std::pair<ui32, si32> raisedStack) const;
  285. //////////////////////////////////////////////////////////////////////////
  286. void initHero();
  287. void initHero(int SUBID);
  288. void recreateArtBonuses();
  289. void giveArtifact (ui32 aid);
  290. void initHeroDefInfo();
  291. CGHeroInstance();
  292. virtual ~CGHeroInstance();
  293. //////////////////////////////////////////////////////////////////////////
  294. void setPropertyDer(ui8 what, ui32 val);//synchr
  295. void initObj();
  296. void onHeroVisit(const CGHeroInstance * h) const;
  297. };
  298. class DLL_EXPORT CGDwelling : public CArmedInstance
  299. {
  300. public:
  301. std::vector<std::pair<ui32, std::vector<ui32> > > creatures; //creatures[level] -> <vector of alternative ids (base creature and upgrades, creatures amount>
  302. template <typename Handler> void serialize(Handler &h, const int version)
  303. {
  304. h & static_cast<CArmedInstance&>(*this) & creatures;
  305. }
  306. void initObj();
  307. void setProperty(ui8 what, ui32 val);
  308. void onHeroVisit(const CGHeroInstance * h) const;
  309. void newTurn() const;
  310. void heroAcceptsCreatures(const CGHeroInstance *h, ui32 answer) const;
  311. void fightOver(const CGHeroInstance *h, BattleResult *result) const;
  312. void wantsFight(const CGHeroInstance *h, ui32 answer) const;
  313. };
  314. class DLL_EXPORT CGVisitableOPH : public CGObjectInstance //objects visitable only once per hero
  315. {
  316. public:
  317. std::set<si32> visitors; //ids of heroes who have visited this obj
  318. si8 ttype; //tree type - used only by trees of knowledge: 0 - give level for free; 1 - take 2000 gold; 2 - take 10 gems
  319. const std::string & getHoverText() const;
  320. void setPropertyDer(ui8 what, ui32 val);//synchr
  321. void onHeroVisit(const CGHeroInstance * h) const;
  322. void onNAHeroVisit(int heroID, bool alreadyVisited) const;
  323. void initObj();
  324. void treeSelected(int heroID, int resType, int resVal, ui64 expVal, ui32 result) const; //handle player's anwer to the Tree of Knowledge dialog
  325. void schoolSelected(int heroID, ui32 which) const;
  326. void arenaSelected(int heroID, int primSkill) const;
  327. template <typename Handler> void serialize(Handler &h, const int version)
  328. {
  329. h & static_cast<CGObjectInstance&>(*this);
  330. h & visitors & ttype;
  331. }
  332. };
  333. class DLL_EXPORT CGTownBuilding : public IObjectInterface
  334. {
  335. ///basic class for town structures handled as map objects
  336. public:
  337. si32 ID; //from buildig list
  338. si32 id; //identifies its index on towns vector
  339. CGTownInstance *town;
  340. template <typename Handler> void serialize(Handler &h, const int version)
  341. {
  342. h & ID & id;
  343. }
  344. };
  345. class DLL_EXPORT COPWBonus : public CGTownBuilding
  346. {///used for OPW bonusing structures
  347. public:
  348. std::set<si32> visitors;
  349. void setProperty(ui8 what, ui32 val);
  350. void onHeroVisit (const CGHeroInstance * h) const;
  351. COPWBonus (int index, CGTownInstance *TOWN);
  352. COPWBonus (){ID = 0; town = NULL;};
  353. template <typename Handler> void serialize(Handler &h, const int version)
  354. {
  355. h & static_cast<CGTownBuilding&>(*this);
  356. h & visitors;
  357. }
  358. };
  359. class DLL_EXPORT CTownBonus : public CGTownBuilding
  360. {
  361. ///used for one-time bonusing structures
  362. ///feel free to merge inheritance tree
  363. public:
  364. std::set<si32> visitors;
  365. void setProperty(ui8 what, ui32 val);
  366. void onHeroVisit (const CGHeroInstance * h) const;
  367. CTownBonus (int index, CGTownInstance *TOWN);
  368. CTownBonus (){ID = 0; town = NULL;};
  369. template <typename Handler> void serialize(Handler &h, const int version)
  370. {
  371. h & static_cast<CGTownBuilding&>(*this);
  372. h & visitors;
  373. }
  374. };
  375. class DLL_EXPORT CGTownInstance : public CGDwelling, public IShipyard
  376. {
  377. public:
  378. CTown * town;
  379. std::string name; // name of town
  380. si32 builded; //how many buildings has been built this turn
  381. si32 destroyed; //how many buildings has been destroyed this turn
  382. const CGHeroInstance * garrisonHero, *visitingHero;
  383. ui32 identifier; //special identifier from h3m (only > RoE maps)
  384. si32 alignment;
  385. std::set<si32> forbiddenBuildings, builtBuildings;
  386. std::vector<CGTownBuilding*> bonusingBuildings;
  387. std::vector<ui32> possibleSpells, obligatorySpells;
  388. std::vector<std::vector<ui32> > spells; //spells[level] -> vector of spells, first will be available in guild
  389. std::set<CCastleEvent> events;
  390. std::pair<si32, si32> bonusValue;//var to store town bonuses (rampart = resources from mystic pond);
  391. //////////////////////////////////////////////////////////////////////////
  392. template <typename Handler> void serialize(Handler &h, const int version)
  393. {
  394. h & static_cast<CGDwelling&>(*this);
  395. h & name & builded & destroyed & identifier & alignment & forbiddenBuildings & builtBuildings & bonusValue
  396. & possibleSpells & obligatorySpells & spells & /*strInfo & */events & bonusingBuildings;
  397. for (std::vector<CGTownBuilding*>::iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  398. (*i)->town = this;
  399. ui8 standardType = (&VLC->townh->towns[subID] == town);
  400. h & standardType;
  401. if(!standardType)
  402. h & town;
  403. else if(!h.saving)
  404. town = &VLC->townh->towns[subID];
  405. //garrison/visiting hero pointers will be restored in the map serialization
  406. }
  407. //////////////////////////////////////////////////////////////////////////
  408. 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
  409. int3 getSightCenter() const; //"center" tile from which the sight distance is calculated
  410. int getSightRadious() const; //returns sight distance
  411. void getOutOffsets(std::vector<int3> &offsets) const; //offsets to obj pos when we boat can be placed
  412. void setPropertyDer(ui8 what, ui32 val);
  413. void newTurn() const;
  414. //////////////////////////////////////////////////////////////////////////
  415. bool needsLastStack() const;
  416. int fortLevel() const; //0 - none, 1 - fort, 2 - citadel, 3 - castle
  417. int hallLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  418. int mageGuildLevel() const; // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  419. bool creatureDwelling(const int & level, bool upgraded=false) const;
  420. int getHordeLevel(const int & HID) const; //HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  421. int creatureGrowth(const int & level) const;
  422. bool hasFort() const;
  423. bool hasCapitol() const;
  424. int dailyIncome() const; //calculates daily income of this town
  425. int spellsAtLevel(int level, bool checkGuild) const; //levels are counted from 1 (1 - 5)
  426. void removeCapitols (ui8 owner, bool me) const;
  427. int defenceBonus(int type) const;//bonuses for defending hero, 0..3 - primary skills, 4 = morale, 5 = luck
  428. CGTownInstance();
  429. virtual ~CGTownInstance();
  430. //////////////////////////////////////////////////////////////////////////
  431. void fightOver(const CGHeroInstance *h, BattleResult *result) const;
  432. void onHeroVisit(const CGHeroInstance * h) const;
  433. void onHeroLeave(const CGHeroInstance * h) const;
  434. void initObj();
  435. };
  436. class DLL_EXPORT CGPandoraBox : public CArmedInstance
  437. {
  438. public:
  439. std::string message;
  440. ui8 removeAfterVisit; //true if event is removed after occurring
  441. //gained things:
  442. ui32 gainedExp;
  443. si32 manaDiff; //amount of gained / lost mana
  444. si32 moraleDiff; //morale modifier
  445. si32 luckDiff; //luck modifier
  446. std::vector<si32> resources;//gained / lost resources
  447. std::vector<si32> primskills;//gained / lost resources
  448. std::vector<si32> abilities; //gained abilities
  449. std::vector<si32> abilityLevels; //levels of gained abilities
  450. std::vector<si32> artifacts; //gained artifacts
  451. std::vector<si32> spells; //gained spells
  452. CCreatureSet creatures; //gained creatures
  453. void initObj();
  454. void onHeroVisit(const CGHeroInstance * h) const;
  455. void open (const CGHeroInstance * h, ui32 accept) const;
  456. void endBattle(const CGHeroInstance *h, BattleResult *result) const;
  457. void giveContents(const CGHeroInstance *h, bool afterBattle) const;
  458. void getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const;
  459. void getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const;
  460. template <typename Handler> void serialize(Handler &h, const int version)
  461. {
  462. h & static_cast<CArmedInstance&>(*this);
  463. h & message & gainedExp & manaDiff & moraleDiff & luckDiff & resources & primskills
  464. & abilities & abilityLevels & artifacts & spells & creatures & army;
  465. }
  466. };
  467. class DLL_EXPORT CGEvent : public CGPandoraBox //event objects
  468. {
  469. public:
  470. ui8 availableFor; //players whom this event is available for
  471. ui8 computerActivate; //true if computre player can activate this event
  472. ui8 humanActivate; //true if human player can activate this event
  473. template <typename Handler> void serialize(Handler &h, const int version)
  474. {
  475. h & static_cast<CArmedInstance&>(*this);
  476. h & message & gainedExp & manaDiff & moraleDiff & luckDiff & resources & primskills
  477. & abilities & abilityLevels & artifacts & spells & creatures & availableFor
  478. & computerActivate & humanActivate & army;
  479. }
  480. void onHeroVisit(const CGHeroInstance * h) const;
  481. void activated(const CGHeroInstance * h) const;
  482. };
  483. class DLL_EXPORT CGCreature : public CArmedInstance //creatures on map
  484. {
  485. public:
  486. ui32 identifier; //unique code for this monster (used in missions)
  487. si8 character; //chracter of this set of creatures (0 - the most friendly, 4 - the most hostile) => on init changed to 0 (compliant) - 10 value (savage)
  488. std::string message; //message printed for attacking hero
  489. std::vector<ui32> resources; //[res_id], resources given to hero that has won with monsters
  490. si32 gainedArtifact; //ID of artifact gained to hero, -1 if none
  491. ui8 neverFlees; //if true, the troops will never flee
  492. ui8 notGrowingTeam; //if true, number of units won't grow
  493. void fight(const CGHeroInstance *h) const;
  494. void onHeroVisit(const CGHeroInstance * h) const;
  495. //const std::string & getHoverText() const;
  496. void flee( const CGHeroInstance * h ) const;
  497. void endBattle(BattleResult *result) const;
  498. void fleeDecision(const CGHeroInstance *h, ui32 pursue) const;
  499. void joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const;
  500. void initObj();
  501. 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)
  502. template <typename Handler> void serialize(Handler &h, const int version)
  503. {
  504. h & static_cast<CArmedInstance&>(*this);
  505. h & identifier & character & message & resources & gainedArtifact & neverFlees & notGrowingTeam;
  506. }
  507. };
  508. class DLL_EXPORT CGSignBottle : public CGObjectInstance //signs and ocean bottles
  509. {
  510. public:
  511. std::string message;
  512. void onHeroVisit(const CGHeroInstance * h) const;
  513. void initObj();
  514. template <typename Handler> void serialize(Handler &h, const int version)
  515. {
  516. h & static_cast<CGObjectInstance&>(*this);
  517. h & message;
  518. }
  519. };
  520. class DLL_EXPORT CGSeerHut : public CGObjectInstance, public CQuest
  521. {
  522. public:
  523. ui8 rewardType; //type of reward: 0 - no reward; 1 - experience; 2 - mana points; 3 - morale bonus; 4 - luck bonus; 5 - resources; 6 - main ability bonus (attak, defence etd.); 7 - secondary ability gain; 8 - artifact; 9 - spell; 10 - creature
  524. si32 rID; //reward ID
  525. si32 rVal; //reward value
  526. ui8 textOption; //store randomized mission write-ups rather than entire string (?)
  527. std::string seerName;
  528. void initObj();
  529. const std::string & getHoverText() const;
  530. void setPropertyDer (ui8 what, ui32 val);
  531. void newTurn() const;
  532. void onHeroVisit (const CGHeroInstance * h) const;
  533. void finishQuest (const CGHeroInstance * h, ui32 accept) const; //common for both objects
  534. void completeQuest (const CGHeroInstance * h) const;
  535. template <typename Handler> void serialize(Handler &h, const int version)
  536. {
  537. h & static_cast<CGObjectInstance&>(*this) & static_cast<CQuest&>(*this);
  538. h & rewardType & rID & rVal & textOption & seerName;
  539. }
  540. };
  541. class DLL_EXPORT CGQuestGuard : public CGSeerHut
  542. {
  543. public:
  544. void initObj();
  545. const std::string & getHoverText() const;
  546. void onHeroVisit( const CGHeroInstance * h ) const;
  547. void completeQuest (const CGHeroInstance * h) const;
  548. void openGate (const CGHeroInstance *h, ui32 accept) const;
  549. template <typename Handler> void serialize(Handler &h, const int version)
  550. {
  551. h & static_cast<CGSeerHut&>(*this);
  552. }
  553. };
  554. class DLL_EXPORT CGWitchHut : public CPlayersVisited
  555. {
  556. public:
  557. std::vector<si32> allowedAbilities;
  558. ui32 ability;
  559. const std::string & getHoverText() const;
  560. void onHeroVisit(const CGHeroInstance * h) const;
  561. void initObj();
  562. template <typename Handler> void serialize(Handler &h, const int version)
  563. {
  564. h & static_cast<CPlayersVisited&>(*this);
  565. h & allowedAbilities & ability;
  566. }
  567. };
  568. class DLL_EXPORT CGScholar : public CGObjectInstance
  569. {
  570. public:
  571. ui8 bonusType; //255 - random, 0 - primary skill, 1 - secondary skill, 2 - spell
  572. ui16 bonusID; //ID of skill/spell
  573. void giveAnyBonus(const CGHeroInstance * h) const;
  574. void onHeroVisit(const CGHeroInstance * h) const;
  575. void initObj();
  576. template <typename Handler> void serialize(Handler &h, const int version)
  577. {
  578. h & static_cast<CGObjectInstance&>(*this);
  579. h & bonusType & bonusID;
  580. }
  581. };
  582. class DLL_EXPORT CGGarrison : public CArmedInstance
  583. {
  584. public:
  585. ui8 removableUnits;
  586. ui8 getPassableness() const;
  587. void onHeroVisit (const CGHeroInstance *h) const;
  588. void fightOver (const CGHeroInstance *h, BattleResult *result) const;
  589. template <typename Handler> void serialize(Handler &h, const int version)
  590. {
  591. h & static_cast<CArmedInstance&>(*this);
  592. h & removableUnits;
  593. }
  594. };
  595. class DLL_EXPORT CGArtifact : public CArmedInstance
  596. {
  597. public:
  598. std::string message;
  599. ui32 spell; //if it's spell scroll
  600. void onHeroVisit(const CGHeroInstance * h) const;
  601. void fightForArt(ui32 agreed, const CGHeroInstance *h) const;
  602. void endBattle(BattleResult *result, const CGHeroInstance *h) const;
  603. void pick( const CGHeroInstance * h ) const;
  604. void initObj();
  605. template <typename Handler> void serialize(Handler &h, const int version)
  606. {
  607. h & static_cast<CArmedInstance&>(*this);
  608. h & message & spell;
  609. }
  610. };
  611. class DLL_EXPORT CGResource : public CArmedInstance
  612. {
  613. public:
  614. ui32 amount; //0 if random
  615. std::string message;
  616. void onHeroVisit(const CGHeroInstance * h) const;
  617. void collectRes(int player) const;
  618. void initObj();
  619. void fightForRes(ui32 agreed, const CGHeroInstance *h) const;
  620. void endBattle(BattleResult *result, const CGHeroInstance *h) const;
  621. template <typename Handler> void serialize(Handler &h, const int version)
  622. {
  623. h & static_cast<CArmedInstance&>(*this);
  624. h & amount & message;
  625. }
  626. };
  627. class DLL_EXPORT CGPickable : public CGObjectInstance //campfire, treasure chest, Flotsam, Shipwreck Survivor, Sea Chest
  628. {
  629. public:
  630. ui32 type, val1, val2;
  631. void onHeroVisit(const CGHeroInstance * h) const;
  632. void initObj();
  633. void chosen(int which, int heroID) const;
  634. template <typename Handler> void serialize(Handler &h, const int version)
  635. {
  636. h & static_cast<CGObjectInstance&>(*this);
  637. h & type & val1 & val2;
  638. }
  639. };
  640. class DLL_EXPORT CGShrine : public CPlayersVisited
  641. {
  642. public:
  643. ui8 spell; //number of spell or 255 if random
  644. void onHeroVisit(const CGHeroInstance * h) const;
  645. void initObj();
  646. const std::string & getHoverText() const;
  647. template <typename Handler> void serialize(Handler &h, const int version)
  648. {
  649. h & static_cast<CPlayersVisited&>(*this);;
  650. h & spell;
  651. }
  652. };
  653. class DLL_EXPORT CGMine : public CArmedInstance
  654. {
  655. public:
  656. void offerLeavingGuards(const CGHeroInstance *h) const;
  657. void onHeroVisit(const CGHeroInstance * h) const;
  658. void newTurn() const;
  659. void initObj();
  660. template <typename Handler> void serialize(Handler &h, const int version)
  661. {
  662. h & static_cast<CArmedInstance&>(*this);
  663. }
  664. };
  665. class DLL_EXPORT CGVisitableOPW : public CGObjectInstance //objects visitable OPW
  666. {
  667. public:
  668. ui8 visited; //true if object has been visited this week
  669. void setPropertyDer(ui8 what, ui32 val);//synchr
  670. void onHeroVisit(const CGHeroInstance * h) const;
  671. void newTurn() const;
  672. template <typename Handler> void serialize(Handler &h, const int version)
  673. {
  674. h & static_cast<CGObjectInstance&>(*this);
  675. h & visited;
  676. }
  677. };
  678. class DLL_EXPORT CGTeleport : public CGObjectInstance //teleports and subterranean gates
  679. {
  680. public:
  681. static std::map<int,std::map<int, std::vector<int> > > objs; //teleports: map[ID][subID] => vector of ids
  682. static std::vector<std::pair<int, int> > gates; //subterranean gates: pairs of ids
  683. void onHeroVisit(const CGHeroInstance * h) const;
  684. void initObj();
  685. static void postInit();
  686. template <typename Handler> void serialize(Handler &h, const int version)
  687. {
  688. h & static_cast<CGObjectInstance&>(*this);
  689. }
  690. };
  691. class DLL_EXPORT CGBonusingObject : public CGObjectInstance //objects giving bonuses to luck/morale/movement
  692. {
  693. public:
  694. void onHeroVisit(const CGHeroInstance * h) const;
  695. const std::string & getHoverText() const;
  696. void initObj();
  697. template <typename Handler> void serialize(Handler &h, const int version)
  698. {
  699. h & static_cast<CGObjectInstance&>(*this);
  700. }
  701. };
  702. class DLL_EXPORT CGMagicSpring : public CGVisitableOPW
  703. {///unfortunatelly, this one is quite different than others
  704. public:
  705. void onHeroVisit(const CGHeroInstance * h) const;
  706. const std::string & getHoverText() const;
  707. template <typename Handler> void serialize(Handler &h, const int version)
  708. {
  709. h & static_cast<CGObjectInstance&>(*this);
  710. h & visited;
  711. }
  712. };
  713. class DLL_EXPORT CGMagicWell : public CGObjectInstance //objects giving bonuses to luck/morale/movement
  714. {
  715. public:
  716. void onHeroVisit(const CGHeroInstance * h) const;
  717. const std::string & getHoverText() const;
  718. template <typename Handler> void serialize(Handler &h, const int version)
  719. {
  720. h & static_cast<CGObjectInstance&>(*this);
  721. }
  722. };
  723. class DLL_EXPORT CGSirens : public CGObjectInstance
  724. {
  725. public:
  726. void onHeroVisit(const CGHeroInstance * h) const;
  727. const std::string & getHoverText() const;
  728. void initObj();
  729. template <typename Handler> void serialize(Handler &h, const int version)
  730. {
  731. h & static_cast<CGObjectInstance&>(*this);
  732. }
  733. };
  734. class DLL_EXPORT CGObservatory : public CGObjectInstance //Redwood observatory
  735. {
  736. public:
  737. void onHeroVisit(const CGHeroInstance * h) const;
  738. template <typename Handler> void serialize(Handler &h, const int version)
  739. {
  740. h & static_cast<CGObjectInstance&>(*this);
  741. }
  742. };
  743. class DLL_EXPORT CGKeys : public CGObjectInstance //Base class for Keymaster and guards
  744. {
  745. public:
  746. static std::map <ui8, std::set <ui8> > playerKeyMap; //[players][keysowned]
  747. //SubID 0 - lightblue, 1 - green, 2 - red, 3 - darkblue, 4 - brown, 5 - purple, 6 - white, 7 - black
  748. void setPropertyDer (ui8 what, ui32 val);
  749. bool wasMyColorVisited (int player) const;
  750. template <typename Handler> void serialize(Handler &h, const int version)
  751. {
  752. h & static_cast<CGObjectInstance&>(*this);
  753. }
  754. };
  755. class DLL_EXPORT CGKeymasterTent : public CGKeys
  756. {
  757. public:
  758. void onHeroVisit(const CGHeroInstance * h) const;
  759. const std::string & getHoverText() const;
  760. template <typename Handler> void serialize(Handler &h, const int version)
  761. {
  762. h & static_cast<CGObjectInstance&>(*this);
  763. }
  764. };
  765. class DLL_EXPORT CGBorderGuard : public CGKeys
  766. {
  767. public:
  768. void initObj();
  769. const std::string & getHoverText() const;
  770. void onHeroVisit(const CGHeroInstance * h) const;
  771. void openGate(const CGHeroInstance *h, ui32 accept) const;
  772. template <typename Handler> void serialize(Handler &h, const int version)
  773. {
  774. h & static_cast<CGObjectInstance&>(*this);
  775. h & blockVisit;
  776. }
  777. };
  778. class DLL_EXPORT CGBorderGate : public CGBorderGuard //not fully imlemented, waiting for garrison
  779. {
  780. public:
  781. void onHeroVisit(const CGHeroInstance * h) const;
  782. ui8 getPassableness() const;
  783. };
  784. class DLL_EXPORT CGBoat : public CGObjectInstance
  785. {
  786. public:
  787. ui8 direction;
  788. const CGHeroInstance *hero; //hero on board
  789. void initObj();
  790. CGBoat()
  791. {
  792. hero = NULL;
  793. direction = 4;
  794. }
  795. template <typename Handler> void serialize(Handler &h, const int version)
  796. {
  797. h & static_cast<CGObjectInstance&>(*this) & direction;
  798. }
  799. };
  800. class DLL_EXPORT CGOnceVisitable : public CPlayersVisited
  801. ///wagon, corpse, lean to, warriors tomb
  802. {
  803. public:
  804. ui8 artOrRes; //0 - nothing; 1 - artifact; 2 - resource
  805. ui32 bonusType, //id of res or artifact
  806. bonusVal; //resource amount (or not used)
  807. void onHeroVisit(const CGHeroInstance * h) const;
  808. const std::string & getHoverText() const;
  809. void initObj();
  810. void searchTomb(const CGHeroInstance *h, ui32 accept) const;
  811. template <typename Handler> void serialize(Handler &h, const int version)
  812. {
  813. h & static_cast<CPlayersVisited&>(*this);;
  814. h & artOrRes & bonusType & bonusVal;
  815. }
  816. };
  817. class DLL_EXPORT CBank : public CArmedInstance
  818. {
  819. public:
  820. int index; //banks have unusal numbering - see ZCRBANK.txt and initObj()
  821. BankConfig *bc;
  822. float multiplier; //for improved banks script
  823. std::vector<ui32> artifacts; //fixed and deterministic
  824. ui32 daycounter;
  825. void initObj();
  826. const std::string & getHoverText() const;
  827. void setPropertyDer (ui8 what, ui32 val);
  828. void initialize() const;
  829. void reset(ui16 var1);
  830. void newTurn() const;
  831. virtual void onHeroVisit (const CGHeroInstance * h) const;
  832. virtual void fightGuards (const CGHeroInstance *h, ui32 accept) const;
  833. virtual void endBattle (const CGHeroInstance *h, const BattleResult *result) const;
  834. template <typename Handler> void serialize(Handler &h, const int version)
  835. {
  836. h & static_cast<CGObjectInstance&>(*this);
  837. h & index & multiplier & artifacts & daycounter & bc;
  838. }
  839. };
  840. class DLL_EXPORT CGPyramid : public CBank
  841. {
  842. public:
  843. static BankConfig pyramidConfig;
  844. ui16 spell;
  845. void initObj();
  846. const std::string & getHoverText() const;
  847. void newTurn() const {}; //empty, no reset
  848. void onHeroVisit (const CGHeroInstance * h) const;
  849. void endBattle (const CGHeroInstance *h, const BattleResult *result) const;
  850. template <typename Handler> void serialize(Handler &h, const int version)
  851. {
  852. h & static_cast<CBank&>(*this);
  853. h & spell;
  854. }
  855. };
  856. class CGShipyard : public CGObjectInstance, public IShipyard
  857. {
  858. public:
  859. void getOutOffsets(std::vector<int3> &offsets) const; //offsets to obj pos when we boat can be placed
  860. CGShipyard();
  861. void onHeroVisit(const CGHeroInstance * h) const;
  862. };
  863. class DLL_EXPORT CGMagi : public CGObjectInstance
  864. {
  865. public:
  866. static std::map <si32, std::vector<si32> > eyelist; //[subID][id], supports multiple sets as in H5
  867. void initObj();
  868. void onHeroVisit(const CGHeroInstance * h) const;
  869. template <typename Handler> void serialize(Handler &h, const int version)
  870. {
  871. h & static_cast<CGObjectInstance&>(*this);
  872. }
  873. };
  874. class DLL_EXPORT CCartographer : public CPlayersVisited
  875. {
  876. ///behaviour varies depending on surface and floor
  877. public:
  878. void onHeroVisit( const CGHeroInstance * h ) const;
  879. void buyMap (const CGHeroInstance *h, ui32 accept) const;
  880. template <typename Handler> void serialize(Handler &h, const int version)
  881. {
  882. h & static_cast<CPlayersVisited&>(*this);
  883. h & players;
  884. }
  885. };
  886. class DLL_EXPORT CShop : public CGObjectInstance
  887. {
  888. ///base class for university, art merchant, slave market etc.
  889. public:
  890. std::map<ui16, Component*> available;
  891. std::map<ui16, Component*> chosen, bought; //redundant?
  892. //keys are unique for all three maps
  893. std::map<ui16, ui32> price;
  894. void initObj() {};
  895. void setPropertyDer (ui8 what, ui32 val);
  896. void newTurn() const;
  897. virtual void reset (ui32 val) {}; //get new items for Black Market, Tavern, Refugee Camp
  898. virtual void onHeroVisit (const CGHeroInstance * h) const {};
  899. virtual void trade (const CGHeroInstance * h) const {};
  900. template <typename Handler> void serialize(Handler &h, const int version)
  901. {
  902. h & available & chosen & bought & price;
  903. }
  904. };
  905. class DLL_EXPORT CGArtMerchant : public CShop
  906. {
  907. public:
  908. void reset (ui32 val);
  909. void onHeroVisit (const CGHeroInstance * h) const {};
  910. };
  911. class DLL_EXPORT CGRefugeeCamp : public CShop
  912. {
  913. public:
  914. void reset (ui32 val);
  915. void onHeroVisit (const CGHeroInstance * h) const {};
  916. };
  917. struct BankConfig
  918. {
  919. BankConfig() {level = chance = upgradeChance = combatValue = value = rewardDifficulty = easiest = 0; };
  920. ui8 level; //1 - 4, how hard the battle will be
  921. ui8 chance; //chance for this level being chosen
  922. ui8 upgradeChance; //chance for creatures to be in upgraded versions
  923. std::vector< std::pair <ui16, ui32> > guards; //creature ID, amount
  924. ui32 combatValue; //how hard are guards of this level
  925. std::vector<si32> resources; //resources given in case of victory
  926. std::vector< std::pair <ui16, ui32> > creatures; //creatures granted in case of victory (creature ID, amount)
  927. std::vector<ui16> artifacts; //number of artifacts given in case of victory [0] -> treasure, [1] -> minor [2] -> major [3] -> relic
  928. ui32 value; //overall value of given things
  929. ui32 rewardDifficulty; //proportion of reward value to difficulty of guards; how profitable is this creature Bank config
  930. ui16 easiest; //?!?
  931. template <typename Handler> void serialize(Handler &h, const int version)
  932. {
  933. h & level & chance & upgradeChance & guards & combatValue & resources & creatures & artifacts & value & rewardDifficulty & easiest;
  934. }
  935. };
  936. class DLL_EXPORT CObjectHandler
  937. {
  938. public:
  939. std::vector<si32> cregens; //type 17. dwelling subid -> creature ID
  940. std::map <ui32, std::vector <BankConfig*> > banksInfo; //[index][preset]
  941. std::map <ui32, std::string> creBanksNames; //[crebank index] -> name of this creature bank
  942. void loadObjects();
  943. template <typename Handler> void serialize(Handler &h, const int version)
  944. {
  945. h & cregens & banksInfo & creBanksNames;
  946. }
  947. };
  948. #endif // __COBJECTHANDLER_H__