z_mirror/StatsGui

git clone https://git.raiguard.me/z_mirror/StatsGui

Caleb HeuerRename global to storage1c9f69e

trunk
2.0 KiB56 linesraw
1local flib_format = require("__flib__.format")
2
3local constants = require("constants")
4
5-- Code based on "improved research queue" by sonaxaton
6-- https://github.com/dbeckwith/factorio-research-queue/blob/af6404ab696502a86eec40a4baf7fe00d0c714c7/control.lua#L316
7return function()
8  local strings = {}
9
10  for _, force in pairs(game.forces) do
11    local tech = force.current_research
12    if tech then
13      -- Retrieve or create progress samples table
14      local progress_samples = storage.research_progress_samples[force.index]
15      if not progress_samples then
16        progress_samples = {}
17        storage.research_progress_samples[force.index] = progress_samples
18      end
19      progress_samples[#progress_samples + 1] = { tech = tech.name, progress = force.research_progress }
20      if #progress_samples > constants.research_progress_samples_count then
21        table.remove(progress_samples, 1)
22      end
23
24      local estimated_ticks = 0
25      local num_samples = 0
26      if #progress_samples > 1 then
27        for i = 2, #progress_samples do
28          local previous_sample = progress_samples[i - 1]
29          local current_sample = progress_samples[i]
30          if previous_sample.tech == current_sample.tech then
31            -- How much the progress increased per tick
32            local speed = (current_sample.progress - previous_sample.progress) / 60
33            -- Don't add if the speed is negative for whatever reason
34            if speed > 0 then
35              -- How many ticks left until the research is finished
36              estimated_ticks = estimated_ticks + ((1 - current_sample.progress) / speed)
37              num_samples = num_samples + 1
38            end
39          end
40        end
41        -- Rolling average
42        if num_samples > 0 then
43          estimated_ticks = estimated_ticks / num_samples
44        end
45      end
46
47      if num_samples > 0 then
48        strings[force.index] = flib_format.time(estimated_ticks)
49      else
50        strings[force.index] = "∞"
51      end
52    end
53  end
54
55  storage.research_progress_strings = strings
56end