NetPacksBase.h 8.3 KB

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