Good question. This is a simple implementation of Goal-Oriented Action Planning (G.O.A.P.) in JavaScript.
If you're making a game with NPC characters that you want to appear realistic and smart, the GOAP can be a great option.
Given the following world state, goals and actions:
export const worldState = {
axe_available: true,
player: {
axe_equipped: false,
wood: 0
}
};
export const actions = {
chopWood: {
condition: s => s.player.axe_equipped,
effect: s => {
s.player.wood++;
return s;
},
cost: s => 2
},
getAxe: {
condition: s => !s.player.axe_equipped && s.axe_available,
effect: s => {
s.player.axe_equipped = true;
return s;
},
cost: s => 2
},
gatherWood: {
condition: s => true,
effect: s => {
s.player.wood++;
return s;
},
cost: s => 5
}
};
export const goals = {
collectWood: {
label: "Collect Wood",
validate: (prevState, nextState) => {
return nextState.player.wood > prevState.player.wood;
}
}
};
The GOAP Planner will create the following plan:
{
"cost": 4,
"goal": {
"label": "Collect Wood"
},
"actions": [
"getAxe",
"chopWood"
]
}
For a more complex example, see Sim 2.
Check out the following resource:
- Install the dependencies
npm init
oryarn
- Run the test
npm run test
oryarn test