GameReadyHtn 2.1.0

dotnet add package GameReadyHtn --version 2.1.0                
NuGet\Install-Package GameReadyHtn -Version 2.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GameReadyHtn" Version="2.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GameReadyHtn --version 2.1.0                
#r "nuget: GameReadyHtn, 2.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GameReadyHtn as a Cake Addin
#addin nuget:?package=GameReadyHtn&version=2.1.0

// Install GameReadyHtn as a Cake Tool
#tool nuget:?package=GameReadyHtn&version=2.1.0                

<img src="https://github.com/Joy-less/GameReadyHtn/blob/main/Assets/Icon.jpg?raw=true" width=256/>

Game Ready Htn

NuGet

An easy-to-use implementation of HTN (Hierarchical Task Networks) to control game characters in C#.

Features

  • Simple and performant, made for game development
  • Expressive with minimal boilerplate

GOAP vs HTN

This library is parallel to Game Ready Goap, an implementation of GOAP.

Goal-Oriented Action Planning and Hierarchical Task Networks are both powerful choices for controlling NPCs. HTN defines a structured hierarchy of nested tasks that should be completed. GOAP defines a set of goals and a set of actions that can be combined to reach those goals.

HTN is simpler, faster and more predictable because the tasks use a predefined order. It's suitable for most game enemies which only have a limited set of actions. It's an implementation of a behaviour tree.

GOAP is more flexible and powerful because the actions can be combined in very complex ways. It's suitable for agents with lots of actions and strategies to consider, such as in Real-Time Strategy games.

This Reddit discussion provides more comparisons between GOAP, HTN and behaviour trees.

Usage

First, create an agent with initial states and a root task:

GoapAgent Agent = new() {
    // These describe the current state of your agent (character).
    States = new() {
        ...
    },
    // This is the root (compound) task your agent will run, changing its states.
    Task = ...,
};

Then, finding a plan is easy:

Agent.FindPlan();

Executing plans is also easy:

Plan.Execute(Agent, Action => {
    ...
});

Example

A farmer tends to his crops. He starts his day by farming to increase his crop health, which requires energy, then sleeps, increasing his energy. If he's too tired to farm, he sleeps instead.

public static readonly HtnAgent Agent = new() {
    States = new() {
        ["Energy"] = 100,
        ["CropHealth"] = 0,
    },
    Task = new HtnSelectorTask("Root") {
        Tasks = [
            new HtnSequenceTask("CompleteDay") {
                Tasks = [
                    new HtnPrimitiveTask("Farm") {
                        Effects = [
                            new HtnEffect() {
                                State = "CropHealth",
                                Operation = HtnOperation.IncreaseBy,
                                Value = 20,
                            },
                            new HtnEffect() {
                                State = "Energy",
                                Operation = HtnOperation.DecreaseBy,
                                Value = 30,
                            },
                        ],
                        Requirements = [
                            new HtnCondition() {
                                State = "Energy",
                                Comparison = HtnComparison.GreaterThanOrEqualTo,
                                Value = 30,
                            },
                        ],
                    },
                    new HtnPrimitiveTask("Sleep") {
                        Effects = [
                            new HtnEffect() {
                                State = "Energy",
                                Operation = HtnOperation.IncreaseBy,
                                Value = 5,
                            },
                        ],
                    },
                ],
            },
            new HtnPrimitiveTask("Sleep") {
                Effects = [
                    new HtnEffect() {
                        State = "Energy",
                        Operation = HtnOperation.IncreaseBy,
                        Value = 5,
                    },
                ],
            },
        ],
    },
};
Farmer.FindPlan();

We get 2 actions which completes our task by selecting the CompleteDay task:

Action Energy Crop Health
- 100 0
Farm 70 20
Sleep 75 20

If we set Energy to 20 instead of 100, we get 1 action which completes our task by selecting the Sleep task:

Action Energy Crop Health
- 20 0
Sleep 25 0

Special Thanks

  • This Is Vini for explaining the HTN algorithm.
  • Fluid HTN for explaining the difference between selector and sequencer compound tasks.
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.0 34 11/19/2024
2.0.0 28 11/19/2024
1.0.0 33 11/19/2024