Unit.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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(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(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 = battle::Unit::getHexes(
  56. getPosition(),
  57. doubleWide(),
  58. unitSide());
  59. BattleHexArray targetableHexes;
  60. for(auto defenderHex : defenderHexes)
  61. {
  62. auto hexes = battle::Unit::getHexes(
  63. defenderHex,
  64. attacker->doubleWide(),
  65. unitSide());
  66. if(hexes.size() == 2 && BattleHex::getDistance(hexes.front(), hexes.back()) != 1)
  67. hexes.pop_back();
  68. for(auto hex : hexes)
  69. targetableHexes.insert(hex.getNeighbouringTiles());
  70. }
  71. return targetableHexes;
  72. }
  73. bool Unit::coversPos(BattleHex pos) const
  74. {
  75. return getPosition() == pos || (doubleWide() && (occupiedHex() == pos));
  76. }
  77. const BattleHexArray & Unit::getHexes() const
  78. {
  79. return getHexes(getPosition(), doubleWide(), unitSide());
  80. }
  81. const BattleHexArray & Unit::getHexes(BattleHex assumedPos) const
  82. {
  83. return getHexes(assumedPos, doubleWide(), unitSide());
  84. }
  85. const BattleHexArray & Unit::getHexes(BattleHex assumedPos, bool twoHex, BattleSide side)
  86. {
  87. static BattleHexArray::ArrayOfBattleHexArrays precomputed[4];
  88. int index = side == BattleSide::ATTACKER ? 0 : 2;
  89. if(!precomputed[index + twoHex][assumedPos.toInt()].empty())
  90. return precomputed[index + twoHex][assumedPos.toInt()];
  91. // first run, compute
  92. BattleHexArray hexes;
  93. hexes.insert(assumedPos);
  94. if(twoHex)
  95. hexes.insert(occupiedHex(assumedPos, twoHex, side));
  96. precomputed[index + twoHex][assumedPos.toInt()] = std::move(hexes);
  97. return precomputed[index + twoHex][assumedPos.toInt()];
  98. }
  99. BattleHex Unit::occupiedHex() const
  100. {
  101. return occupiedHex(getPosition(), doubleWide(), unitSide());
  102. }
  103. BattleHex Unit::occupiedHex(BattleHex assumedPos) const
  104. {
  105. return occupiedHex(assumedPos, doubleWide(), unitSide());
  106. }
  107. BattleHex Unit::occupiedHex(BattleHex assumedPos, bool twoHex, BattleSide side)
  108. {
  109. if(twoHex)
  110. {
  111. if(side == BattleSide::ATTACKER)
  112. return assumedPos.toInt() - 1;
  113. else
  114. return assumedPos.toInt() + 1;
  115. }
  116. else
  117. {
  118. return BattleHex::INVALID;
  119. }
  120. }
  121. void Unit::addText(MetaString & text, EMetaText type, int32_t serial, const boost::logic::tribool & plural) const
  122. {
  123. if(boost::logic::indeterminate(plural))
  124. serial = VLC->generaltexth->pluralText(serial, getCount());
  125. else if(plural)
  126. serial = VLC->generaltexth->pluralText(serial, 2);
  127. else
  128. serial = VLC->generaltexth->pluralText(serial, 1);
  129. text.appendLocalString(type, serial);
  130. }
  131. void Unit::addNameReplacement(MetaString & text, const boost::logic::tribool & plural) const
  132. {
  133. if(boost::logic::indeterminate(plural))
  134. text.replaceName(creatureId(), getCount());
  135. else if(plural)
  136. text.replaceNamePlural(creatureIndex());
  137. else
  138. text.replaceNameSingular(creatureIndex());
  139. }
  140. std::string Unit::formatGeneralMessage(const int32_t baseTextId) const
  141. {
  142. const int32_t textId = VLC->generaltexth->pluralText(baseTextId, getCount());
  143. MetaString text;
  144. text.appendLocalString(EMetaText::GENERAL_TXT, textId);
  145. text.replaceName(creatureId(), getCount());
  146. return text.toString();
  147. }
  148. int Unit::getRawSurrenderCost() const
  149. {
  150. //we pay for our stack that comes from our army slots - condition eliminates summoned cres and war machines
  151. if(unitSlot().validSlot())
  152. return creatureCost() * getCount();
  153. else
  154. return 0;
  155. }
  156. ///UnitInfo
  157. void UnitInfo::serializeJson(JsonSerializeFormat & handler)
  158. {
  159. handler.serializeInt("count", count);
  160. handler.serializeId("type", type, CreatureID(CreatureID::NONE));
  161. handler.serializeInt("side", side);
  162. si16 positionValue = position.toInt();
  163. handler.serializeInt("position", positionValue);
  164. position = positionValue;
  165. handler.serializeBool("summoned", summoned);
  166. }
  167. void UnitInfo::save(JsonNode & data)
  168. {
  169. data.clear();
  170. JsonSerializer ser(nullptr, data);
  171. ser.serializeStruct("newUnitInfo", *this);
  172. }
  173. void UnitInfo::load(uint32_t id_, const JsonNode & data)
  174. {
  175. id = id_;
  176. JsonDeserializer deser(nullptr, data);
  177. deser.serializeStruct("newUnitInfo", *this);
  178. }
  179. }
  180. VCMI_LIB_NAMESPACE_END