|
| 1 | +-- Overlay to allow changing track stop friction and dump direction after construction |
| 2 | +--@ module = true |
| 3 | + |
| 4 | +if not dfhack_flags.module then |
| 5 | + qerror('trackstop cannot be called directly') |
| 6 | +end |
| 7 | + |
| 8 | +local gui = require('gui') |
| 9 | +local widgets = require('gui.widgets') |
| 10 | +local overlay = require('plugins.overlay') |
| 11 | +local utils = require('utils') |
| 12 | + |
| 13 | +local NORTH = 'North ^' |
| 14 | +local EAST = 'East >' |
| 15 | +local SOUTH = 'South v' |
| 16 | +local WEST = 'West <' |
| 17 | + |
| 18 | +local LOW = 'Low' |
| 19 | +local MEDIUM = 'Medium' |
| 20 | +local HIGH = 'High' |
| 21 | +local HIGHER = 'Higher' |
| 22 | +local MAX = 'Max' |
| 23 | + |
| 24 | +local NONE = 'None' |
| 25 | + |
| 26 | +local FRICTION_MAP = { |
| 27 | + [NONE] = 10, |
| 28 | + [LOW] = 50, |
| 29 | + [MEDIUM] = 500, |
| 30 | + [HIGH] = 10000, |
| 31 | + [MAX] = 50000, |
| 32 | +} |
| 33 | + |
| 34 | +local FRICTION_MAP_REVERSE = utils.invert(FRICTION_MAP) |
| 35 | + |
| 36 | +local SPEED_MAP = { |
| 37 | + [LOW] = 10000, |
| 38 | + [MEDIUM] = 20000, |
| 39 | + [HIGH] = 30000, |
| 40 | + [HIGHER] = 40000, |
| 41 | + [MAX] = 50000, |
| 42 | +} |
| 43 | + |
| 44 | +local SPEED_MAP_REVERSE = utils.invert(SPEED_MAP) |
| 45 | + |
| 46 | +local DIRECTION_MAP = { |
| 47 | + [NORTH] = df.screw_pump_direction.FromSouth, |
| 48 | + [EAST] = df.screw_pump_direction.FromWest, |
| 49 | + [SOUTH] = df.screw_pump_direction.FromNorth, |
| 50 | + [WEST] = df.screw_pump_direction.FromEast, |
| 51 | +} |
| 52 | + |
| 53 | +local DIRECTION_MAP_REVERSE = utils.invert(DIRECTION_MAP) |
| 54 | + |
| 55 | +TrackStopOverlay = defclass(TrackStopOverlay, overlay.OverlayWidget) |
| 56 | +TrackStopOverlay.ATTRS{ |
| 57 | + default_pos={x=-73, y=29}, |
| 58 | + default_enabled=true, |
| 59 | + viewscreens='dwarfmode/ViewSheets/BUILDING/Trap/TrackStop', |
| 60 | + frame={w=25, h=4}, |
| 61 | + frame_style=gui.MEDIUM_FRAME, |
| 62 | + frame_background=gui.CLEAR_PEN, |
| 63 | +} |
| 64 | + |
| 65 | +function TrackStopOverlay:getFriction() |
| 66 | + return dfhack.gui.getSelectedBuilding().friction |
| 67 | +end |
| 68 | + |
| 69 | +function TrackStopOverlay:setFriction(friction) |
| 70 | + local building = dfhack.gui.getSelectedBuilding() |
| 71 | + |
| 72 | + building.friction = FRICTION_MAP[friction] |
| 73 | +end |
| 74 | + |
| 75 | +function TrackStopOverlay:getDumpDirection() |
| 76 | + local building = dfhack.gui.getSelectedBuilding() |
| 77 | + local use_dump = building.use_dump |
| 78 | + local dump_x_shift = building.dump_x_shift |
| 79 | + local dump_y_shift = building.dump_y_shift |
| 80 | + |
| 81 | + if use_dump == 0 then |
| 82 | + return NONE |
| 83 | + else |
| 84 | + if dump_x_shift == 0 and dump_y_shift == -1 then |
| 85 | + return NORTH |
| 86 | + elseif dump_x_shift == 1 and dump_y_shift == 0 then |
| 87 | + return EAST |
| 88 | + elseif dump_x_shift == 0 and dump_y_shift == 1 then |
| 89 | + return SOUTH |
| 90 | + elseif dump_x_shift == -1 and dump_y_shift == 0 then |
| 91 | + return WEST |
| 92 | + end |
| 93 | + end |
| 94 | +end |
| 95 | + |
| 96 | +function TrackStopOverlay:setDumpDirection(direction) |
| 97 | + local building = dfhack.gui.getSelectedBuilding() |
| 98 | + |
| 99 | + if direction == NONE then |
| 100 | + building.use_dump = 0 |
| 101 | + building.dump_x_shift = 0 |
| 102 | + building.dump_y_shift = 0 |
| 103 | + elseif direction == NORTH then |
| 104 | + building.use_dump = 1 |
| 105 | + building.dump_x_shift = 0 |
| 106 | + building.dump_y_shift = -1 |
| 107 | + elseif direction == EAST then |
| 108 | + building.use_dump = 1 |
| 109 | + building.dump_x_shift = 1 |
| 110 | + building.dump_y_shift = 0 |
| 111 | + elseif direction == SOUTH then |
| 112 | + building.use_dump = 1 |
| 113 | + building.dump_x_shift = 0 |
| 114 | + building.dump_y_shift = 1 |
| 115 | + elseif direction == WEST then |
| 116 | + building.use_dump = 1 |
| 117 | + building.dump_x_shift = -1 |
| 118 | + building.dump_y_shift = 0 |
| 119 | + end |
| 120 | +end |
| 121 | + |
| 122 | +function TrackStopOverlay:render(dc) |
| 123 | + local building = dfhack.gui.getSelectedBuilding() |
| 124 | + local friction = building.friction |
| 125 | + local friction_cycle = self.subviews.friction |
| 126 | + |
| 127 | + friction_cycle:setOption(FRICTION_MAP_REVERSE[friction]) |
| 128 | + |
| 129 | + self.subviews.dump_direction:setOption(self:getDumpDirection()) |
| 130 | + |
| 131 | + TrackStopOverlay.super.render(self, dc) |
| 132 | +end |
| 133 | + |
| 134 | +function TrackStopOverlay:init() |
| 135 | + self:addviews{ |
| 136 | + widgets.CycleHotkeyLabel{ |
| 137 | + frame={t=0, l=0}, |
| 138 | + label='Dump', |
| 139 | + key='CUSTOM_CTRL_X', |
| 140 | + options={ |
| 141 | + {label=NONE, value=NONE, pen=COLOR_BLUE}, |
| 142 | + NORTH, |
| 143 | + EAST, |
| 144 | + SOUTH, |
| 145 | + WEST, |
| 146 | + }, |
| 147 | + view_id='dump_direction', |
| 148 | + on_change=function(val) self:setDumpDirection(val) end, |
| 149 | + }, |
| 150 | + widgets.CycleHotkeyLabel{ |
| 151 | + label='Friction', |
| 152 | + frame={t=1, l=0}, |
| 153 | + key='CUSTOM_CTRL_F', |
| 154 | + options={ |
| 155 | + {label=NONE, value=NONE, pen=COLOR_BLUE}, |
| 156 | + {label=LOW, value=LOW, pen=COLOR_GREEN}, |
| 157 | + {label=MEDIUM, value=MEDIUM, pen=COLOR_YELLOW}, |
| 158 | + {label=HIGH, value=HIGH, pen=COLOR_LIGHTRED}, |
| 159 | + {label=MAX, value=MAX, pen=COLOR_RED}, |
| 160 | + }, |
| 161 | + view_id='friction', |
| 162 | + on_change=function(val) self:setFriction(val) end, |
| 163 | + }, |
| 164 | + } |
| 165 | +end |
| 166 | + |
| 167 | +RollerOverlay = defclass(RollerOverlay, overlay.OverlayWidget) |
| 168 | +RollerOverlay.ATTRS{ |
| 169 | + default_pos={x=-71, y=29}, |
| 170 | + default_enabled=true, |
| 171 | + viewscreens='dwarfmode/ViewSheets/BUILDING/Rollers', |
| 172 | + frame={w=27, h=4}, |
| 173 | + frame_style=gui.MEDIUM_FRAME, |
| 174 | + frame_background=gui.CLEAR_PEN, |
| 175 | +} |
| 176 | + |
| 177 | +function RollerOverlay:getDirection() |
| 178 | + local building = dfhack.gui.getSelectedBuilding() |
| 179 | + local direction = building.direction |
| 180 | + |
| 181 | + return DIRECTION_MAP_REVERSE[direction] |
| 182 | +end |
| 183 | + |
| 184 | +function RollerOverlay:setDirection(direction) |
| 185 | + local building = dfhack.gui.getSelectedBuilding() |
| 186 | + |
| 187 | + building.direction = DIRECTION_MAP[direction] |
| 188 | +end |
| 189 | + |
| 190 | +function RollerOverlay:getSpeed() |
| 191 | + local building = dfhack.gui.getSelectedBuilding() |
| 192 | + local speed = building.speed |
| 193 | + |
| 194 | + return SPEED_MAP_REVERSE[speed] |
| 195 | +end |
| 196 | + |
| 197 | +function RollerOverlay:setSpeed(speed) |
| 198 | + local building = dfhack.gui.getSelectedBuilding() |
| 199 | + |
| 200 | + building.speed = SPEED_MAP[speed] |
| 201 | +end |
| 202 | + |
| 203 | +function RollerOverlay:render(dc) |
| 204 | + local building = dfhack.gui.getSelectedBuilding() |
| 205 | + |
| 206 | + self.subviews.direction:setOption(DIRECTION_MAP_REVERSE[building.direction]) |
| 207 | + self.subviews.speed:setOption(SPEED_MAP_REVERSE[building.speed]) |
| 208 | + |
| 209 | + TrackStopOverlay.super.render(self, dc) |
| 210 | +end |
| 211 | + |
| 212 | +function RollerOverlay:init() |
| 213 | + self:addviews{ |
| 214 | + widgets.CycleHotkeyLabel{ |
| 215 | + label='Direction', |
| 216 | + frame={t=0, l=0}, |
| 217 | + key='CUSTOM_CTRL_X', |
| 218 | + options={NORTH, EAST, SOUTH, WEST}, |
| 219 | + view_id='direction', |
| 220 | + on_change=function(val) self:setDirection(val) end, |
| 221 | + }, |
| 222 | + widgets.CycleHotkeyLabel{ |
| 223 | + label='Speed', |
| 224 | + frame={t=1, l=0}, |
| 225 | + key='CUSTOM_CTRL_F', |
| 226 | + options={ |
| 227 | + {label=LOW, value=LOW, pen=COLOR_BLUE}, |
| 228 | + {label=MEDIUM, value=MEDIUM, pen=COLOR_GREEN}, |
| 229 | + {label=HIGH, value=HIGH, pen=COLOR_YELLOW}, |
| 230 | + {label=HIGHER, value=HIGHER, pen=COLOR_LIGHTRED}, |
| 231 | + {label=MAX, value=MAX, pen=COLOR_RED}, |
| 232 | + }, |
| 233 | + view_id='speed', |
| 234 | + on_change=function(val) self:setSpeed(val) end, |
| 235 | + }, |
| 236 | + } |
| 237 | +end |
| 238 | + |
| 239 | +OVERLAY_WIDGETS = { |
| 240 | + trackstop=TrackStopOverlay, |
| 241 | + rollers=RollerOverlay, |
| 242 | +} |
0 commit comments