vision

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 29 Imported by: 1 module, 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 vision gives entities a narrowed view of the world: a Sight cone sees what falls inside it, within range and not hidden behind something nearer. Each step fills the Sighted of the observers something reads and runs the rules of a Sighting.

Sight and Sighted

Sight is where an entity looks and how far, a knob vision only reads and an effect may Alter; how wide it sees and from how high is its world.Eye, without which it is never scanned. What the last scan found is its Sighted: at most MaxSeen entities, nearest first. Only an observer something reads is scanned: one a hosted rule of a Sighting is for (it plays the rule's role), one carrying SightOutline, or one carrying Watched, which a game puts on a kind whose Sighted it reads itself; Plugin.WithScanAll scans every observer. Another's Sighted is cleared once, as it stops being read; a log (Plugin.WithLog) reads nobody and shows what is scanned. An observer read is scanned every step, or Plugin.WithFrequency times a second of game time, the scan shared among goroutines (Plugin.WithWorkers). Only an observer within two levels of the finest level any observer camera sees looks, every step (lod.Contact): farther off it sees nothing.

Transparency

Every entity cuts sight unless it carries a Transparency: 1 as if absent, 0 cutting, between them dimming. The world's cover (the board's veiled cells) dims or cuts a ray the same way and is never seen. Sight.Blockers are the world.Layers that cut or dim a sight at all; an entity on none of them is looked over and still seen.

Heights

In a world with heights (Plugin.WithBoard; Plugin.WithHeights and Plugin.WithCover for a ground and a cover of one's own) sight follows geometry: the eye is at the observer's world.Eye, every entity spans its world.Z, and the ground is read along every ray: off its corners inside aabbworld's scan where the ground has them (the relief's lattice), else exactly off a ground.Profile, else sampled every Plugin.WithGroundStep. With a world.Scale the ground sinks under the observer's level as far off as it lies. An Eye with a Vertical field sees up and down within it alone, turned up or down by its Tilt (a rider's head): a sentinel on a wall looking down sees the wall's foot, not the distance; the view drawn is the ground the field meets. Eye.Height and Eye.Vertical are refused in a flat world.

Sighting

A rule of a Sighting (act.Branch[vision.Sighting]{When: cond.Other.If(role), …}) runs once a step per observer playing the rule's role, on everything in view playing the role of Other as Seen values nearest first, even when nothing is. Its Subject is the nearest seen, so an Aimed command is about it and fails with none in view. Its words: SeesNobody of the observer, Closing of the other — the nearest seen on a collision course with the observer.

SightOutline and Renderer

The Renderer draws the views. Through a camera with Rays and in the default style it draws every observer's view on the GPU on [ViewTier], the ground out of sight veiled in a Shadow (Plugin.WithShadow). Otherwise an observer carrying SightOutline has its view's shape computed ([Band]s of hidden ground per angle in a world with heights) and drawn as a ring of [ConePoint]s in a ConeStyle (Plugin.WithStyle). Which views are drawn is the scene's, through world.Show rules (Plugin.WithViews); none, every one. The views drawn on the GPU read no scan: an observer nothing reads has its view drawn all the same.

The views start hidden: the settings screen (Cones) shows and hides them, Write.ShowCones, Write.HideCones and Read.ConesShown do the same from code. A look, not saved; the scan goes on either way.

Vocabulary

Moments, for a role's rules: Sighting (a pair grouped by observer, host.Met, its subject the nearest seen; its words SeesNobody, Closing of the other). Knobs, for an effect's Alter: Sight. Commands: Cones (a player's, a look). Styles, for a figure's Look on the world's atlas (Slot.Styled): Cone, the colour a kind's view is edged in, DefaultCone without; Plugin.WithStyle replaces every kind's with a composer of its own, Plugin.WithShadow the veil over the ground out of sight.

Index

Constants

View Source
const (
	// MaxSightRadius is the longest range the outline is sized for; a longer Radius still sees,
	// its outline is only coarser.
	MaxSightRadius = 600
	// MaxHalfAngleMilli is the widest half-angle the sizing assumes, in milliradians (pi/6).
	MaxHalfAngleMilli = 524
	// EdgeTolerance is how far a shadow edge may land from its true angle at full range.
	EdgeTolerance = 5

	MaxSamples = (arcMilli+gapMilli-1)/gapMilli + 1
)

Sizing of the outline buffer: the first three decide MaxSamples.

View Source
const MaxSeen = 8

MaxSeen caps how many entities one scan records, nearest first.

View Source
const MaxShadowsPerSample = 2

MaxShadowsPerSample caps the stretches of hidden ground kept per angle of an outline; the nearest are kept.

Variables

View Source
var DefaultCone = Cone{Color: color.RGBA{R: 255, G: 220, B: 90, A: 160}}

DefaultCone is a pale yellow edge.

View Source
var DefaultShadow = Shadow{Color: color.RGBA{R: 10, G: 10, B: 20, A: 110}, Fade: 6}

DefaultShadow is a dark veil fading in over a few units.

Functions

func Closing added in v0.3.0

func Closing() cond.Word[Sighting]

Closing is the word of the nearest one seen being on a collision course with the observer, of the other.

func SeesNobody added in v0.3.0

func SeesNobody() cond.Word[Sighting]

SeesNobody is the word of an observer with nobody in view.

Types

type Band added in v0.3.0

type Band struct{ From, To float32 }

Band is a stretch along one angle of a view, From to To away from the observer; zero is none.

type Cone added in v0.3.0

type Cone struct{ Color color.RGBA }

Cone is how the view of a kind of observer is drawn: its edge stroked in Color, a pixel wide, the inside clear. A figure declares it on the world's atlas (Slot.Styled); a kind without one is drawn in DefaultCone.

func (Cone) Compose added in v0.3.0

func (c Cone) Compose(f *render.Frame, outline []ConePoint)

Compose strokes the ring, each stretch where the nearer of its ends lies.

func (Cone) Style added in v0.3.0

func (Cone) Style()

Style: a figure declares it on the world's atlas (render.Style).

type ConePoint added in v0.3.0

type ConePoint struct {
	X, Y  float32
	At    render.At
	Scale float32
}

ConePoint is a point of a view's outline on screen, where the view lies there over the ground (At) and how many screen units a world unit spans there (camera.ScaleAt; 0 not in front of the eye).

type ConeStyle

type ConeStyle interface {
	Compose(f *render.Frame, outline []ConePoint)
}

ConeStyle composes one entity's view from its outline: a closed ring starting at the observer, out along one edge of the cone, round its boundary and back along the other edge. Each point lies where the view does over the ground under it.

type ConeStyleFn

type ConeStyleFn func(f *render.Frame, outline []ConePoint)

ConeStyleFn adapts a plain function to ConeStyle.

func (ConeStyleFn) Compose added in v0.3.0

func (fn ConeStyleFn) Compose(f *render.Frame, outline []ConePoint)

type Cones added in v0.3.0

type Cones struct{ Shown bool }

Cones shows every view drawn — the cones of sight and the ground out of sight — or hides them (Settings.Cones). A look at the world, like the camera's turn: at once, in the pause too. The settings screen alone.

func (Cones) Command added in v0.3.0

func (Cones) Command()

type Plugin

type Plugin struct {
	*world.Self

	Res Resources
	// contains filtered or unexported fields
}

Plugin wires vision into a Stage over the world's space. It publishes what entities can see; what to do about it is a rule's business.

func NewPlugin

func NewPlugin(worldPlugin *world.Plugin) *Plugin

NewPlugin builds the vision plugin over worldPlugin's space; the views drawn start hidden.

func (*Plugin) DefaultBindings added in v0.3.0

func (p *Plugin) DefaultBindings() []control.Binding

DefaultBindings are none: the cones are the settings screen's.

func (*Plugin) EventHandler

func (p *Plugin) EventHandler() control.EventHandler

EventHandler is nil: the Cones command comes from a player's bindings.

func (*Plugin) Install

func (p *Plugin) Install(ctx plugin.Installer) error

func (*Plugin) Name

func (p *Plugin) Name() string

func (*Plugin) Queues added in v0.3.0

func (p *Plugin) Queues() []command.CommandQueue

Queues are where Cones lands.

func (*Plugin) Read added in v0.4.0

func (p *Plugin) Read(int) *Read

Read reads vision's settings on worker w of the game's pool, 0 the game's own goroutine.

func (*Plugin) Restore added in v0.3.0

func (p *Plugin) Restore()

Restore takes the Settings loaded and shows the views as they say.

func (*Plugin) RunPlan

func (p *Plugin) RunPlan(ctx goke.RunCtx, d time.Duration)

RunPlan carries out the commands and runs the scan; call it after the world's RunPlan.

func (*Plugin) Serializable

func (p *Plugin) Serializable() plugin.Serializable

Serializable is vision's Settings, kept whole; its components go with the entities.

func (*Plugin) Settings added in v0.3.0

func (p *Plugin) Settings() []control.Setting

Settings is the one setting, the views drawn (plugin.Tunable).

func (*Plugin) WithBoard added in v0.3.0

func (p *Plugin) WithBoard(brd *board.Plugin) *Plugin

WithBoard has sight follow brd's ground and be held back by its cover, and the views drawn over the ground as the frame drawn holds it; call before Use.

func (*Plugin) WithCover added in v0.3.0

func (p *Plugin) WithCover(cover func() ground.Cover) *Plugin

WithCover has sight held back by the cover cover gives when a scan starts, on every goroutine the scan is shared among; nil, nothing.

func (*Plugin) WithFrequency added in v0.3.0

func (p *Plugin) WithFrequency(perSecond float64) *Plugin

WithFrequency has the observers scanned perSecond times a second of game time in place of every step (0), their rules going on with what each saw last between scans; call before Use.

func (*Plugin) WithGroundStep added in v0.3.0

func (p *Plugin) WithGroundStep(step float32) *Plugin

WithGroundStep sets how far apart a scan with heights samples a ground without a ground.Profile along a ray, in place of the board's cell; call before Use.

func (*Plugin) WithHeights added in v0.3.0

func (p *Plugin) WithHeights(heights func() ground.Heights) *Plugin

WithHeights has sight follow the ground heights gives when a scan starts; nil, flat.

func (*Plugin) WithLog added in v0.3.0

func (p *Plugin) WithLog(l *log.Logger) *Plugin

WithLog has vision write a line to l the first time one entity sees another; call before Use.

func (*Plugin) WithScanAll added in v0.6.0

func (p *Plugin) WithScanAll() *Plugin

WithScanAll has every observer scanned, whether anything reads its Sighted or not; call before Use.

func (*Plugin) WithShadow added in v0.3.0

func (p *Plugin) WithShadow(shadow Shadow) *Plugin

WithShadow sets how the ground out of sight is shaded, in place of DefaultShadow.

func (*Plugin) WithStyle

func (p *Plugin) WithStyle(style ConeStyle) *Plugin

WithStyle sets how every cone is drawn in place of the kinds' Cones: a composer of its own, on the CPU.

func (*Plugin) WithViews added in v0.3.0

func (p *Plugin) WithViews(rules ...world.Rule) *Plugin

WithViews has only the views of the observers a world.Show rule picks drawn; with none, every one is. Call it in the scene, before the first frame.

func (*Plugin) WithWorkers added in v0.3.0

func (p *Plugin) WithWorkers(n int) *Plugin

WithWorkers sets how many goroutines of the game's pool at most share a scan: 0 (as it starts) all, 1 none. Call before Use.

func (*Plugin) Write added in v0.4.0

func (p *Plugin) Write() *Write

Write changes vision's settings, on the game's goroutine.

type Read added in v0.4.0

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

Read reads vision's settings, the same on every goroutine.

func (*Read) ConesShown added in v0.4.0

func (r *Read) ConesShown() bool

ConesShown reports whether the views drawn are shown.

type Renderer

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

Renderer draws the views, lying over the ground under what stands on it: through a camera with Rays and in the default style every observer's on the GPU, the ground out of sight veiled in a Shadow; otherwise, as a render.Source, the outline of every entity carrying SightOutline in a ConeStyle. The observers are published once a frame.

func NewRenderer

func NewRenderer(space *aabbworld.SpaceRead) *Renderer

NewRenderer builds a Renderer drawing every kind in DefaultCone and DefaultShadow over space.

func (*Renderer) Compose added in v0.3.0

func (r *Renderer) Compose(f *render.Frame, cam camera.Camera)

Compose hands f every view in sight of cam, or notes them for Draw; nothing while hidden.

func (*Renderer) Draw

func (r *Renderer) Draw(t render.Target, cam camera.Camera, _ render.Uniforms)

Draw draws the views Compose noted on the GPU; nothing where it composed them.

func (*Renderer) Hidden added in v0.3.0

func (r *Renderer) Hidden() bool

Hidden reports whether the views are hidden.

func (*Renderer) Hide added in v0.3.0

func (r *Renderer) Hide(hidden bool)

Hide has the Renderer compose nothing until shown again.

func (*Renderer) Init

func (r *Renderer) Init(si *goke.SysInit)

func (*Renderer) Publish added in v0.5.0

func (r *Renderer) Publish()

Publish takes every observer some camera sees whose view is drawn, and the cover where any is, for the frame drawn next; none while hidden.

func (*Renderer) Style

func (r *Renderer) Style() ConeStyle

Style is how cones are drawn.

func (*Renderer) Swap added in v0.5.0

func (r *Renderer) Swap()

Swap has what was published last drawn.

func (*Renderer) WithCover added in v0.3.0

func (r *Renderer) WithCover(coverOf func() ground.Cover) *Renderer

WithCover has the views drawn on the GPU dimmed by the cover coverOf gives.

func (*Renderer) WithDrawing added in v0.3.0

func (r *Renderer) WithDrawing(rules *world.Rules) *Renderer

WithDrawing has only the views of the observers a world.Show rule picks drawn; with none, every one is.

func (*Renderer) WithGround added in v0.3.0

func (r *Renderer) WithGround(groundOf func() ground.Heights) *Renderer

WithGround has the views follow the ground groundOf gives.

func (*Renderer) WithGroundStep added in v0.3.0

func (r *Renderer) WithGroundStep(step float32) *Renderer

WithGroundStep drapes the views over the ground every step world units, in place of the ground's own step.

func (*Renderer) WithInView added in v0.5.0

func (r *Renderer) WithInView(inView func(uid.UID64) bool) *Renderer

WithInView has only the observers some camera sees published, as inView says.

func (*Renderer) WithScale added in v0.3.0

func (r *Renderer) WithScale(scale world.Scale) *Renderer

WithScale sinks the ground under an eye's level as far off as it lies, as scale's sight does.

func (*Renderer) WithSeen added in v0.3.0

func (r *Renderer) WithSeen(seen func(camera.Camera) *view.View) *Renderer

WithSeen has the views drawn only of the observers in what a camera sees, as seen says.

func (*Renderer) WithShadow added in v0.3.0

func (r *Renderer) WithShadow(shadow Shadow) *Renderer

WithShadow replaces how the ground out of sight is shaded.

func (*Renderer) WithStyle

func (r *Renderer) WithStyle(style ConeStyle) *Renderer

WithStyle replaces how each cone is drawn; the views are then composed from each entity's SightOutline in every world.

func (*Renderer) WithStyles added in v0.3.0

func (r *Renderer) WithStyles(styles func() *render.Styles) *Renderer

WithStyles has each kind drawn as the Cone the styles hold for it, the renderer's own without; styles may give nil.

type Resources added in v0.3.0

type Resources struct {
	Settings Settings
}

Resources is what vision publishes: its Settings.

type ScanSystem

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

ScanSystem fills in what every observer something reads sees, and the outline of those carrying SightOutline, then runs the rules of a Sighting over what each saw.

func (*ScanSystem) Every added in v0.3.0

func (s *ScanSystem) Every(perSecond float64)

Every has each observer scanned perSecond times a second, 0 every step. Call it before Init.

func (*ScanSystem) Init

func (s *ScanSystem) Init(si *goke.SysInit)

func (*ScanSystem) Paced added in v0.3.0

func (s *ScanSystem) Paced(pace goke.Opt) *ScanSystem

Paced has the system scan each block of observers as pace, an option of its query, says; set before Init.

func (*ScanSystem) Pool added in v0.4.0

func (s *ScanSystem) Pool(p *parallel.Pool)

Pool sets the goroutines a scan is shared among; nil, one. Call it before Init.

func (*ScanSystem) Update

func (s *ScanSystem) Update(cb *goke.CmdBuf, d time.Duration)

func (*ScanSystem) Workers added in v0.3.0

func (s *ScanSystem) Workers(n int)

Workers sets how many goroutines of the pool at most share a scan: 0 all, 1 none. Call it before Init.

type Seen

type Seen struct {
	ID   uid.UID64
	Base *world.Base
	Dist float32
}

Seen is one entity in an observer's view and how far off it is.

type Settings added in v0.3.0

type Settings struct {
	Cones bool
}

Settings is what vision lets the players adjust, saved with the game.

type Shadow added in v0.3.0

type Shadow struct {
	Color color.RGBA
	Fade  float32
}

Shadow is how the ground out of sight is shaded: a veil of Color over it, fading in over Fade world units wherever it meets ground in sight.

type Sight

type Sight struct {
	Facing geom.Vec // unit vector
	Radius float32  // world units
	// Blockers are the world.Layers whose entities and cover cut or dim this sight, every one for
	// zero; one on none of them is looked over and still seen.
	Blockers world.Layers
	// Ahead has the sight look the way the entity moves, Facing only while it has no heading.
	Ahead bool
}

Sight is where an entity looks and how far, a knob vision only reads and an effect may Alter. How wide it sees and from how high is its world.Eye, which the scan needs beside it.

func (Sight) Knob added in v0.3.0

func (Sight) Knob()

Knob: an effect may turn it (effect.Knob).

func (Sight) Looking added in v0.3.0

func (s Sight) Looking(dir geom.Vec) geom.Vec

Looking is where the sight looks for an entity heading dir: dir when Ahead and there is one, Facing otherwise.

type SightOutline

type SightOutline struct {
	Depths  [MaxSamples]float32
	Shadows [MaxSamples][MaxShadowsPerSample]Band
	Count   uint8
}

SightOutline is the shape of one entity's view: a reach per evenly spaced angle across the cone and, in a world with heights, the stretches of ground out of sight along each. Only an entity carrying it has its outline computed.

type Sighted

type Sighted struct {
	IDs   [MaxSeen]uid.UID64
	Dists [MaxSeen]float32
	Count uint8
}

Sighted is what an observer's last scan found, nearest first; vision gives one to an observer that has none. Count says how many of the arrays are in use.

type Sighting

type Sighting struct {
	Self  uid.UID64
	Base  *world.Base
	Sight *Sight
	Seen  []Seen
}

Sighting is one observer and everything in its view carrying the rule's second tag, nearest first, possibly none.

func (Sighting) Subject added in v0.3.0

func (s Sighting) Subject() (uid.UID64, bool)

Subject is the nearest one seen, whom an Aimed command is about; false while none is in view.

func (Sighting) Who added in v0.3.0

func (s Sighting) Who() uid.UID64

Who is the observer.

func (Sighting) Whom added in v0.3.0

func (s Sighting) Whom(each func(uid.UID64))

Whom tells each one the observer sees.

type Transparency added in v0.3.0

type Transparency struct{ Value float32 }

Transparency is how see-through an entity is to a Sight: 0 cuts sight as an entity without it does, 1 is as if absent, between them a ray through it spends 1/Value of its reach per unit.

type Watched added in v0.6.0

type Watched struct{}

Watched marks an observer whose Sighted the game reads itself, in a system, a plan or a fact: vision scans it though no rule of a Sighting is for it.

type Write added in v0.4.0

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

Write changes vision's settings, on the game's goroutine.

func (*Write) HideCones added in v0.4.0

func (w *Write) HideCones()

HideCones hides every view drawn; the scan goes on.

func (*Write) ShowCones added in v0.4.0

func (w *Write) ShowCones()

ShowCones shows every view drawn, as the Cones command does (Settings.Cones).

Jump to

Keyboard shortcuts

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