2
0

CObstacleInstance.cpp 5.2 KB

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