NetPacksBase.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. enum class EInfoWindowMode : uint8_t
  32. {
  33. AUTO,
  34. MODAL,
  35. INFO
  36. };
  37. struct DLL_LINKAGE CPack
  38. {
  39. std::shared_ptr<CConnection> c; // Pointer to connection that pack received from
  40. CPack() = default;
  41. virtual ~CPack() = default;
  42. template <typename Handler> void serialize(Handler &h, const int version)
  43. {
  44. logNetwork->error("CPack serialized... this should not happen!");
  45. assert(false && "CPack serialized");
  46. }
  47. void applyGs(CGameState * gs)
  48. {}
  49. void visit(ICPackVisitor & cpackVisitor);
  50. protected:
  51. /// <summary>
  52. /// For basic types of netpacks hierarchy like CPackForClient. Called first.
  53. /// </summary>
  54. virtual void visitBasic(ICPackVisitor & cpackVisitor);
  55. /// <summary>
  56. /// For leaf types of netpacks hierarchy. Called after visitBasic.
  57. /// </summary>
  58. virtual void visitTyped(ICPackVisitor & cpackVisitor);
  59. };
  60. struct DLL_LINKAGE CPackForClient : public CPack
  61. {
  62. protected:
  63. virtual void visitBasic(ICPackVisitor & cpackVisitor) override;
  64. };
  65. struct DLL_LINKAGE CPackForServer : public CPack
  66. {
  67. mutable PlayerColor player = PlayerColor::NEUTRAL;
  68. mutable si32 requestID;
  69. template <typename Handler> void serialize(Handler &h, const int version)
  70. {
  71. h & player;
  72. h & requestID;
  73. }
  74. protected:
  75. virtual void visitBasic(ICPackVisitor & cpackVisitor) override;
  76. };
  77. struct DLL_LINKAGE CPackForLobby : public CPack
  78. {
  79. virtual bool isForServer() const;
  80. protected:
  81. virtual void visitBasic(ICPackVisitor & cpackVisitor) override;
  82. };
  83. struct DLL_LINKAGE MetaString
  84. {
  85. private:
  86. enum EMessage {TEXACT_STRING, TLOCAL_STRING, TNUMBER, TREPLACE_ESTRING, TREPLACE_LSTRING, TREPLACE_NUMBER, TREPLACE_PLUSNUMBER};
  87. public:
  88. enum {GENERAL_TXT=1, OBJ_NAMES, RES_NAMES, ART_NAMES, ARRAY_TXT, CRE_PL_NAMES, CREGENS, MINE_NAMES,
  89. MINE_EVNTS, ADVOB_TXT, ART_EVNTS, SPELL_NAME, SEC_SKILL_NAME, CRE_SING_NAMES, CREGENS4, COLOR, ART_DESCR, JK_TXT};
  90. std::vector<ui8> message; //vector of EMessage
  91. std::vector<std::pair<ui8,ui32> > localStrings;
  92. std::vector<std::string> exactStrings;
  93. std::vector<int64_t> numbers;
  94. template <typename Handler> void serialize(Handler & h, const int version)
  95. {
  96. h & exactStrings;
  97. h & localStrings;
  98. h & message;
  99. h & numbers;
  100. }
  101. void addTxt(ui8 type, ui32 serial)
  102. {
  103. message.push_back(TLOCAL_STRING);
  104. localStrings.emplace_back(type, serial);
  105. }
  106. MetaString& operator<<(const std::pair<ui8,ui32> &txt)
  107. {
  108. message.push_back(TLOCAL_STRING);
  109. localStrings.push_back(txt);
  110. return *this;
  111. }
  112. MetaString& operator<<(const std::string &txt)
  113. {
  114. message.push_back(TEXACT_STRING);
  115. exactStrings.push_back(txt);
  116. return *this;
  117. }
  118. MetaString& operator<<(int64_t txt)
  119. {
  120. message.push_back(TNUMBER);
  121. numbers.push_back(txt);
  122. return *this;
  123. }
  124. void addReplacement(ui8 type, ui32 serial)
  125. {
  126. message.push_back(TREPLACE_LSTRING);
  127. localStrings.emplace_back(type, serial);
  128. }
  129. void addReplacement(const std::string &txt)
  130. {
  131. message.push_back(TREPLACE_ESTRING);
  132. exactStrings.push_back(txt);
  133. }
  134. void addReplacement(int64_t txt)
  135. {
  136. message.push_back(TREPLACE_NUMBER);
  137. numbers.push_back(txt);
  138. }
  139. void addReplacement2(int64_t txt)
  140. {
  141. message.push_back(TREPLACE_PLUSNUMBER);
  142. numbers.push_back(txt);
  143. }
  144. void addCreReplacement(const CreatureID & id, TQuantity count); //adds sing or plural name;
  145. void addReplacement(const CStackBasicDescriptor &stack); //adds sing or plural name;
  146. std::string buildList () const;
  147. void clear()
  148. {
  149. exactStrings.clear();
  150. localStrings.clear();
  151. message.clear();
  152. numbers.clear();
  153. }
  154. void toString(std::string &dst) const;
  155. std::string toString() const;
  156. void getLocalString(const std::pair<ui8, ui32> & txt, std::string & dst) const;
  157. };
  158. struct Component
  159. {
  160. enum EComponentType {PRIM_SKILL, SEC_SKILL, RESOURCE, CREATURE, ARTIFACT, EXPERIENCE, SPELL, MORALE, LUCK, BUILDING, HERO_PORTRAIT, FLAG};
  161. ui16 id = 0, subtype = 0; //id uses ^^^ enums, when id==EXPPERIENCE subtype==0 means exp points and subtype==1 levels)
  162. si32 val = 0; // + give; - take
  163. si16 when = 0; // 0 - now; +x - within x days; -x - per x days
  164. template <typename Handler> void serialize(Handler &h, const int version)
  165. {
  166. h & id;
  167. h & subtype;
  168. h & val;
  169. h & when;
  170. }
  171. Component() = default;
  172. DLL_LINKAGE explicit Component(const CStackBasicDescriptor &stack);
  173. Component(Component::EComponentType Type, ui16 Subtype, si32 Val, si16 When)
  174. :id(Type),subtype(Subtype),val(Val),when(When)
  175. {
  176. }
  177. };
  178. using TArtHolder = boost::variant<ConstTransitivePtr<CGHeroInstance>, ConstTransitivePtr<CStackInstance>>;
  179. struct ArtifactLocation
  180. {
  181. TArtHolder artHolder;//TODO: identify holder by id
  182. ArtifactPosition slot = ArtifactPosition::PRE_FIRST;
  183. ArtifactLocation()
  184. : artHolder(ConstTransitivePtr<CGHeroInstance>())
  185. {
  186. }
  187. template<typename T>
  188. ArtifactLocation(const T * ArtHolder, ArtifactPosition Slot)
  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. }
  193. ArtifactLocation(TArtHolder ArtHolder, const ArtifactPosition & Slot)
  194. : artHolder(std::move(std::move(ArtHolder)))
  195. , slot(Slot)
  196. {
  197. }
  198. template <typename T>
  199. bool isHolder(const T *t) const
  200. {
  201. if(auto ptrToT = boost::get<ConstTransitivePtr<T> >(&artHolder))
  202. {
  203. return ptrToT->get() == t;
  204. }
  205. return false;
  206. }
  207. DLL_LINKAGE void removeArtifact(); // BE CAREFUL, this operation modifies holder (gs)
  208. DLL_LINKAGE const CArmedInstance *relatedObj() const; //hero or the stack owner
  209. DLL_LINKAGE PlayerColor owningPlayer() const;
  210. DLL_LINKAGE CArtifactSet *getHolderArtSet();
  211. DLL_LINKAGE CBonusSystemNode *getHolderNode();
  212. DLL_LINKAGE const CArtifactSet *getHolderArtSet() const;
  213. DLL_LINKAGE const CBonusSystemNode *getHolderNode() const;
  214. DLL_LINKAGE const CArtifactInstance *getArt() const;
  215. DLL_LINKAGE CArtifactInstance *getArt();
  216. DLL_LINKAGE const ArtSlotInfo *getSlot() const;
  217. template <typename Handler> void serialize(Handler &h, const int version)
  218. {
  219. h & artHolder;
  220. h & slot;
  221. }
  222. };
  223. class EntityChanges
  224. {
  225. public:
  226. Metatype metatype = Metatype::UNKNOWN;
  227. int32_t entityIndex = 0;
  228. JsonNode data;
  229. template <typename Handler> void serialize(Handler & h, const int version)
  230. {
  231. h & metatype;
  232. h & entityIndex;
  233. h & data;
  234. }
  235. };
  236. class BattleChanges
  237. {
  238. public:
  239. enum class EOperation : si8
  240. {
  241. ADD,
  242. RESET_STATE,
  243. UPDATE,
  244. REMOVE,
  245. ACTIVATE_AND_UPDATE,
  246. ACTIVATE_AND_REMOVE
  247. };
  248. JsonNode data;
  249. EOperation operation = EOperation::RESET_STATE;
  250. BattleChanges() = default;
  251. BattleChanges(EOperation operation_)
  252. : operation(operation_)
  253. {
  254. }
  255. };
  256. class UnitChanges : public BattleChanges
  257. {
  258. public:
  259. uint32_t id = 0;
  260. int64_t healthDelta = 0;
  261. UnitChanges() = default;
  262. UnitChanges(uint32_t id_, EOperation operation_)
  263. : BattleChanges(operation_)
  264. , id(id_)
  265. {
  266. }
  267. template <typename Handler> void serialize(Handler & h, const int version)
  268. {
  269. h & id;
  270. h & healthDelta;
  271. h & data;
  272. h & operation;
  273. }
  274. };
  275. class ObstacleChanges : public BattleChanges
  276. {
  277. public:
  278. uint32_t id = 0;
  279. ObstacleChanges() = default;
  280. ObstacleChanges(uint32_t id_, EOperation operation_)
  281. : BattleChanges(operation_),
  282. id(id_)
  283. {
  284. }
  285. template <typename Handler> void serialize(Handler & h, const int version)
  286. {
  287. h & id;
  288. h & data;
  289. h & operation;
  290. }
  291. };
  292. VCMI_LIB_NAMESPACE_END