-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.lua
More file actions
130 lines (109 loc) · 3.78 KB
/
control.lua
File metadata and controls
130 lines (109 loc) · 3.78 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
local xrayBuildingWhitelist = {}
local done = {}
local function suffixed(str, ending) -- Taken from bulk-teleport
return ending == "" or str:sub(-#ending) == ending
end
local function swap(name, old, surface, player)
local newBuilding = surface.create_entity({
name = name,
position = old.position,
force = old.force,
raise_built = false,
create_build_effect_smoke = false,
spawn_decorations = false
})
if old.to_be_deconstructed() then
newBuilding.order_deconstruction(player.force, player)
end
return newBuilding
end
local Enabled = true
local function updatePlayerXray(playerIndex)
if Enabled == true then
local player = game.get_player(playerIndex)
local radius = 9
local surface = player.surface
local machines = surface.find_entities_filtered{position = player.position, radius = radius}
for _, machine in ipairs(machines) do
for _, Whitelisted in pairs(xrayBuildingWhitelist) do
if Whitelisted == machine.name then
local newMachine = swap(machine.name .. '-xray', machine, surface, player)
machine.destroy({raise_destroy = false})
if not global.players_xray[playerIndex] then
global.players_xray[playerIndex] = {}
end
table.insert(global.players_xray[playerIndex], newMachine)
end
end
end
if global.players_xray[playerIndex] then
local player = game.get_player(playerIndex)
for index, machine in pairs(global.players_xray[playerIndex]) do
if machine.valid then
local dx = machine.position.x - player.position.x
local dy = machine.position.y - player.position.y
if dx * dx + dy * dy > radius * radius then
swap(machine.name:sub(1, -6), machine, surface, player)
machine.destroy({raise_destroy = false})
global.players_xray[playerIndex][index] = nil
end
else
global.players_xray[playerIndex][index] = nil
end
end
end
else
if global.players_xray[playerIndex] then
local player = game.get_player(playerIndex)
local surface = player.surface
for index, machine in pairs(global.players_xray[playerIndex]) do
if machine.valid then
swap(machine.name:sub(1, -6), machine, surface, player)
machine.destroy({raise_destroy = false})
end
global.players_xray[playerIndex][index] = nil
end
end
end
end
local function init()
if done["248k"] ~= 'true' then
--ensure that we only create xrayed refinery entity when the mod is installed
if game.active_mods["248k"] then
table.insert(xrayBuildingWhitelist, "fi_refinery_entity")
end
done["248k"] = 'true'
end
end
script.on_event(defines.events.on_player_created,
function(event)
init()
end
)
local function initg()
global.players_xray = global.players_xray or {}
global.player_xray_toggle = global.player_xray_toggle or {}
end
script.on_init(
function()
initg()
end
)
script.on_load(
function()
initg()
end
)
script.on_event(defines.events.on_lua_shortcut,
function(event)
if event.prototype_name == "x-ray-toggle" then
Enabled = not Enabled
updatePlayerXray(event.player_index)
end
end
)
script.on_event(defines.events.on_player_changed_position,
function(event)
updatePlayerXray(event.player_index)
end
)