User:Fleeting Frames/max-wave

From Dwarf Fortress Wiki
Jump to navigation Jump to search

Usage: Save into /hack/scripts folder as max-wave.lua script, put 'repeat -time 1 -timeUnits months -command [ max-wave 10 80 ]' in onMapLoad.init

(Original written by Loci here.)

--Dynamically limit the next immigration wave

Set the population cap to the lesser of current_pop + wave_size or max_pop.
Use with the `repeat` command to set a rolling immigration limit.

Usage examples::

    max-wave wave_size (max_pop)
    repeat -time 1 -timeUnits months -command [ max-wave 10 200 ]

The first example is abstract and only sets the population cap once;
the second will update the population cap monthly, allowing a
maximum of 10 immigrants per wave, up to a total population of 200.


local args = {...}

local wave_size = tonumber(args[1])
local max_pop = tonumber(args[2])
local current_pop = 0

if not wave_size then
  print('max-wave: wave_size required')

--One would think the game would track this value somewhere...
for k,v in ipairs( do
  if dfhack.units.isCitizen(v) or 
  (dfhack.units.isOwnCiv(v) and 
   dfhack.units.isAlive(v) and[v.race].caste[v.caste].flags.CAN_LEARN and
   not (dfhack.units.isMerchant(v) or dfhack.units.isForest(v) or v.flags1.diplomat or v.flags2.visitor)
   current_pop = current_pop + 1

local new_limit = current_pop + wave_size
if max_pop and new_limit > max_pop then new_limit = max_pop end = new_limit
print('max-wave: Population cap set to '.. new_limit)