diff --git a/luaui/Widgets/gui_buildmenu.lua b/luaui/Widgets/gui_buildmenu.lua index 996a37ebb3e..de9db77c84c 100644 --- a/luaui/Widgets/gui_buildmenu.lua +++ b/luaui/Widgets/gui_buildmenu.lua @@ -80,7 +80,7 @@ local advplayerlistLeft = vsx * 0.8 local ui_opacity = tonumber(Spring.GetConfigFloat("ui_opacity", 0.7) or 0.6) local ui_scale = tonumber(Spring.GetConfigFloat("ui_scale", 1) or 1) -local units = VFS.Include("luaui/configs/gridmenu_categories.lua") +local units = VFS.Include("luaui/configs/unit_config.lua") local isSpec = Spring.GetSpectatingState() local myTeamID = Spring.GetMyTeamID() diff --git a/luaui/Widgets/gui_gridmenu.lua b/luaui/Widgets/gui_gridmenu.lua index 0271c567329..bee1abf3726 100644 --- a/luaui/Widgets/gui_gridmenu.lua +++ b/luaui/Widgets/gui_gridmenu.lua @@ -173,7 +173,7 @@ local buildOpts = {} local buildOptsCount local categories = {} local catRects = {} -local currentBuildCategory, currentCategoryIndex +local currentCategory, currentCategoryIndex local currentPage = 1 local pages = 1 local nextPageRect = Rect:new(0, 0, 0, 0) @@ -207,7 +207,8 @@ local ui_opacity, ui_scale local selectNextFrame, switchedCategory -local units = VFS.Include("luaui/configs/gridmenu_categories.lua") +local units = VFS.Include("luaui/configs/unit_config.lua") +local grid = VFS.Include("luaui/configs/gridmenu_config.lua") local showWaterUnits = false @@ -242,11 +243,11 @@ local function RefreshCommands() selectedBuilder = startDefID end - if currentBuildCategory then - gridPos = units.unitGridPos[selectedBuilder] and units.unitGridPos[selectedBuilder][currentCategoryIndex] - lHasUnitGrid = units.hasUnitGrid[selectedBuilder] -- Ensure if unit has static grid to not repeat unit on different category + if currentCategory then + gridPos = grid.unitGridPos[selectedBuilder] and grid.unitGridPos[selectedBuilder][currentCategoryIndex] + lHasUnitGrid = grid.hasUnitGrid[selectedBuilder] -- Ensure if unit has static grid to not repeat unit on different category elseif selectedFactory then - gridPos = units.unitGridPos[selectedFactory] + gridPos = grid.unitGridPos[selectedFactory] end uncategorizedBuildOpts = {} @@ -271,7 +272,7 @@ local function RefreshCommands() name = UnitDefs[udefid].name, params = {} } - elseif currentBuildCategory == nil or (units.unitCategories[udefid] == currentBuildCategory and not (lHasUnitGrid and lHasUnitGrid[udefid])) then + elseif currentCategory == nil or (grid.unitCategories[udefid] == currentCategory and not (lHasUnitGrid and lHasUnitGrid[udefid])) then Spring.Echo("uncategorized options") buildOptsCount = buildOptsCount + 1 buildOpts[udefid] = { @@ -307,7 +308,7 @@ local function RefreshCommands() Spring.Echo("categorized options") buildOptsCount = buildOptsCount + 1 buildOpts[cmd.id * -1] = activeCmdDescs[index] - elseif currentBuildCategory == nil or (units.unitCategories[cmd.id * -1] == currentBuildCategory and not (lHasUnitGrid and lHasUnitGrid[cmd.id * -1])) then + elseif currentCategory == nil or (grid.unitCategories[cmd.id * -1] == currentCategory and not (lHasUnitGrid and lHasUnitGrid[cmd.id * -1])) then Spring.Echo("uncategorized options") buildOptsCount = buildOptsCount + 1 buildOpts[cmd.id * -1] = activeCmdDescs[index] @@ -416,7 +417,7 @@ local function setPreGamestartDefID(uDefID) selBuildQueueDefID = uDefID WG['pregame-build'].setPreGamestartDefID(uDefID) if not uDefID then - currentBuildCategory = nil + currentCategory = nil currentCategoryIndex = nil doUpdate = true end @@ -430,7 +431,7 @@ local function gridmenuCategoryHandler(_, _, args) return end - if not selectedBuilder or (currentBuildCategory and hotkeyActions['1' .. cIndex]) then + if not selectedBuilder or (currentCategory and hotkeyActions['1' .. cIndex]) then return end @@ -438,7 +439,7 @@ local function gridmenuCategoryHandler(_, _, args) if alt or ctrl or meta then return end - currentBuildCategory = categories[cIndex] + currentCategory = categories[cIndex] currentCategoryIndex = cIndex switchedCategory = os.clock() doUpdate = true @@ -498,7 +499,7 @@ local function gridmenuKeyHandler(_, _, args, _, isRepeat) enqueueUnit(uDefID, opts) return true - elseif preGamestartPlayer and currentBuildCategory then + elseif preGamestartPlayer and currentCategory then if alt or ctrl or meta then return end if args[3] and args[3] == 'factory' then return false end @@ -507,7 +508,7 @@ local function gridmenuKeyHandler(_, _, args, _, isRepeat) doUpdate = true return true - elseif selectedBuilder and currentBuildCategory then + elseif selectedBuilder and currentCategory then if args[3] and args[3] == 'factory' then return false end if alt or ctrl or meta then return end @@ -528,7 +529,7 @@ function widget:CommandNotify(cmdID, _, cmdOpts) end if returnToCategoriesOnPick or not cmdOpts.shift then - currentBuildCategory = nil + currentCategory = nil doUpdate = true end end @@ -557,9 +558,9 @@ local function prevPageHandler() end local function gridmenuCategoriesHandler() - if not (selectedBuilder and currentBuildCategory) then return end + if not (selectedBuilder and currentCategory) then return end - currentBuildCategory = nil + currentCategory = nil currentCategoryIndex = nil doUpdate = true @@ -808,7 +809,7 @@ function widget:Update(dt) selectedBuilder = nil selectedFactory = nil - currentBuildCategory = nil + currentCategory = nil currentCategoryIndex = nil selectedBuilders = {} currentPage = 1 @@ -1025,7 +1026,7 @@ local function drawCell(id, usedZoom, cellColor, disabled) end -- hotkey draw - if cmd.hotkey and (selectedFactory or (selectedBuilder and currentBuildCategory)) then + if cmd.hotkey and (selectedFactory or (selectedBuilder and currentCategory)) then local hotkeyText = keyConfig.sanitizeKey(cmd.hotkey, currentLayout) local hotkeyFontSize = priceFontSize * 1.1 @@ -1108,7 +1109,7 @@ local function drawCategories() local rect = catRects[cat] local opts = { - highlight = (cat == currentBuildCategory), + highlight = (cat == currentCategory), hovered = (hoveredButton == rect:getId()), } @@ -1162,9 +1163,9 @@ local function drawGrid() local cellRectID = 0 local unitGrid if selectedFactory then - unitGrid = units.gridPosUnit[selectedFactory] + unitGrid = grid.gridPosUnit[selectedFactory] else - unitGrid = units.gridPosUnit[selectedBuilder] + unitGrid = grid.gridPosUnit[selectedBuilder] end local curCmd = currentPage > 1 and (numCellsPerPage * (currentPage - 1) - (buildOptsCount - uncategorizedBuildOptsCount) + 1) or 1 @@ -1195,7 +1196,7 @@ local function drawGrid() uDefID = uncategorizedBuildOpts[curCmd].id * -1 curCmd = curCmd + 1 end - elseif currentPage == 1 and currentBuildCategory and unitGrid and unitGrid[currentCategoryIndex .. arow .. coll] then + elseif currentPage == 1 and currentCategory and unitGrid and unitGrid[currentCategoryIndex .. arow .. coll] then uDefID = unitGrid[currentCategoryIndex .. arow .. coll] elseif uncategorizedBuildOpts[curCmd] then uDefID = uncategorizedBuildOpts[curCmd].id * -1 @@ -1599,7 +1600,7 @@ function widget:KeyRelease(key) if preGamestartPlayer then setPreGamestartDefID(nil) else - currentBuildCategory = nil + currentCategory = nil currentCategoryIndex = nil doUpdate = true end @@ -1624,7 +1625,7 @@ function widget:MousePress(x, y, button) if not disableInput then for cat, catRect in pairs(catRects) do if catRect:contains(x, y) then - currentBuildCategory = cat + currentCategory = cat switchedCategory = os.clock() Spring.PlaySoundFile(Cfgs.sound_queue_add, 0.75, 'ui') @@ -1663,7 +1664,7 @@ function widget:MousePress(x, y, button) return true end elseif selectedBuilder and button == 3 then - currentBuildCategory = nil + currentCategory = nil currentCategoryIndex = nil doUpdate = true end diff --git a/luaui/configs/gridmenu_config.lua b/luaui/configs/gridmenu_config.lua new file mode 100644 index 00000000000..afcaef7660e --- /dev/null +++ b/luaui/configs/gridmenu_config.lua @@ -0,0 +1,109 @@ + + +local configs = VFS.Include('luaui/configs/gridmenu_layouts.lua') +local labGrids = configs.LabGrids +local unitGrids = configs.UnitGrids + +local unitGridPos = { } +local gridPosUnit = { } +local hasUnitGrid = { } + +local unitCategories = {} + +local BUILDCAT_ECONOMY = "Economy" +local BUILDCAT_COMBAT = "Combat" +local BUILDCAT_UTILITY = "Utility" +local BUILDCAT_PRODUCTION = "Build" + +local categoryGroupMapping = { + energy = BUILDCAT_ECONOMY, + metal = BUILDCAT_ECONOMY, + builder = BUILDCAT_PRODUCTION, + buildert2 = BUILDCAT_PRODUCTION, + buildert3 = BUILDCAT_PRODUCTION, + buildert4 = BUILDCAT_PRODUCTION, + util = BUILDCAT_UTILITY, + weapon = BUILDCAT_COMBAT, + explo = BUILDCAT_COMBAT, + weaponaa = BUILDCAT_COMBAT, + weaponsub = BUILDCAT_COMBAT, + aa = BUILDCAT_COMBAT, + emp = BUILDCAT_COMBAT, + sub = BUILDCAT_COMBAT, + nuke = BUILDCAT_COMBAT, + antinuke = BUILDCAT_COMBAT, +} + +for uname, ugrid in pairs(unitGrids) do + local builder = UnitDefNames[uname] + local uid = builder.id + + unitGridPos[uid] = {{},{},{},{}} + gridPosUnit[uid] = {} + hasUnitGrid[uid] = {} + local uCanBuild = {} + + local uBuilds = builder.buildOptions + for i = 1, #uBuilds do + uCanBuild[uBuilds[i]] = true + end + + for cat=1,4 do + for r=1,3 do + for c=1,4 do + local ugdefname = ugrid[cat] and ugrid[cat][r] and ugrid[cat][r][c] + + if ugdefname then + local ugdef = UnitDefNames[ugdefname] + + if ugdef and ugdef.id and uCanBuild[ugdef.id] then + gridPosUnit[uid][cat .. r .. c] = ugdef.id + unitGridPos[uid][cat][ugdef.id] = cat .. r .. c + hasUnitGrid[uid][ugdef.id] = true + end + end + end + end + end +end + +for uname, ugrid in pairs(labGrids) do + local udef = UnitDefNames[uname] + local uid = udef.id + + unitGridPos[uid] = {} + gridPosUnit[uid] = {} + local uCanBuild = {} + + local uBuilds = udef.buildOptions + for i = 1, #uBuilds do + uCanBuild[uBuilds[i]] = true + end + + for r=1,3 do + for c=1,4 do + local index = (r - 1) * 4 + c + local ugdefname = ugrid[index] + + if ugdefname then + local ugdef = UnitDefNames[ugdefname] + + if ugdef and ugdef.id and uCanBuild[ugdef.id] then + gridPosUnit[uid][r .. c] = ugdef.id + unitGridPos[uid][ugdef.id] = r .. c + end + end + end + end +end + +for unitDefID, unitDef in pairs(UnitDefs) do + unitCategories[unitDefID] = categoryGroupMapping[unitDef.customParams.unitgroup] or BUILDCAT_UTILITY +end + +return { + unitGridPos = unitGridPos, + gridPosUnit = gridPosUnit, + hasUnitGrid = hasUnitGrid, + unitCategories = unitCategories, +} diff --git a/luaui/configs/gridmenu_categories.lua b/luaui/configs/unit_config.lua similarity index 51% rename from luaui/configs/gridmenu_categories.lua rename to luaui/configs/unit_config.lua index 2939bbd67cc..283f9d67695 100644 --- a/luaui/configs/gridmenu_categories.lua +++ b/luaui/configs/unit_config.lua @@ -4,10 +4,6 @@ --- -local configs = VFS.Include('luaui/configs/gridmenu_layouts.lua') -local labGrids = configs.LabGrids -local unitGrids = configs.UnitGrids - local unitName = {} local unitEnergyCost = {} local unitMetalCost = {} @@ -22,30 +18,50 @@ local isWaterUnit = {} local isGeothermal = {} local unitMaxWeaponRange = {} -local unitGridPos = { } -local gridPosUnit = { } -local hasUnitGrid = { } - -local unitCategories = {} - -local categoryGroupMapping = { - energy = BUILDCAT_ECONOMY, - metal = BUILDCAT_ECONOMY, - builder = BUILDCAT_PRODUCTION, - buildert2 = BUILDCAT_PRODUCTION, - buildert3 = BUILDCAT_PRODUCTION, - buildert4 = BUILDCAT_PRODUCTION, - util = BUILDCAT_UTILITY, - weapon = BUILDCAT_COMBAT, - explo = BUILDCAT_COMBAT, - weaponaa = BUILDCAT_COMBAT, - weaponsub = BUILDCAT_COMBAT, - aa = BUILDCAT_COMBAT, - emp = BUILDCAT_COMBAT, - sub = BUILDCAT_COMBAT, - nuke = BUILDCAT_COMBAT, - antinuke = BUILDCAT_COMBAT, -} +local showWaterUnits = false + +for unitDefID, unitDef in pairs(UnitDefs) do + + if unitDef.name == 'armdl' or unitDef.name == 'cordl' or unitDef.name == 'armlance' or unitDef.name == 'cortitan' -- or unitDef.name == 'armbeaver' or unitDef.name == 'cormuskrat' + or (unitDef.minWaterDepth > 0 or unitDef.modCategories['ship']) then + isWaterUnit[unitDefID] = true + end + if unitDef.name == 'armthovr' or unitDef.name == 'corintr' then + isWaterUnit[unitDefID] = nil + end + + if unitDef.needGeo then + isGeothermal[unitDefID] = true + end + + if unitDef.maxWeaponRange > 16 then + unitMaxWeaponRange[unitDefID] = unitDef.maxWeaponRange + end + + unitIconType[unitDefID] = unitDef.iconType + unitEnergyCost[unitDefID] = unitDef.energyCost + unitMetalCost[unitDefID] = unitDef.metalCost + + if unitDef.maxThisUnit == 0 then + unitRestricted[unitDefID] = true + end + + if unitDef.buildSpeed > 0 and unitDef.buildOptions[1] then + isBuilder[unitDefID] = unitDef.buildOptions + end + + if unitDef.isFactory and #unitDef.buildOptions > 0 then + isFactory[unitDefID] = true + end + + if unitDef.extractsMetal > 0 then + isMex[unitDefID] = true + end + + if unitDef.windGenerator > 0 then + isWind[unitDefID] = true + end +end local unbaStartBuildoptions = {} if Spring.GetModOptions().unba then @@ -70,116 +86,6 @@ else unbaStartBuildoptions = nil end -function processUnits() - for uname, ugrid in pairs(unitGrids) do - local builder = UnitDefNames[uname] - local uid = builder.id - - unitGridPos[uid] = {{},{},{},{}} - gridPosUnit[uid] = {} - hasUnitGrid[uid] = {} - local uCanBuild = {} - - local uBuilds = builder.buildOptions - for i = 1, #uBuilds do - uCanBuild[uBuilds[i]] = true - end - - for cat=1,4 do - for r=1,3 do - for c=1,4 do - local ugdefname = ugrid[cat] and ugrid[cat][r] and ugrid[cat][r][c] - - if ugdefname then - local ugdef = UnitDefNames[ugdefname] - - if ugdef and ugdef.id and uCanBuild[ugdef.id] then - gridPosUnit[uid][cat .. r .. c] = ugdef.id - unitGridPos[uid][cat][ugdef.id] = cat .. r .. c - hasUnitGrid[uid][ugdef.id] = true - end - end - end - end - end - end - - for uname, ugrid in pairs(labGrids) do - local udef = UnitDefNames[uname] - local uid = udef.id - - unitGridPos[uid] = {} - gridPosUnit[uid] = {} - local uCanBuild = {} - - local uBuilds = udef.buildOptions - for i = 1, #uBuilds do - uCanBuild[uBuilds[i]] = true - end - - for r=1,3 do - for c=1,4 do - local index = (r - 1) * 4 + c - local ugdefname = ugrid[index] - - if ugdefname then - local ugdef = UnitDefNames[ugdefname] - - if ugdef and ugdef.id and uCanBuild[ugdef.id] then - gridPosUnit[uid][r .. c] = ugdef.id - unitGridPos[uid][ugdef.id] = r .. c - end - end - end - end - end - - for unitDefID, unitDef in pairs(UnitDefs) do - unitName[unitDefID] = unitDef.name - unitGroup[unitDefID] = unitDef.customParams.unitgroup - unitCategories[unitDefID] = categoryGroupMapping[unitDef.customParams.unitgroup] or BUILDCAT_UTILITY - - if unitDef.name == 'armdl' or unitDef.name == 'cordl' or unitDef.name == 'armlance' or unitDef.name == 'cortitan' -- or unitDef.name == 'armbeaver' or unitDef.name == 'cormuskrat' - or (unitDef.minWaterDepth > 0 or unitDef.modCategories['ship']) then - isWaterUnit[unitDefID] = true - end - if unitDef.name == 'armthovr' or unitDef.name == 'corintr' then - isWaterUnit[unitDefID] = nil - end - - if unitDef.needGeo then - isGeothermal[unitDefID] = true - end - - if unitDef.maxWeaponRange > 16 then - unitMaxWeaponRange[unitDefID] = unitDef.maxWeaponRange - end - - unitIconType[unitDefID] = unitDef.iconType - unitEnergyCost[unitDefID] = unitDef.energyCost - unitMetalCost[unitDefID] = unitDef.metalCost - - if unitDef.maxThisUnit == 0 then - unitRestricted[unitDefID] = true - end - - if unitDef.buildSpeed > 0 and unitDef.buildOptions[1] then - isBuilder[unitDefID] = unitDef.buildOptions - end - - if unitDef.isFactory and #unitDef.buildOptions > 0 then - isFactory[unitDefID] = true - end - - if unitDef.extractsMetal > 0 then - isMex[unitDefID] = true - end - - if unitDef.windGenerator > 0 then - isWind[unitDefID] = true - end - end -end function restrictWindUnits(disable) for unitDefID,_ in pairs(isWind) do @@ -200,8 +106,6 @@ function restrictWaterUnits(disable) end -processUnits() - ------------------------------------ -- UNIT ORDER ---------------------- ------------------------------------ @@ -272,9 +176,6 @@ local minWaterUnitDepth = -11 ------------------------------------ return { - unitGrids = unitGrids, - labGrids = labGrids, - unbaStartBuildoptions = unbaStartBuildoptions, unitName = unitName, @@ -290,14 +191,12 @@ return { isWaterUnit = isWaterUnit, isGeothermal = isGeothermal, unitMaxWeaponRange = unitMaxWeaponRange, - unitGridPos = unitGridPos, - gridPosUnit = gridPosUnit, - hasUnitGrid = hasUnitGrid, - unitCategories = unitCategories, minWaterUnitDepth = minWaterUnitDepth, unitOrder = unitOrder, + showWaterUnits = showWaterUnits, + restrictGeothermalUnits = restrictGeothermalUnits, restrictWindUnits = restrictWindUnits, restrictWaterUnits = restrictWaterUnits,