forked from ReikaKalseki/DragonIndustries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.lua
139 lines (128 loc) · 4.12 KB
/
items.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
require "registration"
--"drop" follows standard https://wiki.factorio.com/Types/ProductPrototype / https://wiki.factorio.com/Types/ItemProductPrototype
function addMineableDropToEntity(proto, drop)
if not proto.minable then proto.minable = {mining_time = 1, results = {}} end
if not proto.minable.results then
proto.minable.results = {}
table.insert(proto.minable.results, {type = "item", name = proto.minable.result, amount = proto.minable.count})
proto.minable.result = nil
end
table.insert(proto.minable.results, drop)
end
function getItemByName(name)
if game then return game.item_prototypes[name] end
local keys = {"item", "tool", "ammo", "repair-tool", "selection-tool", "item-with-entity-data", "capsule", "armor", "module", "gun"}
for _,k in pairs(keys) do
if data.raw[k][name] then
return data.raw[k][name]
end
end
end
function getItemCategory(item)
if type(item) == "string" then
item = getItemByName(item)
end
if not item then error("No such item!") end
if game then
return item.group
else
local sub = item.subgroup
return data.raw["item-subgroup"].group
end
end
function getFluidByName(name)
if game then return game.fluid_prototypes[name] end
return data.raw.fluid[name]
end
function getItemType(name)
if getFluidByName(name) then
return "fluid"
elseif getItemByName(name) then
return "item"
else
error("Item " .. name .. " does not exist in any form!")
end
end
function tryLoadItem(name, amt)
if getItemByName(name) then
return {type = "item", name = name, amount = amt}
elseif getFluidByName(name) then
return {type = "fluid", name = name, amount = amt}
else
return nil
end
end
function tryLoadItemWithFallback(name, amt, fallback, fallbackamt)
local val = tryLoadItem(name, amt)
if val == nil and fallback ~= nil then
amt = fallbackamt and fallbackamt or amt
log("Item " .. name .. " not found; switching to fallback " .. fallback .. " x" .. amt)
val = tryLoadItem(fallback, amt)
end
return val
end
--This is an expensive function to call!
function getEntityCategory(item)
if type(item) == "string" then item = data.raw.item[item] end
if not item then error(serpent.block("No such item found!")) end
local place = item.place_result
if place then
local proto = getPrototypeByName(place)
return proto and proto.type or nil
end
end
local function isEntryInCategory(item, cat, nest)
--log("Seeking for " .. serpent.block(item) .. " @ " .. nest)
if type(item) == "table" then
if nest == 0 and not item.name then --is a list of items
--log("Parsing list tabled value " .. serpent.block(item))
for k,e in pairs(item) do
if isEntryInCategory(e, cat, 1) then
return true
end
end
elseif item.name then --actually a table value, likely {type, name, amount}
--log("Parsing named tabled value " .. serpent.block(item))
if item.type == "item" then
local var = data.raw.item[item.name]
--log("plain item lookup is: " .. (var and var.name or "null"))
if var == nil then
var = data.raw.tool[item.name]
--log("tool item lookup is: " .. (var and var.name or "null"))
end
if var == nil then
var = data.raw["repair-tool"][item.name]
--log("repairtool item lookup is: " .. (var and var.name or "null"))
end
if var == nil then
var = data.raw.ammo[item.name]
--log("ammo item lookup is: " .. (var and var.name or "null"))
end
if var == nil then
var = data.raw.capsule[item.name]
--log("capsule item lookup is: " .. (var and var.name or "null"))
end
if var == nil then error("Recipe produces nonexistent item '" .. item.name .. "'!") end
if isEntryInCategory(item.name, cat, nest+1) then
return true
end
else
end
else
--log("ERROR Parsing tabled value " .. serpent.block(item))
end
return false
elseif type(item) == "string" then
local ref = getItemByName(item)
if not ref then error("Recipe produces nonexistent item '" .. item .. "'!") end
if not ref.place_result then return false end
for name,proto in pairs(data.raw[cat]) do
if ref.place_result == name then
return true
end
end
end
end
function isItemInCategory(item, cat)
return isEntryInCategory(item, cat, 0)
end