-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
59 lines (50 loc) · 1.1 KB
/
action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package goap
import (
"errors"
)
type Action interface {
String() string
Cost() int
CanRun(Agent) bool
Run(Agent) (Agent, error)
}
// ensure DefaultAction implements Action
var _ Action = (*DefaultAction)(nil)
type DefaultAction struct {
name string
cost int
conditions State
effects State
}
func (a *DefaultAction) String() string {
return a.name
}
func (a *DefaultAction) Cost() int {
return a.cost
}
func (a *DefaultAction) CanRun(agent Agent) bool {
conditionsMet := agent.WorldState.Contains(a.conditions)
effectsAchieved := agent.WorldState.Contains(a.effects)
return conditionsMet && !effectsAchieved
}
func (a *DefaultAction) Run(agent Agent) (Agent, error) {
if a.CanRun(agent) == false {
return Agent{}, errors.New("Action invalid")
}
newAgent := Agent{
agent.Actions,
State{},
agent.Goals,
}
newAgent.WorldState.Update(agent.WorldState)
newAgent.WorldState.Update(a.effects)
return newAgent, nil
}
func CreateAction(name string, cost int, conditions State, effects State) *DefaultAction {
return &DefaultAction{
name,
cost,
conditions,
effects,
}
}