1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/*
* Unit.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "Unit.h"
#include "../VCMI_Lib.h"
#include "../texts/CGeneralTextHandler.h"
#include "../serializer/JsonDeserializer.h"
#include "../serializer/JsonSerializer.h"
#include <vcmi/Faction.h>
#include <vcmi/FactionService.h>
VCMI_LIB_NAMESPACE_BEGIN
namespace battle
{
///Unit
Unit::~Unit() = default;
bool Unit::isDead() const
{
return !alive() && !isGhost();
}
bool Unit::isTurret() const
{
return creatureIndex() == CreatureID::ARROW_TOWERS;
}
std::string Unit::getDescription() const
{
boost::format fmt("Unit %d of side %d");
fmt % unitId() % static_cast<int>(unitSide());
return fmt.str();
}
//TODO: deduplicate these functions
const IBonusBearer* Unit::getBonusBearer() const
{
return this;
}
const BattleHexArray & Unit::getSurroundingHexes(const BattleHex & assumedPosition) const
{
BattleHex hex = (assumedPosition.toInt() != BattleHex::INVALID) ? assumedPosition : getPosition(); //use hypothetical position
return getSurroundingHexes(hex, doubleWide(), unitSide());
}
const BattleHexArray & Unit::getSurroundingHexes(const BattleHex & position, bool twoHex, BattleSide side)
{
if(!twoHex)
return position.getNeighbouringTiles();
return position.getNeighbouringTilesDoubleWide(side);
}
BattleHexArray Unit::getAttackableHexes(const Unit * attacker) const
{
const BattleHexArray & defenderHexes = getHexes();
BattleHexArray targetableHexes;
for(const auto & defenderHex : defenderHexes)
{
auto hexes = battle::Unit::getHexes(defenderHex);
if(hexes.size() == 2 && BattleHex::getDistance(hexes.front(), hexes.back()) != 1)
hexes.pop_back();
for(const auto & hex : hexes)
targetableHexes.insert(hex.getNeighbouringTiles());
}
return targetableHexes;
}
bool Unit::coversPos(const BattleHex & pos) const
{
return getPosition() == pos || (doubleWide() && (occupiedHex() == pos));
}
const BattleHexArray & Unit::getHexes() const
{
return getHexes(getPosition(), doubleWide(), unitSide());
}
const BattleHexArray & Unit::getHexes(const BattleHex & assumedPos) const
{
return getHexes(assumedPos, doubleWide(), unitSide());
}
BattleHexArray::ArrayOfBattleHexArrays Unit::precomputeUnitHexes(BattleSide side, bool twoHex)
{
BattleHexArray::ArrayOfBattleHexArrays result;
for (BattleHex assumedPos = 0; assumedPos < GameConstants::BFIELD_SIZE; ++assumedPos)
{
BattleHexArray hexes;
hexes.insert(assumedPos);
if(twoHex)
hexes.insert(occupiedHex(assumedPos, twoHex, side));
result[assumedPos.toInt()] = std::move(hexes);
}
return result;
}
const BattleHexArray & Unit::getHexes(const BattleHex & assumedPos, bool twoHex, BattleSide side)
{
static const std::array<BattleHexArray::ArrayOfBattleHexArrays, 4> precomputed = {
precomputeUnitHexes(BattleSide::ATTACKER, false),
precomputeUnitHexes(BattleSide::ATTACKER, true),
precomputeUnitHexes(BattleSide::DEFENDER, false),
precomputeUnitHexes(BattleSide::DEFENDER, true),
};
static const std::array<BattleHexArray, 5> invalidHexes = {
BattleHexArray({BattleHex( 0)}),
BattleHexArray({BattleHex(-1)}),
BattleHexArray({BattleHex(-2)}),
BattleHexArray({BattleHex(-3)}),
BattleHexArray({BattleHex(-4)})
};
if (assumedPos.isValid())
{
int index = side == BattleSide::ATTACKER ? 0 : 2;
return precomputed[index + twoHex][assumedPos.toInt()];
}
else
{
// Towers and such
return invalidHexes.at(-assumedPos.toInt());
}
}
BattleHex Unit::occupiedHex() const
{
return occupiedHex(getPosition(), doubleWide(), unitSide());
}
BattleHex Unit::occupiedHex(const BattleHex & assumedPos) const
{
return occupiedHex(assumedPos, doubleWide(), unitSide());
}
BattleHex Unit::occupiedHex(const BattleHex & assumedPos, bool twoHex, BattleSide side)
{
if(twoHex)
{
if(side == BattleSide::ATTACKER)
return assumedPos.toInt() - 1;
else
return assumedPos.toInt() + 1;
}
else
{
return BattleHex::INVALID;
}
}
void Unit::addText(MetaString & text, EMetaText type, int32_t serial, const boost::logic::tribool & plural) const
{
if(boost::logic::indeterminate(plural))
serial = VLC->generaltexth->pluralText(serial, getCount());
else if(plural)
serial = VLC->generaltexth->pluralText(serial, 2);
else
serial = VLC->generaltexth->pluralText(serial, 1);
text.appendLocalString(type, serial);
}
void Unit::addNameReplacement(MetaString & text, const boost::logic::tribool & plural) const
{
if(boost::logic::indeterminate(plural))
text.replaceName(creatureId(), getCount());
else if(plural)
text.replaceNamePlural(creatureIndex());
else
text.replaceNameSingular(creatureIndex());
}
std::string Unit::formatGeneralMessage(const int32_t baseTextId) const
{
const int32_t textId = VLC->generaltexth->pluralText(baseTextId, getCount());
MetaString text;
text.appendLocalString(EMetaText::GENERAL_TXT, textId);
text.replaceName(creatureId(), getCount());
return text.toString();
}
int Unit::getRawSurrenderCost() const
{
//we pay for our stack that comes from our army slots - condition eliminates summoned cres and war machines
if(unitSlot().validSlot())
return creatureCost() * getCount();
else
return 0;
}
///UnitInfo
void UnitInfo::serializeJson(JsonSerializeFormat & handler)
{
handler.serializeInt("count", count);
handler.serializeId("type", type, CreatureID(CreatureID::NONE));
handler.serializeInt("side", side);
si16 positionValue = position.toInt();
handler.serializeInt("position", positionValue);
position = positionValue;
handler.serializeBool("summoned", summoned);
}
void UnitInfo::save(JsonNode & data)
{
data.clear();
JsonSerializer ser(nullptr, data);
ser.serializeStruct("newUnitInfo", *this);
}
void UnitInfo::load(uint32_t id_, const JsonNode & data)
{
id = id_;
JsonDeserializer deser(nullptr, data);
deser.serializeStruct("newUnitInfo", *this);
}
}
VCMI_LIB_NAMESPACE_END
|