global.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. #pragma once
  2. #ifndef __GLOBAL_H__
  3. #define __GLOBAL_H__
  4. #include <iostream>
  5. #include <sstream>
  6. #include <algorithm> //std::find
  7. #include <string> //std::find
  8. #include <boost/logic/tribool.hpp>
  9. #include <boost/unordered_set.hpp>
  10. using boost::logic::tribool;
  11. #include <boost/cstdint.hpp>
  12. #include <assert.h>
  13. //filesystem version 3 causes problems (and it's default as of boost 1.46)
  14. #define BOOST_FILESYSTEM_VERSION 2
  15. typedef boost::uint64_t ui64; //unsigned int 64 bits (8 bytes)
  16. typedef boost::uint32_t ui32; //unsigned int 32 bits (4 bytes)
  17. typedef boost::uint16_t ui16; //unsigned int 16 bits (2 bytes)
  18. typedef boost::uint8_t ui8; //unsigned int 8 bits (1 byte)
  19. typedef boost::int64_t si64; //signed int 64 bits (8 bytes)
  20. typedef boost::int32_t si32; //signed int 32 bits (4 bytes)
  21. typedef boost::int16_t si16; //signed int 16 bits (2 bytes)
  22. typedef boost::int8_t si8; //signed int 8 bits (1 byte)
  23. typedef si64 expType;
  24. typedef ui16 spelltype;
  25. typedef std::pair<ui32, ui32> TDmgRange;
  26. typedef ui8 TBonusType;
  27. typedef si32 TBonusSubtype;
  28. #include "int3.h"
  29. #include <map>
  30. #include <vector>
  31. #define CHECKTIME 1
  32. #if CHECKTIME
  33. #include "timeHandler.h"
  34. #define THC
  35. #endif
  36. #define NAME_VER ("VCMI 0.84b")
  37. extern std::string NAME; //full name
  38. extern std::string NAME_AFFIX; //client / server
  39. #define CONSOLE_LOGGING_LEVEL 5
  40. #define FILE_LOGGING_LEVEL 6
  41. /*
  42. * DATA_DIR contains the game data (Data/, MP3/, ...).
  43. * USER_DIR is where to save games (Games/) and the config.
  44. * BIN_DIR is where the vcmiclient/vcmiserver binaries reside
  45. * LIB_DIR is where the AI libraries reside (linux only)
  46. */
  47. #ifdef _WIN32
  48. #define DATA_DIR "."
  49. #define USER_DIR "."
  50. #define BIN_DIR "."
  51. #define LIB_DIR "AI"
  52. #define SERVER_NAME "VCMI_server.exe"
  53. #else
  54. #ifndef DATA_DIR
  55. #error DATA_DIR undefined.
  56. #endif
  57. #ifndef BIN_DIR
  58. #error BIN_DIR undefined.
  59. #endif
  60. #ifndef LIB_DIR
  61. #error LIB_DIR undefined.
  62. #endif
  63. #define SERVER_NAME "vcmiserver"
  64. #endif
  65. #ifdef _WIN32
  66. #define PATH_SEPARATOR "\\"
  67. #else
  68. #define PATH_SEPARATOR "/"
  69. #endif
  70. /*
  71. * global.h, part of VCMI engine
  72. *
  73. * Authors: listed in file AUTHORS in main folder
  74. *
  75. * License: GNU General Public License v2.0 or later
  76. * Full text of license available in license.txt file, in main folder
  77. *
  78. */
  79. enum Ecolor {RED, BLUE, TAN, GREEN, ORANGE, PURPLE, TEAL, PINK}; //player's colors
  80. enum EvictoryConditions {artifact, gatherTroop, gatherResource, buildCity, buildGrail, beatHero,
  81. captureCity, beatMonster, takeDwellings, takeMines, transportItem, winStandard=255};
  82. enum ElossCon {lossCastle, lossHero, timeExpires, lossStandard=255};
  83. enum ECombatInfo{ALIVE = 180, SUMMONED, CLONED, HAD_MORALE, WAITING, MOVED, DEFENDING};
  84. class CGameInfo;
  85. extern const CGameInfo* CGI; //game info for general use
  86. class CClientState;
  87. extern CClientState * CCS;
  88. //a few typedefs for CCreatureSet
  89. typedef si32 TSlot;
  90. typedef si32 TQuantity;
  91. typedef ui32 TCreature; //creature id
  92. const int ARMY_SIZE = 7;
  93. const int HEROI_TYPE = 34,
  94. TOWNI_TYPE = 98,
  95. CREI_TYPE = 54,
  96. EVENTI_TYPE = 26;
  97. const int CREATURES_COUNT = 197;
  98. const int CRE_LEVELS = 10;
  99. const int F_NUMBER = 9; //factions (town types) quantity
  100. const int PLAYER_LIMIT = 8; //player limit per map
  101. const int ALL_PLAYERS = 255; //bitfield
  102. const int HEROES_PER_TYPE=8; //amount of heroes of each type
  103. const int SKILL_QUANTITY=28;
  104. const int SKILL_PER_HERO=8;
  105. const int ARTIFACTS_QUANTITY=171;
  106. const int HEROES_QUANTITY=156;
  107. const int SPELLS_QUANTITY=70;
  108. const int RESOURCE_QUANTITY=8;
  109. const int TERRAIN_TYPES=10;
  110. const int PRIMARY_SKILLS=4;
  111. const int NEUTRAL_PLAYER=255;
  112. const int NAMES_PER_TOWN=16;
  113. const int CREATURES_PER_TOWN = 7; //without upgrades
  114. const int MAX_BUILDING_PER_TURN = 1;
  115. const int SPELL_LEVELS = 5;
  116. //const int CREEP_SIZE = 4000; // neutral stacks won't grow beyond this number
  117. const int CREEP_SIZE = 2000000000;
  118. const int WEEKLY_GROWTH = 10; //percent
  119. const int AVAILABLE_HEROES_PER_PLAYER = 2;
  120. const bool DWELLINGS_ACCUMULATE_CREATURES = true;
  121. const bool STACK_EXP = true;
  122. const bool STACK_ARTIFACT = true;
  123. const int BFIELD_WIDTH = 17;
  124. const int BFIELD_HEIGHT = 11;
  125. const int BFIELD_SIZE = BFIELD_WIDTH * BFIELD_HEIGHT;
  126. const int SPELLBOOK_GOLD_COST = 500;
  127. //for battle stacks' positions
  128. struct THex
  129. {
  130. static const si16 INVALID = -1;
  131. enum EDir{RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, LEFT, TOP_LEFT, TOP_RIGHT};
  132. si16 hex;
  133. THex() : hex(INVALID) {}
  134. THex(si16 _hex) : hex(_hex)
  135. {
  136. //assert(isValid());
  137. }
  138. operator si16() const
  139. {
  140. return hex;
  141. }
  142. bool isValid() const
  143. {
  144. return hex >= 0 && hex < BFIELD_SIZE;
  145. }
  146. template<typename inttype>
  147. THex(inttype x, inttype y)
  148. {
  149. setXY(x, y);
  150. }
  151. template<typename inttype>
  152. THex(std::pair<inttype, inttype> xy)
  153. {
  154. setXY(xy);
  155. }
  156. template<typename inttype>
  157. void setX(inttype x)
  158. {
  159. setXY(x, getY());
  160. }
  161. template<typename inttype>
  162. void setY(inttype y)
  163. {
  164. setXY(getX(), y);
  165. }
  166. void setXY(si16 x, si16 y)
  167. {
  168. assert(x >= 0 && x < BFIELD_WIDTH && y >= 0 && y < BFIELD_HEIGHT);
  169. hex = x + y * BFIELD_WIDTH;
  170. }
  171. template<typename inttype>
  172. void setXY(std::pair<inttype, inttype> xy)
  173. {
  174. setXY(xy.first, xy.second);
  175. }
  176. si16 getY() const
  177. {
  178. return hex/BFIELD_WIDTH;
  179. }
  180. si16 getX() const
  181. {
  182. int pos = hex - getY() * BFIELD_WIDTH;
  183. return pos;
  184. }
  185. std::pair<si16, si16> getXY() const
  186. {
  187. return std::make_pair(getX(), getY());
  188. }
  189. //moving to direction
  190. void operator+=(EDir dir)
  191. {
  192. si16 x = getX(),
  193. y = getY();
  194. switch(dir)
  195. {
  196. case TOP_LEFT:
  197. setXY(y%2 ? x-1 : x, y-1);
  198. break;
  199. case TOP_RIGHT:
  200. setXY(y%2 ? x : x+1, y-1);
  201. break;
  202. case RIGHT:
  203. setXY(x+1, y);
  204. break;
  205. case BOTTOM_RIGHT:
  206. setXY(y%2 ? x : x+1, y+1);
  207. break;
  208. case BOTTOM_LEFT:
  209. setXY(y%2 ? x-1 : x, y+1);
  210. break;
  211. case LEFT:
  212. setXY(x-1, y);
  213. break;
  214. default:
  215. throw std::string("Disaster: wrong direction in THex::operator+=!\n");
  216. break;
  217. }
  218. }
  219. //generates new THex moved by given dir
  220. THex operator+(EDir dir) const
  221. {
  222. THex ret(*this);
  223. ret += dir;
  224. return ret;
  225. }
  226. std::vector<THex> neighbouringTiles() const
  227. {
  228. std::vector<THex> ret;
  229. const int WN = BFIELD_WIDTH;
  230. checkAndPush(hex - ( (hex/WN)%2 ? WN+1 : WN ), ret);
  231. checkAndPush(hex - ( (hex/WN)%2 ? WN : WN-1 ), ret);
  232. checkAndPush(hex - 1, ret);
  233. checkAndPush(hex + 1, ret);
  234. checkAndPush(hex + ( (hex/WN)%2 ? WN-1 : WN ), ret);
  235. checkAndPush(hex + ( (hex/WN)%2 ? WN : WN+1 ), ret);
  236. return ret;
  237. }
  238. //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)
  239. static signed char mutualPosition(THex hex1, THex hex2)
  240. {
  241. if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
  242. return 0;
  243. if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
  244. return 1;
  245. if(hex2 == hex1 - 1 && hex1%17 != 0) //left
  246. return 5;
  247. if(hex2 == hex1 + 1 && hex1%17 != 16) //right
  248. return 2;
  249. if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
  250. return 4;
  251. if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
  252. return 3;
  253. return -1;
  254. }
  255. //returns distance between given hexes
  256. static si8 getDistance(THex hex1, THex hex2)
  257. {
  258. int xDst = std::abs(hex1 % BFIELD_WIDTH - hex2 % BFIELD_WIDTH),
  259. yDst = std::abs(hex1 / BFIELD_WIDTH - hex2 / BFIELD_WIDTH);
  260. return std::max(xDst, yDst) + std::min(xDst, yDst) - (yDst + 1)/2;
  261. }
  262. template <typename Handler> void serialize(Handler &h, const int version)
  263. {
  264. h & hex;
  265. }
  266. private:
  267. static void checkAndPush(int tile, std::vector<THex> & ret)
  268. {
  269. if( tile>=0 && tile<BFIELD_SIZE && (tile%BFIELD_WIDTH != (BFIELD_WIDTH - 1)) && (tile%BFIELD_WIDTH != 0) )
  270. ret.push_back(THex(tile));
  271. }
  272. };
  273. enum EMarketMode
  274. {
  275. RESOURCE_RESOURCE, RESOURCE_PLAYER, CREATURE_RESOURCE, RESOURCE_ARTIFACT,
  276. ARTIFACT_RESOURCE, ARTIFACT_EXP, CREATURE_EXP, CREATURE_UNDEAD, RESOURCE_SKILL,
  277. MARTKET_AFTER_LAST_PLACEHOLDER
  278. };
  279. namespace SpellCasting
  280. {
  281. enum ESpellCastProblem
  282. {
  283. OK, NO_HERO_TO_CAST_SPELL, ALREADY_CASTED_THIS_TURN, NO_SPELLBOOK, ANOTHER_ELEMENTAL_SUMMONED,
  284. HERO_DOESNT_KNOW_SPELL, NOT_ENOUGH_MANA, ADVMAP_SPELL_INSTEAD_OF_BATTLE_SPELL,
  285. SECOND_HEROS_SPELL_IMMUNITY, SPELL_LEVEL_LIMIT_EXCEEDED, NO_SPELLS_TO_DISPEL,
  286. NO_APPROPRIATE_TARGET, STACK_IMMUNE_TO_SPELL, WRONG_SPELL_TARGET
  287. };
  288. enum ECastingMode {HERO_CASTING, AFTER_ATTACK_CASTING};
  289. }
  290. namespace Buildings
  291. {
  292. enum EBuildStructure
  293. {
  294. HAVE_CAPITAL, NO_WATER, FORBIDDEN, ADD_MAGES_GUILD, ALREADY_PRESENT, CANT_BUILD_TODAY,
  295. NO_RESOURCES, ALLOWED, PREREQUIRES, ERROR
  296. };
  297. //Quite useful as long as most of building mechanics hardcoded
  298. enum EBuilding
  299. {
  300. MAGES_GUILD_1, MAGES_GUILD_2, MAGES_GUILD_3, MAGES_GUILD_4, MAGES_GUILD_5,
  301. TAVERN, SHIPYARD, FORT, CITADEL, CASTLE,
  302. VILLAGE_HALL, TOWN_HALL, CITY_HALL, CAPITOL, MARKETPLACE,
  303. RESOURCE_SILO, BLACKSMITH, SPECIAL_1, HORDE_1, HORDE_1_UPGR,
  304. SHIP, SPECIAL_2, SPECIAL_3, SPECIAL_4, HORDE_2,
  305. HORDE_2_UPGR, GRAIL, EXTRA_CITY_HALL, EXTRA_TOWN_HALL, EXTRA_CAPITOL,
  306. DWELL_FIRST=30, DWELL_LAST=36, DWELL_UP_FIRST=37, DWELL_UP_LAST=43
  307. };
  308. }
  309. namespace Res
  310. {
  311. enum ERes
  312. {
  313. WOOD = 0, MERCURY, ORE, SULFUR, CRYSTAL, GEMS, GOLD, MITHRIL
  314. };
  315. }
  316. namespace Arts
  317. {
  318. enum EPos
  319. {
  320. PRE_FIRST = -1,
  321. HEAD, SHOULDERS, NECK, RIGHT_HAND, LEFT_HAND, TORSO, RIGHT_RING, LEFT_RING, FEET, MISC1, MISC2, MISC3, MISC4,
  322. MACH1, MACH2, MACH3, MACH4, SPELLBOOK, MISC5,
  323. AFTER_LAST
  324. };
  325. const ui16 BACKPACK_START = 19;
  326. const int ID_CATAPULT = 3, ID_LOCK = 145;
  327. }
  328. enum EAlignment
  329. {
  330. GOOD, EVIL, NEUTRAL
  331. };
  332. // Converts an int/double or any data type you wish to a string
  333. template<typename T>
  334. std::string toString(const T& value)
  335. {
  336. std::ostringstream oss;
  337. oss << value;
  338. return oss.str();
  339. }
  340. //uncomment to make it work
  341. //#define MARK_BLOCKED_POSITIONS
  342. //#define MARK_VISITABLE_POSITIONS
  343. #define DEFBYPASS
  344. #ifdef _WIN32
  345. #define DLL_F_EXPORT __declspec(dllexport)
  346. #else
  347. #if defined(__GNUC__) && __GNUC__ >= 4
  348. #define DLL_F_EXPORT __attribute__ ((visibility("default")))
  349. #else
  350. #define DLL_F_EXPORT
  351. #endif
  352. #endif
  353. #ifdef _WIN32
  354. #define DLL_F_IMPORT __declspec(dllimport)
  355. #else
  356. #if defined(__GNUC__) && __GNUC__ >= 4
  357. #define DLL_F_IMPORT __attribute__ ((visibility("default")))
  358. #else
  359. #define DLL_F_IMPORT
  360. #endif
  361. #endif
  362. #ifdef VCMI_DLL
  363. #define DLL_EXPORT DLL_F_EXPORT
  364. #else
  365. #define DLL_EXPORT DLL_F_IMPORT
  366. #endif
  367. template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
  368. #define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
  369. //a normal std::map with consted operator[] for sanity
  370. template<typename KeyT, typename ValT>
  371. class bmap : public std::map<KeyT, ValT>
  372. {
  373. public:
  374. const ValT & operator[](KeyT key) const
  375. {
  376. return find(key)->second;
  377. }
  378. ValT & operator[](KeyT key)
  379. {
  380. return static_cast<std::map<KeyT, ValT> &>(*this)[key];
  381. }
  382. template <typename Handler> void serialize(Handler &h, const int version)
  383. {
  384. h & static_cast<std::map<KeyT, ValT> &>(*this);
  385. }
  386. };
  387. namespace vstd
  388. {
  389. template <typename Container, typename Item>
  390. bool contains(const Container & c, const Item &i) //returns true if container c contains item i
  391. {
  392. return std::find(c.begin(),c.end(),i) != c.end();
  393. }
  394. template <typename V, typename Item, typename Item2>
  395. bool contains(const std::map<Item,V> & c, const Item2 &i) //returns true if map c contains item i
  396. {
  397. return c.find(i)!=c.end();
  398. }
  399. template <typename V, typename Item, typename Item2>
  400. bool contains(const bmap<Item,V> & c, const Item2 &i) //returns true if bmap c contains item i
  401. {
  402. return c.find(i)!=c.end();
  403. }
  404. template <typename Item>
  405. bool contains(const boost::unordered_set<Item> & c, const Item &i) //returns true if unordered set c contains item i
  406. {
  407. return c.find(i)!=c.end();
  408. }
  409. template <typename Item, size_t N>
  410. bool contains(const Item (&c)[N], const Item &i) //returns true if given array contains item i
  411. {
  412. return std::find(c, c+N, i) != c+N; //TODO: find out why template is not resolved
  413. }
  414. template <typename Container1, typename Container2>
  415. typename Container2::iterator findFirstNot(Container1 &c1, Container2 &c2)//returns first element of c2 not present in c1
  416. {
  417. typename Container2::iterator itr = c2.begin();
  418. while(itr != c2.end())
  419. if(!contains(c1,*itr))
  420. return itr;
  421. else
  422. ++itr;
  423. return c2.end();
  424. }
  425. template <typename Container1, typename Container2>
  426. typename Container2::const_iterator findFirstNot(const Container1 &c1, const Container2 &c2)//returns const first element of c2 not present in c1
  427. {
  428. typename Container2::const_iterator itr = c2.begin();
  429. while(itr != c2.end())
  430. if(!contains(c1,*itr))
  431. return itr;
  432. else
  433. ++itr;
  434. return c2.end();
  435. }
  436. template <typename Container, typename Item>
  437. typename Container::iterator find(const Container & c, const Item &i)
  438. {
  439. return std::find(c.begin(),c.end(),i);
  440. }
  441. template <typename T1, typename T2>
  442. 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
  443. {
  444. for(size_t i=0; i < c.size(); ++i)
  445. if(c[i] == s)
  446. return i;
  447. return -1;
  448. }
  449. template <typename T1, typename T2, typename Func>
  450. int findPos(const std::vector<T1> & c, const T2 &s, const Func &f) //Func(T1,T2) must say if these elements matches
  451. {
  452. for(size_t i=0; i < c.size(); ++i)
  453. if(f(c[i],s))
  454. return i;
  455. return -1;
  456. }
  457. template <typename Container, typename Item>
  458. typename Container::iterator find(Container & c, const Item &i) //returns iterator to the given element if present in container, end() if not
  459. {
  460. return std::find(c.begin(),c.end(),i);
  461. }
  462. template <typename Container, typename Item>
  463. 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
  464. {
  465. return std::find(c.begin(),c.end(),i);
  466. }
  467. template <typename Container, typename Item>
  468. typename Container::size_type operator-=(Container &c, const Item &i) //removes element i from container c, returns false if c does not contain i
  469. {
  470. typename Container::iterator itr = find(c,i);
  471. if(itr == c.end())
  472. return false;
  473. c.erase(itr);
  474. return true;
  475. }
  476. template <typename t1>
  477. void delObj(t1 *a1)
  478. {
  479. delete a1;
  480. }
  481. template <typename t1, typename t2>
  482. void assign(t1 &a1, const t2 &a2)
  483. {
  484. a1 = a2;
  485. }
  486. template <typename t1, typename t2>
  487. struct assigner
  488. {
  489. public:
  490. t1 &op1;
  491. t2 op2;
  492. assigner(t1 &a1, const t2 & a2)
  493. :op1(a1), op2(a2)
  494. {}
  495. void operator()()
  496. {
  497. op1 = op2;
  498. }
  499. };
  500. template <typename t1, typename t2>
  501. assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
  502. {
  503. return assigner<t1,t2>(a1,a2);
  504. }
  505. template <typename t1, typename t2, typename t3>
  506. bool equal(const t1 &a1, const t3 t1::* point, const t2 &a2)
  507. {
  508. return a1.*point == a2;
  509. }
  510. template <typename t1, typename t2>
  511. bool equal(const t1 &a1, const t2 &a2)
  512. {
  513. return a1 == a2;
  514. }
  515. }
  516. using vstd::operator-=;
  517. template <typename t1, typename t2>
  518. t1 & amax(t1 &a, const t2 &b) //assigns greater of (a, b) to a and returns maximum of (a, b)
  519. {
  520. if(a >= b)
  521. return a;
  522. else
  523. {
  524. a = b;
  525. return a;
  526. }
  527. }
  528. template <typename t1, typename t2>
  529. t1 & amin(t1 &a, const t2 &b) //assigns smaller of (a, b) to a and returns minimum of (a, b)
  530. {
  531. if(a <= b)
  532. return a;
  533. else
  534. {
  535. a = b;
  536. return a;
  537. }
  538. }
  539. template <typename t1, typename t2, typename t3>
  540. t1 & abetw(t1 &a, const t2 &b, const t3 &c) //makes a to fit the range <b, c>
  541. {
  542. amax(a,b);
  543. amin(a,c);
  544. return a;
  545. }
  546. template <typename t1, typename t2, typename t3>
  547. bool isbetw(const t1 &a, const t2 &b, const t3 &c) //checks if a is between b and c
  548. {
  549. return a > b && a < c;
  550. }
  551. template <typename t1, typename t2, typename t3>
  552. bool iswith(const t1 &a, const t2 &b, const t3 &c) //checks if a is within b and c
  553. {
  554. return a >= b && a <= c;
  555. }
  556. template <typename T>
  557. void delNull(T* &ptr) //deleted pointer and sets it to NULL
  558. {
  559. delete ptr;
  560. ptr = NULL;
  561. }
  562. #include "CConsoleHandler.h"
  563. extern DLL_EXPORT std::ostream *logfile;
  564. extern DLL_EXPORT CConsoleHandler *console;
  565. class CLogger //logger, prints log info to console and saves in file
  566. {
  567. const int lvl;
  568. public:
  569. CLogger& operator<<(std::ostream& (*fun)(std::ostream&))
  570. {
  571. if(lvl < CONSOLE_LOGGING_LEVEL)
  572. std::cout << fun;
  573. if((lvl < FILE_LOGGING_LEVEL) && logfile)
  574. *logfile << fun;
  575. return *this;
  576. }
  577. template<typename T>
  578. CLogger & operator<<(const T & data)
  579. {
  580. if(lvl < CONSOLE_LOGGING_LEVEL)
  581. {
  582. if(console)
  583. console->print(data,lvl);
  584. else
  585. std::cout << data << std::flush;
  586. }
  587. if((lvl < FILE_LOGGING_LEVEL) && logfile)
  588. *logfile << data << std::flush;
  589. return *this;
  590. }
  591. CLogger(const int Lvl) : lvl(Lvl) {}
  592. };
  593. extern DLL_EXPORT CLogger tlog0; //green - standard progress info
  594. extern DLL_EXPORT CLogger tlog1; //red - big errors
  595. extern DLL_EXPORT CLogger tlog2; //magenta - major warnings
  596. extern DLL_EXPORT CLogger tlog3; //yellow - minor warnings
  597. extern DLL_EXPORT CLogger tlog4; //white - detailed log info
  598. extern DLL_EXPORT CLogger tlog5; //gray - minor log info
  599. extern DLL_EXPORT CLogger tlog6; //teal - AI info
  600. //XXX pls dont - 'debug macros' are usually more trouble than it's worth
  601. #define HANDLE_EXCEPTION \
  602. catch (const std::exception& e) { \
  603. tlog1 << e.what() << std::endl; \
  604. throw; \
  605. } \
  606. catch (const std::exception * e) \
  607. { \
  608. tlog1 << e->what()<< std::endl; \
  609. throw; \
  610. } \
  611. catch (const std::string& e) { \
  612. tlog1 << e << std::endl; \
  613. throw; \
  614. }
  615. #define HANDLE_EXCEPTIONC(COMMAND) \
  616. catch (const std::exception& e) { \
  617. COMMAND; \
  618. tlog1 << e.what() << std::endl; \
  619. throw; \
  620. } \
  621. catch (const std::string &e) \
  622. { \
  623. COMMAND; \
  624. tlog1 << e << std::endl; \
  625. throw; \
  626. }
  627. #if defined(linux) && defined(sparc)
  628. /* SPARC does not support unaligned memory access. Let gcc know when
  629. * to emit the right code. */
  630. struct unaligned_Uint16 { ui16 val __attribute__(( packed )); };
  631. struct unaligned_Uint32 { ui32 val __attribute__(( packed )); };
  632. static inline ui16 read_unaligned_u16(const void *p)
  633. {
  634. const struct unaligned_Uint16 *v = (const struct unaligned_Uint16 *)p;
  635. return v->val;
  636. }
  637. static inline ui32 read_unaligned_u32(const void *p)
  638. {
  639. const struct unaligned_Uint32 *v = (const struct unaligned_Uint32 *)p;
  640. return v->val;
  641. }
  642. #else
  643. #define read_unaligned_u16(p) (* reinterpret_cast<const Uint16 *>(p))
  644. #define read_unaligned_u32(p) (* reinterpret_cast<const Uint32 *>(p))
  645. #endif
  646. //for explicit overrides
  647. #ifdef _MSC_VER
  648. #define OVERRIDE override
  649. #else
  650. #define OVERRIDE //is there any working counterpart?
  651. #endif
  652. #define BONUS_TREE_DESERIALIZATION_FIX if(!h.saving && h.smartPointerSerialization) deserializationFix();
  653. #endif // __GLOBAL_H__