perception

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package perception provides server-side AI perception primitives: sight cones with line-of-sight predicates, hearing radii driven by caller-emitted stimuli, and a threat ring with TTL-based decay.

Each Sensor is per-entity and owns a small ring buffer of recent stimuli. Sight uses a spatial index to gather candidates within range, applies a cone test (FOV half-angle + range), then defers to a caller-supplied LOSFn (typically backed by navmesh or world collision) for final visibility resolution.

Game-AI-specific; lives under kit/game/.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// MemoryTTL is how long a stimulus or last-seen target lingers in
	// the threat ring before decaying. Defaults to 5s.
	MemoryTTL time.Duration
	// MaxStimuli bounds the in-memory stimulus ring. Defaults to 16.
	MaxStimuli int
	// Clock supplies the current time; defaults to time.Now.
	Clock func() time.Time
}

Config configures a Sensor.

type EntityID

type EntityID uint64

EntityID identifies a target the sensor can see/hear.

type LOSFn

type LOSFn func(from, to geometry.Vec3) bool

LOSFn is the caller-supplied line-of-sight predicate. Returns true when `from` can see `to` (no occluders).

type Sensor

type Sensor[T Targetable] struct {
	// contains filtered or unexported fields
}

Sensor is a per-entity perception state. Not safe for concurrent access; typical owner is the per-entity actor goroutine.

func NewSensor

func NewSensor[T Targetable](index spatial.Index[T], los LOSFn, cfg Config) *Sensor[T]

NewSensor constructs a Sensor over a spatial index with the given LOS predicate (nil ⇒ always visible).

func (*Sensor[T]) Hear

func (s *Sensor[T]) Hear(stim Stimulus)

Hear records a stimulus heard by this sensor. Out-of-range stimuli are dropped silently.

func (*Sensor[T]) RecentStimuli

func (s *Sensor[T]) RecentStimuli(now time.Time) []Stimulus

RecentStimuli returns recent stimuli within MemoryTTL, newest first.

func (*Sensor[T]) Threat

func (s *Sensor[T]) Threat() (EntityID, bool)

Threat returns the current top-priority threat, or (0, false) if the sensor has no live target.

func (*Sensor[T]) Tick

func (s *Sensor[T]) Tick(now time.Time)

Tick refreshes the visible set, decays the threat memory.

func (*Sensor[T]) UpdateOwner

func (s *Sensor[T]) UpdateOwner(eye, facing geometry.Vec3, cone SightCone, hearingR float32)

UpdateOwner refreshes the sensor's eye position, facing direction (unit vector), sight cone, and hearing radius.

func (*Sensor[T]) Visible

func (s *Sensor[T]) Visible() []EntityID

Visible returns the entity ids visible to this sensor at the last Tick. The returned slice is owned by the sensor; copy before retaining across calls.

type SightCone

type SightCone struct {
	// Range is the maximum sight distance, in world units.
	Range float32
	// HalfAngleRads is half the cone's apex angle, in radians (0..π).
	HalfAngleRads float32
}

SightCone defines a sensor's visual frustum.

type Stimulus

type Stimulus struct {
	Source   EntityID
	Position geometry.Vec3
	// Loudness is 0..1; the sensor compares against (distance/hearingRadius).
	Loudness float32
	At       time.Time
}

Stimulus is one perceptible event (a sound, a movement, a hit).

type Targetable

type Targetable interface {
	spatial.Locatable
	EntityID() EntityID
}

Targetable is anything a Sensor can perceive: a spatial.Locatable with an EntityID().

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL