Skip to content

Commit

Permalink
Spells configuration version 2 (effect-based)
Browse files Browse the repository at this point in the history
* Indirect spell effects loading
* Json serializer improvements
* spell->canBeCastAt do not allow useless cast for any spell
* Added proxy caster class for spell-created obstacles
* Handle damage from spell-created obstacles inside mechanics
* Experimental GameState integration/regression tests
* Ignore mod settings and load only "vcmi" mod when running tests
* fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests)
* Huge improvements of BattleAI regarding spell casts
* AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells.
* Possible fix for https://bugs.vcmi.eu/view.php?id=1811
* CStack factored out to several classes
* [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional
* [Battle] Allowed BattleAction have multiple destinations
* [Spells] Converted limit|immunity to target condition
* [Spells] Use partial configuration reload for backward compatibility handling
* [Tests] Started tests for CUnitState
* Partial fixes of fire shield effect
* [Battle] Do HP calculations in 64 bits
* [BattleAI] Use threading for spell cast evaluation
* [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state)
* Implemented https://bugs.vcmi.eu/view.php?id=2811
* plug rare freeze when hypnotized unit shots vertically
* Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense
* [BattleAI] Try to not waste a cast if battle is actually won already
* Extended JsonSerializeFormat API
* fixed https://bugs.vcmi.eu/view.php?id=2847
* Any unit effect can be now chained (not only damage like Chain Lightning)
** only damage effect for now actually uses "chainFactor"
* Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
  • Loading branch information
alexvins committed Feb 8, 2018
1 parent ff2d01a commit 0b70baa
Show file tree
Hide file tree
Showing 256 changed files with 20,679 additions and 7,739 deletions.
93 changes: 64 additions & 29 deletions AI/BattleAI/AttackPossibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,88 @@
#include "StdInc.h"
#include "AttackPossibility.h"

int AttackPossibility::damageDiff() const
AttackPossibility::AttackPossibility(BattleHex tile_, const BattleAttackInfo & attack_)
: tile(tile_),
attack(attack_)
{
if (!priorities)
priorities = new Priorities();
const auto dealtDmgValue = priorities->stackEvaluator(enemy) * damageDealt;
const auto receivedDmgValue = priorities->stackEvaluator(attack.attacker) * damageReceived;
return dealtDmgValue - receivedDmgValue;
}

int AttackPossibility::attackValue() const

int64_t AttackPossibility::damageDiff() const
{
//TODO: use target priority from HypotheticBattle
const auto dealtDmgValue = damageDealt;
const auto receivedDmgValue = damageReceived;

int64_t diff = 0;

//friendly fire or not
if(attack.attacker->unitSide() == attack.defender->unitSide())
diff = -dealtDmgValue - receivedDmgValue;
else
diff = dealtDmgValue - receivedDmgValue;

//mind control
auto actualSide = getCbc()->playerToSide(getCbc()->battleGetOwner(attack.attacker));
if(actualSide && actualSide.get() != attack.attacker->unitSide())
diff = -diff;
return diff;
}

int64_t AttackPossibility::attackValue() const
{
return damageDiff() + tacticImpact;
}

AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo &AttackInfo, const HypotheticChangesToBattleState &state, BattleHex hex)
AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInfo, BattleHex hex)
{
auto attacker = AttackInfo.attacker;
auto enemy = AttackInfo.defender;
const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
static const auto selectorBlocksRetaliation = Selector::type(Bonus::BLOCKS_RETALIATION);

const bool counterAttacksBlocked = attackInfo.attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);

AttackPossibility ap(hex, attackInfo);

const int remainingCounterAttacks = getValOr(state.counterAttacksLeft, enemy, enemy->counterAttacks.available());
const bool counterAttacksBlocked = attacker->hasBonusOfType(Bonus::BLOCKS_RETALIATION) || enemy->hasBonusOfType(Bonus::NO_RETALIATION);
const int totalAttacks = 1 + AttackInfo.attackerBonuses->getBonuses(Selector::type(Bonus::ADDITIONAL_ATTACK), (Selector::effectRange (Bonus::NO_LIMIT).Or(Selector::effectRange(Bonus::ONLY_MELEE_FIGHT))))->totalValue();
ap.attackerState = attackInfo.attacker->acquireState();

AttackPossibility ap = {enemy, hex, AttackInfo, 0, 0, 0};
const int totalAttacks = ap.attackerState->getTotalAttacks(attackInfo.shooting);

auto curBai = AttackInfo; //we'll modify here the stack counts
for(int i = 0; i < totalAttacks; i++)
if(!attackInfo.shooting)
ap.attackerState->setPosition(hex);

auto defenderState = attackInfo.defender->acquireState();
ap.affectedUnits.push_back(defenderState);

for(int i = 0; i < totalAttacks; i++)
{
std::pair<ui32, ui32> retaliation(0,0);
auto attackDmg = getCbc()->battleEstimateDamage(CRandomGenerator::getDefault(), curBai, &retaliation);
ap.damageDealt = (attackDmg.first + attackDmg.second) / 2;
ap.damageReceived = (retaliation.first + retaliation.second) / 2;
TDmgRange retaliation(0,0);
auto attackDmg = getCbc()->battleEstimateDamage(ap.attack, &retaliation);

vstd::amin(attackDmg.first, defenderState->getAvailableHealth());
vstd::amin(attackDmg.second, defenderState->getAvailableHealth());

vstd::amin(retaliation.first, ap.attackerState->getAvailableHealth());
vstd::amin(retaliation.second, ap.attackerState->getAvailableHealth());

ap.damageDealt += (attackDmg.first + attackDmg.second) / 2;

if(remainingCounterAttacks <= i || counterAttacksBlocked)
ap.damageReceived = 0;
ap.attackerState->afterAttack(attackInfo.shooting, false);

curBai.attackerHealth = attacker->healthAfterAttacked(ap.damageReceived);
curBai.defenderHealth = enemy->healthAfterAttacked(ap.damageDealt);
if(curBai.attackerHealth.getCount() <= 0)
//FIXME: use ranged retaliation
if(!attackInfo.shooting && defenderState->ableToRetaliate() && !counterAttacksBlocked)
{
ap.damageReceived += (retaliation.first + retaliation.second) / 2;
defenderState->afterAttack(attackInfo.shooting, true);
}

ap.attackerState->damage(ap.damageReceived);
defenderState->damage(ap.damageDealt);

if(!ap.attackerState->alive() || !defenderState->alive())
break;
//TODO what about defender? should we break? but in pessimistic scenario defender might be alive
}

//TODO other damage related to attack (eg. fire shield and other abilities)

return ap;
}


Priorities* AttackPossibility::priorities = nullptr;
42 changes: 14 additions & 28 deletions AI/BattleAI/AttackPossibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,29 @@
*
*/
#pragma once
#include "../../lib/CStack.h"
#include "../../lib/battle/CUnitState.h"
#include "../../CCallback.h"
#include "common.h"


struct HypotheticChangesToBattleState
{
std::map<const CStack *, const IBonusBearer *> bonusesOfStacks;
std::map<const CStack *, int> counterAttacksLeft;
};

class Priorities
{
public:
std::vector<double> resourceTypeBaseValues;
std::function<double(const CStack *)> stackEvaluator;
Priorities()
{
// range::copy(VLC->objh->resVals, std::back_inserter(resourceTypeBaseValues));
stackEvaluator = [](const CStack*){ return 1.0; };
}
};
#include "StackWithBonuses.h"

class AttackPossibility
{
public:
const CStack *enemy; //redundant (to attack.defender) but looks nice
BattleHex tile; //tile from which we attack
BattleAttackInfo attack;

int damageDealt;
int damageReceived; //usually by counter-attack
int tacticImpact;
std::shared_ptr<battle::CUnitState> attackerState;

std::vector<std::shared_ptr<battle::CUnitState>> affectedUnits;

int64_t damageDealt = 0;
int64_t damageReceived = 0; //usually by counter-attack
int64_t tacticImpact = 0;

AttackPossibility(BattleHex tile_, const BattleAttackInfo & attack_);

int damageDiff() const;
int attackValue() const;
int64_t damageDiff() const;
int64_t attackValue() const;

static AttackPossibility evaluate(const BattleAttackInfo &AttackInfo, const HypotheticChangesToBattleState &state, BattleHex hex);
static Priorities * priorities;
static AttackPossibility evaluate(const BattleAttackInfo & attackInfo, BattleHex hex);
};
5 changes: 5 additions & 0 deletions AI/BattleAI/BattleAI.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@
<Add option="-Wno-sign-compare" />
<Add option="-Wno-unused-parameter" />
<Add option="-Wno-overloaded-virtual" />
<Add option="-DBOOST_THREAD_USE_LIB" />
<Add option="-DBOOST_SYSTEM_NO_DEPRECATED" />
<Add option="-D_WIN32_WINNT=0x0501" />
<Add option="-D_WIN32" />
<Add directory="$(#boost.include)" />
<Add directory="../../include" />
</Compiler>
<Linker>
<Add option="-lboost_thread$(#boost.libsuffix)" />
<Add option="-lboost_system$(#boost.libsuffix)" />
<Add option="-lVCMI_lib" />
<Add directory="../.." />
Expand All @@ -73,8 +75,11 @@
<Unit filename="AttackPossibility.h" />
<Unit filename="BattleAI.cpp" />
<Unit filename="BattleAI.h" />
<Unit filename="CMakeLists.txt" />
<Unit filename="EnemyInfo.cpp" />
<Unit filename="EnemyInfo.h" />
<Unit filename="PossibleSpellcast.cpp" />
<Unit filename="PossibleSpellcast.h" />
<Unit filename="PotentialTargets.cpp" />
<Unit filename="PotentialTargets.h" />
<Unit filename="StackWithBonuses.cpp" />
Expand Down
Loading

0 comments on commit 0b70baa

Please sign in to comment.