Skip to content

Commit 99772be

Browse files
authored
Merge pull request #370 from crashkonijn/feature/value-condition
Feature: Dynamic conditions (code only)
2 parents 6d4e13b + 31ad0b3 commit 99772be

File tree

25 files changed

+263
-27
lines changed

25 files changed

+263
-27
lines changed

Demo/Assets/CrashKonijn/GOAP/Demos/Complex/Factories/Capabilities/HungerCapability.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override ICapabilityConfig Create()
2828
.SetTarget<TransformTarget>()
2929
.AddEffect<Hunger>(EffectType.Decrease)
3030
.AddCondition<IsHolding<IEatable>>(Comparison.GreaterThanOrEqual, 1)
31-
.AddCondition<Hunger>(Comparison.GreaterThanOrEqual, 30)
31+
.AddCondition<Hunger, LowHunger>(Comparison.GreaterThanOrEqual)
3232
.SetValidateConditions(false); // We don't need to validate conditions for this action, or it will stop when becoming below 80 hunger
3333

3434
builder.AddAction<GatherItemAction<Apple>>()
@@ -50,6 +50,8 @@ public override ICapabilityConfig Create()
5050
builder.AddIsHoldingSensor<IEatable>();
5151
builder.AddWorldSensor<HungerSensor>()
5252
.SetKey<Hunger>();
53+
builder.AddWorldSensor<LowHungerSensor>()
54+
.SetKey<LowHunger>();
5355

5456
// Multi sensor
5557
builder.AddMultiSensor<ItemSensor<IEatable>>();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using CrashKonijn.Agent.Core;
2+
using CrashKonijn.Goap.Core;
3+
using CrashKonijn.Goap.Runtime;
4+
5+
namespace CrashKonijn.Goap.Demos.Complex.Sensors.World
6+
{
7+
public class LowHungerSensor : LocalWorldSensorBase
8+
{
9+
public override ISensorTimer Timer { get; } = SensorTimer.Once;
10+
11+
public override void Created()
12+
{
13+
}
14+
15+
public override void Update()
16+
{
17+
}
18+
19+
public override SenseValue Sense(IActionReceiver agent, IComponentReference references)
20+
{
21+
return 30;
22+
}
23+
}
24+
}

Demo/Assets/CrashKonijn/GOAP/Demos/Complex/Sensors/World/LowHungerSensor.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using CrashKonijn.Goap.Runtime;
2+
3+
namespace CrashKonijn.Goap.Demos.Complex.WorldKeys
4+
{
5+
public class LowHunger : WorldKeyBase
6+
{
7+
}
8+
}

Demo/Assets/CrashKonijn/GOAP/Demos/Complex/WorldKeys/LowHunger.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package/Documentation/Config/Code.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ namespace CrashKonijn.Docs.GettingStarted.Capabilities
7676
```
7777
{% endcode %}
7878

79+
### Dynamic Conditions
80+
Since v3.1 you can now add dynamic conditions!
81+
82+
{% code lineNumbers="true" %}
83+
```csharp
84+
builder.AddAction<EatAction>()
85+
.AddCondition<Hunger, LowHunger>(Comparison.GreaterThanOrEqual);
86+
```
87+
{% endcode %}
88+
7989
### Callbacks
8090
In v3 you can add a callback to your builder methods, giving you access to the instance of each class. This allows you to set extra data.
8191

Package/Documentation/upgrade/upgrade-guide-3.0-3.1.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public interface IGoapInjector
3030

3131
## New Features
3232

33+
### You can now make dynamic conditions (code only)!
34+
35+
```csharp
36+
builder.AddAction<EatAction>()
37+
.AddCondition<Hunger, LowHunger>(Comparison.GreaterThanOrEqual);
38+
```
39+
3340
### AgenTypeFactory and CapabilityFactory can now be injected
3441
You can now inject into `AgentTypeFactoryBase` and `CapabilityFactoryBase` classes, similar to other actions, goals and sensors.
3542

Package/Editor/CrashKonijn.Goap.Editor/Extensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,13 @@ public static float GetCost(this INode node, IGoapActionProvider provider)
3333

3434
return 0;
3535
}
36+
37+
public static int GetAmount(this ICondition condition)
38+
{
39+
if (condition is IValueCondition serializableCondition)
40+
return serializableCondition.Amount;
41+
42+
return 0;
43+
}
3644
}
3745
}

Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/ConditionElement.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,28 @@ private string GetText(ICondition condition)
6262
{
6363
var suffix = this.GetSuffix(condition);
6464

65-
return $"{condition.WorldKey.Name} {this.GetText(condition.Comparison)} {condition.Amount} {suffix}";
65+
return $"{condition.WorldKey.Name} {this.GetText(condition.Comparison)} {GetValueText(condition)} {suffix}";
66+
}
67+
68+
private string GetValueText(ICondition condition)
69+
{
70+
if (condition is IValueCondition valueCondition)
71+
return valueCondition.Amount.ToString();
72+
73+
if (condition is IReferenceCondition referenceCondition)
74+
{
75+
if (!Application.isPlaying)
76+
return referenceCondition.ValueKey.Name;
77+
78+
if (this.values.SelectedObject is not IMonoGoapActionProvider agent)
79+
return referenceCondition.ValueKey.Name;
80+
81+
var (exists, value) = agent.WorldData.GetWorldValue(referenceCondition.ValueKey);
82+
83+
return $"{value} ({referenceCondition.ValueKey.Name})";
84+
}
85+
86+
return "";
6687
}
6788

6889
private string GetSuffix(ICondition condition)

Package/Editor/CrashKonijn.Goap.Editor/TypeDrawers/GoapSetConfigEditor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private void Upgrade(CapabilityConfigScriptable capabilityScriptable)
125125
Name = x.WorldKey.Name
126126
},
127127
comparison = x.Comparison,
128-
amount = x.Amount
128+
amount = x.GetAmount()
129129
}).ToList()
130130
});
131131
}
@@ -152,7 +152,7 @@ private void Upgrade(CapabilityConfigScriptable capabilityScriptable)
152152
Name = x.WorldKey.Name
153153
},
154154
comparison = x.Comparison,
155-
amount = x.Amount
155+
amount = x.GetAmount()
156156
}).ToList(),
157157
effects = actionConfig.Effects.Select(x => new CapabilityEffect
158158
{

0 commit comments

Comments
 (0)