CObstacleInstance.cpp 5.3 KB

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