Skip to content

Commit

Permalink
Clean up and add docs to unit_buildmenu_config.lua. (#3987)
Browse files Browse the repository at this point in the history
No functional changes. Removes some dead code, tidies and adds LuaLS compatible type annotations for unit_buildmenu_config.lua.
  • Loading branch information
rhys-vdw authored Dec 2, 2024
1 parent 20943cb commit e69f7b2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 47 deletions.
3 changes: 2 additions & 1 deletion luaui/configs/buildmenu_sorting.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---@type table<string, number>
local unitOrderTable = {
-- UNITS
--CONSTRUCTORS
Expand Down Expand Up @@ -769,14 +770,14 @@ local unitOrderTable = {
['coratl'] = 260600,
}

---@type table<string, number>
local newUnitOrder = {}
for id, value in pairs(unitOrderTable) do
if UnitDefNames[id] then
newUnitOrder[UnitDefNames[id].id] = value
end
end
unitOrderTable = newUnitOrder
newUnitOrder = nil

for unitDefID, unitDef in pairs(UnitDefs) do
if unitDef.customParams.isscavenger then
Expand Down
92 changes: 46 additions & 46 deletions luaui/configs/unit_buildmenu_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@
---


local unitEnergyCost = {}
local unitMetalCost = {}
local unitGroup = {}
local unitRestricted = {}
local manualUnitRestricted = {}
local isBuilder = {}
local isFactory = {}
local unitIconType = {}
local isMex = {}
local isWind = {}
local isWaterUnit = {}
local isGeothermal = {}
local unitMaxWeaponRange = {}

local showWaterUnits = false
local unitEnergyCost = {} ---@type table<number, number>
local unitMetalCost = {} ---@type table<number, number>
local unitGroup = {} ---@type table<number, number>
local unitRestricted = {} ---@type table<number, true>
local manualUnitRestricted = {} ---@type table<number, true>
local isBuilder = {} ---@type table<number, true>
local isFactory = {} ---@type table<number, true>
local unitIconType = {} ---@type table<number, number>
local isMex = {} ---@type table<number, true>
local isWind = {} ---@type table<number, true>
local isWaterUnit = {} ---@type table<number, true>
local isGeothermal = {} ---@type table<number, true>
local unitMaxWeaponRange = {} ---@type table<number, number>

for unitDefID, unitDef in pairs(UnitDefs) do

Expand Down Expand Up @@ -66,25 +64,29 @@ for unitDefID, unitDef in pairs(UnitDefs) do
end
end


---@param disable boolean
local function restrictWindUnits(disable)
for unitDefID,_ in pairs(isWind) do
unitRestricted[unitDefID] = manualUnitRestricted[unitDefID] or disable
end
end

---@param disable boolean
local function restrictGeothermalUnits(disable)
for unitDefID,_ in pairs(isGeothermal) do
unitRestricted[unitDefID] = manualUnitRestricted[unitDefID] or disable
end
end

---@param disable boolean
local function restrictWaterUnits(disable)
for unitDefID,_ in pairs(isWaterUnit) do
unitRestricted[unitDefID] = manualUnitRestricted[unitDefID] or disable
end
end

---Sets geothermal unit restriction based on the presence of geothermal
---features.
local function checkGeothermalFeatures()
local hideGeoUnits = true
local geoThermalFeatures = {}
Expand All @@ -108,10 +110,12 @@ end
-- UNIT ORDER ----------------------
------------------------------------

-- At the end of this 'UNIT ORDER' section, unitOrder is an array with unitIDs
-- sorted by their value specified in unitOrderManualOverrideTable. If no
-- value is specified, the unit will be placed at the end of the array.
---At the end of this 'UNIT ORDER' section, unitOrder is an array with unitIDs
---sorted by their value specified in unitOrderManualOverrideTable. If no
---value is specified, the unit will be placed at the end of the array.
---@type number[]
local unitOrder = {}

local unitOrderManualOverrideTable = VFS.Include("luaui/configs/buildmenu_sorting.lua")

-- Populate unitOrder with unit IDs.
Expand Down Expand Up @@ -139,49 +143,45 @@ maxOrder = maxOrder + 1
-- For units who have the same order value we compare the unit's IDs.
-- This sort is always stable, as no two units should have the same ID.
table.sort(unitOrder, function(aID, bID)
local aOrder = unitOrderManualOverrideTable[aID] or maxOrder
local bOrder = unitOrderManualOverrideTable[bID] or maxOrder

if (aOrder == bOrder) then
return aID < bID
end
return aOrder < bOrder
end)

local voidWater = false
local success, mapinfo = pcall(VFS.Include,"mapinfo.lua") -- load mapinfo.lua confs
if success and mapinfo then
voidWater = mapinfo.voidwater
end

local minWaterUnitDepth = -11
local aOrder = unitOrderManualOverrideTable[aID] or maxOrder
local bOrder = unitOrderManualOverrideTable[bID] or maxOrder

if (aOrder == bOrder) then
return aID < bID
end
return aOrder < bOrder
end)

------------------------------------
-- /UNIT ORDER ----------------------
------------------------------------

return {
local units = {
unitEnergyCost = unitEnergyCost,
unitMetalCost = unitMetalCost,
unitGroup = unitGroup,
unitRestricted = unitRestricted,
isBuilder = isBuilder,
isFactory = isFactory,
unitIconType = unitIconType,
unitMaxWeaponRange = unitMaxWeaponRange,
---Set of unit IDs that are factories.
isFactory = isFactory,
---Set of unit IDs that have build options.
isBuilder = isBuilder,
---Set of unit IDs that require metal.
isMex = isMex,
---Set of unit IDs that require wind.
isWind = isWind,
---Set of unit IDs that require water.
isWaterUnit = isWaterUnit,
---Set of unit IDs that require geothermal.
isGeothermal = isGeothermal,
unitMaxWeaponRange = unitMaxWeaponRange,

minWaterUnitDepth = minWaterUnitDepth,
minWaterUnitDepth = -11,
---An array with unitIDs sorted by their value specified in
---`unitOrderManualOverrideTable`. If no value is specified, the unit will be
---placed at the end of the array.
unitOrder = unitOrder,

showWaterUnits = showWaterUnits,

checkGeothermalFeatures = checkGeothermalFeatures,
restrictGeothermalUnits = restrictGeothermalUnits,
restrictWindUnits = restrictWindUnits,
restrictWaterUnits = restrictWaterUnits,
}

return units

0 comments on commit e69f7b2

Please sign in to comment.