Unit.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Unit.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 "Unit.h"
  12. #include "../VCMI_Lib.h"
  13. #include "../texts/CGeneralTextHandler.h"
  14. #include "../serializer/JsonDeserializer.h"
  15. #include "../serializer/JsonSerializer.h"
  16. #include <vcmi/Faction.h>
  17. #include <vcmi/FactionService.h>
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. namespace battle
  20. {
  21. ///Unit
  22. Unit::~Unit() = default;
  23. bool Unit::isDead() const
  24. {
  25. return !alive() && !isGhost();
  26. }
  27. bool Unit::isTurret() const
  28. {
  29. return creatureIndex() == CreatureID::ARROW_TOWERS;
  30. }
  31. std::string Unit::getDescription() const
  32. {
  33. boost::format fmt("Unit %d of side %d");
  34. fmt % unitId() % static_cast<int>(unitSide());
  35. return fmt.str();
  36. }
  37. //TODO: deduplicate these functions
  38. const IBonusBearer* Unit::getBonusBearer() const
  39. {
  40. return this;
  41. }
  42. const BattleHexArray & Unit::getSurroundingHexes(const BattleHex & assumedPosition) const
  43. {
  44. BattleHex hex = (assumedPosition.toInt() != BattleHex::INVALID) ? assumedPosition : getPosition(); //use hypothetical position
  45. return getSurroundingHexes(hex, doubleWide(), unitSide());
  46. }
  47. const BattleHexArray & Unit::getSurroundingHexes(const BattleHex & position, bool twoHex, BattleSide side)
  48. {
  49. if(!twoHex)
  50. return position.getNeighbouringTiles();
  51. return position.getNeighbouringTilesDoubleWide(side);
  52. }
  53. BattleHexArray Unit::getAttackableHexes(const Unit * attacker) const
  54. {
  55. const BattleHexArray & defenderHexes = getHexes();
  56. BattleHexArray targetableHexes;
  57. for(const auto & defenderHex : defenderHexes)
  58. {
  59. auto hexes = battle::Unit::getHexes(defenderHex);
  60. if(hexes.size() == 2 && BattleHex::getDistance(hexes.front(), hexes.back()) != 1)
  61. hexes.pop_back();
  62. for(const auto & hex : hexes)
  63. targetableHexes.insert(hex.getNeighbouringTiles());
  64. }
  65. return targetableHexes;
  66. }
  67. bool Unit::coversPos(const BattleHex & pos) const
  68. {
  69. return getPosition() == pos || (doubleWide() && (occupiedHex() == pos));
  70. }
  71. const BattleHexArray & Unit::getHexes() const
  72. {
  73. return getHexes(getPosition(), doubleWide(), unitSide());
  74. }
  75. const BattleHexArray & Unit::getHexes(const BattleHex & assumedPos) const
  76. {
  77. return getHexes(assumedPos, doubleWide(), unitSide());
  78. }
  79. BattleHexArray::ArrayOfBattleHexArrays Unit::precomputeUnitHexes(BattleSide side, bool twoHex)
  80. {
  81. BattleHexArray::ArrayOfBattleHexArrays result;
  82. for (BattleHex assumedPos = 0; assumedPos < GameConstants::BFIELD_SIZE; ++assumedPos)
  83. {
  84. BattleHexArray hexes;
  85. hexes.insert(assumedPos);
  86. if(twoHex)
  87. hexes.insert(occupiedHex(assumedPos, twoHex, side));
  88. result[assumedPos.toInt()] = std::move(hexes);
  89. }
  90. return result;
  91. }
  92. const BattleHexArray & Unit::getHexes(const BattleHex & assumedPos, bool twoHex, BattleSide side)
  93. {
  94. static const std::array<BattleHexArray::ArrayOfBattleHexArrays, 4> precomputed = {
  95. precomputeUnitHexes(BattleSide::ATTACKER, false),
  96. precomputeUnitHexes(BattleSide::ATTACKER, true),
  97. precomputeUnitHexes(BattleSide::DEFENDER, false),
  98. precomputeUnitHexes(BattleSide::DEFENDER, true),
  99. };
  100. static const std::array<BattleHexArray, 5> invalidHexes = {
  101. BattleHexArray({BattleHex( 0)}),
  102. BattleHexArray({BattleHex(-1)}),
  103. BattleHexArray({BattleHex(-2)}),
  104. BattleHexArray({BattleHex(-3)}),
  105. BattleHexArray({BattleHex(-4)})
  106. };
  107. if (assumedPos.isValid())
  108. {
  109. int index = side == BattleSide::ATTACKER ? 0 : 2;
  110. return precomputed[index + twoHex][assumedPos.toInt()];
  111. }
  112. else
  113. {
  114. // Towers and such
  115. return invalidHexes.at(-assumedPos.toInt());
  116. }
  117. }
  118. BattleHex Unit::occupiedHex() const
  119. {
  120. return occupiedHex(getPosition(), doubleWide(), unitSide());
  121. }
  122. BattleHex Unit::occupiedHex(const BattleHex & assumedPos) const
  123. {
  124. return occupiedHex(assumedPos, doubleWide(), unitSide());
  125. }
  126. BattleHex Unit::occupiedHex(const BattleHex & assumedPos, bool twoHex, BattleSide side)
  127. {
  128. if(twoHex)
  129. {
  130. if(side == BattleSide::ATTACKER)
  131. return assumedPos.toInt() - 1;
  132. else
  133. return assumedPos.toInt() + 1;
  134. }
  135. else
  136. {
  137. return BattleHex::INVALID;
  138. }
  139. }
  140. void Unit::addText(MetaString & text, EMetaText type, int32_t serial, const boost::logic::tribool & plural) const
  141. {
  142. if(boost::logic::indeterminate(plural))
  143. serial = VLC->generaltexth->pluralText(serial, getCount());
  144. else if(plural)
  145. serial = VLC->generaltexth->pluralText(serial, 2);
  146. else
  147. serial = VLC->generaltexth->pluralText(serial, 1);
  148. text.appendLocalString(type, serial);
  149. }
  150. void Unit::addNameReplacement(MetaString & text, const boost::logic::tribool & plural) const
  151. {
  152. if(boost::logic::indeterminate(plural))
  153. text.replaceName(creatureId(), getCount());
  154. else if(plural)
  155. text.replaceNamePlural(creatureIndex());
  156. else
  157. text.replaceNameSingular(creatureIndex());
  158. }
  159. std::string Unit::formatGeneralMessage(const int32_t baseTextId) const
  160. {
  161. const int32_t textId = VLC->generaltexth->pluralText(baseTextId, getCount());
  162. MetaString text;
  163. text.appendLocalString(EMetaText::GENERAL_TXT, textId);
  164. text.replaceName(creatureId(), getCount());
  165. return text.toString();
  166. }
  167. int Unit::getRawSurrenderCost() const
  168. {
  169. //we pay for our stack that comes from our army slots - condition eliminates summoned cres and war machines
  170. if(unitSlot().validSlot())
  171. return creatureCost() * getCount();
  172. else
  173. return 0;
  174. }
  175. ///UnitInfo
  176. void UnitInfo::serializeJson(JsonSerializeFormat & handler)
  177. {
  178. handler.serializeInt("count", count);
  179. handler.serializeId("type", type, CreatureID(CreatureID::NONE));
  180. handler.serializeInt("side", side);
  181. si16 positionValue = position.toInt();
  182. handler.serializeInt("position", positionValue);
  183. position = positionValue;
  184. handler.serializeBool("summoned", summoned);
  185. }
  186. void UnitInfo::save(JsonNode & data)
  187. {
  188. data.clear();
  189. JsonSerializer ser(nullptr, data);
  190. ser.serializeStruct("newUnitInfo", *this);
  191. }
  192. void UnitInfo::load(uint32_t id_, const JsonNode & data)
  193. {
  194. id = id_;
  195. JsonDeserializer deser(nullptr, data);
  196. deser.serializeStruct("newUnitInfo", *this);
  197. }
  198. }
  199. VCMI_LIB_NAMESPACE_END