-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcraftmanager.lua
More file actions
262 lines (230 loc) · 6.86 KB
/
Copy pathcraftmanager.lua
File metadata and controls
262 lines (230 loc) · 6.86 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
-- Spec: https://github.com/minetest/minetest/blob/master/src/craftdef.cpp
--
-- Utilities and some definitions
--
local bit = require("bit")
local function init_lookup_table(values)
local result = {}
for index, name in ipairs(values) do
result[name] = index
result[index] = name
end
setmetatable(result, {
__newindex = error,
__index = error,
})
return result
end
local RecipePriority = init_lookup_table({
"PRIORITY_NO_RECIPE", "PRIORITY_TOOLREPAIR",
"PRIORITY_SHAPELESS_AND_GROUPS", "PRIORITY_SHAPELESS",
"PRIORITY_SHAPED_AND_GROUPS", "PRIORITY_SHAPED",
})
-- TODO: Use this for methods to prevent mistakes (it is already messed up, needs some work to clean up methods)
--local CraftMethod = init_lookup_table({
-- "CRAFT_METHOD_NORMAL", "CRAFT_METHOD_COOKING", "CRAFT_METHOD_FUEL",
--})
local CRAFT_HASH_TYPE_MAX = 2
local HASH_TYPE_NAME = 1
local HASH_TYPE_COUNT = 2
local function getHashForGrid(hash_type, input_names)
if hash_type == HASH_TYPE_NAME then
local h = 0
for _, name in ipairs(input_names) do
h = h + #name
for c in name:gmatch(".") do
h = bit.bxor(h, bit.lshift(h, 5) + bit.rshift(h, 2) + c:byte())
end
h = bit.bxor(h, bit.lshift(h, 5) + bit.rshift(h, 2) + 10)
end
return h
elseif hash_type == HASH_TYPE_COUNT then
local count = 0
for _, name in ipairs(input_names) do
if name ~= "" then
count = count + 1
end
end
return count
end
return 0
end
local function insert_all_flat(t, src)
if type(src) == "userdata" then
table.insert(t, src:get_name())
elseif type(src) == "string" then
table.insert(t, src)
elseif type(src) == "table" then
for _, value in ipairs(src) do
insert_all_flat(t, value)
end
else
error("Invalid type in list")
end
return t
end
local function ensuretable(t, key)
if t[key] == nil then
t[key] = {}
end
assert(type(t[key] == "table"))
return t[key]
end
local function decrement_input(input)
for _,item in pairs(input.items) do
item:set_count(item:get_count() - 1)
end
-- FIXME: Should return replacements, see craftDecrementOrReplaceInput
return {}
end
local function match_fuel(input, def)
local item = #input.items == 1 and input.items[1]:get_name()
if item and input.type == def.type then
local recipe = def.recipe
if item == recipe then
return true
end
local itemdef = core.registered_items[item]
if itemdef then
local group = recipe:match("group:(.+)")
if group then
local groups = itemdef.groups
return groups and groups[group]
end
end
end
return false
end
--
-- CraftManager
--
local CraftManager = {}
-- @CraftInput input
-- TODO: @ItemStack[] output_replacements Craft replacements not implemented, possibly return value
-- @bool decrementInput
-- @return @CraftOutput|nil
function CraftManager:getCraftResult(input, decrementInput)
--local input_names = table.sort(insert_all_flat({}, input.items))
local input_names = insert_all_flat({}, input.items)
local priority_best = RecipePriority.PRIORITY_NO_RECIPE
local output
for hash_type = 1, CRAFT_HASH_TYPE_MAX, 1 do -- @CraftHashType
local hash = getHashForGrid(hash_type, input_names)
local hash_collisions = self.hashed_crafts[input.type][hash_type][hash] or {}
for i = #hash_collisions, 1, -1 do
local def = hash_collisions[i] -- @CraftDefinition
local priority = assert(def._recipe_priority) + 1 - hash_type -- @RecipePriority
if priority > priority_best then
--[[ FIXME: Full input recipe compatibility check, currently only for fuel recipes ]]
if input.type ~= "fuel" or match_fuel(input, def) then
-- FIXME: pick `time` value based on `method`
local out = { item = def.output, time = def.time or def.cooktime or def.burntime or 0 }
assert(def.type == "fuel" or out.item)
out.item = ItemStack(out.item)
if out.item:is_known() then
output = out
priority_best = priority
else
mineunit:warningf("Trying to craft non-existent '%s', ignoring recipe", out.item)
end
end
end
end
end
if output and decrementInput then
output.replacements = decrement_input(input)
end
return output
end
-- @CraftOutput output
-- @unsigned limit = 0
-- @return \CraftDefinition[]
function CraftManager:getCraftRecipes(output, limit)
--limit = limit or 0
end
-- @CraftOutput output
-- @return \bool
function CraftManager:clearCraftsByOutput(output)
end
-- @CraftInput input
-- @return \bool
function CraftManager:clearCraftsByInput(input)
end
-- @return \string
function CraftManager:dump()
return dump(self.registered_crafts)
end
-- @string method
-- @string priority TODO: Allow number
-- @string output
-- @CraftDefinition def
-- @return \nil
function CraftManager:registerCraft(method, priority, output, def)
-- Get name without additional stack data
local outname = method == "fuel" and def.burntime or output:match("^%S+")
def._recipe_priority = assert(RecipePriority[priority])
if not self.registered_crafts[method][outname] then
self.registered_crafts[method][outname] = {}
end
table.insert(self.registered_crafts[method][outname], def)
-- FIXME: Hashing should be done after mods been loaded and before globalstep starts
local hc = self.hashed_crafts
local items = self.get_craft_items(def)
table.insert(ensuretable(hc[method][HASH_TYPE_NAME], getHashForGrid(HASH_TYPE_NAME, items)), def)
table.insert(ensuretable(hc[method][HASH_TYPE_COUNT], getHashForGrid(HASH_TYPE_COUNT, items)), def)
end
-- @return \nil
function CraftManager:clear()
-- FIXME: Quick and easy but not good
self.registered_crafts = {
shaped = {},
shapeless = {},
toolrepair = {},
cooking = {},
fuel = {},
}
-- Create table for craft hashes, each sub table represents priority for hash type
self.hashed_crafts = {}
for k in pairs(self.registered_crafts) do
self.hashed_crafts[k] = {{},{},{}}
end
-- Alias normal -> shaped
self.registered_crafts.normal = self.registered_crafts.shaped
self.hashed_crafts.normal = self.hashed_crafts.shaped
end
-- @return \nil
function CraftManager:initHashes()
end
-- Utility functions
function CraftManager:recipe_to_craft(method, recipe)
for output, crafts in pairs(self.registered_crafts[method]) do
for _, craft in ipairs(crafts) do
if craft.recipe == recipe then
return craft
end
end
end
end
function CraftManager.get_craft_items(craft)
-- TODO: Add assertions based on recipe type, maybe hide this function?
return insert_all_flat({}, craft.recipe)
end
function CraftManager:registered_craft_recipe(output, method)
local crafts = self.registered_crafts[method or "normal"][output]
return crafts and crafts[1] and crafts[1].recipe
end
function CraftManager:registered_craft(output, method)
local crafts = self.registered_crafts[method or "normal"][output]
return crafts and crafts[1] or nil
end
mineunit.export_object(CraftManager, {
name = "CraftManager",
private = true,
constructor = function(self)
local obj = {}
setmetatable(obj, CraftManager)
obj:clear()
return obj
end,
})
return CraftManager()