An easy to use hierarchical task planner (HTN) for JS. Based on FluidHTN.
npm install gameplan-htnA simple but powerful HTN planner in Javascript based on the excellent work of FluidHTN. There are several changes to the library to make it more idiomatic JS rather than C# (these are detailed below.)
``js
new Domain({
name: "MyDomain",
tasks: [
{
name: "GetC",
type: "select",
tasks: [
{
name: "Get C (Primitive Task)",
conditions: [
// Has A and B
(context) => context.hasState("HasA") && context.hasState("HasB"),
// Has NOT C
(context) => !context.hasState("HasC"),
],
operator: () => {
log.info("Get C");
return TaskStatus.Success;
},
effects: [
// Has C
(context) => context.setState("HasC"),
],
},
],
},
{
name: "GatAandB",
type: "sequence",
tasks: [
{
name: "Get A (Primitive Task)",
conditions: [
// Has NOT A NOR B
(context) => !(context.hasState("HasA") && context.hasState("HasB")),
],
operator:
// Get A
() => {
log.info("Get A");
return TaskStatus.Success;
},
effects: [
// Has A
(context) => context.setState("HasA"),
],
}, {
name: "Get B (Primitive Task)",
operator:
// Get A
() => {
log.info("Get B");
return TaskStatus.Success;
},
effects: [
// Has B
(context) => context.setState("HasB"),
],
},
],
},
{
name: "Done",
type: "select",
tasks: [
{
name: "Done",
operator: (context) => {
log.info("Done");
context.Done = true;
return TaskStatus.Continue;
},
},
],
},
],
});
`
A context is used to track our world state for the purposes of planning. A Context contains methods for setting/getting world state, and starts with a simple set of GetState(), SetState() and HasState() methods, but in most cases you will want to add functions to the Context object.
There are a few significant changes from FluidHTN:
1) GamePlanHTN uses object keys for world state rather than an array indexed by an enum, this simplifies finding worldstate to context.WorldState.HasC rather than context.WorldState[(int)MyWorldState.HasC]
1) The Context object's function set is mutable at runtime. You can assign functions directly to it at runtime, which means you do not necessarily need to subclass it for simple cases.
`js
let context = new Context();
context.WorldState = {
HasA: 0,
HasB: 0,
HasC: 0,
};
context.init();
`
to true on the context.`js
let domain = new Domain({ / see large definition above /});
let context = new Context();
context.WorldState = {
HasA: 0,
HasB: 0,
HasC: 0,
};let planner = new Planner();
ctx.init();
while (!context.Done) {
planner.tick(domain, context);
}
``