Skip to content

Commit

Permalink
Allow Hotbuild to redirect orders to mobile factories (#5656)
Browse files Browse the repository at this point in the history
  • Loading branch information
gordenwunderlich committed Jun 18, 2024
1 parent a7ccd80 commit c20eac9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
56 changes: 45 additions & 11 deletions lua/keymap/hotbuild.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ local modifiersKeys = {}
local worldview = import("/lua/ui/game/worldview.lua").viewLeft
local oldHandleEvent = worldview.HandleEvent

---@param selection UserUnit[]
---@return UserUnit[] | nil
function TranslateExFacUnits(selection)
local exFacs = EntityCategoryFilterDown(categories.EXTERNALFACTORY, selection) -- get all selected units with a factory attachment
if not table.empty(exFacs) then
---@type UserUnit[]
local nonExFacs = EntityCategoryFilterOut(categories.EXTERNALFACTORY, selection) -- get all selected Units without a factory attachment
for _, exFac in exFacs do
table.insert(nonExFacs, exFac:GetCreator()) -- for each unit with a factory attachment add the attachment to the list of factories
end

-- in case we've somehow selected both the platform and the factory, only put the fac in once
return table.unique(nonExFacs)
end

return nil
end

function initCycleButtons(values)
local buttonH = 48
Expand Down Expand Up @@ -270,8 +287,8 @@ end
--- A 'hack' to allow us to detect whether the `ButtonRelease` event was from a left-click
factoryHotkeyLastClickWasLeft = false

function factoryHotkey(units, count)
CommandMode.StartCommandMode("build", { name = '' })
function factoryHotkey(units, count, selection, exFacUnits)
CommandMode.StartCommandMode("build", {name = ''})

-- Another 'hack' that is, uuhh - a hack. Override the event handle of the world view. If it doesn't
-- return false then the event is captured and the engine ignores it (no orders are issued when clicking, etc)
Expand All @@ -283,14 +300,22 @@ function factoryHotkey(units, count)
if IsKeyDown("Shift") then
count = 5
end
IssueBlueprintCommand("UNITCOMMAND_BuildFactory", units, count)
if exFacUnits then
IssueBlueprintCommandToUnits(exFacUnits, "UNITCOMMAND_BuildFactory", units, count)
else
IssueBlueprintCommand("UNITCOMMAND_BuildFactory", units, count)
end
count = 1
factoryHotkey(units, count)
factoryHotkey(units, count, selection, exFacUnits)
else
for _, unit in units do
local v = unit.id
local count = unit.count
IssueBlueprintCommand("UNITCOMMAND_BuildFactory", v, count)
if exFacUnits then
IssueBlueprintCommandToUnits(exFacUnits, "UNITCOMMAND_BuildFactory", v, count)
else
IssueBlueprintCommand("UNITCOMMAND_BuildFactory", v, count)
end
end
StopCycleMap(self, event)
end
Expand Down Expand Up @@ -423,15 +448,19 @@ function buildActionFactoryTemplate(modifier)

local template = effectiveTemplates[cyclePos]
local selectedTemplate = template.templateData

local filteredUnits = TranslateExFacUnits(selection)
if maxPos == 1 then
for _, units in selectedTemplate do
local v = units.id
local count = units.count
IssueBlueprintCommand("UNITCOMMAND_BuildFactory", v, count)
if filteredUnits then
IssueBlueprintCommandToUnits(filteredUnits, "UNITCOMMAND_BuildFactory", v, count)
else
IssueBlueprintCommand("UNITCOMMAND_BuildFactory", v, count)
end
end
else
factoryHotkey(selectedTemplate)
factoryHotkey(selectedTemplate, nil, selection, filteredUnits)
end
end

Expand Down Expand Up @@ -575,11 +604,16 @@ function buildActionUnit(name, modifier)

local unit = effectiveValues[cyclePos]

local filteredUnits = TranslateExFacUnits(selection)
if maxPos == 1 then
IssueBlueprintCommand("UNITCOMMAND_BuildFactory", unit, count)
if filteredUnits then
IssueBlueprintCommandToUnits(filteredUnits, "UNITCOMMAND_BuildFactory", unit, count)
else
IssueBlueprintCommand("UNITCOMMAND_BuildFactory", unit, count)
end
Construction.RefreshUI()
else
factoryHotkey(unit, count)
factoryHotkey(unit, count, selection, filteredUnits)
end
end

Expand Down Expand Up @@ -633,4 +667,4 @@ function buildActionUpgrade()
end
SelectUnits(selectedUnits)
return result
end
end
3 changes: 3 additions & 0 deletions lua/system/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@ function table.count(t, fn)
end

--- Returns a new table with unique values stored using numeric keys and it does not preserve keys of the original table
---@generic T, G
---@param t? table<T, G>
---@return table<T, G> | nil
function table.unique(t)
if not t then return end -- prevents looping over nil table
local unique = {}
Expand Down

0 comments on commit c20eac9

Please sign in to comment.