-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a display for melt remainders #1326
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
-- Add a display for how much melted metal is stored inside smelters. | ||
--@module = true | ||
--@enable = true | ||
--[====[ | ||
gui/melt-remainder | ||
================== | ||
When enabled, a line of text is added to the Tasks screen of melters and magma smelters | ||
that shows how much metal is "stored" as a result of melting items in it. (The base game | ||
has no display of this information,) | ||
Click on the text for more detailed information. | ||
]====] | ||
|
||
local overlay = require("plugins.overlay") | ||
local widgets = require("gui.widgets") | ||
-- the existence of this is not documented anywhere of course | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. contributions are welcome for the docs, of course, but you can remove this comment. It doesn't help. |
||
local dialogs = require("gui.dialogs") | ||
|
||
ENABLED = ENABLED or false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. overlays don't need to track their enabled state. that is handled by the overlay framework itself. You will be able to toggle the overlay in |
||
|
||
local function i_hate_lua(tbl) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't really an appropriate place to express your opinions on the scripting language DFHack uses. |
||
local worst_language = 0 | ||
for _,_ in pairs(tbl) do | ||
worst_language = worst_language + 1 | ||
end | ||
return worst_language | ||
end | ||
|
||
local function get_melt_remainders(smelter) | ||
if not smelter.melt_remainder then return nil end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a non-pointer vector field, this can never be falsey. if you want to check for an empty vector, it should be |
||
local fractions = {} | ||
local mat_count = #df.global.world.raws.inorganics | ||
for i = 0, mat_count - 1 do | ||
Comment on lines
+34
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. although |
||
local melt_frac = smelter.melt_remainder[i] | ||
if melt_frac > 0 then | ||
fractions[i] = melt_frac | ||
end | ||
end | ||
return fractions | ||
end | ||
|
||
-- lua doesn't hoist functions nerd emoji | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is true, but the comment doesn't add value |
||
local function popup_full_list() | ||
local workshop = dfhack.gui.getSelectedBuilding(true) | ||
if not workshop then return end | ||
local rems = get_melt_remainders(workshop) | ||
if not rems then return end | ||
|
||
printall(rems) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leftover debug statement? |
||
local lines = {} | ||
for mat_id, tenths in pairs(rems) do | ||
local mat_name = df.global.world.raws.inorganics[mat_id].id | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should probably be using the material lookup functions for this: https://docs.dfhack.org/en/stable/docs/dev/Lua%20API.html#material-info-lookup e.g. then you can get the in-game name from the returned object. |
||
table.insert(lines, mat_name .. ": " .. (tenths * 10) .. "%\n") | ||
end | ||
if #lines == 0 then | ||
table.insert(lines, "<There were no melt remainders>") | ||
end | ||
dialogs.DialogScreen{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
title = "Melt Remainders", | ||
message_label_attrs = { text = lines }, | ||
}:show():raise() | ||
end | ||
|
||
MeltRemainderOverlay = defclass(MeltRemainderOverlay, overlay.OverlayWidget) | ||
MeltRemainderOverlay.ATTRS = { | ||
desc = "Displays the fractions of a complete bar 'stored' in the smelter by melting", | ||
default_pos = { x = -39, y = 41 }, | ||
version = 1, | ||
default_enabled = true, | ||
viewscreens = { | ||
'dwarfmode/ViewSheets/BUILDING/Furnace/Smelter/Tasks', | ||
'dwarfmode/ViewSheets/BUILDING/Furnace/MagmaSmelter/Tasks', | ||
}, | ||
frame = { w = 58, h = 1 }, | ||
visible = function() return ENABLED end, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can take this out. overlay widget visibility is controlled by the framework |
||
} | ||
|
||
function MeltRemainderOverlay:init() | ||
self:addviews { | ||
widgets.Label{ | ||
view_id = "the_label", | ||
text = "<loading...>", | ||
on_click = popup_full_list, | ||
} | ||
} | ||
end | ||
|
||
function MeltRemainderOverlay:onRenderBody(painter) | ||
local workshop = dfhack.gui.getSelectedBuilding(true) | ||
if not workshop then return end | ||
local rems = get_melt_remainders(workshop) | ||
if not rems then return end | ||
|
||
local count = i_hate_lua(rems) | ||
if count == 0 then | ||
self.subviews.the_label:setText("No melt remainders.") | ||
elseif count == 1 then | ||
-- Singleton material | ||
local mat_id, tenths = next(rems) | ||
local mat_name = df.global.world.raws.inorganics[mat_id].id | ||
self.subviews.the_label:setText("Melting " .. mat_name .. ": " .. (tenths * 10) .. "%") | ||
else | ||
self.subviews.the_label:setText(count .. " melt remainders...") | ||
end | ||
end | ||
|
||
OVERLAY_WIDGETS = { melt_remainder = MeltRemainderOverlay } | ||
|
||
function isEnabled() | ||
return ENABLED | ||
end | ||
if dfhack_flags.enable then | ||
ENABLED = dfhack_flags.enable_state | ||
return | ||
end | ||
|
||
print("gui/melt-remainder is " .. (ENABLED and "enabled" or "disabled") .. ".") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when this script is run by name, it should check to see if it is in an appropriate context (a smelter is selected). If not in an appropriate context, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docs should go in
docs/gui/melt-remainder.rst
. You can copy another file in that directory to get the format. More info on the documentation format is at: https://docs.dfhack.org/en/stable/docs/dev/Documentation.html#documentation-standards