CObstacleInstance.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "StdInc.h"
  2. #include "CObstacleInstance.h"
  3. #include "CHeroHandler.h"
  4. #include "CTownHandler.h"
  5. #include "VCMI_Lib.h"
  6. #include "spells/CSpellHandler.h"
  7. /*
  8. * CObstacleInstance.cpp, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. CObstacleInstance::CObstacleInstance()
  17. {
  18. obstacleType = USUAL;
  19. uniqueID = -1;
  20. ID = -1;
  21. }
  22. CObstacleInstance::~CObstacleInstance()
  23. {
  24. }
  25. const CObstacleInfo & CObstacleInstance::getInfo() const
  26. {
  27. switch(obstacleType)
  28. {
  29. case ABSOLUTE_OBSTACLE:
  30. return VLC->heroh->absoluteObstacles[ID];
  31. case USUAL:
  32. return VLC->heroh->obstacles[ID];
  33. default:
  34. throw std::runtime_error("Unknown obstacle type in CObstacleInstance::getInfo()");
  35. }
  36. }
  37. std::vector<BattleHex> CObstacleInstance::getBlockedTiles() const
  38. {
  39. if(blocksTiles())
  40. return getAffectedTiles();
  41. return std::vector<BattleHex>();
  42. }
  43. std::vector<BattleHex> CObstacleInstance::getStoppingTile() const
  44. {
  45. if(stopsMovement())
  46. return getAffectedTiles();
  47. return std::vector<BattleHex>();
  48. }
  49. std::vector<BattleHex> CObstacleInstance::getAffectedTiles() const
  50. {
  51. switch(obstacleType)
  52. {
  53. case ABSOLUTE_OBSTACLE:
  54. case USUAL:
  55. return getInfo().getBlocked(pos);
  56. default:
  57. assert(0);
  58. return std::vector<BattleHex>();
  59. }
  60. }
  61. // bool CObstacleInstance::spellGenerated() const
  62. // {
  63. // if(obstacleType == USUAL || obstacleType == ABSOLUTE_OBSTACLE)
  64. // return false;
  65. //
  66. // return true;
  67. // }
  68. bool CObstacleInstance::visibleForSide(ui8 side, bool hasNativeStack) const
  69. {
  70. //by default obstacle is visible for everyone
  71. return true;
  72. }
  73. bool CObstacleInstance::stopsMovement() const
  74. {
  75. return obstacleType == QUICKSAND || obstacleType == MOAT;
  76. }
  77. bool CObstacleInstance::blocksTiles() const
  78. {
  79. return obstacleType == USUAL || obstacleType == ABSOLUTE_OBSTACLE || obstacleType == FORCE_FIELD;
  80. }
  81. SpellCreatedObstacle::SpellCreatedObstacle()
  82. {
  83. casterSide = -1;
  84. spellLevel = -1;
  85. casterSpellPower = -1;
  86. turnsRemaining = -1;
  87. visibleForAnotherSide = -1;
  88. }
  89. bool SpellCreatedObstacle::visibleForSide(ui8 side, bool hasNativeStack) const
  90. {
  91. switch(obstacleType)
  92. {
  93. case FIRE_WALL:
  94. case FORCE_FIELD:
  95. //these are always visible
  96. return true;
  97. case QUICKSAND:
  98. case LAND_MINE:
  99. //we hide mines and not discovered quicksands
  100. //quicksands are visible to the caster or if owned unit stepped into that particular patch
  101. //additionally if side has a native unit, mines/quicksands will be visible
  102. return casterSide == side || visibleForAnotherSide || hasNativeStack;
  103. default:
  104. assert(0);
  105. return false;
  106. }
  107. }
  108. std::vector<BattleHex> SpellCreatedObstacle::getAffectedTiles() const
  109. {
  110. switch(obstacleType)
  111. {
  112. case QUICKSAND:
  113. case LAND_MINE:
  114. case FIRE_WALL:
  115. return std::vector<BattleHex>(1, pos);
  116. case FORCE_FIELD:
  117. return SpellID(SpellID::FORCE_FIELD).toSpell()->rangeInHexes(pos, spellLevel, casterSide);
  118. default:
  119. assert(0);
  120. return std::vector<BattleHex>();
  121. }
  122. }
  123. void SpellCreatedObstacle::battleTurnPassed()
  124. {
  125. if(turnsRemaining > 0)
  126. turnsRemaining--;
  127. }
  128. std::vector<BattleHex> MoatObstacle::getAffectedTiles() const
  129. {
  130. return VLC->townh->factions[ID]->town->moatHexes;
  131. }