Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ExecutorBase/ExecutorBase.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Game\AI\Enums\*.cs" />
<Compile Include="Game\AI\Executor.cs" />
<Compile Include="Game\AI\HintMsg.cs" />
<Compile Include="Game\AI\IMainPhaseDecisionObserver.cs" />
<Compile Include="Game\AI\Opcodes.cs" />
<Compile Include="Game\AI\ExecutorType.cs" />
<Compile Include="Game\AI\Zones.cs" />
Expand Down
6 changes: 6 additions & 0 deletions ExecutorBase/Game/AI/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ public virtual bool OnSelectMonsterSummonOrSet(ClientCard card)
return false;
}

public virtual bool TryGetMainPhaseAction(MainPhase main, out MainPhaseAction action)
{
action = null;
return false;
}

/// <summary>
/// Called when bot is going to annouce a card
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions ExecutorBase/Game/AI/IMainPhaseDecisionObserver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace WindBot.Game.AI
{
// Optional observer; other decks need not implement it.
public interface IMainPhaseDecisionObserver
{
void ObserveDuelStart();
void ObserveLegacyMainPhaseAction(string kind, int cardId, long description);
}
}
37 changes: 25 additions & 12 deletions ExecutorBase/Game/GameAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public void OnJoinGame()
/// </summary>
public void OnStart()
{
var observer = Executor as IMainPhaseDecisionObserver;
if (observer != null) observer.ObserveDuelStart();
_dialogs.SendDuelStart();
}

Expand Down Expand Up @@ -474,19 +476,30 @@ public bool OnSelectEffectYn(ClientCard card, long desc)
/// </summary>
/// <param name="main">A lot of informations about the available actions.</param>
/// <returns>A new MainPhaseAction containing the action to do.</returns>
private MainPhaseAction ObserveLegacyIdleAction(MainPhaseAction action, string kind, int cardId, long description)
{
var observer = Executor as IMainPhaseDecisionObserver;
if (observer != null) observer.ObserveLegacyMainPhaseAction(kind, cardId, description);
return action;
}

public MainPhaseAction OnSelectIdleCmd(MainPhase main)
{
CheckSurrender();
MainPhaseAction candidateAction;
if (Executor.TryGetMainPhaseAction(main, out candidateAction))
return candidateAction;

foreach (CardExecutor exec in Executor.Executors)
{
if (exec.Type == ExecutorType.GoToEndPhase && main.CanEndPhase && exec.Func()) // check if should enter end phase directly
{
_dialogs.SendEndTurn();
return new MainPhaseAction(MainPhaseAction.MainAction.ToEndPhase);
return ObserveLegacyIdleAction(new MainPhaseAction(MainPhaseAction.MainAction.ToEndPhase), "ToEndPhase", 0, -1);
}
if (exec.Type==ExecutorType.GoToBattlePhase && main.CanBattlePhase && exec.Func()) // check if should enter battle phase directly
{
return new MainPhaseAction(MainPhaseAction.MainAction.ToBattlePhase);
return ObserveLegacyIdleAction(new MainPhaseAction(MainPhaseAction.MainAction.ToBattlePhase), "ToBattlePhase", 0, -1);
}
// NOTICE: GoToBattlePhase and GoToEndPhase has no "card" can be accessed to ShouldExecute(), so instead use exec.Func() to check ...
// enter end phase and enter battle pahse is in higher priority.
Expand All @@ -497,60 +510,60 @@ public MainPhaseAction OnSelectIdleCmd(MainPhase main)
if (ShouldExecute(exec, card, ExecutorType.Activate, main.ActivableDescs[i]))
{
_dialogs.SendActivate(card.Name);
return new MainPhaseAction(MainPhaseAction.MainAction.Activate, card.ActionActivateIndex[main.ActivableDescs[i]]);
return ObserveLegacyIdleAction(new MainPhaseAction(MainPhaseAction.MainAction.Activate, card.ActionActivateIndex[main.ActivableDescs[i]]), "Activate", card.Id, main.ActivableDescs[i]);
}
}
foreach (ClientCard card in main.MonsterSetableCards)
{
if (ShouldExecute(exec, card, ExecutorType.MonsterSet))
{
_dialogs.SendSetMonster();
return new MainPhaseAction(MainPhaseAction.MainAction.SetMonster, card.ActionIndex);
return ObserveLegacyIdleAction(new MainPhaseAction(MainPhaseAction.MainAction.SetMonster, card.ActionIndex), "SetMonster", card.Id, -1);
}
}
foreach (ClientCard card in main.ReposableCards)
{
if (ShouldExecute(exec, card, ExecutorType.Repos))
return new MainPhaseAction(MainPhaseAction.MainAction.Repos, card.ActionIndex);
return ObserveLegacyIdleAction(new MainPhaseAction(MainPhaseAction.MainAction.Repos, card.ActionIndex), "Repos", card.Id, -1);
}
foreach (ClientCard card in main.SpecialSummonableCards)
{
if (ShouldExecute(exec, card, ExecutorType.SpSummon))
{
_dialogs.SendSummon(card.Name);
return new MainPhaseAction(MainPhaseAction.MainAction.SpSummon, card.ActionIndex);
return ObserveLegacyIdleAction(new MainPhaseAction(MainPhaseAction.MainAction.SpSummon, card.ActionIndex), "SpSummon", card.Id, -1);
}
}
foreach (ClientCard card in main.SummonableCards)
{
if (ShouldExecute(exec, card, ExecutorType.Summon))
{
_dialogs.SendSummon(card.Name);
return new MainPhaseAction(MainPhaseAction.MainAction.Summon, card.ActionIndex);
return ObserveLegacyIdleAction(new MainPhaseAction(MainPhaseAction.MainAction.Summon, card.ActionIndex), "Summon", card.Id, -1);
}
if (ShouldExecute(exec, card, ExecutorType.SummonOrSet))
{
if (main.MonsterSetableCards.Contains(card) && Executor.OnSelectMonsterSummonOrSet(card))
{
_dialogs.SendSetMonster();
return new MainPhaseAction(MainPhaseAction.MainAction.SetMonster, card.ActionIndex);
return ObserveLegacyIdleAction(new MainPhaseAction(MainPhaseAction.MainAction.SetMonster, card.ActionIndex), "SetMonster", card.Id, -1);
}
_dialogs.SendSummon(card.Name);
return new MainPhaseAction(MainPhaseAction.MainAction.Summon, card.ActionIndex);
return ObserveLegacyIdleAction(new MainPhaseAction(MainPhaseAction.MainAction.Summon, card.ActionIndex), "Summon", card.Id, -1);
}
}
foreach (ClientCard card in main.SpellSetableCards)
{
if (ShouldExecute(exec, card, ExecutorType.SpellSet))
return new MainPhaseAction(MainPhaseAction.MainAction.SetSpell, card.ActionIndex);
return ObserveLegacyIdleAction(new MainPhaseAction(MainPhaseAction.MainAction.SetSpell, card.ActionIndex), "SetSpell", card.Id, -1);
}
}

if (main.CanBattlePhase && Duel.Fields[0].HasAttackingMonster())
return new MainPhaseAction(MainPhaseAction.MainAction.ToBattlePhase);
return ObserveLegacyIdleAction(new MainPhaseAction(MainPhaseAction.MainAction.ToBattlePhase), "ToBattlePhase", 0, -1);

_dialogs.SendEndTurn();
return new MainPhaseAction(MainPhaseAction.MainAction.ToEndPhase);
return ObserveLegacyIdleAction(new MainPhaseAction(MainPhaseAction.MainAction.ToEndPhase), "ToEndPhase", 0, -1);
}

/// <summary>
Expand Down
21 changes: 21 additions & 0 deletions Game/AI/Decks/ActionCandidate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace RyzealAI.Prototype
{
public enum ActionType { NormalSummon, SpecialSummon, Activate }

// AI policy가 평가할 수 있는, 엔진이 이미 합법으로 제시한 행동 1개.
public sealed class ActionCandidate
{
public ActionType ActionType { get; set; }
public int CardId { get; set; }
public int ActionIndex { get; set; }
public long ActivationDescription { get; set; }
public double BaselineScore { get; set; }
public string Label { get; set; }
public string GoalTag { get; set; }
// Planned prompt choices, for traceability. This is not an engine legality source.
public string SelectionPlan { get; set; }
public Func<bool> Commit { get; set; }
}
}
Loading