-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontrol.lua
133 lines (116 loc) · 4.66 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
util = require "__core__.lualib.util"
event = require "scripts.event"
CustomInput = require "scripts.custom-input"
Search = require "scripts.search"
SearchResults = require "scripts.search-results"
ResultLocation = require "scripts.result-location"
Gui = require "scripts.gui"
require "scripts.remote"
DEBOUNCE_TICKS = 60
function filtered_surfaces(override_surface, player_surface)
if override_surface then
return {player_surface}
end
-- Skip certain modded surfaces that won't have assemblers/chests placed on them
local surfaces = {}
for _, surface in pairs(game.surfaces) do
local surface_name = surface.name
if string.sub(surface_name, -12) ~= "-transformer" -- Power Overload
and string.sub(surface_name, 1, 17) ~= "nullius-landfill-" -- Nullius
and string.sub(surface_name, 0, 8) ~= "starmap-" -- Space Exploration
and string.sub(surface_name, 0, 15) ~= "EE_TESTSURFACE_" -- Editor Extensions
and string.sub(surface_name, 0, 10) ~= "BPL_TheLab" -- Blueprint Designer Lab
and string.sub(surface_name, 0, 9) ~= "bpsb-lab-" -- Blueprint Sandboxes
and surface.name ~= "IR-limbo" -- Industrial Revolution 2/3
and surface_name ~= "aai-signals" -- AAI Signals
and surface_name ~= "secret_companion_surface_please_dont_touch" -- Companion Drones
then
table.insert(surfaces, surface)
end
end
return surfaces
end
local function update_surface_count()
-- Hides 'All surfaces' button
local multiple_surfaces = #filtered_surfaces() > 1
if multiple_surfaces ~= storage.multiple_surfaces then
for _, player_data in pairs(storage.players) do
local all_surfaces = player_data.refs.all_surfaces
all_surfaces.visible = multiple_surfaces
end
end
storage.multiple_surfaces = multiple_surfaces
end
script.on_event({defines.events.on_surface_created, defines.events.on_surface_deleted}, update_surface_count)
local function generate_item_to_entity_table()
-- Make map of {item_name -> list[entity_name]}
-- First try items_to_place_this, then mineable_properties
-- Exception in mineable_properties is we don't want to include entity_name when type == "simple-entity" if item_name is a resource
-- This prevents things like rocks showing when searching for stone
local resource_prototypes = prototypes.get_entity_filtered({{filter = "type", type = "resource"}})
local is_resource = {}
for _, resource in pairs(resource_prototypes) do
is_resource[resource.name] = true
end
local prototypes = prototypes.get_entity_filtered({})
-- Filter out rocks
local item_to_entities = {}
for _, prototype in pairs(prototypes) do
local items_to_place_this = prototype.items_to_place_this
if items_to_place_this then
for _, item in pairs(items_to_place_this) do
local item_name = item.name
local associated_entities = item_to_entities[item_name] or {}
table.insert(associated_entities, prototype.name)
item_to_entities[item_name] = associated_entities
end
else
local properties = prototype.mineable_properties
if properties.minable and properties.products then
for _, item in pairs(prototype.mineable_properties.products) do
local item_name = item.name
if prototype.type ~= "simple-entity" or not is_resource[item_name] then
local associated_entities = item_to_entities[item_name] or {}
table.insert(associated_entities, prototype.name)
item_to_entities[item_name] = associated_entities
end
end
end
end
end
-- Hardcode some Pyanodons associations
if script.active_mods["pypetroleumhandling"] then
-- or {} in case something removed those items or playing an older version of Py
table.insert(item_to_entities["raw-gas"] or {}, "bitumen-seep")
table.insert(item_to_entities["tar"] or {}, "bitumen-seep")
table.insert(item_to_entities["crude-oil"] or {}, "bitumen-seep")
end
storage.item_to_entities = item_to_entities
end
script.on_init(
function()
storage.players = {}
storage.current_searches = {}
storage.multiple_surfaces = false
update_surface_count()
generate_item_to_entity_table()
end
)
script.on_configuration_changed(
function()
-- Destroy all GUIs
for player_index, player_data in pairs(storage.players) do
local player = game.get_player(player_index)
if player then
Gui.destroy(player, player_data)
else
storage.players[player_index] = nil
end
end
-- Stop in-progress non-blocking searches
storage.current_searches = {}
storage.multiple_surfaces = false
update_surface_count()
generate_item_to_entity_table()
end
)