Commit 98f7c3e1 authored by mercury233's avatar mercury233

update OnBattle

parent 5870c2d9
...@@ -45,13 +45,40 @@ namespace WindBot.Game.AI ...@@ -45,13 +45,40 @@ namespace WindBot.Game.AI
AddExecutor(ExecutorType.Activate, _CardId.ChickenGame, DefaultChickenGame); AddExecutor(ExecutorType.Activate, _CardId.ChickenGame, DefaultChickenGame);
} }
/// <summary>
/// Decide which card should the attacker attack.
/// </summary>
/// <param name="attacker">Card that attack.</param>
/// <param name="defenders">Cards that defend.</param>
/// <returns>BattlePhaseAction including the target, or null (in this situation, GameAI will check the next attacker)</returns>
public override BattlePhaseAction OnSelectAttackTarget(ClientCard attacker, IList<ClientCard> defenders)
{
for (int i = 0; i < defenders.Count; ++i)
{
ClientCard defender = defenders[i];
attacker.RealPower = attacker.Attack;
defender.RealPower = defender.GetDefensePower();
if (!OnPreBattleBetween(attacker, defender))
continue;
if (attacker.RealPower > defender.RealPower || (attacker.RealPower >= defender.RealPower && attacker.IsLastAttacker))
return AI.Attack(attacker, defender);
}
if (attacker.CanDirectAttack)
return AI.Attack(attacker, null);
return null;
}
/// <summary> /// <summary>
/// Decide whether to declare attack between attacker and defender. /// Decide whether to declare attack between attacker and defender.
/// Can be overrided to update the RealPower of attacker for cards like Honest. /// Can be overrided to update the RealPower of attacker for cards like Honest.
/// </summary> /// </summary>
/// <param name="attacker">Card that attack.</param> /// <param name="attacker">Card that attack.</param>
/// <param name="defender">Card that defend.</param> /// <param name="defender">Card that defend.</param>
/// <returns>false if the attack can't be done.</returns> /// <returns>false if the attack shouldn't be done.</returns>
public override bool OnPreBattleBetween(ClientCard attacker, ClientCard defender) public override bool OnPreBattleBetween(ClientCard attacker, ClientCard defender)
{ {
if (attacker.RealPower <= 0) if (attacker.RealPower <= 0)
......
...@@ -52,47 +52,26 @@ namespace WindBot.Game.AI ...@@ -52,47 +52,26 @@ namespace WindBot.Game.AI
/// <returns>A new BattlePhaseAction containing the action to do.</returns> /// <returns>A new BattlePhaseAction containing the action to do.</returns>
public virtual BattlePhaseAction OnBattle(IList<ClientCard> attackers, IList<ClientCard> defenders) public virtual BattlePhaseAction OnBattle(IList<ClientCard> attackers, IList<ClientCard> defenders)
{ {
if (attackers.Count == 0) // For overriding
return AI.ToMainPhase2(); return null;
}
if (defenders.Count == 0)
{
for (int i = attackers.Count - 1; i >= 0; --i)
{
ClientCard attacker = attackers[i];
if (attacker.Attack > 0)
return AI.Attack(attacker, null);
}
}
else
{
for (int i = defenders.Count - 1; i >= 0; --i)
{
ClientCard defender = defenders[i];
for (int j = 0; j < attackers.Count; ++j)
{
ClientCard attacker = attackers[j];
attacker.RealPower = attacker.Attack;
defender.RealPower = defender.GetDefensePower();
if (!OnPreBattleBetween(attacker, defender))
continue;
if (attacker.RealPower > defender.RealPower || (attacker.RealPower >= defender.RealPower && j == attackers.Count - 1))
return AI.Attack(attacker, defender);
}
}
for (int i = attackers.Count - 1; i >= 0; --i)
{
ClientCard attacker = attackers[i];
if (attacker.CanDirectAttack)
return AI.Attack(attacker, null);
}
}
if (!Battle.CanMainPhaseTwo) /// <summary>
return AI.Attack(attackers[0], (defenders.Count == 0) ? null : defenders[0]); /// Called when the AI has to decide which card to attack first
/// </summary>
/// <param name="attackers">List of monsters that can attcack.</param>
/// <param name="defenders">List of monsters of enemy.</param>
/// <returns>The card to attack first.</returns>
public virtual ClientCard OnSelectAttacker(IList<ClientCard> attackers, IList<ClientCard> defenders)
{
// For overriding
return null;
}
return AI.ToMainPhase2(); public virtual BattlePhaseAction OnSelectAttackTarget(ClientCard attacker, IList<ClientCard> defenders)
{
// Overrided in DefalultExecutor
return null;
} }
public virtual bool OnPreBattleBetween(ClientCard attacker, ClientCard defender) public virtual bool OnPreBattleBetween(ClientCard attacker, ClientCard defender)
......
...@@ -36,6 +36,7 @@ namespace WindBot.Game ...@@ -36,6 +36,7 @@ namespace WindBot.Game
public bool CanDirectAttack { get; set; } public bool CanDirectAttack { get; set; }
public bool ShouldDirectAttack { get; set; } public bool ShouldDirectAttack { get; set; }
public bool Attacked { get; set; } public bool Attacked { get; set; }
public bool IsLastAttacker { get; set; }
public int[] ActionIndex { get; set; } public int[] ActionIndex { get; set; }
public IDictionary<int, int> ActionActivateIndex { get; private set; } public IDictionary<int, int> ActionActivateIndex { get; private set; }
......
...@@ -143,13 +143,57 @@ namespace WindBot.Game ...@@ -143,13 +143,57 @@ namespace WindBot.Game
} }
} }
// Sort the attackers and defenders, make monster with higher attack go first.
List<ClientCard> attackers = new List<ClientCard>(battle.AttackableCards); List<ClientCard> attackers = new List<ClientCard>(battle.AttackableCards);
attackers.Sort(AIFunctions.CompareCardAttack); attackers.Sort(AIFunctions.CompareCardAttack);
attackers.Reverse();
List<ClientCard> defenders = new List<ClientCard>(Duel.Fields[1].GetMonsters()); List<ClientCard> defenders = new List<ClientCard>(Duel.Fields[1].GetMonsters());
defenders.Sort(AIFunctions.CompareDefensePower); defenders.Sort(AIFunctions.CompareDefensePower);
defenders.Reverse();
return Executor.OnBattle(attackers, defenders); // Let executor decide which card should attack first.
ClientCard selected = Executor.OnSelectAttacker(attackers, defenders);
if (selected != null && attackers.Contains(selected))
{
attackers.Remove(selected);
attackers.Insert(0, selected);
}
// Check for the executor.
BattlePhaseAction result = Executor.OnBattle(attackers, defenders);
if (result != null)
return result;
if (attackers.Count == 0)
return ToMainPhase2();
if (defenders.Count == 0)
{
// Attack with the monster with the lowest attack first
for (int i = attackers.Count - 1; i >= 0; --i)
{
ClientCard attacker = attackers[i];
if (attacker.Attack > 0)
return Attack(attacker, null);
}
}
else
{
for (int k = 0; k < attackers.Count; ++k)
{
ClientCard attacker = attackers[k];
attacker.IsLastAttacker = (k == attackers.Count - 1);
result = Executor.OnSelectAttackTarget(attacker, defenders);
if (result != null)
return result;
}
}
if (!battle.CanMainPhaseTwo)
return Attack(attackers[0], (defenders.Count == 0) ? null : defenders[0]);
return ToMainPhase2();
} }
/// <summary> /// <summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment