CObstacleInstance.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 "../GameLibrary.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. BattleHexArray CObstacleInstance::getBlockedTiles() const
  23. {
  24. if(blocksTiles())
  25. return getAffectedTiles();
  26. return BattleHexArray();
  27. }
  28. BattleHexArray CObstacleInstance::getStoppingTile() const
  29. {
  30. if(stopsMovement())
  31. return getAffectedTiles();
  32. return BattleHexArray();
  33. }
  34. BattleHexArray 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 BattleHexArray();
  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. si16 posValue = pos.toInt();
  98. handler.serializeInt("position", posValue);
  99. pos = posValue;
  100. handler.serializeInt("animationYOffset", animationYOffset);
  101. handler.serializeBool("hidden", hidden);
  102. handler.serializeBool("needAnimationOffsetFix", needAnimationOffsetFix);
  103. }
  104. void CObstacleInstance::toInfo(ObstacleChanges & info, BattleChanges::EOperation operation)
  105. {
  106. info.id = uniqueID;
  107. info.operation = operation;
  108. info.data.clear();
  109. JsonSerializer ser(nullptr, info.data);
  110. ser.serializeStruct("obstacle", *this);
  111. }
  112. SpellCreatedObstacle::SpellCreatedObstacle()
  113. : turnsRemaining(-1),
  114. casterSpellPower(0),
  115. spellLevel(0),
  116. casterSide(BattleSide::NONE),
  117. hidden(false),
  118. passable(false),
  119. trigger(false),
  120. trap(false),
  121. removeOnTrigger(false),
  122. revealed(false),
  123. animationYOffset(0),
  124. nativeVisible(true),
  125. minimalDamage(0)
  126. {
  127. obstacleType = SPELL_CREATED;
  128. }
  129. bool SpellCreatedObstacle::visibleForSide(BattleSide side, bool hasNativeStack) const
  130. {
  131. //we hide mines and not discovered quicksands
  132. //quicksands are visible to the caster or if owned unit stepped into that particular patch
  133. //additionally if side has a native unit, mines/quicksands will be visible
  134. //but it is not a case for a moat, so, hasNativeStack should not work for moats
  135. auto nativeVis = hasNativeStack && nativeVisible;
  136. return casterSide == side || !hidden || revealed || nativeVis;
  137. }
  138. bool SpellCreatedObstacle::blocksTiles() const
  139. {
  140. return !passable;
  141. }
  142. bool SpellCreatedObstacle::stopsMovement() const
  143. {
  144. return trap;
  145. }
  146. SpellID SpellCreatedObstacle::getTrigger() const
  147. {
  148. return trigger;
  149. }
  150. void SpellCreatedObstacle::fromInfo(const ObstacleChanges & info)
  151. {
  152. uniqueID = info.id;
  153. if(info.operation != ObstacleChanges::EOperation::ADD && info.operation != ObstacleChanges::EOperation::UPDATE)
  154. logGlobal->error("ADD or UPDATE operation expected");
  155. JsonDeserializer deser(nullptr, info.data);
  156. deser.serializeStruct("obstacle", *this);
  157. }
  158. void SpellCreatedObstacle::serializeJson(JsonSerializeFormat & handler)
  159. {
  160. handler.serializeInt("spell", ID);
  161. si16 posValue = pos.toInt();
  162. handler.serializeInt("position", posValue);
  163. pos = posValue;
  164. handler.serializeInt("turnsRemaining", turnsRemaining);
  165. handler.serializeInt("casterSpellPower", casterSpellPower);
  166. handler.serializeInt("spellLevel", spellLevel);
  167. handler.serializeInt("casterSide", casterSide);
  168. handler.serializeInt("minimalDamage", minimalDamage);
  169. handler.serializeInt("type", obstacleType);
  170. handler.serializeBool("hidden", hidden);
  171. handler.serializeBool("revealed", revealed);
  172. handler.serializeBool("passable", passable);
  173. handler.serializeId("trigger", trigger, SpellID::NONE);
  174. handler.serializeBool("trap", trap);
  175. handler.serializeBool("removeOnTrigger", removeOnTrigger);
  176. handler.serializeBool("nativeVisible", nativeVisible);
  177. handler.serializeStruct("appearSound", appearSound);
  178. handler.serializeStruct("appearAnimation", appearAnimation);
  179. handler.serializeStruct("animation", animation);
  180. handler.serializeInt("animationYOffset", animationYOffset);
  181. {
  182. JsonArraySerializer customSizeJson = handler.enterArray("customSize");
  183. customSizeJson.syncSize(customSize, JsonNode::JsonType::DATA_INTEGER);
  184. for(size_t index = 0; index < customSizeJson.size(); index++)
  185. {
  186. si16 hex = customSize.at(index).toInt();
  187. customSizeJson.serializeInt(index, hex);
  188. customSize.set(index, hex);
  189. }
  190. }
  191. }
  192. BattleHexArray SpellCreatedObstacle::getAffectedTiles() const
  193. {
  194. return customSize;
  195. }
  196. void SpellCreatedObstacle::battleTurnPassed()
  197. {
  198. if(turnsRemaining > 0)
  199. turnsRemaining--;
  200. }
  201. const AnimationPath & SpellCreatedObstacle::getAnimation() const
  202. {
  203. return animation;
  204. }
  205. const AnimationPath & SpellCreatedObstacle::getAppearAnimation() const
  206. {
  207. return appearAnimation;
  208. }
  209. const AudioPath & SpellCreatedObstacle::getAppearSound() const
  210. {
  211. return appearSound;
  212. }
  213. int SpellCreatedObstacle::getAnimationYOffset(int imageHeight) const
  214. {
  215. int offset = imageHeight % 42;
  216. if(obstacleType == CObstacleInstance::SPELL_CREATED || obstacleType == CObstacleInstance::MOAT)
  217. {
  218. offset += animationYOffset;
  219. }
  220. return offset;
  221. }
  222. VCMI_LIB_NAMESPACE_END