NetPacksBase.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. ui16 type;
  24. CPack() {};
  25. virtual ~CPack() {};
  26. ui16 getType() const { return type; }
  27. template <typename Handler> void serialize(Handler &h, const int version)
  28. {
  29. logNetwork->errorStream() << "CPack serialized... this should not happen!";
  30. assert(false && "CPack serialized");
  31. }
  32. void applyGs(CGameState *gs) { }
  33. virtual std::string toString() const { return boost::str(boost::format("{CPack: type '%d'}") % type); }
  34. };
  35. std::ostream & operator<<(std::ostream & out, const CPack * pack);
  36. struct DLL_LINKAGE MetaString
  37. {
  38. private:
  39. enum EMessage {TEXACT_STRING, TLOCAL_STRING, TNUMBER, TREPLACE_ESTRING, TREPLACE_LSTRING, TREPLACE_NUMBER, TREPLACE_PLUSNUMBER};
  40. public:
  41. enum {GENERAL_TXT=1, XTRAINFO_TXT, OBJ_NAMES, RES_NAMES, ART_NAMES, ARRAY_TXT, CRE_PL_NAMES, CREGENS, MINE_NAMES,
  42. MINE_EVNTS, ADVOB_TXT, ART_EVNTS, SPELL_NAME, SEC_SKILL_NAME, CRE_SING_NAMES, CREGENS4, COLOR, ART_DESCR};
  43. std::vector<ui8> message; //vector of EMessage
  44. std::vector<std::pair<ui8,ui32> > localStrings; //pairs<text handler type, text number>; types: 1 - generaltexthandler->all; 2 - objh->xtrainfo; 3 - objh->names; 4 - objh->restypes; 5 - arth->artifacts[id].name; 6 - generaltexth->arraytxt; 7 - creh->creatures[os->subID].namePl; 8 - objh->creGens; 9 - objh->mines[ID]->first; 10 - objh->mines[ID]->second; 11 - objh->advobtxt
  45. std::vector<std::string> exactStrings;
  46. std::vector<si32> numbers;
  47. template <typename Handler> void serialize(Handler &h, const int version)
  48. {
  49. h & exactStrings & localStrings & message & numbers;
  50. }
  51. void addTxt(ui8 type, ui32 serial)
  52. {
  53. message.push_back(TLOCAL_STRING);
  54. localStrings.push_back(std::pair<ui8,ui32>(type, serial));
  55. }
  56. MetaString& operator<<(const std::pair<ui8,ui32> &txt)
  57. {
  58. message.push_back(TLOCAL_STRING);
  59. localStrings.push_back(txt);
  60. return *this;
  61. }
  62. MetaString& operator<<(const std::string &txt)
  63. {
  64. message.push_back(TEXACT_STRING);
  65. exactStrings.push_back(txt);
  66. return *this;
  67. }
  68. MetaString& operator<<(int txt)
  69. {
  70. message.push_back(TNUMBER);
  71. numbers.push_back(txt);
  72. return *this;
  73. }
  74. void addReplacement(ui8 type, ui32 serial)
  75. {
  76. message.push_back(TREPLACE_LSTRING);
  77. localStrings.push_back(std::pair<ui8,ui32>(type, serial));
  78. }
  79. void addReplacement(const std::string &txt)
  80. {
  81. message.push_back(TREPLACE_ESTRING);
  82. exactStrings.push_back(txt);
  83. }
  84. void addReplacement(int txt)
  85. {
  86. message.push_back(TREPLACE_NUMBER);
  87. numbers.push_back(txt);
  88. }
  89. void addReplacement2(int txt)
  90. {
  91. message.push_back(TREPLACE_PLUSNUMBER);
  92. numbers.push_back(txt);
  93. }
  94. void addCreReplacement(CreatureID id, TQuantity count); //adds sing or plural name;
  95. void addReplacement(const CStackBasicDescriptor &stack); //adds sing or plural name;
  96. std::string buildList () const;
  97. void clear()
  98. {
  99. exactStrings.clear();
  100. localStrings.clear();
  101. message.clear();
  102. numbers.clear();
  103. }
  104. void toString(std::string &dst) const;
  105. std::string toString() const;
  106. void getLocalString(const std::pair<ui8,ui32> &txt, std::string &dst) const;
  107. MetaString(){}
  108. };
  109. struct Component
  110. {
  111. enum EComponentType {PRIM_SKILL, SEC_SKILL, RESOURCE, CREATURE, ARTIFACT, EXPERIENCE, SPELL, MORALE, LUCK, BUILDING, HERO_PORTRAIT, FLAG};
  112. ui16 id, subtype; //id uses ^^^ enums, when id==EXPPERIENCE subtype==0 means exp points and subtype==1 levels)
  113. si32 val; // + give; - take
  114. si16 when; // 0 - now; +x - within x days; -x - per x days
  115. template <typename Handler> void serialize(Handler &h, const int version)
  116. {
  117. h & id & subtype & val & when;
  118. }
  119. Component()
  120. {
  121. }
  122. DLL_LINKAGE explicit Component(const CStackBasicDescriptor &stack);
  123. Component(Component::EComponentType Type, ui16 Subtype, si32 Val, si16 When)
  124. :id(Type),subtype(Subtype),val(Val),when(When)
  125. {
  126. }
  127. };
  128. typedef boost::variant<ConstTransitivePtr<CGHeroInstance>, ConstTransitivePtr<CStackInstance> > TArtHolder;
  129. struct ArtifactLocation
  130. {
  131. TArtHolder artHolder;
  132. ArtifactPosition slot;
  133. ArtifactLocation()
  134. {
  135. artHolder = ConstTransitivePtr<CGHeroInstance>();
  136. slot = ArtifactPosition::PRE_FIRST;
  137. }
  138. template <typename T>
  139. ArtifactLocation(const T *ArtHolder, ArtifactPosition Slot)
  140. {
  141. artHolder = const_cast<T*>(ArtHolder); //we are allowed here to const cast -> change will go through one of our packages... do not abuse!
  142. slot = Slot;
  143. }
  144. ArtifactLocation(TArtHolder ArtHolder, ArtifactPosition Slot)
  145. {
  146. artHolder = ArtHolder;
  147. slot = Slot;
  148. }
  149. template <typename T>
  150. bool isHolder(const T *t) const
  151. {
  152. if(auto ptrToT = boost::get<ConstTransitivePtr<T> >(&artHolder))
  153. {
  154. return ptrToT->get() == t;
  155. }
  156. return false;
  157. }
  158. DLL_LINKAGE void removeArtifact(); // BE CAREFUL, this operation modifies holder (gs)
  159. DLL_LINKAGE const CArmedInstance *relatedObj() const; //hero or the stack owner
  160. DLL_LINKAGE PlayerColor owningPlayer() const;
  161. DLL_LINKAGE CArtifactSet *getHolderArtSet();
  162. DLL_LINKAGE CBonusSystemNode *getHolderNode();
  163. DLL_LINKAGE const CArtifactSet *getHolderArtSet() const;
  164. DLL_LINKAGE const CBonusSystemNode *getHolderNode() const;
  165. DLL_LINKAGE const CArtifactInstance *getArt() const;
  166. DLL_LINKAGE CArtifactInstance *getArt();
  167. DLL_LINKAGE const ArtSlotInfo *getSlot() const;
  168. template <typename Handler> void serialize(Handler &h, const int version)
  169. {
  170. h & artHolder & slot;
  171. }
  172. };