compact atc_chains
This commit is contained in:
+78
-17
@@ -79,40 +79,65 @@ local function ensureChainsLoaded()
|
||||
local data = textutils.unserializeJSON(file.readAll())
|
||||
file.close()
|
||||
|
||||
assert(type(data) == "table" and type(data.chains) == "table", "Invalid JSON data")
|
||||
local chains = data
|
||||
|
||||
if type(data) == "table" and type(data.chains) == "table" then
|
||||
chains = data.chains
|
||||
end
|
||||
|
||||
assert(type(chains) == "table", "Invalid JSON data")
|
||||
|
||||
chainItemsByBaseId = {}
|
||||
baseItemIds = {}
|
||||
itemById = {}
|
||||
|
||||
for _, chain in ipairs(data.chains) do
|
||||
if not whitelistLookup or whitelistLookup[chain.base_id] then
|
||||
for _, chain in ipairs(chains) do
|
||||
local baseId = chain.base_id or chain[1]
|
||||
|
||||
if not whitelistLookup or whitelistLookup[baseId] then
|
||||
if not defaultBaseId then
|
||||
defaultBaseId = chain.base_id
|
||||
defaultBaseId = baseId
|
||||
end
|
||||
|
||||
baseItemIds[#baseItemIds + 1] = chain.base_id
|
||||
baseItemIds[#baseItemIds + 1] = baseId
|
||||
|
||||
local items = {}
|
||||
local itemCount = 0
|
||||
|
||||
for _, item in ipairs(chain.items or {}) do
|
||||
if item.stage == "item" then
|
||||
itemById[item.item_id] = {
|
||||
id = item.item_id,
|
||||
icon_nfp = item.icon_nfp_16x16,
|
||||
}
|
||||
else
|
||||
if chain.base_id then
|
||||
for _, item in ipairs(chain.items or {}) do
|
||||
if item.stage == "item" then
|
||||
itemById[item.item_id] = {
|
||||
id = item.item_id,
|
||||
icon_nfp = item.icon_nfp_16x16,
|
||||
}
|
||||
else
|
||||
itemCount = itemCount + 1
|
||||
items[itemCount] = {
|
||||
id = item.item_id,
|
||||
icon_nfp = item.icon_nfp_16x16,
|
||||
}
|
||||
itemById[item.item_id] = items[itemCount]
|
||||
end
|
||||
end
|
||||
else
|
||||
itemById[baseId] = {
|
||||
id = baseId,
|
||||
icon_nfp = chain[2],
|
||||
}
|
||||
|
||||
for i = 1, #(chain[3] or {}) do
|
||||
local compactItem = chain[3][i]
|
||||
itemCount = itemCount + 1
|
||||
items[itemCount] = {
|
||||
id = item.item_id,
|
||||
icon_nfp = item.icon_nfp_16x16,
|
||||
id = compactItem[1],
|
||||
icon_nfp = compactItem[2],
|
||||
}
|
||||
itemById[item.item_id] = items[itemCount]
|
||||
itemById[compactItem[1]] = items[itemCount]
|
||||
end
|
||||
end
|
||||
|
||||
chainItemsByBaseId[chain.base_id] = items
|
||||
chainItemsByBaseId[baseId] = items
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -164,13 +189,49 @@ local function parseNfpImage(nfp)
|
||||
return image
|
||||
end
|
||||
|
||||
local function parseIconData(iconData)
|
||||
if type(iconData) == "string" then
|
||||
return parseNfpImage(iconData)
|
||||
end
|
||||
|
||||
if type(iconData) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
|
||||
local offsetX = tonumber(iconData[1]) or 0
|
||||
local offsetY = tonumber(iconData[2]) or 0
|
||||
local croppedImage = parseNfpImage(iconData[3])
|
||||
|
||||
if not croppedImage then
|
||||
return nil
|
||||
end
|
||||
|
||||
if offsetX == 0 and offsetY == 0 then
|
||||
return croppedImage
|
||||
end
|
||||
|
||||
local image = {}
|
||||
|
||||
for y, row in pairs(croppedImage) do
|
||||
local shiftedRow = {}
|
||||
|
||||
for x, value in pairs(row) do
|
||||
shiftedRow[x + offsetX] = value
|
||||
end
|
||||
|
||||
image[y + offsetY] = shiftedRow
|
||||
end
|
||||
|
||||
return image
|
||||
end
|
||||
|
||||
local function getItemIcon(item)
|
||||
if not item then
|
||||
return nil
|
||||
end
|
||||
|
||||
if item.icon == nil then
|
||||
item.icon = parseNfpImage(item.icon_nfp) or false
|
||||
item.icon = parseIconData(item.icon_nfp) or false
|
||||
item.icon_nfp = nil
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user