add: Agent Actions

This commit is contained in:
h z
2025-02-12 14:09:56 +00:00
parent f7da4ee8bb
commit 2323211a65
8 changed files with 127 additions and 30 deletions

View File

@@ -3,6 +3,7 @@ namespace Polonium.Agents;
public abstract class Agent public abstract class Agent
{ {
protected static int ComputerCount = 0;
public string Name { get; init; } public string Name { get; init; }
@@ -11,15 +12,27 @@ public abstract class Agent
Name = name; Name = name;
PoloniumRegistry.Instance.Agents[name] = this; PoloniumRegistry.Instance.Agents[name] = this;
} }
public int Level { get; private set; }
public abstract int MaxLevel { get; }
public virtual void LevelUp()
{
if(Level < MaxLevel)
Level++;
}
} }
public abstract class Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge> : Agent public abstract class Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction> : Agent
where TAgent : Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge> where TAgent : Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
where TAgentDecisionMaker : AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge>, new() where TAgentDecisionMaker : AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
where TAgentKnowledge : AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge>, new() where TAgentKnowledge : AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
where TAgentAction : AgentAction<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
{ {
private static int ComputerCount = 0; public Dictionary<string, TAgentAction> Actions { get; } = new();
public TAgentDecisionMaker DecisionMaker { get; init; } public TAgentDecisionMaker DecisionMaker { get; init; }
public TAgentKnowledge Knowledge { get; init; } public TAgentKnowledge Knowledge { get; init; }

60
src/Agents/AgentAction.cs Normal file
View File

@@ -0,0 +1,60 @@
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;
}
}

View File

@@ -1,28 +1,17 @@
namespace Polonium.Agents; namespace Polonium.Agents;
public abstract class AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge> public abstract class AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>(DecisionMakerType type)
where TAgent : Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge> where TAgent : Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
where TAgentDecisionMaker : AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge>, new() where TAgentDecisionMaker : AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
where TAgentKnowledge : AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge>, new() where TAgentKnowledge : AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
where TAgentAction : AgentAction<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
{ {
public enum DecisionMakerType
{
None = 0,
Player = 1,
Computer = 2,
Global = 3,
}
public DecisionMakerType Type { get; init; }
public AgentDecisionMaker() public DecisionMakerType Type { get; init; } = type;
{
Type = DecisionMakerType.Computer;
}
public AgentDecisionMaker(DecisionMakerType type) public AgentDecisionMaker() : this(DecisionMakerType.Computer)
{ {
Type = type;
} }
} }

View File

@@ -1,9 +1,14 @@
namespace Polonium.Agents; namespace Polonium.Agents;
public abstract class AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge> public abstract class AgentKnowledge
where TAgent : Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge> {
where TAgentDecisionMaker : AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge>, new() }
where TAgentKnowledge : AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge>, new()
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 AgentKnowledge()
{ {

View File

@@ -0,0 +1,9 @@
namespace Polonium.Agents;
public enum DecisionMakerType
{
None = 0,
Player = 1,
Computer = 2,
Global = 3,
}

View File

@@ -0,0 +1,6 @@
namespace Polonium.Interfaces;
public interface ITimeConsumer
{
public void Process(double time);
}

View File

@@ -1,5 +1,6 @@
using Godot; using Godot;
using Polonium.Agents; using Polonium.Agents;
using Polonium.Interfaces;
using Polonium.Resources; using Polonium.Resources;
using Polonium.Scenes; using Polonium.Scenes;
@@ -18,4 +19,8 @@ public class PoloniumRegistry
DirAccess.MakeDirAbsolute("user://saves"); DirAccess.MakeDirAbsolute("user://saves");
} }
public Dictionary<string, Agent> Agents { get; set; } = new(); public Dictionary<string, Agent> Agents { get; set; } = new();
public HashSet<ITimeConsumer> TimeConsumers { get; set; } = new();
public bool Paused { get; set; }
} }

View File

@@ -1,5 +1,6 @@
using Godot; using Godot;
using Polonium.Attributes; using Polonium.Attributes;
using Polonium.Interfaces;
namespace Polonium.Scenes; namespace Polonium.Scenes;
[ProxyNode] [ProxyNode]
@@ -31,4 +32,13 @@ public partial class RootScene : Node2D
Enter(); Enter();
base._EnterTree(); base._EnterTree();
} }
public override void _Process(double delta)
{
if(!PoloniumRegistry.Instance.Paused)
foreach (ITimeConsumer tc in PoloniumRegistry.Instance.TimeConsumers)
tc.Process(delta);
base._Process(delta);
}
} }