Skip to content

Add spAddTeamResourceStats, for post-game graphs#2608

Closed
sprunk wants to merge 1 commit into
masterfrom
add-spAddTeamResourceStats
Closed

Add spAddTeamResourceStats, for post-game graphs#2608
sprunk wants to merge 1 commit into
masterfrom
add-spAddTeamResourceStats

Conversation

@sprunk

@sprunk sprunk commented Oct 26, 2025

Copy link
Copy Markdown
Collaborator

Draft because untested

@sprunk sprunk added enhancement Feature request or improvement to existing behavior area: Lua API The Lua scripting API exposed to game mods, widgets, and gadgets labels Oct 26, 2025
Comment thread rts/Lua/LuaSyncedCtrl.cpp
*
* @function Spring.AddTeamResourceStats
* @param teamID integer
* @param amounts table

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should explain the format, do I need to introduce a type for this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, yeah.

Comment thread rts/Lua/LuaSyncedCtrl.cpp
continue;

using TS = TeamStatistics;
std::array <float TS::*, SResourcePack::MAX_RESOURCES> entries;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is annoying, but TeamStatistics is part of the replay/network protocol so I can't turn its members into SResourcePacks.

Comment thread rts/Lua/LuaSyncedCtrl.cpp
}

for (int i = 0; i < entries.size(); ++i) {
// Parse by resource index, like `produced = { 1.23, 45.6 }`

@sprunk sprunk Oct 26, 2025

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inside of this loop should probably end up as some sort of helper utility function eventually.

Edit: #2631

@Fx-Doo

Fx-Doo commented Oct 27, 2025

Copy link
Copy Markdown

I'll build locally. I already had my custom AdjustTeamResourceStats() there so just adapting to this function's expected args should be trivial.
Will give you feed back on any issue i come across.

@Fx-Doo

Fx-Doo commented Oct 30, 2025

Copy link
Copy Markdown

Looking at how stats work vs Spring.GetTeamResources()
We actually have an accumulator for everything besides excess, which then get dumped into:
resPrevPull, resPrevIncome, resPrevExpense, resPrevSent, resPrevReceived, resPrevExcess => These are used to show on resbar, and also dumped into currentstats later

If i understand it smoothly, they accumulate during TEAM_SLOWUPDATE_RATE
And get added to currentsStats.resPull, income, expense, sent, received, excess etc.

Excess doesn't need this in native engine because nothing is excessed between slowupdates

If instead of adding to the teamstatistics value, we added/removed from those accumulators
They would shift the statistics with the same values;
But those expenses, pulls etc would appear on resbar, and Spring.GetTeamResource values.

I dunno if that would be wanted.
Do we want our add/use functions to be completely ignored by engine stats and log them only for the purpose of the end game stats.
Or do we want the ability to add/remove resources AND log them as something that the engine understands
ie:
Spring.UseTeamResource(teamID, "metal", amount)
Spring.AddTeamResourceStats(teamID, {metal = {sent = amount}})

=> Should that be understood on engine side as:

TeamID has lost amount metal, game tells me this was a sharing process, i can account for amount metal sent in the previous slowUpdate from teamID, and update the teamstats as if this truely happened from a ShareTeamResource() process, Spring.GetTeamResources will show it happened
OR
TeamID has been removed amount metal -- no log
Game asks to add amount metal as metal sent to team stats -- no actual real time log in team resourcing Spring.GetTeamResources wont show it either

@Fx-Doo

Fx-Doo commented Oct 30, 2025

Copy link
Copy Markdown

Yup, definetly something i'd look into.
Doing ie this.

local function AddResourceAsUnitProduction(unitID, teamID, resType, amount)
Spring.AddteamResource(teamID, resType, amount) -- RAW, no log
Spring.AddUnitResourceStats(unitID, resType, statsType = "produced", amount) -- => maps to unit -> resourceMakeI.resType
Spring.AddTeamResourceStats(teamID, resType, statsType = "produced", amount)-- => maps to team->resIncome.resType
end

or ie something like

local function AddResourceAsTeamReceived(teamID, resType, amount)
Spring.AddTeamResource(teamID, resType, amount) -- RAW, no log
Spring.AddTeamResourceStats(teamID, resType, statsType = "received", amount) -- => maps to team -> resReceived.resType
end

4 functions:
AddTeamResource(teamID,resType, amount) => No capping, no checking if enough stored, no log, just adding to the res pool
UseTeamResource(teamID, resType, amount) => No cappinge etc
AddUnitResourceStats()
AddTeamResourceStats()

I think with that you could get rid of all other resource transfer related function (eventually reimplement them on game side if needed)

function Spring.ShareTeamResources(team1, team2, resType, amount) is just a combination of a raw resource manipulation + stats logging
Current UseTeamResources is just a combination of checking + clamping, raw resource manipulation + stats logging

and so on

@Fx-Doo

Fx-Doo commented Oct 30, 2025

Copy link
Copy Markdown

After setting an accumulator for resExcess, similar to other accumulators already used:
2b667c9

AdjustUnitResourceStats
https://github.com/Fx-Doo/spring/blob/adb7e087eb85ca6df63315f9e8d8fc9f1575b6bd/rts/Lua/LuaSyncedCtrl.cpp#L1270C1-L1303C2

AdjustTeamResourceStats
https://github.com/Fx-Doo/spring/blob/adb7e087eb85ca6df63315f9e8d8fc9f1575b6bd/rts/Lua/LuaSyncedCtrl.cpp#L1219C1-L1268C2

-> We write into the accumulators
They get added in currentStats later
And provide data that can be read from Spring.GetUnitResources//Spring.GetTeamResource instantaneously.

Games will be responsible for keeping consistency.
But as long as each Use/Add is logged with the corresponding stats adjustment, engine will keep following up by rendering the gamestats accurately

@sprunk

sprunk commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator Author

Probably superseded by #3032.

@sprunk sprunk closed this Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Lua API The Lua scripting API exposed to game mods, widgets, and gadgets enhancement Feature request or improvement to existing behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants