global.h 13 KB

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