NetPacksBase.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include "../ConstTransitivePtr.h"
  12. #include "../GameConstants.h"
  13. class CClient;
  14. class CGameHandler;
  15. class CLobbyScreen;
  16. class CServerHandler;
  17. class CVCMIServer;
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. class CGameState;
  20. class CConnection;
  21. class CStackBasicDescriptor;
  22. class CGHeroInstance;
  23. class CStackInstance;
  24. class CArmedInstance;
  25. class CArtifactSet;
  26. class CBonusSystemNode;
  27. struct ArtSlotInfo;
  28. class ICPackVisitor;
  29. struct DLL_LINKAGE CPack
  30. {
  31. std::shared_ptr<CConnection> c; // Pointer to connection that pack received from
  32. CPack() = default;
  33. virtual ~CPack() = default;
  34. template <typename Handler> void serialize(Handler &h, const int version)
  35. {
  36. logNetwork->error("CPack serialized... this should not happen!");
  37. assert(false && "CPack serialized");
  38. }
  39. void applyGs(CGameState * gs)
  40. {}
  41. void visit(ICPackVisitor & cpackVisitor);
  42. protected:
  43. /// <summary>
  44. /// For basic types of netpacks hierarchy like CPackForClient. Called first.
  45. /// </summary>
  46. virtual void visitBasic(ICPackVisitor & cpackVisitor);
  47. /// <summary>
  48. /// For leaf types of netpacks hierarchy. Called after visitBasic.
  49. /// </summary>
  50. virtual void visitTyped(ICPackVisitor & cpackVisitor);
  51. };
  52. struct DLL_LINKAGE CPackForClient : public CPack
  53. {
  54. protected:
  55. virtual void visitBasic(ICPackVisitor & cpackVisitor) override;
  56. };
  57. struct DLL_LINKAGE Query : public CPackForClient
  58. {
  59. QueryID queryID; // equals to -1 if it is not an actual query (and should not be answered)
  60. };
  61. struct DLL_LINKAGE CPackForServer : public CPack
  62. {
  63. mutable PlayerColor player = PlayerColor::NEUTRAL;
  64. mutable si32 requestID;
  65. template <typename Handler> void serialize(Handler &h, const int version)
  66. {
  67. h & player;
  68. h & requestID;
  69. }
  70. protected:
  71. virtual void visitBasic(ICPackVisitor & cpackVisitor) override;
  72. };
  73. struct DLL_LINKAGE CPackForLobby : public CPack
  74. {
  75. virtual bool isForServer() const;
  76. protected:
  77. virtual void visitBasic(ICPackVisitor & cpackVisitor) override;
  78. };
  79. using TArtHolder = std::variant<ConstTransitivePtr<CGHeroInstance>, ConstTransitivePtr<CStackInstance>>;
  80. struct ArtifactLocation
  81. {
  82. TArtHolder artHolder;//TODO: identify holder by id
  83. ArtifactPosition slot = ArtifactPosition::PRE_FIRST;
  84. ArtifactLocation()
  85. : artHolder(ConstTransitivePtr<CGHeroInstance>())
  86. {
  87. }
  88. template<typename T>
  89. ArtifactLocation(const T * ArtHolder, ArtifactPosition Slot)
  90. : artHolder(const_cast<T *>(ArtHolder)) //we are allowed here to const cast -> change will go through one of our packages... do not abuse!
  91. , slot(Slot)
  92. {
  93. }
  94. ArtifactLocation(TArtHolder ArtHolder, const ArtifactPosition & Slot)
  95. : artHolder(std::move(std::move(ArtHolder)))
  96. , slot(Slot)
  97. {
  98. }
  99. template <typename T>
  100. bool isHolder(const T *t) const
  101. {
  102. if(auto ptrToT = std::get<ConstTransitivePtr<T>>(artHolder))
  103. {
  104. return ptrToT == t;
  105. }
  106. return false;
  107. }
  108. DLL_LINKAGE void removeArtifact(); // BE CAREFUL, this operation modifies holder (gs)
  109. DLL_LINKAGE const CArmedInstance *relatedObj() const; //hero or the stack owner
  110. DLL_LINKAGE PlayerColor owningPlayer() const;
  111. DLL_LINKAGE CArtifactSet *getHolderArtSet();
  112. DLL_LINKAGE CBonusSystemNode *getHolderNode();
  113. DLL_LINKAGE CArtifactSet *getHolderArtSet() const;
  114. DLL_LINKAGE const CBonusSystemNode *getHolderNode() const;
  115. DLL_LINKAGE const CArtifactInstance *getArt() const;
  116. DLL_LINKAGE CArtifactInstance *getArt();
  117. DLL_LINKAGE const ArtSlotInfo *getSlot() const;
  118. template <typename Handler> void serialize(Handler &h, const int version)
  119. {
  120. h & artHolder;
  121. h & slot;
  122. }
  123. };
  124. VCMI_LIB_NAMESPACE_END