global.h 9.0 KB

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