Unit.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "../MetaString.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. std::vector<BattleHex> Unit::getSurroundingHexes(BattleHex assumedPosition) const
  44. {
  45. BattleHex hex = (assumedPosition != BattleHex::INVALID) ? assumedPosition : getPosition(); //use hypothetical position
  46. return getSurroundingHexes(hex, doubleWide(), unitSide());
  47. }
  48. std::vector<BattleHex> Unit::getSurroundingHexes(BattleHex position, bool twoHex, ui8 side)
  49. {
  50. std::vector<BattleHex> hexes;
  51. if(twoHex)
  52. {
  53. const BattleHex otherHex = occupiedHex(position, twoHex, side);
  54. if(side == BattleSide::ATTACKER)
  55. {
  56. for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
  57. BattleHex::checkAndPush(position.cloneInDirection(dir, false), hexes);
  58. BattleHex::checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false), hexes);
  59. BattleHex::checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::LEFT, false), hexes);
  60. BattleHex::checkAndPush(otherHex.cloneInDirection(BattleHex::EDir::TOP_LEFT, false), hexes);
  61. }
  62. else
  63. {
  64. BattleHex::checkAndPush(position.cloneInDirection(BattleHex::EDir::TOP_LEFT, false), hexes);
  65. for(auto dir = static_cast<BattleHex::EDir>(0); dir <= static_cast<BattleHex::EDir>(4); dir = static_cast<BattleHex::EDir>(dir + 1))
  66. BattleHex::checkAndPush(otherHex.cloneInDirection(dir, false), hexes);
  67. BattleHex::checkAndPush(position.cloneInDirection(BattleHex::EDir::BOTTOM_LEFT, false), hexes);
  68. BattleHex::checkAndPush(position.cloneInDirection(BattleHex::EDir::LEFT, false), hexes);
  69. }
  70. return hexes;
  71. }
  72. else
  73. {
  74. return position.neighbouringTiles();
  75. }
  76. }
  77. std::vector<BattleHex> Unit::getAttackableHexes(const Unit * attacker) const
  78. {
  79. auto defenderHexes = battle::Unit::getHexes(
  80. getPosition(),
  81. doubleWide(),
  82. unitSide());
  83. std::vector<BattleHex> targetableHexes;
  84. for(auto defenderHex : defenderHexes)
  85. {
  86. auto hexes = battle::Unit::getHexes(
  87. defenderHex,
  88. attacker->doubleWide(),
  89. unitSide());
  90. if(hexes.size() == 2 && BattleHex::getDistance(hexes.front(), hexes.back()) != 1)
  91. hexes.pop_back();
  92. for(auto hex : hexes)
  93. vstd::concatenate(targetableHexes, hex.neighbouringTiles());
  94. }
  95. vstd::removeDuplicates(targetableHexes);
  96. return targetableHexes;
  97. }
  98. bool Unit::coversPos(BattleHex pos) const
  99. {
  100. return getPosition() == pos || (doubleWide() && (occupiedHex() == pos));
  101. }
  102. std::vector<BattleHex> Unit::getHexes() const
  103. {
  104. return getHexes(getPosition(), doubleWide(), unitSide());
  105. }
  106. std::vector<BattleHex> Unit::getHexes(BattleHex assumedPos) const
  107. {
  108. return getHexes(assumedPos, doubleWide(), unitSide());
  109. }
  110. std::vector<BattleHex> Unit::getHexes(BattleHex assumedPos, bool twoHex, ui8 side)
  111. {
  112. std::vector<BattleHex> hexes;
  113. hexes.push_back(assumedPos);
  114. if(twoHex)
  115. hexes.push_back(occupiedHex(assumedPos, twoHex, side));
  116. return hexes;
  117. }
  118. BattleHex Unit::occupiedHex() const
  119. {
  120. return occupiedHex(getPosition(), doubleWide(), unitSide());
  121. }
  122. BattleHex Unit::occupiedHex(BattleHex assumedPos) const
  123. {
  124. return occupiedHex(assumedPos, doubleWide(), unitSide());
  125. }
  126. BattleHex Unit::occupiedHex(BattleHex assumedPos, bool twoHex, ui8 side)
  127. {
  128. if(twoHex)
  129. {
  130. if(side == BattleSide::ATTACKER)
  131. return assumedPos - 1;
  132. else
  133. return assumedPos + 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. handler.serializeInt("position", position);
  182. handler.serializeBool("summoned", summoned);
  183. }
  184. void UnitInfo::save(JsonNode & data)
  185. {
  186. data.clear();
  187. JsonSerializer ser(nullptr, data);
  188. ser.serializeStruct("newUnitInfo", *this);
  189. }
  190. void UnitInfo::load(uint32_t id_, const JsonNode & data)
  191. {
  192. id = id_;
  193. JsonDeserializer deser(nullptr, data);
  194. deser.serializeStruct("newUnitInfo", *this);
  195. }
  196. }
  197. VCMI_LIB_NAMESPACE_END