-
Notifications
You must be signed in to change notification settings - Fork 0
/
available_recipes_finder.lua
42 lines (34 loc) · 1.39 KB
/
available_recipes_finder.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
local function get_available_recipes_from_inventory(inventory, recipes) -- TODO returns itemId instead of item_id because of reagent json converted table, should be cleaned up
local availableRecipes = {}
-- Iterate through each recipe
for _, recipe in ipairs(recipes.allRecipes) do
local can_craft = true
-- Check each reagent in the recipe
for _, reagent in ipairs(recipe.reagents) do
local item_id = reagent.itemId
local requiredAmount = reagent.amount
-- Check if the reagent is present in the inventory and in sufficient quantity
if not inventory[item_id] or inventory[item_id] < requiredAmount then
can_craft = false
break -- No need to check further if one reagent is missing
end
end
-- If all reagents are present, add the recipe to the list of available recipes
if can_craft then
table.insert(availableRecipes, recipe)
end
end
return availableRecipes
end
local function get_reagents_from_recipe_id(recipe_id, recipes)
for _, recipe in ipairs(recipes) do
if recipe.itemId == recipe_id then
return recipe.reagents
end
end
return nil
end
return {
get_available_recipes_from_inventory = get_available_recipes_from_inventory,
get_reagents_from_recipe_id = get_reagents_from_recipe_id
}