CObstacleInstance.cpp 6.7 KB

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