This repository has been archived by the owner on May 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fusioncrafter.lua
178 lines (139 loc) · 3.85 KB
/
fusioncrafter.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
-- fusioncrafter.lua
-- Author: sedlak477
-- This program automates Draconic Evolution Fusion Crafting
local transposer = require("component").transposer
local config = require("config")
-- Load side mappings from config
local input = config.inventories.input
local core = config.inventories.core
local injectors = config.inventories.injectors
local output = config.inventories.output
local result = config.inventories.result
-- Load recipes from config
local recipes = config.recipes
-- Greet user with a friendly message
local function printHello()
print("FusionCrafter v1.0 by sedlak477")
print()
end
-- Retrun amount of item in inventory
local function getItemAmount(id, side)
-- Look through all items to see if the requested exists
for stack in transposer.getAllStacks(side) do
if stack.name == id then
return stack.size
end
end
return 0
end
-- Check if recipe is craftable with
-- current items
local function canCraft(recipe)
for id, neededAmount in pairs(recipe.input) do
local availableAmount = getItemAmount(id, input)
if availableAmount < neededAmount then
return false
end
end
return true
end
-- Find item slot in inventory
-- Pass nil for item to find empty slot
-- Returns nil if no slot is found
local function find(inventory, item)
for slot = 1, transposer.getInventorySize(inventory), 1 do
if item == nil then
if transposer.getSlotStackSize(inventory, slot) == 0 then
return slot
end
else
local stack = transposer.getStackInSlot(inventory, slot)
if stack ~= nil and stack.name == item then
return slot
end
end
end
end
-- Transfer 'amount' of item 'id' from 'from' to a free slot in 'to'
local function transfer(id, amount, from, to)
-- Get slots with items
local slotFrom = find(from, id)
local slotTo = find(to, nil)
-- Do some error checking
if slotFrom == nil then
print("Error: transfer: No item '" .. id .. "' in source inventory")
return
end
if slotTo == nil then
print("Error: transfer: No free slot in target inventory")
return
end
transposer.transferItem(from, to, amount, slotFrom, slotTo)
end
-- Wait until items are in inventory
local function waitItems(inventory, items)
while true do
local itemsExist = true
for item, amount in pairs(items) do
local slot = find(inventory, item)
if slot == nil then
itemsExist = false
break
end
if transposer.getSlotStackSize(inventory, slot) < amount then
itemsExist = false
break
end
end
if itemsExist then
return
end
os.sleep(1)
end
end
-- Craft provided recipe
local function craft(recipe)
-- Tell the user what we are doing
local resultString = ""
for item, amount in pairs(recipe.output) do
resultString = resultString .. item .. " "
end
io.write("Crafting " .. resultString .. "...")
-- Put input items into their places
for item, amount in pairs(recipe.input) do
-- If item is the core item transfer it to the core inventory
if item == recipe.core then
transfer(item, amount, input, core)
else -- Else put it into the injectors
transfer(item, amount, input, injectors)
end
end
-- Wait for output
waitItems(result, recipe.output)
-- Transfer items to output inventory
for item, amount in pairs(recipe.output) do
transfer(item, amount, result, output)
end
print(" Done")
end
-- Check if a valid recipe is in the input
local function checkCrafting()
-- Check all recipes if one is craftable
for i, recipe in pairs(recipes) do
-- If we can craft it, we craft it
if canCraft(recipe) then
craft(recipe)
return
end
end
os.sleep(2)
end
-- The fun starts here
local function main()
printHello()
print("Ready! Waiting for craftable recipes")
while true do
checkCrafting()
end
end
main()