CObstacleInstance.cpp 5.2 KB

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