NetPacksBase.h 5.9 KB

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