Add warn-starving.lua

With minor changes; it doesn't repeat here.
Closes issue #596.
develop
Peridexis Errant 2015-05-13 14:49:10 +10:00
parent 9b8b95e145
commit 3583004c6d
3 changed files with 72 additions and 0 deletions

@ -12,6 +12,7 @@ DFHack Future
burial: sets all unowned coffins to allow burial ("-pets" to allow pets too) burial: sets all unowned coffins to allow burial ("-pets" to allow pets too)
fix-ster: changes fertility/sterility of animals or dwarves fix-ster: changes fertility/sterility of animals or dwarves
view-item-info: adds information and customisable descriptions to item viewscreens view-item-info: adds information and customisable descriptions to item viewscreens
warn-starving: check for starving, thirsty, or very drowsy units and pasue with warning if any are found
New tweaks New tweaks
New features New features
dwarfmonitor date format can be modified (see Readme) dwarfmonitor date format can be modified (see Readme)

@ -2629,6 +2629,12 @@ of items. Individual descriptions can be added or overridden by a similar
script ``raw/scripts/more-item-descriptions.lua``. Both work as sparse lists, script ``raw/scripts/more-item-descriptions.lua``. Both work as sparse lists,
so missing items simply go undescribed if not defined in the fallback. so missing items simply go undescribed if not defined in the fallback.
warn-starving
=============
If any (live) units are starving, very thirsty, or very drowsy, the game will
be paused and a warnign shown. Use with the ``repeat`` command for regular
checks.
======== ========
modtools modtools
======== ========

@ -0,0 +1,65 @@
-- Pauses the game with a warning if a creature is starving, dehydrated, or very drowsy.
-- By Meneth32, and PeridexisErrant
starvingUnits = starvingUnits or {}
dehydratedUnits = dehydratedUnits or {}
sleepyUnits = sleepyUnits or {}
local units = df.global.world.units.active
local function findRaceCaste(unit)
local rraw = df.creature_raw.find(unit.race)
return rraw, safe_index(rraw, 'caste', unit.caste)
end
local function getSexString(sex)
local sexStr = ""
if sex==0 then sexStr=string.char(12)
elseif sex==1 then sexStr=string.char(11)
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
end
local function doCheck()
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 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
end
end
end
if announcement then
df.global.pause_state = true
dfhack.gui.showPopupAnnouncement(announcement, 7, true)
end
end
doCheck()