global.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #ifndef __GLOBAL_H__
  2. #define __GLOBAL_H__
  3. #include <iostream>
  4. #include <algorithm> //std::find
  5. #include <boost/logic/tribool.hpp>
  6. #include <boost/cstdint.hpp>
  7. typedef boost::uint64_t ui64; //unsigned int 64 bits (8 bytes)
  8. typedef boost::uint32_t ui32; //unsigned int 32 bits (4 bytes)
  9. typedef boost::uint16_t ui16; //unsigned int 16 bits (2 bytes)
  10. typedef boost::uint8_t ui8; //unsigned int 8 bits (1 byte)
  11. typedef boost::int64_t si64; //signed int 64 bits (8 bytes)
  12. typedef boost::int32_t si32; //signed int 32 bits (4 bytes)
  13. typedef boost::int16_t si16; //signed int 16 bits (2 bytes)
  14. typedef boost::int8_t si8; //signed int 8 bits (1 byte)
  15. #include "int3.h"
  16. #define CHECKTIME 1
  17. #if CHECKTIME
  18. #include "timeHandler.h"
  19. #define THC
  20. #endif
  21. #define NAME_VER ("VCMI 0.71b")
  22. #define CONSOLE_LOGGING_LEVEL 5
  23. #define FILE_LOGGING_LEVEL 6
  24. #ifdef _WIN32
  25. #define PATHSEPARATOR "\\"
  26. #define DATA_DIR ""
  27. #define SERVER_NAME "VCMI_server.exe"
  28. #else
  29. #define PATHSEPARATOR "/"
  30. #define DATA_DIR ""
  31. #define SERVER_NAME "./vcmiserver"
  32. #endif
  33. /*
  34. * global.h, part of VCMI engine
  35. *
  36. * Authors: listed in file AUTHORS in main folder
  37. *
  38. * License: GNU General Public License v2.0 or later
  39. * Full text of license available in license.txt file, in main folder
  40. *
  41. */
  42. enum Ecolor {RED, BLUE, TAN, GREEN, ORANGE, PURPLE, TEAL, PINK}; //player's colors
  43. enum EvictoryConditions {artifact, gatherTroop, gatherResource, buildCity, buildGrail, beatHero,
  44. captureCity, beatMonster, takeDwellings, takeMines, transportItem, winStandard=255};
  45. enum ElossCon {lossCastle, lossHero, timeExpires, lossStandard=255};
  46. //enum EAbilities {DOUBLE_WIDE, FLYING, SHOOTER, TWO_HEX_ATTACK, SIEGE_ABILITY, SIEGE_WEAPON,
  47. // KING1, KING2, KING3, MIND_IMMUNITY, NO_OBSTACLE_PENALTY, NO_CLOSE_COMBAT_PENALTY,
  48. // JOUSTING, FIRE_IMMUNITY, TWICE_ATTACK, NO_ENEMY_RETALIATION, NO_MORAL_PENALTY,
  49. // UNDEAD, MULTI_HEAD_ATTACK, EXTENDED_RADIOUS_SHOOTER, GHOST, RAISES_MORALE,
  50. // LOWERS_MORALE, DRAGON, STRIKE_AND_RETURN, FEARLESS, REBIRTH, NOT_ACTIVE}; //some flags are used only for battles
  51. enum ECombatInfo{ALIVE = 180, SUMMONED, CLONED, HAD_MORALE, WAITING, MOVED, DEFENDING};
  52. struct StackFeature
  53. {
  54. //general list of stack abilities and effects
  55. enum ECombatFeatures
  56. {
  57. NO_TYPE,
  58. DOUBLE_WIDE, FLYING, SHOOTER, CHARGE_IMMUNITY, ADDITIONAL_ATTACK, UNLIMITED_RETAILATIONS,
  59. NO_MELEE_PENALTY, JOUSTING /*for champions*/,
  60. RAISING_MORALE, HATE /*eg. angels hate devils*/,
  61. KING1,
  62. KING2, KING3, MAGIC_RESISTANCE /*in %*/,
  63. CHANGES_SPELL_COST /*in mana points, eg. pegasus or mage*/,
  64. SPELL_AFTER_ATTACK /* subtype - spell id, value - spell level, aditional info - chance in %; eg. dendroids*/,
  65. SPELL_RESISTANCE_AURA /*eg. unicorns, value - resistance bonus in % for adjacent creatures*/,
  66. LEVEL_SPELL_IMMUNITY /*creature is immune to all spell with level below value of this bonus*/,
  67. TWO_HEX_ATTACK_BREATH /*eg. dragons*/,
  68. SPELL_DAMAGE_REDUCTION /*eg. golems; value - reduction in %, subtype - spell school; -1 - all, 0 - air, 1 - fire, 2 - water, 3 - earth*/,
  69. NO_WALL_PENALTY, NON_LIVING /*eg. gargoyle*/,
  70. RANDOM_SPELLCASTER, BLOCKS_RETAILATION /*eg. naga*/,
  71. SPELL_IMMUNITY /*value - spell id*/,
  72. MANA_CHANNELING /*in %, eg. familiar*/,
  73. SPELL_LIKE_ATTACK /*value - spell id; range is taken from spell, but damage from creature; eg. magog*/,
  74. THREE_HEADED_ATTACK /*eg. cerberus*/,
  75. DEAMON_SUMMONING /*pit lord*/,
  76. FIRE_IMMUNITY, FIRE_SHIELD, ENEMY_MORALE_DECREASING, ENEMY_LUCK_DECREASING, UNDEAD, REGENERATION, MANA_DRAIN, LIFE_DRAIN,
  77. DOUBLE_DAMAGE_CHANCE /*in %, eg. dread knight*/,
  78. RETURN_AFTER_STRIKE, SELF_MORALE /*eg. minotaur*/,
  79. SPELLCASTER /*value - spell id*/, CATAPULT,
  80. ENEMY_DEFENCE_REDUCTION /*in %, eg. behemots*/,
  81. GENERAL_DAMAGE_REDUCTION /*eg. while stoned or blinded - in %, subtype: -1 - any damage, 0 - melee damage, 1 - ranged damage*/,
  82. ATTACKS_ALL_ADAJCENT /*eg. hydra*/,
  83. MORE_DAMEGE_FROM_SPELL /*value - damage increase in %, subtype - spell id*/,
  84. CASTS_SPELL_WHEN_KILLED /*similar to spell after attack*/,
  85. FEAR, FEARLESS, NO_DISTANCE_PENALTY, NO_OBSTACLES_PENALTY,
  86. SELF_LUCK /*halfling*/,
  87. ATTACK_BONUS, DEFENCE_BONUS, SPEED_BONUS, HP_BONUS, ENCHANTER, HEALER, SIEGE_WEAPON, LUCK_BONUS, MORALE_BONUS, HYPNOTIZED, ADDITIONAL_RETAILATION,
  88. MAGIC_MIRROR /* value - chance of redirecting in %*/,
  89. SUMMONED, ALWAYS_MINUMUM_DAMAGE /*unit does its minimum damage from range; -1 - any attack, 0 - melee, 1 - ranged*/,
  90. ALWAYS_MAXIMUM_DAMAGE /*eg. bless effect, -1 - any attack, 0 - melee, 1 - ranged*/,
  91. ATTACKS_NEAREST_CREATURE /*while in berserk*/, IN_FRENZY,
  92. SLAYER /*value - level*/,
  93. FORGETFULL /*forgetfullnes spell effect*/,
  94. CLONED, NOT_ACTIVE
  95. };
  96. enum EDuration
  97. {
  98. WHOLE_BATTLE,
  99. N_TURNS,
  100. UNITL_BEING_ATTACKED,/*removed after attack and counterattacks are performed*/
  101. UNTIL_ATTACK /*removed after attack and counterattacks are performed*/
  102. };
  103. ECombatFeatures type;
  104. EDuration duration;
  105. ui16 turnsRemain; //if duration is N_TURNS it describes how long the effect will last
  106. si16 subtype; //subtype of bonus/feature
  107. si32 value;
  108. si32 additionalInfo;
  109. inline bool operator == (const ECombatFeatures & cf) const
  110. {
  111. return type == cf;
  112. }
  113. StackFeature() : type(NO_TYPE)
  114. {}
  115. StackFeature(const ECombatFeatures & cf) : type(cf), duration(WHOLE_BATTLE)
  116. {}
  117. template <typename Handler> void serialize(Handler &h, const int version)
  118. {
  119. h & type & duration & turnsRemain & subtype & value & additionalInfo;
  120. }
  121. };
  122. //generates StackFeature from given data
  123. inline StackFeature makeFeature(StackFeature::ECombatFeatures type, StackFeature::EDuration duration, si16 subtype, si32 value, ui16 turnsRemain = 0, si32 additionalInfo = 0)
  124. {
  125. StackFeature sf;
  126. sf.type = type;
  127. sf.duration = duration;
  128. sf.turnsRemain = turnsRemain;
  129. sf.subtype = subtype;
  130. sf.value = value;
  131. sf.additionalInfo = additionalInfo;
  132. return sf;
  133. }
  134. class CGameInfo;
  135. extern CGameInfo* CGI;
  136. #define HEROI_TYPE (34)
  137. #define TOWNI_TYPE (98)
  138. const int F_NUMBER = 9; //factions (town types) quantity
  139. const int PLAYER_LIMIT = 8; //player limit per map
  140. const int HEROES_PER_TYPE=8; //amount of heroes of each type
  141. const int SKILL_QUANTITY=28;
  142. const int SKILL_PER_HERO=8;
  143. const int ARTIFACTS_QUANTITY=171;
  144. const int HEROES_QUANTITY=156;
  145. const int SPELLS_QUANTITY=70;
  146. const int RESOURCE_QUANTITY=8;
  147. const int TERRAIN_TYPES=10;
  148. const int PRIMARY_SKILLS=4;
  149. const int NEUTRAL_PLAYER=255;
  150. const int NAMES_PER_TOWN=16;
  151. const int CREATURES_PER_TOWN = 7; //without upgrades
  152. const int MAX_BUILDING_PER_TURN = 1;
  153. const int SPELL_LEVELS = 5;
  154. #define BFIELD_WIDTH (17)
  155. #define BFIELD_HEIGHT (11)
  156. #define BFIELD_SIZE ((BFIELD_WIDTH) * (BFIELD_HEIGHT))
  157. //uncomment to make it work
  158. //#define MARK_BLOCKED_POSITIONS
  159. //#define MARK_VISITABLE_POSITIONS
  160. #define DEFBYPASS
  161. #ifdef _WIN32
  162. #ifdef VCMI_DLL
  163. #define DLL_EXPORT __declspec(dllexport)
  164. #else
  165. #define DLL_EXPORT __declspec(dllimport)
  166. #endif
  167. #else
  168. #if defined(__GNUC__) && __GNUC__ >= 4
  169. #define DLL_EXPORT __attribute__ ((visibility("default")))
  170. #else
  171. #define DLL_EXPORT
  172. #endif
  173. #endif
  174. template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
  175. #define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
  176. namespace vstd
  177. {
  178. template <typename Container, typename Item>
  179. bool contains(const Container & c, const Item &i) //returns true if container c contains item i
  180. {
  181. return std::find(c.begin(),c.end(),i) != c.end();
  182. }
  183. template <typename V, typename Item, typename Item2>
  184. bool contains(const std::map<Item,V> & c, const Item2 &i) //returns true if map c contains item i
  185. {
  186. return c.find(i)!=c.end();
  187. }
  188. template <typename Container1, typename Container2>
  189. typename Container2::iterator findFirstNot(Container1 &c1, Container2 &c2)//returns first element of c2 not present in c1
  190. {
  191. typename Container2::iterator itr = c2.begin();
  192. while(itr != c2.end())
  193. if(!contains(c1,*itr))
  194. return itr;
  195. else
  196. ++itr;
  197. return c2.end();
  198. }
  199. template <typename Container, typename Item>
  200. typename Container::iterator find(const Container & c, const Item &i)
  201. {
  202. return std::find(c.begin(),c.end(),i);
  203. }
  204. template <typename T1, typename T2>
  205. int findPos(const std::vector<T1> & c, const T2 &s) //returns position of first element in vector c equal to s, if there is no such element, -1 is returned
  206. {
  207. for(size_t i=0; i < c.size(); ++i)
  208. if(c[i] == s)
  209. return i;
  210. return -1;
  211. }
  212. template <typename T1, typename T2, typename Func>
  213. int findPos(const std::vector<T1> & c, const T2 &s, const Func &f) //Func(T1,T2) must say if these elements matches
  214. {
  215. for(size_t i=0; i < c.size(); ++i)
  216. if(f(c[i],s))
  217. return i;
  218. return -1;
  219. }
  220. template <typename Container, typename Item>
  221. typename Container::iterator find(Container & c, const Item &i)
  222. {
  223. return std::find(c.begin(),c.end(),i);
  224. }
  225. template <typename Container, typename Item>
  226. bool operator-=(Container &c, const Item &i) //removes element i from container c, returns false if c does not contain i
  227. {
  228. typename Container::iterator itr = find(c,i);
  229. if(itr == c.end())
  230. return false;
  231. c.erase(itr);
  232. return true;
  233. }
  234. template <typename t1>
  235. void delObj(t1 *a1)
  236. {
  237. delete a1;
  238. }
  239. template <typename t1, typename t2>
  240. void assign(t1 &a1, const t2 &a2)
  241. {
  242. a1 = a2;
  243. }
  244. template <typename t1, typename t2>
  245. struct assigner
  246. {
  247. public:
  248. t1 &op1;
  249. t2 op2;
  250. assigner(t1 &a1, t2 a2)
  251. :op1(a1), op2(a2)
  252. {}
  253. void operator()()
  254. {
  255. op1 = op2;
  256. }
  257. };
  258. template <typename t1, typename t2>
  259. assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
  260. {
  261. return assigner<t1,t2>(a1,a2);
  262. }
  263. template <typename t1, typename t2, typename t3>
  264. bool equal(const t1 &a1, const t3 t1::* point, const t2 &a2)
  265. {
  266. return a1.*point == a2;
  267. }
  268. template <typename t1, typename t2>
  269. bool equal(const t1 &a1, const t2 &a2)
  270. {
  271. return a1 == a2;
  272. }
  273. }
  274. using vstd::operator-=;
  275. template <typename t1, typename t2>
  276. t1 & amax(t1 &a, const t2 &b) //assigns greater of (a, b) to a and returns maximum of (a, b)
  277. {
  278. if(a >= b)
  279. return a;
  280. else
  281. {
  282. a = b;
  283. return a;
  284. }
  285. }
  286. template <typename t1, typename t2>
  287. t1 & amin(t1 &a, const t2 &b) //assigns smaller of (a, b) to a and returns minimum of (a, b)
  288. {
  289. if(a <= b)
  290. return a;
  291. else
  292. {
  293. a = b;
  294. return a;
  295. }
  296. }
  297. #include "CConsoleHandler.h"
  298. extern DLL_EXPORT std::ostream *logfile;
  299. extern DLL_EXPORT CConsoleHandler *console;
  300. template <int lvl> class CLogger //logger, prints log info to console and saves in file
  301. {
  302. public:
  303. CLogger<lvl>& operator<<(std::ostream& (*fun)(std::ostream&))
  304. {
  305. if(lvl < CONSOLE_LOGGING_LEVEL)
  306. std::cout << fun;
  307. if((lvl < FILE_LOGGING_LEVEL) && logfile)
  308. *logfile << fun;
  309. return *this;
  310. }
  311. template<typename T>
  312. CLogger<lvl> & operator<<(const T & data)
  313. {
  314. if(lvl < CONSOLE_LOGGING_LEVEL)
  315. {
  316. if(console)
  317. {
  318. console->print(data,lvl);
  319. }
  320. else
  321. {
  322. std::cout << data << std::flush;
  323. }
  324. }
  325. if((lvl < FILE_LOGGING_LEVEL) && logfile)
  326. {
  327. *logfile << data << std::flush;
  328. }
  329. return *this;
  330. }
  331. };
  332. extern DLL_EXPORT CLogger<0> tlog0; //green - standard progress info
  333. extern DLL_EXPORT CLogger<1> tlog1; //red - big errors
  334. extern DLL_EXPORT CLogger<2> tlog2; //magenta - major warnings
  335. extern DLL_EXPORT CLogger<3> tlog3; //yellow - minor warnings
  336. extern DLL_EXPORT CLogger<4> tlog4; //white - detailed log info
  337. extern DLL_EXPORT CLogger<5> tlog5; //gray - minor log info
  338. //XXX pls dont - 'debug macros' are usually more trubble then its worth
  339. #define HANDLE_EXCEPTION \
  340. catch (const std::exception& e) { \
  341. tlog1 << e.what() << std::endl; \
  342. } \
  343. catch (const std::exception * e) \
  344. { \
  345. tlog1 << e->what()<< std::endl; \
  346. throw; \
  347. } \
  348. catch (const std::string& e) { \
  349. tlog1 << e << std::endl; \
  350. throw; \
  351. }
  352. #define HANDLE_EXCEPTIONC(COMMAND) \
  353. catch (const std::exception& e) { \
  354. COMMAND; \
  355. tlog1 << e.what() << std::endl; \
  356. throw; \
  357. } \
  358. catch (const std::exception * e) \
  359. { \
  360. COMMAND; \
  361. tlog1 << e->what()<< std::endl; \
  362. throw; \
  363. }
  364. #endif // __GLOBAL_H__