NetPacksBase.h 8.5 KB

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