CObstacleInstance.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 "../serializer/JsonDeserializer.h"
  17. #include "../serializer/JsonSerializer.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. const ObstacleInfo & CObstacleInstance::getInfo() const
  20. {
  21. assert( obstacleType == USUAL || obstacleType == ABSOLUTE_OBSTACLE);
  22. return *Obstacle(ID).getInfo();
  23. }
  24. std::vector<BattleHex> CObstacleInstance::getBlockedTiles() const
  25. {
  26. if(blocksTiles())
  27. return getAffectedTiles();
  28. return std::vector<BattleHex>();
  29. }
  30. std::vector<BattleHex> CObstacleInstance::getStoppingTile() const
  31. {
  32. if(stopsMovement())
  33. return getAffectedTiles();
  34. return std::vector<BattleHex>();
  35. }
  36. std::vector<BattleHex> CObstacleInstance::getAffectedTiles() const
  37. {
  38. switch(obstacleType)
  39. {
  40. case ABSOLUTE_OBSTACLE:
  41. case USUAL:
  42. return getInfo().getBlocked(pos);
  43. default:
  44. assert(0);
  45. return std::vector<BattleHex>();
  46. }
  47. }
  48. bool CObstacleInstance::visibleForSide(ui8 side, bool hasNativeStack) const
  49. {
  50. //by default obstacle is visible for everyone
  51. return true;
  52. }
  53. const AnimationPath & CObstacleInstance::getAnimation() const
  54. {
  55. return getInfo().animation;
  56. }
  57. const AnimationPath & CObstacleInstance::getAppearAnimation() const
  58. {
  59. return getInfo().appearAnimation;
  60. }
  61. const AudioPath & CObstacleInstance::getAppearSound() const
  62. {
  63. return getInfo().appearSound;
  64. }
  65. int CObstacleInstance::getAnimationYOffset(int imageHeight) const
  66. {
  67. int offset = imageHeight % 42;
  68. if(obstacleType == CObstacleInstance::USUAL)
  69. {
  70. if(getInfo().blockedTiles.front() < 0 || offset > 37) //second or part is for holy ground ID=62,65,63
  71. offset -= 42;
  72. }
  73. return offset;
  74. }
  75. bool CObstacleInstance::stopsMovement() const
  76. {
  77. return obstacleType == MOAT;
  78. }
  79. bool CObstacleInstance::blocksTiles() const
  80. {
  81. return obstacleType == USUAL || obstacleType == ABSOLUTE_OBSTACLE ;
  82. }
  83. bool CObstacleInstance::triggersEffects() const
  84. {
  85. return getTrigger() != SpellID::NONE;
  86. }
  87. SpellID CObstacleInstance::getTrigger() const
  88. {
  89. return SpellID::NONE;
  90. }
  91. void CObstacleInstance::serializeJson(JsonSerializeFormat & handler)
  92. {
  93. auto hidden = false;
  94. auto needAnimationOffsetFix = obstacleType == CObstacleInstance::USUAL;
  95. int animationYOffset = 0;
  96. if(getInfo().blockedTiles.front() < 0) //TODO: holy ground ID=62,65,63
  97. animationYOffset -= 42;
  98. //We need only a subset of obstacle info for correct render
  99. handler.serializeInt("position", pos);
  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(0),
  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(ui8 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. handler.serializeInt("position", pos);
  162. handler.serializeInt("turnsRemaining", turnsRemaining);
  163. handler.serializeInt("casterSpellPower", casterSpellPower);
  164. handler.serializeInt("spellLevel", spellLevel);
  165. handler.serializeInt("casterSide", casterSide);
  166. handler.serializeInt("minimalDamage", minimalDamage);
  167. handler.serializeInt("type", obstacleType);
  168. handler.serializeBool("hidden", hidden);
  169. handler.serializeBool("revealed", revealed);
  170. handler.serializeBool("passable", passable);
  171. handler.serializeId("trigger", trigger, SpellID::NONE);
  172. handler.serializeBool("trap", trap);
  173. handler.serializeBool("removeOnTrigger", removeOnTrigger);
  174. handler.serializeBool("nativeVisible", nativeVisible);
  175. handler.serializeStruct("appearSound", appearSound);
  176. handler.serializeStruct("appearAnimation", appearAnimation);
  177. handler.serializeStruct("animation", animation);
  178. handler.serializeInt("animationYOffset", animationYOffset);
  179. {
  180. JsonArraySerializer customSizeJson = handler.enterArray("customSize");
  181. customSizeJson.syncSize(customSize, JsonNode::JsonType::DATA_INTEGER);
  182. for(size_t index = 0; index < customSizeJson.size(); index++)
  183. customSizeJson.serializeInt(index, customSize.at(index));
  184. }
  185. }
  186. std::vector<BattleHex> SpellCreatedObstacle::getAffectedTiles() const
  187. {
  188. return customSize;
  189. }
  190. void SpellCreatedObstacle::battleTurnPassed()
  191. {
  192. if(turnsRemaining > 0)
  193. turnsRemaining--;
  194. }
  195. const AnimationPath & SpellCreatedObstacle::getAnimation() const
  196. {
  197. return animation;
  198. }
  199. const AnimationPath & SpellCreatedObstacle::getAppearAnimation() const
  200. {
  201. return appearAnimation;
  202. }
  203. const AudioPath & SpellCreatedObstacle::getAppearSound() const
  204. {
  205. return appearSound;
  206. }
  207. int SpellCreatedObstacle::getAnimationYOffset(int imageHeight) const
  208. {
  209. int offset = imageHeight % 42;
  210. if(obstacleType == CObstacleInstance::SPELL_CREATED || obstacleType == CObstacleInstance::MOAT)
  211. {
  212. offset += animationYOffset;
  213. }
  214. return offset;
  215. }
  216. VCMI_LIB_NAMESPACE_END