Math help: dynamic throttle based on #players

Place to get help with not working mods / modding interface.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Math help: dynamic throttle based on #players

Post by aubergine18 »

I'd like to try and throttle a frivolous mod based on number of players in game. So when there are very few players, I don't want the mod throttled, but as more players join I want to throttle the mod so it doesn't cause message spam.

I can easily determine the number of players: #game.players

But I'm too useless at math to think of a decent, non-desyncable way to calculate the throttle.

The throttle calculation would only be performed when a player dies, and even then if that death is not ignored due to throttling.

The resulting value will be added to game.tick to determine when the event can next be processed, sort of like this:

Code: Select all

local delay = 60 -- ticks
local threshold = 0

function handle_event( event )
  if event.tick < threshold then return end

 -- something to calculate new `delay` value based on `#game.players`

  threshold = event.tick + delay

  -- handle event
end
I'd ideally want lower and upper bounds for the delay, eg. min 1 minute, max 10 minutes.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3752
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Math help: dynamic throttle based on #players

Post by DaveMcW »

aubergine18 wrote:non-desyncable
Store everything in global. Initialize it in script.on_init.


The math is pretty simple. Multiply the delay by number of players. Pick delay so that it works out to 1 minute for a low number of players and 10 minutes for a high number of players. If you want a hard cap of 1-10 minutes, do some if statements at the end.
Post Reply

Return to “Modding help”