-
Notifications
You must be signed in to change notification settings - Fork 7
/
control.lua
151 lines (147 loc) · 6.08 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
local function on_player_selected_area(event)
if event.item == "power-grid-comb" then
local distances = {}
local create = {}
for k, entity in pairs(event.entities) do
if entity.type == "electric-pole" and entity.name ~= "ret-pole-wire" then
local info = {
surface = entity.surface,
name = entity.name,
unit_number = entity.unit_number,
position = entity.position,
health = entity.health,
force = entity.force,
connections = {},
}
for k, connection in pairs(entity.circuit_connection_definitions) do
info.connections[k] = {
wire = connection.wire,
source_circuit_id = connection.source_circuit_id,
target_circuit_id = connection.target_circuit_id,
target_unit_number = connection.target_entity.unit_number,
target_entity = connection.target_entity,
}
end
local max_wire_distance = entity.prototype.max_wire_distance
if create[max_wire_distance] == nil then
create[max_wire_distance] = {}
table.insert(distances, max_wire_distance)
end
table.insert(create[max_wire_distance], info)
entity.destroy()
end
end
table.sort(distances)
local old_new_map = {}
for _, distance in ipairs(distances) do
for k, newinfo in pairs(create[distance]) do
local newpole = newinfo.surface.create_entity({
name = newinfo.name,
position = newinfo.position,
force = newinfo.force,
})
newpole.health = newinfo.health
create[distance][k].newpole = newpole
old_new_map[newinfo.unit_number] = newpole
end
end
for _, distance in ipairs(distances) do
for _, newinfo in pairs(create[distance]) do
for k, connection in pairs(newinfo.connections) do
local target
if connection.target_entity.valid then
target = connection.target_entity
else
target = old_new_map[connection.target_unit_number]
end
if target then
newinfo.newpole.connect_neighbour({
wire = connection.wire,
target_entity = target,
source_circuit_id = connection.source_circuit_id,
target_circuit_id = connection.target_circuit_id,
})
end
end
end
end
end
end
script.on_event(defines.events.on_player_selected_area, on_player_selected_area)
local function find_pole_in_direction(entity, direction, distance)
local selection_box = entity.prototype.selection_box
local area
if direction == defines.direction.north then
-- top left - top left of current plus distance north
-- bot right - top right of current
area = {{entity.position.x + selection_box.left_top.x, entity.position.y + selection_box.left_top.y - distance},
{entity.position.x + selection_box.right_bottom.x, entity.position.y + selection_box.left_top.y}}
elseif direction == defines.direction.south then
-- top left - bot left of current
-- bot right - bot right of current plus distance south
area = {{entity.position.x + selection_box.left_top.x, entity.position.y + selection_box.right_bottom.y},
{entity.position.x + selection_box.right_bottom.x, entity.position.y + selection_box.right_bottom.y + distance}}
elseif direction == defines.direction.west then
-- top left - top left of current plus distance west
-- bot right - bot left of current
area = {{entity.position.x + selection_box.left_top.x - distance, entity.position.y + selection_box.left_top.y},
{entity.position.x + selection_box.left_top.x, entity.position.y + selection_box.right_bottom.y}}
elseif direction == defines.direction.east then
-- top left - top right of current
-- bot right - bot right of current plus distance east
area = {{entity.position.x + selection_box.right_bottom.x, entity.position.y + selection_box.left_top.y},
{entity.position.x + selection_box.right_bottom.x + distance, entity.position.y + selection_box.right_bottom.y}}
end
local closest_distance
local closest
local entities = entity.surface.find_entities_filtered({
area = area,
type = "electric-pole",
force = entity.force,
})
for _, found_entity in ipairs(entities) do
local x_dist = math.abs(entity.position.x - found_entity.position.x)
local y_dist = math.abs(entity.position.y - found_entity.position.y)
local found_distance = math.sqrt(x_dist * x_dist + y_dist * y_dist)
if found_distance <= distance and (closest_distance == nil or found_distance < closest_distance) then
closest = found_entity
closest_distance = found_distance
end
end
return closest
end
local scan = {
defines.direction.north,
defines.direction.south,
defines.direction.east,
defines.direction.west,
}
local function on_player_alt_selected_area(event)
if event.item == "power-grid-comb" then
local distances = {}
local entities = {}
for k, entity in pairs(event.entities) do
if entity.type == "electric-pole" then
local max_wire_distance = entity.prototype.max_wire_distance
if not entities[max_wire_distance] then
entities[max_wire_distance] = {}
table.insert(distances, max_wire_distance)
end
table.insert(entities[max_wire_distance], entity)
end
end
table.sort(distances)
for _, distance in ipairs(distances) do
for k, entity in pairs(entities[distance]) do
entity.disconnect_neighbour()
for _, direction in ipairs(scan) do
local pole = find_pole_in_direction(entity, direction, distance)
if pole then
entity.connect_neighbour(pole)
end
end
end
end
end
end
script.on_event(defines.events.on_player_alt_selected_area, on_player_alt_selected_area)