ai

The AI toolkit: brains, Ice Age's AI helpers for any creature, species brains.

Creature AI for scripts, in three levels (the guide Creature behaviour has complete examples):

  1. Brains. c.brain runs something else in place of a creature's AI: another species' AI (when the animations line up: ai.compatible), one of the Ice Age-style brains "prey", "predator", "defender" (for any species of both games), or your own function(c, dt). ai.setSpeciesBrain does it for a whole species.
  2. The game's creature functions on the creature: c:walkTo, c:runTo, c:idle, c:sees, c:attackHunter, c:attack, c:jump, c:roar ... (world: Creature methods).
  3. Ice Age's AI helpers (AIGen_*, the building blocks of every Ice Age animal), here for every creature of both games. They work on one creature's AI frame: ai.begin(c) first, then the helpers in the order an Ice Age AI calls them (animate, pick the animation, phaseChanging, rotate, thinkSpeed). Speeds are the game's own: units per millisecond, times the creature's scale (the Wolf walks at 0.32 and runs at 2.048).

ai.anims(c) gives a species' walk, run, idle, attack (kill), jump and roar animations and its idles, as in the species tables on the world page.

Functions

# ai.brains() → table

since API 3

What c.brain takes besides a function.

Returns
tablethe brain names: "prey", "predator", "defender" and every species id of this game

# ai.compatible(c, brain) → boolean, string?

since API 3

Whether the creature can run that species' AI: its model needs at least as many animations (the species is loaded when the hunt did not), and flyers and walkers do not mix. The generic brains fit every creature.

ParameterType
cCreature
brainSpecies|stringa species or a brain name
Returns
boolean
string?why not

# ai.setBrain(c, brain, force?)

since API 3

c.brain = brain, with a way past the compatibility check.

ParameterType
cCreature
brainanyas c.brain
force optionalbooleanskip the animation check (at your own risk)

# ai.setSpeciesBrain(species, brain, force?)

since API 3

Every creature of the species runs this brain (a creature's own c.brain comes first).

ParameterType
speciesSpecies
brainanyas c.brain; nil = the species' own again
force optionalbooleanskip the animation check
-- T-Rexes graze and flee like deer
ai.setSpeciesBrain("trex", "prey")

# ai.run(c, brain)

hunt only since API 3

One frame of a brain, from inside your own: mix behaviours.

ParameterType
cCreature
brainSpecies|stringa species, "prey", "predator" or "defender"

# ai.anims(c) → table?

since API 3

A species' animations and speeds as its AI uses them.

ParameterType
cCreature|Species
Returns
table?{walk, run, idle, roar, kill, jump (missing = none), walkSpeed, runSpeed (units per second, x scale), idles (a list), predator, attacks, flyer}

# ai.setIdles(species, anims)

since API 3

The calm idles c:idle plays for this species (and ai.anims(...).idles, the possession's standing) instead of its AI's list. Species whose AI never stands (Dinosaur Hunter's raptors, the Allosaurus) have none; their models often have spare animations. A content pack's species uses its AI species' list unless it has its own. Undone when the scripts unload (listed in game.changes()).

ParameterType
speciesSpecies
animstable|nilanimation numbers; nil = its AI's own list again
-- the Velociraptor stands with its spare animations 2, 3 and 7 instead of holding a step
ai.setIdles("velociraptor", {2, 3, 7})

# ai.state(c) → string, integer, number

since API 3

Its AI's raw state (c.mood is the readable one).

ParameterType
cCreature
Returns
string"calm", "alarmed", "noticed" or "looking"
integerthe raw state
numberfear seconds

# ai.begin(c, front?) → number, number

hunt only since API 3

Starts an AI frame: remembers its animation and time (for phaseChanging) and the distances the helpers read. Call it once per frame before the other helpers.

ParameterType
cCreature
front optionalnumberwhere the hunter is measured from, ahead of its feet
Returns
numberdistance to its walk target
numberdistance to the hunter

# ai.animate(c) → boolean

hunt only since API 3

The blend and the clock of its current animation (the AIs' loop flag).

ParameterType
cCreature
Returns
booleanthe animation looped (time to pick the next one)

# ai.phaseChanging(c, phase?, blend?, soundChance?)

hunt only since API 3

AIGen_PhaseChanging: switches to the animation with the game's blend and plays its sound.

ParameterType
cCreature
phase optionalintegerthe animation to play from now
blend optionalbooleancarry the step over (walk <-> run)
soundChance optionalnumber-1 = its sound every time

# ai.rotate(c, rotate?, slowPhase?, minSpeed?, k?, afraidK?, mode?) → number

hunt only since API 3

AIGen_RotationToTargetAlpha: turns it toward its target heading (selectWay sets it).

ParameterType
cCreature
rotate optionalbooleandefault true
slowPhase optionalintegerit turns slower in this animation
minSpeed optionalnumberdefault 0.4
k optionalnumberdefault 1.5
afraidK optionalnumberdefault 1.5
mode optionalinteger0-3: how quickly it gets into the turn
Returns
numberdegrees still to turn

# ai.thinkSpeed(c, speed, phase?, gammaK?, gammaK2?, glim?)

hunt only since API 3

AIGen_ThinkSpeed: speeds up or slows down to speed, moves it along its heading with the terrain collision and tilts it to the ground.

ParameterType
cCreature
speednumberunits per ms (x scale): the Wolf walks 0.32, runs 2.048
phase optionalintegerits walk animation (it leans in turns)
gammaK optionalnumber
gammaK2 optionalnumber
glim optionalnumber

# ai.selectWay(c, playerR?, wobble?, wobbleFreq?, wobbleAmp?, water?, maxTries?, waitBase?, waitRnd?)

hunt only since API 3

AIGen_SelectWay: its heading toward its walk target, around obstacles and water.

ParameterType
cCreature
playerR optionalnumberdefault 2048
wobble optionalboolean
wobbleFreq optionalnumber
wobbleAmp optionalnumber
water optionalbooleanavoid water (default true)
maxTries optionalinteger
waitBase optionalinteger
waitRnd optionalnumber

# ai.exploreArea(c, radius?) → boolean

hunt only since API 3

AIGen_ExploreArea: wandering.

ParameterType
cCreature
radius optionalnumberdefault 8048
Returns
booleanfalse when it reached its target and picked a new one

# ai.eat(c, stop?, state?, phase?, deadPhase?)

hunt only since API 3

AIGen_Eat: kills the hunter when he is within reach of its mouth (AddDeadBody).

ParameterType
cCreature
stop optionalboolean
state optionalinteger
phase optionalintegerits attack animation
deadPhase optionalintegerthe hunter's death animation

# ai.lookForAWay(c, water?, objects?) → number

hunt only since API 3

LookForAWay: turns its target heading away from obstacles.

ParameterType
cCreature
water optionalbooleanavoid water
objects optionalbooleanavoid trees and rocks
Returns
numberthe free heading it found (yaw)

# ai.moveCharacter(c, dx, dz, water?)

hunt only since API 3

MoveCharacter: a step with the terrain collision (it slides along steep slopes).

ParameterType
cCreature
dxnumber
dznumber
water optionalboolean

# ai.tilt(c, blook?, glook?, blim?, glim?)

hunt only since API 3

ThinkY_Beta_Gamma: its height on the ground and its tilt along the slope.

ParameterType
cCreature
blook optionalnumber
glook optionalnumber
blim optionalnumber
glim optionalnumber

# ai.goodPlace(x, z, species?) → boolean

hunt only since API 3

IsGoodPlaceForCharacter: a place its AI would walk to (no steep slope, water or trees).

ParameterType
xnumber
znumber
species optionalSpecies
Returns
boolean