Zones, populations, herds and packs

A new animal should feel like part of the world: it lives in certain places and at certain times, in herds that move and flee together or packs that hunt together, with a home it returns to and places it keeps away from. Three parts of the API (since API 3: "api": 3 in mod.json) do this, from Lua or from plain JSON files:

Nothing of this runs unless a mod uses it: without such a mod both games place and move their animals exactly as the original does.

Zones

A zone is a circle, a box, a polygon or the whole map, cut down by conditions. Coordinates are game units (128 = 1 m; x from 0 to 262144, z from 0 to -262144, Scripting basics).

local x, _, z = player.pos()
local camp  = zone.circle("camp", x, z, 3000)                           -- 23 m around the hunter
local field = zone.box("field", 60000, -90000, 90000, -60000)
local bay   = zone.polygon("bay", {{100000, -50000}, {120000, -52000}, {110000, -70000}})
local shore = zone.terrain("shore", {nearWater = 2000, land = true})    -- every stretch of land near water
local peaks = zone.terrain("peaks", {minHeight = 9000})                 -- the high ground
Field of the definition Meaning
name its name; a zone with the name of an existing one replaces it (the old handle keeps working)
circle = {x, z, radius} / x, z, radius a circle
box = {x1, z1, x2, z2} / x1, z1, x2, z2 a box (corners in any order)
polygon = {{x, z}, ...} / points a polygon, at least 3 corners
(no shape) the whole map: a terrain region, made by its conditions
water = true / land = true only water / only land
nearWater, awayFromWater within / at least this far from water
minHeight, maxHeight the ground's height
maxSlope how steep the ground may be (rise per unit: 0.5 = 1 in 2)
minY, maxY a height range for what is tested: a creature's or the hunter's feet (z:contains(x, z, y))
areas = {0, 2}, maps = {"area3"} it exists only on these maps (game.area() numbers or content map ids)
hunter = false no zone_enter / zone_leave for the hunter
creatures = true / {species = {...}, team = n, group = id, predator = bool} creatures fire them too (all, or these)
games = {"iceage"} JSON files only: the entry is for that game alone
local nest = zone.circle("nest", 150000, -120000, 5000, {creatures = {species = {"velociraptor"}}})
on("zone_enter", function(e)
  if e.zone == nest then
    if e.hunter then game.message("You walked into a raptor nest") else print(e.creature.name .. " came home") end
  end
end)

A circle zone can move and change size: z.x, z.z and z.radius are writable (or z:set{radius = 4000}); who is inside is kept, and the next frame fires zone_enter / zone_leave for what the new circle takes in or leaves out. z:remove() returns false when the zone is gone already, as zone.remove(z) does.

z:contains(x, z), z:creatures(), z:count(), z:randomPoint() and zone.at(x, z) answer the rest; world.randomPoint{...} finds a point of the map on land (never in water) that meets conditions such as nearWater, maxSlope, minHunterDistance and species (a place that species can stand on, the game's own test).

A script's zones last until the scripts are reloaded, from hunt to hunt; make them in main.lua (or in hunt_start for zones that depend on where the hunter starts).

Populations

A population rule places a species when a hunt begins and can keep it up during the hunt. Put rules into populations.json (or spawns/*.json, any number of files, and zones.json for zones alone) in your mod folder; a data mod without scripts can have them. The files are read again for every hunt.

{
  "zones": [
    {"name": "shore", "terrain": {"nearWater": 2500, "maxSlope": 0.5}, "land": true}
  ],
  "populations": [
    {
      "id": "lake_herds",
      "species": "parasaurolophus",
      "time": "dawn",
      "groups": [2, 3],
      "herd": {"size": [4, 7], "formation": "loose", "wary": 2500},
      "where": {"zone": "shore", "minHunterDistance": 9000},
      "respawn": {"delay": 180, "minHunterDistance": 12000},
      "cap": 18
    },
    {"species": "velociraptor", "pack": {"size": 4, "prey": ["parasaurolophus"]}, "territory": "nest"},
    {"species": "moschops", "mode": "replace", "count": [5, 10], "where": {"maxHeight": 3000}}
  ]
}

The same table from Lua: population.add{species = "parasaurolophus", herd = {size = {4, 7}}, ...}. A rule added in main.lua takes part from the next hunt on; one added during a hunt places its creatures at once.

Field Meaning
id a name for it (default: the mod, the species and a number); a rule with an existing id replaces it
species a species id (world.spawn's ids, content packs' too) or a type number
areas, maps only on these maps (game.area() numbers, content map ids); default every map
time "dawn" (or "morning"), "day", "night", or a list: the hunt's time of day; default all
chance that the species lives on the map in this hunt (0-1, default 1)
groups how many groups: a number or [min, max] (default 1)
size how many in each group: a number or [min, max] (default 1)
count single animals: count = [5, 10] is groups = [5, 10] of size 1
herd / pack its groups are herds / packs, with these options (groups); size and groups may go inside: "herd": {"size": [3, 6], "formation": "loose"}
group the same with kind inside, or "herd" / "pack"; false: placed together, no group. Groups of 2 or more are herds (packs for predators) unless it says otherwise
territory, avoid its groups' home zone and the zones they keep out of (the home zone is also where they are placed when where names no zone)
where.zone / where.zones placed in one of these zones
where.water "land" (default), "water", "any"
where.nearWater, where.awayFromWater, where.minHeight, where.maxHeight, where.maxSlope conditions, as for zones
where.minHunterDistance never closer to the hunter's start (default 8192, as the game keeps its hunt dinosaurs)
where.maxHunterDistance never farther
mode "add" (default): to the game's own placement; "replace": the game (and content packs) place none of this species, only the rule
respawn true, a delay in seconds, or {"delay": 120, "minHunterDistance": 10000, "max": 5}: when its creatures die, new groups come (out of the hunter's sight) until the rule has as many as it placed
cap the most living creatures of this rule at once
speciesCap the most living creatures of the species on the map, from any source
survive also in Survive mode (default: hunts only)
games JSON files: ["dinohunter"] or ["iceage"] for a rule of one game in a mod for both

Every placement fires spawn_planned first: change its species, count, x / z or cancel it. The game's own placement fires it too (source == "game", without a position: set x and z to choose one). population_changed tells when a rule's creatures die, are removed or come back. population.list(), population.members(id) and population.spawn(id) look at and drive the rules from a script.

Herds and packs

A group has a leader that roams with its own AI (or walks to g:moveTo, or back to its territory); the others keep their place around it and move with it. Members that fell far behind run to catch up; a group that was split waits (group_regroup); a leader that cannot get anywhere hands over to the next member.

Option Default Meaning
kind "group" "herd" (flees together), "pack" (hunts together), "group" (follows only)
formation "loose" "loose" (each roams around its own place near the leader), "line", "column", "wedge", "circle", "free" (only kept within spread)
spacing by size between neighbours in a formation
spread by size and count how far from the leader a loose group roams
sharedAlert true one notices danger: all react
flee herds "together" "together", "own" (the alert is shared, each runs its own AI's way), false
wary 0 flee from any predator this close (0: only what the AIs notice)
hunt packs hunt together
huntHunter true a pack that notices the hunter hunts him together
prey none species a pack hunts on its own
huntRange 8000 prey this close to the leader is hunted
huntCooldown 60 seconds before it hunts prey again
surround true spread around the target before attacking
attackers 1 members attacking at once, 0 = all
turnTime 5 seconds a turn lasts at the target (the way there does not count)
ring by target circling distance around the target
feedTime 20 seconds at the kill
territory none home zone (a Zone or its name)
leash 0 how far outside the territory before it walks back
avoid none zones it keeps out of
regroup by spread a member this far from the leader splits the group
drive false move script-driven members (c.ai = false) with their walk and run animations
-- a pack from three raptors that already exist, with a home and prey
local raptors = world.creatures({species = "velociraptor", radius = 8000})
local nest = zone.circle("nest", raptors[1].x, raptors[1].z, 5000)
local pack = group.create{kind = "pack", members = raptors, prey = {"parasaurolophus", "gallimimus"},
                          territory = nest, leash = 2000, attackers = 1}
on("group_kill", function(e) if e.group == pack then print("the pack killed a " .. e.creature.name) end end)

How it moves the animals. Groups steer the game's own AIs: every frame, before the AIs run, each member gets a walk target (c.targetX / c.targetZ, which its AI heads for) and an alert state (an alerted AI runs). The animals keep their own animations, speeds, sounds and way-finding around trees and rocks. A short run to catch up or to get home is an alert state too (only while the hunter is far away, so no AI reacts to him). Members that the world API drives (creature fights, possession) and a frozen world are left alone.

Script-driven creatures. A member with c.ai = false gets the same targets and states; read them (c.targetX, c.state, g:slot(c), g.state) and move it yourself, or give the group drive = true and it moves them with their walk / run animations.

A script's groups end with a new hunt and with a reload (make them in hunt_start, spawn or later); populations make their groups for every hunt.

Both games

Everything works the same in Dinosaur Hunter and Ice Age, with each game's species: Parasaurolophus herds and raptor packs there, mammoth herds and wolf packs here. A JSON file for both games marks rules and zones of one game with "games": ["iceage"]; a script picks with game.id().

Example: Living Herds

Parasaurolophus herds near water at dawn that flee together, Gallimimus herds that run from predators, and a Velociraptor pack with its own territory that hunts them; in Ice Age mammoth herds and a wolf pack. In the examples folder: examples/mods/living_herds.

{
  "zones": [
    {"name": "shore", "terrain": {"nearWater": 2500, "maxSlope": 0.5}, "land": true}
  ],
  "populations": [
    {
      "id": "living:parasaurolophus",
      "games": ["dinohunter"],
      "species": "parasaurolophus",
      "time": "dawn",
      "groups": [2, 3],
      "herd": {"size": [4, 7], "formation": "loose", "wary": 2500},
      "where": {"zone": "shore", "minHunterDistance": 9000},
      "respawn": {"delay": 180, "minHunterDistance": 12000},
      "cap": 18
    },
    {
      "id": "living:gallimimus",
      "games": ["dinohunter"],
      "species": "gallimimus",
      "groups": [1, 2],
      "herd": {"size": [5, 8], "formation": "loose", "wary": 3000},
      "where": {"minHunterDistance": 8000}
    },
    {
      "id": "living:mammoth",
      "games": ["iceage"],
      "species": "mammoth",
      "groups": [2, 3],
      "herd": {"size": [3, 5], "formation": "wedge"},
      "where": {"minHunterDistance": 9000, "maxSlope": 0.5},
      "respawn": {"delay": 240, "minHunterDistance": 12000},
      "cap": 12
    }
  ]
}
-- Living Herds: the herds come from populations.json (read for every hunt). The pack is placed here, because its
-- territory is chosen for each hunt: a stretch of land 16000-22000 units (125-170 m) from where the hunter starts
-- (farther out, or any dry land, when there is none), so the raptors do not smell him the moment the hunt begins.
local IA = game.id() == "iceage"
local PACK = IA and "wolf" or "velociraptor"
local PREY = IA and {"deer", "wildboar"} or {"parasaurolophus", "gallimimus"}
local NAME = IA and "wolves" or "raptors"

-- a place for the pack's territory: ground its species can stand on, 16000-22000 units away; near forests or water
-- there may be none, so farther out next, then any dry land at a distance
local TRIES = {
  {minHunterDistance = 16000, maxHunterDistance = 22000, species = PACK, maxSlope = 0.4},
  {minHunterDistance = 14000, maxHunterDistance = 30000, species = PACK},
  {minHunterDistance = 14000, species = PACK},   -- anywhere on the map its species can stand
  {minHunterDistance = 14000},                   -- any dry land
}
local function territoryPlace()
  for i, opts in ipairs(TRIES) do
    local x, z = world.randomPoint(opts)
    if x then
      if i > 1 then print(string.format("the %s' territory: fallback %d of %d", NAME, i, #TRIES)) end
      return x, z
    end
  end
end

on("hunt_start", function()
  local x, z = territoryPlace()
  if not x then
    print("no place for the " .. NAME .. "' territory on this map")
    return
  end
  local home = zone.circle("living:territory", x, z, 4500)
  population.add{
    id = "living:pack", species = PACK,
    pack = {size = {3, 4}, prey = PREY, huntRange = 7000, attackers = 1, turnTime = 5, territory = home.name,
            leash = 3000},
    where = {zone = home.name, minHunterDistance = 12000},   -- never right beside the hunter's start
  }
end)

on("hunt_end", function()
  population.remove("living:pack")  -- a new territory next hunt
end)

-- a word now and then about what the animals do (the console shows it too)
on("group_flee", function(e)
  if e.group.kind == "herd" and e.group.leader then
    local d = e.group.leader.distance
    if d and d < 12000 then game.message(string.format("A herd of %s flees (%d m away)", e.group.leader.name, math.floor(d / 128))) end
  end
end)
on("group_hunt", function(e)
  if e.group.rule == "living:pack" then
    game.message(e.hunter and ("The " .. NAME .. " are hunting you!") or ("The " .. NAME .. " are on the hunt"))
  end
end)
on("group_kill", function(e)
  if e.group.rule == "living:pack" then print("the " .. NAME .. " killed a " .. e.creature.name) end
end)