redesign: Agents
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Polonium.Tasks" Version="$(PoloniumTasksVersion)" />
|
||||
<None Include="summerizer" />
|
||||
<None Include="VersionInfo.props" Pack="true" PackagePath="build" Condition="Exists('VersionInfo.props')" />
|
||||
</ItemGroup>
|
||||
<Target Name="Prepare" BeforeTargets="BeforeBuild">
|
||||
|
||||
13
src/Agents/ActionSet.cs
Normal file
13
src/Agents/ActionSet.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Godot;
|
||||
using Polonium.Resources;
|
||||
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract partial class ActionSet : Node
|
||||
{
|
||||
[Signal]
|
||||
public delegate void ActionSubmittedEventHandler(ActionBody action);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,51 +1,39 @@
|
||||
using Godot;
|
||||
using Polonium.Resources;
|
||||
|
||||
namespace Polonium.Agents;
|
||||
|
||||
|
||||
public abstract class Agent
|
||||
public abstract partial class Agent : Node
|
||||
{
|
||||
protected static int ComputerCount = 0;
|
||||
[Signal]
|
||||
public delegate void ActionExecutedEventHandler(SignalHeader header, ActionBody actionBody);
|
||||
protected Knowledge Knowledge { get; set; }
|
||||
public ActionSet ActionSet { get; set; }
|
||||
|
||||
public string Name { get; init; }
|
||||
|
||||
public Agent(string name)
|
||||
public virtual void __Ready()
|
||||
{
|
||||
Name = name;
|
||||
PoloniumRegistry.Instance.Agents[name] = this;
|
||||
Knowledge = GetNode<Knowledge>("Knowledge");
|
||||
ActionSet = GetNode<ActionSet>("ActionSet");
|
||||
ActionSet.ActionSubmitted += ExecuteAction;
|
||||
}
|
||||
|
||||
public int Level { get; private set; }
|
||||
public abstract int MaxLevel { get; }
|
||||
|
||||
public virtual void LevelUp()
|
||||
public sealed override void _Ready()
|
||||
{
|
||||
if(Level < MaxLevel)
|
||||
Level++;
|
||||
Knowledge = GetNode<Knowledge>("Knowledge");
|
||||
ActionSet = GetNode<ActionSet>("ActionSet");
|
||||
__Ready();
|
||||
base._Ready();
|
||||
}
|
||||
|
||||
public void UpdateKnowledge(KnowledgePatch update) => update.Patch(Knowledge);
|
||||
|
||||
public virtual void ExecuteAction(ActionBody actionBody)
|
||||
{
|
||||
SignalHeader header = PoloniumRegistry.Instance.PoloniumFactory.CreateSignalHeader();
|
||||
header.SetSender(this);
|
||||
EmitSignalActionExecuted(header, actionBody);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public abstract class Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction> : Agent
|
||||
where TAgent : Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentDecisionMaker : AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentKnowledge : AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentAction : AgentAction<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
{
|
||||
|
||||
public Dictionary<string, TAgentAction> Actions { get; } = new();
|
||||
|
||||
public TAgentDecisionMaker DecisionMaker { get; init; }
|
||||
public TAgentKnowledge Knowledge { get; init; }
|
||||
|
||||
|
||||
public Agent() : base($"Computer {ComputerCount++}")
|
||||
{
|
||||
DecisionMaker = new TAgentDecisionMaker();
|
||||
Knowledge = new TAgentKnowledge();
|
||||
}
|
||||
|
||||
public Agent(string name, TAgentDecisionMaker decisionMaker, TAgentKnowledge knowledge) : base(name)
|
||||
{
|
||||
DecisionMaker = decisionMaker;
|
||||
Knowledge = knowledge;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
using Polonium.Interfaces;
|
||||
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract class AgentAction
|
||||
{
|
||||
}
|
||||
|
||||
public abstract class AgentAction<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction> : AgentAction, ITimeConsumer
|
||||
where TAgent : Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentDecisionMaker : AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentKnowledge : AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentAction : AgentAction<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
{
|
||||
|
||||
public int Level { get; private set; }
|
||||
|
||||
public abstract int MaxLevel { get; }
|
||||
|
||||
public virtual void LevelUp()
|
||||
{
|
||||
if(Level < MaxLevel)
|
||||
Level++;
|
||||
}
|
||||
|
||||
public AgentAction(double coolDown)
|
||||
{
|
||||
CoolDown = coolDown;
|
||||
PoloniumRegistry.Instance.TimeConsumers.Add(this);
|
||||
}
|
||||
|
||||
public abstract class ActionParameter
|
||||
{
|
||||
}
|
||||
|
||||
public virtual bool Execute(ActionParameter parameter)
|
||||
{
|
||||
if (Disabled || CoolDown > 0)
|
||||
return false;
|
||||
CoolDownTime = CoolDown;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private double CoolDownTime { get; set; }
|
||||
public bool Disabled { get; set; }
|
||||
|
||||
private double CoolDown { get; }
|
||||
|
||||
public void Process(double delta)
|
||||
{
|
||||
|
||||
if (Disabled || CoolDownTime <= 0)
|
||||
return;
|
||||
CoolDownTime -= delta;
|
||||
if (CoolDownTime <= 0)
|
||||
CoolDownTime = 0;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract class AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>(DecisionMakerType type)
|
||||
where TAgent : Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentDecisionMaker : AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentKnowledge : AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentAction : AgentAction<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
{
|
||||
|
||||
|
||||
|
||||
public DecisionMakerType Type { get; init; } = type;
|
||||
|
||||
public AgentDecisionMaker() : this(DecisionMakerType.Computer)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract class AgentKnowledge
|
||||
{
|
||||
}
|
||||
|
||||
public abstract class AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction> : AgentKnowledge
|
||||
where TAgent : Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentDecisionMaker : AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentKnowledge : AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentAction : AgentAction<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
{
|
||||
public AgentKnowledge()
|
||||
{
|
||||
}
|
||||
|
||||
public abstract void Update(TAgentKnowledge newKnowledge);
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public enum DecisionMakerType
|
||||
{
|
||||
None = 0,
|
||||
Player = 1,
|
||||
Computer = 2,
|
||||
Global = 3,
|
||||
}
|
||||
8
src/Agents/Knowledge.cs
Normal file
8
src/Agents/Knowledge.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Godot;
|
||||
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract partial class Knowledge : Node
|
||||
{
|
||||
|
||||
}
|
||||
45
src/Agents/World.cs
Normal file
45
src/Agents/World.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using Godot;
|
||||
using Polonium.Resources;
|
||||
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract partial class World : Node
|
||||
{
|
||||
private WorldModel Model { get; set; }
|
||||
public HashSet<Agent> Agents { get; } = new();
|
||||
public Knowledge CommonKnowledge { get; set; }
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Model = GetNode<WorldModel>("WorldModel");
|
||||
CommonKnowledge = GetNode<Knowledge>("CommonKnowledge");
|
||||
Model.CommonKnowledgeUpdated += CommonKnowledgeUpdated;
|
||||
Model.PrivateKnowledgeUpdated += ForwardPrivateKnowledgeUpdated;
|
||||
base._Ready();
|
||||
}
|
||||
|
||||
public void RegisterAgent(Agent agent)
|
||||
{
|
||||
Agents.Add(agent);
|
||||
agent.ActionExecuted += Model.ActionExecuted;
|
||||
}
|
||||
|
||||
public virtual void Enter()
|
||||
{
|
||||
PoloniumRegistry.Instance.CurrentWorld = this;
|
||||
}
|
||||
|
||||
public virtual void Exit()
|
||||
{
|
||||
PoloniumRegistry.Instance.CurrentWorld = null;
|
||||
}
|
||||
|
||||
public void CommonKnowledgeUpdated(KnowledgePatch update) => update.Patch(CommonKnowledge);
|
||||
|
||||
public void ForwardPrivateKnowledgeUpdated(SignalHeader header, KnowledgePatch update)
|
||||
{
|
||||
foreach (Agent receiver in header.ResolveReceivers())
|
||||
receiver.UpdateKnowledge(update);
|
||||
}
|
||||
|
||||
}
|
||||
22
src/Agents/WorldModel.cs
Normal file
22
src/Agents/WorldModel.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Godot;
|
||||
using Polonium.Resources;
|
||||
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract partial class WorldModel : Node
|
||||
{
|
||||
|
||||
[Signal]
|
||||
public delegate void CommonKnowledgeUpdatedEventHandler(KnowledgePatch update);
|
||||
[Signal]
|
||||
public delegate void PrivateKnowledgeUpdatedEventHandler(SignalHeader header, KnowledgePatch update);
|
||||
|
||||
private Knowledge WorldKnowledge { get; set; }
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
WorldKnowledge = GetNode<Knowledge>("WorldKnowledge");
|
||||
base._Ready();
|
||||
}
|
||||
public abstract void ActionExecuted(SignalHeader header, ActionBody actionBody);
|
||||
}
|
||||
8
src/Interfaces/IPoloniumFactory.cs
Normal file
8
src/Interfaces/IPoloniumFactory.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Polonium.Resources;
|
||||
|
||||
namespace Polonium.Interfaces;
|
||||
|
||||
public interface IPoloniumFactory
|
||||
{
|
||||
public SignalHeader CreateSignalHeader();
|
||||
}
|
||||
@@ -14,6 +14,12 @@ public class PoloniumRegistry
|
||||
public Config Config { get; set; }
|
||||
[RegistryPassThrough]
|
||||
public Save Save { get; set; }
|
||||
[RegistryPassThrough]
|
||||
public World CurrentWorld { get; set; }
|
||||
|
||||
[RegistryPassThrough]
|
||||
public IPoloniumFactory PoloniumFactory { get; set; }
|
||||
|
||||
|
||||
public static void Prepare()
|
||||
{
|
||||
|
||||
9
src/Resources/ActionBody.cs
Normal file
9
src/Resources/ActionBody.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Godot;
|
||||
|
||||
namespace Polonium.Resources;
|
||||
|
||||
public abstract partial class ActionBody : Resource
|
||||
{
|
||||
public StringName ActionName { get; set; }
|
||||
|
||||
}
|
||||
11
src/Resources/KnowledgePatch.cs
Normal file
11
src/Resources/KnowledgePatch.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Godot;
|
||||
using Polonium.Agents;
|
||||
|
||||
namespace Polonium.Resources;
|
||||
|
||||
public abstract partial class KnowledgePatch : Resource
|
||||
{
|
||||
public SignalHeader Header { get; set; }
|
||||
public abstract void Patch(Knowledge knowledge);
|
||||
|
||||
}
|
||||
15
src/Resources/SignalHeader.cs
Normal file
15
src/Resources/SignalHeader.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Godot;
|
||||
using Polonium.Agents;
|
||||
|
||||
namespace Polonium.Resources;
|
||||
|
||||
public abstract partial class SignalHeader : Resource
|
||||
{
|
||||
|
||||
public abstract Agent[] ResolveReceivers();
|
||||
public abstract Agent ResolveSender();
|
||||
|
||||
public abstract void SetSender(Agent agent);
|
||||
public abstract void AddReceiver(Agent agent);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user