Skip to content

Commit 818dc5e

Browse files
committed
Changed any Goap* to ReGoap* to keep naming style.
Changed every ReGoap class to use generics so we are not bound anymore to <string, object> states, but anything can be used. Using Vector3? instead of Vector3 in the Unity example. Related to #13 This commit breaks every implementation.
1 parent baf64e9 commit 818dc5e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+544
-490
lines changed

README.md

+29-27
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ This library is very generic, if you don't include the Unity folder you can use
77
2. [Get Started, long version](#get-started-long-version)
88
1. [Explaining GOAP](#explaining-goap)
99
2. [How to use ReGoap in Unity3D](#how-to-use-regoap-in-unity3d)
10-
1. [How to implement your own GoapAction](#how-to-implement-your-own-goapaction)
11-
2. [How to implement your own GoapGoal](#how-to-implement-your-own-goapgoal)
12-
3. [How to implement your own GoapSensor](#how-to-implement-your-own-goapsensor)
10+
1. [How to implement your own ReGoapAction](#how-to-implement-your-own-regoapaction)
11+
2. [How to implement your own ReGoapGoal](#how-to-implement-your-own-regoapgoal)
12+
3. [How to implement your own ReGoapSensor](#how-to-implement-your-own-regoapsensor)
1313
3. [Debugging](#debugging)
1414
4. [Pull Requests](#pull-requests)
1515

@@ -65,7 +65,7 @@ Examples:
6565

6666
*IMPORTANT*: false preconditions are NOT supported
6767
*IMPORTANT*: the action effects aren't written in the memory when the action is done, this is a wanted behaviour because in most of the games you will want to set these variables from the memory or from the sensors.
68-
If you want you can override Exit in your GoapAction and set the effects to the memory, example following.
68+
If you want you can override Exit in your ReGoapAction and set the effects to the memory, example following.
6969

7070

7171
#### Goal
@@ -97,49 +97,51 @@ Command line:
9797
git clone https://github.com/luxkun/ReGoap.git
9898
```
9999
2. Create a GameObject for your Agent
100-
3. Add a GoapAgent component, choose a name (it is advised to create your own class that inherit GoapAgent, or implements IReGoapAgent)
101-
4. Add a GoapMemory component, choose a name (it is advised to create your own class that inherit GoapMemory, or implements IReGoapMemory)
102-
5. [optional | repeat as needed] Add your own sensor class that inherit GoapSensor or implements IReGoapSensor
103-
6. [repeat as needed] Add your own class that inherit GoapAction or implements IReGoapAction (choose wisely what preconditions and effects should this action have) and implement the action logic by overriding the Run function, this function will be called by the GoapAgent.
104-
7. [repeat as needed] Add your own class that inherit GoapGoal or implements IReGoapGoal (choose wisely what goal state the goal has)
105-
8. Add ONE GoapPlannerManager to any GameObject (not the agent!), this will handle all the planning in multiple-threads.
100+
3. Add a ReGoapAgent component, choose a name (you must create your own class that inherit ReGoapAgent, or implements IReGoapAgent)
101+
4. Add a ReGoapMemory component, choose a name (you must create your own class that inherit ReGoapMemory, or implements IReGoapMemory)
102+
5. [optional | repeat as needed] Add your own sensor class that inherit ReGoapSensor or implements IReGoapSensor
103+
6. [repeat as needed] Add your own class that inherit ReGoapAction or implements IReGoapAction (choose wisely what preconditions and effects should this action have) and implement the action logic by overriding the Run function, this function will be called by the ReGoapAgent.
104+
7. [repeat as needed] Add your own class that inherit ReGoapGoal or implements IReGoapGoal (choose wisely what goal state the goal has)
105+
8. Add ONE ReGoapPlannerManager (you must create your own class that inherit ReGoapPlannerManager) to any GameObject (not the agent!), this will handle all the planning.
106106
9. Play the game :-)
107107

108108
What's more? nothing really, the library will handle all the planning, choose the actions to complete a goal and run the first one until it's done, then the second one and so on, all you need to do is implement your own actions and goals.
109109

110110
In the next paragraphs I'll explain how to create your own classes (but for most of behaviours all you need to implement is GoapAction and GoapGoal).
111111

112-
#### How to implement your own GoapAction
112+
#### How to implement your own ReGoapAction
113113
Check out the actions in this example: https://github.com/luxkun/ReGoap/tree/master/ReGoap/Unity/FSMExample/Actions
114114

115-
Check out GoapAction implementation, to see what functions you can override: https://github.com/luxkun/ReGoap/blob/master/ReGoap/Unity/GoapAction.cs
115+
Check out ReGoapAction implementation, to see what functions you can override: https://github.com/luxkun/ReGoap/blob/master/ReGoap/Unity/ReGoapAction.cs
116116

117-
You can also implement your own GoapAction by implementing IReGoapAction interface, not advised except you know what you are doing!
117+
You must implement your own ReGoapAction by implementing IReGoapAction interface or inheriting ReGoapAction.
118+
Choose wisely the generic types, they must be the same across all the classes of the agent.
119+
Usually string, object is the most generic, also int/enum, object is as well generic but lighter.
118120

119121
For a simple implementation all you have to do is this:
120122
```C#
121-
public class MyGoapAction : GoapAction
123+
public class MyGoapAction : ReGoapAction<string, object>
122124
{
123125
protected override void Awake()
124126
{
125127
base.Awake();
126-
preconditions.Set("myPrecondition", myValue); // myValue can be anything, it's an object internally
128+
preconditions.Set("myPrecondition", myValue);
127129
effects.Set("myEffects", myValue);
128130
}
129-
public override void Run(IReGoapAction previous, IReGoapAction next, IReGoapActionSettings settings, ReGoapState goalState, Action<IReGoapAction> done, Action<IReGoapAction> fail)
131+
public override void Run(IReGoapAction<string, object> previous, IReGoapAction<string, object> next, IReGoapActionSettings<string, object> settings, ReGoapState<string, object> goalState, Action<IReGoapAction<string, object>> done, Action<IReGoapAction<string, object>> fail)
130132
{
131133
base.Run(previous, next, goalState, done, fail);
132134
// do your own game logic here
133-
// when done, in this function or outside this function, call the done or fail callback, automatically saved to doneCallback and failCallback by GoapAction
134-
doneCallback(this); // this will tell the GoapAgent that the action is succerfully done and go ahead in the action plan
135-
// if the action has failed then run failCallback(this), the GoapAgent will automatically invalidate the whole plan and ask the GoapPlannerManager to create a new plan
135+
// when done, in this function or outside this function, call the done or fail callback, automatically saved to doneCallback and failCallback by ReGoapAction
136+
doneCallback(this); // this will tell the ReGoapAgent that the action is succerfully done and go ahead in the action plan
137+
// if the action has failed then run failCallback(this), the ReGoapAgent will automatically invalidate the whole plan and ask the ReGoapPlannerManager to create a new plan
136138
}
137139
}
138140
```
139141

140-
As written before the GoapAction does not, by default, write the effects on the memory, but the memory should check out if the effects are effectively done, if for any reason you want to set the effects at the end of the action you can add this code to your GoapAction implementation:
142+
As written before the ReGoapAction does not, by default, write the effects on the memory, but the memory should check out if the effects are effectively done, if for any reason you want to set the effects at the end of the action you can add this code to your ReGoapAction implementation:
141143
```C#
142-
public override void Exit(IReGoapAction next)
144+
public override void Exit(IReGoapAction<string, object> next)
143145
{
144146
base.Exit(next);
145147

@@ -155,20 +157,20 @@ You can also have preconditions and effects that are dynamically changed based o
155157
Check out FSMExample to see how to do this:
156158
https://github.com/luxkun/ReGoap/blob/master/ReGoap/Unity/FSMExample/Actions/GenericGoToAction.cs
157159

158-
#### How to implement your own GoapGoal
160+
#### How to implement your own ReGoapGoal
159161
This is less tricky, most of the goal will only override the Awake function to add your own goal state (objectives).
160162

161-
Anyway check out GoapGoal, like everything you can implement your own class from scratch by implementing IReGoapGoal interface: https://github.com/luxkun/ReGoap/blob/master/ReGoap/Unity/GoapGoal.cs
163+
Anyway check out ReGoapGoal, like everything you have to implement your own class from scratch by implementing IReGoapGoal interface or inheriting ReGoapGoal: https://github.com/luxkun/ReGoap/blob/master/ReGoap/Unity/ReGoapGoal.cs
162164

163165
Also check out the goals in this example: https://github.com/luxkun/ReGoap/tree/master/ReGoap/Unity/FSMExample/Goals
164166

165167
```C#
166-
public class MyGoapGoal : GoapGoal
168+
public class MyGoapGoal : ReGoapGoal<string, object>
167169
{
168170
protected override void Awake()
169171
{
170172
base.Awake();
171-
goal.Set("myRequirement", myValue); // like any State myValue is an object, so can be anything
173+
goal.Set("myRequirement", myValue);
172174
}
173175
}
174176
```
@@ -178,10 +180,10 @@ Check out GoapSensor basic class here: https://github.com/luxkun/ReGoap/blob/mas
178180

179181
Check out examples here: https://github.com/luxkun/ReGoap/tree/master/ReGoap/Unity/FSMExample/Sensors
180182

181-
As always you can implement your own class by implementing IReGoapSensor interface.
183+
As always you have to implement your own class by inheriting ReGoapSensor or implementing IReGoapSensor interface.
182184

183185
```C#
184-
public class MySensor : GoapSensor
186+
public class MySensor : ReGoapSensor<string, object>
185187
{
186188
void FixedUpdate()
187189
{

ReGoap/Core/IReGoapAction.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
using System;
22
using System.Collections.Generic;
33

4-
public interface IReGoapAction
4+
public interface IReGoapAction<T, W>
55
{
66
// this should return current's action calculated parameter, will be added to the run method
77
// userful for dynamic actions, for example a GoTo action can save some informations (wanted position)
88
// while being chosen from the planner, we save this information and give it back when we run the method
9-
IReGoapActionSettings GetSettings(IReGoapAgent goapAgent, ReGoapState goalState);
10-
void Run(IReGoapAction previousAction, IReGoapAction nextAction, IReGoapActionSettings settings, ReGoapState goalState, Action<IReGoapAction> done, Action<IReGoapAction> fail);
11-
void Exit(IReGoapAction nextAction);
9+
IReGoapActionSettings<T, W> GetSettings(IReGoapAgent<T, W> goapAgent, ReGoapState<T, W> goalState);
10+
void Run(IReGoapAction<T, W> previousAction, IReGoapAction<T, W> nextAction, IReGoapActionSettings<T, W> settings, ReGoapState<T, W> goalState, Action<IReGoapAction<T, W>> done, Action<IReGoapAction<T, W>> fail);
11+
void Exit(IReGoapAction<T, W> nextAction);
1212
Dictionary<string, object> GetGenericValues();
1313
string GetName();
1414
bool IsActive();
15-
void PostPlanCalculations(IReGoapAgent goapAgent);
15+
void PostPlanCalculations(IReGoapAgent<T, W> goapAgent);
1616
bool IsInterruptable();
1717
void AskForInterruption();
1818
// THREAD SAFE
19-
ReGoapState GetPreconditions(ReGoapState goalState, IReGoapAction next = null);
20-
ReGoapState GetEffects(ReGoapState goalState, IReGoapAction next = null);
21-
bool CheckProceduralCondition(IReGoapAgent goapAgent, ReGoapState goalState, IReGoapAction nextAction = null);
22-
float GetCost(ReGoapState goalState, IReGoapAction next = null);
19+
ReGoapState<T, W> GetPreconditions(ReGoapState<T, W> goalState, IReGoapAction<T, W> next = null);
20+
ReGoapState<T, W> GetEffects(ReGoapState<T, W> goalState, IReGoapAction<T, W> next = null);
21+
bool CheckProceduralCondition(IReGoapAgent<T, W> goapAgent, ReGoapState<T, W> goalState, IReGoapAction<T, W> nextAction = null);
22+
float GetCost(ReGoapState<T, W> goalState, IReGoapAction<T, W> next = null);
2323
// DO NOT CHANGE RUNTIME ACTION VARIABLES, precalculation can be runned many times even while an action is running
24-
void Precalculations(IReGoapAgent goapAgent, ReGoapState goalState);
24+
void Precalculations(IReGoapAgent<T, W> goapAgent, ReGoapState<T, W> goalState);
2525
}
2626

27-
public interface IReGoapActionSettings
27+
public interface IReGoapActionSettings<T, W>
2828
{
2929
}

ReGoap/Core/IReGoapAgent.cs

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
using System.Collections.Generic;
22

3-
public interface IReGoapAgent
3+
public interface IReGoapAgent<T, W>
44
{
5-
IReGoapMemory GetMemory();
6-
IReGoapGoal GetCurrentGoal();
7-
void WarnPossibleGoal(IReGoapGoal goal);
5+
IReGoapMemory<T, W> GetMemory();
6+
IReGoapGoal<T, W> GetCurrentGoal();
7+
// called from a goal when the goal is available
8+
void WarnPossibleGoal(IReGoapGoal<T, W> goal);
89
bool IsActive();
9-
List<ReGoapActionState> GetStartingPlan();
10-
T GetPlanValue<T>(string key);
11-
void SetPlanValue<T>(string key, T value);
12-
bool HasPlanValue(string target);
10+
List<ReGoapActionState<T, W>> GetStartingPlan();
11+
W GetPlanValue(T key);
12+
void SetPlanValue(T key, W value);
13+
bool HasPlanValue(T target);
1314
// THREAD SAFE
14-
List<IReGoapGoal> GetGoalsSet();
15-
List<IReGoapAction> GetActionsSet();
15+
List<IReGoapGoal<T, W>> GetGoalsSet();
16+
List<IReGoapAction<T, W>> GetActionsSet();
17+
ReGoapState<T, W> InstantiateNewState();
1618
}

ReGoap/Core/IReGoapAgentHelper.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
// interface needed only in Unity to use GetComponent and such features for generic agents
7+
public interface IReGoapAgentHelper
8+
{
9+
Type[] GetGenericArguments();
10+
}

ReGoap/Core/IReGoapAgentHelper.cs.meta

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ReGoap/Core/IReGoapGoal.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using System;
22
using System.Collections.Generic;
33

4-
public interface IReGoapGoal
4+
public interface IReGoapGoal<T, W>
55
{
6-
void Run(Action<IReGoapGoal> callback);
6+
void Run(Action<IReGoapGoal<T, W>> callback);
77
// THREAD SAFE METHODS (cannot use any unity library!)
8-
Queue<ReGoapActionState> GetPlan();
8+
Queue<ReGoapActionState<T, W>> GetPlan();
99
string GetName();
10-
void Precalculations(IGoapPlanner goapPlanner);
10+
void Precalculations(IGoapPlanner<T, W> goapPlanner);
1111
bool IsGoalPossible();
12-
ReGoapState GetGoalState();
12+
ReGoapState<T, W> GetGoalState();
1313
float GetPriority();
14-
void SetPlan(Queue<ReGoapActionState> path);
14+
void SetPlan(Queue<ReGoapActionState<T, W>> path);
1515
float GetErrorDelay();
1616
}

ReGoap/Core/IReGoapMemory.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public interface IReGoapMemory
1+
public interface IReGoapMemory<T, W>
22
{
3-
ReGoapState GetWorldState();
3+
ReGoapState<T, W> GetWorldState();
44
}

ReGoap/Core/IReGoapSensor.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
// you want to re-use different sensors in different agents
88
// the interface does not dictate how you should update the memory from the sensor
99
// - in a unity game probably you will want to update the memory in the sensor's Update/FixedUpdate
10-
public interface IReGoapSensor
10+
public interface IReGoapSensor<T, W>
1111
{
12-
void Init(IReGoapMemory memory);
13-
IReGoapMemory GetMemory();
12+
void Init(IReGoapMemory<T, W> memory);
13+
IReGoapMemory<T, W> GetMemory();
1414
void UpdateSensor();
1515
}

ReGoap/Core/ReGoapActionState.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
public class ReGoapActionState
1+
public class ReGoapActionState<T, W>
22
{
3-
public IReGoapAction Action;
4-
public IReGoapActionSettings Settings;
3+
public IReGoapAction<T, W> Action;
4+
public IReGoapActionSettings<T, W> Settings;
55

6-
public ReGoapActionState(IReGoapAction action, IReGoapActionSettings settings)
6+
public ReGoapActionState(IReGoapAction<T, W> action, IReGoapActionSettings<T, W> settings)
77
{
88
Action = action;
99
Settings = settings;

0 commit comments

Comments
 (0)