Skip to main content

Game AI algorithms in pure Python

Project description

pychology

Simple Python implementations of popular game AI techniques.

Current State: Very alpha. Only behavior trees are implemented.

Behavior Trees

Behavior trees are tree structures that represent the logic of choosing and switching the current task. Each node in the tree can be considered as a function that is called, and returns an indication of the current task's state; Whether it is active, done, or has failed.

The tasks themselves are the leafs of the tree. pychology expects you to provide functions to call when a task is started or continued, and that it returns one of the above mentioned node states, namely NodeState.ACTIVE, NodeState.DONE, or NodeState.FAILED. These functions then are plugged into pychology.behavior_trees.Action.

Inner nodes either have a single child and do some logic; These are called decorators, and do a lot of heavy lifting. They can change the returning node state, prevent sub-trees from being executed, check for conditions, count how often or for how long a sub-tree has been active, provide debug information, and much more.

There's also nodes that have multiple children. The two most popular are the Chain (usually called Sequence), which executes a series of tasks (or rather sub-trees) one after the other, and Priorities (also called Selector or Fallback) which tries running one sub-tree after the other until one reports something other than failure.

The class pychology.behavior_trees.BehaviorTree can act as a root node that automatically resets the tree's state after it has finished or failed, and can also run arbitrary code to integrate with the wider environment that the tree is embedded in.

An example on how to go through a door:

BehaviorTree(
    Chain(
        DoneOnPrecondition(                  # The first task is done
            is_door_open,                    # if the door is open,
            Priorities(                      # otherwise try to
                Action(open_door),           # open it
                ReturnActiveOnDone(          # or try again after
                    Action(unlock_door),     # unlocking it,
                ),
                ReturnActiveOnDone(          # or try again after
                    Action(pick_door_lock),  # picking the lock,
                ),
                FailOnPrecounter(            # or try
                    4,                       # three times to
                    Action(kick_in_door),    # kick in the door.
                ),
            ),
        ),
        Action(walk_through_door),           # Afterwards, go through.
    ),
)

TODO

  • Project
    • Packaging
  • Behavior Trees
    • Multinodes: Parallel, weighted random choice
    • Decorators: More return value logic
    • Blackboard support
    • Debug tools
    • Visualization
    • De-/Serialization
    • Tests
  • Hierarchical Finite State Machines (HFSM): Everything
  • Goal-Oriented Action Planning (GOAP): Everything
  • Hierarchical Task Planning (HTN): Everything
  • Whatever this is

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pychology-0.1a0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file pychology-0.1a0-py3-none-any.whl.

File metadata

  • Download URL: pychology-0.1a0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.1

File hashes

Hashes for pychology-0.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b70fd245101a91ff78c41ca270ce0ccbeeacf66e1c6c947a2cd7adb00ac3d64
MD5 d6b72fd30d922181aab088b46679efef
BLAKE2b-256 c60d0d8cdd9da046419e5b02ef6325b5c323dc4381cc87dec3dffa597b502ec3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page