NetPacksBase.h 7.9 KB

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