Unit.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "../GameLibrary.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. bool Unit::isMeleeAttacker() const
  32. {
  33. //exclude war machines
  34. if (hasBonusOfType(BonusType::SIEGE_WEAPON))
  35. return false;
  36. //TODO consider that a mod may introduce a melee war machine. Possibly a new bonus type NO_MELEE_ATTACK is needed.
  37. return true;
  38. }
  39. std::string Unit::getDescription() const
  40. {
  41. boost::format fmt("Unit %d of side %d");
  42. fmt % unitId() % static_cast<int>(unitSide());
  43. return fmt.str();
  44. }
  45. //TODO: deduplicate these functions
  46. const IBonusBearer* Unit::getBonusBearer() const
  47. {
  48. return this;
  49. }
  50. const BattleHexArray & Unit::getSurroundingHexes(const BattleHex & assumedPosition) const
  51. {
  52. BattleHex hex = (assumedPosition.toInt() != BattleHex::INVALID) ? assumedPosition : getPosition(); //use hypothetical position
  53. return getSurroundingHexes(hex, doubleWide(), unitSide());
  54. }
  55. const BattleHexArray & Unit::getSurroundingHexes(const BattleHex & position, bool twoHex, BattleSide side)
  56. {
  57. if(!twoHex)
  58. return position.getNeighbouringTiles();
  59. return position.getNeighbouringTilesDoubleWide(side);
  60. }
  61. BattleHexArray Unit::getAttackableHexes(const Unit * attacker) const
  62. {
  63. if (!attacker->doubleWide())
  64. {
  65. return getSurroundingHexes();
  66. }
  67. else
  68. {
  69. BattleHexArray result;
  70. for (const auto & attackOrigin : getSurroundingHexes())
  71. {
  72. BattleHex occupiedHex = attacker->occupiedHex(attackOrigin);
  73. if (!coversPos(occupiedHex) && occupiedHex.isAvailable())
  74. result.insert(attackOrigin);
  75. BattleHex headHex = attackOrigin.cloneInDirection(attacker->headDirection());
  76. if (!coversPos(headHex) && headHex.isAvailable())
  77. result.insert(headHex);
  78. }
  79. return result;
  80. }
  81. }
  82. bool Unit::coversPos(const BattleHex & pos) const
  83. {
  84. return getPosition() == pos || (doubleWide() && (occupiedHex() == pos));
  85. }
  86. BattleHex::EDir Unit::headDirection() const
  87. {
  88. if(doubleWide())
  89. {
  90. if(unitSide() == BattleSide::ATTACKER)
  91. return BattleHex::EDir::RIGHT;
  92. else
  93. return BattleHex::EDir::LEFT;
  94. }
  95. else
  96. {
  97. return BattleHex::EDir::NONE;
  98. }
  99. }
  100. const BattleHexArray & Unit::getHexes() const
  101. {
  102. return getHexes(getPosition(), doubleWide(), unitSide());
  103. }
  104. const BattleHexArray & Unit::getHexes(const BattleHex & assumedPos) const
  105. {
  106. return getHexes(assumedPos, doubleWide(), unitSide());
  107. }
  108. BattleHexArray::ArrayOfBattleHexArrays Unit::precomputeUnitHexes(BattleSide side, bool twoHex)
  109. {
  110. BattleHexArray::ArrayOfBattleHexArrays result;
  111. for (BattleHex assumedPos = 0; assumedPos < GameConstants::BFIELD_SIZE; ++assumedPos)
  112. {
  113. BattleHexArray hexes;
  114. hexes.insert(assumedPos);
  115. if(twoHex)
  116. hexes.insert(occupiedHex(assumedPos, twoHex, side));
  117. result[assumedPos.toInt()] = std::move(hexes);
  118. }
  119. return result;
  120. }
  121. const BattleHexArray & Unit::getHexes(const BattleHex & assumedPos, bool twoHex, BattleSide side)
  122. {
  123. static const std::array<BattleHexArray::ArrayOfBattleHexArrays, 4> precomputed = {
  124. precomputeUnitHexes(BattleSide::ATTACKER, false),
  125. precomputeUnitHexes(BattleSide::ATTACKER, true),
  126. precomputeUnitHexes(BattleSide::DEFENDER, false),
  127. precomputeUnitHexes(BattleSide::DEFENDER, true),
  128. };
  129. static const std::array<BattleHexArray, 5> invalidHexes = {
  130. BattleHexArray({BattleHex( 0)}),
  131. BattleHexArray({BattleHex(-1)}),
  132. BattleHexArray({BattleHex(-2)}),
  133. BattleHexArray({BattleHex(-3)}),
  134. BattleHexArray({BattleHex(-4)})
  135. };
  136. if (assumedPos.isValid())
  137. {
  138. int index = side == BattleSide::ATTACKER ? 0 : 2;
  139. return precomputed[index + twoHex][assumedPos.toInt()];
  140. }
  141. else
  142. {
  143. // Towers and such
  144. return invalidHexes.at(-assumedPos.toInt());
  145. }
  146. }
  147. BattleHex Unit::occupiedHex() const
  148. {
  149. return occupiedHex(getPosition(), doubleWide(), unitSide());
  150. }
  151. BattleHex Unit::occupiedHex(const BattleHex & assumedPos) const
  152. {
  153. return occupiedHex(assumedPos, doubleWide(), unitSide());
  154. }
  155. BattleHex Unit::occupiedHex(const BattleHex & assumedPos, bool twoHex, BattleSide side)
  156. {
  157. if(twoHex)
  158. {
  159. if(side == BattleSide::ATTACKER)
  160. return assumedPos.toInt() - 1;
  161. else
  162. return assumedPos.toInt() + 1;
  163. }
  164. else
  165. {
  166. return BattleHex::INVALID;
  167. }
  168. }
  169. void Unit::addText(MetaString & text, EMetaText type, int32_t serial, const boost::logic::tribool & plural) const
  170. {
  171. if(boost::logic::indeterminate(plural))
  172. serial = LIBRARY->generaltexth->pluralText(serial, getCount());
  173. else if(plural)
  174. serial = LIBRARY->generaltexth->pluralText(serial, 2);
  175. else
  176. serial = LIBRARY->generaltexth->pluralText(serial, 1);
  177. text.appendLocalString(type, serial);
  178. }
  179. void Unit::addNameReplacement(MetaString & text, const boost::logic::tribool & plural) const
  180. {
  181. if(boost::logic::indeterminate(plural))
  182. text.replaceName(creatureId(), getCount());
  183. else if(plural)
  184. text.replaceNamePlural(creatureIndex());
  185. else
  186. text.replaceNameSingular(creatureIndex());
  187. }
  188. std::string Unit::formatGeneralMessage(const int32_t baseTextId) const
  189. {
  190. const int32_t textId = LIBRARY->generaltexth->pluralText(baseTextId, getCount());
  191. MetaString text;
  192. text.appendLocalString(EMetaText::GENERAL_TXT, textId);
  193. text.replaceName(creatureId(), getCount());
  194. return text.toString();
  195. }
  196. int Unit::getRawSurrenderCost() const
  197. {
  198. //we pay for our stack that comes from our army slots - condition eliminates summoned cres and war machines
  199. if(unitSlot().validSlot())
  200. return creatureCost() * getCount();
  201. else
  202. return 0;
  203. }
  204. ///UnitInfo
  205. void UnitInfo::serializeJson(JsonSerializeFormat & handler)
  206. {
  207. handler.serializeInt("count", count);
  208. handler.serializeId("type", type, CreatureID(CreatureID::NONE));
  209. handler.serializeInt("side", side);
  210. si16 positionValue = position.toInt();
  211. handler.serializeInt("position", positionValue);
  212. position = positionValue;
  213. handler.serializeBool("summoned", summoned);
  214. }
  215. void UnitInfo::save(JsonNode & data)
  216. {
  217. data.clear();
  218. JsonSerializer ser(nullptr, data);
  219. ser.serializeStruct("newUnitInfo", *this);
  220. }
  221. void UnitInfo::load(uint32_t id_, const JsonNode & data)
  222. {
  223. id = id_;
  224. JsonDeserializer deser(nullptr, data);
  225. deser.serializeStruct("newUnitInfo", *this);
  226. }
  227. }
  228. VCMI_LIB_NAMESPACE_END