-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata-updates.lua
235 lines (201 loc) · 6.39 KB
/
data-updates.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
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
local DEBUG_MODE = true
local debugLog = nil
if DEBUG_MODE then
debugLog = log
end
PROTOTYPE_PREFIX = "RoEq_alt_"
-- Flatten the equipment to a single table
local allEquipment = {}
for category, catData in pairs(data.raw) do
if string.match(category, "equipment") and category ~= "equipment-grid" and category ~= "equipment-category" then
for name, datum in pairs(catData) do
debugLog("Equipment category: " .. category .. " - name: " .. name)
allEquipment[name] = datum
end
end
end
local allTech = {} -- Mapping of recipes to list of technology names that unlock it -- recipe -> list of techs
for _, tech in pairs(data.raw["technology"]) do
if tech.effects then
for _, effect in ipairs(tech.effects) do
if effect.type == "unlock-recipe" then
local recipe = effect.recipe
if not allTech[recipe] then
allTech[recipe] = {}
end
table.insert(allTech[recipe], tech.name)
end
end
end
end
-- Find each item that places an equipment and create a new one if needed
local newPrototypes = {}
-- Item Groups
table.insert(newPrototypes, {
type = "item-group",
name = "RoEq_alt_rotated_equipment",
order = "zzz",
icon = "__Rotatable_Equipment__/graphics/item_group.png",
icon_size = 128,
hidden = true,
hidden_in_factoriopedia = true
})
table.insert(newPrototypes, {
type = "item-subgroup",
name = "RoEq_alt_rotated_equipment",
group = "RoEq_alt_rotated_equipment"
})
-- Equipment
function flip_equipment(rawEquipment)
local shape = rawEquipment.shape
if not shape then return end
if shape.height == shape.width then return end
local equipment = table.deepcopy(rawEquipment)
equipment.name = PROTOTYPE_PREFIX .. rawEquipment.name
equipment.localised_name = {"RoEq_alt_rotated_prefix", {"equipment-name." .. rawEquipment.name}}
equipment.localised_description = {"item-description." .. rawEquipment.name}
equipment.shape.height = shape.width
equipment.shape.width = shape.height
equipment.sprite = handleSprites(equipment, equipment.sprite, rawEquipment.sprite)
return equipment
end
function handleSprites(equipment, sprite, originalSprite)
local skip_overrides = equipment["expected_base_filename"] and
equipment["expected_base_filename"] ~= sprite["filename"]
if not skip_overrides and equipment["rotated_sprite"] then
debugLog("Found rotated sprite")
return equipment["rotated_sprite"]
elseif not skip_overrides and equipment["rotated_sprite_filename"] then
debugLog("Found rotated sprite filename")
sprite.height = originalSprite.width
sprite.width = originalSprite.height
sprite.filename = equipment["rotated_sprite_filename"]
elseif sprite.layers then
debugLog("Defaulting to shrinking layers")
for _, layer in ipairs(sprite.layers) do
resizeSprite(layer, equipment.shape)
end
else
debugLog("Defaulting to shrinking")
resizeSprite(sprite, equipment.shape)
end
return sprite
end
function resizeSprite(sprite, shape)
local h = shape.height
local w = shape.width
if h > w then
sprite.scale = w / h
else
sprite.scale = h / w
end
end
function new_item_for_equipment(oldItem, equipment)
local item = table.deepcopy(oldItem)
item.name = PROTOTYPE_PREFIX .. oldItem.name
item.localised_name = {"RoEq_alt_rotated_prefix", {"equipment-name." .. oldItem.name}}
item.place_as_equipment_result = equipment.name
item.icons = create_icons({oldItem}, true)
item.hidden = true
item.hidden_in_factoriopedia = true
return item
end
function should_show_recipes()
if mods["equipment-gantry"] then
return false
end
return true
end
function add_recipe_to_techs(oldRecipeName, newRecipeName)
local techs = allTech[oldRecipeName]
for _, t in ipairs(techs) do
table.insert(data.raw["technology"][t].effects, {
type = "unlock-recipe",
recipe = newRecipeName,
hidden = true
})
end
end
-- isRotate - true for "_rotate", false for "_restore"
function get_icon_file_name(isRotate)
if isRotate then
return "__Rotatable_Equipment__/graphics/rotate_symbol.png"
else
return "__Rotatable_Equipment__/graphics/restore_symbol.png"
end
end
function create_icons(oldPrototypes, isRotate)
local icons = {}
for _, oldP in ipairs(oldPrototypes) do
if oldP.icons then
icons = table.deepcopy(oldP.icons)
elseif oldP.icon then
table.insert(icons, {
icon = oldP.icon,
icon_size = oldP.icon_size
})
end
if #icons > 0 then
break
end
end
table.insert(icons, {
icon = get_icon_file_name(isRotate),
icon_size = 64
})
return icons
end
function get_recipe_vars(oldName, rotatedName, isRotate)
if isRotate then
return "rotate", "az", oldName, rotatedName
else
return "restore", "zz", rotatedName, oldName
end
end
function create_rotational_recipe(oldItem, isRotate, hidden)
local oldName = oldItem.name
local oldRecipe = data.raw["recipe"][oldName]
local recipe = table.deepcopy(oldRecipe)
local rotatedName = PROTOTYPE_PREFIX .. oldName
local rotateType, order, ingredientName, resultName = get_recipe_vars(oldName, rotatedName, isRotate)
if recipe then
recipe.name = rotatedName .. "_" .. rotateType
recipe.localised_name = {"RoEq_alt_recipe_" .. rotateType, {"equipment-name." .. oldName}}
recipe.order = oldItem.order .. order
recipe.category = "advanced-crafting"
recipe.subgroup = "RoEq_alt_rotated_equipment"
recipe.energy_required = 0.5
recipe.hide_from_player_crafting = true
recipe.hidden = hidden
recipe.hidden_in_factoriopedia = true
recipe.ingredients = {
{type="item", name=ingredientName, amount=1}
}
recipe.results = {
{type="item", name=resultName, amount=1, ignored_by_stats=1}
}
recipe.icons = create_icons({oldRecipe, oldItem}, isRotate)
add_recipe_to_techs(oldName, recipe.name)
end
return recipe
end
function create_alt_equipment(item)
local rawEquipment = allEquipment[item.place_as_equipment_result]
if rawEquipment then
debugLog("Found equipment for " .. item.name)
local newEquipment = flip_equipment(rawEquipment)
if not newEquipment then return end
local newItem = new_item_for_equipment(item, newEquipment)
debugLog("Creating alternative equipment for " .. item.name)
table.insert(newPrototypes, newEquipment)
table.insert(newPrototypes, newItem)
table.insert(newPrototypes, create_rotational_recipe(item, true, should_show_recipes()))
table.insert(newPrototypes, create_rotational_recipe(item, false, should_show_recipes()))
end
end
for _, item in pairs(data.raw["item"]) do
if item.place_as_equipment_result then
create_alt_equipment(item)
end
end
data:extend(newPrototypes)