-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdata_init.lua
More file actions
309 lines (281 loc) · 10.5 KB
/
data_init.lua
File metadata and controls
309 lines (281 loc) · 10.5 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
local create_sprite = require('scripts.sprite_generation')
--- @param chest_name string
--- @param width number
--- @param height number
local function get_circuit_connector(chest_name, width, height)
local mod_settings = MergingChests.get_mod_settings(chest_name)
local variation, x, y
local _, _, side, position = string.find(mod_settings.circuit_connector_position, '([^-]+)[-]([^-]+)')
if side == 'right' then
variation = 24
x = -width / 2
elseif side == 'left' then
variation = 28
x = width / 2
elseif side == 'bottom' then
variation = 26
y = height / 2 - 0.5
elseif side == 'center' then
if width >= height then
variation = 26
x = 0
y = height / 2 - 0.5
else
variation = 24
x = -width / 2
y = 0
end
end
if position == 'top' then
y = -height / 2 + 0.5
elseif position == 'middle' then
if x == nil then
x = 0
else
y = 0
end
elseif position == 'bottom' then
y = height / 2 - 0.75
elseif position == 'right' then
x = -width / 2 + 0.5
elseif position == 'left' then
x = width / 2 - 0.5
end
return circuit_connector_definitions.create_single(
universal_connector_template,
{
variation = variation,
main_offset = { x, y },
shadow_offset = { x + 0.1, y + 0.1 },
show_shadow = false
}
)
end
--- @alias entity_data
--- | { chest_name: string, override_prototype_properties?: table }
--- @param entity_data entity_data
--- @param loc_name table
--- @param subgroup string
--- @param width number
--- @param height number
--- @param segment_data entity_sprite
local function create_entity(entity_data, loc_name, subgroup, width, height, segment_data)
local base_chest = data.raw['logistic-container'][entity_data.chest_name] or data.raw.container[entity_data.chest_name]
if base_chest == nil then
error('Chest with name '..entity_data.chest_name..' not found')
end
local sprite = create_sprite(width, height, segment_data)
local connector = get_circuit_connector(entity_data.chest_name, width, height)
local type_specific_properties
if base_chest.logistic_mode then
type_specific_properties = {
type = 'logistic-container',
logistic_mode = base_chest.logistic_mode,
animation_sound = base_chest.animation_sound,
trash_inventory_size = base_chest.trash_inventory_size,
opened_duration = 7,
animation = {
layers = sprite
}
}
if base_chest.logistic_mode == 'storage' then
type_specific_properties.max_logistic_slots = 1
end
else
type_specific_properties = {
type = 'container',
picture = {
layers = sprite
}
}
end
local merged_chest_name = MergingChests.get_merged_chest_name(entity_data.chest_name, width, height)
table.insert(data.raw['selection-tool'][MergingChests.merge_selection_tool_name].alt_select.entity_filters, merged_chest_name)
return util.merge({
type_specific_properties,
{
name = merged_chest_name,
localised_name = loc_name,
icon = base_chest.icon,
icons = base_chest.icons,
icon_size = base_chest.icon_size,
open_sound = base_chest.open_sound,
close_sound = base_chest.close_sound,
max_health = base_chest.max_health * math.min(width * height, 10),
inventory_size = MergingChests.get_inventory_size(base_chest.inventory_size, width * height, entity_data.chest_name),
inventory_type = MergingChests.get_mod_settings(entity_data.chest_name).inventory_type,
flags = { 'placeable-player', 'player-creation' },
minable = { mining_time = 2, result = entity_data.chest_name, count = width * height },
placeable_by = { item = entity_data.chest_name, count = width * height },
corpse = 'medium-remnants',
dying_explosion = 'medium-explosion',
vehicle_impact_sound = { filename = '__base__/sound/car-metal-impact.ogg', volume = 0.65 },
collision_box = { { -width / 2 + 0.15, -height / 2 + 0.15 }, { width / 2 - 0.15, height / 2 - 0.15 } },
selection_box = { { -width / 2, -height / 2 }, { width / 2, height / 2 } },
subgroup = subgroup,
circuit_connector = connector,
circuit_wire_max_distance = default_circuit_wire_max_distance + math.min(width, height) - 1,
hidden_in_factoriopedia = true,
surface_conditions = base_chest.surface_conditions
},
settings.startup[MergingChests.setting_names.enable_upgrading_merged_chests].value and {
fast_replaceable_group = 'merged-container',
} or {},
entity_data.override_prototype_properties or {}
})
end
--- @param entity_data entity_data
--- @param segment_data entity_sprite
--- @param width number
local function create_wide_chest_entity(entity_data, segment_data, width)
return create_entity(
entity_data,
{ 'chest-name.'..MergingChests.prefix_with_modname('wide-'..entity_data.chest_name), ''..width },
MergingChests.item_group_names.wide_chests,
width,
1,
segment_data
)
end
--- @param entity_data entity_data
--- @param segment_data entity_sprite
--- @param height number
local function create_high_chest_entity(entity_data, segment_data, height)
return create_entity(
entity_data,
{ 'chest-name.'..MergingChests.prefix_with_modname('high-'..entity_data.chest_name), ''..height },
MergingChests.item_group_names.high_chests,
1,
height,
segment_data
)
end
--- @param entity_data entity_data
--- @param segment_data entity_sprite
--- @param width number
--- @param height number
local function create_warehouse_entity(entity_data, segment_data, width, height)
return create_entity(
entity_data,
{ 'chest-name.'..MergingChests.prefix_with_modname(entity_data.chest_name..'-warehouse'), ''..width, ''..height },
MergingChests.item_group_names.warehouses,
width,
height,
segment_data
)
end
--- @param entity_data entity_data
--- @param segment_data entity_sprite
--- @param width number
--- @param height number
local function create_trashdump_entity(entity_data, segment_data, width, height)
return create_entity(
entity_data,
{ 'chest-name.'..MergingChests.prefix_with_modname(entity_data.chest_name..'-trashdump'), ''..width, ''..height },
MergingChests.item_group_names.trashdumps,
width,
height,
segment_data
)
end
--- @type { [setting_allowed_value]: boolean[] }
local setting_value_to_enabled_flags = {
['none'] = { false, false, false },
['chest'] = { true, false, false },
['warehouse'] = { false, true, false },
['trashdump'] = { false, false, true },
['chest-warehouse'] = { true, true, false },
['chest-trashdump'] = { true, false, true },
['warehouse-trashdump'] = { false, true, true },
['chest-warehouse-trashdump'] = { true, true, true }
}
--- Creates merged chest prototypes
---
--- Reads settings made during settings stage with same `chest_name`.
--- @param entity_data entity_data
--- @param segments_data segments_data
function MergingChests.create_mergeable_chest(entity_data, segments_data)
local setting = settings.startup[MergingChests.chest_specific_setting_name(MergingChests.setting_names.mergeable_chest, entity_data.chest_name)]
local enable_chest, enable_warehouse, enable_trashdump = table.unpack(
setting_value_to_enabled_flags[setting and setting.value or 'none']
)
if setting and MergingChests.is_mod_active(MergingChests.all_types_mod_name) then
enable_chest = true
enable_warehouse = true
enable_trashdump = true
end
local mod_settings = MergingChests.get_mod_settings(entity_data.chest_name)
local max_area = 0
if enable_chest and segments_data.high_segments then
for height = 2, math.min(mod_settings.max_height, mod_settings.max_area) do
if MergingChests.is_size_allowed(1, height, entity_data.chest_name) then
data:extend({ create_high_chest_entity(entity_data, segments_data.high_segments, height) })
max_area = math.max(max_area, height)
end
end
end
for width = 2, math.min(mod_settings.max_width, mod_settings.max_area) do
if enable_chest and segments_data.wide_segments then
if MergingChests.is_size_allowed(width, 1, entity_data.chest_name) then
data:extend({ create_wide_chest_entity(entity_data, segments_data.wide_segments, width) })
max_area = math.max(max_area, width)
end
end
for height = 2, math.min(mod_settings.max_height, mod_settings.max_area) do
if MergingChests.is_size_allowed(width, height, entity_data.chest_name) then
if enable_trashdump and width > mod_settings.warehouse_threshold and height > mod_settings.warehouse_threshold and segments_data.trashdump_segments then
data:extend({ create_trashdump_entity(entity_data, segments_data.trashdump_segments, width, height) })
max_area = math.max(max_area, width * height)
elseif enable_warehouse and segments_data.warehouse_segments then
data:extend({ create_warehouse_entity(entity_data, segments_data.warehouse_segments, width, height) })
max_area = math.max(max_area, width * height)
end
end
end
end
if enable_chest or enable_warehouse or enable_trashdump then
table.insert(data.raw['selection-tool'][MergingChests.merge_selection_tool_name].select.entity_filters, entity_data.chest_name)
data.raw.item[entity_data.chest_name].stack_size = math.max(data.raw.item[entity_data.chest_name].stack_size, max_area)
end
end
--- Sets next_upgrade of chests of type `type` merged from `chest_name`
--- @param type `logistic-container` | `container`
--- @param chest_name string
--- @param next_upgrade string
function MergingChests.set_next_upgrade_of(type, chest_name, next_upgrade)
if settings.startup[MergingChests.setting_names.enable_upgrading_merged_chests].value then
for _, prototype in pairs(data.raw[type]) do
local name, width, height = MergingChests.get_merged_chest_info(prototype.name)
if name == chest_name and width and height then
local merged_upgrade_name = MergingChests.get_merged_chest_name(next_upgrade, width, height)
if data.raw[type][merged_upgrade_name] then
prototype.next_upgrade = merged_upgrade_name
end
end
end
end
end
--- Disables next_upgrade of chests of type `type` merged from `chest_name`
--- @param type `logistic-container` | `container`
--- @param chest_name string
function MergingChests.disable_next_upgrade_of(type, chest_name)
for _, prototype in pairs(data.raw[type]) do
local name, _ = MergingChests.get_merged_chest_info(prototype.name)
if name == chest_name then
prototype.next_upgrade = nil
end
end
end
--- Disables next_upgrade of chests which of type `type` which are upgraded to `chest_name`
--- @param type `logistic-container` | `container`
--- @param chest_name string
function MergingChests.disable_next_upgrade_to(type, chest_name)
for _, prototype in pairs(data.raw[type]) do
if prototype.next_upgrade then
local name, _ = MergingChests.get_merged_chest_info(prototype.next_upgrade)
if name == chest_name then
prototype.next_upgrade = nil
end
end
end
end