global.h 11 KB

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