NetPacksBase.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 Component
  97. {
  98. enum class EComponentType : uint8_t
  99. {
  100. PRIM_SKILL,
  101. SEC_SKILL,
  102. RESOURCE,
  103. CREATURE,
  104. ARTIFACT,
  105. EXPERIENCE,
  106. SPELL,
  107. MORALE,
  108. LUCK,
  109. BUILDING,
  110. HERO_PORTRAIT,
  111. FLAG,
  112. INVALID //should be last
  113. };
  114. EComponentType id = EComponentType::INVALID;
  115. ui16 subtype = 0; //id==EXPPERIENCE subtype==0 means exp points and subtype==1 levels
  116. si32 val = 0; // + give; - take
  117. si16 when = 0; // 0 - now; +x - within x days; -x - per x days
  118. template <typename Handler> void serialize(Handler &h, const int version)
  119. {
  120. h & id;
  121. h & subtype;
  122. h & val;
  123. h & when;
  124. }
  125. Component() = default;
  126. DLL_LINKAGE explicit Component(const CStackBasicDescriptor &stack);
  127. Component(Component::EComponentType Type, ui16 Subtype, si32 Val, si16 When)
  128. :id(Type),subtype(Subtype),val(Val),when(When)
  129. {
  130. }
  131. };
  132. using TArtHolder = std::variant<ConstTransitivePtr<CGHeroInstance>, ConstTransitivePtr<CStackInstance>>;
  133. struct ArtifactLocation
  134. {
  135. TArtHolder artHolder;//TODO: identify holder by id
  136. ArtifactPosition slot = ArtifactPosition::PRE_FIRST;
  137. ArtifactLocation()
  138. : artHolder(ConstTransitivePtr<CGHeroInstance>())
  139. {
  140. }
  141. template<typename T>
  142. ArtifactLocation(const T * ArtHolder, ArtifactPosition Slot)
  143. : artHolder(const_cast<T *>(ArtHolder)) //we are allowed here to const cast -> change will go through one of our packages... do not abuse!
  144. , slot(Slot)
  145. {
  146. }
  147. ArtifactLocation(TArtHolder ArtHolder, const ArtifactPosition & Slot)
  148. : artHolder(std::move(std::move(ArtHolder)))
  149. , slot(Slot)
  150. {
  151. }
  152. template <typename T>
  153. bool isHolder(const T *t) const
  154. {
  155. if(auto ptrToT = std::get<ConstTransitivePtr<T>>(artHolder))
  156. {
  157. return ptrToT == t;
  158. }
  159. return false;
  160. }
  161. DLL_LINKAGE void removeArtifact(); // BE CAREFUL, this operation modifies holder (gs)
  162. DLL_LINKAGE const CArmedInstance *relatedObj() const; //hero or the stack owner
  163. DLL_LINKAGE PlayerColor owningPlayer() const;
  164. DLL_LINKAGE CArtifactSet *getHolderArtSet();
  165. DLL_LINKAGE CBonusSystemNode *getHolderNode();
  166. DLL_LINKAGE const CArtifactSet *getHolderArtSet() const;
  167. DLL_LINKAGE const CBonusSystemNode *getHolderNode() const;
  168. DLL_LINKAGE const CArtifactInstance *getArt() const;
  169. DLL_LINKAGE CArtifactInstance *getArt();
  170. DLL_LINKAGE const ArtSlotInfo *getSlot() const;
  171. template <typename Handler> void serialize(Handler &h, const int version)
  172. {
  173. h & artHolder;
  174. h & slot;
  175. }
  176. };
  177. class EntityChanges
  178. {
  179. public:
  180. Metatype metatype = Metatype::UNKNOWN;
  181. int32_t entityIndex = 0;
  182. JsonNode data;
  183. template <typename Handler> void serialize(Handler & h, const int version)
  184. {
  185. h & metatype;
  186. h & entityIndex;
  187. h & data;
  188. }
  189. };
  190. class BattleChanges
  191. {
  192. public:
  193. enum class EOperation : si8
  194. {
  195. ADD,
  196. RESET_STATE,
  197. UPDATE,
  198. REMOVE,
  199. };
  200. JsonNode data;
  201. EOperation operation = EOperation::RESET_STATE;
  202. BattleChanges() = default;
  203. BattleChanges(EOperation operation_)
  204. : operation(operation_)
  205. {
  206. }
  207. };
  208. class UnitChanges : public BattleChanges
  209. {
  210. public:
  211. uint32_t id = 0;
  212. int64_t healthDelta = 0;
  213. UnitChanges() = default;
  214. UnitChanges(uint32_t id_, EOperation operation_)
  215. : BattleChanges(operation_)
  216. , id(id_)
  217. {
  218. }
  219. template <typename Handler> void serialize(Handler & h, const int version)
  220. {
  221. h & id;
  222. h & healthDelta;
  223. h & data;
  224. h & operation;
  225. }
  226. };
  227. class ObstacleChanges : public BattleChanges
  228. {
  229. public:
  230. uint32_t id = 0;
  231. ObstacleChanges() = default;
  232. ObstacleChanges(uint32_t id_, EOperation operation_)
  233. : BattleChanges(operation_),
  234. id(id_)
  235. {
  236. }
  237. template <typename Handler> void serialize(Handler & h, const int version)
  238. {
  239. h & id;
  240. h & data;
  241. h & operation;
  242. }
  243. };
  244. VCMI_LIB_NAMESPACE_END