Skip to content

Commit

Permalink
Fixes multi-sensor missing keys warning
Browse files Browse the repository at this point in the history
  • Loading branch information
crashkonijn committed Nov 7, 2024
1 parent 08fd2d9 commit f7cf91c
Show file tree
Hide file tree
Showing 17 changed files with 179 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
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.Multi;
using CrashKonijn.Goap.Demos.Complex.Sensors.World;
using CrashKonijn.Goap.Demos.Complex.Targets;
using CrashKonijn.Goap.Demos.Complex.WorldKeys;
using CrashKonijn.Goap.Runtime;
using TransformTarget = CrashKonijn.Goap.Demos.Complex.Targets.TransformTarget;

namespace CrashKonijn.Goap.Demos.Complex.Factories.Capabilities
{
Expand All @@ -20,38 +22,39 @@ public override ICapabilityConfig Create()
// Goals
builder.AddGoal<FixHungerGoal>()
.AddCondition<Hunger>(Comparison.SmallerThanOrEqual, 0);

// Actions
builder.AddAction<EatAction>()
.SetTarget<Targets.TransformTarget>()
.SetTarget<TransformTarget>()
.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>>()
.AddEffect<IsHolding<IEatable>>(EffectType.Increase)
.SetBaseCost(3)
.SetProperties(new GatherItemAction<Apple>.Props
{
pickupItem = true,
timer = 3
timer = 3,
});

builder.AddPickupItemAction<IEatable>();

// Target sensors
builder.AddClosestItemTargetSensor<IEatable>();
builder.AddClosestSourceTargetSensor<Apple>();

// World sensors
builder.AddIsHoldingSensor<IEatable>();
builder.AddIsInWorldSensor<IEatable>();
builder.AddWorldSensor<HungerSensor>()
.SetKey<Hunger>();

// Multi sensor
builder.AddMultiSensor<ItemSensor<IEatable>>();

return builder.Build();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ namespace CrashKonijn.Goap.Demos.Complex.Factories.Extensions
{
public static class TargetSensorExtensions
{
public static void AddClosestItemTargetSensor<T>(this CapabilityBuilder builder)
where T : class, IHoldable
{
builder.AddTargetSensor<ClosestItemSensor<T>>()
.SetTarget<ClosestTarget<T>>();
}

public static void AddClosestObjectTargetSensor<T>(this CapabilityBuilder builder)
where T : MonoBehaviour
{
Expand All @@ -29,4 +22,4 @@ public static void AddClosestSourceTargetSensor<T>(this CapabilityBuilder builde
.SetTarget<ClosestSourceTarget<T>>();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,5 @@ public static void AddIsHoldingSensor<THoldable>(this CapabilityBuilder builder)
builder.AddWorldSensor<IsHoldingSensor<THoldable>>()
.SetKey<IsHolding<THoldable>>();
}

public static void AddIsInWorldSensor<THoldable>(this CapabilityBuilder builder)
where THoldable : IHoldable
{
builder.AddWorldSensor<IsInWorldSensor<THoldable>>()
.SetKey<IsInWorld<THoldable>>();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CrashKonijn.Goap.Demos.Complex.Classes.Items;
using CrashKonijn.Goap.Demos.Complex.Factories.Capabilities;
using CrashKonijn.Goap.Demos.Complex.Factories.Extensions;
using CrashKonijn.Goap.Demos.Complex.Sensors.Multi;
using CrashKonijn.Goap.Runtime;

namespace CrashKonijn.Goap.Demos.Complex.Factories
Expand All @@ -12,34 +13,32 @@ public class MinerAgentTypeConfigFactory : AgentTypeFactoryBase
public override IAgentTypeConfig Create()
{
var builder = new AgentTypeBuilder(SetIds.Miner);

builder.AddCapability<BaseCapability>();
builder.AddCapability<WanderCapability>();
builder.AddCapability<HungerCapability>();

builder.CreateCapability("MineCapability", (capability) =>
{
capability.AddPickupItemGoal<Pickaxe>();
capability.AddGatherItemGoal<Iron>();

capability.AddPickupItemAction<Pickaxe>();

capability.AddGatherItemAction<Iron, Pickaxe>();
capability.AddGatherItemSlowAction<Iron>();

capability.AddClosestItemTargetSensor<Iron>();
capability.AddClosestItemTargetSensor<Pickaxe>();


capability.AddClosestSourceTargetSensor<Iron>();

capability.AddIsHoldingSensor<Pickaxe>();
capability.AddIsHoldingSensor<Iron>();

capability.AddIsInWorldSensor<Pickaxe>();
capability.AddIsInWorldSensor<Iron>();

// Multi sensor
capability.AddMultiSensor<ItemSensor<Pickaxe>>();
capability.AddMultiSensor<ItemSensor<Iron>>();
});

return builder.Build();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CrashKonijn.Goap.Demos.Complex.Classes.Sources;
using CrashKonijn.Goap.Demos.Complex.Factories.Capabilities;
using CrashKonijn.Goap.Demos.Complex.Factories.Extensions;
using CrashKonijn.Goap.Demos.Complex.Sensors.Multi;
using CrashKonijn.Goap.Runtime;

namespace CrashKonijn.Goap.Demos.Complex.Factories
Expand All @@ -13,7 +14,7 @@ public class SmithAgentTypeConfigFactory : AgentTypeFactoryBase
public override IAgentTypeConfig Create()
{
var builder = new AgentTypeBuilder(SetIds.Smith);

builder.AddCapability<BaseCapability>();
builder.AddCapability<WanderCapability>();
builder.AddCapability<HungerCapability>();
Expand All @@ -34,23 +35,21 @@ public override IAgentTypeConfig Create()
// TargetSensors
capability.AddClosestObjectTargetSensor<AnvilSource>();

capability.AddClosestItemTargetSensor<Iron>();
capability.AddClosestItemTargetSensor<Wood>();

capability.AddClosestSourceTargetSensor<Iron>();
capability.AddClosestSourceTargetSensor<Wood>();

// WorldSensors
capability.AddIsHoldingSensor<Wood>();
capability.AddIsHoldingSensor<Iron>();

capability.AddIsInWorldSensor<Axe>();
capability.AddIsInWorldSensor<Pickaxe>();
capability.AddIsInWorldSensor<Wood>();
capability.AddIsInWorldSensor<Iron>();
// Multi sensor
capability.AddMultiSensor<ItemSensor<Axe>>();
capability.AddMultiSensor<ItemSensor<Pickaxe>>();
capability.AddMultiSensor<ItemSensor<Wood>>();
capability.AddMultiSensor<ItemSensor<Iron>>();
});

return builder.Build();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CrashKonijn.Goap.Demos.Complex.Classes.Items;
using CrashKonijn.Goap.Demos.Complex.Factories.Capabilities;
using CrashKonijn.Goap.Demos.Complex.Factories.Extensions;
using CrashKonijn.Goap.Demos.Complex.Sensors.Multi;
using CrashKonijn.Goap.Runtime;

namespace CrashKonijn.Goap.Demos.Complex.Factories
Expand All @@ -12,38 +13,36 @@ public class WoodCutterAgentTypeConfigFactory : AgentTypeFactoryBase
public override IAgentTypeConfig Create()
{
var builder = new AgentTypeBuilder(SetIds.WoodCutter);

builder.AddCapability<BaseCapability>();
builder.AddCapability<WanderCapability>();
builder.AddCapability<HungerCapability>();

builder.CreateCapability("WoodCutterCapability", (capability) =>
{
// Goals
capability.AddPickupItemGoal<Axe>();
capability.AddGatherItemGoal<Wood>();

// Actions
capability.AddPickupItemAction<Axe>();

capability.AddGatherItemAction<Wood, Axe>();
capability.AddGatherItemSlowAction<Wood>();

// Target sensors
capability.AddClosestItemTargetSensor<Axe>();
capability.AddClosestItemTargetSensor<Wood>();

capability.AddClosestSourceTargetSensor<Wood>();

// World sensors
capability.AddIsHoldingSensor<Axe>();
capability.AddIsHoldingSensor<Wood>();

capability.AddIsInWorldSensor<Axe>();
capability.AddIsInWorldSensor<Wood>();

// Multi sensor
capability.AddMultiSensor<ItemSensor<Axe>>();
capability.AddMultiSensor<ItemSensor<Wood>>();
});

return builder.Build();
}
}
}
}
3 changes: 3 additions & 0 deletions Demo/Assets/CrashKonijn/GOAP/Demos/Complex/Sensors/Multi.meta

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,51 @@
using System.Linq;
using CrashKonijn.Agent.Core;
using CrashKonijn.Goap.Core;
using CrashKonijn.Goap.Demos.Complex.Behaviours;
using CrashKonijn.Goap.Demos.Complex.Interfaces;
using CrashKonijn.Goap.Demos.Complex.Targets;
using CrashKonijn.Goap.Demos.Complex.WorldKeys;
using CrashKonijn.Goap.Runtime;
using UnityEngine;
using TransformTarget = CrashKonijn.Goap.Runtime.TransformTarget;

namespace CrashKonijn.Goap.Demos.Complex.Sensors.Multi
{
public class ItemSensor<T> : MultiSensorBase
where T : class, IHoldable
{
private ItemCollection collection;

public ItemSensor()
{
this.AddLocalTargetSensor<ClosestTarget<T>>(this.SenseClosestTarget);
this.AddLocalWorldSensor<IsInWorld<T>>(this.SenseIsInWorld);
}

public override void Created()
{
this.collection = Object.FindObjectOfType<ItemCollection>();
}

public override void Update() { }

private ITarget SenseClosestTarget(IActionReceiver agent, IComponentReference references, ITarget target)
{
var closest = this.collection.GetFiltered<T>(false, true, agent.Transform.gameObject).Cast<ItemBase>().Closest(agent.Transform.position);

if (closest == null)
return null;

// Re-use the existing target if the target exists
if (target is TransformTarget targetTransform)
return targetTransform.SetTransform(closest.transform);

return new TransformTarget(closest.transform);
}

private SenseValue SenseIsInWorld(IActionReceiver agent, IComponentReference references)
{
return this.collection.GetFiltered<T>(false, true, agent.Transform.gameObject).Length;
}
}
}

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

Loading

0 comments on commit f7cf91c

Please sign in to comment.