-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcraftmanager.lua
235 lines (204 loc) · 6.21 KB
/
craftmanager.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
-- 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 = 1 -- FIXME: Implement HASH_TYPE_COUNT and increase this number
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
--
-- 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[hash_type][hash] or {}
for i = #hash_collisions, 1, -1 do
local def = hash_collisions[i] -- @CraftDefinition
local priority = assert(def._recipe_priority) -- @RecipePriority
if priority > priority_best --[[ FIXME: Check input recipe compatibility: and def:check(input) ]] then
local out = { item = def.output, time = def.time or def.cooktime or 0 }
assert(out.item) -- Not inlined because assert seems to return 3 arguments where last two are nil
out.item = ItemStack(out.item)
if out.item:is_known() then
output = out
priority_best = priority
else
mineunit:warning("trying to craft non-existent " .. out.item .. ", ignoring recipe")
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[HASH_TYPE_NAME], getHashForGrid(HASH_TYPE_NAME, items)), def)
table.insert(ensuretable(hc[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 = {},
}
-- Alias normal -> shaped
self.registered_crafts.normal = self.registered_crafts.shaped
-- Create table for craft hashes, each sub table represents priority for hash type
self.hashed_crafts = {{},{},{}}
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()