forked from ReikaKalseki/DragonIndustries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprites.lua
268 lines (252 loc) · 7.59 KB
/
sprites.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
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
function clearTexture(entry)
if entry.sheet then clearTexture(entry.sheet) return end
entry.filename = "__core__/graphics/empty.png"
entry.width = 1
entry.height = 1
entry.shift = nil
entry.hr_version = nil
end
function createCircuitSprite()
local ret = {
filename = "__DragonIndustries__/graphics/signal-connection.png",
x = 0,
y = 0,
width = 61,
height = 50,
frame_count = 1,
shift = {0.140625, 0.140625},
}
return ret
end
function createCircuitActivitySprite()
local ret = {
filename = "__core__/graphics/empty.png",
width = 1,
height = 1,
frame_count = 1,
shift = {-0.296875, -0.078125},
}
return ret
end
local function suffixFilename(filename, suffix)
local parts = splitString(filename, ".")
local newname = parts[1] .. suffix
return newname .. "." .. parts[2]
end
local function genReparentedFilename(from, to, filename)
local val = literalReplace(filename, "__" .. from .. "__", "__" .. to .. "__")
return val
end
local function genNewFilename(modname, itemname, oldname, filename)
if string.find(filename, "__base__/graphics/terrain/masks", 1, true) then return filename end
local val = literalReplace(filename, "__base__", "__" .. modname .. "__")
return literalReplace(val, oldname, itemname)
end
local function replaceSpritesInTable(tab, new, hr)
if not tab.filename then return end
tab.filename = new
if tab.hr_version then
tab.hr_version.filename = hr
end
end
local function reparentSpritesDynamic(from, to, entry)
if entry.filename then
entry.filename = genReparentedFilename(from, to, entry.filename)
elseif entry.picture then
entry.picture = genReparentedFilename(from, to, entry.picture)
end
if entry.hr_version then
if entry.hr_version.filename then
entry.hr_version.filename = genReparentedFilename(from, to, entry.hr_version.filename)
elseif entry.picture then
entry.hr_version.picture = genReparentedFilename(from, to, entry.hr_version.picture)
end
end
end
local function replaceSpritesInTableDynamic(modname, itemname, oldname, entry)
if entry.filename then
entry.filename = genNewFilename(modname, itemname, oldname, entry.filename)
elseif entry.picture then
entry.picture = genNewFilename(modname, itemname, oldname, entry.picture)
end
if entry.hr_version then
if entry.hr_version.filename then
entry.hr_version.filename = genNewFilename(modname, itemname, oldname, entry.hr_version.filename)
elseif entry.picture then
entry.hr_version.picture = genNewFilename(modname, itemname, oldname, entry.hr_version.picture)
end
end
end
local function suffixSpritesInTableDynamic(entry, suffix)
if not entry.filename then return end
entry.filename = suffixFilename(entry.filename, suffix)
if entry.hr_version then
entry.hr_version.filename = suffixFilename(entry.hr_version.filename, suffix)
end
end
function replaceSprites(obj, new, hr)
if obj.picture then
replaceSpritesInTable(obj.picture, new, hr)
end
if obj.pictures then
for _,pic in pairs(obj.pictures) do
replaceSpritesInTable(pic, new, hr)
end
end
if obj.sprites then
for _,pic in pairs(obj.sprites) do
replaceSpritesInTable(pic, new, hr)
end
end
if obj.animation then
if obj.animation.layers then
for _,lyr in pairs(obj.animation.layers) do
replaceSpritesInTable(lyr, new, hr)
end
else
replaceSpritesInTable(obj.animation, new, hr)
end
end
end
local function handleAnimationTable(from, to, obj, key)
local anim = obj[key]
--log(from .. " > " .. to .. " in " .. obj.name .. "[" .. key .. "]" .. (anim and "yes" or "no"))
if not anim then return end
if anim.layers then
for _,lyr in pairs(anim.layers) do
reparentSpritesDynamic(from, to, lyr)
end
else
reparentSpritesDynamic(from, to, anim)
end
end
function reparentSprites(from, to, obj)
log("Reparenting sprites in " .. obj.name)
if obj.icon then
obj.icon = genReparentedFilename(from, to, obj.icon)
end
if obj.icons then
log(serpent.block(obj.icons))
for _,ico in pairs(obj.icons) do
ico.icon = genReparentedFilename(from, to, ico.icon)
end
end
if obj.picture then
reparentSpritesDynamic(from, to, obj.picture)
end
if obj.pictures then
for _,pic in pairs(obj.pictures) do
reparentSpritesDynamic(from, to, pic)
end
end
if obj.sprites then
for _,pic in pairs(obj.sprites) do
reparentSpritesDynamic(from, to, pic)
end
end
handleAnimationTable(from, to, obj, "animation")
handleAnimationTable(from, to, obj, "on_animation")
handleAnimationTable(from, to, obj, "off_animation")
handleAnimationTable(from, to, obj, "folded_animation")
handleAnimationTable(from, to, obj, "folding_animation")
handleAnimationTable(from, to, obj, "preparing_animation")
handleAnimationTable(from, to, obj, "prepared_animation")
handleAnimationTable(from, to, obj, "energy_glow_animation")
if obj.variants then
for __,cat in pairs(obj.variants) do
for _,pic in pairs(cat) do
reparentSpritesDynamic(from, to, pic)
end
end
end
end
function replaceSpritesDynamic(modname, oldname, obj)
if type(obj) ~= "table" then error("Tried to resprite a primitive object!") end
if not obj.name then error("Cannot resprite a nameless object: " .. serpent.block(obj)) end
log("Replacing sprites from '" .. oldname .. "' in __" .. modname .. "__/" .. obj.type .. "/" .. obj.name)
if obj.icon then
obj.icon = genNewFilename(modname, obj.name, oldname, obj.icon)
end
if obj.icons then
for _,ico in pairs(obj.icons) do
ico.filename = genNewFilename(modname, obj.name, oldname, ico.filename)
end
end
if obj.picture then
replaceSpritesInTableDynamic(modname, obj.name, oldname, obj.picture)
end
if obj.pictures then
for _,pic in pairs(obj.pictures) do
replaceSpritesInTableDynamic(modname, obj.name, oldname, pic)
end
end
if obj.sprites then
for _,pic in pairs(obj.sprites) do
replaceSpritesInTableDynamic(modname, obj.name, oldname, pic)
end
end
if obj.animation then
if obj.animation.layers then
for _,lyr in pairs(obj.animation.layers) do
replaceSpritesInTableDynamic(modname, obj.name, oldname, lyr)
end
else
replaceSpritesInTableDynamic(modname, obj.name, oldname, obj.animation)
end
end
if obj.variants then
for catname,cat in pairs(obj.variants) do
replaceSpritesInTableDynamic(modname, obj.name, oldname, cat)
end
end
end
function suffixSpritesDynamic(obj, suffix)
if obj.icon then
obj.icon = suffixFilename(obj.icon, suffix)
end
if obj.icons then
for _,ico in pairs(obj.icons) do
ico.filename = suffixFilename(ico.filename, suffix)
end
end
if obj.picture then
suffixSpritesInTableDynamic(obj.picture, suffix)
end
if obj.pictures then
for _,pic in pairs(obj.pictures) do
suffixSpritesInTableDynamic(pic, suffix)
end
end
if obj.sprites then
for _,pic in pairs(obj.sprites) do
suffixSpritesInTableDynamic(pic, suffix)
end
end
if obj.animation then
if obj.animation.layers then
for _,lyr in pairs(obj.animation.layers) do
suffixSpritesInTableDynamic(lyr, suffix)
end
else
suffixSpritesInTableDynamic(obj.animation, suffix)
end
end
end
local function swapObjSpriteField(obj1, obj2, field)
if obj1[field] then
local temp = table.deepcopy(obj1[field])
obj1[field] = table.deepcopy(obj2[field])
obj2[field] = temp
end
end
function swapSprites(obj1, obj2)
swapObjSpriteField(obj1, obj2, "icon")
swapObjSpriteField(obj1, obj2, "icons")
swapObjSpriteField(obj1, obj2, "picture")
swapObjSpriteField(obj1, obj2, "pictures")
swapObjSpriteField(obj1, obj2, "sprites")
swapObjSpriteField(obj1, obj2, "animation")
swapObjSpriteField(obj1, obj2, "on_animation")
swapObjSpriteField(obj1, obj2, "off_animation")
swapObjSpriteField(obj1, obj2, "variants")
end