Unit.cpp 6.0 KB

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