EntityAI API
EntityAI controls high-level AI behavior for an Entity.
Reference
setActive(active: Boolean)Enables (true) or disables (false) AI control for this entity.isActive()Returnstruewhen AI is currently active.moveToSector(sector: LuaVec3i)Sets the AI’s sector navigation target.getTargetSector()Returns the current sector target asLuaVec3i. Returns the entity’s current sector when no target is set.setTarget(entity: RemoteEntity)Sets the combat/aim target. The target must be in the same or an adjacent sector.getTarget()Returns the current target asRemoteEntity, ornilwhen no target is set.getTargetType()Returns the exact current AI target preference name as a string.getAvailableTargetTypes()Returns the currently available target preference names as aString[].setTargetType(type: String)Changes the AI target preference totype.
Common built-in values for type are Any, Selected Target, Ships, Stations, Missiles, Astronauts, and Asteroids.
setTargetType(type) always expects a String. The running game may also provide additional custom target type names through CustomAITargetUtil.
Invalid target type names now raise a clear error that includes the currently available values.
Target type names
These built-in target type names are present in the current StarMade build used by Logiscript:
Target type |
Expected arg type |
Notes |
|---|---|---|
|
|
Default catch-all target preference |
|
|
Use the currently selected target |
|
|
Prefer ship targets |
|
|
Prefer station targets |
|
|
Prefer missile targets |
|
|
Prefer astronaut targets |
|
|
Prefer asteroid targets |
StarMade also appends any custom AI target program names registered through CustomAITargetUtil, so getTargetType() may return additional names and setTargetType(type) can accept them when they are present in the running build.
moveToPos(pos: LuaVec3i)Commands the AI to move toward world block positionpos.moveToEntity(entity: RemoteEntity)Commands the AI to move towardentity. The target must be in the same sector.navigateToPos(pos: LuaVec3i, stopRadius: Number)Moves towardposand automatically stops when the entity is withinstopRadiusblocks.hasReachedPos(pos: LuaVec3i, radius: Number)Returnstrueif the entity is currently withinradiusblocks ofpos.stop()/stopNavigation()Stops current AI movement.
Heading and orientation
getHeading()Returns the ship’s normalized forward direction as aLuaVec3f.isAlignedWith(direction: LuaVec3f, threshold: Number)Returnstrueif the ship’s heading dot-product withdirectionis >=threshold(range 0.0–1.0). Use0.9for roughly aligned,0.99for very tight.isFacingTowards(entity: RemoteEntity, threshold: Number)Returnstrueif the ship is facing towardentitywithin the given dot-product threshold.faceDirection(direction: LuaVec3f)Commands the AI to turn towarddirection. Also causes the ship to begin moving in that direction — callstopNavigation()once aligned.faceTowards(entity: RemoteEntity)Commands the AI to face towardentity. Computes direction from current position to target automatically.
Roll control
getUp()Returns the ship’s normalized up vector as aLuaVec3f(basis column 1 of the world transform).getRoll()Returns the ship’s current roll angle in radians relative to galactic up (world Y axis). Positive values indicate counter-clockwise roll when viewed from behind; negative values indicate clockwise roll. Returns0when the ship is pointing straight up or down (roll is undefined in that orientation).isRollAligned(threshold: Number)Returnstrueif the ship’s up vector Y-component is >=threshold. Equivalent to checkingdot(shipUp, galacticUp) >= threshold. Use0.99for tight alignment,0.9for loose.faceWithRoll(forward: LuaVec3f, up: LuaVec3f)Commands the AI to orient the ship so that it facesforwardwithupas the desired up vector, giving full roll control. Also applies thrust in theforwarddirection (equivalent tofaceDirectionplus roll).updoes not need to be perfectly perpendicular toforward— it is Gram-Schmidt orthogonalized internally.alignRollToGalacticUp()Corrects the ship’s roll so its up vector aligns with galactic up(0, 1, 0)while preserving the current heading. Does not apply thrust — usefaceDirectionormoveToPosfor movement.
Typical alignment workflow
local station = ...
ai:faceTowards(station)
while not ai:isFacingTowards(station, 0.95) do sleep(0.5) end
ai:stopNavigation()
Typical roll alignment workflow
-- Correct roll while holding heading
while math.abs(ai:getRoll()) > 0.05 do
ai:alignRollToGalacticUp()
sleep(0.1)
end
-- Fly toward a point level with galactic up
local heading = ai:getHeading()
ai:faceWithRoll(heading, vec3f(0, 1, 0))