Skip to content

Instantly share code, notes, and snippets.

@Meneth32
Created February 19, 2015 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Meneth32/ff6fab74d327d4cde84c to your computer and use it in GitHub Desktop.
Save Meneth32/ff6fab74d327d4cde84c to your computer and use it in GitHub Desktop.
-- Pauses the game with a warning when a creature becomes starving, dehydrated, or very drowsy.
starvingUnits = starvingUnits or {}
dehydratedUnits = dehydratedUnits or {}
sleepyUnits = sleepyUnits or {}
local units = df.global.world.units.active
local repeatUtil = require 'repeat-util'
local function findRaceCaste(unit)
local rraw = df.creature_raw.find(unit.race)
return rraw, safe_index(rraw, 'caste', unit.caste)
end
function dumpKeys(o)
local s = '{'
for k,v in pairs(o) do
s = s .. '['..k..'],'
end
return s .. '} '
end
local function getSexString(sex)
local sexStr
if sex==0 then
sexStr=string.char(12)
elseif sex==1 then
sexStr=string.char(11)
else
return ""
end
return string.char(40)..sexStr..string.char(41)
end
local function nameOrSpeciesAndNumber(unit)
if unit.name.has_name then
return dfhack.TranslateName(dfhack.units.getVisibleName(unit))..' '..getSexString(unit.sex),true
else
return 'Unit #'..unit.id..' ('..df.creature_raw.find(unit.race).caste[unit.caste].caste_name[0]..' '..getSexString(unit.sex)..')',false
end
end
local function checkVariable(var, limit, description, map, unit)
local rraw = findRaceCaste(unit)
local species = rraw.name[0]
local name = nameOrSpeciesAndNumber(unit)
if(var > limit) then
if(not map[unit.id]) then
map[unit.id] = true
return species.." "..name.." is "..description.."!"
end
else
map[unit.id] = false
end
return false
end
local function doCheck(suppressAnnouncements)
suppressAnnouncements = suppressAnnouncements or true
local count = 0
local announcement
for i=#units-1,0,-1 do
local unit = units[i]
local rraw = findRaceCaste(unit)
if rraw and not unit.flags1.dead and not dfhack.units.isOpposedToLife(unit) then
--local species = rraw.name[0]
--local name = nameOrSpeciesAndNumber(unit)
--local msg = species.." "..name.."("..unit.id..") h/t/s: "..unit.counters2.hunger_timer..
-- " "..unit.counters2.thirst_timer.." "..unit.counters2.sleepiness_timer--..dumpKeys(unit.name)
--print(msg)
local na = checkVariable(unit.counters2.hunger_timer, 75000, 'starving', starvingUnits, unit)
na = na or checkVariable(unit.counters2.thirst_timer, 50000, 'dehydrated', dehydratedUnits, unit)
na = na or checkVariable(unit.counters2.sleepiness_timer, 150000, 'very drowsy', sleepyUnits, unit)
if(na) then
announcement = newAnnouncement
count = count + 1
end
end
end
if not suppressAnnouncements and announcement then
df.global.pause_state = true
dfhack.gui.showPopupAnnouncement(announcement, 7, true)
end
end
doCheck(true)
repeatUtil.scheduleEvery('warn-starving',1000,'ticks',doCheck)
print("warn-starving timer enabled.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment