gale.ai.markov module

This file contains support for Markov-process-driven behavior: MarkovChain (a weighted-random transition table) and MarkovState (a gale.sequence.Step – the same lifecycle already used by gale.quest and gale.cutscene) tied together by MarkovStateMachine, which picks its next state probabilistically through the chain whenever the current one completes. Useful to vary a character’s idle/patrol behavior without scripting a fixed sequence, while still reusing the same enter/update/is_complete lifecycle as the rest of gale.

Author: Alejandro Mujica (aledrums@gmail.com)

class gale.ai.markov.MarkovChain[source]

Bases: Generic[T]

A weighted-random transition table between states: add_transition records how likely one state is to lead to another, and next_state samples a next state according to those weights.

Usage example:

chain = MarkovChain() chain.add_transition(“idle”, “patrol”, 0.7) chain.add_transition(“idle”, “investigate”, 0.3) chain.next_state(“idle”) # “patrol” about 70% of the time

add_transition(state, next_state, weight)[source]
Parameters:
  • state (T) – The state this transition starts from.

  • next_state (T) – The state this transition leads to.

  • weight (float) – Relative likelihood of this transition being chosen, among every transition added for state. Weights don’t need to add up to 1; they’re normalized when sampled.

Return type:

None

next_state(state)[source]
Parameters:

state (T) – The state to sample a transition from.

Returns:

A next state chosen among state’s transitions, weighted by the weight each was added with.

Raises:

KeyError – If state has no transitions added.

Return type:

T

class gale.ai.markov.MarkovState(name, **kwargs)[source]

Bases: Step

A single named state of a MarkovStateMachine: a gale.sequence.Step (so it gets enter/update/is_complete, and can complete after a fixed duration or on a specific input for free) that also does whatever per-frame work it needs via update/render, exactly like any other Step.

Parameters:
  • name (str)

  • kwargs (Any)

class gale.ai.markov.MarkovStateMachine(chain, states, start)[source]

Bases: object

Drives a set of MarkovState objects, picking the next one probabilistically (via a MarkovChain over their names) whenever the current one’s is_complete() turns True – the same “run the active one until it’s done” idea as gale.sequence.Sequence, except the next step is chosen randomly instead of following a fixed order.

Usage example:

chain = MarkovChain() chain.add_transition(“idle”, “patrol”, 0.7) chain.add_transition(“idle”, “investigate”, 0.3) chain.add_transition(“patrol”, “idle”, 1.0) chain.add_transition(“investigate”, “idle”, 1.0)

machine = MarkovStateMachine(

chain, {

“idle”: IdleState(“idle”, duration=2.0), “patrol”: PatrolState(“patrol”, duration=5.0), “investigate”: InvestigateState(“investigate”, duration=3.0),

}, start=”idle”,

)

# In the game loop: machine.update(dt)

Parameters:
update(dt)[source]
Parameters:

dt (float) – Time elapsed (in seconds) since the last update.

Return type:

None

render(surface)[source]
Parameters:

surface (Any) – The surface to draw on.

Return type:

None