NetPacksBase.h 8.5 KB

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