global.h 19 KB

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