API 레퍼런스는 소스 코드의 Go 문서 주석에서 추출되며 영어로 작성되어 있습니다.
package perception3d
import "github.com/nsgo-dev/game/pkg/ai/perception3d"개요
Package perception3d is ai's perception for agents in a 3D world: sight cones around a forward vector, hearing and per-stimulus memory on math3d.Vec3, with line of sight through physics3d raycasts or a game's own check.
It shares ai's engine, so its sight, hearing, memory, events, teams, line-of-sight budget and determinism behave exactly as ai.PerceptionSystem's do, and it uses ai's StimulusID, Team, SightConfig, HearingConfig and event types.
색인
- type Event
- type LineOfSight
- type Memory
- type Noise
- type Perception
- func (p *Perception) AppendMemories(dst []Memory) []Memory
- func (p *Perception) Clear()
- func (p *Perception) Events() []Event
- func (p *Perception) Forget(id ai.StimulusID) bool
- func (p *Perception) Memories() []Memory
- func (p *Perception) Memory(id ai.StimulusID) (Memory, bool)
- func (p *Perception) Sees(id ai.StimulusID) bool
- type System
- type Target
타입
type Event struct {
// Kind is what happened.
Kind ai.EventKind
// Memory is the memory as the event left it; for ai.EventForgotten it is the memory that was dropped.
Memory Memory
}Event reports a change to one memory during the last step.
type LineOfSight func(from, to math3d.Vec3) boolLineOfSight reports whether nothing blocks the segment from an agent's eye at from to a target at to. It must be deterministic for perception to be.
func PhysicsLineOfSight(w *physics3d.World, layers physics3d.LayerMask) LineOfSightPhysicsLineOfSight returns a LineOfSight that is blocked by the first hit of w.RaycastFirstLayer with layers; zero selects every layer.
A ray starting inside a body hits it, and a ray to a target hits the target's own body, so keep agents' and targets' bodies off the layers that block sight.
type Memory struct {
// ID is the stimulus's ID.
ID ai.StimulusID
// Team is the stimulus's team when it was last sensed.
Team ai.Team
// Attitude is how the agent regarded the stimulus when it was last sensed.
Attitude ai.Attitude
// Sense is the sense that last refreshed the memory.
Sense ai.Sense
// Pos is the last known position.
Pos math3d.Vec3
// Vel is the last known velocity: the target's velocity when seen, zero when heard.
Vel math3d.Vec3
// Age is the time since the stimulus was last sensed; zero while it is in sight.
Age time.Duration
// Strength is 1 when seen, and hearing raises it to at least 1 - distance/reach (1 for an infinite reach); it falls linearly to 0 over ForgetAfter.
Strength float32
// Visible reports whether the stimulus is in sight now.
Visible bool
}Memory is what an agent remembers about one stimulus; its fields mean what ai.Memory's do.
type Noise struct {
// Source identifies what made the noise; the memory of it is kept under this ID.
Source ai.StimulusID
// Team is the source's team.
Team ai.Team
// Pos is where the noise was made.
Pos math3d.Vec3
// Loudness scales each listener's hearing range; zero or less is inaudible.
Loudness float32
}Noise is a sound posted to a System and heard on its next Step.
type Perception struct {
// Self is the agent's own StimulusID, so the agent never senses itself; zero means the agent is not a stimulus, so an agent that is one needs a non-zero ID.
Self ai.StimulusID
// Team is the agent's team.
Team ai.Team
// Pos is the agent's position.
Pos math3d.Vec3
// Forward is the direction the sight cone points along; its length does not matter, and zero faces -Z.
Forward math3d.Vec3
// EyeOffset is added to Pos, in world space, to give the point both senses measure from and sight looks from, such as head height on +Y.
EyeOffset math3d.Vec3
// Sight is the agent's vision; its angles are measured in 3D around Forward.
Sight ai.SightConfig
// Hearing is the agent's hearing.
Hearing ai.HearingConfig
// ForgetAfter is how long a stimulus is remembered after it was last sensed; zero or less forgets it on the step after.
ForgetAfter time.Duration
// contains filtered or unexported fields
}Perception is one 3D agent's senses and memory. Set its exported fields, add it to a System, and step the system on game.Loop's fixed steps; the system reads the fields at the start of every Step.
func (p *Perception) AppendMemories(dst []Memory) []MemoryAppendMemories appends every memory, sorted by ID, to dst and returns the extended slice.
func (p *Perception) Clear()Clear drops every memory and the last step's events.
func (p *Perception) Events() []EventEvents returns the events of the last Step; the slice is reused by the next Step.
func (p *Perception) Forget(id ai.StimulusID) boolForget drops the memory of id without an event and reports whether there was one.
func (p *Perception) Memories() []MemoryMemories returns a new slice of every memory, sorted by ID.
func (p *Perception) Memory(id ai.StimulusID) (Memory, bool)Memory returns the memory of the stimulus id, if any.
func (p *Perception) Sees(id ai.StimulusID) boolSees reports whether the stimulus id is in sight now.
type System struct {
// LineOfSight decides whether a target in view is seen; nil means nothing blocks sight.
LineOfSight LineOfSight
// Attitude decides how agents regard targets and noises; nil means ai.DefaultAttitude.
Attitude ai.AttitudeFunc
// Budget caps the LineOfSight calls of one step; zero or less checks every pair in view, and a nil LineOfSight makes no calls, so the budget does not apply and every pair in view is seen at once. When the cap is reached, the remaining pairs are checked first on the next step, round-robin, and keep their previous result until then.
Budget int
// contains filtered or unexported fields
}System steps the Perceptions of many 3D agents together, so their line-of-sight checks share one per-step Budget, as ai.PerceptionSystem does in 2D. The zero System is ready to use, with every line of sight clear.
func (s *System) Add(p *Perception)Add adds p to the system, after the agents already in it. Adding p again does nothing; adding a Perception that belongs to another system panics. A copy of a Perception that was added, such as a configured prototype, is a separate agent: Add gives it its own copy of the memories and no events.
func (s *System) PostNoise(n Noise)PostNoise queues n to be heard on the next Step. Noises are heard in the order they were posted.
func (s *System) Remove(p *Perception) boolRemove removes p from the system and reports whether it was there. p keeps its memories, but none of them is visible any more and Events is empty, so Sees reports false until p is added and stepped again.
func (s *System) Stats() ai.PerceptionStatsStats returns the line-of-sight work of the last Step.
func (s *System) Step(dt time.Duration, targets []Target)Step advances every agent by dt: memories age, targets in view are checked for line of sight within the budget, posted noises are heard, and memories not sensed for ForgetAfter are forgotten. targets is read only during the call.
type Target struct {
// ID identifies the target; a later target with the same ID in one step is ignored.
ID ai.StimulusID
// Team is the target's team.
Team ai.Team
// Pos is the point agents look at.
Pos math3d.Vec3
// Vel is the target's velocity, remembered while it is seen.
Vel math3d.Vec3
}Target is a stimulus that agents can see during one step.