CObstacleInstance.cpp 5.3 KB

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