global.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #ifndef GLOBAL_H
  2. #define GLOBAL_H
  3. #include <iostream>
  4. #include <boost/logic/tribool.hpp>
  5. #include <boost/cstdint.hpp>
  6. typedef boost::uint64_t ui64; //unsigned int 64 bits (8 bytes)
  7. typedef boost::uint32_t ui32; //unsigned int 32 bits (4 bytes)
  8. typedef boost::uint16_t ui16; //unsigned int 16 bits (2 bytes)
  9. typedef boost::uint8_t ui8; //unsigned int 8 bits (1 byte)
  10. typedef boost::int64_t si64; //signed int 64 bits (8 bytes)
  11. typedef boost::int32_t si32; //signed int 32 bits (4 bytes)
  12. typedef boost::int16_t si16; //signed int 16 bits (2 bytes)
  13. typedef boost::int8_t si8; //signed int 8 bits (1 byte)
  14. #include "int3.h"
  15. #define CHECKTIME 1
  16. #if CHECKTIME
  17. #include "timeHandler.h"
  18. #define THC
  19. #endif
  20. #define NAME_VER ("VCMI 0.65")
  21. #define CONSOLE_LOGGING_LEVEL 5
  22. #define FILE_LOGGING_LEVEL 6
  23. #ifdef _WIN32
  24. #define PATHSEPARATOR "\\"
  25. #define DATA_DIR ""
  26. #define SERVER_NAME "VCMI_server.exe"
  27. #else
  28. #define PATHSEPARATOR "/"
  29. #define DATA_DIR ""
  30. #define SERVER_NAME "./vcmiserver"
  31. #endif
  32. enum Ecolor {RED, BLUE, TAN, GREEN, ORANGE, PURPLE, TEAL, PINK}; //player's colors
  33. enum EterrainType {border=-1, dirt, sand, grass, snow, swamp, rough, subterranean, lava, water, rock};
  34. enum Eriver {noRiver=0, clearRiver, icyRiver, muddyRiver, lavaRiver};
  35. enum Eroad {dirtRoad=1, grazvelRoad, cobblestoneRoad};
  36. enum Eformat { WoG=0x33, AB=0x15, RoE=0x0e, SoD=0x1c};
  37. enum EvictoryConditions {artifact, gatherTroop, gatherResource, buildCity, buildGrail, beatHero,
  38. captureCity, beatMonster, takeDwellings, takeMines, transportItem, winStandard=255};
  39. enum ElossCon {lossCastle, lossHero, timeExpires, lossStandard=255};
  40. enum EHeroClasses {HERO_KNIGHT, HERO_CLERIC, HERO_RANGER, HERO_DRUID, HERO_ALCHEMIST, HERO_WIZARD,
  41. HERO_DEMONIAC, HERO_HERETIC, HERO_DEATHKNIGHT, HERO_NECROMANCER, HERO_WARLOCK, HERO_OVERLORD,
  42. HERO_BARBARIAN, HERO_BATTLEMAGE, HERO_BEASTMASTER, HERO_WITCH, HERO_PLANESWALKER, HERO_ELEMENTALIST};
  43. enum EAbilities {DOUBLE_WIDE, FLYING, SHOOTER, TWO_HEX_ATTACK, SIEGE_ABILITY, SIEGE_WEAPON,
  44. KING1, KING2, KING3, MIND_IMMUNITY, NO_OBSTACLE_PENALTY, NO_CLOSE_COMBAT_PENALTY,
  45. JOUSTING, FIRE_IMMUNITY, TWICE_ATTACK, NO_ENEMY_RETALIATION, NO_MORAL_PENALTY,
  46. UNDEAD, MULTI_HEAD_ATTACK, EXTENDED_RADIOUS_SHOOTER, GHOST, RAISES_MORALE,
  47. LOWERS_MORALE, DRAGON, STRIKE_AND_RETURN, FEARLESS, REBIRTH}; //some flags are used only for battles
  48. enum ECombatInfo{ALIVE = REBIRTH+1, SUMMONED, CLONED, HAD_MORALE, WAITING, MOVED, DEFENDING};
  49. class CGameInfo;
  50. extern CGameInfo* CGI;
  51. #define HEROI_TYPE (34)
  52. #define TOWNI_TYPE (98)
  53. const int F_NUMBER = 9; //factions (town types) quantity
  54. const int PLAYER_LIMIT = 8; //player limit per map
  55. const int HEROES_PER_TYPE=8; //amount of heroes of each type
  56. const int SKILL_QUANTITY=28;
  57. const int SKILL_PER_HERO=8;
  58. const int ARTIFACTS_QUANTITY=171;
  59. const int HEROES_QUANTITY=156;
  60. const int SPELLS_QUANTITY=70;
  61. const int RESOURCE_QUANTITY=8;
  62. const int TERRAIN_TYPES=10;
  63. const int PRIMARY_SKILLS=4;
  64. const int NEUTRAL_PLAYER=255;
  65. const int NAMES_PER_TOWN=16;
  66. const int CREATURES_PER_TOWN = 7; //without upgrades
  67. const int MAX_BUILDING_PER_TURN = 1;
  68. const int SPELL_LEVELS = 5;
  69. #define BFIELD_WIDTH (17)
  70. #define BFIELD_HEIGHT (11)
  71. #define BFIELD_SIZE ((BFIELD_WIDTH) * (BFIELD_HEIGHT))
  72. //uncomment to make it work
  73. //#define MARK_BLOCKED_POSITIONS
  74. //#define MARK_VISITABLE_POSITIONS
  75. #define DEFBYPASS
  76. #ifdef _WIN32
  77. #ifdef VCMI_DLL
  78. #define DLL_EXPORT __declspec(dllexport)
  79. #else
  80. #define DLL_EXPORT __declspec(dllimport)
  81. #endif
  82. #else
  83. #if defined(__GNUC__) && __GNUC__ >= 4
  84. #define DLL_EXPORT __attribute__ ((visibility("default")))
  85. #else
  86. #define DLL_EXPORT
  87. #endif
  88. #endif
  89. namespace vstd
  90. {
  91. template <typename Container, typename Item>
  92. bool contains(const Container & c, const Item &i)
  93. {
  94. return std::find(c.begin(),c.end(),i) != c.end();
  95. }
  96. template <typename V, typename Item, typename Item2>
  97. bool contains(const std::map<Item,V> & c, const Item2 &i)
  98. {
  99. return c.find(i)!=c.end();
  100. }
  101. template <typename Container1, typename Container2>
  102. typename Container2::iterator findFirstNot(Container1 &c1, Container2 &c2)//returns first element of c2 not present in c1
  103. {
  104. typename Container2::iterator itr = c2.begin();
  105. while(itr != c2.end())
  106. if(!contains(c1,*itr))
  107. return itr;
  108. else
  109. ++itr;
  110. return c2.end();
  111. }
  112. template <typename Container, typename Item>
  113. typename Container::iterator find(const Container & c, const Item &i)
  114. {
  115. return std::find(c.begin(),c.end(),i);
  116. }
  117. template <typename T1, typename T2>
  118. int findPos(const std::vector<T1> & c, const T2 &s)
  119. {
  120. for(int i=0;i<c.size();i++)
  121. if(c[i] == s)
  122. return i;
  123. return -1;
  124. }
  125. template <typename T1, typename T2, typename Func>
  126. int findPos(const std::vector<T1> & c, const T2 &s, const Func &f) //Func(T1,T2) must say if these elements matches
  127. {
  128. for(int i=0;i<c.size();i++)
  129. if(f(c[i],s))
  130. return i;
  131. return -1;
  132. }
  133. template <typename Container, typename Item>
  134. typename Container::iterator find(Container & c, const Item &i)
  135. {
  136. return std::find(c.begin(),c.end(),i);
  137. }
  138. template <typename Container, typename Item>
  139. bool operator-=(Container &c, const Item &i)
  140. {
  141. typename Container::iterator itr = find(c,i);
  142. if(itr == c.end())
  143. return false;
  144. c.erase(itr);
  145. return true;
  146. }
  147. template <typename t1>
  148. void delObj(t1 *a1)
  149. {
  150. delete a1;
  151. }
  152. template <typename t1, typename t2>
  153. void assign(t1 &a1, const t2 &a2)
  154. {
  155. a1 = a2;
  156. }
  157. template <typename t1, typename t2>
  158. struct assigner
  159. {
  160. public:
  161. t1 &op1;
  162. t2 op2;
  163. assigner(t1 &a1, t2 a2)
  164. :op1(a1), op2(a2)
  165. {}
  166. void operator()()
  167. {
  168. op1 = op2;
  169. }
  170. };
  171. template <typename t1, typename t2>
  172. assigner<t1,t2> assigno(t1 &a1, const t2 &a2)
  173. {
  174. return assigner<t1,t2>(a1,a2);
  175. }
  176. template <typename t1, typename t2, typename t3>
  177. bool equal(const t1 &a1, const t3 t1::* point, const t2 &a2)
  178. {
  179. return a1.*point == a2;
  180. }
  181. template <typename t1, typename t2>
  182. bool equal(const t1 &a1, const t2 &a2)
  183. {
  184. return a1 == a2;
  185. }
  186. }
  187. using vstd::operator-=;
  188. #include "CConsoleHandler.h"
  189. extern DLL_EXPORT std::ostream *logfile;
  190. extern DLL_EXPORT CConsoleHandler *console;
  191. template <int lvl> class CLogger
  192. {
  193. public:
  194. CLogger<lvl>& operator<<(std::ostream& (*fun)(std::ostream&))
  195. {
  196. if(lvl < CONSOLE_LOGGING_LEVEL)
  197. std::cout << fun;
  198. if((lvl < FILE_LOGGING_LEVEL) && logfile)
  199. *logfile << fun;
  200. return *this;
  201. }
  202. template<typename T>
  203. CLogger<lvl> & operator<<(const T & data)
  204. {
  205. if(lvl < CONSOLE_LOGGING_LEVEL)
  206. if(console)
  207. console->print(data,lvl);
  208. else
  209. std::cout << data << std::flush;
  210. if((lvl < FILE_LOGGING_LEVEL) && logfile)
  211. *logfile << data << std::flush;
  212. return *this;
  213. }
  214. };
  215. extern DLL_EXPORT CLogger<0> tlog0; //green - standard progress info
  216. extern DLL_EXPORT CLogger<1> tlog1; //red - big errors
  217. extern DLL_EXPORT CLogger<2> tlog2; //magenta - major warnings
  218. extern DLL_EXPORT CLogger<3> tlog3; //yellow - minor warnings
  219. extern DLL_EXPORT CLogger<4> tlog4; //white - detailed log info
  220. extern DLL_EXPORT CLogger<5> tlog5; //gray - minor log info
  221. #define HANDLE_EXCEPTION \
  222. catch (const std::exception& e) { \
  223. tlog1 << e.what() << std::endl; \
  224. } \
  225. catch (const std::exception * e) \
  226. { \
  227. tlog1 << e->what()<< std::endl; \
  228. delete e; \
  229. } \
  230. catch (const std::string& e) { \
  231. tlog1 << e << std::endl; \
  232. }
  233. #define HANDLE_EXCEPTIONC(COMMAND) \
  234. catch (const std::exception& e) { \
  235. COMMAND; \
  236. tlog1 << e.what() << std::endl; \
  237. } \
  238. catch (const std::exception * e) \
  239. { \
  240. COMMAND; \
  241. tlog1 << e->what()<< std::endl; \
  242. delete e; \
  243. }
  244. #endif //GLOBAL_H