IMIsle Modkit
Pages in this area

Population lock

A population lock keeps a server from turning into forty rexes. You give species a cap, and the server takes a capped species off the character select screen while the cap is reached. The functions are in modkit.population.

A complete resource

resources/poplock/manifest.lua

name "poplock"
server "server.lua"

resources/poplock/server.lua

local RULES = {
  Tyrannosaurus = { cap = 4 },
  Deinosuchus = { cap = 3 },
  Carnotaurus = { cap = 20, percent = true },
  Troodon = { enabled = false },
}

for species, rule in pairs(RULES) do
  modkit.population.setRule(species, rule)
end
modkit.population.setEggCleanup(true)
modkit.population.setAutoThreshold(20)

modkit.events.add("playerConnected", function(player)
  if player.admin then modkit.population.setExempt(player, true) end
end)

local lastBlocked = ""
modkit.events.add("populationChanged", function()
  local blocked = table.concat(modkit.population.blocked(), ", ")
  if blocked == lastBlocked then return end
  lastBlocked = blocked
  if blocked ~= "" then modkit.chat.broadcast("Full right now: " .. blocked, "Population") end
end)

modkit.commands.add("pop", function(player)
  local counts, online = modkit.population.distribution()
  for species, n in pairs(counts) do
    local rule = modkit.population.getRule(species)
    local cap = ""
    if rule and rule.cap > 0 then cap = " / " .. rule.cap .. (rule.percent and "%" or "") end
    player:message(species .. ": " .. n .. cap)
  end
  player:message(online .. " players online")
end)

modkit.commands.add("poplock", function(player, mode)
  modkit.population.setEnabled(mode == "on")
  local enabled, active = modkit.population.isEnabled()
  player:message("lock switch " .. tostring(enabled) .. ", active " .. tostring(active))
end, true)

Add load poplock to server.cfg and restart.

What the parts do

setAutoThreshold(20) switches the lock on by itself once 20 players are online. Below that everyone picks what they want. /poplock on forces it on at any player count.

cap = 20, percent = true means 20 percent of the online players, rounded, and never less than one. With 30 players online that is 6 Carnotaurus.

enabled = false removes a species from the select screen for as long as the lock is active.

setEggCleanup(true) destroys nests of blocked species every 5 seconds. Without it a full species can still grow through eggs. It costs a walk through all game objects, so switch it on only when players really use that trick.

Admins are made exempt when they connect. The exempt list is not saved, which is why the resource sets it on every connect.

Limits

  • The lock only stops new spawns. It never kills or swaps a player who is already on a capped species.
  • A cap counts living dinosaurs of players who are online. A player in the select screen does not count.
  • Rules are not saved. Keep them in your script, or in modkit.storage if admins change them with commands.
  • While the lock is active the select screen shows every species the server has cooked, minus the blocked ones. If you removed species in Game.ini, add a rule with enabled = false for each of them.
  • Species from modkit.roster follow the same rules.
  • Restoring a dinosaur from the garage does not ask the lock. If it should, check modkit.population.isBlocked(slot.species) in the garageRestoring event. The garage guide shows that.