CObstacleInstance.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * CObstacleInstance.cpp, 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. #include "StdInc.h"
  11. #include "CObstacleInstance.h"
  12. #include "../CHeroHandler.h"
  13. #include "../CTownHandler.h"
  14. #include "../ObstacleHandler.h"
  15. #include "../VCMI_Lib.h"
  16. #include "../NetPacksBase.h"
  17. #include "../serializer/JsonDeserializer.h"
  18. #include "../serializer/JsonSerializer.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. const ObstacleInfo & CObstacleInstance::getInfo() const
  21. {
  22. assert( obstacleType == USUAL || obstacleType == ABSOLUTE_OBSTACLE);
  23. return *Obstacle(ID).getInfo();
  24. }
  25. std::vector<BattleHex> CObstacleInstance::getBlockedTiles() const
  26. {
  27. if(blocksTiles())
  28. return getAffectedTiles();
  29. return std::vector<BattleHex>();
  30. }
  31. std::vector<BattleHex> CObstacleInstance::getStoppingTile() const
  32. {
  33. if(stopsMovement())
  34. return getAffectedTiles();
  35. return std::vector<BattleHex>();
  36. }
  37. std::vector<BattleHex> CObstacleInstance::getAffectedTiles() const
  38. {
  39. switch(obstacleType)
  40. {
  41. case ABSOLUTE_OBSTACLE:
  42. case USUAL:
  43. return getInfo().getBlocked(pos);
  44. default:
  45. assert(0);
  46. return std::vector<BattleHex>();
  47. }
  48. }
  49. bool CObstacleInstance::visibleForSide(ui8 side, bool hasNativeStack) const
  50. {
  51. //by default obstacle is visible for everyone
  52. return true;
  53. }
  54. int CObstacleInstance::getAnimationYOffset(int imageHeight) const
  55. {
  56. int offset = imageHeight % 42;
  57. if(obstacleType == CObstacleInstance::USUAL)
  58. {
  59. if(getInfo().blockedTiles.front() < 0 || offset > 37) //second or part is for holy ground ID=62,65,63
  60. offset -= 42;
  61. }
  62. return offset;
  63. }
  64. bool CObstacleInstance::stopsMovement() const
  65. {
  66. return obstacleType == MOAT;
  67. }
  68. bool CObstacleInstance::blocksTiles() const
  69. {
  70. return obstacleType == USUAL || obstacleType == ABSOLUTE_OBSTACLE ;
  71. }
  72. bool CObstacleInstance::triggersEffects() const
  73. {
  74. return false;
  75. }
  76. SpellCreatedObstacle::SpellCreatedObstacle()
  77. : turnsRemaining(-1),
  78. casterSpellPower(0),
  79. spellLevel(0),
  80. casterSide(0),
  81. hidden(false),
  82. passable(false),
  83. trigger(false),
  84. trap(false),
  85. removeOnTrigger(false),
  86. revealed(false),
  87. animationYOffset(0),
  88. nativeVisible(true),
  89. minimalDamage(0)
  90. {
  91. obstacleType = SPELL_CREATED;
  92. }
  93. bool SpellCreatedObstacle::visibleForSide(ui8 side, bool hasNativeStack) const
  94. {
  95. //we hide mines and not discovered quicksands
  96. //quicksands are visible to the caster or if owned unit stepped into that particular patch
  97. //additionally if side has a native unit, mines/quicksands will be visible
  98. //but it is not a case for a moat, so, hasNativeStack should not work for moats
  99. auto nativeVis = hasNativeStack && nativeVisible;
  100. return casterSide == side || !hidden || revealed || nativeVis;
  101. }
  102. bool SpellCreatedObstacle::blocksTiles() const
  103. {
  104. return !passable;
  105. }
  106. bool SpellCreatedObstacle::stopsMovement() const
  107. {
  108. return trap;
  109. }
  110. bool SpellCreatedObstacle::triggersEffects() const
  111. {
  112. return trigger;
  113. }
  114. void SpellCreatedObstacle::toInfo(ObstacleChanges & info)
  115. {
  116. info.id = uniqueID;
  117. info.operation = ObstacleChanges::EOperation::ADD;
  118. info.data.clear();
  119. JsonSerializer ser(nullptr, info.data);
  120. ser.serializeStruct("obstacle", *this);
  121. }
  122. void SpellCreatedObstacle::fromInfo(const ObstacleChanges & info)
  123. {
  124. uniqueID = info.id;
  125. if(info.operation != ObstacleChanges::EOperation::ADD && info.operation != ObstacleChanges::EOperation::UPDATE)
  126. logGlobal->error("ADD or UPDATE operation expected");
  127. JsonDeserializer deser(nullptr, info.data);
  128. deser.serializeStruct("obstacle", *this);
  129. }
  130. void SpellCreatedObstacle::serializeJson(JsonSerializeFormat & handler)
  131. {
  132. handler.serializeInt("spell", ID);
  133. handler.serializeInt("position", pos);
  134. handler.serializeInt("turnsRemaining", turnsRemaining);
  135. handler.serializeInt("casterSpellPower", casterSpellPower);
  136. handler.serializeInt("spellLevel", spellLevel);
  137. handler.serializeInt("casterSide", casterSide);
  138. handler.serializeInt("minimalDamage", minimalDamage);
  139. handler.serializeInt("type", obstacleType);
  140. handler.serializeBool("hidden", hidden);
  141. handler.serializeBool("revealed", revealed);
  142. handler.serializeBool("passable", passable);
  143. handler.serializeBool("trigger", trigger);
  144. handler.serializeBool("trap", trap);
  145. handler.serializeBool("removeOnTrigger", removeOnTrigger);
  146. handler.serializeBool("nativeVisible", nativeVisible);
  147. handler.serializeString("appearSound", appearSound);
  148. handler.serializeString("appearAnimation", appearAnimation);
  149. handler.serializeString("animation", animation);
  150. handler.serializeInt("animationYOffset", animationYOffset);
  151. {
  152. JsonArraySerializer customSizeJson = handler.enterArray("customSize");
  153. customSizeJson.syncSize(customSize, JsonNode::JsonType::DATA_INTEGER);
  154. for(size_t index = 0; index < customSizeJson.size(); index++)
  155. customSizeJson.serializeInt(index, customSize.at(index));
  156. }
  157. }
  158. std::vector<BattleHex> SpellCreatedObstacle::getAffectedTiles() const
  159. {
  160. return customSize;
  161. }
  162. void SpellCreatedObstacle::battleTurnPassed()
  163. {
  164. if(turnsRemaining > 0)
  165. turnsRemaining--;
  166. }
  167. int SpellCreatedObstacle::getAnimationYOffset(int imageHeight) const
  168. {
  169. int offset = imageHeight % 42;
  170. if(obstacleType == CObstacleInstance::SPELL_CREATED || obstacleType == CObstacleInstance::MOAT)
  171. {
  172. offset += animationYOffset;
  173. }
  174. return offset;
  175. }
  176. VCMI_LIB_NAMESPACE_END