Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
perf: remove the periodic ghost receiver cleanup
  • Loading branch information
KoharaKazuya committed Dec 20, 2020
1 parent 859b57d commit 3dfc365
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
4 changes: 3 additions & 1 deletion changelog.txt
@@ -1,10 +1,12 @@
---------------------------------------------------------------------------------------------------
Version: 1.2.0
Date: 2020.12.12
Date: 2020.12.20
Features:
- More mods compatibility. Battery Locomotive has come to handle `script_raised_built`, `script_raised_destroy` and `on_entity_cloned` events.
Bugfixes:
- Fixed a bug that caused a crash when an invalid BatteryLocomotive receiver entity was created for some reason.
Optimisations:
- Reduced the load by removing the internal processing that was being executed periodically.
Modding:
- Added remote interface to add a new battery locomotive type.
---------------------------------------------------------------------------------------------------
Expand Down
20 changes: 7 additions & 13 deletions control.lua
Expand Up @@ -53,6 +53,12 @@ local function remove_all_invalid_receivers()
end
end

local function remove_uncontrolled_receiver(entity)
if not receiver.is_receiver(entity) then return end
if receiver.is_valid_receiver(global.entities, entity) then return end
entity.destroy()
end

script.on_event(defines.events.on_train_changed_state, function(event)
if event.train.speed == 0 then
--- create hidden receiver over locomotive when the train stopped
Expand All @@ -72,6 +78,7 @@ script.on_event({
}, function(event)
local entity = event.created_entity or event.entity or event.destination
create_receiver(entity)
remove_uncontrolled_receiver(entity)
end)

script.on_event({
Expand All @@ -90,19 +97,6 @@ script.on_event({
end
end)

script.on_nth_tick(180, function()
-- remove ghost receivers
remove_all_invalid_receivers()
for _, surface in pairs(game.surfaces) do
local receivers = surface.find_entities_filtered {name = key.receiver}
for _, r in pairs(receivers) do
if not receiver.is_valid_receiver(global.entities, r) then
r.destroy()
end
end
end
end)

-- receivers feed battery locomotives on every tick
script.on_event(defines.events.on_tick, function()
for i, e in pairs(global.entities) do
Expand Down
12 changes: 11 additions & 1 deletion utils/receiver.lua
@@ -1,4 +1,10 @@
local train = require "train"
local key = require "key"

local function is_receiver(entity)
if not entity then return false end
return entity.name == key.receiver
end

local function is_valid_receiver(global_pool, receiver)
-- existence check
Expand Down Expand Up @@ -35,4 +41,8 @@ local function refuel(receiver, locomotive)
locomotive.burner.remaining_burning_fuel + transfer_energy
end

return {is_valid_receiver = is_valid_receiver, refuel = refuel}
return {
is_receiver = is_receiver,
is_valid_receiver = is_valid_receiver,
refuel = refuel
}

0 comments on commit 3dfc365

Please sign in to comment.