global.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 = NOT_ACTIVE+1, SUMMONED, CLONED, HAD_MORALE, WAITING, MOVED, DEFENDING};
  52. class CGameInfo;
  53. extern CGameInfo* CGI;
  54. #define HEROI_TYPE (34)
  55. #define TOWNI_TYPE (98)
  56. const int F_NUMBER = 9; //factions (town types) quantity
  57. const int PLAYER_LIMIT = 8; //player limit per map
  58. const int HEROES_PER_TYPE=8; //amount of heroes of each type
  59. const int SKILL_QUANTITY=28;
  60. const int SKILL_PER_HERO=8;
  61. const int ARTIFACTS_QUANTITY=171;
  62. const int HEROES_QUANTITY=156;
  63. const int SPELLS_QUANTITY=70;
  64. const int RESOURCE_QUANTITY=8;
  65. const int TERRAIN_TYPES=10;
  66. const int PRIMARY_SKILLS=4;
  67. const int NEUTRAL_PLAYER=255;
  68. const int NAMES_PER_TOWN=16;
  69. const int CREATURES_PER_TOWN = 7; //without upgrades
  70. const int MAX_BUILDING_PER_TURN = 1;
  71. const int SPELL_LEVELS = 5;
  72. #define BFIELD_WIDTH (17)
  73. #define BFIELD_HEIGHT (11)
  74. #define BFIELD_SIZE ((BFIELD_WIDTH) * (BFIELD_HEIGHT))
  75. //uncomment to make it work
  76. //#define MARK_BLOCKED_POSITIONS
  77. //#define MARK_VISITABLE_POSITIONS
  78. #define DEFBYPASS
  79. #ifdef _WIN32
  80. #ifdef VCMI_DLL
  81. #define DLL_EXPORT __declspec(dllexport)
  82. #else
  83. #define DLL_EXPORT __declspec(dllimport)
  84. #endif
  85. #else
  86. #if defined(__GNUC__) && __GNUC__ >= 4
  87. #define DLL_EXPORT __attribute__ ((visibility("default")))
  88. #else
  89. #define DLL_EXPORT
  90. #endif
  91. #endif
  92. template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
  93. #define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
  94. namespace vstd
  95. {
  96. template <typename Container, typename Item>
  97. bool contains(const Container & c, const Item &i) //returns true if container c contains item i
  98. {
  99. return std::find(c.begin(),c.end(),i) != c.end();
  100. }
  101. template <typename V, typename Item, typename Item2>
  102. bool contains(const std::map<Item,V> & c, const Item2 &i) //returns true if map c contains item i
  103. {
  104. return c.find(i)!=c.end();
  105. }
  106. template <typename Container1, typename Container2>
  107. typename Container2::iterator findFirstNot(Container1 &c1, Container2 &c2)//returns first element of c2 not present in c1
  108. {
  109. typename Container2::iterator itr = c2.begin();
  110. while(itr != c2.end())
  111. if(!contains(c1,*itr))
  112. return itr;
  113. else
  114. ++itr;
  115. return c2.end();
  116. }
  117. template <typename Container, typename Item>
  118. typename Container::iterator find(const Container & c, const Item &i)
  119. {
  120. return std::find(c.begin(),c.end(),i);
  121. }
  122. template <typename T1, typename T2>
  123. 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
  124. {
  125. for(size_t i=0; i < c.size(); ++i)
  126. if(c[i] == s)
  127. return i;
  128. return -1;
  129. }
  130. template <typename T1, typename T2, typename Func>
  131. int findPos(const std::vector<T1> & c, const T2 &s, const Func &f) //Func(T1,T2) must say if these elements matches
  132. {
  133. for(size_t i=0; i < c.size(); ++i)
  134. if(f(c[i],s))
  135. return i;
  136. return -1;
  137. }
  138. template <typename Container, typename Item>
  139. typename Container::iterator find(Container & c, const Item &i)
  140. {
  141. return std::find(c.begin(),c.end(),i);
  142. }
  143. template <typename Container, typename Item>
  144. bool operator-=(Container &c, const Item &i) //removes element i from container c, returns false if c does not contain i
  145. {
  146. typename Container::iterator itr = find(c,i);
  147. if(itr == c.end())
  148. return false;
  149. c.erase(itr);
  150. return true;
  151. }
  152. template <typename t1>
  153. void delObj(t1 *a1)
  154. {
  155. delete a1;
  156. }
  157. template <typename t1, typename t2>
  158. void assign(t1 &a1, const t2 &a2)
  159. {
  160. a1 = a2;
  161. }
  162. template <typename t1, typename t2>
  163. struct assigner
  164. {
  165. public:
  166. t1 &op1;
  167. t2 op2;
  168. assigner(t1 &a1, t2 a2)
  169. :op1(a1), op2(a2)
  170. {}
  171. void operator()()
  172. {
  173. op1 = op2;
  174. }
  175. };
  176. template <typename t1, typename t2>
  177. assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
  178. {
  179. return assigner<t1,t2>(a1,a2);
  180. }
  181. template <typename t1, typename t2, typename t3>
  182. bool equal(const t1 &a1, const t3 t1::* point, const t2 &a2)
  183. {
  184. return a1.*point == a2;
  185. }
  186. template <typename t1, typename t2>
  187. bool equal(const t1 &a1, const t2 &a2)
  188. {
  189. return a1 == a2;
  190. }
  191. }
  192. using vstd::operator-=;
  193. template <typename t1, typename t2>
  194. t1 & amax(t1 &a, const t2 &b) //assigns greater of (a, b) to a and returns maximum of (a, b)
  195. {
  196. if(a >= b)
  197. return a;
  198. else
  199. {
  200. a = b;
  201. return a;
  202. }
  203. }
  204. template <typename t1, typename t2>
  205. t1 & amin(t1 &a, const t2 &b) //assigns smaller of (a, b) to a and returns minimum of (a, b)
  206. {
  207. if(a <= b)
  208. return a;
  209. else
  210. {
  211. a = b;
  212. return a;
  213. }
  214. }
  215. #include "CConsoleHandler.h"
  216. extern DLL_EXPORT std::ostream *logfile;
  217. extern DLL_EXPORT CConsoleHandler *console;
  218. template <int lvl> class CLogger //logger, prints log info to console and saves in file
  219. {
  220. public:
  221. CLogger<lvl>& operator<<(std::ostream& (*fun)(std::ostream&))
  222. {
  223. if(lvl < CONSOLE_LOGGING_LEVEL)
  224. std::cout << fun;
  225. if((lvl < FILE_LOGGING_LEVEL) && logfile)
  226. *logfile << fun;
  227. return *this;
  228. }
  229. template<typename T>
  230. CLogger<lvl> & operator<<(const T & data)
  231. {
  232. if(lvl < CONSOLE_LOGGING_LEVEL)
  233. {
  234. if(console)
  235. {
  236. console->print(data,lvl);
  237. }
  238. else
  239. {
  240. std::cout << data << std::flush;
  241. }
  242. }
  243. if((lvl < FILE_LOGGING_LEVEL) && logfile)
  244. {
  245. *logfile << data << std::flush;
  246. }
  247. return *this;
  248. }
  249. };
  250. extern DLL_EXPORT CLogger<0> tlog0; //green - standard progress info
  251. extern DLL_EXPORT CLogger<1> tlog1; //red - big errors
  252. extern DLL_EXPORT CLogger<2> tlog2; //magenta - major warnings
  253. extern DLL_EXPORT CLogger<3> tlog3; //yellow - minor warnings
  254. extern DLL_EXPORT CLogger<4> tlog4; //white - detailed log info
  255. extern DLL_EXPORT CLogger<5> tlog5; //gray - minor log info
  256. //XXX pls dont - 'debug macros' are usually more trubble then its worth
  257. #define HANDLE_EXCEPTION \
  258. catch (const std::exception& e) { \
  259. tlog1 << e.what() << std::endl; \
  260. } \
  261. catch (const std::exception * e) \
  262. { \
  263. tlog1 << e->what()<< std::endl; \
  264. throw; \
  265. } \
  266. catch (const std::string& e) { \
  267. tlog1 << e << std::endl; \
  268. throw; \
  269. }
  270. #define HANDLE_EXCEPTIONC(COMMAND) \
  271. catch (const std::exception& e) { \
  272. COMMAND; \
  273. tlog1 << e.what() << std::endl; \
  274. throw; \
  275. } \
  276. catch (const std::exception * e) \
  277. { \
  278. COMMAND; \
  279. tlog1 << e->what()<< std::endl; \
  280. throw; \
  281. }
  282. #endif // __GLOBAL_H__