From eada1296200de7e4b5d4354af43e62836a4ed3d0 Mon Sep 17 00:00:00 2001 From: hihoman23 Date: Fri, 11 Jul 2025 18:39:52 +0200 Subject: [PATCH 1/7] feat: select idle builders by distance to camera --- luaui/Widgets/gui_idle_builders.lua | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/luaui/Widgets/gui_idle_builders.lua b/luaui/Widgets/gui_idle_builders.lua index 45d48fef667..069d5b071c8 100644 --- a/luaui/Widgets/gui_idle_builders.lua +++ b/luaui/Widgets/gui_idle_builders.lua @@ -25,6 +25,8 @@ local setHeight = 0.046 local maxIcons = 9 local showRez = true local doUpdateForce = true +local justSelected = false +local listUpdated = {} -- listUpdated[unitDefID]: set to true when idle list is updated, set to false when idleList[unitDefID] is sorted local leftclick = 'LuaUI/Sounds/buildbar_add.wav' local rightclick = 'LuaUI/Sounds/buildbar_click.wav' @@ -295,6 +297,7 @@ local function updateList(force) idleList = {} local queue for unitID, unitDefID in pairs(unitList) do + listUpdated[unitDefID] = true queue = unitConf[unitDefID] and spGetFactoryCommands(unitID, 0) or spGetUnitCommandCount(unitID, 0) if queue == 0 then if spValidUnitID(unitID) and not spGetUnitIsDead(unitID) and not spGetUnitIsBeingBuilt(unitID) then @@ -732,12 +735,23 @@ function widget:MousePress(x, y, button) if clicks[unitDefID] then clicks[unitDefID] = clicks[unitDefID] + 1 else - clicks[unitDefID] = 1 + clicks[unitDefID] = 0 end num = (clicks[unitDefID]) % (#idleList[unitDefID]) + 1 end + if listUpdated[unitDefID] then + local camX, _, camZ = Spring.GetCameraPosition() + Spring.Echo("sort") + table.sort(idleList[unitDefID], function (a, b) + local unitAX, _, unitAZ = Spring.GetUnitPosition(a) + local unitBX, _, unitBZ = Spring.GetUnitPosition(b) + return math.distance2d(unitAX, unitAZ, camX, camZ) < math.distance2d(unitBX, unitBZ, camX, camZ) + end) + listUpdated[unitDefID] = false + end units = { idleList[unitDefID][num] } end + justSelected = true Spring.SelectUnitArray(units) end if button == 3 then @@ -756,6 +770,16 @@ end function widget:SelectionChanged(sel) selectedUnits = sel or {} + if justSelected then + justSelected = false + return + end + for unitDefID, _ in pairs(idleList) do + listUpdated[unitDefID] = true + end + for unitDefID, _ in pairs(clicks) do + clicks[unitDefID] = -1 + end end From 5f5d60e88ddafa52677378ca442c960d62ce02ad Mon Sep 17 00:00:00 2001 From: hihoman23 Date: Thu, 24 Jul 2025 15:20:11 +0200 Subject: [PATCH 2/7] apply suggestions --- luaui/Widgets/gui_idle_builders.lua | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/luaui/Widgets/gui_idle_builders.lua b/luaui/Widgets/gui_idle_builders.lua index 069d5b071c8..73d90c06bcf 100644 --- a/luaui/Widgets/gui_idle_builders.lua +++ b/luaui/Widgets/gui_idle_builders.lua @@ -26,7 +26,7 @@ local maxIcons = 9 local showRez = true local doUpdateForce = true local justSelected = false -local listUpdated = {} -- listUpdated[unitDefID]: set to true when idle list is updated, set to false when idleList[unitDefID] is sorted +local listSorted = {} -- listUpdated[unitDefID]: set to true when idle list is updated, set to false when idleList[unitDefID] is sorted local leftclick = 'LuaUI/Sounds/buildbar_add.wav' local rightclick = 'LuaUI/Sounds/buildbar_click.wav' @@ -44,12 +44,14 @@ local spGetMouseState = Spring.GetMouseState local spGetUnitCommandCount = Spring.GetUnitCommandCount local spGetFactoryCommands = Spring.GetFactoryCommands local myTeamID = Spring.GetMyTeamID() +local spGetUnitPosition = Spring.GetUnitPosition local floor = math.floor local ceil = math.ceil local min = math.min local max = math.max local math_isInRect = math.isInRect +local math_distance2dSquared = math.distance2dSquared local GL_SRC_ALPHA = GL.SRC_ALPHA local GL_ONE = GL.ONE @@ -296,8 +298,8 @@ local function updateList(force) local prevIdleList = idleList idleList = {} local queue + listSorted = {} for unitID, unitDefID in pairs(unitList) do - listUpdated[unitDefID] = true queue = unitConf[unitDefID] and spGetFactoryCommands(unitID, 0) or spGetUnitCommandCount(unitID, 0) if queue == 0 then if spValidUnitID(unitID) and not spGetUnitIsDead(unitID) and not spGetUnitIsBeingBuilt(unitID) then @@ -739,15 +741,14 @@ function widget:MousePress(x, y, button) end num = (clicks[unitDefID]) % (#idleList[unitDefID]) + 1 end - if listUpdated[unitDefID] then + if not listSorted[unitDefID] then local camX, _, camZ = Spring.GetCameraPosition() - Spring.Echo("sort") table.sort(idleList[unitDefID], function (a, b) - local unitAX, _, unitAZ = Spring.GetUnitPosition(a) - local unitBX, _, unitBZ = Spring.GetUnitPosition(b) - return math.distance2d(unitAX, unitAZ, camX, camZ) < math.distance2d(unitBX, unitBZ, camX, camZ) + local unitAX, _, unitAZ = spGetUnitPosition(a) + local unitBX, _, unitBZ = spGetUnitPosition(b) + return math_distance2dSquared(unitAX, unitAZ, camX, camZ) < math_distance2dSquared(unitBX, unitBZ, camX, camZ) end) - listUpdated[unitDefID] = false + listSorted[unitDefID] = true end units = { idleList[unitDefID][num] } end @@ -774,12 +775,8 @@ function widget:SelectionChanged(sel) justSelected = false return end - for unitDefID, _ in pairs(idleList) do - listUpdated[unitDefID] = true - end - for unitDefID, _ in pairs(clicks) do - clicks[unitDefID] = -1 - end + listSorted = {} + clicks = {} end From b633b913ee8c00fbef8a52c601c718623d414767 Mon Sep 17 00:00:00 2001 From: hihoman23 <78002940+hihoman23@users.noreply.github.com> Date: Sun, 27 Jul 2025 14:26:35 +0200 Subject: [PATCH 3/7] Fix comment --- luaui/Widgets/gui_idle_builders.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/luaui/Widgets/gui_idle_builders.lua b/luaui/Widgets/gui_idle_builders.lua index 73d90c06bcf..6c501ea5f84 100644 --- a/luaui/Widgets/gui_idle_builders.lua +++ b/luaui/Widgets/gui_idle_builders.lua @@ -26,7 +26,7 @@ local maxIcons = 9 local showRez = true local doUpdateForce = true local justSelected = false -local listSorted = {} -- listUpdated[unitDefID]: set to true when idle list is updated, set to false when idleList[unitDefID] is sorted +local listSorted = {} -- listSorted[unitDefID]: set to true when idle list is updated, set to false when idleList[unitDefID] is sorted local leftclick = 'LuaUI/Sounds/buildbar_add.wav' local rightclick = 'LuaUI/Sounds/buildbar_click.wav' From a40655fc1f2d32df556c27dcc38d48577e43c7fe Mon Sep 17 00:00:00 2001 From: hihoman23 <78002940+hihoman23@users.noreply.github.com> Date: Sun, 27 Jul 2025 14:33:31 +0200 Subject: [PATCH 4/7] Actually fix comment --- luaui/Widgets/gui_idle_builders.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/luaui/Widgets/gui_idle_builders.lua b/luaui/Widgets/gui_idle_builders.lua index 6c501ea5f84..bcf80a556f8 100644 --- a/luaui/Widgets/gui_idle_builders.lua +++ b/luaui/Widgets/gui_idle_builders.lua @@ -26,7 +26,7 @@ local maxIcons = 9 local showRez = true local doUpdateForce = true local justSelected = false -local listSorted = {} -- listSorted[unitDefID]: set to true when idle list is updated, set to false when idleList[unitDefID] is sorted +local listSorted = {} -- listSorted[unitDefID]: set to nil when idle list is updated, set to true when idleList[unitDefID] is sorted local leftclick = 'LuaUI/Sounds/buildbar_add.wav' local rightclick = 'LuaUI/Sounds/buildbar_click.wav' From 62a8dec8683474285b1ac23ccf93712488d05278 Mon Sep 17 00:00:00 2001 From: sprunk Date: Thu, 8 Jan 2026 12:08:25 +0100 Subject: [PATCH 5/7] Document a fixme --- luaui/Widgets/gui_idle_builders.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/luaui/Widgets/gui_idle_builders.lua b/luaui/Widgets/gui_idle_builders.lua index bcf80a556f8..e28a8a364bf 100644 --- a/luaui/Widgets/gui_idle_builders.lua +++ b/luaui/Widgets/gui_idle_builders.lua @@ -742,6 +742,7 @@ function widget:MousePress(x, y, button) num = (clicks[unitDefID]) % (#idleList[unitDefID]) + 1 end if not listSorted[unitDefID] then + -- FIXME: should use center of screen rather than camera local camX, _, camZ = Spring.GetCameraPosition() table.sort(idleList[unitDefID], function (a, b) local unitAX, _, unitAZ = spGetUnitPosition(a) From 4f5ba55f40e1b736a73b12c82be9f6b92779905b Mon Sep 17 00:00:00 2001 From: hihoman23 Date: Sun, 15 Mar 2026 18:28:55 +0100 Subject: [PATCH 6/7] add comment --- luaui/Widgets/gui_idle_builders.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/luaui/Widgets/gui_idle_builders.lua b/luaui/Widgets/gui_idle_builders.lua index e28a8a364bf..e6f33ac256c 100644 --- a/luaui/Widgets/gui_idle_builders.lua +++ b/luaui/Widgets/gui_idle_builders.lua @@ -772,7 +772,7 @@ end function widget:SelectionChanged(sel) selectedUnits = sel or {} - if justSelected then + if justSelected then -- ignore selections done by the widget itself justSelected = false return end From f2ac041b589ba480f652702296125ce0eb07d219 Mon Sep 17 00:00:00 2001 From: hihoman23 Date: Sun, 29 Mar 2026 19:50:04 +0200 Subject: [PATCH 7/7] fix moving camera --- luaui/Widgets/gui_idle_builders.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/luaui/Widgets/gui_idle_builders.lua b/luaui/Widgets/gui_idle_builders.lua index e6f33ac256c..6eb21f41e08 100644 --- a/luaui/Widgets/gui_idle_builders.lua +++ b/luaui/Widgets/gui_idle_builders.lua @@ -27,6 +27,7 @@ local showRez = true local doUpdateForce = true local justSelected = false local listSorted = {} -- listSorted[unitDefID]: set to nil when idle list is updated, set to true when idleList[unitDefID] is sorted +local prevCameraPosition -- {posX, posY, posZ}: camera position for sorting idle units, used to have consistent point of reference when right clicking local leftclick = 'LuaUI/Sounds/buildbar_add.wav' local rightclick = 'LuaUI/Sounds/buildbar_click.wav' @@ -743,7 +744,8 @@ function widget:MousePress(x, y, button) end if not listSorted[unitDefID] then -- FIXME: should use center of screen rather than camera - local camX, _, camZ = Spring.GetCameraPosition() + local camX, camY, camZ = unpack(prevCameraPosition or {Spring.GetCameraPosition()}) + prevCameraPosition = {camX, camY, camZ} table.sort(idleList[unitDefID], function (a, b) local unitAX, _, unitAZ = spGetUnitPosition(a) local unitBX, _, unitBZ = spGetUnitPosition(b) @@ -776,6 +778,7 @@ function widget:SelectionChanged(sel) justSelected = false return end + prevCameraPosition = nil listSorted = {} clicks = {} end