2015-06-13 15:13:09 -06:00
|
|
|
local _ENV = mkmodule('plugins.dwarfmonitor')
|
|
|
|
|
2022-11-02 13:40:18 -06:00
|
|
|
local json = require('json')
|
|
|
|
local guidm = require('gui.dwarfmode')
|
|
|
|
local overlay = require('plugins.overlay')
|
2022-11-30 22:08:56 -07:00
|
|
|
local utils = require('utils')
|
2015-06-13 15:13:09 -06:00
|
|
|
|
2022-11-02 13:40:18 -06:00
|
|
|
local DWARFMONITOR_CONFIG_FILE = 'dfhack-config/dwarfmonitor.json'
|
2015-06-13 15:13:09 -06:00
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
-- ------------- --
|
|
|
|
-- WeatherWidget --
|
|
|
|
-- ------------- --
|
2015-06-13 15:13:09 -06:00
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
WeatherWidget = defclass(WeatherWidget, overlay.OverlayWidget)
|
|
|
|
WeatherWidget.ATTRS{
|
|
|
|
default_pos={x=15,y=-1},
|
|
|
|
viewscreens={'dungeonmode', 'dwarfmode'},
|
|
|
|
}
|
2015-06-13 15:13:09 -06:00
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
function WeatherWidget:init()
|
2022-11-02 13:40:18 -06:00
|
|
|
self.rain = false
|
|
|
|
self.snow = false
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
function WeatherWidget:overlay_onupdate()
|
2022-11-02 13:40:18 -06:00
|
|
|
local rain, snow = false, false
|
|
|
|
local cw = df.global.current_weather
|
|
|
|
for i=0,4 do
|
|
|
|
for j=0,4 do
|
|
|
|
weather = cw[i][j]
|
2022-11-14 16:23:30 -07:00
|
|
|
if weather == df.weather_type.Rain then rain = true end
|
|
|
|
if weather == df.weather_type.Snow then snow = true end
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
|
|
|
end
|
2022-11-02 13:40:18 -06:00
|
|
|
self.frame.w = (rain and 4 or 0) + (snow and 4 or 0) +
|
|
|
|
((snow and rain) and 1 or 0)
|
|
|
|
self.rain, self.snow = rain, snow
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
function WeatherWidget:onRenderBody(dc)
|
2022-11-02 13:40:18 -06:00
|
|
|
if self.rain then dc:string('Rain', COLOR_LIGHTBLUE):advance(1) end
|
|
|
|
if self.snow then dc:string('Snow', COLOR_WHITE) end
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
-- ---------- --
|
|
|
|
-- DateWidget --
|
|
|
|
-- ---------- --
|
2015-06-13 15:13:09 -06:00
|
|
|
|
2022-11-02 13:40:18 -06:00
|
|
|
local function get_date_format()
|
|
|
|
local ok, config = pcall(json.decode_file, DWARFMONITOR_CONFIG_FILE)
|
|
|
|
if not ok or not config.date_format then
|
|
|
|
return 'Y-M-D'
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
2022-11-02 13:40:18 -06:00
|
|
|
return config.date_format
|
|
|
|
end
|
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
DateWidget = defclass(DateWidget, overlay.OverlayWidget)
|
|
|
|
DateWidget.ATTRS{
|
|
|
|
default_pos={x=-16,y=1},
|
|
|
|
viewscreens={'dungeonmode', 'dwarfmode'},
|
|
|
|
}
|
2022-11-02 13:40:18 -06:00
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
function DateWidget:init()
|
2022-11-02 13:40:18 -06:00
|
|
|
self.datestr = ''
|
|
|
|
self.fmt = get_date_format()
|
|
|
|
end
|
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
function DateWidget:overlay_onupdate()
|
2015-06-13 15:13:09 -06:00
|
|
|
local year = dfhack.world.ReadCurrentYear()
|
|
|
|
local month = dfhack.world.ReadCurrentMonth() + 1
|
|
|
|
local day = dfhack.world.ReadCurrentDay()
|
2022-11-02 13:40:18 -06:00
|
|
|
|
|
|
|
local fmt = self.fmt
|
|
|
|
local datestr = 'Date:'
|
|
|
|
for i=1,#fmt do
|
|
|
|
local c = fmt:sub(i, i)
|
2015-06-13 15:13:09 -06:00
|
|
|
if c == 'y' or c == 'Y' then
|
2022-11-02 13:40:18 -06:00
|
|
|
datestr = datestr .. year
|
2015-06-13 15:13:09 -06:00
|
|
|
elseif c == 'm' or c == 'M' then
|
|
|
|
if c == 'M' and month < 10 then
|
2022-11-02 13:40:18 -06:00
|
|
|
datestr = datestr .. '0'
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
2022-11-02 13:40:18 -06:00
|
|
|
datestr = datestr .. month
|
2015-06-13 15:13:09 -06:00
|
|
|
elseif c == 'd' or c == 'D' then
|
|
|
|
if c == 'D' and day < 10 then
|
2022-11-02 13:40:18 -06:00
|
|
|
datestr = datestr .. '0'
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
2022-11-02 13:40:18 -06:00
|
|
|
datestr = datestr .. day
|
2015-06-13 15:13:09 -06:00
|
|
|
else
|
2022-11-02 13:40:18 -06:00
|
|
|
datestr = datestr .. c
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-02 13:40:18 -06:00
|
|
|
self.frame.w = #datestr
|
|
|
|
self.datestr = datestr
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
function DateWidget:onRenderBody(dc)
|
2022-11-02 13:40:18 -06:00
|
|
|
dc:string(self.datestr, COLOR_GREY)
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
-- ------------ --
|
|
|
|
-- MiseryWidget --
|
|
|
|
-- ------------ --
|
2022-11-02 13:40:18 -06:00
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
MiseryWidget = defclass(MiseryWidget, overlay.OverlayWidget)
|
|
|
|
MiseryWidget.ATTRS{
|
|
|
|
default_pos={x=-2,y=-1},
|
|
|
|
viewscreens={'dwarfmode'},
|
|
|
|
}
|
2015-06-13 15:13:09 -06:00
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
function MiseryWidget:init()
|
2022-11-02 13:40:18 -06:00
|
|
|
self.colors = getStressCategoryColors()
|
|
|
|
self.stress_category_counts = {}
|
2022-11-28 16:13:41 -07:00
|
|
|
self.frame.w = 2*#self.colors + 1
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
function MiseryWidget:overlay_onupdate()
|
2022-11-02 13:40:18 -06:00
|
|
|
local counts, num_colors = {}, #self.colors
|
|
|
|
for _,unit in ipairs(df.global.world.units.active) do
|
2022-11-20 10:45:23 -07:00
|
|
|
if not dfhack.units.isCitizen(unit, true) then goto continue end
|
2022-11-02 13:40:18 -06:00
|
|
|
local stress_category = math.min(num_colors,
|
2022-12-04 16:34:00 -07:00
|
|
|
dfhack.units.getStressCategory(unit)+1)
|
2022-11-02 13:40:18 -06:00
|
|
|
counts[stress_category] = (counts[stress_category] or 0) + 1
|
2022-11-20 10:45:23 -07:00
|
|
|
::continue::
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
|
|
|
|
2022-11-02 13:40:18 -06:00
|
|
|
local width = 2 + num_colors - 1 -- 'H:' plus the slashes
|
|
|
|
for i=1,num_colors do
|
|
|
|
width = width + #tostring(counts[i] or 0)
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
|
|
|
|
2022-11-02 13:40:18 -06:00
|
|
|
self.stress_category_counts = counts
|
|
|
|
self.frame.w = width
|
|
|
|
end
|
2015-06-15 11:42:29 -06:00
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
function MiseryWidget:onRenderBody(dc)
|
2022-11-02 13:40:18 -06:00
|
|
|
dc:string('H:', COLOR_WHITE)
|
|
|
|
local counts = self.stress_category_counts
|
|
|
|
for i,color in ipairs(self.colors) do
|
|
|
|
dc:string(tostring(counts[i] or 0), color)
|
|
|
|
if i < #self.colors then dc:string('/', COLOR_WHITE) end
|
2015-06-15 11:42:29 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
-- ------------ --
|
|
|
|
-- CursorWidget --
|
|
|
|
-- ------------ --
|
2015-06-15 11:42:29 -06:00
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
CursorWidget = defclass(CursorWidget, overlay.OverlayWidget)
|
|
|
|
CursorWidget.ATTRS{
|
|
|
|
default_pos={x=2,y=-4},
|
|
|
|
viewscreens={'dungeonmode', 'dwarfmode'},
|
2022-11-30 22:08:56 -07:00
|
|
|
type='all',
|
|
|
|
short=false,
|
2022-11-04 11:29:09 -06:00
|
|
|
}
|
2015-06-15 11:42:29 -06:00
|
|
|
|
2022-11-30 22:08:56 -07:00
|
|
|
CursorWidget.COORD_TYPES = utils.OrderedTable()
|
|
|
|
CursorWidget.COORD_TYPES.mouse_ui = {
|
|
|
|
description = 'mouse UI grid',
|
|
|
|
get_coords = function()
|
|
|
|
return xyz2pos(dfhack.screen.getMousePos())
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
CursorWidget.COORD_TYPES.mouse_map = {
|
|
|
|
description = 'mouse map coord',
|
|
|
|
get_coords = dfhack.gui.getMousePos,
|
|
|
|
}
|
|
|
|
CursorWidget.COORD_TYPES.keyboard_map = {
|
|
|
|
description = 'kbd cursor coord',
|
|
|
|
get_coords = guidm.getCursorPos,
|
|
|
|
}
|
2015-06-13 15:13:09 -06:00
|
|
|
|
2022-11-30 22:08:56 -07:00
|
|
|
function CursorWidget:init()
|
|
|
|
local ok, config = pcall(json.decode_file, DWARFMONITOR_CONFIG_FILE)
|
|
|
|
if ok then
|
|
|
|
self.type = tostring(config.coords_type or self.type)
|
|
|
|
self.short = config.coords_short
|
|
|
|
else
|
|
|
|
dfhack.printerr(('Failed to load %s: %s'):format(DWARFMONITOR_CONFIG_FILE, config))
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
2022-11-30 22:08:56 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function CursorWidget:onRenderBody(dc)
|
|
|
|
local text = {}
|
|
|
|
for k, v in pairs(CursorWidget.COORD_TYPES) do
|
|
|
|
-- fall back to 'all' behavior if type isn't recognized
|
|
|
|
if k == self.type or not CursorWidget.COORD_TYPES[self.type] then
|
|
|
|
local coords = v.get_coords()
|
|
|
|
if coords then
|
|
|
|
local prefix = self.short and '' or (v.description .. ' ')
|
|
|
|
local coord_str = table.concat({coords.x, coords.y, coords.z}, ',')
|
|
|
|
table.insert(text, ('%s(%s)'):format(prefix, coord_str))
|
|
|
|
end
|
|
|
|
end
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
2022-11-30 22:08:56 -07:00
|
|
|
|
2022-11-02 13:40:18 -06:00
|
|
|
local width = 0
|
|
|
|
for i,line in ipairs(text) do
|
|
|
|
dc:seek(0, i-1):string(line)
|
|
|
|
width = math.max(width, #line)
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
2022-11-02 13:40:18 -06:00
|
|
|
self.frame.w = width
|
|
|
|
self.frame.h = #text
|
2015-06-13 15:13:09 -06:00
|
|
|
end
|
|
|
|
|
2022-11-04 11:29:09 -06:00
|
|
|
-- register our widgets with the overlay
|
|
|
|
OVERLAY_WIDGETS = {
|
|
|
|
cursor=CursorWidget,
|
|
|
|
date=DateWidget,
|
|
|
|
misery=MiseryWidget,
|
|
|
|
weather=WeatherWidget,
|
|
|
|
}
|
|
|
|
|
2015-06-13 15:13:09 -06:00
|
|
|
return _ENV
|