global.h 9.8 KB

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