global.h 20 KB

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