Skip to content

Commit

Permalink
Add trackstop overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
robob27 committed Nov 6, 2023
1 parent 3683a1b commit 1fc3e1a
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions docs/trackstop.rst
Original file line number Diff line number Diff line change
@@ -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.
155 changes: 155 additions & 0 deletions trackstop.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
-- 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')

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()

if friction == 'None' then
building.friction = 10
elseif friction == 'Low' then
building.friction = 50
elseif friction == 'Medium' then
building.friction = 500
elseif friction == 'High' then
building.friction = 10000
elseif friction == 'Max' then
building.friction = 50000
end
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

if friction == 10 then
friction_cycle:setOption('None')
elseif friction == 50 then
friction_cycle:setOption('Low')
elseif friction == 500 then
friction_cycle:setOption('Medium')
elseif friction == 10000 then
friction_cycle:setOption('High')
elseif friction == 50000 then
friction_cycle:setOption('Max')
end

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

0 comments on commit 1fc3e1a

Please sign in to comment.