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