-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrol.lua
More file actions
70 lines (61 loc) · 2.14 KB
/
control.lua
File metadata and controls
70 lines (61 loc) · 2.14 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
local function delete_chunk(surface, chunk, destroy_all)
if not surface.valid then
return
end
local area = { top_left = { chunk.x * 32, chunk.y * 32 }, bottom_right = { (chunk.x * 32) + 32, (chunk.y * 32) + 32 } }
local entities_in_chunk = surface.find_entities_filtered { area = area, force = "player" }
local players_in_chunk = surface.find_entities_filtered { area = area, force = "player", type = "character" }
if destroy_all then
if players_in_chunk and #players_in_chunk > 0 then
game.print(string.format("player(s) in chunk (%d, %d) can not delete", chunk.x, chunk.y))
else
surface.delete_chunk(chunk)
end
else
if entities_in_chunk and #entities_in_chunk > 0 then
game.print("player entities in chunk can not delete")
else
surface.delete_chunk(chunk)
end
end
end
local function on_player_selected_area(event, destroy_all)
local player = game.players[event.player_index]
local surface = event.surface or player.surface
local area = event.area
local item = event.item
if item == "chunk-eraser" then
if not player.admin then
player.print("You are not an admin so this tool will do nothing for you")
return
end
for x = area.left_top.x, area.right_bottom.x, 32 do
for y = area.left_top.y, area.right_bottom.y, 32 do
local chunk = {}
chunk.x = math.floor(x / 32)
chunk.y = math.floor(y / 32)
delete_chunk(surface, chunk, destroy_all)
end
end
end
end
script.on_event(
defines.events.on_player_selected_area,
function(event)
on_player_selected_area(event, false)
end
)
script.on_event(
defines.events.on_player_alt_selected_area,
function(event)
on_player_selected_area(event, true)
end
)
script.on_event(
defines.events.on_player_dropped_item,
function(event)
if event.entity ~= nil and event.entity.stack ~= nil and event.entity.stack.name == "chunk-eraser" then
event.entity.stack.clear()
end
end
)