sense

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 4 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 sense is the perception engine behind ai.PerceptionSystem and perception3d.System: sight cones, a round-robin line-of-sight budget, hearing, and per-stimulus memory, all on float64 3D vectors so the 2D and 3D front ends share one implementation.

Its numeric codes match the public types in package ai: attitudes are 0 hostile, 1 neutral and 2 friendly; senses are 1 sight and 2 hearing; event kinds are 1 gained, 2 updated, 3 lost and 4 forgotten.

Index

Constants

View Source
const (
	Hostile  uint8 = 0
	Neutral  uint8 = 1
	Friendly uint8 = 2
)

Attitude codes.

View Source
const (
	BySight   uint8 = 1
	ByHearing uint8 = 2
)

Sense codes.

View Source
const (
	Gained    uint8 = 1
	Updated   uint8 = 2
	Lost      uint8 = 3
	Forgotten uint8 = 4
)

Event kind codes.

Variables

This section is empty.

Functions

func Detects

func Detects(mask, attitude uint8) bool

Detects reports whether mask detects attitude, treating a zero mask as hostile only.

Types

type Agent

type Agent struct {
	State      *State
	Self       uint64
	HasSelf    bool
	Team       uint8
	Eye, Fwd   Vec
	Sight      Sight
	HearRange  float64
	HearDetect uint8
	Forget     time.Duration
}

Agent is one agent's input to a step.

type Engine

type Engine struct {
	Agents []Agent
	Stats  Stats
	// contains filtered or unexported fields
}

Engine steps agents against targets and noises and owns the round-robin line-of-sight cursor. Fill Agents and call SetTargets before each Step.

func (*Engine) PostNoise

func (e *Engine) PostNoise(n Noise)

PostNoise queues n for the next Step.

func (*Engine) Register

func (e *Engine) Register(st *State, owner any) bool

Register claims st for the system owner and gives it the next place in the agent order. It reports false when owner already has st and panics when another system does. A struct copy of a registered State is a separate agent: Register gives it its own copy of the memories, no events and no owner before claiming it, so the copy and the original never share backing arrays.

func (*Engine) SetTargets

func (e *Engine) SetTargets(n int, at func(i int) Target)

SetTargets sets the step's targets from n calls to at, sorted by ID with the first of each ID kept.

func (*Engine) Step

func (e *Engine) Step(dt time.Duration, budget int, attitude func(observer, other uint8) uint8, los func(from, to Vec) bool)

Step advances every agent by dt. attitude rates a team as seen from an observer's team; los reports a clear line of sight and may be nil.

type Event

type Event struct {
	Kind   uint8
	Memory Memory
}

Event is a change to one memory during a step, with the memory as the change left it.

type Memory

type Memory struct {
	ID       uint64
	Team     uint8
	Attitude uint8
	Sense    uint8
	Pos, Vel Vec
	Age      time.Duration
	Visible  bool
	// contains filtered or unexported fields
}

Memory is one remembered stimulus.

func (*Memory) Strength

func (m *Memory) Strength(forget time.Duration) float32

Strength is the memory's strength given the agent's forget time: its sensed strength falling linearly to 0 over forget.

type Noise

type Noise struct {
	ID       uint64
	Team     uint8
	Pos      Vec
	Loudness float64
}

Noise is a stimulus heard on the next step.

type Sight

type Sight struct {
	// contains filtered or unexported fields
}

Sight is an agent's resolved vision.

func NewSight

func NewSight(rng, halfAngle, periphRange, periphHalf float32, detect uint8) Sight

NewSight resolves a sight configuration: a cone of halfAngle radians out to rng, and a peripheral cone of periphHalf radians (zero meaning all around) out to periphRange.

type State

type State struct {
	Mems   []Memory
	Events []Event
	// contains filtered or unexported fields
}

State is one agent's memories, sorted by ID, and the events of its last step.

func (*State) Clear

func (st *State) Clear()

Clear drops every memory and event.

func (*State) Find

func (st *State) Find(id uint64) (int, bool)

Find returns the index of the memory of id, or where it would be inserted.

func (*State) Forget

func (st *State) Forget(id uint64) bool

Forget drops the memory of id and reports whether there was one.

func (*State) Release

func (st *State) Release()

Release detaches st from the system that owned it. It keeps the memories but marks none of them visible and drops the last step's events, so a removed agent reports no stale sightings.

type Stats

type Stats struct {
	Candidates, Checks, Deferred int
}

Stats is the line-of-sight work of a step.

type Target

type Target struct {
	ID       uint64
	Team     uint8
	Pos, Vel Vec
}

Target is a stimulus that can be seen during a step.

type Vec

type Vec struct{ X, Y, Z float64 }

Vec is a 3D vector; 2D callers leave Z zero.

func (Vec) Add

func (a Vec) Add(b Vec) Vec

Add returns a + b.

func (Vec) Norm

func (a Vec) Norm(fallback Vec) Vec

Norm returns a scaled to unit length, or fallback when a has no finite non-zero length.

func (Vec) Sub

func (a Vec) Sub(b Vec) Vec

Sub returns a - b.

Jump to

Keyboard shortcuts

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