Unit.cpp 5.8 KB

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