2
0

CObstacleInstance.cpp 6.5 KB

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