another approach for making the debugger nicer
This commit is contained in:
parent
b5d5a8f9f3
commit
b37c78d75b
4 changed files with 65 additions and 55 deletions
|
|
@ -13,14 +13,14 @@ public partial class BehaviorDebug : EditorPlugin
|
||||||
|
|
||||||
private partial class BtEditorDebugger : EditorDebuggerPlugin
|
private partial class BtEditorDebugger : EditorDebuggerPlugin
|
||||||
{
|
{
|
||||||
private BehaviorDebugControl? _behaviorTreeView1;
|
private BehaviorDebugControl? _behaviorDebugControl;
|
||||||
private static readonly PackedScene DebuggerScene = GD.Load<PackedScene>("res://addons/BehaviorDebug/behavior_debug_dock.tscn");
|
private static readonly PackedScene DebuggerScene = GD.Load<PackedScene>("res://addons/BehaviorDebug/behavior_debug_dock.tscn");
|
||||||
public override bool _HasCapture(string capture) => capture == "behavior_debug";
|
public override bool _HasCapture(string capture) => capture == "behavior_debug";
|
||||||
public override bool _Capture(string message, Array data, int sessionId)
|
public override bool _Capture(string message, Array data, int sessionId)
|
||||||
{
|
{
|
||||||
if (message == "behavior_debug:add_tree")
|
if (message == "behavior_debug:add_tree")
|
||||||
{
|
{
|
||||||
_behaviorTreeView1?.AddTreeData(data);
|
_behaviorDebugControl?.AddTreeData(data);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -28,10 +28,11 @@ public partial class BehaviorDebug : EditorPlugin
|
||||||
|
|
||||||
public override void _SetupSession(int sessionId)
|
public override void _SetupSession(int sessionId)
|
||||||
{
|
{
|
||||||
_behaviorTreeView1 = DebuggerScene.Instantiate<BehaviorDebugControl>();
|
_behaviorDebugControl = DebuggerScene.Instantiate<BehaviorDebugControl>();
|
||||||
_behaviorTreeView1.Name = "Behavior Trees";
|
_behaviorDebugControl.Name = "Behavior Trees";
|
||||||
var session = GetSession(sessionId);
|
var session = GetSession(sessionId);
|
||||||
session.AddSessionTab(_behaviorTreeView1);
|
session.AddSessionTab(_behaviorDebugControl);
|
||||||
|
session.Started += _behaviorDebugControl.Clear;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,8 @@
|
||||||
#if TOOLS
|
#if TOOLS
|
||||||
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
|
||||||
using Godot.Collections;
|
using Godot.Collections;
|
||||||
|
|
||||||
namespace Dancer;
|
|
||||||
|
|
||||||
[Tool]
|
[Tool]
|
||||||
public sealed partial class BehaviorDebugControl : Control
|
public sealed partial class BehaviorDebugControl : Control
|
||||||
{
|
{
|
||||||
|
|
@ -35,16 +32,17 @@ public sealed partial class BehaviorDebugControl : Control
|
||||||
|
|
||||||
public void AddTreeData(Array raw)
|
public void AddTreeData(Array raw)
|
||||||
{
|
{
|
||||||
(ulong instanceId, Dictionary tree) data = (raw[0].AsUInt64(), raw[1].AsGodotDictionary());
|
(string name, ulong instanceId, Dictionary tree) data = (raw[0].AsString(), raw[1].AsUInt64(),
|
||||||
|
raw[2].AsGodotDictionary());
|
||||||
var idx = _trees.FindIndex(tuple => tuple.instanceId == data.instanceId);
|
var idx = _trees.FindIndex(tuple => tuple.instanceId == data.instanceId);
|
||||||
if (idx == -1)
|
if (idx == -1)
|
||||||
{
|
{
|
||||||
_trees.Add(data);
|
_trees.Add((data.instanceId, data.tree));
|
||||||
_itemList.AddItem($"{data.instanceId}");
|
_itemList.AddItem($"{data.name} ({data.instanceId})");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_trees[idx] = data;
|
_trees[idx] = (data.instanceId, data.tree);
|
||||||
if (idx == _selectedIndex)
|
if (idx == _selectedIndex)
|
||||||
{
|
{
|
||||||
UpdateTree(data.tree);
|
UpdateTree(data.tree);
|
||||||
|
|
@ -103,19 +101,38 @@ public sealed partial class BehaviorDebugControl : Control
|
||||||
"Selector" => "?",
|
"Selector" => "?",
|
||||||
var other => other,
|
var other => other,
|
||||||
};
|
};
|
||||||
item.SetText(0, displayName);
|
|
||||||
var state = node["state"].As<string>();
|
var state = node["state"].As<string>();
|
||||||
|
var isStale = node["is_stale"].AsBool();
|
||||||
|
|
||||||
|
var theme = EditorInterface.Singleton.GetEditorTheme();
|
||||||
|
var textColor = theme.GetColor("font_color", "Tree");
|
||||||
|
var textLightness = textColor.OkHslL;
|
||||||
|
var bgLightness = textLightness > .5f ? .3f : .8f;
|
||||||
|
|
||||||
|
var bgAlpha = isStale ? .06f : .4f;
|
||||||
var bgColor = state switch
|
var bgColor = state switch
|
||||||
{
|
{
|
||||||
"Idle" => Color.FromOkHsl(.653f, .3f, .2f),
|
"Idle" => Colors.Transparent,
|
||||||
"Running" => Color.FromOkHsl(.222f, .7f, .3f),
|
"Running" => Color.FromOkHsl(.653f, .7f, bgLightness) with { A = bgAlpha + .1f },
|
||||||
"Success" => Color.FromOkHsl(.375f, .3f, .2f),
|
"Success" => Color.FromOkHsl(.375f, .7f, bgLightness) with { A = bgAlpha },
|
||||||
"Failure" => Color.FromOkHsl(.055f, .3f, .2f),
|
"Failure" => Color.FromOkHsl(.055f, .7f, bgLightness) with { A = bgAlpha },
|
||||||
_ => throw new UnreachableException(),
|
_ => throw new UnreachableException(),
|
||||||
};
|
};
|
||||||
item.SetCustomBgColor(1, bgColor);
|
var color = textColor with { A = isStale ? .35f : 1f };
|
||||||
item.SetCustomColor(1, Colors.AntiqueWhite);
|
item.SetText(0, displayName);
|
||||||
item.SetText(1, state);
|
item.SetText(1, isStale ? $"Stale ({state})" : state);
|
||||||
|
for (var col = 0; col < 2; col++)
|
||||||
|
{
|
||||||
|
item.SetCustomColor(col, color);
|
||||||
|
item.SetCustomBgColor(col, bgColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
_itemList.Clear();
|
||||||
|
_trees.Clear();
|
||||||
|
_tree.GetRoot()?.Free();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,23 +13,27 @@ public sealed class Tree(Node root)
|
||||||
public EntityTable Blackboard { get; } = new();
|
public EntityTable Blackboard { get; } = new();
|
||||||
public Random Random { get; init; } = new();
|
public Random Random { get; init; } = new();
|
||||||
public AnimationPlayer? AnimationPlayer { get; init; }
|
public AnimationPlayer? AnimationPlayer { get; init; }
|
||||||
|
private int _tickNumber;
|
||||||
|
|
||||||
public Result Tick() => Process(0f);
|
public Result Tick() => Process(0f);
|
||||||
|
|
||||||
public Result Process(double delta)
|
public Result Process(double delta)
|
||||||
{
|
{
|
||||||
return Root.Tick(new()
|
var result = Root.Tick(new()
|
||||||
{
|
{
|
||||||
Delta = delta,
|
Delta = delta,
|
||||||
Blackboard = Blackboard,
|
Blackboard = Blackboard,
|
||||||
Random = Random,
|
Random = Random,
|
||||||
AnimationPlayer = AnimationPlayer,
|
AnimationPlayer = AnimationPlayer,
|
||||||
|
TickNumber = _tickNumber,
|
||||||
});
|
});
|
||||||
|
_tickNumber += 1;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Halt() => Root.Halt();
|
public void Halt() => Root.Halt();
|
||||||
|
|
||||||
public Godot.Collections.Dictionary DebugData => Root.DebugData;
|
public Godot.Collections.Dictionary DebugData => Root.GetDebugData(_tickNumber - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Result
|
public enum Result
|
||||||
|
|
@ -53,9 +57,11 @@ public abstract class Node
|
||||||
public NodeState State { get; private set; } = NodeState.Idle;
|
public NodeState State { get; private set; } = NodeState.Idle;
|
||||||
public virtual Node[] Children => [];
|
public virtual Node[] Children => [];
|
||||||
public virtual string? Name { get; init; }
|
public virtual string? Name { get; init; }
|
||||||
|
private int _lastTicked = -1;
|
||||||
|
|
||||||
public Result Tick(in Context context)
|
public Result Tick(in Context context)
|
||||||
{
|
{
|
||||||
|
_lastTicked = context.TickNumber;
|
||||||
if (State is not NodeState.Running) OnStart(context);
|
if (State is not NodeState.Running) OnStart(context);
|
||||||
var result = OnTick(in context);
|
var result = OnTick(in context);
|
||||||
State = result.ToState();
|
State = result.ToState();
|
||||||
|
|
@ -88,7 +94,7 @@ public abstract class Node
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called whenever this node is halted forcibly,
|
/// Called whenever this node is halted forcibly,
|
||||||
/// but only if its state is <see cref="NodeState.Running"/>.
|
/// but only if its state is not <see cref="NodeState.Idle"/>.
|
||||||
/// Therefore, this will never be called twice in a row.
|
/// Therefore, this will never be called twice in a row.
|
||||||
/// Before being ticked again, <see cref="OnStart"/> is guaranteed
|
/// Before being ticked again, <see cref="OnStart"/> is guaranteed
|
||||||
/// to be called first.
|
/// to be called first.
|
||||||
|
|
@ -97,14 +103,15 @@ public abstract class Node
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public Godot.Collections.Dictionary DebugData =>
|
public Godot.Collections.Dictionary GetDebugData(int lastTick) =>
|
||||||
new()
|
new()
|
||||||
{
|
{
|
||||||
["type"] = GetType().Name,
|
["type"] = GetType().Name,
|
||||||
["name"] = Name ?? "",
|
["name"] = Name ?? "",
|
||||||
["state"] = State.ToString(),
|
["state"] = State.ToString(),
|
||||||
|
["is_stale"] = lastTick != _lastTicked,
|
||||||
["children"] =
|
["children"] =
|
||||||
new Godot.Collections.Array(Children.Select(c => Variant.CreateFrom(c.DebugData))),
|
new Godot.Collections.Array(Children.Select(c => Variant.CreateFrom(c.GetDebugData(lastTick)))),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -114,6 +121,7 @@ public readonly record struct Context
|
||||||
public required EntityTable Blackboard { get; init; }
|
public required EntityTable Blackboard { get; init; }
|
||||||
public Random Random { get; init; }
|
public Random Random { get; init; }
|
||||||
public AnimationPlayer? AnimationPlayer { get; init; }
|
public AnimationPlayer? AnimationPlayer { get; init; }
|
||||||
|
public int TickNumber { get; init; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class BehaviorTreeExtensions
|
public static class BehaviorTreeExtensions
|
||||||
|
|
@ -148,10 +156,6 @@ public abstract class ListNode(Node[] children) : Node, IEnumerable<Node>
|
||||||
|
|
||||||
public IEnumerator<Node> GetEnumerator() => ((IEnumerable<Node>)Children).GetEnumerator();
|
public IEnumerator<Node> GetEnumerator() => ((IEnumerable<Node>)Children).GetEnumerator();
|
||||||
IEnumerator IEnumerable.GetEnumerator() => Children.GetEnumerator();
|
IEnumerator IEnumerable.GetEnumerator() => Children.GetEnumerator();
|
||||||
protected override void OnHalt()
|
|
||||||
{
|
|
||||||
foreach (var child in Children) child.Halt();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class Sequence(Node[] children) : ListNode(children)
|
public sealed class Sequence(Node[] children) : ListNode(children)
|
||||||
|
|
@ -181,13 +185,9 @@ public sealed class Sequence(Node[] children) : ListNode(children)
|
||||||
}
|
}
|
||||||
|
|
||||||
LoopDone:
|
LoopDone:
|
||||||
// if (State is NodeState.Running && childIdx < _runningChildIdx)
|
if (State is NodeState.Running && childIdx < _runningChildIdx)
|
||||||
// {
|
|
||||||
// Children[_runningChildIdx].Halt();
|
|
||||||
// }
|
|
||||||
if (State is NodeState.Running or NodeState.Failure)
|
|
||||||
{
|
{
|
||||||
for (var i = childIdx + 1; i < Children.Length; i++) Children[i].Halt();
|
Children[_runningChildIdx].Halt();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result is Result.Running) _runningChildIdx = childIdx;
|
if (result is Result.Running) _runningChildIdx = childIdx;
|
||||||
|
|
@ -195,7 +195,7 @@ public sealed class Sequence(Node[] children) : ListNode(children)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// protected override void OnHalt() => Children[_runningChildIdx].Halt();
|
protected override void OnHalt() => Children[_runningChildIdx].Halt();
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class MemoSequence(Node[] children) : ListNode(children)
|
public sealed class MemoSequence(Node[] children) : ListNode(children)
|
||||||
|
|
@ -228,15 +228,11 @@ public sealed class MemoSequence(Node[] children) : ListNode(children)
|
||||||
|
|
||||||
LoopDone:
|
LoopDone:
|
||||||
if (result is Result.Running) _runningChildIdx = childIdx;
|
if (result is Result.Running) _runningChildIdx = childIdx;
|
||||||
if (State is NodeState.Running or NodeState.Failure)
|
|
||||||
{
|
|
||||||
for (var i = childIdx + 1; i < Children.Length; i++) Children[i].Halt();
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// protected override void OnHalt() => Children[_runningChildIdx].Halt();
|
protected override void OnHalt() => Children[_runningChildIdx].Halt();
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class Selector(Node[] children) : ListNode(children)
|
public sealed class Selector(Node[] children) : ListNode(children)
|
||||||
|
|
@ -266,13 +262,9 @@ public sealed class Selector(Node[] children) : ListNode(children)
|
||||||
}
|
}
|
||||||
|
|
||||||
LoopDone:
|
LoopDone:
|
||||||
// if (State is NodeState.Running && childIdx < _runningChildIdx)
|
if (State is NodeState.Running && childIdx < _runningChildIdx)
|
||||||
// {
|
|
||||||
// Children[_runningChildIdx].Halt();
|
|
||||||
// }
|
|
||||||
if (State is NodeState.Running or NodeState.Success)
|
|
||||||
{
|
{
|
||||||
for (var i = childIdx + 1; i < Children.Length; i++) Children[i].Halt();
|
Children[_runningChildIdx].Halt();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result is Result.Running) _runningChildIdx = childIdx;
|
if (result is Result.Running) _runningChildIdx = childIdx;
|
||||||
|
|
@ -280,7 +272,7 @@ public sealed class Selector(Node[] children) : ListNode(children)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// protected override void OnHalt() => Children[_runningChildIdx].Halt();
|
protected override void OnHalt() => Children[_runningChildIdx].Halt();
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class DoNothing : Node
|
public sealed class DoNothing : Node
|
||||||
|
|
|
||||||
|
|
@ -52,18 +52,20 @@ public sealed partial class Birb : CharacterBody2D, IAttackable,
|
||||||
{
|
{
|
||||||
if (EngineDebugger.IsActive())
|
if (EngineDebugger.IsActive())
|
||||||
EngineDebugger.SendMessage("behavior_debug:add_tree",
|
EngineDebugger.SendMessage("behavior_debug:add_tree",
|
||||||
new() { GetInstanceId(), Tree.DebugData });
|
new() { Name, GetInstanceId(), Tree.DebugData });
|
||||||
}
|
}
|
||||||
|
|
||||||
private Bt.Tree CreateTree() => new(
|
private Bt.Tree CreateTree() => new(
|
||||||
new Bt.Selector([
|
new Bt.Selector([
|
||||||
|
new Bt.Check(() => _state is State.Attacking) { Name = "Is state Attacking?" },
|
||||||
new Bt.Sequence([
|
new Bt.Sequence([
|
||||||
new Bt.Check(() => Target is not null) { Name = "Has target?" },
|
new Bt.Check(() => Target is not null) { Name = "Has target?" },
|
||||||
new Bt.Action(() => FaceToward(Target!.GlobalPosition)),
|
new Bt.Action(() => FaceToward(Target!.GlobalPosition)) { Name = "Face target" },
|
||||||
new Bt.Selector([
|
new Bt.Selector([
|
||||||
new Bt.MemoSequence([
|
new Bt.MemoSequence([
|
||||||
new Bt.Sequence([
|
new Bt.Sequence([
|
||||||
new Bt.Check(() => StabHitbox.GetOverlappingBodies().Contains(Target)),
|
new Bt.Check(() => StabHitbox.GetOverlappingBodies().Contains(Target))
|
||||||
|
{ Name = "Target in stab hitbox?" },
|
||||||
new Bt.MemoSequence([
|
new Bt.MemoSequence([
|
||||||
new Bt.Action(Stop) { Name = "Stop" },
|
new Bt.Action(Stop) { Name = "Stop" },
|
||||||
new Bt.PlayAnimation("prepare_attack"),
|
new Bt.PlayAnimation("prepare_attack"),
|
||||||
|
|
@ -71,8 +73,7 @@ public sealed partial class Birb : CharacterBody2D, IAttackable,
|
||||||
]),
|
]),
|
||||||
]),
|
]),
|
||||||
new Bt.PlayAnimation("stab"),
|
new Bt.PlayAnimation("stab"),
|
||||||
new Bt.Action(() => _state = State.Attacking),
|
new Bt.Action(() => _state = State.Attacking) { Name = "Set state to Attacking" },
|
||||||
new Bt.WaitUntil(() => _state is not State.Attacking) { Name = "Done attacking?" },
|
|
||||||
]),
|
]),
|
||||||
new Bt.MemoSequence([
|
new Bt.MemoSequence([
|
||||||
new Bt.Delay(0.1) { RandomVariation = 0.05 },
|
new Bt.Delay(0.1) { RandomVariation = 0.05 },
|
||||||
|
|
@ -94,7 +95,6 @@ public sealed partial class Birb : CharacterBody2D, IAttackable,
|
||||||
]),
|
]),
|
||||||
]),
|
]),
|
||||||
new Bt.MemoSequence([
|
new Bt.MemoSequence([
|
||||||
new Bt.Check(() => _state is State.Idle),
|
|
||||||
new Bt.PlayAnimation("idle"),
|
new Bt.PlayAnimation("idle"),
|
||||||
new Bt.DoNothing(),
|
new Bt.DoNothing(),
|
||||||
]),
|
]),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue