-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcontrol.lua
110 lines (92 loc) · 4.5 KB
/
control.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
--require
local constants = require('modules/constants')
local Event = require('__stdlib__/stdlib/event/event')
local util = require('util')
local table = require('__stdlib__/stdlib/utils/table')
local Queue = require('__stdlib__/stdlib/misc/queue')
local Is = require('__stdlib__/stdlib/utils/is')
local blueprints = require('modules/blueprints')
local waterGhostUpdater = require('modules/waterGhostUpdater')
local waterGhostCommon = require('modules/waterGhostCommon')
--get all entity prototypes names whos name starts with constants.dummyPrefix
local function fillWaterGhostTypes()
global.GhostOnWater.WaterGhostNames = table.filter(
table.map(game.entity_prototypes, function(prototype) return prototype.name end),
function(name) return util.string_starts_with(name, constants.dummyPrefix) end)
end
--sets event filters for the diffrent build events this needs to be done both on reInint and on load
local function setBuildEventFilters()
local filterArray = table.map(global.GhostOnWater.WaterGhostNames,
function(name) return { filter = 'ghost_name', name = name } end)
script.set_event_filter(defines.events.on_built_entity, filterArray)
script.set_event_filter(defines.events.script_raised_built, filterArray)
script.set_event_filter(defines.events.on_entity_cloned, filterArray)
end
--re initilises data global table
local function reInitGlobalTable()
global.GhostOnWater =
{
WaterGhostNames = {},
KnownWaterGhosts = Queue()
}
fillWaterGhostTypes()
setBuildEventFilters()
--force the known water ghosts table to be updated
waterGhostUpdater.forceUpdateKnownWaterGhosts()
--space exploration compatibility
--get collision for space (space exploration compatibility) to the global table if it exists
local emptySpaceTileCollisionLayerPrototype = game.entity_prototypes["collision-mask-empty-space-tile"]
if emptySpaceTileCollisionLayerPrototype then
global.GhostOnWater.emptySpaceCollsion = table.first(table.keys(emptySpaceTileCollisionLayerPrototype.collision_mask))
end
--compatiblity with waterfill mods
global.GhostOnWater.placableWaterTile = waterGhostCommon.getPlacableWaterTile()
end
local function onLoad()
Queue.load(global.GhostOnWater.KnownWaterGhosts)
setBuildEventFilters()
end
--event handlers for everything non inilisation related
--runs when user triggers a blueprint update with the shortcut or hotkey
local function onBlueprintUpdateTriggerd(event)
local playerIndex = event.player_index
blueprints.updateBlueprint(playerIndex, blueprints.bpReplacerToDummy)
end
local function onBlueprintRevertTriggerd(event)
local playerIndex = event.player_index
blueprints.updateBlueprint(playerIndex, blueprints.bpReplacerToOriginal)
end
--handles on_build_entity, on_script_raised_built and on_entity_cloned events
local function onBuildEvent(event)
local buildEntity = event.created_entity or event.entity or event.destination
--check if the build entity is valid
if not buildEntity then return end
waterGhostUpdater.addEntityToKnownWaterGhosts(buildEntity)
if __Profiler then
remote.call("profiler", "dump")
end
end
--on configuration changed event
Event.on_configuration_changed(reInitGlobalTable)
Event.on_init(reInitGlobalTable)
--on load event to on_load
Event.on_load(onLoad)
--add event handler for waterGhostUpdate
Event.register(defines.events.on_tick, waterGhostUpdater.waterGhostUpdate)
--add event handler for updateSettings
--Event.on_nth_tick(constants.settingsUpdateDelay, updateSettings)
--register event handlers for on_build_entity, on_script_raised_built and on_entity_cloned
Event.register(defines.events.on_built_entity, onBuildEvent)
Event.register(defines.events.script_raised_built, onBuildEvent)
Event.register(defines.events.on_entity_cloned, onBuildEvent)
--add event handler for update blueprint shortcut using filter function
Event.register(defines.events.on_lua_shortcut, onBlueprintUpdateTriggerd, function(event, shortcut)
return event.prototype_name == "ShortcutWaterGhostBlueprintUpdate"
end, "")
--add event handler for update blueprint hotkey
Event.register("InputWaterGhostBlueprintUpdate", onBlueprintUpdateTriggerd)
--add event handler for revert blueprint shortcut using filter function
Event.register(defines.events.on_lua_shortcut, onBlueprintRevertTriggerd, function(event, shortcut)
return event.prototype_name == "ShortcutWaterGhostBlueprintRevert"
end, "")
Event.register("InputWaterGhostBlueprintRevert", onBlueprintRevertTriggerd)