CObstacleInstance.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. CObstacleInstance::CObstacleInstance()
  21. {
  22. obstacleType = USUAL;
  23. uniqueID = -1;
  24. ID = -1;
  25. }
  26. CObstacleInstance::~CObstacleInstance()
  27. {
  28. }
  29. const ObstacleInfo & CObstacleInstance::getInfo() const
  30. {
  31. assert( obstacleType == USUAL || obstacleType == ABSOLUTE_OBSTACLE);
  32. return *Obstacle(ID).getInfo();
  33. }
  34. std::vector<BattleHex> CObstacleInstance::getBlockedTiles() const
  35. {
  36. if(blocksTiles())
  37. return getAffectedTiles();
  38. return std::vector<BattleHex>();
  39. }
  40. std::vector<BattleHex> CObstacleInstance::getStoppingTile() const
  41. {
  42. if(stopsMovement())
  43. return getAffectedTiles();
  44. return std::vector<BattleHex>();
  45. }
  46. std::vector<BattleHex> CObstacleInstance::getAffectedTiles() const
  47. {
  48. switch(obstacleType)
  49. {
  50. case ABSOLUTE_OBSTACLE:
  51. case USUAL:
  52. return getInfo().getBlocked(pos);
  53. default:
  54. assert(0);
  55. return std::vector<BattleHex>();
  56. }
  57. }
  58. bool CObstacleInstance::visibleForSide(ui8 side, bool hasNativeStack) const
  59. {
  60. //by default obstacle is visible for everyone
  61. return true;
  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 false;
  84. }
  85. SpellCreatedObstacle::SpellCreatedObstacle()
  86. : turnsRemaining(-1),
  87. casterSpellPower(0),
  88. spellLevel(0),
  89. casterSide(0),
  90. hidden(false),
  91. passable(false),
  92. trigger(false),
  93. trap(false),
  94. removeOnTrigger(false),
  95. revealed(false),
  96. animationYOffset(0)
  97. {
  98. obstacleType = SPELL_CREATED;
  99. }
  100. bool SpellCreatedObstacle::visibleForSide(ui8 side, bool hasNativeStack) const
  101. {
  102. //we hide mines and not discovered quicksands
  103. //quicksands are visible to the caster or if owned unit stepped into that particular patch
  104. //additionally if side has a native unit, mines/quicksands will be visible
  105. return casterSide == side || !hidden || revealed || hasNativeStack;
  106. }
  107. bool SpellCreatedObstacle::blocksTiles() const
  108. {
  109. return !passable;
  110. }
  111. bool SpellCreatedObstacle::stopsMovement() const
  112. {
  113. return trap;
  114. }
  115. bool SpellCreatedObstacle::triggersEffects() const
  116. {
  117. return trigger;
  118. }
  119. void SpellCreatedObstacle::toInfo(ObstacleChanges & info)
  120. {
  121. info.id = uniqueID;
  122. info.operation = ObstacleChanges::EOperation::ADD;
  123. info.data.clear();
  124. JsonSerializer ser(nullptr, info.data);
  125. ser.serializeStruct("obstacle", *this);
  126. }
  127. void SpellCreatedObstacle::fromInfo(const ObstacleChanges & info)
  128. {
  129. uniqueID = info.id;
  130. if(info.operation != ObstacleChanges::EOperation::ADD && info.operation != ObstacleChanges::EOperation::UPDATE)
  131. logGlobal->error("ADD or UPDATE operation expected");
  132. JsonDeserializer deser(nullptr, info.data);
  133. deser.serializeStruct("obstacle", *this);
  134. }
  135. void SpellCreatedObstacle::serializeJson(JsonSerializeFormat & handler)
  136. {
  137. handler.serializeInt("spell", ID);
  138. handler.serializeInt("position", pos);
  139. handler.serializeInt("turnsRemaining", turnsRemaining);
  140. handler.serializeInt("casterSpellPower", casterSpellPower);
  141. handler.serializeInt("spellLevel", spellLevel);
  142. handler.serializeInt("casterSide", casterSide);
  143. handler.serializeBool("hidden", hidden);
  144. handler.serializeBool("revealed", revealed);
  145. handler.serializeBool("passable", passable);
  146. handler.serializeBool("trigger", trigger);
  147. handler.serializeBool("trap", trap);
  148. handler.serializeBool("removeOnTrigger", removeOnTrigger);
  149. handler.serializeString("appearSound", appearSound);
  150. handler.serializeString("appearAnimation", appearAnimation);
  151. handler.serializeString("triggerSound", triggerSound);
  152. handler.serializeString("triggerAnimation", triggerAnimation);
  153. handler.serializeString("animation", animation);
  154. handler.serializeInt("animationYOffset", animationYOffset);
  155. {
  156. JsonArraySerializer customSizeJson = handler.enterArray("customSize");
  157. customSizeJson.syncSize(customSize, JsonNode::JsonType::DATA_INTEGER);
  158. for(size_t index = 0; index < customSizeJson.size(); index++)
  159. customSizeJson.serializeInt(index, customSize.at(index));
  160. }
  161. }
  162. std::vector<BattleHex> SpellCreatedObstacle::getAffectedTiles() const
  163. {
  164. return customSize;
  165. }
  166. void SpellCreatedObstacle::battleTurnPassed()
  167. {
  168. if(turnsRemaining > 0)
  169. turnsRemaining--;
  170. }
  171. int SpellCreatedObstacle::getAnimationYOffset(int imageHeight) const
  172. {
  173. int offset = imageHeight % 42;
  174. if(obstacleType == CObstacleInstance::SPELL_CREATED)
  175. {
  176. offset += animationYOffset;
  177. }
  178. return offset;
  179. }
  180. std::vector<BattleHex> MoatObstacle::getAffectedTiles() const
  181. {
  182. return (*VLC->townh)[ID]->town->moatHexes;
  183. }
  184. VCMI_LIB_NAMESPACE_END