|
| 1 | +-- Add a display for how much melted metal is stored inside smelters. |
| 2 | +--@module = true |
| 3 | +--@enable = true |
| 4 | +--[====[ |
| 5 | +
|
| 6 | +gui/melt-remainder |
| 7 | +================== |
| 8 | +When enabled, a line of text is added to the Tasks screen of melters and magma smelters |
| 9 | +that shows how much metal is "stored" as a result of melting items in it. (The base game |
| 10 | +has no display of this information,) |
| 11 | +
|
| 12 | +Click on the text for more detailed information. |
| 13 | +
|
| 14 | +]====] |
| 15 | + |
| 16 | +local overlay = require("plugins.overlay") |
| 17 | +local widgets = require("gui.widgets") |
| 18 | +-- the existence of this is not documented anywhere of course |
| 19 | +local dialogs = require("gui.dialogs") |
| 20 | + |
| 21 | +ENABLED = ENABLED or false |
| 22 | + |
| 23 | +local function i_hate_lua(tbl) |
| 24 | + local worst_language = 0 |
| 25 | + for _,_ in pairs(tbl) do |
| 26 | + worst_language = worst_language + 1 |
| 27 | + end |
| 28 | + return worst_language |
| 29 | +end |
| 30 | + |
| 31 | +local function get_melt_remainders(smelter) |
| 32 | + if not smelter.melt_remainder then return nil end |
| 33 | + local fractions = {} |
| 34 | + local mat_count = #df.global.world.raws.inorganics |
| 35 | + for i = 0, mat_count - 1 do |
| 36 | + local melt_frac = smelter.melt_remainder[i] |
| 37 | + if melt_frac > 0 then |
| 38 | + fractions[i] = melt_frac |
| 39 | + end |
| 40 | + end |
| 41 | + return fractions |
| 42 | +end |
| 43 | + |
| 44 | +-- lua doesn't hoist functions nerd emoji |
| 45 | +local function popup_full_list() |
| 46 | + local workshop = dfhack.gui.getSelectedBuilding(true) |
| 47 | + if not workshop then return end |
| 48 | + local rems = get_melt_remainders(workshop) |
| 49 | + if not rems then return end |
| 50 | + |
| 51 | + printall(rems) |
| 52 | + local lines = {} |
| 53 | + for mat_id, tenths in pairs(rems) do |
| 54 | + local mat_name = df.global.world.raws.inorganics[mat_id].id |
| 55 | + table.insert(lines, mat_name .. ": " .. (tenths * 10) .. "%\n") |
| 56 | + end |
| 57 | + if #lines == 0 then |
| 58 | + table.insert(lines, "<There were no melt remainders>") |
| 59 | + end |
| 60 | + dialogs.DialogScreen{ |
| 61 | + title = "Melt Remainders", |
| 62 | + message_label_attrs = { text = lines }, |
| 63 | + }:show():raise() |
| 64 | +end |
| 65 | + |
| 66 | +MeltRemainderOverlay = defclass(MeltRemainderOverlay, overlay.OverlayWidget) |
| 67 | +MeltRemainderOverlay.ATTRS = { |
| 68 | + desc = "Displays the fractions of a complete bar 'stored' in the smelter by melting", |
| 69 | + default_pos = { x = -39, y = 41 }, |
| 70 | + version = 1, |
| 71 | + default_enabled = true, |
| 72 | + viewscreens = { |
| 73 | + 'dwarfmode/ViewSheets/BUILDING/Furnace/Smelter/Tasks', |
| 74 | + 'dwarfmode/ViewSheets/BUILDING/Furnace/MagmaSmelter/Tasks', |
| 75 | + }, |
| 76 | + frame = { w = 58, h = 1 }, |
| 77 | + visible = function() return ENABLED end, |
| 78 | +} |
| 79 | + |
| 80 | +function MeltRemainderOverlay:init() |
| 81 | + self:addviews { |
| 82 | + widgets.Label{ |
| 83 | + view_id = "the_label", |
| 84 | + text = "<loading...>", |
| 85 | + on_click = popup_full_list, |
| 86 | + } |
| 87 | + } |
| 88 | +end |
| 89 | + |
| 90 | +function MeltRemainderOverlay:onRenderBody(painter) |
| 91 | + local workshop = dfhack.gui.getSelectedBuilding(true) |
| 92 | + if not workshop then return end |
| 93 | + local rems = get_melt_remainders(workshop) |
| 94 | + if not rems then return end |
| 95 | + |
| 96 | + local count = i_hate_lua(rems) |
| 97 | + if count == 0 then |
| 98 | + self.subviews.the_label:setText("No melt remainders.") |
| 99 | + elseif count == 1 then |
| 100 | + -- Singleton material |
| 101 | + local mat_id, tenths = next(rems) |
| 102 | + local mat_name = df.global.world.raws.inorganics[mat_id].id |
| 103 | + self.subviews.the_label:setText("Melting " .. mat_name .. ": " .. (tenths * 10) .. "%") |
| 104 | + else |
| 105 | + self.subviews.the_label:setText(count .. " melt remainders...") |
| 106 | + end |
| 107 | +end |
| 108 | + |
| 109 | +OVERLAY_WIDGETS = { melt_remainder = MeltRemainderOverlay } |
| 110 | + |
| 111 | +function isEnabled() |
| 112 | + return ENABLED |
| 113 | +end |
| 114 | +if dfhack_flags.enable then |
| 115 | + ENABLED = dfhack_flags.enable_state |
| 116 | + return |
| 117 | +end |
| 118 | + |
| 119 | +print("gui/melt-remainder is " .. (ENABLED and "enabled" or "disabled") .. ".") |
0 commit comments