Skip to content

77 game-dev skills for AI coding agents — Godot, Unity, Unreal, Phaser, PixiJS, three.js, Bevy, pygame, LÖVE, Roblox. Portable SKILL.md Agent Skills (the format Anthropic launched as Claude Skills), with a router that loads the right skill for your engine and task. Runs in Claude Code, Cursor, Kiro, Codex, Copilot, Gemini CLI and more.

fix(game-ai): compilable FSM, diagonal-correct A* grid, reactive selector guidance, AStarGrid2D and NavMeshSurface

Kiro Crew176591503103
diff --git a/skills/disciplines/game-ai/SKILL.md b/skills/disciplines/game-ai/SKILL.md
index 7a77e9e..6b90b50 100644
--- a/skills/disciplines/game-ai/SKILL.md
+++ b/skills/disciplines/game-ai/SKILL.md
@@ -54,25 +54,30 @@ spawning waves along lanes, see the `tower-defense` genre skill.
### 1. Finite state machine (one state object, explicit transitions)
```gdscript
-# Each state is a small object with enter/update/exit. The machine owns "current".
-class_name State
-func enter(agent): pass
-func update(agent, dt) -> State: return null # return a new state to transition
-func exit(agent): pass
-
-# --- Chase state: returns Patrol when the player escapes sight range ---
-class Chase extends State:
- func update(agent, dt) -> State:
- if not agent.can_see(agent.target):
- return Patrol.new() # transition by returning next state
- agent.move_toward(agent.target.position, dt)
- return null # null = stay in this state
-
-# --- Driver: call once per frame ---
-func tick(dt):
- var next = current.update(self, dt)
+# Godot 4.7, one class per file.
+# state.gd: each state is a small object with enter/update/exit.
+class_name State extends RefCounted
+func enter(_agent) -> void: pass
+func update(_agent, _dt: float) -> State: return null # return a state to transition
+func exit(_agent) -> void: pass
+
+# chase.gd: returns Patrol when the player escapes sight range.
+class_name Chase extends State
+func update(agent, dt: float) -> State:
+ if not agent.can_see(agent.target):
+ return Patrol.new() # transition by returning the next state
+ agent.move_toward(agent.target.position, dt)
+ return null # null = stay in this state
+
+# enemy.gd: the agent owns `current` and ticks it once per frame.
+var current: State = Patrol.new() # call current.enter(self) in _ready()
+
+func tick(dt: float) -> void:
+ var next := current.update(self, dt)
if next != null:
- current.exit(self); next.enter(self); current = next
+ current.exit(self)
+ next.enter(self)
+ current = next
```
Keep transition logic *inside* states (or in a table), never as a growing pile
@@ -117,7 +122,7 @@ func seek(pos, vel, target, max_speed, max_force) -> Vector2:
func arrive(pos, vel, target, max_speed, max_force, slow_radius) -> Vector2:
var offset = target - pos
var dist = offset.length()
- if dist < 0.001: return -vel # already there: kill drift
+ if dist < 0.001: return (-vel).limit_length(max_force) # already there: brake
var ramped = max_speed * min(dist / slow_radius, 1.0)
var desired = offset / dist * ramped
return (desired - vel).limit_length(max_force)
@@ -130,8 +135,8 @@ func arrive(pos, vel, target, max_speed, max_force, slow_radius) -> Vector2:
```python
# Match the heuristic to the movement. An ADMISSIBLE heuristic (never larger
# than the true remaining cost) keeps A* optimal.
-def heuristic(a, b):
- dx, dy = abs(a.x - b.x), abs(a.y - b.y)
+def heuristic(a, b): # a, b are (x, y) grid cells
+ dx, dy = abs(a[0] - b[0]), abs(a[1] - b[1])
# return dx + dy # Manhattan: 4-direction grids (no diagonals)
return (dx + dy) + (1.414 - 2) * min(dx, dy) # octile: 8-direction grids
# f(n) = g(n) + h(n): g = cost from start, h = heuristic to goal.
@@ -167,6 +172,7 @@ graphs) is in `references/pathfinding.md`.
## Related skills
+- `ai-behavior-trees-utility-ai` — deeper behavior-tree and utility-AI design.
- `unity-navmesh`, `unreal-behavior-trees` — concrete engine AI/navigation APIs.
- `physics-tuning` — movement, collision response, and agent radius.
- `procedural-gen` — generating the graph/level the AI navigates.
diff --git a/skills/disciplines/game-ai/references/behavior-trees.md b/skills/disciplines/game-ai/references/behavior-trees.md
index bb3ddd6..c60be39 100644
--- a/skills/disciplines/game-ai/references/behavior-trees.md
+++ b/skills/disciplines/game-ai/references/behavior-trees.md
@@ -50,6 +50,18 @@ class Sequence:
A Selector is the mirror image: it advances on `FAILURE`, returns on `SUCCESS`
or `RUNNING`, and resets its index when a child succeeds.
+Memory has a cost: while `MoveTo` is `RUNNING`, this Sequence skips
+`CanSeePlayer`, so the guard keeps chasing a player it lost. And a priority
+Selector that resumes its running child never re-checks the higher branches, so a
+running Patrol would block Attack forever. Keep the two roles apart:
+
+- **Priority selectors are reactive**: tick from child 0 every frame, and when a
+ higher child takes over, tell the previously running child to stop (an
+ `abort()`/`reset()` that cancels its move or animation).
+- **Sequences of actions keep memory**, but either re-check their guard
+ conditions each tick (a *reactive sequence*) or let the action fail itself when
+ its precondition no longer holds.
+
## Leaf examples
```gdscript
diff --git a/skills/disciplines/game-ai/references/pathfinding.md b/skills/disciplines/game-ai/references/pathfinding.md
index 5b94255..59a71e4 100644
--- a/skills/disciplines/game-ai/references/pathfinding.md
+++ b/skills/disciplines/game-ai/references/pathfinding.md
@@ -6,13 +6,17 @@ or navmesh polygons — only `neighbors()` and `cost()` change.
A* keeps a priority queue (the *frontier*) ordered by `f = g + h`, where `g` is
the known cost from the start and `h` is a heuristic estimate to the goal. It is
-optimal **iff** `h` never overestimates the true remaining cost (it is
+optimal **if** `h` never overestimates the true remaining cost (it is
*admissible*). With `h = 0`, A* degenerates to Dijkstra's algorithm.
## Complete A* (engine-neutral Python)
```python
-import heapq
+import heapq, math
+
+def heuristic(a, b): # nodes are (x, y) tuples; see the table below
+ dx, dy = abs(a[0] - b[0]), abs(a[1] - b[1])
+ return (dx + dy) + (math.sqrt(2) - 2) * min(dx, dy) # octile (8-way grid)
def a_star(graph, start, goal):
# Priority queue of (f_score, tie, node). heapq pops the SMALLEST first.
@@ -76,28 +80,44 @@ Scale the heuristic by the minimum step cost so it stays in the same units as
```python
class GridGraph:
- def __init__(self, walls, width, height):
- self.walls, self.w, self.h = walls, width, height
+ def __init__(self, walls, width, height, diagonal=True):
+ self.walls, self.w, self.h, self.diagonal = walls, width, height, diagonal
def in_bounds(self, p): return 0 <= p[0] < self.w and 0 <= p[1] < self.h
def passable(self, p): return p not in self.walls
+ def open(self, p): return self.in_bounds(p) and self.passable(p)
def neighbors(self, p):
x, y = p
- candidates = [(x+1,y),(x-1,y),(x,y+1),(x,y-1)] # add diagonals for octile
- return [c for c in candidates if self.in_bounds(c) and self.passable(c)]
+ result = [c for c in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)] if self.open(c)]
+ if self.diagonal:
+ for dx, dy in [(1,1),(1,-1),(-1,1),(-1,-1)]:
+ # No corner cutting: both orthogonal neighbours must be open too.
+ if self.open((x+dx, y+dy)) and self.open((x+dx, y)) and self.open((x, y+dy)):
+ result.append((x+dx, y+dy))
+ return result
def cost(self, a, b):
- return 1.0 # uniform; return terrain weight for varied cost
+ diagonal = a[0] != b[0] and a[1] != b[1]
+ return math.sqrt(2) if diagonal else 1.0 # multiply by a terrain weight >= 1
```
+The step costs must match the heuristic: octile assumes diagonal steps cost `sqrt(2)`.
+If diagonals cost 1 the true distance is Chebyshev (`max(dx, dy)`) and octile
+overestimates; on a 4-way grid (`diagonal=False`) switch the heuristic to Manhattan.
+Terrain weights below 1 make any of these heuristics inadmissible.
+
## When to use an engine navmesh instead
Hand-rolled grid A* is ideal for 2D tile games and for understanding the
algorithm. For 3D worlds, arbitrary geometry, dynamic obstacle avoidance, and
agent radius/height, prefer the engine's baked navigation:
-- **Unity** — bake a NavMesh and drive a `NavMeshAgent` (see `unity-navmesh`).
+- **Unity** — bake a `NavMeshSurface` (AI Navigation package) and drive a
+ `NavMeshAgent` (see `unity-navmesh`).
- **Unreal** — Nav Mesh Bounds + `AIController` MoveTo (see `unreal-behavior-trees`).
- **Godot** — `NavigationRegion2D/3D` + `NavigationAgent2D/3D`, querying
- `NavigationServer` for paths.
+ `NavigationServer2D/3D` for paths. For a tile grid, the built-in `AStarGrid2D`
+ already implements this file: set `region`, `cell_size`, `diagonal_mode` and
+ `default_compute_heuristic`/`default_estimate_heuristic`, call `update()`, mark walls
+ with `set_point_solid()`, then read `get_id_path(from, to)` (or `get_point_path`).
These handle path smoothing, off-mesh links, and crowd avoidance you would
otherwise reimplement. Use A* directly when the world is a discrete grid/graph or