global.h 8.3 KB

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