NetPacksBase.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #pragma once
  2. /*
  3. * NetPacksBase.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  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->errorStream() << "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 & localStrings & message & numbers;
  48. }
  49. void addTxt(ui8 type, ui32 serial)
  50. {
  51. message.push_back(TLOCAL_STRING);
  52. localStrings.push_back(std::pair<ui8,ui32>(type, serial));
  53. }
  54. MetaString& operator<<(const std::pair<ui8,ui32> &txt)
  55. {
  56. message.push_back(TLOCAL_STRING);
  57. localStrings.push_back(txt);
  58. return *this;
  59. }
  60. MetaString& operator<<(const std::string &txt)
  61. {
  62. message.push_back(TEXACT_STRING);
  63. exactStrings.push_back(txt);
  64. return *this;
  65. }
  66. MetaString& operator<<(int txt)
  67. {
  68. message.push_back(TNUMBER);
  69. numbers.push_back(txt);
  70. return *this;
  71. }
  72. void addReplacement(ui8 type, ui32 serial)
  73. {
  74. message.push_back(TREPLACE_LSTRING);
  75. localStrings.push_back(std::pair<ui8,ui32>(type, serial));
  76. }
  77. void addReplacement(const std::string &txt)
  78. {
  79. message.push_back(TREPLACE_ESTRING);
  80. exactStrings.push_back(txt);
  81. }
  82. void addReplacement(int txt)
  83. {
  84. message.push_back(TREPLACE_NUMBER);
  85. numbers.push_back(txt);
  86. }
  87. void addReplacement2(int txt)
  88. {
  89. message.push_back(TREPLACE_PLUSNUMBER);
  90. numbers.push_back(txt);
  91. }
  92. void addCreReplacement(CreatureID id, TQuantity count); //adds sing or plural name;
  93. void addReplacement(const CStackBasicDescriptor &stack); //adds sing or plural name;
  94. std::string buildList () const;
  95. void clear()
  96. {
  97. exactStrings.clear();
  98. localStrings.clear();
  99. message.clear();
  100. numbers.clear();
  101. }
  102. void toString(std::string &dst) const;
  103. std::string toString() const;
  104. void getLocalString(const std::pair<ui8,ui32> &txt, std::string &dst) const;
  105. MetaString(){}
  106. };
  107. struct Component
  108. {
  109. enum EComponentType {PRIM_SKILL, SEC_SKILL, RESOURCE, CREATURE, ARTIFACT, EXPERIENCE, SPELL, MORALE, LUCK, BUILDING, HERO_PORTRAIT, FLAG};
  110. ui16 id, subtype; //id uses ^^^ enums, when id==EXPPERIENCE subtype==0 means exp points and subtype==1 levels)
  111. si32 val; // + give; - take
  112. si16 when; // 0 - now; +x - within x days; -x - per x days
  113. template <typename Handler> void serialize(Handler &h, const int version)
  114. {
  115. h & id & subtype & val & when;
  116. }
  117. Component()
  118. :id(0), subtype(0), val(0), when(0)
  119. {
  120. }
  121. DLL_LINKAGE explicit Component(const CStackBasicDescriptor &stack);
  122. Component(Component::EComponentType Type, ui16 Subtype, si32 Val, si16 When)
  123. :id(Type),subtype(Subtype),val(Val),when(When)
  124. {
  125. }
  126. };
  127. typedef boost::variant<ConstTransitivePtr<CGHeroInstance>, ConstTransitivePtr<CStackInstance> > TArtHolder;
  128. struct ArtifactLocation
  129. {
  130. TArtHolder artHolder;
  131. ArtifactPosition slot;
  132. ArtifactLocation()
  133. {
  134. artHolder = ConstTransitivePtr<CGHeroInstance>();
  135. slot = ArtifactPosition::PRE_FIRST;
  136. }
  137. template <typename T>
  138. ArtifactLocation(const T *ArtHolder, ArtifactPosition Slot)
  139. {
  140. artHolder = const_cast<T*>(ArtHolder); //we are allowed here to const cast -> change will go through one of our packages... do not abuse!
  141. slot = Slot;
  142. }
  143. ArtifactLocation(TArtHolder ArtHolder, ArtifactPosition Slot)
  144. {
  145. artHolder = ArtHolder;
  146. slot = Slot;
  147. }
  148. template <typename T>
  149. bool isHolder(const T *t) const
  150. {
  151. if(auto ptrToT = boost::get<ConstTransitivePtr<T> >(&artHolder))
  152. {
  153. return ptrToT->get() == t;
  154. }
  155. return false;
  156. }
  157. DLL_LINKAGE void removeArtifact(); // BE CAREFUL, this operation modifies holder (gs)
  158. DLL_LINKAGE const CArmedInstance *relatedObj() const; //hero or the stack owner
  159. DLL_LINKAGE PlayerColor owningPlayer() const;
  160. DLL_LINKAGE CArtifactSet *getHolderArtSet();
  161. DLL_LINKAGE CBonusSystemNode *getHolderNode();
  162. DLL_LINKAGE const CArtifactSet *getHolderArtSet() const;
  163. DLL_LINKAGE const CBonusSystemNode *getHolderNode() const;
  164. DLL_LINKAGE const CArtifactInstance *getArt() const;
  165. DLL_LINKAGE CArtifactInstance *getArt();
  166. DLL_LINKAGE const ArtSlotInfo *getSlot() const;
  167. template <typename Handler> void serialize(Handler &h, const int version)
  168. {
  169. h & artHolder & slot;
  170. }
  171. };
  172. class CHealthInfo
  173. {
  174. public:
  175. CHealthInfo():
  176. stackId(0), delta(0), firstHPleft(0), fullUnits(0), resurrected(0)
  177. {
  178. }
  179. uint32_t stackId;
  180. int32_t delta;
  181. int32_t firstHPleft;
  182. int32_t fullUnits;
  183. int32_t resurrected;
  184. template <typename Handler> void serialize(Handler & h, const int version)
  185. {
  186. h & stackId & delta & firstHPleft & fullUnits & resurrected;
  187. }
  188. };