Skip to content

Commit

Permalink
Merge pull request #1096 from myk002/myk_quantum
Browse files Browse the repository at this point in the history
[gui/quantum] make minecart ordering/attaching optional
  • Loading branch information
myk002 authored Apr 30, 2024
2 parents 4c2ef9d + 1b289b9 commit d19bf7a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 27 deletions.
13 changes: 10 additions & 3 deletions assign-minecarts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ local function has_stops(route)
return #route.stops > 0
end

local function get_minecart(route)
if not has_minecart(route) then return end
local vehicle = utils.binsearch(df.global.world.vehicles.active, route.vehicle_ids[0], 'id')
if not vehicle then return end
return df.item.find(vehicle.item_id)
end

local function get_name(route)
return route.name and #route.name > 0 and route.name or ('Route '..route.id)
end
Expand All @@ -31,7 +38,7 @@ end

local function assign_minecart_to_route(route, quiet, minecart)
if has_minecart(route) then
return true
return get_minecart(route)
end
if not has_stops(route) then
if not quiet then
Expand All @@ -57,11 +64,11 @@ local function assign_minecart_to_route(route, quiet, minecart)
print(('Assigned a minecart to route %s.')
:format(get_id_and_name(route)))
end
return true
return df.item.find(minecart.item_id)
end

-- assign first free minecart to the most recently-created route
-- returns whether route now has a minecart assigned
-- returns assigned minecart (or nil if assignment failed)
function assign_minecart_to_last_route(quiet)
local routes = df.global.plotinfo.hauling.routes
local route_idx = #routes - 1
Expand Down
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Template for new versions:
## Fixes
- `modtools/create-item`: adjust for API changes, tolerate ``reaction-gloves`` tweak being active
- `quickfort`: don't designate multiple tiles of the same tree for chopping when applying a tree chopping blueprint to a multi-tile tree
- `gui/quantum`: fix processing when creating a quantum dump instead of a quantum stockpile

## Misc Improvements
- `gui/autobutcher`: add functionality to butcher and unbutcher all animals
Expand All @@ -44,6 +45,8 @@ Template for new versions:
- `gui/launcher`: add button for copying output to the system clipboard
- `deathcause`: give better responses for vermin remains
- `deathcause`: automatically choose a corpse when a pile of items is selected
- `gui/quantum`: add option for whether a minecart automatically gets ordered and/or attached
- `gui/quantum`: when attaching a minecart, show which minecart was attached

## Removed

Expand Down
104 changes: 80 additions & 24 deletions gui/quantum.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ local function create_quantum(pos, qsp_pos, feeder_id, name, trackstop_dir)
:format(pos.x, pos.y, pos.z))
end

data = get_quantumsp_data(name)
stats = quickfort.apply_blueprint{mode='place', pos=qsp_pos, data=data}
if stats.place_designated.value == 0 then
error(('failed to place stockpile at (%d, %d, %d)')
:format(qsp_pos.x, qsp_pos.y, qsp_pos.z))
if qsp_pos then
data = get_quantumsp_data(name)
stats = quickfort.apply_blueprint{mode='place', pos=qsp_pos, data=data}
if stats.place_designated.value == 0 then
error(('failed to place stockpile at (%d, %d, %d)')
:format(qsp_pos.x, qsp_pos.y, qsp_pos.z))
end
end
end

Expand All @@ -90,15 +92,13 @@ end
Quantum = defclass(Quantum, widgets.Window)
Quantum.ATTRS {
frame_title='Quantum',
frame={w=35, h=17, r=2, t=18},
frame={w=35, h=19, r=2, t=18},
autoarrange_subviews=true,
autoarrange_gap=1,
feeder=DEFAULT_NIL,
}

function Quantum:init()
local cart_count = #assign_minecarts.get_free_vehicles()

self:addviews{
widgets.WrappedLabel{
text_to_wrap=self:callback('get_help_text'),
Expand All @@ -109,7 +109,7 @@ function Quantum:init()
label_text='Name: ',
key='CUSTOM_CTRL_N',
key_sep=' ',
on_char=function(char, text) return #text < 12 end, -- TODO can be circumvented by pasting
on_char=function(_, text) return #text < 12 end, -- TODO can be circumvented by pasting
text='',
},
widgets.CycleHotkeyLabel{
Expand All @@ -129,14 +129,51 @@ function Quantum:init()
key='CUSTOM_CTRL_Z',
label='Create output pile:',
},
widgets.WrappedLabel{
text_to_wrap=('%d minecart%s available: %s will be %s'):format(
cart_count, cart_count == 1 and '' or 's',
cart_count == 1 and 'it' or 'one',
cart_count > 0 and 'automatically assigned to the quantum route'
or 'ordered via the manager for you to assign later'),
widgets.CycleHotkeyLabel{
view_id='minecart',
key='CUSTOM_CTRL_M',
label='Assign minecart:',
options={
{label='Auto', value='auto', pen=COLOR_GREEN},
{label='Order', value='order', pen=COLOR_YELLOW},
{label='Manual', value='manual', pen=COLOR_LIGHTRED},
},
},
widgets.Panel{
frame={h=4},
subviews={
widgets.WrappedLabel{
text_to_wrap=function() return ('%d minecart%s available: %s will be %s'):format(
self.cart_count, self.cart_count == 1 and '' or 's',
self.cart_count == 1 and 'it' or 'one',
self.cart_count > 0 and 'automatically assigned to the quantum route'
or 'ordered via the manager for you to assign to the quantum route later')
end,
visible=function() return self.subviews.minecart:getOptionValue() == 'auto' end,
},
widgets.WrappedLabel{
text_to_wrap=function() return ('%d minecart%s available: %s will be ordered for you to assign to the quantum route later'):format(
self.cart_count, self.cart_count == 1 and '' or 's',
self.cart_count >= 1 and 'an additional one' or 'one')
end,
visible=function() return self.subviews.minecart:getOptionValue() == 'order' end,
},
widgets.WrappedLabel{
text_to_wrap=function() return ('%d minecart%s available: please %s a minecart of your choice to the quantum route later'):format(
self.cart_count, self.cart_count == 1 and '' or 's',
self.cart_count == 0 and 'order and assign' or 'assign')
end,
visible=function() return self.subviews.minecart:getOptionValue() == 'manual' end,
},
},
},
}

self:refresh()
end

function Quantum:refresh()
self.cart_count = #assign_minecarts.get_free_vehicles()
end

function Quantum:get_help_text()
Expand Down Expand Up @@ -230,18 +267,35 @@ function Quantum:try_commit()
create_quantum(pos, qsp_pos, self.feeder.id, self.subviews.name.text,
self.subviews.dump_dir:getOptionLabel():sub(1,1))

local message = nil
if assign_minecarts.assign_minecart_to_last_route(true) then
message = 'An available minecart was assigned to your new' ..
' quantum stockpile. You\'re all done!'
else
local minecart, message = nil, nil
local minecart_option = self.subviews.minecart:getOptionValue()
if minecart_option == 'auto' then
minecart = assign_minecarts.assign_minecart_to_last_route(true)
if minecart then
message = 'An available minecart (' ..
dfhack.items.getReadableDescription(minecart) ..
') was assigned to your new' ..
' quantum stockpile. You\'re all done!'
else
message = 'There are no minecarts available to assign to the' ..
' quantum stockpile, but a manager order to produce' ..
' one was created for you. Once the minecart is' ..
' built, please add it to the quantum stockpile route' ..
' with the "assign-minecarts all" command or manually in' ..
' the (H)auling menu.'
end
end
if minecart_option == 'order' then
order_minecart(pos)
message = 'There are no minecarts available to assign to the' ..
' quantum stockpile, but a manager order to produce' ..
' one was created for you. Once the minecart is' ..
message = 'A manager order to produce a minecart has been' ..
' created for you. Once the minecart is' ..
' built, please add it to the quantum stockpile route' ..
' with the "assign-minecarts all" command or manually in' ..
' the (h)auling menu.'
' the (H)auling menu.'
end
if not message then
message = 'Please add a minecart of your choice to the quantum' ..
' stockpile route in the (H)auling menu.'
end
-- display a message box telling the user what we just did
dialogs.MessageBox{text=message:wrap(70)}:show()
Expand All @@ -260,6 +314,8 @@ function Quantum:onInput(keys)
end
elseif self.feeder then
self:try_commit()
self:refresh()
self:updateLayout()
end
end

Expand Down

0 comments on commit d19bf7a

Please sign in to comment.