Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 81 additions & 78 deletions luarules/gadgets/game_team_resources.lua
Original file line number Diff line number Diff line change
@@ -1,102 +1,105 @@
local gadget = gadget ---@type Gadget

function gadget:GetInfo()
return {
name = 'Team Resourcing',
desc = 'Sets up team resources',
author = 'Niobium',
date = 'May 2011',
license = 'GNU GPL, v2 or later',
layer = 1,
enabled = true
}
return {
name = 'Team Resourcing',
desc = 'Sets up team resources',
author = 'Niobium', -- Updated by Maxie12
date = 'May 2011', -- November 2025
license = 'GNU GPL, v2 or later',
layer = 1,
enabled = true
}
end

if not gadgetHandler:IsSyncedCode() then
return false
return false
end

local minStorageMetal = 1000
local minStorageEnergy = 1000
local mathMax = math.max


local function setup(addResources)
local function GetTeamPlayerCounts()
local teamPlayerCounts = {}
local playerList = Spring.GetPlayerList()
for i = 1, #playerList do
local playerID = playerList[i]
local _, _, isSpec, teamID = Spring.GetPlayerInfo(playerID, false)
if not isSpec then
teamPlayerCounts[teamID] = (teamPlayerCounts[teamID] or 0) + 1
end
end
return teamPlayerCounts
end

local startMetal = Spring.GetModOptions().startmetal
local startEnergy = Spring.GetModOptions().startenergy
local startMetalStorage = Spring.GetModOptions().startmetalstorage
local startEnergyStorage = Spring.GetModOptions().startenergystorage
local commanderMinMetal, commanderMinEnergy = 0, 0

if GG.coopMode then

local teamPlayerCounts = {}
local playerList = Spring.GetPlayerList()
for i = 1, #playerList do
local playerID = playerList[i]
local _, _, isSpec, teamID = Spring.GetPlayerInfo(playerID, false)
if not isSpec then
teamPlayerCounts[teamID] = (teamPlayerCounts[teamID] or 0) + 1
end
end

local teamList = Spring.GetTeamList()
for i = 1, #teamList do
local teamID = teamList[i]
local multiplier = teamPlayerCounts[teamID] or 1 -- Gaia has no players

-- Get the player's start unit to make sure staring storage is no less than its storage
local com = UnitDefs[Spring.GetTeamRulesParam(teamID, 'startUnit')]
if com then
commanderMinMetal = com.metalStorage or 0
commanderMinEnergy = com.energyStorage or 0
end

Spring.SetTeamResource(teamID, 'ms', mathMax(minStorageMetal, startMetalStorage * multiplier, startMetal * multiplier, commanderMinMetal))
Spring.SetTeamResource(teamID, 'es', mathMax(minStorageEnergy, startEnergyStorage * multiplier, startEnergy * multiplier, commanderMinEnergy))
if addResources then
Spring.SetTeamResource(teamID, 'm', startMetal * multiplier)
Spring.SetTeamResource(teamID, 'e', startEnergy * multiplier)
end
end
else
local teamList = Spring.GetTeamList()
for i = 1, #teamList do
local teamID = teamList[i]

-- Get the player's start unit to make sure staring storage is no less than its storage
local com = UnitDefs[Spring.GetTeamRulesParam(teamID, 'startUnit')]
if com then
commanderMinMetal = com.metalStorage or 0
commanderMinEnergy = com.energyStorage or 0
end

Spring.SetTeamResource(teamID, 'ms', mathMax(minStorageMetal, startMetalStorage, startMetal, commanderMinMetal))
Spring.SetTeamResource(teamID, 'es', mathMax(minStorageEnergy, startEnergyStorage, startEnergy, commanderMinEnergy))
if addResources then
Spring.SetTeamResource(teamID, 'm', startMetal)
Spring.SetTeamResource(teamID, 'e', startEnergy)
end
end
end
local function setup(addResources)
local startMetalStorage = Spring.GetModOptions().startmetalstorage
local startEnergyStorage = Spring.GetModOptions().startenergystorage
local startMetal = Spring.GetModOptions().startmetal
local startEnergy = Spring.GetModOptions().startenergy
local bonusMultiplierEnabled = Spring.GetModOptions().bonusstartresourcemultiplier;

local commanderMinMetal, commanderMinEnergy = 0, 0

-- Coop mode specific modification. Store amount of non-spectating players per team
local teamPlayerCounts = {}
if GG.coopMode then
teamPlayerCounts = GetTeamPlayerCounts()
end

local teamList = Spring.GetTeamList()
for i = 1, #teamList do
local teamID = teamList[i]

-- If coopmode is enabled, multiplier depending on player count.
local multiplier = 1
if GG.coopMode then
multiplier = teamPlayerCounts[teamID] or 1 -- Gaia has no players
end

--If starting bonus multiplication is enabled, multiply it.
local teamMultiplier = 1;
if (bonusMultiplierEnabled) then
teamMultiplier = select(7, Spring.GetTeamInfo(teamID));
end

-- Get starting resources and storage including any bonuses from mods
local startingMetal = startMetal * teamMultiplier * multiplier
local startingEnergy = startEnergy * teamMultiplier * multiplier
local startingMetalStorage = startMetalStorage * teamMultiplier * multiplier
local startingEnergyStorage = startEnergyStorage * teamMultiplier * multiplier

-- Get the player's start unit to make sure starting storage is no less than its storage
local com = UnitDefs[Spring.GetTeamRulesParam(teamID, 'startUnit')]
if com then
commanderMinMetal = com.metalStorage or 0
commanderMinEnergy = com.energyStorage or 0
end

Spring.SetTeamResource(teamID, 'ms', mathMax(minStorageMetal, startingMetalStorage, startingMetal, commanderMinMetal))
Spring.SetTeamResource(teamID, 'es', mathMax(minStorageEnergy, startingEnergyStorage, startingEnergy, commanderMinEnergy))
if addResources then
Spring.SetTeamResource(teamID, 'm', startingMetal)
Spring.SetTeamResource(teamID, 'e', startingEnergy)
end
end
end

function gadget:Initialize()
if Spring.GetGameFrame() > 0 then
return
end
setup(true)
if Spring.GetGameFrame() > 0 then
return
end
setup(true)
end

function gadget:GameStart()
-- reset because commander added additional storage as well
setup()
-- reset because commander added additional storage as well
setup()
end

function gadget:TeamDied(teamID)
Spring.SetTeamShareLevel(teamID, 'metal', 0)
Spring.SetTeamShareLevel(teamID, 'energy', 0)
Spring.SetTeamShareLevel(teamID, 'metal', 0)
Spring.SetTeamShareLevel(teamID, 'energy', 0)
end


11 changes: 10 additions & 1 deletion modoptions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1840,7 +1840,7 @@ Example: Armada VS Cortex VS Legion: 273 or 100 010 001 or 256 + 16 + 1]],
max = 10000,
step = 1,
},

{
key = "startmetalstorage",
name = "Starting Metal Storage",
Expand Down Expand Up @@ -1877,6 +1877,15 @@ Example: Armada VS Cortex VS Legion: 273 or 100 010 001 or 256 + 16 + 1]],
step = 1,
},

{
key = "bonusstartresourcemultiplier",
name = "Apply Bonus to Starting Resources",
desc = "If enabled, multiplies each players starting resources with their bonus.",
type = "bool",
section = "options_cheats",
def = false,
},

{
key = "sub_header",
section = "options_cheats",
Expand Down