From 7d184a9ca2443ff5685c9e0a7b36604357157273 Mon Sep 17 00:00:00 2001 From: Robob27 Date: Sun, 5 Nov 2023 20:19:33 -0500 Subject: [PATCH 1/4] Add trackstop overlay --- changelog.txt | 1 + docs/trackstop.rst | 9 +++ trackstop.lua | 160 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 docs/trackstop.rst create mode 100644 trackstop.lua diff --git a/changelog.txt b/changelog.txt index c40e9e4ce5..79a8bcefca 100644 --- a/changelog.txt +++ b/changelog.txt @@ -28,6 +28,7 @@ Template for new versions: ## New Tools - `sync-windmills`: synchronize or randomize movement of active windmills +- `trackstop`: new overlay to allow changing track stop dump direction and friction after construction ## New Features - `gui/design`: show selected dimensions next to the mouse cursor when designating with vanilla tools, for example when painting a burrow or designating digging diff --git a/docs/trackstop.rst b/docs/trackstop.rst new file mode 100644 index 0000000000..cabaabb8f7 --- /dev/null +++ b/docs/trackstop.rst @@ -0,0 +1,9 @@ +trackstop +========= + +.. dfhack-tool:: + :summary: Overlay to allow changing track stop friction and dump direction after construction + :tags: fort gameplay buildings interface + +This script provides an overlay that is managed by the `overlay` framework. +The overlay allows the player to change the friction and dump direction of a selected track stop after it has been constructed. diff --git a/trackstop.lua b/trackstop.lua new file mode 100644 index 0000000000..b7b5ed0b1b --- /dev/null +++ b/trackstop.lua @@ -0,0 +1,160 @@ +-- Overlay to allow changing track stop friction and dump direction after construction +--@ module = true +local gui = require('gui') +local widgets = require('gui.widgets') +local overlay = require('plugins.overlay') + +local NORTH = 'North' +local EAST = 'East' +local SOUTH = 'South' +local WEST = 'West' + +local LOW = 'Low' +local MEDIUM = 'Medium' +local HIGH = 'High' +local MAX = 'Max' + +local NONE = 'None' + +local FRICTION_MAP = { + [NONE] = 10, + [LOW] = 50, + [MEDIUM] = 500, + [HIGH] = 10000, + [MAX] = 50000, +} + +local FRICTION_MAP_REVERSE = {} +for k, v in pairs(FRICTION_MAP) do + FRICTION_MAP_REVERSE[v] = k +end + +TrackStopOverlay = defclass(TrackStopOverlay, overlay.OverlayWidget) +TrackStopOverlay.ATTRS{ + default_pos={x=-71, y=29}, + default_enabled=true, + viewscreens='dwarfmode/ViewSheets/BUILDING/Trap', + frame={w=27, h=4}, + frame_style=gui.MEDIUM_FRAME, + frame_background=gui.CLEAR_PEN, +} + +function TrackStopOverlay:getFriction() + return dfhack.gui.getSelectedBuilding().friction +end + +function TrackStopOverlay:setFriction(friction) + local building = dfhack.gui.getSelectedBuilding() + + building.friction = FRICTION_MAP[friction] +end + +function TrackStopOverlay:getDumpDirection() + local building = dfhack.gui.getSelectedBuilding() + local use_dump = building.use_dump + local dump_x_shift = building.dump_x_shift + local dump_y_shift = building.dump_y_shift + + if use_dump == 0 then + return NONE + else + if dump_x_shift == 0 and dump_y_shift == -1 then + return NORTH + elseif dump_x_shift == 1 and dump_y_shift == 0 then + return EAST + elseif dump_x_shift == 0 and dump_y_shift == 1 then + return SOUTH + elseif dump_x_shift == -1 and dump_y_shift == 0 then + return WEST + end + end +end + +function TrackStopOverlay:setDumpDirection(direction) + local building = dfhack.gui.getSelectedBuilding() + + if direction == NONE then + building.use_dump = 0 + building.dump_x_shift = 0 + building.dump_y_shift = 0 + elseif direction == NORTH then + building.use_dump = 1 + building.dump_x_shift = 0 + building.dump_y_shift = -1 + elseif direction == EAST then + building.use_dump = 1 + building.dump_x_shift = 1 + building.dump_y_shift = 0 + elseif direction == SOUTH then + building.use_dump = 1 + building.dump_x_shift = 0 + building.dump_y_shift = 1 + elseif direction == WEST then + building.use_dump = 1 + building.dump_x_shift = -1 + building.dump_y_shift = 0 + end +end + +function TrackStopOverlay:render(dc) + if not self:shouldRender() then + return + end + + local building = dfhack.gui.getSelectedBuilding() + local friction = building.friction + local friction_cycle = self.subviews.friction + + friction_cycle:setOption(FRICTION_MAP_REVERSE[friction]) + + self.subviews.dump_direction:setOption(self:getDumpDirection()) + + TrackStopOverlay.super.render(self, dc) +end + +function TrackStopOverlay:shouldRender() + local building = dfhack.gui.getSelectedBuilding() + return building and building.trap_type == df.trap_type.TrackStop +end + +function TrackStopOverlay:onInput(keys) + if not self:shouldRender() then + return + end + TrackStopOverlay.super.onInput(self, keys) +end + +function TrackStopOverlay:init() + self:addviews{ + widgets.Label{ + frame={t=0, l=0}, + text='Dump', + }, + widgets.CycleHotkeyLabel{ + frame={t=0, l=9}, + key='CUSTOM_CTRL_X', + options={NONE, NORTH, EAST, SOUTH, WEST}, + view_id='dump_direction', + on_change=function(val) self:setDumpDirection(val) end, + }, + widgets.Label{ + frame={t=1, l=0}, + text='Friction', + }, + widgets.CycleHotkeyLabel{ + frame={t=1, l=9}, + key='CUSTOM_CTRL_F', + options={NONE, LOW, MEDIUM, HIGH, MAX}, + view_id='friction', + on_change=function(val) self:setFriction(val) end, + }, + } +end + +OVERLAY_WIDGETS = { + trackstop=TrackStopOverlay +} + +if not dfhack_flags.module then + main{...} +end From c7dd669a56416eec94a47b8c09ba7a6a89992330 Mon Sep 17 00:00:00 2001 From: Robob27 Date: Sun, 5 Nov 2023 22:04:04 -0500 Subject: [PATCH 2/4] Add rollers overlay --- changelog.txt | 2 +- docs/trackstop.rst | 7 ++-- trackstop.lua | 101 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 5 deletions(-) diff --git a/changelog.txt b/changelog.txt index 79a8bcefca..f7e41acd3b 100644 --- a/changelog.txt +++ b/changelog.txt @@ -28,7 +28,7 @@ Template for new versions: ## New Tools - `sync-windmills`: synchronize or randomize movement of active windmills -- `trackstop`: new overlay to allow changing track stop dump direction and friction after construction +- `trackstop`: new overlay to allow changing track stop dump direction and friction and roller direction and speed after construction ## New Features - `gui/design`: show selected dimensions next to the mouse cursor when designating with vanilla tools, for example when painting a burrow or designating digging diff --git a/docs/trackstop.rst b/docs/trackstop.rst index cabaabb8f7..d91bb01932 100644 --- a/docs/trackstop.rst +++ b/docs/trackstop.rst @@ -2,8 +2,9 @@ trackstop ========= .. dfhack-tool:: - :summary: Overlay to allow changing track stop friction and dump direction after construction + :summary: Overlay to allow changing track stop friction and dump direction and roller direction and speed after construction. :tags: fort gameplay buildings interface -This script provides an overlay that is managed by the `overlay` framework. -The overlay allows the player to change the friction and dump direction of a selected track stop after it has been constructed. +This script provides 2 overlays that are managed by the `overlay` framework. +The trackstop overlay allows the player to change the friction and dump direction of a selected track stop after it has been constructed. +The rollers overlay allows the player to change the roller direction and speed of a selected track stop after it has been constructed. diff --git a/trackstop.lua b/trackstop.lua index b7b5ed0b1b..03a2252857 100644 --- a/trackstop.lua +++ b/trackstop.lua @@ -12,6 +12,7 @@ local WEST = 'West' local LOW = 'Low' local MEDIUM = 'Medium' local HIGH = 'High' +local HIGHER = 'Higher' local MAX = 'Max' local NONE = 'None' @@ -29,6 +30,31 @@ for k, v in pairs(FRICTION_MAP) do FRICTION_MAP_REVERSE[v] = k end +local SPEED_MAP = { + [LOW] = 10000, + [MEDIUM] = 20000, + [HIGH] = 30000, + [HIGHER] = 40000, + [MAX] = 50000, +} + +local SPEED_MAP_REVERSE = {} +for k, v in pairs(SPEED_MAP) do + SPEED_MAP_REVERSE[v] = k +end + +local DIRECTION_MAP = { + [NORTH] = df.screw_pump_direction.FromSouth, + [EAST] = df.screw_pump_direction.FromWest, + [SOUTH] = df.screw_pump_direction.FromNorth, + [WEST] = df.screw_pump_direction.FromEast, +} + +local DIRECTION_MAP_REVERSE = {} +for k, v in pairs(DIRECTION_MAP) do + DIRECTION_MAP_REVERSE[v] = k +end + TrackStopOverlay = defclass(TrackStopOverlay, overlay.OverlayWidget) TrackStopOverlay.ATTRS{ default_pos={x=-71, y=29}, @@ -151,8 +177,81 @@ function TrackStopOverlay:init() } end +RollerOverlay = defclass(RollerOverlay, overlay.OverlayWidget) +RollerOverlay.ATTRS{ + default_pos={x=-71, y=29}, + default_enabled=true, + viewscreens='dwarfmode/ViewSheets/BUILDING/Rollers', + frame={w=27, h=4}, + frame_style=gui.MEDIUM_FRAME, + frame_background=gui.CLEAR_PEN, +} + +function RollerOverlay:getDirection() + local building = dfhack.gui.getSelectedBuilding() + local direction = building.direction + + return DIRECTION_MAP_REVERSE[direction] +end + +function RollerOverlay:setDirection(direction) + local building = dfhack.gui.getSelectedBuilding() + + building.direction = DIRECTION_MAP[direction] +end + +function RollerOverlay:getSpeed() + local building = dfhack.gui.getSelectedBuilding() + local speed = building.speed + + return SPEED_MAP_REVERSE[speed] +end + +function RollerOverlay:setSpeed(speed) + local building = dfhack.gui.getSelectedBuilding() + + building.speed = SPEED_MAP[speed] +end + +function RollerOverlay:render(dc) + local building = dfhack.gui.getSelectedBuilding() + + self.subviews.direction:setOption(DIRECTION_MAP_REVERSE[building.direction]) + self.subviews.speed:setOption(SPEED_MAP_REVERSE[building.speed]) + + TrackStopOverlay.super.render(self, dc) +end + +function RollerOverlay:init() + self:addviews{ + widgets.Label{ + frame={t=0, l=0}, + text='Direction', + }, + widgets.CycleHotkeyLabel{ + frame={t=0, l=10}, + key='CUSTOM_CTRL_X', + options={NORTH, EAST, SOUTH, WEST}, + view_id='direction', + on_change=function(val) self:setDirection(val) end, + }, + widgets.Label{ + frame={t=1, l=0}, + text='Speed', + }, + widgets.CycleHotkeyLabel{ + frame={t=1, l=10}, + key='CUSTOM_CTRL_F', + options={LOW, MEDIUM, HIGH, HIGHER, MAX}, + view_id='speed', + on_change=function(val) self:setSpeed(val) end, + }, + } +end + OVERLAY_WIDGETS = { - trackstop=TrackStopOverlay + trackstop=TrackStopOverlay, + rollers=RollerOverlay, } if not dfhack_flags.module then From eba5ea9dcf4727192e4c34def7a521b999c60e8c Mon Sep 17 00:00:00 2001 From: Robob27 Date: Mon, 6 Nov 2023 21:15:32 -0500 Subject: [PATCH 3/4] Feedback --- docs/trackstop.rst | 8 ++--- trackstop.lua | 89 +++++++++++++++++++++++----------------------- 2 files changed, 48 insertions(+), 49 deletions(-) diff --git a/docs/trackstop.rst b/docs/trackstop.rst index d91bb01932..88579b783f 100644 --- a/docs/trackstop.rst +++ b/docs/trackstop.rst @@ -2,9 +2,9 @@ trackstop ========= .. dfhack-tool:: - :summary: Overlay to allow changing track stop friction and dump direction and roller direction and speed after construction. - :tags: fort gameplay buildings interface + :summary: Add dynamic configuration options for track stops. + :tags: fort buildings interface -This script provides 2 overlays that are managed by the `overlay` framework. +This script provides 2 overlays that are managed by the `overlay` framework. The script does nothing when executed. The trackstop overlay allows the player to change the friction and dump direction of a selected track stop after it has been constructed. -The rollers overlay allows the player to change the roller direction and speed of a selected track stop after it has been constructed. +The rollers overlay allows the player to change the roller direction and speed of a selected roller after it has been constructed. diff --git a/trackstop.lua b/trackstop.lua index 03a2252857..4a68b3c35f 100644 --- a/trackstop.lua +++ b/trackstop.lua @@ -1,13 +1,19 @@ -- Overlay to allow changing track stop friction and dump direction after construction --@ module = true + +if not dfhack_flags.module then + qerror('trackstop cannot be called directly') +end + local gui = require('gui') local widgets = require('gui.widgets') local overlay = require('plugins.overlay') +local utils = require('utils') -local NORTH = 'North' -local EAST = 'East' -local SOUTH = 'South' -local WEST = 'West' +local NORTH = 'North ^' +local EAST = 'East >' +local SOUTH = 'South v' +local WEST = 'West <' local LOW = 'Low' local MEDIUM = 'Medium' @@ -25,10 +31,7 @@ local FRICTION_MAP = { [MAX] = 50000, } -local FRICTION_MAP_REVERSE = {} -for k, v in pairs(FRICTION_MAP) do - FRICTION_MAP_REVERSE[v] = k -end +local FRICTION_MAP_REVERSE = utils.invert(FRICTION_MAP) local SPEED_MAP = { [LOW] = 10000, @@ -38,10 +41,7 @@ local SPEED_MAP = { [MAX] = 50000, } -local SPEED_MAP_REVERSE = {} -for k, v in pairs(SPEED_MAP) do - SPEED_MAP_REVERSE[v] = k -end +local SPEED_MAP_REVERSE = utils.invert(SPEED_MAP) local DIRECTION_MAP = { [NORTH] = df.screw_pump_direction.FromSouth, @@ -50,17 +50,14 @@ local DIRECTION_MAP = { [WEST] = df.screw_pump_direction.FromEast, } -local DIRECTION_MAP_REVERSE = {} -for k, v in pairs(DIRECTION_MAP) do - DIRECTION_MAP_REVERSE[v] = k -end +local DIRECTION_MAP_REVERSE = utils.invert(DIRECTION_MAP) TrackStopOverlay = defclass(TrackStopOverlay, overlay.OverlayWidget) TrackStopOverlay.ATTRS{ - default_pos={x=-71, y=29}, + default_pos={x=-73, y=29}, default_enabled=true, viewscreens='dwarfmode/ViewSheets/BUILDING/Trap', - frame={w=27, h=4}, + frame={w=25, h=4}, frame_style=gui.MEDIUM_FRAME, frame_background=gui.CLEAR_PEN, } @@ -152,25 +149,31 @@ end function TrackStopOverlay:init() self:addviews{ - widgets.Label{ - frame={t=0, l=0}, - text='Dump', - }, widgets.CycleHotkeyLabel{ - frame={t=0, l=9}, + frame={t=0, l=0}, + label='Dump', key='CUSTOM_CTRL_X', - options={NONE, NORTH, EAST, SOUTH, WEST}, + options={ + {label=NONE, value=NONE, pen=COLOR_BLUE}, + NORTH, + EAST, + SOUTH, + WEST, + }, view_id='dump_direction', on_change=function(val) self:setDumpDirection(val) end, }, - widgets.Label{ - frame={t=1, l=0}, - text='Friction', - }, widgets.CycleHotkeyLabel{ - frame={t=1, l=9}, + label='Friction', + frame={t=1, l=0}, key='CUSTOM_CTRL_F', - options={NONE, LOW, MEDIUM, HIGH, MAX}, + options={ + {label=NONE, value=NONE, pen=COLOR_BLUE}, + {label=LOW, value=LOW, pen=COLOR_GREEN}, + {label=MEDIUM, value=MEDIUM, pen=COLOR_YELLOW}, + {label=HIGH, value=HIGH, pen=COLOR_LIGHTRED}, + {label=MAX, value=MAX, pen=COLOR_RED}, + }, view_id='friction', on_change=function(val) self:setFriction(val) end, }, @@ -224,25 +227,25 @@ end function RollerOverlay:init() self:addviews{ - widgets.Label{ - frame={t=0, l=0}, - text='Direction', - }, widgets.CycleHotkeyLabel{ - frame={t=0, l=10}, + label='Direction', + frame={t=0, l=0}, key='CUSTOM_CTRL_X', options={NORTH, EAST, SOUTH, WEST}, view_id='direction', on_change=function(val) self:setDirection(val) end, }, - widgets.Label{ - frame={t=1, l=0}, - text='Speed', - }, widgets.CycleHotkeyLabel{ - frame={t=1, l=10}, + label='Speed', + frame={t=1, l=0}, key='CUSTOM_CTRL_F', - options={LOW, MEDIUM, HIGH, HIGHER, MAX}, + options={ + {label=LOW, value=LOW, pen=COLOR_BLUE}, + {label=MEDIUM, value=MEDIUM, pen=COLOR_GREEN}, + {label=HIGH, value=HIGH, pen=COLOR_YELLOW}, + {label=HIGHER, value=HIGHER, pen=COLOR_LIGHTRED}, + {label=MAX, value=MAX, pen=COLOR_RED}, + }, view_id='speed', on_change=function(val) self:setSpeed(val) end, }, @@ -253,7 +256,3 @@ OVERLAY_WIDGETS = { trackstop=TrackStopOverlay, rollers=RollerOverlay, } - -if not dfhack_flags.module then - main{...} -end From ac3584b8e1a8b18a7c8727badd7d9a9093495de3 Mon Sep 17 00:00:00 2001 From: Robob27 Date: Mon, 6 Nov 2023 23:16:42 -0500 Subject: [PATCH 4/4] Remove shouldRender, use better focus string --- trackstop.lua | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/trackstop.lua b/trackstop.lua index 4a68b3c35f..6657209b08 100644 --- a/trackstop.lua +++ b/trackstop.lua @@ -56,7 +56,7 @@ TrackStopOverlay = defclass(TrackStopOverlay, overlay.OverlayWidget) TrackStopOverlay.ATTRS{ default_pos={x=-73, y=29}, default_enabled=true, - viewscreens='dwarfmode/ViewSheets/BUILDING/Trap', + viewscreens='dwarfmode/ViewSheets/BUILDING/Trap/TrackStop', frame={w=25, h=4}, frame_style=gui.MEDIUM_FRAME, frame_background=gui.CLEAR_PEN, @@ -120,10 +120,6 @@ function TrackStopOverlay:setDumpDirection(direction) end function TrackStopOverlay:render(dc) - if not self:shouldRender() then - return - end - local building = dfhack.gui.getSelectedBuilding() local friction = building.friction local friction_cycle = self.subviews.friction @@ -135,18 +131,6 @@ function TrackStopOverlay:render(dc) TrackStopOverlay.super.render(self, dc) end -function TrackStopOverlay:shouldRender() - local building = dfhack.gui.getSelectedBuilding() - return building and building.trap_type == df.trap_type.TrackStop -end - -function TrackStopOverlay:onInput(keys) - if not self:shouldRender() then - return - end - TrackStopOverlay.super.onInput(self, keys) -end - function TrackStopOverlay:init() self:addviews{ widgets.CycleHotkeyLabel{