NetPacksBase.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 class EComponentType : uint8_t
  174. {
  175. PRIM_SKILL,
  176. SEC_SKILL,
  177. RESOURCE,
  178. CREATURE,
  179. ARTIFACT,
  180. EXPERIENCE,
  181. SPELL,
  182. MORALE,
  183. LUCK,
  184. BUILDING,
  185. HERO_PORTRAIT,
  186. FLAG,
  187. INVALID //should be last
  188. };
  189. EComponentType id = EComponentType::INVALID;
  190. ui16 subtype = 0; //id==EXPPERIENCE subtype==0 means exp points and subtype==1 levels
  191. si32 val = 0; // + give; - take
  192. si16 when = 0; // 0 - now; +x - within x days; -x - per x days
  193. template <typename Handler> void serialize(Handler &h, const int version)
  194. {
  195. h & id;
  196. h & subtype;
  197. h & val;
  198. h & when;
  199. }
  200. Component() = default;
  201. DLL_LINKAGE explicit Component(const CStackBasicDescriptor &stack);
  202. Component(Component::EComponentType Type, ui16 Subtype, si32 Val, si16 When)
  203. :id(Type),subtype(Subtype),val(Val),when(When)
  204. {
  205. }
  206. };
  207. using TArtHolder = boost::variant<ConstTransitivePtr<CGHeroInstance>, ConstTransitivePtr<CStackInstance>>;
  208. struct ArtifactLocation
  209. {
  210. TArtHolder artHolder;//TODO: identify holder by id
  211. ArtifactPosition slot = ArtifactPosition::PRE_FIRST;
  212. ArtifactLocation()
  213. : artHolder(ConstTransitivePtr<CGHeroInstance>())
  214. {
  215. }
  216. template<typename T>
  217. ArtifactLocation(const T * ArtHolder, ArtifactPosition Slot)
  218. : artHolder(const_cast<T *>(ArtHolder)) //we are allowed here to const cast -> change will go through one of our packages... do not abuse!
  219. , slot(Slot)
  220. {
  221. }
  222. ArtifactLocation(TArtHolder ArtHolder, const ArtifactPosition & Slot)
  223. : artHolder(std::move(std::move(ArtHolder)))
  224. , slot(Slot)
  225. {
  226. }
  227. template <typename T>
  228. bool isHolder(const T *t) const
  229. {
  230. if(auto ptrToT = boost::get<ConstTransitivePtr<T> >(&artHolder))
  231. {
  232. return ptrToT->get() == t;
  233. }
  234. return false;
  235. }
  236. DLL_LINKAGE void removeArtifact(); // BE CAREFUL, this operation modifies holder (gs)
  237. DLL_LINKAGE const CArmedInstance *relatedObj() const; //hero or the stack owner
  238. DLL_LINKAGE PlayerColor owningPlayer() const;
  239. DLL_LINKAGE CArtifactSet *getHolderArtSet();
  240. DLL_LINKAGE CBonusSystemNode *getHolderNode();
  241. DLL_LINKAGE const CArtifactSet *getHolderArtSet() const;
  242. DLL_LINKAGE const CBonusSystemNode *getHolderNode() const;
  243. DLL_LINKAGE const CArtifactInstance *getArt() const;
  244. DLL_LINKAGE CArtifactInstance *getArt();
  245. DLL_LINKAGE const ArtSlotInfo *getSlot() const;
  246. template <typename Handler> void serialize(Handler &h, const int version)
  247. {
  248. h & artHolder;
  249. h & slot;
  250. }
  251. };
  252. class EntityChanges
  253. {
  254. public:
  255. Metatype metatype = Metatype::UNKNOWN;
  256. int32_t entityIndex = 0;
  257. JsonNode data;
  258. template <typename Handler> void serialize(Handler & h, const int version)
  259. {
  260. h & metatype;
  261. h & entityIndex;
  262. h & data;
  263. }
  264. };
  265. class BattleChanges
  266. {
  267. public:
  268. enum class EOperation : si8
  269. {
  270. ADD,
  271. RESET_STATE,
  272. UPDATE,
  273. REMOVE,
  274. };
  275. JsonNode data;
  276. EOperation operation = EOperation::RESET_STATE;
  277. BattleChanges() = default;
  278. BattleChanges(EOperation operation_)
  279. : operation(operation_)
  280. {
  281. }
  282. };
  283. class UnitChanges : public BattleChanges
  284. {
  285. public:
  286. uint32_t id = 0;
  287. int64_t healthDelta = 0;
  288. UnitChanges() = default;
  289. UnitChanges(uint32_t id_, EOperation operation_)
  290. : BattleChanges(operation_)
  291. , id(id_)
  292. {
  293. }
  294. template <typename Handler> void serialize(Handler & h, const int version)
  295. {
  296. h & id;
  297. h & healthDelta;
  298. h & data;
  299. h & operation;
  300. }
  301. };
  302. class ObstacleChanges : public BattleChanges
  303. {
  304. public:
  305. uint32_t id = 0;
  306. ObstacleChanges() = default;
  307. ObstacleChanges(uint32_t id_, EOperation operation_)
  308. : BattleChanges(operation_),
  309. id(id_)
  310. {
  311. }
  312. template <typename Handler> void serialize(Handler & h, const int version)
  313. {
  314. h & id;
  315. h & data;
  316. h & operation;
  317. }
  318. };
  319. VCMI_LIB_NAMESPACE_END