NetPacksBase.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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<<(int64_t txt)
  126. {
  127. message.push_back(TNUMBER);
  128. numbers.push_back(txt);
  129. return *this;
  130. }
  131. void addRawString(std::string value)
  132. {
  133. message.push_back(TEXACT_STRING);
  134. exactStrings.push_back(value);
  135. }
  136. void addNumber(int64_t value)
  137. {
  138. message.push_back(TNUMBER);
  139. numbers.push_back(value);
  140. }
  141. void addReplacement(ui8 type, ui32 serial)
  142. {
  143. message.push_back(TREPLACE_LSTRING);
  144. localStrings.emplace_back(type, serial);
  145. }
  146. void addReplacement(const std::string &txt)
  147. {
  148. message.push_back(TREPLACE_ESTRING);
  149. exactStrings.push_back(txt);
  150. }
  151. void addReplacement(int64_t txt)
  152. {
  153. message.push_back(TREPLACE_NUMBER);
  154. numbers.push_back(txt);
  155. }
  156. void addReplacement2(int64_t txt)
  157. {
  158. message.push_back(TREPLACE_PLUSNUMBER);
  159. numbers.push_back(txt);
  160. }
  161. void addCreReplacement(const CreatureID & id, TQuantity count); //adds sing or plural name;
  162. void addReplacement(const CStackBasicDescriptor &stack); //adds sing or plural name;
  163. std::string buildList () const;
  164. void clear()
  165. {
  166. exactStrings.clear();
  167. localStrings.clear();
  168. message.clear();
  169. numbers.clear();
  170. }
  171. void toString(std::string &dst) const;
  172. std::string toString() const;
  173. void getLocalString(const std::pair<ui8, ui32> & txt, std::string & dst) const;
  174. };
  175. struct Component
  176. {
  177. enum class EComponentType : uint8_t
  178. {
  179. PRIM_SKILL,
  180. SEC_SKILL,
  181. RESOURCE,
  182. CREATURE,
  183. ARTIFACT,
  184. EXPERIENCE,
  185. SPELL,
  186. MORALE,
  187. LUCK,
  188. BUILDING,
  189. HERO_PORTRAIT,
  190. FLAG,
  191. INVALID //should be last
  192. };
  193. EComponentType id = EComponentType::INVALID;
  194. ui16 subtype = 0; //id==EXPPERIENCE subtype==0 means exp points and subtype==1 levels
  195. si32 val = 0; // + give; - take
  196. si16 when = 0; // 0 - now; +x - within x days; -x - per x days
  197. template <typename Handler> void serialize(Handler &h, const int version)
  198. {
  199. h & id;
  200. h & subtype;
  201. h & val;
  202. h & when;
  203. }
  204. Component() = default;
  205. DLL_LINKAGE explicit Component(const CStackBasicDescriptor &stack);
  206. Component(Component::EComponentType Type, ui16 Subtype, si32 Val, si16 When)
  207. :id(Type),subtype(Subtype),val(Val),when(When)
  208. {
  209. }
  210. };
  211. using TArtHolder = std::variant<ConstTransitivePtr<CGHeroInstance>, ConstTransitivePtr<CStackInstance>>;
  212. struct ArtifactLocation
  213. {
  214. TArtHolder artHolder;//TODO: identify holder by id
  215. ArtifactPosition slot = ArtifactPosition::PRE_FIRST;
  216. ArtifactLocation()
  217. : artHolder(ConstTransitivePtr<CGHeroInstance>())
  218. {
  219. }
  220. template<typename T>
  221. ArtifactLocation(const T * ArtHolder, ArtifactPosition Slot)
  222. : artHolder(const_cast<T *>(ArtHolder)) //we are allowed here to const cast -> change will go through one of our packages... do not abuse!
  223. , slot(Slot)
  224. {
  225. }
  226. ArtifactLocation(TArtHolder ArtHolder, const ArtifactPosition & Slot)
  227. : artHolder(std::move(std::move(ArtHolder)))
  228. , slot(Slot)
  229. {
  230. }
  231. template <typename T>
  232. bool isHolder(const T *t) const
  233. {
  234. if(auto ptrToT = std::get<ConstTransitivePtr<T>>(artHolder))
  235. {
  236. return ptrToT == t;
  237. }
  238. return false;
  239. }
  240. DLL_LINKAGE void removeArtifact(); // BE CAREFUL, this operation modifies holder (gs)
  241. DLL_LINKAGE const CArmedInstance *relatedObj() const; //hero or the stack owner
  242. DLL_LINKAGE PlayerColor owningPlayer() const;
  243. DLL_LINKAGE CArtifactSet *getHolderArtSet();
  244. DLL_LINKAGE CBonusSystemNode *getHolderNode();
  245. DLL_LINKAGE const CArtifactSet *getHolderArtSet() const;
  246. DLL_LINKAGE const CBonusSystemNode *getHolderNode() const;
  247. DLL_LINKAGE const CArtifactInstance *getArt() const;
  248. DLL_LINKAGE CArtifactInstance *getArt();
  249. DLL_LINKAGE const ArtSlotInfo *getSlot() const;
  250. template <typename Handler> void serialize(Handler &h, const int version)
  251. {
  252. h & artHolder;
  253. h & slot;
  254. }
  255. };
  256. class EntityChanges
  257. {
  258. public:
  259. Metatype metatype = Metatype::UNKNOWN;
  260. int32_t entityIndex = 0;
  261. JsonNode data;
  262. template <typename Handler> void serialize(Handler & h, const int version)
  263. {
  264. h & metatype;
  265. h & entityIndex;
  266. h & data;
  267. }
  268. };
  269. class BattleChanges
  270. {
  271. public:
  272. enum class EOperation : si8
  273. {
  274. ADD,
  275. RESET_STATE,
  276. UPDATE,
  277. REMOVE,
  278. };
  279. JsonNode data;
  280. EOperation operation = EOperation::RESET_STATE;
  281. BattleChanges() = default;
  282. BattleChanges(EOperation operation_)
  283. : operation(operation_)
  284. {
  285. }
  286. };
  287. class UnitChanges : public BattleChanges
  288. {
  289. public:
  290. uint32_t id = 0;
  291. int64_t healthDelta = 0;
  292. UnitChanges() = default;
  293. UnitChanges(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 & healthDelta;
  302. h & data;
  303. h & operation;
  304. }
  305. };
  306. class ObstacleChanges : public BattleChanges
  307. {
  308. public:
  309. uint32_t id = 0;
  310. ObstacleChanges() = default;
  311. ObstacleChanges(uint32_t id_, EOperation operation_)
  312. : BattleChanges(operation_),
  313. id(id_)
  314. {
  315. }
  316. template <typename Handler> void serialize(Handler & h, const int version)
  317. {
  318. h & id;
  319. h & data;
  320. h & operation;
  321. }
  322. };
  323. VCMI_LIB_NAMESPACE_END