global.h 9.4 KB

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