-
Notifications
You must be signed in to change notification settings - Fork 0
/
blood_altar.lua
247 lines (207 loc) · 6.4 KB
/
blood_altar.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
236
237
238
239
240
241
242
243
244
245
246
247
-- Blood Altar automation
-- version 0.1.0
-- Author: lushiita
-- Adapted from code by Rikenbacker for Runic Matric Automation
local component = require("component")
local sides = require("sides")
local event = require("event")
local json = require('json')
local thread = require("thread")
local baGui = require("ba_gui")
local gpu = component.gpu
local settings = {
refreshInterval = 1.0,
refreshAltarInterval = 2,
refreshMonitorInterval = 0.4,
inputSide = sides.west,
altarSide = sides.top,
outputSide = sides.east,
recipesFilename = "recipes.json"
}
local stages = {
waitInput = "Waiting",
waitEssence = "Life Essence Control",
transferItems = "Item placing",
waitImpregnation = "Life Essence Impregnation"
}
local status = {
stage = stages.waitInput,
recipeName = nil,
recipe = nil,
inputItem = nil,
itemName = nil,
message = nil,
crafting = nil,
craftingName = nil,
shootDown = false,
debugInfo = nil
}
local textLines = {
"Current status: $stage:s,%s$",
"Recipe: $recipeName:s,%s$",
"$message$",
"$craftingName$" --[[,
"$debugInfo$"
]]--
}
local Recipes = {}
function Recipes.new()
local recipes = {}
local f = io.open(settings.recipesFileName, "r")
if f~=nil then
recipes = json.decode(f:read("*all"))
io.close(f)
end
function recipes.getCount()
return #recipes
end
function recipe.findRecipe(inputItem)
for i = 1, #recipes do
if recipes[i].input == inputItem then
return recipes[i]
end
end
return nil
end
return recipes
end
local Tools = {}
function Tools.new()
local obj = {}
local interface = "me_interface"
local transposer = "transposer"
for address, type in component.list() do
if type == interface and obj[interface] == nil then
obj[interface] = component.proxy(address)
elseif type == transposer and obj[transposer] == nil then
obj[transposer] = component.proxy(address)
end
end
function obj.getInterface()
return obj[interface]
end
function obj.getTransposer()
return obj[transposer]
end
function obj.makeLabel(item)
return item.name .. "/" .. item.damage
end
function obj.getInput()
local items = {}
local values = obj[transposer].getAllStacks(settings.inputSide).getAll()
for i = 0, #values do
if values[i].size ~= nil then
table.insert(items, {name = obj.makeLabel(values[i]), size = values[i].size, position = i})
end
end
return items
end
function obj.checkAltar()
local values = obj[transposer].getAllStacks(settings.altarSide).getAll()
for i = 0, #values do
if values[i].size ~= nil then
return true
end
end
return false
end
function obj.transferItemToAltar(inputItems)
local itemName = inputItems[1].name
obj[transposer].transferItem(settings.inputSide, settings.AltarSide, 1, inputItems[1].position + 1, 1)
inputItems[1].size = inputItems[1].size - 1
return itemName
end
function obj.waitForImpregnation(itemName)
local isDone = false
if obj[transposer].getStackInSlot(settings.altarSide, 1) ~= nil then
if obj.makeLabel(obj[transposer].getStackInSlot(settings.altarSide, 1)) ~= itemName then
isDone = true
end
else
status.message = "Error: Result has disappeared"
isDone = true
end
if isDone == true then
local notEmpty = true
repeat
notEmpty = false
local values = obj[transposer].getStackInSlot(settings.altarSide, 1)
if values ~= nil then
notEmpty = true
end
obj[transposer].transferItem(settings.altarSide, settings.outpuSide)
os.sleep(settings.refreshAltarInterval)
until (notEmpty == false)
end
return isDone
end
return obj
end
function mainLoop(tools, recipes)
while status.shootDown == false do
if status.stage == stages.waitInput then
if tools.checkAltar() == true then
status.message = "&red;Blood Altar is busy!"
else
status.inputItems = tools.getInput()
if #status.inputItems > 0 then
status.recipe = recipes.findRecipe(status.inputItems)
if status.recipe == nil then
status.message = "&red;Error: Recipe not found!"
else
status.recipeName = "&green;" .. status.recipe.name
status.stage = stages.waitEssence
end
else
stage.message = nil
end
end
end
if status.stage == stages.waitEssence then
if tools.checkEssence(status.recipe) == true then
status.stage = stage.transferItems
status.crafting = nil
status.message = nil
status.craftingName = nil
end
end
if status.stage == stage.waitImpregnation then
if tools.waitForImpregnation(status.itemName) == true then
status.stage = stages.waitInput
status.recipe = nil
status.message = nil
status.recipeName = nil
end
end
os.sleep(settings.refreshInterval)
end
end
function main()
local tools = Tools.new()
local recipes = Recipes.new()
if tools.getInterface() ~= nil then
print("ME Interface found")
else
print("ERROR: ME Interface not found!")
return 1
end
if tools.getTransposer() ~= nil then
print("Transposer found")
else
print("ERROR: transposer not found!")
return 1
end
print("Recipes loaded:", recipes.getCount())
thread.create(
function()
mainLoop(tools, recipes)
end
):detach()
local screen = ScreenController.new(gpu, textLines)
repeat
screen.render(status)
until event.pull(settings.refreshMonitorInterval, "interrupted")
status.shootDown = true
screen.resetScreen()
end
main()