NetPacksBase.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 CGameState;
  12. class CStackBasicDescriptor;
  13. class CGHeroInstance;
  14. class CStackInstance;
  15. class CArmedInstance;
  16. class CArtifactSet;
  17. class CBonusSystemNode;
  18. struct ArtSlotInfo;
  19. #include "ConstTransitivePtr.h"
  20. #include "GameConstants.h"
  21. struct DLL_LINKAGE CPack
  22. {
  23. CPack() {};
  24. virtual ~CPack() {};
  25. template <typename Handler> void serialize(Handler &h, const int version)
  26. {
  27. logNetwork->error("CPack serialized... this should not happen!");
  28. assert(false && "CPack serialized");
  29. }
  30. void applyGs(CGameState *gs) { }
  31. virtual std::string toString() const { return boost::str(boost::format("{CPack: type '%s'}") % typeid(this).name()); }
  32. };
  33. std::ostream & operator<<(std::ostream & out, const CPack * pack);
  34. struct DLL_LINKAGE MetaString
  35. {
  36. private:
  37. enum EMessage {TEXACT_STRING, TLOCAL_STRING, TNUMBER, TREPLACE_ESTRING, TREPLACE_LSTRING, TREPLACE_NUMBER, TREPLACE_PLUSNUMBER};
  38. public:
  39. enum {GENERAL_TXT=1, XTRAINFO_TXT, OBJ_NAMES, RES_NAMES, ART_NAMES, ARRAY_TXT, CRE_PL_NAMES, CREGENS, MINE_NAMES,
  40. MINE_EVNTS, ADVOB_TXT, ART_EVNTS, SPELL_NAME, SEC_SKILL_NAME, CRE_SING_NAMES, CREGENS4, COLOR, ART_DESCR, JK_TXT};
  41. std::vector<ui8> message; //vector of EMessage
  42. std::vector<std::pair<ui8,ui32> > localStrings;
  43. std::vector<std::string> exactStrings;
  44. std::vector<si32> numbers;
  45. template <typename Handler> void serialize(Handler &h, const int version)
  46. {
  47. h & exactStrings;
  48. h & localStrings;
  49. h & message;
  50. h & numbers;
  51. }
  52. void addTxt(ui8 type, ui32 serial)
  53. {
  54. message.push_back(TLOCAL_STRING);
  55. localStrings.push_back(std::pair<ui8,ui32>(type, serial));
  56. }
  57. MetaString& operator<<(const std::pair<ui8,ui32> &txt)
  58. {
  59. message.push_back(TLOCAL_STRING);
  60. localStrings.push_back(txt);
  61. return *this;
  62. }
  63. MetaString& operator<<(const std::string &txt)
  64. {
  65. message.push_back(TEXACT_STRING);
  66. exactStrings.push_back(txt);
  67. return *this;
  68. }
  69. MetaString& operator<<(int txt)
  70. {
  71. message.push_back(TNUMBER);
  72. numbers.push_back(txt);
  73. return *this;
  74. }
  75. void addReplacement(ui8 type, ui32 serial)
  76. {
  77. message.push_back(TREPLACE_LSTRING);
  78. localStrings.push_back(std::pair<ui8,ui32>(type, serial));
  79. }
  80. void addReplacement(const std::string &txt)
  81. {
  82. message.push_back(TREPLACE_ESTRING);
  83. exactStrings.push_back(txt);
  84. }
  85. void addReplacement(int txt)
  86. {
  87. message.push_back(TREPLACE_NUMBER);
  88. numbers.push_back(txt);
  89. }
  90. void addReplacement2(int txt)
  91. {
  92. message.push_back(TREPLACE_PLUSNUMBER);
  93. numbers.push_back(txt);
  94. }
  95. void addCreReplacement(CreatureID id, TQuantity count); //adds sing or plural name;
  96. void addReplacement(const CStackBasicDescriptor &stack); //adds sing or plural name;
  97. std::string buildList () const;
  98. void clear()
  99. {
  100. exactStrings.clear();
  101. localStrings.clear();
  102. message.clear();
  103. numbers.clear();
  104. }
  105. void toString(std::string &dst) const;
  106. std::string toString() const;
  107. void getLocalString(const std::pair<ui8,ui32> &txt, std::string &dst) const;
  108. MetaString(){}
  109. };
  110. struct Component
  111. {
  112. enum EComponentType {PRIM_SKILL, SEC_SKILL, RESOURCE, CREATURE, ARTIFACT, EXPERIENCE, SPELL, MORALE, LUCK, BUILDING, HERO_PORTRAIT, FLAG};
  113. ui16 id, subtype; //id uses ^^^ enums, when id==EXPPERIENCE subtype==0 means exp points and subtype==1 levels)
  114. si32 val; // + give; - take
  115. si16 when; // 0 - now; +x - within x days; -x - per x days
  116. template <typename Handler> void serialize(Handler &h, const int version)
  117. {
  118. h & id;
  119. h & subtype;
  120. h & val;
  121. h & when;
  122. }
  123. Component()
  124. :id(0), subtype(0), val(0), when(0)
  125. {
  126. }
  127. DLL_LINKAGE explicit Component(const CStackBasicDescriptor &stack);
  128. Component(Component::EComponentType Type, ui16 Subtype, si32 Val, si16 When)
  129. :id(Type),subtype(Subtype),val(Val),when(When)
  130. {
  131. }
  132. };
  133. typedef boost::variant<ConstTransitivePtr<CGHeroInstance>, ConstTransitivePtr<CStackInstance> > TArtHolder;
  134. struct ArtifactLocation
  135. {
  136. TArtHolder artHolder;
  137. ArtifactPosition slot;
  138. ArtifactLocation()
  139. {
  140. artHolder = ConstTransitivePtr<CGHeroInstance>();
  141. slot = ArtifactPosition::PRE_FIRST;
  142. }
  143. template <typename T>
  144. ArtifactLocation(const T *ArtHolder, ArtifactPosition Slot)
  145. {
  146. artHolder = const_cast<T*>(ArtHolder); //we are allowed here to const cast -> change will go through one of our packages... do not abuse!
  147. slot = Slot;
  148. }
  149. ArtifactLocation(TArtHolder ArtHolder, ArtifactPosition Slot)
  150. {
  151. artHolder = ArtHolder;
  152. slot = Slot;
  153. }
  154. template <typename T>
  155. bool isHolder(const T *t) const
  156. {
  157. if(auto ptrToT = boost::get<ConstTransitivePtr<T> >(&artHolder))
  158. {
  159. return ptrToT->get() == t;
  160. }
  161. return false;
  162. }
  163. DLL_LINKAGE void removeArtifact(); // BE CAREFUL, this operation modifies holder (gs)
  164. DLL_LINKAGE const CArmedInstance *relatedObj() const; //hero or the stack owner
  165. DLL_LINKAGE PlayerColor owningPlayer() const;
  166. DLL_LINKAGE CArtifactSet *getHolderArtSet();
  167. DLL_LINKAGE CBonusSystemNode *getHolderNode();
  168. DLL_LINKAGE const CArtifactSet *getHolderArtSet() const;
  169. DLL_LINKAGE const CBonusSystemNode *getHolderNode() const;
  170. DLL_LINKAGE const CArtifactInstance *getArt() const;
  171. DLL_LINKAGE CArtifactInstance *getArt();
  172. DLL_LINKAGE const ArtSlotInfo *getSlot() const;
  173. template <typename Handler> void serialize(Handler &h, const int version)
  174. {
  175. h & artHolder;
  176. h & slot;
  177. }
  178. };
  179. class CHealthInfo
  180. {
  181. public:
  182. CHealthInfo():
  183. stackId(0), delta(0), firstHPleft(0), fullUnits(0), resurrected(0)
  184. {
  185. }
  186. uint32_t stackId;
  187. int32_t delta;
  188. int32_t firstHPleft;
  189. int32_t fullUnits;
  190. int32_t resurrected;
  191. template <typename Handler> void serialize(Handler & h, const int version)
  192. {
  193. h & stackId;
  194. h & delta;
  195. h & firstHPleft;
  196. h & fullUnits;
  197. h & resurrected;
  198. }
  199. };