2
0

CObstacleInstance.cpp 6.4 KB

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