Class AIAimSolver
- Namespace
- FishMMO.Server.Implementation.World.SceneServer.AI
- Assembly
- FishMMO.Server.dll
The pure arithmetic behind an NPC's aim: which point to aim at, how accurately, and with how much error.
public static class AIAimSolver
- Inheritance
-
AIAimSolver
- Inherited Members
Remarks
Static and free of scene, tick and controller state, so the whole of an aim profile's
behaviour is assertable in an EditMode test — the same split
AICombatDecision and AIKiteBudget use. Every value here is a function of its
arguments; nothing is remembered between calls, and the callers own all of the state.
Why the aim point is resolved per call rather than cached. The previous implementation cached the target's collider half-height against the target's transform and only re-resolved it when the target identity changed. A collider that was not yet resolvable on the first sight of a target — which is normal, since the model is instantiated asynchronously — cached a height of zero and aimed at the feet for the entire fight. Reading the bounds on each call cannot latch: there is no remembered value to go stale.
Fields
MinimumAimDistanceSqr
Squared distance below which an aim direction is treated as degenerate.
public const float MinimumAimDistanceSqr = 0.0001
Field Value
Remarks
A direction this short has no meaningful orientation, so the caller keeps its previous rotation instead of writing garbage. The old code normalised first and then tested the squared length, which — a normalised vector being unit length by definition — could only ever fail for an exactly-zero input. The test is honest about what it is for now.
Methods
ResolveAccuracy(float, float, float)
Returns the accuracy an NPC has reached after tracking a target for a while.
public static float ResolveAccuracy(float lockedSeconds, float lockSeconds, float baseAccuracy)
Parameters
lockedSecondsfloatSeconds the current target has been continuously held.
lockSecondsfloatSeconds of tracking required to reach full accuracy.
baseAccuracyfloatThe 0-1 ceiling the ramp climbs to.
Returns
- float
Accuracy in 0-1, where 1 is perfectly on target.
Remarks
The curve is a smoothstep rather than a straight line for two reasons. It eases in, so a just-acquired target is meaningfully hard to hit for a beat instead of being at three-quarters accuracy on the first tick. And it eases out, so the last part of the ramp does not visibly snap from imperfect to perfect — which is the moment a player would notice that the misses were scripted.
A lockSeconds of zero or less means no ramp at all: the NPC is at its
authored ceiling immediately. That is the profile that reproduces the old perfect-aim
behaviour and is what a profile with nothing but a spread value degenerates to.
ResolveAimPoint(Vector3, Collider, AIAimPoint)
Returns the world-space point an NPC should aim at on a target.
public static Vector3 ResolveAimPoint(Vector3 targetPosition, Collider targetCollider, AIAimPoint aimPoint)
Parameters
targetPositionVector3The target's root position, at its feet. Used when there is no collider.
targetColliderColliderThe target's collider, or null.
aimPointAIAimPointWhich point on the body to aim at.
Returns
- Vector3
The aim point in world space.
Remarks
Why a collider and not a half-height. The half-height form asked the caller to have already decided where the body's centre was, which is the decision this makes. Taking the collider whole also means a target whose collider changes shape — a crouch, a form change — is aimed at correctly on the next tick for free.
ResolveLeadOffset(Vector3, float)
Returns the world-space offset that leads a moving target.
public static Vector3 ResolveLeadOffset(Vector3 targetVelocity, float leadSeconds)
Parameters
targetVelocityVector3The target's velocity in units per second.
leadSecondsfloatSeconds of flight time to aim ahead by.
Returns
- Vector3
The offset to add to the aim point.
ResolveLeadTime(float, float, float)
Returns how long a shot should be led by, from the target's distance and the projectile's speed.
public static float ResolveLeadTime(float distance, float projectileSpeed, float leadAccuracy)
Parameters
distancefloatDistance from the aim origin to the aim point.
projectileSpeedfloatThe ability's travel speed, or 0 for an instant ability.
leadAccuracyfloat0-1 authoring dial. 0 leads not at all, 1 leads perfectly.
Returns
- float
Seconds of flight time to aim ahead by, or 0.
Remarks
Derived from the ability, not authored per NPC. An earlier design put an absolute lead in seconds on the aim profile, which cannot be right for two abilities of different speeds held by the same NPC: the same lead over-leads the fast one and under-leads the slow one. The flight time is a property of the shot, so it is computed from the shot.
Why projectileSpeed is guarded. An ability with no travel speed
— a melee swing, an instant hit, a self-buff — has no flight time to lead, and dividing
by its zero speed would produce an infinite lead. The guard is what makes this safe to
call for every ability in a spellbook without classifying them first.
ResolveSpread(float, float)
Returns the angular spread a given accuracy produces.
public static float ResolveSpread(float spreadDegrees, float accuracy)
Parameters
spreadDegreesfloatThe profile's worst-case spread, at zero accuracy.
accuracyfloatAccuracy in 0-1 from ResolveAccuracy(float, float, float).
Returns
- float
Spread in degrees, from
spreadDegreesdown to 0.
RollScatter(float, float, float)
Builds the angular error applied to a cast.
public static Quaternion RollScatter(float spreadDegrees, float yawRoll, float pitchRoll)
Parameters
spreadDegreesfloatWorst-case angular error in degrees.
yawRollfloatFirst roll in -1..1. Values outside are clamped.
pitchRollfloatSecond roll in -1..1. Values outside are clamped.
Returns
- Quaternion
A rotation to apply on top of the solved aim direction.
Remarks
Why the error is a rotation and not noise. This is rolled once per cast and held for the whole cast, so an inaccurate NPC tracks its target continuously and misses by a fixed angle. Re-rolling per tick would average the error out over a fast projectile's flight and produce a shower of near-misses that reads as broken; a held angle reads as an archer who is aiming at you and is not very good. It is also what makes the error visible at all — a per-tick wobble of a degree or two is invisible at range, where a held degree is metres.
Why the rolls are taken as a unit disc. The two values arrive independently from
the NPC's seeded RNG, and independent uniform pairs cover a square, whose corners
reach sqrt(2) times further than its edges — so a "2 degree" spread would occasionally
miss by nearly 3. Folding the pair into a disc before scaling makes
spreadDegrees the true worst case in every direction.
TryComposeAim(Vector3, Vector3, Quaternion, out Vector3)
Solves the unit direction to fire along.
public static bool TryComposeAim(Vector3 origin, Vector3 aimPoint, Quaternion scatter, out Vector3 direction)
Parameters
originVector3The aim origin, from
AIController.AimOrigin.aimPointVector3The world-space point to hit.
scatterQuaternionThe held per-cast error rotation, or UnityEngine.Quaternion.identity.
directionVector3Receives the unit aim direction when solvable, else UnityEngine.Vector3.zero.
Returns
- bool
True when a direction was solved. False means the caller should keep its last good rotation.
Remarks
UnityEngine.Quaternion.LookRotation(UnityEngine.Vector3) normalises internally, so the direction is deliberately not pre-normalised: the length is what the degeneracy test needs, and normalising first would discard it.