From 3583004c6de97dd0f03832a25cc85be9de219ba0 Mon Sep 17 00:00:00 2001 From: Peridexis Errant Date: Wed, 13 May 2015 14:49:10 +1000 Subject: [PATCH 1/4] Add warn-starving.lua With minor changes; it doesn't repeat here. Closes issue #596. --- NEWS | 1 + Readme.rst | 6 ++++ scripts/warn-starving.lua | 65 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 scripts/warn-starving.lua diff --git a/NEWS b/NEWS index c72b77835..cab85e07f 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,7 @@ DFHack Future burial: sets all unowned coffins to allow burial ("-pets" to allow pets too) fix-ster: changes fertility/sterility of animals or dwarves 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 features dwarfmonitor date format can be modified (see Readme) diff --git a/Readme.rst b/Readme.rst index 3870635ce..3b07cd38f 100644 --- a/Readme.rst +++ b/Readme.rst @@ -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, 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 ======== diff --git a/scripts/warn-starving.lua b/scripts/warn-starving.lua new file mode 100644 index 000000000..18eb8fa85 --- /dev/null +++ b/scripts/warn-starving.lua @@ -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() From 2569abcdec955c4b04e39e62b1e2fbd19901a825 Mon Sep 17 00:00:00 2001 From: PeridexisErrant Date: Wed, 13 May 2015 22:58:34 +1000 Subject: [PATCH 2/4] Fix variables na and announcement -> msg --- scripts/warn-starving.lua | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/scripts/warn-starving.lua b/scripts/warn-starving.lua index 18eb8fa85..d8dd2d4ee 100644 --- a/scripts/warn-starving.lua +++ b/scripts/warn-starving.lua @@ -43,22 +43,20 @@ local function checkVariable(var, limit, description, map, unit) end local function doCheck() - local announcement + local msg 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 + msg = checkVariable(unit.counters2.hunger_timer, 75000, 'starving', starvingUnits, unit) + msg = msg or checkVariable(unit.counters2.thirst_timer, 50000, 'dehydrated', dehydratedUnits, unit) + msg = msg or checkVariable(unit.counters2.sleepiness_timer, 150000, 'very drowsy', sleepyUnits, unit) end end end - if announcement then + if msg then df.global.pause_state = true - dfhack.gui.showPopupAnnouncement(announcement, 7, true) + dfhack.gui.showPopupAnnouncement(msg, 7, true) end end From 1706c2884541239bd9b8d3142e1674d592492779 Mon Sep 17 00:00:00 2001 From: PeridexisErrant Date: Wed, 13 May 2015 23:01:18 +1000 Subject: [PATCH 3/4] FIx documentation typos --- NEWS | 2 +- Readme.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index cab85e07f..327cb7795 100644 --- a/NEWS +++ b/NEWS @@ -12,7 +12,7 @@ DFHack Future burial: sets all unowned coffins to allow burial ("-pets" to allow pets too) fix-ster: changes fertility/sterility of animals or dwarves 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 + warn-starving: check for starving, thirsty, or very drowsy units and pause with warning if any are found New tweaks New features dwarfmonitor date format can be modified (see Readme) diff --git a/Readme.rst b/Readme.rst index 3b07cd38f..eb03ea813 100644 --- a/Readme.rst +++ b/Readme.rst @@ -2632,7 +2632,7 @@ 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 +be paused and a warning shown. Use with the ``repeat`` command for regular checks. ======== From 7c3c92dd54095d7d048b23bcc1ac713235a29a42 Mon Sep 17 00:00:00 2001 From: PeridexisErrant Date: Wed, 13 May 2015 23:04:32 +1000 Subject: [PATCH 4/4] Remove surplus end --- scripts/warn-starving.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/warn-starving.lua b/scripts/warn-starving.lua index d8dd2d4ee..16748a0a2 100644 --- a/scripts/warn-starving.lua +++ b/scripts/warn-starving.lua @@ -51,7 +51,6 @@ local function doCheck() msg = checkVariable(unit.counters2.hunger_timer, 75000, 'starving', starvingUnits, unit) msg = msg or checkVariable(unit.counters2.thirst_timer, 50000, 'dehydrated', dehydratedUnits, unit) msg = msg or checkVariable(unit.counters2.sleepiness_timer, 150000, 'very drowsy', sleepyUnits, unit) - end end end if msg then