NetPacksBase.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. class CLobbyScreen;
  18. class CServerHandler;
  19. class CVCMIServer;
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. class CGameState;
  22. class CConnection;
  23. class CStackBasicDescriptor;
  24. class CGHeroInstance;
  25. class CStackInstance;
  26. class CArmedInstance;
  27. class CArtifactSet;
  28. class CBonusSystemNode;
  29. struct ArtSlotInfo;
  30. class ICPackVisitor;
  31. struct DLL_LINKAGE CPack
  32. {
  33. std::shared_ptr<CConnection> c; // Pointer to connection that pack received from
  34. CPack() = default;
  35. virtual ~CPack() = default;
  36. template <typename Handler> void serialize(Handler &h, const int version)
  37. {
  38. logNetwork->error("CPack serialized... this should not happen!");
  39. assert(false && "CPack serialized");
  40. }
  41. void applyGs(CGameState * gs)
  42. {}
  43. void visit(ICPackVisitor & cpackVisitor);
  44. protected:
  45. /// <summary>
  46. /// For basic types of netpacks hierarchy like CPackForClient. Called first.
  47. /// </summary>
  48. virtual void visitBasic(ICPackVisitor & cpackVisitor);
  49. /// <summary>
  50. /// For leaf types of netpacks hierarchy. Called after visitBasic.
  51. /// </summary>
  52. virtual void visitTyped(ICPackVisitor & cpackVisitor);
  53. };
  54. struct DLL_LINKAGE CPackForClient : public CPack
  55. {
  56. protected:
  57. virtual void visitBasic(ICPackVisitor & cpackVisitor) override;
  58. };
  59. struct DLL_LINKAGE CPackForServer : public CPack
  60. {
  61. mutable PlayerColor player = PlayerColor::NEUTRAL;
  62. mutable si32 requestID;
  63. template <typename Handler> void serialize(Handler &h, const int version)
  64. {
  65. h & player;
  66. h & requestID;
  67. }
  68. protected:
  69. virtual void visitBasic(ICPackVisitor & cpackVisitor) override;
  70. };
  71. struct DLL_LINKAGE CPackForLobby : public CPack
  72. {
  73. virtual bool isForServer() const;
  74. protected:
  75. virtual void visitBasic(ICPackVisitor & cpackVisitor) override;
  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, 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.emplace_back(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.emplace_back(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(const 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. };
  152. struct Component
  153. {
  154. enum EComponentType {PRIM_SKILL, SEC_SKILL, RESOURCE, CREATURE, ARTIFACT, EXPERIENCE, SPELL, MORALE, LUCK, BUILDING, HERO_PORTRAIT, FLAG};
  155. ui16 id = 0, subtype = 0; //id uses ^^^ enums, when id==EXPPERIENCE subtype==0 means exp points and subtype==1 levels)
  156. si32 val = 0; // + give; - take
  157. si16 when = 0; // 0 - now; +x - within x days; -x - per x days
  158. template <typename Handler> void serialize(Handler &h, const int version)
  159. {
  160. h & id;
  161. h & subtype;
  162. h & val;
  163. h & when;
  164. }
  165. Component() = default;
  166. DLL_LINKAGE explicit Component(const CStackBasicDescriptor &stack);
  167. Component(Component::EComponentType Type, ui16 Subtype, si32 Val, si16 When)
  168. :id(Type),subtype(Subtype),val(Val),when(When)
  169. {
  170. }
  171. };
  172. using TArtHolder = boost::variant<ConstTransitivePtr<CGHeroInstance>, ConstTransitivePtr<CStackInstance>>;
  173. struct ArtifactLocation
  174. {
  175. TArtHolder artHolder;//TODO: identify holder by id
  176. ArtifactPosition slot = ArtifactPosition::PRE_FIRST;
  177. ArtifactLocation()
  178. : artHolder(ConstTransitivePtr<CGHeroInstance>())
  179. {
  180. }
  181. template<typename T>
  182. ArtifactLocation(const T * ArtHolder, ArtifactPosition Slot)
  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. }
  187. ArtifactLocation(TArtHolder ArtHolder, const ArtifactPosition & Slot)
  188. : artHolder(std::move(std::move(ArtHolder)))
  189. , slot(Slot)
  190. {
  191. }
  192. template <typename T>
  193. bool isHolder(const T *t) const
  194. {
  195. if(auto ptrToT = boost::get<ConstTransitivePtr<T> >(&artHolder))
  196. {
  197. return ptrToT->get() == t;
  198. }
  199. return false;
  200. }
  201. DLL_LINKAGE void removeArtifact(); // BE CAREFUL, this operation modifies holder (gs)
  202. DLL_LINKAGE const CArmedInstance *relatedObj() const; //hero or the stack owner
  203. DLL_LINKAGE PlayerColor owningPlayer() const;
  204. DLL_LINKAGE CArtifactSet *getHolderArtSet();
  205. DLL_LINKAGE CBonusSystemNode *getHolderNode();
  206. DLL_LINKAGE const CArtifactSet *getHolderArtSet() const;
  207. DLL_LINKAGE const CBonusSystemNode *getHolderNode() const;
  208. DLL_LINKAGE const CArtifactInstance *getArt() const;
  209. DLL_LINKAGE CArtifactInstance *getArt();
  210. DLL_LINKAGE const ArtSlotInfo *getSlot() const;
  211. template <typename Handler> void serialize(Handler &h, const int version)
  212. {
  213. h & artHolder;
  214. h & slot;
  215. }
  216. };
  217. class EntityChanges
  218. {
  219. public:
  220. Metatype metatype = Metatype::UNKNOWN;
  221. int32_t entityIndex = 0;
  222. JsonNode data;
  223. template <typename Handler> void serialize(Handler & h, const int version)
  224. {
  225. h & metatype;
  226. h & entityIndex;
  227. h & data;
  228. }
  229. };
  230. class BattleChanges
  231. {
  232. public:
  233. enum class EOperation : si8
  234. {
  235. ADD,
  236. RESET_STATE,
  237. UPDATE,
  238. REMOVE,
  239. ACTIVATE_AND_UPDATE,
  240. ACTIVATE_AND_REMOVE
  241. };
  242. JsonNode data;
  243. EOperation operation = EOperation::RESET_STATE;
  244. BattleChanges() = default;
  245. BattleChanges(EOperation operation_)
  246. : operation(operation_)
  247. {
  248. }
  249. };
  250. class UnitChanges : public BattleChanges
  251. {
  252. public:
  253. uint32_t id = 0;
  254. int64_t healthDelta = 0;
  255. UnitChanges() = default;
  256. UnitChanges(uint32_t id_, EOperation operation_)
  257. : BattleChanges(operation_)
  258. , id(id_)
  259. {
  260. }
  261. template <typename Handler> void serialize(Handler & h, const int version)
  262. {
  263. h & id;
  264. h & healthDelta;
  265. h & data;
  266. h & operation;
  267. }
  268. };
  269. class ObstacleChanges : public BattleChanges
  270. {
  271. public:
  272. uint32_t id = 0;
  273. ObstacleChanges() = default;
  274. ObstacleChanges(uint32_t id_, EOperation operation_)
  275. : BattleChanges(operation_),
  276. id(id_)
  277. {
  278. }
  279. template <typename Handler> void serialize(Handler & h, const int version)
  280. {
  281. h & id;
  282. h & data;
  283. h & operation;
  284. }
  285. };
  286. VCMI_LIB_NAMESPACE_END