2
0

global.h 12 KB

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