-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
224 lines (187 loc) · 6.15 KB
/
Copy pathutils.lua
File metadata and controls
224 lines (187 loc) · 6.15 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
local utils = {}
function convert_energy(energy)
return energy and energy > 0 and energy / 1000 * 60 or nil
end
function utils.usage(entity, quality)
local usage = entity.get_max_energy_usage(quality)
return convert_energy(usage)
end
function utils.drain(entity)
local drain = entity.electric_energy_source_prototype and entity.electric_energy_source_prototype.drain
return convert_energy(drain)
end
function utils.disallowed_effects(entity)
if not entity.allowed_effects then
return nil
end
local result = {}
for effect, allow in pairs(entity.allowed_effects) do
if allow == false then
table.insert(result, effect)
end
end
return #result > 0 and result or nil
end
function utils.size(entity)
local box = entity.collision_box
if not box then
return {0, 0}
end
local left, top, right, bottom = 0
if box[1] then
left = box[1][1]
top = box[1][2]
right = box[2][1]
bottom = box[2][2]
else
left = box.left_top.x
top = box.left_top.y
right = box.right_bottom.x
bottom = box.right_bottom.y
end
if entity.has_flag("placeable-off-grid") then
return {right - left, bottom - top}
end
local width_even = (entity.tile_width % 2) == 0
local height_even = (entity.tile_height % 2) == 0
-- Count tile centers occluded by collision box
local tile_width, tile_height
if width_even then
-- Box origin is offset 0.5 from tile centers
tile_width = math.floor(0.5 - left) + math.floor(0.5 + right)
else
-- Add 1 for the box's {0, 0}
tile_width = 1 + math.floor(-left) + math.floor(right)
end
if height_even then
tile_height = math.floor(0.5 - top) + math.floor(0.5 + bottom)
else
tile_height = 1 + math.floor(-top) + math.floor(bottom)
end
return {tile_width, tile_height}
end
function eval_surface_conditions(location, conditions)
for _, condition in ipairs(conditions) do
local value =
location.surface_properties and location.surface_properties[condition.property] or
prototypes.surface_property[condition.property].default_value
if condition.max and value > condition.max then
return false
end
if condition.min and value < condition.min then
return false
end
end
return true
end
function utils.locations(entity)
if not entity.surface_conditions then
return nil
end
local result = {}
for name, proto in pairs(prototypes.space_location) do
if proto.type == "planet" and eval_surface_conditions(proto, entity.surface_conditions) then
table.insert(result, name)
end
end
for name, proto in pairs(prototypes.surface) do
if eval_surface_conditions(proto, entity.surface_conditions) then
table.insert(result, name)
end
end
if #result == #prototypes.space_location + #prototypes.surface then
return nil
end
return result
end
function utils.energy_type(entity)
return entity.electric_energy_source_prototype and "electric" or entity.burner_prototype and "burner" or nil
end
function utils.is_crafting_machine(entity)
return entity.type == "assembling-machine" or entity.type == "furnace" or entity.type == "rocket-silo"
end
function modules(entity, quality)
if quality and entity.quality_affects_module_slots then
if quality.beacon_module_slots_bonus and entity.type == "beacon" then
return entity.module_inventory_size + quality.beacon_module_slots_bonus
elseif quality.crafting_machine_slots_bonus and utils.is_crafting_machine(entity) then
return entity.module_inventory_size + quality.crafting_machine_module_slots_bonus
elseif quality.mining_drill_slots_bonus and entity.type == "mining-drill" then
return entity.module_inventory_size + quality.mining_drill_module_slots_bonus
elseif quality.lab_module_slots_bonus and entity.type == "lab" then
return entity.module_inventory_size + quality.lab_module_slots_bonus
end
end
return entity.module_inventory_size
end
function utils.modules(entity, quality)
local result = modules(entity, quality)
return result > 0 and result or nil
end
function utils.has_ocean(planet, name)
if
planet.type ~= "planet" or not planet.map_gen_settings or not planet.map_gen_settings.autoplace_settings.tile or
not planet.map_gen_settings.autoplace_settings.tile.settings
then
return false
end
for tile_name, settings in pairs(planet.map_gen_settings.autoplace_settings.tile.settings) do
local tile = prototypes.tile[tile_name]
if tile.fluid and tile.fluid.name == name then
return true
end
end
return false
end
function utils.has_resource(planet, name)
if
planet.type ~= "planet" or not planet.map_gen_settings or not planet.map_gen_settings.autoplace_settings.entity or
not planet.map_gen_settings.autoplace_settings.entity.settings
then
return false
end
if planet.map_gen_settings.autoplace_settings.entity.settings[name] then
return true
end
return false
end
function utils.compare_protos(a, b)
local ap = a.proto
local bp = b.proto
local result = nil
if ap.group.order ~= bp.group.order then
return ap.group.order < bp.group.order
elseif ap.group.name ~= bp.group.name then
return ap.group.name < bp.group.name
elseif ap.subgroup.order ~= bp.subgroup.order then
return ap.subgroup.order < bp.subgroup.order
elseif ap.subgroup.name ~= bp.subgroup.name then
return ap.subgroup.name < bp.subgroup.name
elseif ap.order ~= bp.order then
return ap.order < bp.order
else
return ap.name < bp.name
end
end
function utils.first_product_proto(products)
local product = products[1]
if product == nil then
return nil
elseif product.type == "item" then
return prototypes.item[product.name]
elseif product.type == "fluid" then
return prototypes.fluid[product.name]
else
return nil
end
end
function utils.json_number(value)
if value == math.huge then
return math.maxinteger
elseif value == -math.huge then
return -math.maxinteger
else
return value
end
end
return utils