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