smart3d

package
v0.4.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 10 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 smart3d is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

Package smart3d provides a 3D spatial-hash-indexed smart-object registry on math3d.Vec3 and math3d.Quat, sharing ai.SmartClaim and hierarchical gameplay-tag queries with pkg/ai.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Object

type Object struct {
	ID       string
	Pos      math3d.Vec3
	Rotation math3d.Quat
	Tags     []string
	Enabled  bool
	Slots    []Slot
}

Object is a 3D world object that advertises interaction slots. A zero Rotation is treated as math3d.QuatIdentity().

type Query

type Query struct {
	Tags     []string
	TagQuery *tags.QueryDoc
	Filter   func(Object, Slot) bool
}

Query filters free slots on enabled 3D smart objects within a search sphere.

type Slot

type Slot struct {
	ID       string
	Offset   math3d.Vec3
	Facing   math3d.Vec3
	Tags     []string
	Behavior string
	Params   map[string]any
	Agent    string
	Lease    time.Duration
}

Slot is one 3D interaction point on an Object. Offset is relative to the owning Object's Pos in local space, and Facing is the local forward direction (a zero Facing defaults to (0, 0, -1), matching perception3d's forward convention).

type SmartObjects

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

SmartObjects is a deterministic 3D registry of smart objects and their slot claims, indexed on the XZ plane with spatial/index.Hash and verified against exact 3D Euclidean distance. It is safe for concurrent use, and callbacks may re-enter the registry without deadlocking.

Example
package main

import (
	"fmt"

	"github.com/nsgo-dev/game/pkg/ai/smart3d"
	"github.com/nsgo-dev/game/pkg/math3d"
)

func main() {
	reg := smart3d.New()
	reg.Register(smart3d.Object{
		ID:   "forge",
		Pos:  math3d.Vec3{X: 4, Y: 0, Z: 2},
		Tags: []string{"activity.work"},
		Slots: []smart3d.Slot{
			{ID: "anvil", Offset: math3d.Vec3{Z: 1}, Behavior: "smith"},
		},
	})
	found := reg.FindInRadius(math3d.Vec3{X: 3, Y: 0, Z: 2}, 5, smart3d.Query{Tags: []string{"activity.work"}})
	claim, ok := reg.Claim("npc1", found[0].ObjectID, found[0].SlotID)
	fmt.Println(ok, claim.ObjectID, claim.SlotID, claim.Agent)
}
Output:
true forge anvil npc1

func New

func New() *SmartObjects

New returns an empty 3D smart-object registry with the default XZ spatial-hash cell size (16).

func NewWithCellSize

func NewWithCellSize(cellSize float64) *SmartObjects

NewWithCellSize returns an empty 3D smart-object registry with XZ grid cells of cellSize units. Non-positive or non-finite cell sizes fall back to 16.

func (*SmartObjects) Claim

func (s *SmartObjects) Claim(agent, obj, slot string) (ai.SmartClaim, bool)

Claim gives slot on object obj to agent without a lease timeout.

func (*SmartObjects) ClaimWithTimeout

func (s *SmartObjects) ClaimWithTimeout(agent, obj, slot string, ttl time.Duration) (ai.SmartClaim, bool)

ClaimWithTimeout gives slot on object obj to agent with an optional lease duration ttl (>= 0).

func (*SmartObjects) ClaimedBy

func (s *SmartObjects) ClaimedBy(agent string) []ai.SmartClaim

ClaimedBy returns all active claims currently held by agent, ordered by ObjectID then SlotID.

func (*SmartObjects) Disable

func (s *SmartObjects) Disable(id string)

Disable hides an object from queries and claims and releases every claim on it in slot order.

func (*SmartObjects) Enable

func (s *SmartObjects) Enable(id string)

Enable makes a disabled object visible to queries and claims again with all slots free.

func (*SmartObjects) FindInRadius

func (s *SmartObjects) FindInRadius(pos math3d.Vec3, r float32, q Query) []ai.SmartClaim

FindInRadius returns free slots matching q on enabled objects within sphere radius r of pos, ordered deterministically by ObjectID then SlotID.

func (*SmartObjects) Object

func (s *SmartObjects) Object(id string) (Object, bool)

Object returns a deep copy of the registered object named id.

func (*SmartObjects) Register

func (s *SmartObjects) Register(o Object)

Register adds or replaces an object, enabled. Objects with an empty ID or non-finite Pos or Rotation are ignored; slots with empty/duplicate IDs or non-finite Offset or Facing are dropped. Replacing an existing object releases any active claims on the previous version via the release callback.

func (*SmartObjects) Release

func (s *SmartObjects) Release(c ai.SmartClaim) bool

Release frees the slot c holds and runs the release callback.

func (*SmartObjects) ReleaseAll

func (s *SmartObjects) ReleaseAll(agent string) int

ReleaseAll frees every slot held by agent in deterministic object-ID then slot-ID order.

func (*SmartObjects) Restore

func (s *SmartObjects) Restore(snap Snapshot)

Restore replaces the registry with a deep copy of snap without invoking the release callback.

func (*SmartObjects) SetOnReleased

func (s *SmartObjects) SetOnReleased(fn func(ai.SmartClaim))

SetOnReleased sets the callback run after a claim is released, whether by Release, ReleaseAll, Disable, Unregister, Register replacing an object, or lease expiration in Step.

func (*SmartObjects) SetPose

func (s *SmartObjects) SetPose(id string, pos math3d.Vec3, rot math3d.Quat) bool

SetPose updates a registered object's world position and rotation and refreshes its spatial index entry.

func (*SmartObjects) Slot

func (s *SmartObjects) Slot(objID, slotID string) (Slot, bool)

Slot returns a deep copy of slotID on objID.

func (*SmartObjects) SlotWorldPose

func (s *SmartObjects) SlotWorldPose(objID, slotID string) (pos, facing math3d.Vec3, ok bool)

SlotWorldPose returns the world-space position and unit facing direction of slotID on objID.

func (*SmartObjects) Snapshot

func (s *SmartObjects) Snapshot() Snapshot

Snapshot returns a deep copy of the registry, objects sorted by ID.

func (*SmartObjects) Step

func (s *SmartObjects) Step(dt time.Duration)

Step advances timed slot leases by dt, releasing expired claims in deterministic order.

func (*SmartObjects) Unregister

func (s *SmartObjects) Unregister(id string)

Unregister removes an object and releases every active claim on it in slot order via the release callback.

func (*SmartObjects) Use

func (s *SmartObjects) Use(c ai.SmartClaim) bool

Use reports whether c is still held on an enabled object.

type Snapshot

type Snapshot struct {
	Objects []Object
}

Snapshot is the full 3D registry state, objects sorted by ID and slots by ID, for saves and rollback.

Jump to

Keyboard shortcuts

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