NetPacksBase.h 8.1 KB

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