namespace MrGameEng.AI;
///
/// A small typed key/value store for an agent's working memory: perceived facts, a current target, a
/// cached path goal — whatever the considerations and actions need to share without being threaded
/// through method signatures. Keys are case-sensitive strings; values are stored boxed, so the
/// blackboard is a convenience for cold paths (perception, planning), not the per-frame hot loop.
///
public sealed class Blackboard
{
private readonly Dictionary _values = new(StringComparer.Ordinal);
/// The number of keys currently stored.
public int Count => _values.Count;
/// Stores under , replacing any existing entry.
public void Set(string key, T value) => _values[key] = value;
///
/// Reads the value under as . Returns false when
/// the key is missing or holds a value of a different type.
///
public bool TryGet(string key, out T value)
{
if (_values.TryGetValue(key, out var stored) && stored is T typed)
{
value = typed;
return true;
}
value = default!;
return false;
}
///
/// Reads the value under , or returns when the key is
/// missing or holds a different type.
///
public T GetOrDefault(string key, T fallback = default!) =>
TryGet(key, out var value) ? value : fallback;
/// True when has a value (of any type).
public bool Has(string key) => _values.ContainsKey(key);
/// Removes . Returns true when it was present.
public bool Remove(string key) => _values.Remove(key);
/// Drops every stored value.
public void Clear() => _values.Clear();
}