Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: improved goal handling #189

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using CrashKonijn.Goap.Demos.Complex.Goap;
using CrashKonijn.Goap.Demos.Complex.Interfaces;
using CrashKonijn.Goap.Runtime;
using UnityEngine;

namespace CrashKonijn.Goap.Demos.Complex.Actions
{
Expand All @@ -16,7 +17,7 @@ public void Inject(GoapInjector injector)
{
this.instanceHandler = injector.instanceHandler;
}

public override void Created()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ private void Awake()
private void Start()
{
this.SpawnAgent(SetIds.Cleaner, ComplexAgentBrain.AgentType.Cleaner, this.cleanerColor);
this.SpawnAgent(SetIds.Cleaner, ComplexAgentBrain.AgentType.Cleaner, this.cleanerColor);
this.SpawnAgent(SetIds.Cleaner, ComplexAgentBrain.AgentType.Cleaner, this.cleanerColor);

this.SpawnAgent(SetIds.Smith, ComplexAgentBrain.AgentType.Smith, this.smithColor);
this.SpawnAgent(SetIds.Miner, ComplexAgentBrain.AgentType.Miner, this.minerColor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,53 @@ namespace CrashKonijn.Goap.Demos.Complex.Behaviours
public class ComplexAgentBrain : MonoBehaviour
{
public AgentType agentType;
private GoapActionProvider agent;
private GoapActionProvider provider;
private ComplexHungerBehaviour complexHunger;
private ItemCollection itemCollection;
private ComplexInventoryBehaviour inventory;

private void Awake()
{
this.agent = this.GetComponent<GoapActionProvider>();
this.provider = this.GetComponent<GoapActionProvider>();
this.complexHunger = this.GetComponent<ComplexHungerBehaviour>();
this.inventory = this.GetComponent<ComplexInventoryBehaviour>();
this.itemCollection = FindObjectOfType<ItemCollection>();
}

private void OnEnable()
{
this.agent.Events.OnActionEnd += this.OnActionEnd;
this.agent.Events.OnNoActionFound += this.OnNoActionFound;
this.agent.Events.OnGoalCompleted += this.OnGoalCompleted;
this.provider.Events.OnActionEnd += this.OnActionEnd;
this.provider.Events.OnNoActionFound += this.OnNoActionFound;
this.provider.Events.OnGoalCompleted += this.OnGoalCompleted;
}

private void OnDisable()
{
this.agent.Events.OnActionEnd -= this.OnActionEnd;
this.agent.Events.OnNoActionFound -= this.OnNoActionFound;
this.agent.Events.OnGoalCompleted -= this.OnGoalCompleted;
this.provider.Events.OnActionEnd -= this.OnActionEnd;
this.provider.Events.OnNoActionFound -= this.OnNoActionFound;
this.provider.Events.OnGoalCompleted -= this.OnGoalCompleted;
}

private void Start()
{
this.agent.SetGoal<WanderGoal>(false);
this.provider.RequestGoal<WanderGoal>(false);
}

private void OnNoActionFound(IGoal goal)
private void OnNoActionFound(IGoalRequest request)
{
this.agent.SetGoal<WanderGoal>(false);
this.provider.RequestGoal<WanderGoal>(false);
}

private void OnGoalCompleted(IGoal goal)
{
this.agent.SetGoal<WanderGoal>(false);
this.provider.RequestGoal<WanderGoal>(false);
}

private void OnActionEnd(IAction action)
{
this.UpdateHunger();

if (this.agent.CurrentGoal is FixHungerGoal)
if (this.provider.CurrentPlan.Goal is FixHungerGoal)
return;

this.DetermineGoal();
Expand All @@ -66,11 +66,11 @@ private void UpdateHunger()
{
if (this.complexHunger.hunger > 80)
{
this.agent.SetGoal<FixHungerGoal>(false);
this.provider.RequestGoal<FixHungerGoal>(true);
return;
}

if (this.agent.CurrentGoal is not FixHungerGoal)
if (this.provider.CurrentPlan.Goal is not FixHungerGoal)
return;

if (this.complexHunger.hunger < 20)
Expand Down Expand Up @@ -100,62 +100,56 @@ private void DetermineMinerGoals()
{
if (this.inventory.Count<Pickaxe>() == 0 && this.itemCollection.Get<Pickaxe>().Length >= 1)
{
this.agent.SetGoal<PickupItemGoal<Pickaxe>>(false);
this.provider.RequestGoal<PickupItemGoal<Pickaxe>>(false);
return;
}

if (this.itemCollection.Get<Iron>().Length <= 2)
{
this.agent.SetGoal<GatherItemGoal<Iron>>(false);
this.provider.RequestGoal<GatherItemGoal<Iron>>(false);
return;
}

this.agent.SetGoal<WanderGoal>(false);
this.provider.RequestGoal<WanderGoal>(false);
}

private void DetermineWoodCutterGoals()
{
if (this.inventory.Count<Axe>() == 0 && this.itemCollection.Get<Axe>().Length >= 1)
{
this.agent.SetGoal<PickupItemGoal<Axe>>(false);
this.provider.RequestGoal<PickupItemGoal<Axe>>(false);
return;
}

if (this.itemCollection.Get<Wood>().Length <= 2)
{
this.agent.SetGoal<GatherItemGoal<Wood>>(false);
this.provider.RequestGoal<GatherItemGoal<Wood>>(false);
return;
}

this.agent.SetGoal<WanderGoal>(false);
this.provider.RequestGoal<WanderGoal>(false);
}

private void DetermineSmithGoals()
{
if (this.itemCollection.Get<Axe>().Length <= 1)
{
this.agent.SetGoal<CreateItemGoal<Axe>>(false);
this.provider.RequestGoal<CreateItemGoal<Axe>>(false);
return;
}

if (this.itemCollection.Get<Pickaxe>().Length <= 1)
{
this.agent.SetGoal<CreateItemGoal<Pickaxe>>(false);
this.provider.RequestGoal<CreateItemGoal<Pickaxe>>(false);
return;
}

this.agent.SetGoal<WanderGoal>(false);
this.provider.RequestGoal<WanderGoal>(false);
}

private void DetermineCleanerGoals()
{
if (this.itemCollection.Count(false, false) > 0)
{
this.agent.SetGoal<CleanItemsGoal>(false);
return;
}

this.agent.SetGoal<WanderGoal>(false);
this.provider.RequestGoal<CleanItemsGoal, FixHungerGoal, WanderGoal>(true);
}

public enum AgentType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ private void Update()

private string GetText()
{
if (this.actionProvider.CurrentGoal is null)
if (this.actionProvider.CurrentPlan is null)
return $"{this.GetTypeText()}\nIdle";

if (this.actionProvider.Agent.ActionState.Action is null)
if (this.actionProvider.Receiver.ActionState.Action is null)
return $"{this.GetTypeText()}\nIdle";

return $"{this.GetTypeText()}\n{this.actionProvider.CurrentGoal.GetType().GetGenericTypeName()}\n{this.actionProvider.Agent.ActionState.Action.GetType().GetGenericTypeName()}\n{this.agent.State}\nhunger: {this.simpleComplexHunger.hunger:0.00}";
return $"{this.GetTypeText()}\n{this.actionProvider.CurrentPlan.GetType().GetGenericTypeName()}\n{this.actionProvider.Receiver.ActionState.Action.GetType().GetGenericTypeName()}\n{this.agent.State}\nhunger: {this.simpleComplexHunger.hunger:0.00}";
}

private string GetTypeText()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CrashKonijn.Goap.Demos.Complex.Factories.Extensions;
using CrashKonijn.Goap.Demos.Complex.Goals;
using CrashKonijn.Goap.Demos.Complex.Interfaces;
using CrashKonijn.Goap.Demos.Complex.Sensors.World;
using CrashKonijn.Goap.Demos.Complex.Targets;
using CrashKonijn.Goap.Demos.Complex.WorldKeys;
using CrashKonijn.Goap.Runtime;
Expand All @@ -18,13 +19,15 @@ public override ICapabilityConfig Create()

// Goals
builder.AddGoal<FixHungerGoal>()
.AddCondition<IsHungry>(Comparison.SmallerThanOrEqual, 0);
.AddCondition<Hunger>(Comparison.SmallerThanOrEqual, 0);

// Actions
builder.AddAction<EatAction>()
.SetTarget<Targets.TransformTarget>()
.AddEffect<IsHungry>(EffectType.Decrease)
.AddCondition<IsHolding<IEatable>>(Comparison.GreaterThanOrEqual, 1);
.AddEffect<Hunger>(EffectType.Decrease)
.AddCondition<IsHolding<IEatable>>(Comparison.GreaterThanOrEqual, 1)
.AddCondition<Hunger>(Comparison.GreaterThanOrEqual, 30)
.SetValidateConditions(false); // We don't need to validate conditions for this action, or it will stop when becoming below 80 hunger

builder.AddAction<GatherItemAction<Apple>>()
.SetTarget<ClosestSourceTarget<Apple>>()
Expand All @@ -45,6 +48,8 @@ public override ICapabilityConfig Create()
// World sensors
builder.AddIsHoldingSensor<IEatable>();
builder.AddIsInWorldSensor<IEatable>();
builder.AddWorldSensor<HungerSensor>()
.SetKey<Hunger>();

return builder.Build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CrashKonijn.Goap.Core;
using CrashKonijn.Goap.Demos.Complex.Actions;
using CrashKonijn.Goap.Demos.Complex.Factories.Extensions;
using CrashKonijn.Goap.Demos.Complex.Goals;
using CrashKonijn.Goap.Demos.Complex.Sensors.Target;
using CrashKonijn.Goap.Demos.Complex.Targets;
Expand All @@ -16,6 +15,7 @@ public override ICapabilityConfig Create()
var builder = new CapabilityBuilder("WanderCapability");

builder.AddGoal<WanderGoal>()
.SetBaseCost(50)
.AddCondition<IsWandering>(Comparison.GreaterThanOrEqual, 1);

builder.AddAction<WanderAction>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public override IAgentTypeConfig Create()
builder.CreateCapability("CleanCapability", (capability) =>
{
capability.AddGoal<CleanItemsGoal>()
.SetBaseCost(20)
.AddCondition<ItemsOnFloor>(Comparison.SmallerThanOrEqual, 0);

capability.AddAction<HaulItemAction>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
using CrashKonijn.Goap.Runtime;
using CrashKonijn.Agent.Core;
using CrashKonijn.Goap.Demos.Complex.Behaviours;
using CrashKonijn.Goap.Runtime;
using UnityEngine;

namespace CrashKonijn.Goap.Demos.Complex.Goals
{
public class FixHungerGoal : GoalBase
{
public override float GetCost(IActionReceiver agent, IComponentReference references)
{
var hunger = references.GetCachedComponent<ComplexHungerBehaviour>().hunger;
var limited = Mathf.Clamp(hunger, 0f, 100f); // Clamp between 0 and 100
var normalized = limited / 100f; // Normalize between 0 and 1
var curved = this.ExponentialCurve(normalized); // Apply exponential curve
var inverse = 1f - curved; // Inverse

return 50 * inverse;
}

private float ExponentialCurve(float t)
{
float sqr = t * t;
return sqr / (2.0f * (sqr - t) + 1.0f);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 1cef53d855ccf0b46bcf004b27c4a52a, type: 3}
m_Name:
m_EditorClassIdentifier:
<ActionProvider>k__BackingField: {fileID: -5926512986792016431}
<ActionProviderBase>k__BackingField: {fileID: -5926512986792016431}
<LoggerConfig>k__BackingField:
<DebugMode>k__BackingField: 1
<MaxLogSize>k__BackingField: 20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,10 +705,10 @@ MonoBehaviour:
m_EditorClassIdentifier:
configInitializer: {fileID: 395702497}
agentTypeConfigFactories:
- {fileID: 1526725135}
- {fileID: 267963326}
- {fileID: 544038563}
- {fileID: 1526725135}
- {fileID: 1534347707}
- {fileID: 267963326}
--- !u!4 &395702494
Transform:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CrashKonijn.Agent.Core;
using CrashKonijn.Goap.Core;
using CrashKonijn.Goap.Demos.Complex.Behaviours;
using CrashKonijn.Goap.Runtime;

namespace CrashKonijn.Goap.Demos.Complex.Sensors.World
{
public class HungerSensor : LocalWorldSensorBase
{
public override void Created()
{

}

public override void Update()
{

}

public override SenseValue Sense(IActionReceiver agent, IComponentReference references)
{
return (int) references.GetCachedComponent<ComplexHungerBehaviour>().hunger;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using CrashKonijn.Goap.Runtime;

namespace CrashKonijn.Goap.Demos.Complex
{
[GoapId("Hunger-7de38d7e-b184-453a-9220-e8619c55fc1e")]
public class Hunger : WorldKeyBase {}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ private void Awake()

private void Start()
{
this.agent.SetGoal<WanderGoal>(false);
this.agent.RequestGoal<WanderGoal>(false);
}

private void Update()
{
if (this.hunger.hunger > 80)
{
this.agent.SetGoal<FixHungerGoal>(false);
this.agent.RequestGoal<FixHungerGoal>(false);
return;
}

if (this.hunger.hunger < 20)
this.agent.SetGoal<WanderGoal>(true);
this.agent.RequestGoal<WanderGoal>(true);
}
}
}
Loading
Loading