global.h 11 KB

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