softtarget

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: 2 Imported by: 0

Documentation

Overview

Package softtarget is the controller-aim tie-breaker for action games: given a set of swept-volume hit candidates, pick one as the canonical target using a caller-composed scoring function.

The kit doesn't gather candidates — that's the caller's spatial query. It only ranks them, with a `Pick` that's allocation-free after warmup. Use the same Resolver client-side (for the preview reticle) and server-side (for hit adjudication) to keep the two in lock-step.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Candidate

type Candidate[T any] struct {
	Item       T
	Position   geometry.Vec3
	AngularErr float32 // radians off the aim axis
	Distance   float32 // distance from the player
}

Candidate is one hit candidate the spatial query produced.

AngularErr and Distance are pre-computed against the player's aim axis so Pick doesn't recompute geometry per candidate.

type Config

type Config[T any] struct {
	Score Score[T]
	// MaxAngularError rejects candidates outside this cone half-angle
	// (radians) before scoring. Set ≤ 0 to disable.
	MaxAngularError float32
	// MaxDistance rejects candidates further than this. Set ≤ 0 to
	// disable.
	MaxDistance float32
}

Config configures a Resolver.

type Resolver

type Resolver[T any] struct {
	// contains filtered or unexported fields
}

Resolver picks one canonical target from a candidate list.

func New

func New[T any](cfg Config[T]) *Resolver[T]

New constructs a Resolver. A nil Score returns the highest-score candidate of zero — only useful when MaxAngularError + MaxDistance alone decide selection. Practical callers should always provide Score.

func (*Resolver[T]) Pick

func (r *Resolver[T]) Pick(cands []Candidate[T]) (Candidate[T], bool)

Pick returns the highest-scoring candidate (after the cone + distance gates) and whether one was found. Allocation-free.

type Score

type Score[T any] func(c Candidate[T]) float32

Score returns a per-candidate scalar where higher wins. Compose caller-game-specific terms (HP, threat, debuff bias) on top of the built-ins via Compose.

func Compose

func Compose[T any](scores ...Score[T]) Score[T]

Compose combines multiple Scores into one (sum).

func ScoreAngular

func ScoreAngular[T any](weight float32) Score[T]

ScoreAngular returns a Score that rewards small angular error. `weight` scales the contribution; `+1` at perfect aim, decaying to 0 as AngularErr → π.

func ScoreDistance

func ScoreDistance[T any](weight, falloff float32) Score[T]

ScoreDistance returns a Score that rewards close targets. `falloff` is the distance at which the contribution halves. `weight` scales the contribution.

Jump to

Keyboard shortcuts

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