group

Herds and packs: groups that follow a leader, share alerts, flee and hunt together (since API 3).

A group is a set of creatures with a leader. The others keep a formation (or a loose distance) around it and move with it; when one member notices danger the whole group reacts; a herd flees together, a pack hunts together: it spreads around its target, then attacks in turns while the others circle. A group can have a territory (a home zone it returns to) and zones it avoids, and it regroups when it was split.

Groups steer the game's own AIs through their walk targets and alert state, so the creatures keep their own animations and movement; script-driven members (c.ai = false) get the same targets (c.targetX / c.targetZ, c.state, or g:slot(c)) and are moved by the group when it has drive = true. A script's groups end with a new hunt and with a reload; populations make groups for every hunt. The guide: Zones, populations, herds and packs.

Functions

# group.create(def) → Group

hunt only since API 3

Makes a group of creatures.

ParameterType
deftablekind ("herd", "pack" or "group"), members = {creatures}, leader = a creature (default: the first member), name, and any options (see the guide): formation, spacing, spread, sharedAlert, flee ("together", "own", false), hunt, huntHunter, prey = {species}, huntRange, surround, attackers, turnTime, ring, territory (a Zone or its name), leash, avoid = {zones}, regroup, wary, drive, feedTime, huntCooldown
Returns
Group
local herd = {}
for i = 1, 5 do herd[i] = world.spawn("parasaurolophus", x + i * 400, z) end
local g = group.create{kind = "herd", members = herd, formation = "loose", wary = 2500}

# group.of(c) → Group|nil

since API 3

The group a creature belongs to.

ParameterType
cCreature
Returns
Group|nil

# group.get(id) → Group|nil

since API 3

A group by id or name.

ParameterType
idinteger|stringan id or a name
Returns
Group|nil

# group.list() → table

since API 3

Every group.

Returns
tablea list of Group

Group

A handle to one group (group.create, group.of ...). Handles compare equal when they are the same group.

Fields

FieldTypeAccess
idintegerreada number for it
validbooleanreadfalse once it was disbanded (also: a new hunt, a reload for a script's group)
kindstringread"herd", "pack" or "group"
namestringread / writea name (default: kind and id)
statestringread"calm", "alert", "flee", "hunt", "feed" (a pack at its kill), "regroup" or "home" (walking back to its territory)
formationstringread / write"loose", "line", "column", "wedge", "circle" or "free"
sizeintegerreadhow many members
leaderCreature|nilread / writethe member the others follow; set it to another creature (it joins first)
memberstablereada list of its creatures, the leader first
targetCreature|string|nilreadwhat a pack hunts: a creature, "hunter", or nil
territorystring|nilread / writeits home zone's name; set a Zone, a name or nil
rulestring|nilreadthe population that placed it (nil: a script made it)

Methods

# g:add(c) → boolean

since API 3

Adds a creature (it leaves its old group).

ParameterType
cCreature
Returns
boolean

# g:remove(c) → boolean

since API 3

Takes a creature out of the group.

ParameterType
cCreature
Returns
boolean

# g:has(c) → boolean

since API 3

Whether a creature is a member.

ParameterType
cCreature
Returns
boolean

# g:set(opts)

since API 3

Changes behaviour options.

ParameterType
optstableoptions, as group.create's
g:set{formation = "wedge", wary = 3000}

# g:alert(threat?)

hunt only since API 3

As if a member noticed the threat: the group reacts (a herd flees, a pack hunts the hunter).

ParameterType
threat optionalCreature|stringa creature, or "hunter" / nil for the hunter

# g:flee(from?)

hunt only since API 3

The group flees together.

ParameterType
from optionalCreature|stringa creature, or "hunter" / nil

# g:hunt(target?)

hunt only since API 3

The group hunts: it spreads around the target, then attacks in turns while the others circle.

ParameterType
target optionalCreature|stringa creature, or "hunter" / nil

# g:calm()

since API 3

Ends a flight or a hunt; the members calm down.

# g:moveTo(x, z)

since API 3

The leader walks there (the others follow), then roams again.

ParameterType
xnumber
znumber

# g:center() → number, number

since API 3

The middle of its members.

Returns
numberx
numberz

# g:slot(c) → number?, number?

since API 3

Where that member should be now (its place in the formation): for script-driven members.

ParameterType
cCreaturea member
Returns
number?x
number?z

# g:disband()

since API 3

Ends the group (the creatures stay).