diff --git a/addons/BehaviorDebug/BehaviorDebug.cs b/addons/BehaviorDebug/BehaviorDebug.cs index a259bec..59d5301 100644 --- a/addons/BehaviorDebug/BehaviorDebug.cs +++ b/addons/BehaviorDebug/BehaviorDebug.cs @@ -13,14 +13,14 @@ public partial class BehaviorDebug : EditorPlugin private partial class BtEditorDebugger : EditorDebuggerPlugin { - private BehaviorDebugControl? _behaviorTreeView1; + private BehaviorDebugControl? _behaviorDebugControl; private static readonly PackedScene DebuggerScene = GD.Load("res://addons/BehaviorDebug/behavior_debug_dock.tscn"); public override bool _HasCapture(string capture) => capture == "behavior_debug"; public override bool _Capture(string message, Array data, int sessionId) { if (message == "behavior_debug:add_tree") { - _behaviorTreeView1?.AddTreeData(data); + _behaviorDebugControl?.AddTreeData(data); return true; } return false; @@ -28,10 +28,11 @@ public partial class BehaviorDebug : EditorPlugin public override void _SetupSession(int sessionId) { - _behaviorTreeView1 = DebuggerScene.Instantiate(); - _behaviorTreeView1.Name = "Behavior Trees"; + _behaviorDebugControl = DebuggerScene.Instantiate(); + _behaviorDebugControl.Name = "Behavior Trees"; var session = GetSession(sessionId); - session.AddSessionTab(_behaviorTreeView1); + session.AddSessionTab(_behaviorDebugControl); + session.Started += _behaviorDebugControl.Clear; } } } diff --git a/addons/BehaviorDebug/BehaviorDebugControl.cs b/addons/BehaviorDebug/BehaviorDebugControl.cs index 04389a7..9420a24 100644 --- a/addons/BehaviorDebug/BehaviorDebugControl.cs +++ b/addons/BehaviorDebug/BehaviorDebugControl.cs @@ -1,11 +1,8 @@ #if TOOLS using System.Diagnostics; -using System.Linq; using Godot.Collections; -namespace Dancer; - [Tool] public sealed partial class BehaviorDebugControl : Control { @@ -35,16 +32,17 @@ public sealed partial class BehaviorDebugControl : Control 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); if (idx == -1) { - _trees.Add(data); - _itemList.AddItem($"{data.instanceId}"); + _trees.Add((data.instanceId, data.tree)); + _itemList.AddItem($"{data.name} ({data.instanceId})"); return; } - _trees[idx] = data; + _trees[idx] = (data.instanceId, data.tree); if (idx == _selectedIndex) { UpdateTree(data.tree); @@ -103,19 +101,38 @@ public sealed partial class BehaviorDebugControl : Control "Selector" => "?", var other => other, }; - item.SetText(0, displayName); var state = node["state"].As(); + 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 { - "Idle" => Color.FromOkHsl(.653f, .3f, .2f), - "Running" => Color.FromOkHsl(.222f, .7f, .3f), - "Success" => Color.FromOkHsl(.375f, .3f, .2f), - "Failure" => Color.FromOkHsl(.055f, .3f, .2f), + "Idle" => Colors.Transparent, + "Running" => Color.FromOkHsl(.653f, .7f, bgLightness) with { A = bgAlpha + .1f }, + "Success" => Color.FromOkHsl(.375f, .7f, bgLightness) with { A = bgAlpha }, + "Failure" => Color.FromOkHsl(.055f, .7f, bgLightness) with { A = bgAlpha }, _ => throw new UnreachableException(), }; - item.SetCustomBgColor(1, bgColor); - item.SetCustomColor(1, Colors.AntiqueWhite); - item.SetText(1, state); + var color = textColor with { A = isStale ? .35f : 1f }; + item.SetText(0, displayName); + 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(); } } diff --git a/src/behavior/Behavior.cs b/src/behavior/Behavior.cs index 39976cb..e725d24 100644 --- a/src/behavior/Behavior.cs +++ b/src/behavior/Behavior.cs @@ -13,23 +13,27 @@ public sealed class Tree(Node root) public EntityTable Blackboard { get; } = new(); public Random Random { get; init; } = new(); public AnimationPlayer? AnimationPlayer { get; init; } + private int _tickNumber; public Result Tick() => Process(0f); public Result Process(double delta) { - return Root.Tick(new() + var result = Root.Tick(new() { Delta = delta, Blackboard = Blackboard, Random = Random, AnimationPlayer = AnimationPlayer, + TickNumber = _tickNumber, }); + _tickNumber += 1; + return result; } public void Halt() => Root.Halt(); - public Godot.Collections.Dictionary DebugData => Root.DebugData; + public Godot.Collections.Dictionary DebugData => Root.GetDebugData(_tickNumber - 1); } public enum Result @@ -53,9 +57,11 @@ public abstract class Node public NodeState State { get; private set; } = NodeState.Idle; public virtual Node[] Children => []; public virtual string? Name { get; init; } + private int _lastTicked = -1; public Result Tick(in Context context) { + _lastTicked = context.TickNumber; if (State is not NodeState.Running) OnStart(context); var result = OnTick(in context); State = result.ToState(); @@ -88,7 +94,7 @@ public abstract class Node /// /// Called whenever this node is halted forcibly, - /// but only if its state is . + /// but only if its state is not . /// Therefore, this will never be called twice in a row. /// Before being ticked again, is guaranteed /// 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() { ["type"] = GetType().Name, ["name"] = Name ?? "", ["state"] = State.ToString(), + ["is_stale"] = lastTick != _lastTicked, ["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 Random Random { get; init; } public AnimationPlayer? AnimationPlayer { get; init; } + public int TickNumber { get; init; } } public static class BehaviorTreeExtensions @@ -148,10 +156,6 @@ public abstract class ListNode(Node[] children) : Node, IEnumerable public IEnumerator GetEnumerator() => ((IEnumerable)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) @@ -181,13 +185,9 @@ public sealed class Sequence(Node[] children) : ListNode(children) } LoopDone: - // if (State is NodeState.Running && childIdx < _runningChildIdx) - // { - // Children[_runningChildIdx].Halt(); - // } - if (State is NodeState.Running or NodeState.Failure) + if (State is NodeState.Running && childIdx < _runningChildIdx) { - for (var i = childIdx + 1; i < Children.Length; i++) Children[i].Halt(); + Children[_runningChildIdx].Halt(); } if (result is Result.Running) _runningChildIdx = childIdx; @@ -195,7 +195,7 @@ public sealed class Sequence(Node[] children) : ListNode(children) return result; } - // protected override void OnHalt() => Children[_runningChildIdx].Halt(); + protected override void OnHalt() => Children[_runningChildIdx].Halt(); } public sealed class MemoSequence(Node[] children) : ListNode(children) @@ -228,15 +228,11 @@ public sealed class MemoSequence(Node[] children) : ListNode(children) LoopDone: 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; } - // protected override void OnHalt() => Children[_runningChildIdx].Halt(); + protected override void OnHalt() => Children[_runningChildIdx].Halt(); } public sealed class Selector(Node[] children) : ListNode(children) @@ -266,13 +262,9 @@ public sealed class Selector(Node[] children) : ListNode(children) } LoopDone: - // if (State is NodeState.Running && childIdx < _runningChildIdx) - // { - // Children[_runningChildIdx].Halt(); - // } - if (State is NodeState.Running or NodeState.Success) + if (State is NodeState.Running && childIdx < _runningChildIdx) { - for (var i = childIdx + 1; i < Children.Length; i++) Children[i].Halt(); + Children[_runningChildIdx].Halt(); } if (result is Result.Running) _runningChildIdx = childIdx; @@ -280,7 +272,7 @@ public sealed class Selector(Node[] children) : ListNode(children) return result; } - // protected override void OnHalt() => Children[_runningChildIdx].Halt(); + protected override void OnHalt() => Children[_runningChildIdx].Halt(); } public sealed class DoNothing : Node diff --git a/src/creatures/birb/Birb.cs b/src/creatures/birb/Birb.cs index a27a1b8..8802734 100644 --- a/src/creatures/birb/Birb.cs +++ b/src/creatures/birb/Birb.cs @@ -52,27 +52,28 @@ public sealed partial class Birb : CharacterBody2D, IAttackable, { if (EngineDebugger.IsActive()) EngineDebugger.SendMessage("behavior_debug:add_tree", - new() { GetInstanceId(), Tree.DebugData }); + new() { Name, GetInstanceId(), Tree.DebugData }); } private Bt.Tree CreateTree() => new( new Bt.Selector([ + new Bt.Check(() => _state is State.Attacking) { Name = "Is state Attacking?" }, new Bt.Sequence([ 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.MemoSequence([ 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.Action(Stop) { Name = "Stop" }, + new Bt.Action(Stop) { Name = "Stop" }, new Bt.PlayAnimation("prepare_attack"), new Bt.Delay(0.4) { RandomVariation = 0.2 }, ]), ]), new Bt.PlayAnimation("stab"), - new Bt.Action(() => _state = State.Attacking), - new Bt.WaitUntil(() => _state is not State.Attacking) { Name = "Done attacking?" }, + new Bt.Action(() => _state = State.Attacking) { Name = "Set state to Attacking" }, ]), new Bt.MemoSequence([ new Bt.Delay(0.1) { RandomVariation = 0.05 }, @@ -94,7 +95,6 @@ public sealed partial class Birb : CharacterBody2D, IAttackable, ]), ]), new Bt.MemoSequence([ - new Bt.Check(() => _state is State.Idle), new Bt.PlayAnimation("idle"), new Bt.DoNothing(), ]),