-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrumswitcher.lua
More file actions
109 lines (92 loc) · 2.63 KB
/
drumswitcher.lua
File metadata and controls
109 lines (92 loc) · 2.63 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
local settings = {
drumSide = "front",
cableSide = "bottom",
activatorColor = colors.white,
lampColor = colors.orange,
cycleDelay = 5,
emptyDrumSlot = 1,
fullDrumSlot = 2,
activatorSignalDuration = 1,
lampDuration = 1,
drumPlacementDelay = 1
}
function mainLoop()
print("DrumSwitcher started.")
init(settings)
while true do
if isDrumFull() then
removeFullDrum()
os.sleep(settings.drumPlacementDelay)
placeEmptyDrum()
end
os.sleep(settings.cycleDelay)
end
end
function init()
redstone.setBundledOutput(settings.cableSide, 0)
end
function placeEmptyDrum()
if turtle.getItemCount(settings.emptyDrumSlot) == 0 then
blinkLightUntilSlotRefill()
end
turtle.select(settings.emptyDrumSlot)
turtle.place()
end
function removeFullDrum()
print("Sending redstone signal to activator")
redstone.setBundledOutput(settings.cableSide, settings.activatorColor)
os.sleep(settings.activatorSignalDuration)
redstone.setBundledOutput(settings.cableSide, 0)
print("Sucking full drum")
turtle.select(settings.fullDrumSlot)
if turtle.suck() then
turtle.dropUp()
else
error("Failed to suck full drum")
end
end
function blinkLightUntilSlotRefill()
local currentColor = redstone.getBundledOutput(settings.cableSide)
local signal = true
while turtle.getItemCount(settings.emptyDrumSlot) == 0 do
if signal then
redstone.setBundledOutput(settings.cableSide, colors.combine(currentColor, settings.lampColor))
else
redstone.setBundledOutput(settings.cableSide, currentColor)
end
os.sleep(settings.lampDuration)
signal = not signal
end
redstone.setBundledOutput(settings.cableSide, currentColor)
end
function isDrumFull()
local side = settings.drumSide
local tank = peripheral.wrap(side)
if tank == nil then
print("Found no peripheral on " .. side .. ", placing drum")
placeEmptyDrum()
return false
end
local tankInfo = tank.getTankInfo(side)
if tankInfo == nil then
print("Found no tank on " .. side)
return false
end
local capacity = 0
local amount = 0
for name, value in pairs(tankInfo[1]) do
if name == "capacity" then
capacity = value
end
if name == "amount" then
amount = value
end
end
print("Amount on " .. side .. " is " .. amount)
if amount >= capacity then
return true
else
return false
end
end
mainLoop()