Spawning AI
modkit.ai puts AI creatures into the world. You can feed a hungry corner of the map, run an event with a pack of hostile dinosaurs, or clean up when the server gets slow.
What you can spawn
print(table.concat(modkit.ai.species(), ", "))
The list has dinosaurs, the small animals (boar, deer, goat, rabbit, chicken, crab, bullfrog, lizard, sea turtle) and four kinds of fish. A spawned dinosaur is a full AI dinosaur with the game's own behaviour for that species. Fish only make sense in water.
A complete resource
resources/wildlife/manifest.lua
name "wildlife"
server "server.lua"
resources/wildlife/server.lua
local FEEDING_GROUNDS = {
{ species = "deer", x = -180000, y = 52000, z = 1500, count = 4 },
{ species = "boar", x = 96000, y = -41000, z = 900, count = 3 },
}
local MAX_SCRIPT_AI = 40
local function restock()
if modkit.players.count() == 0 then return end
local alive = 0
for _, creature in ipairs(modkit.ai.list()) do
if creature.alive then alive = alive + 1 end
end
for _, spot in ipairs(FEEDING_GROUNDS) do
if alive + spot.count > MAX_SCRIPT_AI then return end
local ids = modkit.ai.spawn(spot.species, spot.x, spot.y, spot.z, { count = spot.count, behavior = "passive" })
if ids then alive = alive + #ids end
end
end
modkit.events.add("serverStarted", function()
modkit.setInterval(20 * 60 * 1000, restock)
end)
modkit.commands.add("spawnai", function(player, species, count, behavior)
local x, y, z = player:pos()
if not x then player:message("You need a dinosaur to mark the place.") return end
local ids, why = modkit.ai.spawn(species or "", x + 1000, y, z, {
count = tonumber(count) or 1,
behavior = behavior or "default",
yaw = player.yaw + 180,
})
player:message(ids and (#ids .. " spawned") or ("Failed: " .. why))
end, "ai.spawn")
modkit.commands.add("hunt", function(player)
local x, y, z = player:pos()
if not x then return end
local ids = modkit.ai.spawn("carnotaurus", x + 6000, y, z, { count = 2, growth = 0.8, behavior = "aggressive" })
if not ids then return end
modkit.world.announce("Two hungry Carnotaurus were seen near " .. player.name .. ".", "Hunt", 10)
modkit.setTimeout(10 * 60 * 1000, function()
for _, id in ipairs(ids) do modkit.ai.despawn(id) end
end)
end, "ai.spawn")
modkit.commands.add("aicount", function(player)
local counts, total = modkit.ai.census()
for species, n in pairs(counts) do player:message(species .. ": " .. n) end
player:message(total .. " AI in the world, " .. modkit.ai.count() .. " from scripts")
end, "ai.spawn")
modkit.commands.add("aiwipe", function(player)
player:message(modkit.ai.wipe() .. " AI removed")
end, "ai.wipe")
The coordinates are placeholders. Use positions from your map.
Things to know
- A creature spawns 5 m above the position you give and drops to the ground. Pass the ground height as
z, for example fromplayer:pos(). - Several creatures are placed in rows of five, 3 m apart, starting 6 m to the side of the position.
behavior = "passive"makes the creature flee from everything."aggressive"stops it from fleeing and puts it into the attack state."default"leaves the behaviour of the species alone.- The first spawn of a species loads its blueprint, which can cause a short hitch. Spawn rare species once after the server started if that bothers you.
- Handles from
spawnbelong to creatures your scripts made. The game's own AI has no handle.censusandwipesee all AI. censusandwipewalk through every object of the game. Run them from a command, not from a fast timer.- Scripts can keep 500 creatures alive at once. A server gets slow long before that, so set your own limit like
MAX_SCRIPT_AIabove. - Creatures are gone after a restart. There is nothing to clean up.
Spawned creatures behave the way the game made their species. To decide yourself how they sense, hunt and flee, give them a brain. The custom AI guide shows how.