global.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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.85b")
  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 = false;
  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. //uncomment to make it work
  333. //#define MARK_BLOCKED_POSITIONS
  334. //#define MARK_VISITABLE_POSITIONS
  335. #define DEFBYPASS
  336. #ifdef _WIN32
  337. #define DLL_F_EXPORT __declspec(dllexport)
  338. #else
  339. #if defined(__GNUC__) && __GNUC__ >= 4
  340. #define DLL_F_EXPORT __attribute__ ((visibility("default")))
  341. #else
  342. #define DLL_F_EXPORT
  343. #endif
  344. #endif
  345. #ifdef _WIN32
  346. #define DLL_F_IMPORT __declspec(dllimport)
  347. #else
  348. #if defined(__GNUC__) && __GNUC__ >= 4
  349. #define DLL_F_IMPORT __attribute__ ((visibility("default")))
  350. #else
  351. #define DLL_F_IMPORT
  352. #endif
  353. #endif
  354. #ifdef VCMI_DLL
  355. #define DLL_EXPORT DLL_F_EXPORT
  356. #else
  357. #define DLL_EXPORT DLL_F_IMPORT
  358. #endif
  359. template<typename T, size_t N> char (&_ArrayCountObj(const T (&)[N]))[N];
  360. #define ARRAY_COUNT(arr) (sizeof(_ArrayCountObj(arr)))
  361. //a normal std::map with consted operator[] for sanity
  362. template<typename KeyT, typename ValT>
  363. class bmap : public std::map<KeyT, ValT>
  364. {
  365. public:
  366. const ValT & operator[](KeyT key) const
  367. {
  368. return find(key)->second;
  369. }
  370. ValT & operator[](KeyT key)
  371. {
  372. return static_cast<std::map<KeyT, ValT> &>(*this)[key];
  373. }
  374. template <typename Handler> void serialize(Handler &h, const int version)
  375. {
  376. h & static_cast<std::map<KeyT, ValT> &>(*this);
  377. }
  378. };
  379. namespace vstd
  380. {
  381. template <typename Container, typename Item>
  382. bool contains(const Container & c, const Item &i) //returns true if container c contains item i
  383. {
  384. return std::find(c.begin(),c.end(),i) != c.end();
  385. }
  386. template <typename V, typename Item, typename Item2>
  387. bool contains(const std::map<Item,V> & c, const Item2 &i) //returns true if map c contains item i
  388. {
  389. return c.find(i)!=c.end();
  390. }
  391. template <typename V, typename Item, typename Item2>
  392. bool contains(const bmap<Item,V> & c, const Item2 &i) //returns true if bmap c contains item i
  393. {
  394. return c.find(i)!=c.end();
  395. }
  396. template <typename Item>
  397. bool contains(const boost::unordered_set<Item> & c, const Item &i) //returns true if unordered set c contains item i
  398. {
  399. return c.find(i)!=c.end();
  400. }
  401. template <typename Item, size_t N>
  402. bool contains(const Item (&c)[N], const Item &i) //returns true if given array contains item i
  403. {
  404. return std::find(c, c+N, i) != c+N; //TODO: find out why template is not resolved
  405. }
  406. template <typename Container1, typename Container2>
  407. typename Container2::iterator findFirstNot(Container1 &c1, Container2 &c2)//returns first element of c2 not present in c1
  408. {
  409. typename Container2::iterator itr = c2.begin();
  410. while(itr != c2.end())
  411. if(!contains(c1,*itr))
  412. return itr;
  413. else
  414. ++itr;
  415. return c2.end();
  416. }
  417. template <typename Container1, typename Container2>
  418. typename Container2::const_iterator findFirstNot(const Container1 &c1, const Container2 &c2)//returns const first element of c2 not present in c1
  419. {
  420. typename Container2::const_iterator itr = c2.begin();
  421. while(itr != c2.end())
  422. if(!contains(c1,*itr))
  423. return itr;
  424. else
  425. ++itr;
  426. return c2.end();
  427. }
  428. template <typename Container, typename Item>
  429. typename Container::iterator find(const Container & c, const Item &i)
  430. {
  431. return std::find(c.begin(),c.end(),i);
  432. }
  433. template <typename T1, typename T2>
  434. 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
  435. {
  436. for(size_t i=0; i < c.size(); ++i)
  437. if(c[i] == s)
  438. return i;
  439. return -1;
  440. }
  441. template <typename T1, typename T2, typename Func>
  442. int findPos(const std::vector<T1> & c, const T2 &s, const Func &f) //Func(T1,T2) must say if these elements matches
  443. {
  444. for(size_t i=0; i < c.size(); ++i)
  445. if(f(c[i],s))
  446. return i;
  447. return -1;
  448. }
  449. template <typename Container, typename Item>
  450. typename Container::iterator find(Container & c, const Item &i) //returns iterator to the given element if present in container, end() if not
  451. {
  452. return std::find(c.begin(),c.end(),i);
  453. }
  454. template <typename Container, typename Item>
  455. 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
  456. {
  457. return std::find(c.begin(),c.end(),i);
  458. }
  459. template <typename Container, typename Item>
  460. typename Container::size_type operator-=(Container &c, const Item &i) //removes element i from container c, returns false if c does not contain i
  461. {
  462. typename Container::iterator itr = find(c,i);
  463. if(itr == c.end())
  464. return false;
  465. c.erase(itr);
  466. return true;
  467. }
  468. template <typename t1>
  469. void delObj(t1 *a1)
  470. {
  471. delete a1;
  472. }
  473. template <typename t1, typename t2>
  474. void assign(t1 &a1, const t2 &a2)
  475. {
  476. a1 = a2;
  477. }
  478. template <typename t1, typename t2>
  479. struct assigner
  480. {
  481. public:
  482. t1 &op1;
  483. t2 op2;
  484. assigner(t1 &a1, const t2 & a2)
  485. :op1(a1), op2(a2)
  486. {}
  487. void operator()()
  488. {
  489. op1 = op2;
  490. }
  491. };
  492. template <typename t1, typename t2>
  493. assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
  494. {
  495. return assigner<t1,t2>(a1,a2);
  496. }
  497. template <typename t1, typename t2, typename t3>
  498. bool equal(const t1 &a1, const t3 t1::* point, const t2 &a2)
  499. {
  500. return a1.*point == a2;
  501. }
  502. template <typename t1, typename t2>
  503. bool equal(const t1 &a1, const t2 &a2)
  504. {
  505. return a1 == a2;
  506. }
  507. }
  508. using vstd::operator-=;
  509. template <typename t1, typename t2>
  510. t1 & amax(t1 &a, const t2 &b) //assigns greater of (a, b) to a and returns maximum of (a, b)
  511. {
  512. if(a >= b)
  513. return a;
  514. else
  515. {
  516. a = b;
  517. return a;
  518. }
  519. }
  520. template <typename t1, typename t2>
  521. t1 & amin(t1 &a, const t2 &b) //assigns smaller of (a, b) to a and returns minimum of (a, b)
  522. {
  523. if(a <= b)
  524. return a;
  525. else
  526. {
  527. a = b;
  528. return a;
  529. }
  530. }
  531. template <typename t1, typename t2, typename t3>
  532. t1 & abetw(t1 &a, const t2 &b, const t3 &c) //makes a to fit the range <b, c>
  533. {
  534. amax(a,b);
  535. amin(a,c);
  536. return a;
  537. }
  538. template <typename t1, typename t2, typename t3>
  539. bool isbetw(const t1 &a, const t2 &b, const t3 &c) //checks if a is between b and c
  540. {
  541. return a > b && a < c;
  542. }
  543. template <typename t1, typename t2, typename t3>
  544. bool iswith(const t1 &a, const t2 &b, const t3 &c) //checks if a is within b and c
  545. {
  546. return a >= b && a <= c;
  547. }
  548. template <typename T>
  549. void delNull(T* &ptr) //deleted pointer and sets it to NULL
  550. {
  551. delete ptr;
  552. ptr = NULL;
  553. }
  554. #include "CConsoleHandler.h"
  555. extern DLL_EXPORT std::ostream *logfile;
  556. extern DLL_EXPORT CConsoleHandler *console;
  557. class CLogger //logger, prints log info to console and saves in file
  558. {
  559. const int lvl;
  560. public:
  561. CLogger& operator<<(std::ostream& (*fun)(std::ostream&))
  562. {
  563. if(lvl < CONSOLE_LOGGING_LEVEL)
  564. std::cout << fun;
  565. if((lvl < FILE_LOGGING_LEVEL) && logfile)
  566. *logfile << fun;
  567. return *this;
  568. }
  569. template<typename T>
  570. CLogger & operator<<(const T & data)
  571. {
  572. if(lvl < CONSOLE_LOGGING_LEVEL)
  573. {
  574. if(console)
  575. console->print(data,lvl);
  576. else
  577. std::cout << data << std::flush;
  578. }
  579. if((lvl < FILE_LOGGING_LEVEL) && logfile)
  580. *logfile << data << std::flush;
  581. return *this;
  582. }
  583. CLogger(const int Lvl) : lvl(Lvl) {}
  584. };
  585. extern DLL_EXPORT CLogger tlog0; //green - standard progress info
  586. extern DLL_EXPORT CLogger tlog1; //red - big errors
  587. extern DLL_EXPORT CLogger tlog2; //magenta - major warnings
  588. extern DLL_EXPORT CLogger tlog3; //yellow - minor warnings
  589. extern DLL_EXPORT CLogger tlog4; //white - detailed log info
  590. extern DLL_EXPORT CLogger tlog5; //gray - minor log info
  591. extern DLL_EXPORT CLogger tlog6; //teal - AI info
  592. //XXX pls dont - 'debug macros' are usually more trouble than it's worth
  593. #define HANDLE_EXCEPTION \
  594. catch (const std::exception& e) { \
  595. tlog1 << e.what() << std::endl; \
  596. throw; \
  597. } \
  598. catch (const std::exception * e) \
  599. { \
  600. tlog1 << e->what()<< std::endl; \
  601. throw; \
  602. } \
  603. catch (const std::string& e) { \
  604. tlog1 << e << std::endl; \
  605. throw; \
  606. }
  607. #define HANDLE_EXCEPTIONC(COMMAND) \
  608. catch (const std::exception& e) { \
  609. COMMAND; \
  610. tlog1 << e.what() << std::endl; \
  611. throw; \
  612. } \
  613. catch (const std::string &e) \
  614. { \
  615. COMMAND; \
  616. tlog1 << e << std::endl; \
  617. throw; \
  618. }
  619. #if defined(linux) && defined(sparc)
  620. /* SPARC does not support unaligned memory access. Let gcc know when
  621. * to emit the right code. */
  622. struct unaligned_Uint16 { ui16 val __attribute__(( packed )); };
  623. struct unaligned_Uint32 { ui32 val __attribute__(( packed )); };
  624. static inline ui16 read_unaligned_u16(const void *p)
  625. {
  626. const struct unaligned_Uint16 *v = (const struct unaligned_Uint16 *)p;
  627. return v->val;
  628. }
  629. static inline ui32 read_unaligned_u32(const void *p)
  630. {
  631. const struct unaligned_Uint32 *v = (const struct unaligned_Uint32 *)p;
  632. return v->val;
  633. }
  634. #else
  635. #define read_unaligned_u16(p) (* reinterpret_cast<const Uint16 *>(p))
  636. #define read_unaligned_u32(p) (* reinterpret_cast<const Uint32 *>(p))
  637. #endif
  638. //for explicit overrides
  639. #ifdef _MSC_VER
  640. #define OVERRIDE override
  641. #else
  642. #define OVERRIDE //is there any working counterpart?
  643. #endif
  644. #define BONUS_TREE_DESERIALIZATION_FIX if(!h.saving && h.smartPointerSerialization) deserializationFix();
  645. #endif // __GLOBAL_H__