Unit.cpp 6.0 KB

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