Unit.cpp 5.8 KB

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