Skip to content

Commit 231580a

Browse files
committed
Prevent NaNs from breaking site deposit counts
- stop them from being created - fix them if they did get created
1 parent 90ab309 commit 231580a

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

resmon.lua

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,10 @@ function resmon.smooth_clamp_diff(diff)
537537
return 0.1 * diff
538538
end
539539

540+
local function isnan(n)
541+
return type(n) == "number" and n ~= n
542+
end
543+
540544
---Update the site ore counts and depletion rate/time
541545
---@param site yarm_site
542546
function resmon.finish_deposit_count(site)
@@ -551,14 +555,21 @@ function resmon.finish_deposit_count(site)
551555
local delta_ore_since_last_update = site.last_modified_amount - site.amount
552556
if delta_ore_since_last_update ~= 0 then
553557
-- only store the amount and tick from last update if it actually changed
554-
site.last_modified_tick = site.last_ore_check --
555-
site.last_modified_amount = site.amount --
558+
site.last_modified_tick = site.last_ore_check
559+
site.last_modified_amount = site.amount
560+
end
561+
local delta_ore_since_last_change = (site.update_amount - site.last_modified_amount)
562+
local delta_ticks = game.tick - site.last_modified_tick
563+
if delta_ticks > 0 then
564+
-- we don't want a nan to gum up the works
565+
local new_ore_per_minute = (delta_ore_since_last_change * 3600 / delta_ticks)
566+
local diff_step = resmon.smooth_clamp_diff(new_ore_per_minute - site.scanned_ore_per_minute)
567+
site.scanned_ore_per_minute = site.scanned_ore_per_minute + diff_step
568+
end
569+
if isnan(site.scanned_ore_per_minute) then
570+
-- but if a nan happened to get in, fix it
571+
site.scanned_ore_per_minute = 0
556572
end
557-
local delta_ore_since_last_change = (site.update_amount - site.last_modified_amount) -- use final amount and tick to calculate
558-
local delta_ticks = game.tick - site.last_modified_tick --
559-
local new_ore_per_minute = (delta_ore_since_last_change * 3600 / delta_ticks) -- ease the per minute value over time
560-
local diff_step = resmon.smooth_clamp_diff(new_ore_per_minute - site.scanned_ore_per_minute) --
561-
site.scanned_ore_per_minute = site.scanned_ore_per_minute + diff_step --
562573
end
563574

564575
local entity_prototype = prototypes.entity[site.ore_type]

0 commit comments

Comments
 (0)