Unit.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. bool Unit::coversPos(BattleHex pos) const
  70. {
  71. return getPosition() == pos || (doubleWide() && (occupiedHex() == pos));
  72. }
  73. std::vector<BattleHex> Unit::getHexes() const
  74. {
  75. return getHexes(getPosition(), doubleWide(), unitSide());
  76. }
  77. std::vector<BattleHex> Unit::getHexes(BattleHex assumedPos) const
  78. {
  79. return getHexes(assumedPos, doubleWide(), unitSide());
  80. }
  81. std::vector<BattleHex> Unit::getHexes(BattleHex assumedPos, bool twoHex, ui8 side)
  82. {
  83. std::vector<BattleHex> hexes;
  84. hexes.push_back(assumedPos);
  85. if(twoHex)
  86. hexes.push_back(occupiedHex(assumedPos, twoHex, side));
  87. return hexes;
  88. }
  89. BattleHex Unit::occupiedHex() const
  90. {
  91. return occupiedHex(getPosition(), doubleWide(), unitSide());
  92. }
  93. BattleHex Unit::occupiedHex(BattleHex assumedPos) const
  94. {
  95. return occupiedHex(assumedPos, doubleWide(), unitSide());
  96. }
  97. BattleHex Unit::occupiedHex(BattleHex assumedPos, bool twoHex, ui8 side)
  98. {
  99. if(twoHex)
  100. {
  101. if(side == BattleSide::ATTACKER)
  102. return assumedPos - 1;
  103. else
  104. return assumedPos + 1;
  105. }
  106. else
  107. {
  108. return BattleHex::INVALID;
  109. }
  110. }
  111. void Unit::addText(MetaString & text, ui8 type, int32_t serial, const boost::logic::tribool & plural) const
  112. {
  113. if(boost::logic::indeterminate(plural))
  114. serial = VLC->generaltexth->pluralText(serial, getCount());
  115. else if(plural)
  116. serial = VLC->generaltexth->pluralText(serial, 2);
  117. else
  118. serial = VLC->generaltexth->pluralText(serial, 1);
  119. text.addTxt(type, serial);
  120. }
  121. void Unit::addNameReplacement(MetaString & text, const boost::logic::tribool & plural) const
  122. {
  123. if(boost::logic::indeterminate(plural))
  124. text.addCreReplacement(creatureId(), getCount());
  125. else if(plural)
  126. text.addReplacement(MetaString::CRE_PL_NAMES, creatureIndex());
  127. else
  128. text.addReplacement(MetaString::CRE_SING_NAMES, creatureIndex());
  129. }
  130. std::string Unit::formatGeneralMessage(const int32_t baseTextId) const
  131. {
  132. const int32_t textId = VLC->generaltexth->pluralText(baseTextId, getCount());
  133. MetaString text;
  134. text.addTxt(MetaString::GENERAL_TXT, textId);
  135. text.addCreReplacement(creatureId(), getCount());
  136. return text.toString();
  137. }
  138. int Unit::getRawSurrenderCost() const
  139. {
  140. //we pay for our stack that comes from our army slots - condition eliminates summoned cres and war machines
  141. if(unitSlot().validSlot())
  142. return creatureCost() * getCount();
  143. else
  144. return 0;
  145. }
  146. ///UnitInfo
  147. UnitInfo::UnitInfo()
  148. : id(0),
  149. count(0),
  150. type(),
  151. side(0),
  152. position(),
  153. summoned(false)
  154. {
  155. }
  156. void UnitInfo::serializeJson(JsonSerializeFormat & handler)
  157. {
  158. handler.serializeInt("count", count);
  159. handler.serializeId("type", type, CreatureID::NONE);
  160. handler.serializeInt("side", side);
  161. handler.serializeInt("position", position);
  162. handler.serializeBool("summoned", summoned);
  163. }
  164. void UnitInfo::save(JsonNode & data)
  165. {
  166. data.clear();
  167. JsonSerializer ser(nullptr, data);
  168. ser.serializeStruct("newUnitInfo", *this);
  169. }
  170. void UnitInfo::load(uint32_t id_, const JsonNode & data)
  171. {
  172. id = id_;
  173. JsonDeserializer deser(nullptr, data);
  174. deser.serializeStruct("newUnitInfo", *this);
  175. }
  176. }