forked from ReikaKalseki/DragonIndustries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentitytracker.lua
117 lines (103 loc) · 2.81 KB
/
entitytracker.lua
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
--local MATCH_CACHE = {}
local tracker = {
["add"] = {},
["remove"] = {},
--["globals"] = {}
}
local matchTracker = {
["add"] = {},
["remove"] = {},
--["globals"] = {}
}
local tickGroups = {}
--call from all the entity creation/place/build events
function trackEntityAddition(entity, glbl)
--local glbl = getGlobal(entity.name)
local func = tracker["add"][entity.name]
if func then
func(glbl, entity, entity.force)
else
for k,func in pairs(matchTracker["add"]) do
if string.find(entity.name, k, 1, true) then
func(glbl, entity, entity.force)
end
end
end
end
--call from all the entity removal/died/mined events
function trackEntityRemoval(entity, glbl)
--local glbl = getGlobal(entity.name)
local func = tracker["remove"][entity.name]
if func then
func(glbl, entity, entity.force)
else
for k,func in pairs(matchTracker["remove"]) do
if string.find(entity.name, k, 1, true) then
func(glbl, entity, entity.force)
end
end
end
end
--call from onTick
function runTickHooks(glbl, tick)
for _,func in pairs(tickGroups) do
func(glbl, tick)
end
end
--[[
local function getOrCreateTickGroup(id, glbl)
if not tickGroups[id] then
tickGroups[id] = {id = id, getGlobal = glbl, calls = {}}
end
return tickGroups[id]
end
function getGlobal(name)
local func = tracker["globals"][name]
if not func then
for k,func2 in pairs(matchTracker["globals"]) do
game.print("Checking for '" .. k .. "' in '" .. name .. "'")
if string.find(name, k, 1, true) then
func = func2
end
end
end
if func then
return func()
else
game.print("Entity '" .. name .. "' with no remove hook?")
end
end
--]]
function addTracker(name, add, _remove, tick--[[, globalID, glbl--]])
log("Registering entity tracker for '" .. name .. "'")
--local hook = getOrCreateTickGroup(globalID, glbl)
--table.insert(hook.calls, tick)
tracker["add"][name] = add
tracker["remove"][name] = _remove
table.insert(tickGroups, tick)
--tracker["globals"][name] = glbl
end
--[[
function buildMatchCache()
for n,proto in pairs(game.entity_prototypes) do
for k,entry in pairs(MATCH_CACHE) do
if string.find(n, k, 1, true) then
addTracker(n, entry.onAdd, entry.onRemove, entry.onTick)
end
end
end
end
function addMatcherTracker(name, add, _remove, tick)
MATCH_CACHE[name] = {name = name, onAdd = add, onRemove = _remove, onTick = tick}
end
--]]
function addMatcherTracker(name, add, _remove, tick--[[, globalID, glbl--]])
log("Registering string-search entity tracker for '" .. name .. "'")
--local hook = getOrCreateTickGroup(globalID, glbl)
--table.insert(hook.calls, tick)
matchTracker["add"][name] = add
matchTracker["remove"][name] = _remove
table.insert(tickGroups, tick)
--matchTracker["globals"][name] = glbl
end
--remote.add_interface("entitytracker", {addTracker = addTracker, addMatcherTracker = addMatcherTracker})