-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.lua
More file actions
63 lines (53 loc) · 2.8 KB
/
Copy pathcontrol.lua
File metadata and controls
63 lines (53 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require "util"
require "__InserterModule__/Mod_logic"
-----------------------------
-- LOCAL CONSTANTS --
-----------------------------
--This is a filter for subscribing to certain types of events, usually "entity was created" events for the
--four types of entities that we care about.
local SHARED_FILTER = {{ filter = "type", type = "mining-drill" }, { filter = "type", type = "beacon" },
{ filter = "type", type = "lab" }, { filter = "type", type = "furnace" }}
-------------------------------------------------------------------------
-- EVENT HANDLERS SPECIFICALLY FOR UPDATING THE TRACKING TABLE --
-------------------------------------------------------------------------
--Setup tracking table when a new game is created or this mod is added to a save for the first time:
script.on_init( function()
create_tracking_table()
end )
--Called when a player builds an entity:
script.on_event( defines.events.on_built_entity, function( event )
begin_tracking( event.created_entity )
end, SHARED_FILTER )
--Called when the map editor clones an area onto another area:
script.on_event( defines.events.on_entity_cloned, function( event )
begin_tracking( event.destination )
end, SHARED_FILTER )
--Called when a Construction Robot builds an entity:
script.on_event( defines.events.on_robot_built_entity, function( event )
begin_tracking( event.created_entity )
end, SHARED_FILTER )
--Called when a piece of Lua code builds an entity & allows other mods to know about it:
script.on_event( defines.events.script_raised_built, function( event )
begin_tracking( event.entity )
end, SHARED_FILTER )
--Called when an entity is destroyed for any reason.
--This is not the only place we keep track of whether entities are valid or not;
--we will also remove invalid entries as we come across them when iterating through global.trackedEntities
script.on_event( defines.events.on_entity_destroyed, function( event )
stop_tracking( event.registration_number )
end )
-------------------------------------------------------------------------------------------------------------
-- EVERY FEW TICKS, QUICKLY CHECK ALL TRACKED ENTITIES TO REMOVE ILLEGALLY PLACED INSERTER MODULES --
-------------------------------------------------------------------------------------------------------------
--Instead of iterating through the entire table at once every N ticks, we process 1 element per tick until we reach
--the end of the table, then start over again.
script.on_event( defines.events.on_tick, function( event )
for i = 1, settings.global[ "Q-InserterModule:updates-per-tick" ].value do
single_step_update_tracked_entities()
if global.keyToProcess == nil then
--This is to prevent us from iterating through the table multiple
--times per tick when it's small & the setting is at a high value.
break
end
end
end )