Unit.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "../CGeneralTextHandler.h"
  14. #include "../NetPacksBase.h"
  15. #include "../serializer/JsonDeserializer.h"
  16. #include "../serializer/JsonSerializer.h"
  17. #include <vcmi/Faction.h>
  18. #include <vcmi/FactionService.h>
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. namespace battle
  21. {
  22. ///Unit
  23. Unit::~Unit() = default;
  24. bool Unit::isDead() const
  25. {
  26. return !alive() && !isGhost();
  27. }
  28. bool Unit::isTurret() const
  29. {
  30. return creatureIndex() == CreatureID::ARROW_TOWERS;
  31. }
  32. std::string Unit::getDescription() const
  33. {
  34. boost::format fmt("Unit %d of side %d");
  35. fmt % unitId() % unitSide();
  36. return fmt.str();
  37. }
  38. //TODO: deduplicate these functions
  39. const IBonusBearer* Unit::getBonusBearer() const
  40. {
  41. return this;
  42. }
  43. TerrainId Unit::getNativeTerrain() const
  44. {
  45. const std::string cachingStringNoTerrainPenalty = "type_NO_TERRAIN_PENALTY_sANY";
  46. static const auto selectorNoTerrainPenalty = Selector::typeSubtype(Bonus::NO_TERRAIN_PENALTY, static_cast<int>(ETerrainId::ANY_TERRAIN));
  47. //this code is used in the CreatureTerrainLimiter::limit to setup battle bonuses
  48. //and in the CGHeroInstance::getNativeTerrain() to setup movement bonuses or/and penalties.
  49. return getBonusBearer()->hasBonus(selectorNoTerrainPenalty, cachingStringNoTerrainPenalty)
  50. ? TerrainId(ETerrainId::ANY_TERRAIN)
  51. : VLC->factions()->getById(getFaction())->getNativeTerrain();
  52. }
  53. std::vector<BattleHex> Unit::getSurroundingHexes(BattleHex assumedPosition) const
  54. {
  55. BattleHex hex = (assumedPosition != BattleHex::INVALID) ? assumedPosition : getPosition(); //use hypothetical position
  56. return getSurroundingHexes(hex, doubleWide(), unitSide());
  57. }
  58. std::vector<BattleHex> Unit::getSurroundingHexes(BattleHex position, bool twoHex, ui8 side)
  59. {
  60. std::vector<BattleHex> hexes;
  61. if(twoHex)
  62. {
  63. const BattleHex otherHex = occupiedHex(position, twoHex, side);
  64. if(side == BattleSide::ATTACKER)
  65. {
  66. for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
  67. BattleHex::checkAndPush(position.cloneInDirection(dir, false), hexes);
  68. BattleHex::checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false), hexes);
  69. BattleHex::checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::LEFT, false), hexes);
  70. BattleHex::checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::TOP_LEFT, false), hexes);
  71. }
  72. else
  73. {
  74. BattleHex::checkAndPush(position.cloneInDirection(BattleHex::EDir::TOP_LEFT, false), hexes);
  75. for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
  76. BattleHex::checkAndPush(otherHex.cloneInDirection(dir, false), hexes);
  77. BattleHex::checkAndPush(position.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false), hexes);
  78. BattleHex::checkAndPush(position.cloneInDirection(BattleHex::EDir::LEFT, false), hexes);
  79. }
  80. return hexes;
  81. }
  82. else
  83. {
  84. return position.neighbouringTiles();
  85. }
  86. }
  87. std::vector<BattleHex> Unit::getAttackableHexes(const Unit * attacker) const
  88. {
  89. auto defenderHexes = battle::Unit::getHexes(
  90. getPosition(),
  91. doubleWide(),
  92. unitSide());
  93. std::vector<BattleHex> targetableHexes;
  94. for(auto defenderHex : defenderHexes)
  95. {
  96. auto hexes = battle::Unit::getHexes(
  97. defenderHex,
  98. attacker->doubleWide(),
  99. unitSide());
  100. if(hexes.size() == 2 && BattleHex::getDistance(hexes.front(), hexes.back()) != 1)
  101. hexes.pop_back();
  102. for(auto hex : hexes)
  103. vstd::concatenate(targetableHexes, hex.neighbouringTiles());
  104. }
  105. vstd::removeDuplicates(targetableHexes);
  106. return targetableHexes;
  107. }
  108. bool Unit::coversPos(BattleHex pos) const
  109. {
  110. return getPosition() == pos || (doubleWide() && (occupiedHex() == pos));
  111. }
  112. std::vector<BattleHex> Unit::getHexes() const
  113. {
  114. return getHexes(getPosition(), doubleWide(), unitSide());
  115. }
  116. std::vector<BattleHex> Unit::getHexes(BattleHex assumedPos) const
  117. {
  118. return getHexes(assumedPos, doubleWide(), unitSide());
  119. }
  120. std::vector<BattleHex> Unit::getHexes(BattleHex assumedPos, bool twoHex, ui8 side)
  121. {
  122. std::vector<BattleHex> hexes;
  123. hexes.push_back(assumedPos);
  124. if(twoHex)
  125. hexes.push_back(occupiedHex(assumedPos, twoHex, side));
  126. return hexes;
  127. }
  128. BattleHex Unit::occupiedHex() const
  129. {
  130. return occupiedHex(getPosition(), doubleWide(), unitSide());
  131. }
  132. BattleHex Unit::occupiedHex(BattleHex assumedPos) const
  133. {
  134. return occupiedHex(assumedPos, doubleWide(), unitSide());
  135. }
  136. BattleHex Unit::occupiedHex(BattleHex assumedPos, bool twoHex, ui8 side)
  137. {
  138. if(twoHex)
  139. {
  140. if(side == BattleSide::ATTACKER)
  141. return assumedPos - 1;
  142. else
  143. return assumedPos + 1;
  144. }
  145. else
  146. {
  147. return BattleHex::INVALID;
  148. }
  149. }
  150. void Unit::addText(MetaString & text, ui8 type, int32_t serial, const boost::logic::tribool & plural) const
  151. {
  152. if(boost::logic::indeterminate(plural))
  153. serial = VLC->generaltexth->pluralText(serial, getCount());
  154. else if(plural)
  155. serial = VLC->generaltexth->pluralText(serial, 2);
  156. else
  157. serial = VLC->generaltexth->pluralText(serial, 1);
  158. text.addTxt(type, serial);
  159. }
  160. void Unit::addNameReplacement(MetaString & text, const boost::logic::tribool & plural) const
  161. {
  162. if(boost::logic::indeterminate(plural))
  163. text.addCreReplacement(creatureId(), getCount());
  164. else if(plural)
  165. text.addReplacement(MetaString::CRE_PL_NAMES, creatureIndex());
  166. else
  167. text.addReplacement(MetaString::CRE_SING_NAMES, creatureIndex());
  168. }
  169. std::string Unit::formatGeneralMessage(const int32_t baseTextId) const
  170. {
  171. const int32_t textId = VLC->generaltexth->pluralText(baseTextId, getCount());
  172. MetaString text;
  173. text.addTxt(MetaString::GENERAL_TXT, textId);
  174. text.addCreReplacement(creatureId(), getCount());
  175. return text.toString();
  176. }
  177. int Unit::getRawSurrenderCost() const
  178. {
  179. //we pay for our stack that comes from our army slots - condition eliminates summoned cres and war machines
  180. if(unitSlot().validSlot())
  181. return creatureCost() * getCount();
  182. else
  183. return 0;
  184. }
  185. ///UnitInfo
  186. void UnitInfo::serializeJson(JsonSerializeFormat & handler)
  187. {
  188. handler.serializeInt("count", count);
  189. handler.serializeId("type", type, CreatureID::NONE);
  190. handler.serializeInt("side", side);
  191. handler.serializeInt("position", position);
  192. handler.serializeBool("summoned", summoned);
  193. }
  194. void UnitInfo::save(JsonNode & data)
  195. {
  196. data.clear();
  197. JsonSerializer ser(nullptr, data);
  198. ser.serializeStruct("newUnitInfo", *this);
  199. }
  200. void UnitInfo::load(uint32_t id_, const JsonNode & data)
  201. {
  202. id = id_;
  203. JsonDeserializer deser(nullptr, data);
  204. deser.serializeStruct("newUnitInfo", *this);
  205. }
  206. }
  207. VCMI_LIB_NAMESPACE_END