engine-mk1
Planner

The Planner library is an implementation of goal oriented action planning (GOAP). GOAP is used as a more dynamic alternative to behaviour trees. It works by taking a disparate set of actions and working backwards from a goal to a bespoke tree. Actions are sets of pre/post-conditions that act on a world state.

Examples

Creating Conditions

The Planner describes state and atomic state changes through conditions, modifiers and properties. Their only difference is the operators they make use of. Multiple can be held together in a ConditionSet.

Conditions consist of a value and comparison operator.

rz::planner::Condition hasPick{ "PLAYER", "HAS_PICK", 0, Operation::GREATER_EQUAL };

Modifiers consist of a value and arithmatic operator.

rz::planner::Condition addMetal{ "INVENTORY", "METAL", 1, Operation::PLUS };

Properties consist of a value and null/no operator.

rz::planner::Condition availableMetal{ "INVENTORY", "METAL", 3};

Creating Actions

Actions are a larger collection of operations that can be performed on a world state.

actions.push_back(Action{ "CRAFT_TOOL", 2,
ConditionSet{
Condition{ "INVENTORY", "WOOD", 0, Operation::GREATER_EQUAL },
Condition{ "INVENTORY", "METAL", 0, Operation::GREATER_EQUAL } },
ConditionSet{
Condition{ "INVENTORY", "TOOL", 1, Operation::PLUS },
Condition{ "INVENTORY", "WOOD", 1, Operation::MINUS },
Condition{ "INVENTORY", "METAL", 1, Operation::MINUS } } });

Generating plans

Plans can be generated by supplying the Planner with an ActionSet and a goal Action. A goal Action is an Action with preconditions but no postconditions, that the Planner can use as a starting point to plan from.

Planner p;
// Optional world state ConditionSet
ConditionSet worldState{Condition{ "PLAYER", "HAS_AXE", 1 }};
p.setWorldState(worldState);
// define actions here
// action1, action2, action3, ..., actionN
ActionSet actions{action1, action2, action3, actionN};
// Define a goal Action.
Action goal{ "GOAL", 0,
ConditionSet{
Condition{ "INVENTORY", "TOOL", 1, Operation::GREATER_EQUAL, 5 } } };
auto plan = p.plan(actions, goal);
// Plans can be saved to disk (in DOT format)
p.savePlan("plan.dot");

Generated Plan. Red equals failed plans, green equals valid plans