global.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. #include <assert.h>
  11. typedef boost::uint64_t ui64; //unsigned int 64 bits (8 bytes)
  12. typedef boost::uint32_t ui32; //unsigned int 32 bits (4 bytes)
  13. typedef boost::uint16_t ui16; //unsigned int 16 bits (2 bytes)
  14. typedef boost::uint8_t ui8; //unsigned int 8 bits (1 byte)
  15. typedef boost::int64_t si64; //signed int 64 bits (8 bytes)
  16. typedef boost::int32_t si32; //signed int 32 bits (4 bytes)
  17. typedef boost::int16_t si16; //signed int 16 bits (2 bytes)
  18. typedef boost::int8_t si8; //signed int 8 bits (1 byte)
  19. typedef si64 expType;
  20. typedef ui16 spelltype;
  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. //for battle stacks' positions
  118. struct THex
  119. {
  120. enum EDir{RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT, TOP_LEFT, TOP_RIGHT};
  121. si16 hex;
  122. THex() : hex(-1) {}
  123. THex(si16 _hex) : hex(_hex)
  124. {
  125. assert(hex >= 0 && hex < BFIELD_SIZE);
  126. }
  127. operator si16() const
  128. {
  129. return hex;
  130. }
  131. template<typename inttype>
  132. THex(inttype x, inttype y)
  133. {
  134. setXY(x, y);
  135. }
  136. template<typename inttype>
  137. THex(std::pair<inttype, inttype> xy)
  138. {
  139. setXY(xy);
  140. }
  141. template<typename inttype>
  142. void setX(inttype x)
  143. {
  144. setXY(x, getY());
  145. }
  146. template<typename inttype>
  147. void setY(inttype y)
  148. {
  149. setXY(getX(), y);
  150. }
  151. void setXY(si16 x, si16 y)
  152. {
  153. assert(x >= 0 && x < BFIELD_WIDTH && y >= 0 && y < BFIELD_HEIGHT);
  154. hex = x + y * BFIELD_WIDTH;
  155. }
  156. template<typename inttype>
  157. void setXY(std::pair<inttype, inttype> xy)
  158. {
  159. setXY(xy.first, xy.second);
  160. }
  161. si16 getY() const
  162. {
  163. return hex/BFIELD_WIDTH;
  164. }
  165. si16 getX() const
  166. {
  167. int pos = hex - getY() * BFIELD_WIDTH;
  168. return pos;
  169. }
  170. std::pair<si16, si16> getXY() const
  171. {
  172. return std::make_pair(getX(), getY());
  173. }
  174. //moving to direction
  175. void operator+=(EDir dir)
  176. {
  177. si16 x = getX(),
  178. y = getY();
  179. switch(dir)
  180. {
  181. case TOP_LEFT:
  182. setXY(y%2 ? x-1 : x, y-1);
  183. case TOP_RIGHT:
  184. setXY(y%2 ? x : x+1, y-1);
  185. case RIGHT:
  186. setXY(x+1, y);
  187. case BOTTOM_RIGHT:
  188. setXY(y%2 ? x : x+1, y+1);
  189. case BOTTOM_LEFT:
  190. setXY(y%2 ? x-1 : x, y+1);
  191. case LEFT:
  192. setXY(x-1, y);
  193. default:
  194. throw std::string("Disaster: wrong direction in THex::operator+=!\n");
  195. }
  196. }
  197. //generates new THex moved by given dir
  198. THex operator+(EDir dir) const
  199. {
  200. THex ret(*this);
  201. ret += dir;
  202. return ret;
  203. }
  204. std::vector<THex> neighbouringTiles() const
  205. {
  206. std::vector<THex> ret;
  207. const int WN = BFIELD_WIDTH;
  208. checkAndPush(hex - ( (hex/WN)%2 ? WN+1 : WN ), ret);
  209. checkAndPush(hex - ( (hex/WN)%2 ? WN : WN-1 ), ret);
  210. checkAndPush(hex - 1, ret);
  211. checkAndPush(hex + 1, ret);
  212. checkAndPush(hex + ( (hex/WN)%2 ? WN-1 : WN ), ret);
  213. checkAndPush(hex + ( (hex/WN)%2 ? WN : WN+1 ), ret);
  214. return ret;
  215. }
  216. //returns info about mutual position of given hexes (-1 - they're distant, 0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left)
  217. static signed char mutualPosition(THex hex1, THex hex2)
  218. {
  219. if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
  220. return 0;
  221. if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
  222. return 1;
  223. if(hex2 == hex1 - 1 && hex1%17 != 0) //left
  224. return 5;
  225. if(hex2 == hex1 + 1 && hex1%17 != 16) //right
  226. return 2;
  227. if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
  228. return 4;
  229. if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
  230. return 3;
  231. return -1;
  232. }
  233. //returns distance between given hexes
  234. static si8 getDistance(THex hex1, THex hex2)
  235. {
  236. int xDst = std::abs(hex1 % BFIELD_WIDTH - hex2 % BFIELD_WIDTH),
  237. yDst = std::abs(hex1 / BFIELD_WIDTH - hex2 / BFIELD_WIDTH);
  238. return std::max(xDst, yDst) + std::min(xDst, yDst) - (yDst + 1)/2;
  239. }
  240. template <typename Handler> void serialize(Handler &h, const int version)
  241. {
  242. h & hex;
  243. }
  244. private:
  245. static void checkAndPush(int tile, std::vector<THex> & ret)
  246. {
  247. if( tile>=0 && tile<BFIELD_SIZE && (tile%BFIELD_WIDTH != (BFIELD_WIDTH - 1)) && (tile%BFIELD_WIDTH != 0) )
  248. ret.push_back(THex(tile));
  249. }
  250. };
  251. enum EMarketMode
  252. {
  253. RESOURCE_RESOURCE, RESOURCE_PLAYER, CREATURE_RESOURCE, RESOURCE_ARTIFACT,
  254. ARTIFACT_RESOURCE, ARTIFACT_EXP, CREATURE_EXP, CREATURE_UNDEAD, RESOURCE_SKILL,
  255. MARTKET_AFTER_LAST_PLACEHOLDER
  256. };
  257. namespace Res
  258. {
  259. enum ERes
  260. {
  261. WOOD = 0, MERCURY, ORE, SULFUR, CRYSTAL, GEMS, GOLD, MITHRIL
  262. };
  263. }
  264. namespace Arts
  265. {
  266. enum EPos
  267. {
  268. PRE_FIRST = -1,
  269. HEAD, SHOULDERS, NECK, RIGHT_HAND, LEFT_HAND, TORSO, RIGHT_RING, LEFT_RING, FEET, MISC1, MISC2, MISC3, MISC4,
  270. MACH1, MACH2, MACH3, MACH4, SPELLBOOK, MISC5,
  271. AFTER_LAST
  272. };
  273. const ui16 BACKPACK_START = 19;
  274. const int ID_CATAPULT = 3, ID_LOCK = 145;
  275. }
  276. enum EAlignment
  277. {
  278. GOOD, EVIL, NEUTRAL
  279. };
  280. //uncomment to make it work
  281. //#define MARK_BLOCKED_POSITIONS
  282. //#define MARK_VISITABLE_POSITIONS
  283. #define DEFBYPASS
  284. #ifdef _WIN32
  285. #define DLL_F_EXPORT __declspec(dllexport)
  286. #else
  287. #if defined(__GNUC__) && __GNUC__ >= 4
  288. #define DLL_F_EXPORT __attribute__ ((visibility("default")))
  289. #else
  290. #define DLL_F_EXPORT
  291. #endif
  292. #endif
  293. #ifdef _WIN32
  294. #define DLL_F_IMPORT __declspec(dllimport)
  295. #else
  296. #if defined(__GNUC__) && __GNUC__ >= 4
  297. #define DLL_F_IMPORT __attribute__ ((visibility("default")))
  298. #else
  299. #define DLL_F_IMPORT
  300. #endif
  301. #endif
  302. #ifdef VCMI_DLL
  303. #define DLL_EXPORT DLL_F_EXPORT
  304. #else
  305. #define DLL_EXPORT DLL_F_IMPORT
  306. #endif
  307. template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
  308. #define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
  309. //a normal std::map with consted operator[] for sanity
  310. template<typename KeyT, typename ValT>
  311. class bmap : public std::map<KeyT, ValT>
  312. {
  313. public:
  314. const ValT & operator[](KeyT key) const
  315. {
  316. const_iterator it = find(key);
  317. return it->second;
  318. }
  319. ValT & operator[](KeyT key)
  320. {
  321. return static_cast<std::map<KeyT, ValT> &>(*this)[key];
  322. }
  323. template <typename Handler> void serialize(Handler &h, const int version)
  324. {
  325. h & static_cast<std::map<KeyT, ValT> &>(*this);
  326. }
  327. };
  328. namespace vstd
  329. {
  330. template <typename Container, typename Item>
  331. bool contains(const Container & c, const Item &i) //returns true if container c contains item i
  332. {
  333. return std::find(c.begin(),c.end(),i) != c.end();
  334. }
  335. template <typename V, typename Item, typename Item2>
  336. bool contains(const std::map<Item,V> & c, const Item2 &i) //returns true if map c contains item i
  337. {
  338. return c.find(i)!=c.end();
  339. }
  340. template <typename V, typename Item, typename Item2>
  341. bool contains(const bmap<Item,V> & c, const Item2 &i) //returns true if map c contains item i
  342. {
  343. return c.find(i)!=c.end();
  344. }
  345. template <typename Container1, typename Container2>
  346. typename Container2::iterator findFirstNot(Container1 &c1, Container2 &c2)//returns first element of c2 not present in c1
  347. {
  348. typename Container2::iterator itr = c2.begin();
  349. while(itr != c2.end())
  350. if(!contains(c1,*itr))
  351. return itr;
  352. else
  353. ++itr;
  354. return c2.end();
  355. }
  356. template <typename Container1, typename Container2>
  357. typename Container2::const_iterator findFirstNot(const Container1 &c1, const Container2 &c2)//returns const first element of c2 not present in c1
  358. {
  359. typename Container2::const_iterator itr = c2.begin();
  360. while(itr != c2.end())
  361. if(!contains(c1,*itr))
  362. return itr;
  363. else
  364. ++itr;
  365. return c2.end();
  366. }
  367. template <typename Container, typename Item>
  368. typename Container::iterator find(const Container & c, const Item &i)
  369. {
  370. return std::find(c.begin(),c.end(),i);
  371. }
  372. template <typename T1, typename T2>
  373. 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
  374. {
  375. for(size_t i=0; i < c.size(); ++i)
  376. if(c[i] == s)
  377. return i;
  378. return -1;
  379. }
  380. template <typename T1, typename T2, typename Func>
  381. int findPos(const std::vector<T1> & c, const T2 &s, const Func &f) //Func(T1,T2) must say if these elements matches
  382. {
  383. for(size_t i=0; i < c.size(); ++i)
  384. if(f(c[i],s))
  385. return i;
  386. return -1;
  387. }
  388. template <typename Container, typename Item>
  389. typename Container::iterator find(Container & c, const Item &i) //returns iterator to the given element if present in container, end() if not
  390. {
  391. return std::find(c.begin(),c.end(),i);
  392. }
  393. template <typename Container, typename Item>
  394. 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
  395. {
  396. return std::find(c.begin(),c.end(),i);
  397. }
  398. template <typename Container, typename Item>
  399. typename Container::size_type operator-=(Container &c, const Item &i) //removes element i from container c, returns false if c does not contain i
  400. {
  401. typename Container::iterator itr = find(c,i);
  402. if(itr == c.end())
  403. return false;
  404. c.erase(itr);
  405. return true;
  406. }
  407. template <typename t1>
  408. void delObj(t1 *a1)
  409. {
  410. delete a1;
  411. }
  412. template <typename t1, typename t2>
  413. void assign(t1 &a1, const t2 &a2)
  414. {
  415. a1 = a2;
  416. }
  417. template <typename t1, typename t2>
  418. struct assigner
  419. {
  420. public:
  421. t1 &op1;
  422. t2 op2;
  423. assigner(t1 &a1, const t2 & a2)
  424. :op1(a1), op2(a2)
  425. {}
  426. void operator()()
  427. {
  428. op1 = op2;
  429. }
  430. };
  431. template <typename t1, typename t2>
  432. assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
  433. {
  434. return assigner<t1,t2>(a1,a2);
  435. }
  436. template <typename t1, typename t2, typename t3>
  437. bool equal(const t1 &a1, const t3 t1::* point, const t2 &a2)
  438. {
  439. return a1.*point == a2;
  440. }
  441. template <typename t1, typename t2>
  442. bool equal(const t1 &a1, const t2 &a2)
  443. {
  444. return a1 == a2;
  445. }
  446. }
  447. using vstd::operator-=;
  448. template <typename t1, typename t2>
  449. t1 & amax(t1 &a, const t2 &b) //assigns greater of (a, b) to a and returns maximum of (a, b)
  450. {
  451. if(a >= b)
  452. return a;
  453. else
  454. {
  455. a = b;
  456. return a;
  457. }
  458. }
  459. template <typename t1, typename t2>
  460. t1 & amin(t1 &a, const t2 &b) //assigns smaller of (a, b) to a and returns minimum of (a, b)
  461. {
  462. if(a <= b)
  463. return a;
  464. else
  465. {
  466. a = b;
  467. return a;
  468. }
  469. }
  470. template <typename t1, typename t2, typename t3>
  471. t1 & abetw(t1 &a, const t2 &b, const t3 &c) //makes a to fit the range <b, c>
  472. {
  473. amax(a,b);
  474. amin(a,c);
  475. return a;
  476. }
  477. template <typename T>
  478. void delNull(T* &ptr) //deleted pointer and sets it to NULL
  479. {
  480. delete ptr;
  481. ptr = NULL;
  482. }
  483. #include "CConsoleHandler.h"
  484. extern DLL_EXPORT std::ostream *logfile;
  485. extern DLL_EXPORT CConsoleHandler *console;
  486. class CLogger //logger, prints log info to console and saves in file
  487. {
  488. const int lvl;
  489. public:
  490. CLogger& operator<<(std::ostream& (*fun)(std::ostream&))
  491. {
  492. if(lvl < CONSOLE_LOGGING_LEVEL)
  493. std::cout << fun;
  494. if((lvl < FILE_LOGGING_LEVEL) && logfile)
  495. *logfile << fun;
  496. return *this;
  497. }
  498. template<typename T>
  499. CLogger & operator<<(const T & data)
  500. {
  501. if(lvl < CONSOLE_LOGGING_LEVEL)
  502. {
  503. if(console)
  504. console->print(data,lvl);
  505. else
  506. std::cout << data << std::flush;
  507. }
  508. if((lvl < FILE_LOGGING_LEVEL) && logfile)
  509. *logfile << data << std::flush;
  510. return *this;
  511. }
  512. CLogger(const int Lvl) : lvl(Lvl) {}
  513. };
  514. extern DLL_EXPORT CLogger tlog0; //green - standard progress info
  515. extern DLL_EXPORT CLogger tlog1; //red - big errors
  516. extern DLL_EXPORT CLogger tlog2; //magenta - major warnings
  517. extern DLL_EXPORT CLogger tlog3; //yellow - minor warnings
  518. extern DLL_EXPORT CLogger tlog4; //white - detailed log info
  519. extern DLL_EXPORT CLogger tlog5; //gray - minor log info
  520. extern DLL_EXPORT CLogger tlog6; //teal - AI info
  521. //XXX pls dont - 'debug macros' are usually more trouble than it's worth
  522. #define HANDLE_EXCEPTION \
  523. catch (const std::exception& e) { \
  524. tlog1 << e.what() << std::endl; \
  525. throw; \
  526. } \
  527. catch (const std::exception * e) \
  528. { \
  529. tlog1 << e->what()<< std::endl; \
  530. throw; \
  531. } \
  532. catch (const std::string& e) { \
  533. tlog1 << e << std::endl; \
  534. throw; \
  535. }
  536. #define HANDLE_EXCEPTIONC(COMMAND) \
  537. catch (const std::exception& e) { \
  538. COMMAND; \
  539. tlog1 << e.what() << std::endl; \
  540. throw; \
  541. } \
  542. catch (const std::string &e) \
  543. { \
  544. COMMAND; \
  545. tlog1 << e << std::endl; \
  546. throw; \
  547. }
  548. #if defined(linux) && defined(sparc)
  549. /* SPARC does not support unaligned memory access. Let gcc know when
  550. * to emit the right code. */
  551. struct unaligned_Uint16 { ui16 val __attribute__(( packed )); };
  552. struct unaligned_Uint32 { ui32 val __attribute__(( packed )); };
  553. static inline ui16 read_unaligned_u16(const void *p)
  554. {
  555. const struct unaligned_Uint16 *v = (const struct unaligned_Uint16 *)p;
  556. return v->val;
  557. }
  558. static inline ui32 read_unaligned_u32(const void *p)
  559. {
  560. const struct unaligned_Uint32 *v = (const struct unaligned_Uint32 *)p;
  561. return v->val;
  562. }
  563. #else
  564. #define read_unaligned_u16(p) (* reinterpret_cast<const Uint16 *>(p))
  565. #define read_unaligned_u32(p) (* reinterpret_cast<const Uint32 *>(p))
  566. #endif
  567. //for explicit overrides
  568. #ifdef _MSC_VER
  569. #define OVERRIDE override
  570. #else
  571. #define OVERRIDE //is there any working counterpart?
  572. #endif
  573. #endif // __GLOBAL_H__