States
Factories for the states every enemy ends up writing: States.Patrol, States.Investigate,
States.Search, States.Wander and States.Wait. Exposed as VluxyAI.States.
local States = VluxyAI.States
local When = VluxyAI.When
local Brain: VluxyAI.Brain = {
Initial = "Patrol",
Interrupts = {
{ To = "Hunt", When = When.Has("SeenTarget"), Except = { "Hunt", "Attack" } },
{ To = "Investigate", When = When.New("HeardAt"), Except = { "Hunt", "Attack" } },
},
States = {
Patrol = States.Patrol({ Points = "Nodes", Speed = 10 }),
Investigate = States.Investigate({ Target = "HeardPosition", Speed = 14, Then = "Patrol" }),
Hunt = { ... },
Search = States.Search({ Around = "SeenPosition", Radius = 12, Points = 3, Duration = 14, Then = "Patrol" }),
Attack = VluxyAI.Combat.Attack.State({ ... }),
},
}
States.Roam walks the best point of an Interest map, marking each visited, so an enemy that has lost you checks the places you would plausibly be.
Every factory accepts Transitions (checked before the state's own logic, like any state),
Enter and Exit hooks run after the built-in ones, Speed to set on entry, and Key, the
agent.Data key its scratch lives under (default: the state's own name). Positions may be
given as a blackboard or Data key, a Vector3, or a function of the agent. A point the
navigator cannot reach is skipped on the next tick, so a search point inside a wall costs
nothing but a failed plan.
Types
RoamOptions
interface RoamOptions {Interest: Interest | string--
The map of points to roam, or the agent.Data key it is under, so a brain written once can roam a map each agent is given (SetData({ Interest = map })). Required.
Range: number?--
Studs beyond which a point scores nothing for distance. Default 120.
Tags: {string}?--
Only points with one of these tags.
Linger: number | {number}?--
Seconds to stand at each point before moving on. Default 1.
Duration: number | {number}?--
Seconds to roam before Then. Without it, roams until a transition or until no point scores.
Then: string?--
The state to enter when time is up or no point is worth a look.
Stray: number?--
Chance, 0 to 1, that a pick ignores the clues and goes to a random point it has not looked at lately (still nearer ones more often), so a searcher is not perfectly predictable. Default 0.
Speed: number?--
Set on entry.
Key: string?--
Default "Roam".
}Functions
RandomPoint
States.RandomPoint() → Vector3--
A point on the horizontal disc of radius around centre, uniformly distributed, at the centre's height.
Patrol
Types
interface PatrolOptions {Points: string | {Vector3} | (agent: Agent) → {Vector3}--
The route: a Data key holding a list, the list itself, or a function. Required.
Order: "Loop" | "PingPong" | "Random"?--
Loop back to the first point, walk the list both ways, or pick any other point. Default "Loop".
Pause: number | {number}?--
Seconds to stand at each point, or a { min, max } range. Default none.
Speed: number?--
Set on entry.
Key: string?--
The Data key for the current index. Default "Patrol".
}Walks the points in order, forever. The index survives leaving and re-entering the state, so an enemy that broke off to investigate resumes where it was. A point that cannot be reached is skipped.
Investigate
Types
interface InvestigateOptions {Target: string | Vector3 | (agent: Agent) → Vector3?--
Where to go: a blackboard or Data key such as "HeardPosition", a position, or a function. Read once on entry. Required.
Then: string?--
The state to enter when there, when the target is missing, or when the path fails. Without it the state waits for a transition.
Linger: number | {number}?--
Seconds to stand at the spot before Then. Default 0.
Acknowledge: string?--
A blackboard key to Agent:Acknowledge on entry, such as "HeardAt", so the noise that brought the agent here does not count as new again.
Speed: number?--
Set on entry.
Key: string?--
Default "Investigate".
}
Goes to one place and, once there (or once the path fails), lingers and moves on to Then.
The place is read on entry, so a newer noise re-enters the state through a transition rather
than pulling the agent mid-walk.
Search
Types
interface SearchOptions {Around: string | Vector3 | (agent: Agent) → Vector3?--
The centre: usually "SeenPosition" or "LastKnownPosition". Missing means the agent's own position. Read once on entry.
Radius: number?--
Studs around the centre the points are picked in. Default 12.
Points: number?--
How many points to pick. They are walked in turn and cycled until Duration runs out. Default 3.
Duration: number | {number}?--
Seconds to search before Then. Default 15.
Then: string?--
The state to enter when time is up. Without it the state searches until a transition.
Speed: number?--
Set on entry.
Key: string?--
Default "Search".