This repository was archived by the owner on Jun 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathprototypes.lua
More file actions
284 lines (275 loc) · 17.8 KB
/
prototypes.lua
File metadata and controls
284 lines (275 loc) · 17.8 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
--- Provides utilities for locating and iterating prototypes in `data.raw`.
--- @class flib_prototypes
local flib_prototypes = {}
--- Returns a list of all prototypes with the given `base_type`.
--- @overload fun(base_type: "achievement"): data.AchievementPrototype[]
--- @overload fun(base_type: "active-trigger"): data.ActiveTriggerPrototype[]
--- @overload fun(base_type: "airborne-pollutant"): data.AirbornePollutantPrototype[]
--- @overload fun(base_type: "ambient-sound"): data.AmbientSound[]
--- @overload fun(base_type: "ammo-category"): data.AmmoCategory[]
--- @overload fun(base_type: "animation"): data.AnimationPrototype[]
--- @overload fun(base_type: "asteroid-chunk"): data.AsteroidChunkPrototype[]
--- @overload fun(base_type: "autoplace-control"): data.AutoplaceSpecification[]
--- @overload fun(base_type: "burner-usage"): data.BurnerUsagePrototype[]
--- @overload fun(base_type: "collision-layer"): data.CollisionLayerPrototype[]
--- @overload fun(base_type: "custom-event"): data.CustomEventPrototype[]
--- @overload fun(base_type: "custom-input"): data.CustomInputPrototype[]
--- @overload fun(base_type: "damage-type"): data.DamageType[]
--- @overload fun(base_type: "decorative"): data.DecorativePrototype[]
--- @overload fun(base_type: "deliver-category"): data.DeliverCategory[]
--- @overload fun(base_type: "deliver-impact-combination"): data.DeliverImpactCombination[]
--- @overload fun(base_type: "editor-controller"): data.EditorControllerPrototype[]
--- @overload fun(base_type: "entity"): data.EntityPrototype[]
--- @overload fun(base_type: "equipment"): data.EquipmentPrototype[]
--- @overload fun(base_type: "equipment-category"): data.EquipmentCategory[]
--- @overload fun(base_type: "equipment-grid"): data.EquipmentGridPrototype[]
--- @overload fun(base_type: "fluid"): data.FluidPrototype[]
--- @overload fun(base_type: "font"): data.FontPrototype[]
--- @overload fun(base_type: "fuel-category"): data.FuelCategory[]
--- @overload fun(base_type: "god-controller"): data.GodControllerPrototype[]
--- @overload fun(base_type: "gui-style"): data.GuiStyle[]
--- @overload fun(base_type: "impact-category"): data.ImpactCategory[]
--- @overload fun(base_type: "item"): data.ItemPrototype[]
--- @overload fun(base_type: "item-group"): data.ItemGroup[]
--- @overload fun(base_type: "item-subgroup"): data.ItemSubGroup[]
--- @overload fun(base_type: "map-gen-presets"): data.MapGenPresets[]
--- @overload fun(base_type: "map-settings"): data.MapSettings[]
--- @overload fun(base_type: "module-category"): data.ModuleCategory[]
--- @overload fun(base_type: "mouse-cursor"): data.MouseCursor[]
--- @overload fun(base_type: "noise-expression"): data.NamedNoiseExpression[]
--- @overload fun(base_type: "noise-function"): data.NamedNoiseFunction[]
--- @overload fun(base_type: "particle"): data.ParticlePrototype[]
--- @overload fun(base_type: "procession"): data.ProcessionPrototype[]
--- @overload fun(base_type: "procession-layer-inheritance-group"): data.ProcessionLayerInheritanceGroup[]
--- @overload fun(base_type: "quality"): data.QualityPrototype[]
--- @overload fun(base_type: "recipe"): data.RecipePrototype[]
--- @overload fun(base_type: "recipe-category"): data.RecipeCategory[]
--- @overload fun(base_type: "remote-controller"): data.RemoteControllerPrototype[]
--- @overload fun(base_type: "resource-category"): data.ResourceCategory[]
--- @overload fun(base_type: "shortcut"): data.ShortcutPrototype[]
--- @overload fun(base_type: "sound"): data.SoundPrototype[]
--- @overload fun(base_type: "space-connection"): data.SpaceConnectionPrototype[]
--- @overload fun(base_type: "space-location"): data.SpaceLocationPrototype[]
--- @overload fun(base_type: "spectator-controller"): data.SpectatorControllerPrototype[]
--- @overload fun(base_type: "sprite"): data.SpritePrototype[]
--- @overload fun(base_type: "surface"): data.SurfacePrototype[]
--- @overload fun(base_type: "surface-property"): data.SurfacePropertyPrototype[]
--- @overload fun(base_type: "technology"): data.TechnologyPrototype[]
--- @overload fun(base_type: "tile"): data.TilePrototype[]
--- @overload fun(base_type: "tile-effect"): data.TileEffectDefinition[]
--- @overload fun(base_type: "tips-and-tricks-item"): data.TipsAndTricksItem[]
--- @overload fun(base_type: "tips-and-tricks-item-category"): data.TipsAndTricksItemCategory[]
--- @overload fun(base_type: "trigger-target-type"): data.TriggerTargetType[]
--- @overload fun(base_type: "trivial-smoke"): data.TrivialSmokePrototype[]
--- @overload fun(base_type: "tutorial"): data.TutorialDefinition[]
--- @overload fun(base_type: "utility-constants"): data.UtilityConstants[]
--- @overload fun(base_type: "utility-sounds"): data.UtilitySounds[]
--- @overload fun(base_type: "utility-sprites"): data.UtilitySounds[]
--- @overload fun(base_type: "virtual-signal"): data.VirtualSignalPrototype[]
function flib_prototypes.all(base_type)
if not base_type then
error("Did not provide a base_type")
end
if type(base_type) ~= "string" then
error("base_type must be a string")
end
if not defines.prototypes[base_type] then
error("'" .. base_type .. "' is not a valid base prototype type")
end
local result = {}
local result_len = 0
for prototype_type in pairs(defines.prototypes[base_type]) do
for _, prototype in pairs(data.raw[prototype_type] or {}) do
result_len = result_len + 1
result[result_len] = prototype
end
end
return result
end
--- Returns the prototype with the given `base_type` and `name`, if it exists.
--- @overload fun(base_type: "achievement", name: string): data.AchievementPrototype?
--- @overload fun(base_type: "active-trigger", name: string): data.ActiveTriggerPrototype?
--- @overload fun(base_type: "airborne-pollutant", name: string): data.AirbornePollutantPrototype?
--- @overload fun(base_type: "ambient-sound", name: string): data.AmbientSound?
--- @overload fun(base_type: "ammo-category", name: string): data.AmmoCategory?
--- @overload fun(base_type: "animation", name: string): data.AnimationPrototype?
--- @overload fun(base_type: "asteroid-chunk", name: string): data.AsteroidChunkPrototype?
--- @overload fun(base_type: "autoplace-control", name: string): data.AutoplaceSpecification?
--- @overload fun(base_type: "burner-usage", name: string): data.BurnerUsagePrototype?
--- @overload fun(base_type: "collision-layer", name: string): data.CollisionLayerPrototype?
--- @overload fun(base_type: "custom-event", name: string): data.CustomEventPrototype?
--- @overload fun(base_type: "custom-input", name: string): data.CustomInputPrototype?
--- @overload fun(base_type: "damage-type", name: string): data.DamageType?
--- @overload fun(base_type: "decorative", name: string): data.DecorativePrototype?
--- @overload fun(base_type: "deliver-category", name: string): data.DeliverCategory?
--- @overload fun(base_type: "deliver-impact-combination", name: string): data.DeliverImpactCombination?
--- @overload fun(base_type: "editor-controller", name: string): data.EditorControllerPrototype?
--- @overload fun(base_type: "entity", name: string): data.EntityPrototype?
--- @overload fun(base_type: "equipment", name: string): data.EquipmentPrototype?
--- @overload fun(base_type: "equipment-category", name: string): data.EquipmentCategory?
--- @overload fun(base_type: "equipment-grid", name: string): data.EquipmentGridPrototype?
--- @overload fun(base_type: "fluid", name: string): data.FluidPrototype?
--- @overload fun(base_type: "font", name: string): data.FontPrototype?
--- @overload fun(base_type: "fuel-category", name: string): data.FuelCategory?
--- @overload fun(base_type: "god-controller", name: string): data.GodControllerPrototype?
--- @overload fun(base_type: "gui-style", name: string): data.GuiStyle?
--- @overload fun(base_type: "impact-category", name: string): data.ImpactCategory?
--- @overload fun(base_type: "item", name: string): data.ItemPrototype?
--- @overload fun(base_type: "item-group", name: string): data.ItemGroup?
--- @overload fun(base_type: "item-subgroup", name: string): data.ItemSubGroup?
--- @overload fun(base_type: "map-gen-presets", name: string): data.MapGenPresets?
--- @overload fun(base_type: "map-settings", name: string): data.MapSettings?
--- @overload fun(base_type: "module-category", name: string): data.ModuleCategory?
--- @overload fun(base_type: "mouse-cursor", name: string): data.MouseCursor?
--- @overload fun(base_type: "noise-expression", name: string): data.NamedNoiseExpression?
--- @overload fun(base_type: "noise-function", name: string): data.NamedNoiseFunction?
--- @overload fun(base_type: "particle", name: string): data.ParticlePrototype?
--- @overload fun(base_type: "procession", name: string): data.ProcessionPrototype?
--- @overload fun(base_type: "procession-layer-inheritance-group", name: string): data.ProcessionLayerInheritanceGroup?
--- @overload fun(base_type: "quality", name: string): data.QualityPrototype?
--- @overload fun(base_type: "recipe", name: string): data.RecipePrototype?
--- @overload fun(base_type: "recipe-category", name: string): data.RecipeCategory?
--- @overload fun(base_type: "remote-controller", name: string): data.RemoteControllerPrototype?
--- @overload fun(base_type: "resource-category", name: string): data.ResourceCategory?
--- @overload fun(base_type: "shortcut", name: string): data.ShortcutPrototype?
--- @overload fun(base_type: "sound", name: string): data.SoundPrototype?
--- @overload fun(base_type: "space-connection", name: string): data.SpaceConnectionPrototype?
--- @overload fun(base_type: "space-location", name: string): data.SpaceLocationPrototype?
--- @overload fun(base_type: "spectator-controller", name: string): data.SpectatorControllerPrototype?
--- @overload fun(base_type: "sprite", name: string): data.SpritePrototype?
--- @overload fun(base_type: "surface", name: string): data.SurfacePrototype?
--- @overload fun(base_type: "surface-property", name: string): data.SurfacePropertyPrototype?
--- @overload fun(base_type: "technology", name: string): data.TechnologyPrototype?
--- @overload fun(base_type: "tile", name: string): data.TilePrototype?
--- @overload fun(base_type: "tile-effect", name: string): data.TileEffectDefinition?
--- @overload fun(base_type: "tips-and-tricks-item", name: string): data.TipsAndTricksItem?
--- @overload fun(base_type: "tips-and-tricks-item-category", name: string): data.TipsAndTricksItemCategory?
--- @overload fun(base_type: "trigger-target-type", name: string): data.TriggerTargetType?
--- @overload fun(base_type: "trivial-smoke", name: string): data.TrivialSmokePrototype?
--- @overload fun(base_type: "tutorial", name: string): data.TutorialDefinition?
--- @overload fun(base_type: "utility-constants", name: string): data.UtilityConstants?
--- @overload fun(base_type: "utility-sounds", name: string): data.UtilitySounds?
--- @overload fun(base_type: "utility-sprites", name: string): data.UtilitySounds?
--- @overload fun(base_type: "virtual-signal", name: string): data.VirtualSignalPrototype?
function flib_prototypes.find(base_type, name)
if not base_type then
error("Did not provide a base_type")
end
if type(base_type) ~= "string" then
error("base_type must be a string")
end
if base_type == "" then
error("base_type must be a non-empty string")
end
if not defines.prototypes[base_type] then
error("'" .. base_type .. "' is not a valid base prototype type")
end
if not name then
error("Did not provide a name")
end
if type(name) ~= "string" then
error("name must be a string")
end
if name == "" then
error("name must be a non-empty string")
end
for derived_type in pairs(defines.prototypes[base_type]) do
local prototypes = data.raw[derived_type]
if prototypes then
local prototype = prototypes[name]
if prototype then
return prototype
end
end
end
end
--- Returns the prototype with the given `base_type` and `name`, throwing an error if it doesn't exist.
--- @overload fun(base_type: "achievement", name: string): data.AchievementPrototype
--- @overload fun(base_type: "active-trigger", name: string): data.ActiveTriggerPrototype
--- @overload fun(base_type: "airborne-pollutant", name: string): data.AirbornePollutantPrototype
--- @overload fun(base_type: "ambient-sound", name: string): data.AmbientSound
--- @overload fun(base_type: "ammo-category", name: string): data.AmmoCategory
--- @overload fun(base_type: "animation", name: string): data.AnimationPrototype
--- @overload fun(base_type: "asteroid-chunk", name: string): data.AsteroidChunkPrototype
--- @overload fun(base_type: "autoplace-control", name: string): data.AutoplaceSpecification
--- @overload fun(base_type: "burner-usage", name: string): data.BurnerUsagePrototype
--- @overload fun(base_type: "collision-layer", name: string): data.CollisionLayerPrototype
--- @overload fun(base_type: "custom-event", name: string): data.CustomEventPrototype
--- @overload fun(base_type: "custom-input", name: string): data.CustomInputPrototype
--- @overload fun(base_type: "damage-type", name: string): data.DamageType
--- @overload fun(base_type: "decorative", name: string): data.DecorativePrototype
--- @overload fun(base_type: "deliver-category", name: string): data.DeliverCategory
--- @overload fun(base_type: "deliver-impact-combination", name: string): data.DeliverImpactCombination
--- @overload fun(base_type: "editor-controller", name: string): data.EditorControllerPrototype
--- @overload fun(base_type: "entity", name: string): data.EntityPrototype
--- @overload fun(base_type: "equipment", name: string): data.EquipmentPrototype
--- @overload fun(base_type: "equipment-category", name: string): data.EquipmentCategory
--- @overload fun(base_type: "equipment-grid", name: string): data.EquipmentGridPrototype
--- @overload fun(base_type: "fluid", name: string): data.FluidPrototype
--- @overload fun(base_type: "font", name: string): data.FontPrototype
--- @overload fun(base_type: "fuel-category", name: string): data.FuelCategory
--- @overload fun(base_type: "god-controller", name: string): data.GodControllerPrototype
--- @overload fun(base_type: "gui-style", name: string): data.GuiStyle
--- @overload fun(base_type: "impact-category", name: string): data.ImpactCategory
--- @overload fun(base_type: "item", name: string): data.ItemPrototype
--- @overload fun(base_type: "item-group", name: string): data.ItemGroup
--- @overload fun(base_type: "item-subgroup", name: string): data.ItemSubGroup
--- @overload fun(base_type: "map-gen-presets", name: string): data.MapGenPresets
--- @overload fun(base_type: "map-settings", name: string): data.MapSettings
--- @overload fun(base_type: "module-category", name: string): data.ModuleCategory
--- @overload fun(base_type: "mouse-cursor", name: string): data.MouseCursor
--- @overload fun(base_type: "noise-expression", name: string): data.NamedNoiseExpression
--- @overload fun(base_type: "noise-function", name: string): data.NamedNoiseFunction
--- @overload fun(base_type: "particle", name: string): data.ParticlePrototype
--- @overload fun(base_type: "procession", name: string): data.ProcessionPrototype
--- @overload fun(base_type: "procession-layer-inheritance-group", name: string): data.ProcessionLayerInheritanceGroup
--- @overload fun(base_type: "quality", name: string): data.QualityPrototype
--- @overload fun(base_type: "recipe", name: string): data.RecipePrototype
--- @overload fun(base_type: "recipe-category", name: string): data.RecipeCategory
--- @overload fun(base_type: "remote-controller", name: string): data.RemoteControllerPrototype
--- @overload fun(base_type: "resource-category", name: string): data.ResourceCategory
--- @overload fun(base_type: "shortcut", name: string): data.ShortcutPrototype
--- @overload fun(base_type: "sound", name: string): data.SoundPrototype
--- @overload fun(base_type: "space-connection", name: string): data.SpaceConnectionPrototype
--- @overload fun(base_type: "space-location", name: string): data.SpaceLocationPrototype
--- @overload fun(base_type: "spectator-controller", name: string): data.SpectatorControllerPrototype
--- @overload fun(base_type: "sprite", name: string): data.SpritePrototype
--- @overload fun(base_type: "surface", name: string): data.SurfacePrototype
--- @overload fun(base_type: "surface-property", name: string): data.SurfacePropertyPrototype
--- @overload fun(base_type: "technology", name: string): data.TechnologyPrototype
--- @overload fun(base_type: "tile", name: string): data.TilePrototype
--- @overload fun(base_type: "tile-effect", name: string): data.TileEffectDefinition
--- @overload fun(base_type: "tips-and-tricks-item", name: string): data.TipsAndTricksItem
--- @overload fun(base_type: "tips-and-tricks-item-category", name: string): data.TipsAndTricksItemCategory
--- @overload fun(base_type: "trigger-target-type", name: string): data.TriggerTargetType
--- @overload fun(base_type: "trivial-smoke", name: string): data.TrivialSmokePrototype
--- @overload fun(base_type: "tutorial", name: string): data.TutorialDefinition
--- @overload fun(base_type: "utility-constants", name: string): data.UtilityConstants
--- @overload fun(base_type: "utility-sounds", name: string): data.UtilitySounds
--- @overload fun(base_type: "utility-sprites", name: string): data.UtilitySounds
--- @overload fun(base_type: "virtual-signal", name: string): data.VirtualSignalPrototype
function flib_prototypes.get(base_type, name)
local result = flib_prototypes.find(base_type, name)
if not result then
error("Prototype '" .. base_type .. "/" .. name .. "' does not exist.")
end
return result
end
--- @type table<string, string?>
local base_type_lookup = {}
for base_type, derived_types in pairs(defines.prototypes) do
for derived_type in pairs(derived_types) do
base_type_lookup[derived_type] = base_type
end
end
--- Returns the base type of the given prototype type.
--- @param derived_type string
--- @return string
function flib_prototypes.get_base_type(derived_type)
local base_type = base_type_lookup[derived_type]
if not base_type then
error("'" .. derived_type .. "' is not a valid prototype type.")
end
return base_type
end
return flib_prototypes