mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-29 23:07:48 +02:00
Earthquake spell: do not target already destroyed sections
This commit is contained in:
@@ -68,7 +68,7 @@ bool Catapult::applicable(Problem & problem, const Mechanics * m) const
|
|||||||
void Catapult::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & /* eTarget */) const
|
void Catapult::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & /* eTarget */) const
|
||||||
{
|
{
|
||||||
//start with all destructible parts
|
//start with all destructible parts
|
||||||
static const std::set<EWallPart::EWallPart> possibleTargets =
|
static const std::set<EWallPart::EWallPart> potentialTargets =
|
||||||
{
|
{
|
||||||
EWallPart::KEEP,
|
EWallPart::KEEP,
|
||||||
EWallPart::BOTTOM_TOWER,
|
EWallPart::BOTTOM_TOWER,
|
||||||
@@ -80,7 +80,20 @@ void Catapult::apply(ServerCallback * server, const Mechanics * m, const EffectT
|
|||||||
EWallPart::GATE
|
EWallPart::GATE
|
||||||
};
|
};
|
||||||
|
|
||||||
assert(possibleTargets.size() == EWallPart::PARTS_COUNT);
|
assert(potentialTargets.size() == EWallPart::PARTS_COUNT);
|
||||||
|
|
||||||
|
std::set<EWallPart::EWallPart> allowedTargets;
|
||||||
|
|
||||||
|
for (auto const & target : potentialTargets)
|
||||||
|
{
|
||||||
|
auto state = m->battle()->battleGetWallState(target);
|
||||||
|
|
||||||
|
if(state != EWallState::DESTROYED && state != EWallState::NONE)
|
||||||
|
allowedTargets.insert(target);
|
||||||
|
}
|
||||||
|
assert(!allowedTargets.empty());
|
||||||
|
if (allowedTargets.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
CatapultAttack ca;
|
CatapultAttack ca;
|
||||||
ca.attacker = -1;
|
ca.attacker = -1;
|
||||||
@@ -89,21 +102,31 @@ void Catapult::apply(ServerCallback * server, const Mechanics * m, const EffectT
|
|||||||
|
|
||||||
for(int i = 0; i < targetsToAttack; i++)
|
for(int i = 0; i < targetsToAttack; i++)
|
||||||
{
|
{
|
||||||
//Any destructible part can be hit regardless of its HP. Multiple hit on same target is allowed.
|
// Hit on any existing, not destroyed targets are allowed
|
||||||
EWallPart::EWallPart target = *RandomGeneratorUtil::nextItem(possibleTargets, *server->getRNG());
|
// Multiple hit on same target are allowed.
|
||||||
|
// Potential overshots (more hits on same targets than remaining HP) are allowed
|
||||||
|
EWallPart::EWallPart target = *RandomGeneratorUtil::nextItem(allowedTargets, *server->getRNG());
|
||||||
|
|
||||||
auto state = m->battle()->battleGetWallState(target);
|
auto state = m->battle()->battleGetWallState(target);
|
||||||
|
|
||||||
if(state == EWallState::DESTROYED || state == EWallState::NONE)
|
auto attackInfo = ca.attackedParts.begin();
|
||||||
continue;
|
for ( ; attackInfo != ca.attackedParts.end(); ++attackInfo)
|
||||||
|
if ( attackInfo->attackedPart == target )
|
||||||
|
break;
|
||||||
|
|
||||||
CatapultAttack::AttackInfo attackInfo;
|
if (attackInfo == ca.attackedParts.end()) // new part
|
||||||
|
{
|
||||||
attackInfo.damageDealt = 1;
|
CatapultAttack::AttackInfo newInfo;
|
||||||
attackInfo.attackedPart = target;
|
newInfo.damageDealt = 1;
|
||||||
attackInfo.destinationTile = m->battle()->wallPartToBattleHex(target);
|
newInfo.attackedPart = target;
|
||||||
|
newInfo.destinationTile = m->battle()->wallPartToBattleHex(target);
|
||||||
ca.attackedParts.push_back(attackInfo);
|
ca.attackedParts.push_back(newInfo);
|
||||||
|
attackInfo = ca.attackedParts.end() - 1;
|
||||||
|
}
|
||||||
|
else // already damaged before, update damage
|
||||||
|
{
|
||||||
|
attackInfo->damageDealt += 1;
|
||||||
|
}
|
||||||
|
|
||||||
//removing creatures in turrets / keep if one is destroyed
|
//removing creatures in turrets / keep if one is destroyed
|
||||||
BattleHex posRemove;
|
BattleHex posRemove;
|
||||||
@@ -111,17 +134,17 @@ void Catapult::apply(ServerCallback * server, const Mechanics * m, const EffectT
|
|||||||
switch(target)
|
switch(target)
|
||||||
{
|
{
|
||||||
case EWallPart::KEEP:
|
case EWallPart::KEEP:
|
||||||
posRemove = -2;
|
posRemove = BattleHex::CASTLE_CENTRAL_TOWER;
|
||||||
break;
|
break;
|
||||||
case EWallPart::BOTTOM_TOWER:
|
case EWallPart::BOTTOM_TOWER:
|
||||||
posRemove = -3;
|
posRemove = BattleHex::CASTLE_BOTTOM_TOWER;
|
||||||
break;
|
break;
|
||||||
case EWallPart::UPPER_TOWER:
|
case EWallPart::UPPER_TOWER:
|
||||||
posRemove = -4;
|
posRemove = BattleHex::CASTLE_UPPER_TOWER;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(posRemove != BattleHex::INVALID && state - attackInfo.damageDealt <= 0) //HP enum subtraction not intuitive, consider using SiegeInfo::applyDamage
|
if(posRemove != BattleHex::INVALID && state - attackInfo->damageDealt <= 0) //HP enum subtraction not intuitive, consider using SiegeInfo::applyDamage
|
||||||
{
|
{
|
||||||
auto all = m->battle()->battleGetUnitsIf([=](const battle::Unit * unit)
|
auto all = m->battle()->battleGetUnitsIf([=](const battle::Unit * unit)
|
||||||
{
|
{
|
||||||
@@ -130,14 +153,23 @@ void Catapult::apply(ServerCallback * server, const Mechanics * m, const EffectT
|
|||||||
|
|
||||||
for(auto & elem : all)
|
for(auto & elem : all)
|
||||||
{
|
{
|
||||||
if(elem->getPosition() == posRemove)
|
if(elem->getPosition() != posRemove)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// if tower was hit multiple times, it may have been destroyed already
|
||||||
|
bool stackWasRemovedBefore = false;
|
||||||
|
for(auto & removed : removeUnits.changedStacks)
|
||||||
{
|
{
|
||||||
|
if (removed.id == elem->unitId())
|
||||||
|
stackWasRemovedBefore = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!stackWasRemovedBefore)
|
||||||
removeUnits.changedStacks.emplace_back(elem->unitId(), UnitChanges::EOperation::REMOVE);
|
removeUnits.changedStacks.emplace_back(elem->unitId(), UnitChanges::EOperation::REMOVE);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
server->apply(&ca);
|
server->apply(&ca);
|
||||||
|
|
||||||
|
|||||||
@@ -4920,13 +4920,13 @@ bool CGameHandler::makeBattleAction(BattleAction &ba)
|
|||||||
switch(attackedPart)
|
switch(attackedPart)
|
||||||
{
|
{
|
||||||
case EWallPart::KEEP:
|
case EWallPart::KEEP:
|
||||||
posRemove = -2;
|
posRemove = BattleHex::CASTLE_CENTRAL_TOWER;
|
||||||
break;
|
break;
|
||||||
case EWallPart::BOTTOM_TOWER:
|
case EWallPart::BOTTOM_TOWER:
|
||||||
posRemove = -3;
|
posRemove = BattleHex::CASTLE_BOTTOM_TOWER;
|
||||||
break;
|
break;
|
||||||
case EWallPart::UPPER_TOWER:
|
case EWallPart::UPPER_TOWER:
|
||||||
posRemove = -4;
|
posRemove = BattleHex::CASTLE_UPPER_TOWER;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user