NetPacksBase.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * NetPacksBase.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. class CClient;
  12. class CGameState;
  13. class CGameHandler;
  14. class CConnection;
  15. class CStackBasicDescriptor;
  16. class CGHeroInstance;
  17. class CStackInstance;
  18. class CArmedInstance;
  19. class CArtifactSet;
  20. class CBonusSystemNode;
  21. struct ArtSlotInfo;
  22. class BattleInfo;
  23. #include "ConstTransitivePtr.h"
  24. #include "GameConstants.h"
  25. #include "JsonNode.h"
  26. struct DLL_LINKAGE CPack
  27. {
  28. CPack() {};
  29. virtual ~CPack() {};
  30. template <typename Handler> void serialize(Handler &h, const int version)
  31. {
  32. logNetwork->error("CPack serialized... this should not happen!");
  33. assert(false && "CPack serialized");
  34. }
  35. void applyGs(CGameState * gs)
  36. {}
  37. };
  38. struct CPackForClient : public CPack
  39. {
  40. CPackForClient(){};
  41. CGameState* GS(CClient *cl);
  42. void applyFirstCl(CClient *cl)//called before applying to gs
  43. {}
  44. void applyCl(CClient *cl)//called after applying to gs
  45. {}
  46. };
  47. struct CPackForServer : public CPack
  48. {
  49. PlayerColor player;
  50. CConnection *c;
  51. CGameState* GS(CGameHandler *gh);
  52. CPackForServer():
  53. player(PlayerColor::NEUTRAL),
  54. c(nullptr)
  55. {
  56. }
  57. bool applyGh(CGameHandler *gh) //called after applying to gs
  58. {
  59. logGlobal->error("Should not happen... applying plain CPackForServer");
  60. return false;
  61. }
  62. protected:
  63. void throwNotAllowedAction();
  64. void throwOnWrongOwner(CGameHandler * gh, ObjectInstanceID id);
  65. void throwOnWrongPlayer(CGameHandler * gh, PlayerColor player);
  66. void throwAndCompain(CGameHandler * gh, std::string txt);
  67. bool isPlayerOwns(CGameHandler * gh, ObjectInstanceID id);
  68. private:
  69. void wrongPlayerMessage(CGameHandler * gh, PlayerColor expectedplayer);
  70. };
  71. struct DLL_LINKAGE MetaString
  72. {
  73. private:
  74. enum EMessage {TEXACT_STRING, TLOCAL_STRING, TNUMBER, TREPLACE_ESTRING, TREPLACE_LSTRING, TREPLACE_NUMBER, TREPLACE_PLUSNUMBER};
  75. public:
  76. enum {GENERAL_TXT=1, XTRAINFO_TXT, OBJ_NAMES, RES_NAMES, ART_NAMES, ARRAY_TXT, CRE_PL_NAMES, CREGENS, MINE_NAMES,
  77. MINE_EVNTS, ADVOB_TXT, ART_EVNTS, SPELL_NAME, SEC_SKILL_NAME, CRE_SING_NAMES, CREGENS4, COLOR, ART_DESCR, JK_TXT};
  78. std::vector<ui8> message; //vector of EMessage
  79. std::vector<std::pair<ui8,ui32> > localStrings;
  80. std::vector<std::string> exactStrings;
  81. std::vector<si32> numbers;
  82. template <typename Handler> void serialize(Handler &h, const int version)
  83. {
  84. h & exactStrings;
  85. h & localStrings;
  86. h & message;
  87. h & numbers;
  88. }
  89. void addTxt(ui8 type, ui32 serial)
  90. {
  91. message.push_back(TLOCAL_STRING);
  92. localStrings.push_back(std::pair<ui8,ui32>(type, serial));
  93. }
  94. MetaString& operator<<(const std::pair<ui8,ui32> &txt)
  95. {
  96. message.push_back(TLOCAL_STRING);
  97. localStrings.push_back(txt);
  98. return *this;
  99. }
  100. MetaString& operator<<(const std::string &txt)
  101. {
  102. message.push_back(TEXACT_STRING);
  103. exactStrings.push_back(txt);
  104. return *this;
  105. }
  106. MetaString& operator<<(int txt)
  107. {
  108. message.push_back(TNUMBER);
  109. numbers.push_back(txt);
  110. return *this;
  111. }
  112. void addReplacement(ui8 type, ui32 serial)
  113. {
  114. message.push_back(TREPLACE_LSTRING);
  115. localStrings.push_back(std::pair<ui8,ui32>(type, serial));
  116. }
  117. void addReplacement(const std::string &txt)
  118. {
  119. message.push_back(TREPLACE_ESTRING);
  120. exactStrings.push_back(txt);
  121. }
  122. void addReplacement(int txt)
  123. {
  124. message.push_back(TREPLACE_NUMBER);
  125. numbers.push_back(txt);
  126. }
  127. void addReplacement2(int txt)
  128. {
  129. message.push_back(TREPLACE_PLUSNUMBER);
  130. numbers.push_back(txt);
  131. }
  132. void addCreReplacement(CreatureID id, TQuantity count); //adds sing or plural name;
  133. void addReplacement(const CStackBasicDescriptor &stack); //adds sing or plural name;
  134. std::string buildList () const;
  135. void clear()
  136. {
  137. exactStrings.clear();
  138. localStrings.clear();
  139. message.clear();
  140. numbers.clear();
  141. }
  142. void toString(std::string &dst) const;
  143. std::string toString() const;
  144. void getLocalString(const std::pair<ui8,ui32> &txt, std::string &dst) const;
  145. MetaString(){}
  146. };
  147. struct Component
  148. {
  149. enum EComponentType {PRIM_SKILL, SEC_SKILL, RESOURCE, CREATURE, ARTIFACT, EXPERIENCE, SPELL, MORALE, LUCK, BUILDING, HERO_PORTRAIT, FLAG};
  150. ui16 id, subtype; //id uses ^^^ enums, when id==EXPPERIENCE subtype==0 means exp points and subtype==1 levels)
  151. si32 val; // + give; - take
  152. si16 when; // 0 - now; +x - within x days; -x - per x days
  153. template <typename Handler> void serialize(Handler &h, const int version)
  154. {
  155. h & id;
  156. h & subtype;
  157. h & val;
  158. h & when;
  159. }
  160. Component()
  161. :id(0), subtype(0), val(0), when(0)
  162. {
  163. }
  164. DLL_LINKAGE explicit Component(const CStackBasicDescriptor &stack);
  165. Component(Component::EComponentType Type, ui16 Subtype, si32 Val, si16 When)
  166. :id(Type),subtype(Subtype),val(Val),when(When)
  167. {
  168. }
  169. };
  170. typedef boost::variant<ConstTransitivePtr<CGHeroInstance>, ConstTransitivePtr<CStackInstance> > TArtHolder;
  171. struct ArtifactLocation
  172. {
  173. TArtHolder artHolder;
  174. ArtifactPosition slot;
  175. ArtifactLocation()
  176. {
  177. artHolder = ConstTransitivePtr<CGHeroInstance>();
  178. slot = ArtifactPosition::PRE_FIRST;
  179. }
  180. template <typename T>
  181. ArtifactLocation(const T *ArtHolder, ArtifactPosition Slot)
  182. {
  183. artHolder = const_cast<T*>(ArtHolder); //we are allowed here to const cast -> change will go through one of our packages... do not abuse!
  184. slot = Slot;
  185. }
  186. ArtifactLocation(TArtHolder ArtHolder, ArtifactPosition Slot)
  187. {
  188. artHolder = ArtHolder;
  189. slot = Slot;
  190. }
  191. template <typename T>
  192. bool isHolder(const T *t) const
  193. {
  194. if(auto ptrToT = boost::get<ConstTransitivePtr<T> >(&artHolder))
  195. {
  196. return ptrToT->get() == t;
  197. }
  198. return false;
  199. }
  200. DLL_LINKAGE void removeArtifact(); // BE CAREFUL, this operation modifies holder (gs)
  201. DLL_LINKAGE const CArmedInstance *relatedObj() const; //hero or the stack owner
  202. DLL_LINKAGE PlayerColor owningPlayer() const;
  203. DLL_LINKAGE CArtifactSet *getHolderArtSet();
  204. DLL_LINKAGE CBonusSystemNode *getHolderNode();
  205. DLL_LINKAGE const CArtifactSet *getHolderArtSet() const;
  206. DLL_LINKAGE const CBonusSystemNode *getHolderNode() const;
  207. DLL_LINKAGE const CArtifactInstance *getArt() const;
  208. DLL_LINKAGE CArtifactInstance *getArt();
  209. DLL_LINKAGE const ArtSlotInfo *getSlot() const;
  210. template <typename Handler> void serialize(Handler &h, const int version)
  211. {
  212. h & artHolder;
  213. h & slot;
  214. }
  215. };
  216. ///custom effect (resistance, reflection, etc)
  217. struct CustomEffectInfo
  218. {
  219. CustomEffectInfo()
  220. :effect(0),
  221. sound(0),
  222. stack(0)
  223. {
  224. }
  225. /// WoG AC format
  226. ui32 effect;
  227. ui32 sound;
  228. ui32 stack;
  229. template <typename Handler> void serialize(Handler & h, const int version)
  230. {
  231. h & effect;
  232. h & sound;
  233. h & stack;
  234. }
  235. };
  236. class BattleChanges
  237. {
  238. public:
  239. enum class EOperation : si8
  240. {
  241. ADD,
  242. RESET_STATE,
  243. UPDATE,
  244. REMOVE
  245. };
  246. JsonNode data;
  247. EOperation operation;
  248. BattleChanges()
  249. : operation(EOperation::RESET_STATE),
  250. data()
  251. {
  252. }
  253. BattleChanges(EOperation operation_)
  254. : operation(operation_),
  255. data()
  256. {
  257. }
  258. };
  259. class UnitChanges : public BattleChanges
  260. {
  261. public:
  262. uint32_t id;
  263. int64_t healthDelta;
  264. UnitChanges()
  265. : BattleChanges(EOperation::RESET_STATE),
  266. id(0),
  267. healthDelta(0)
  268. {
  269. }
  270. UnitChanges(uint32_t id_, EOperation operation_)
  271. : BattleChanges(operation_),
  272. id(id_),
  273. healthDelta(0)
  274. {
  275. }
  276. template <typename Handler> void serialize(Handler & h, const int version)
  277. {
  278. h & id;
  279. h & healthDelta;
  280. h & data;
  281. h & operation;
  282. }
  283. };
  284. class ObstacleChanges : public BattleChanges
  285. {
  286. public:
  287. uint32_t id;
  288. ObstacleChanges()
  289. : BattleChanges(EOperation::RESET_STATE),
  290. id(0)
  291. {
  292. }
  293. ObstacleChanges(uint32_t id_, EOperation operation_)
  294. : BattleChanges(operation_),
  295. id(id_)
  296. {
  297. }
  298. template <typename Handler> void serialize(Handler & h, const int version)
  299. {
  300. h & id;
  301. h & data;
  302. h & operation;
  303. }
  304. };