-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaplingfarm.lua
More file actions
195 lines (169 loc) · 4.89 KB
/
saplingfarm.lua
File metadata and controls
195 lines (169 loc) · 4.89 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
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
local settings = {
axeActivatorSide = "top",
axeRechargerDirection = "south",
meInterfaceSide = "right",
meExtractDirection = "west",
sonicSensorSide = "left",
plantingPosition = {
Y = 1,
X = -1,
Z = -1
},
boneMealStack = {
id = 351,
dmg = 15,
qty = 1
},
saplingID = 2137,
boneMealCraftingQuantity = 63,
activatorPeripheralType = "tile_thermalexpansion_device_activator_name",
meInterfacePeripheralType = "me_interface",
sensorPeripheralType = "sensor"
}
function mainLoop()
print("SaplingFarm started.")
init()
while true do
checkBoneMealSupply()
local stack = getLowestQuantityStack()
if stack ~= nil then
print("Transferring sapling to planter activator")
transferStackToActivator(stack)
waitUntilBlockType("UNKNOWN")
print("Transferring bone meal to planter activator")
transferStackToActivator(settings.boneMealStack)
waitUntilBlockType("SOLID")
print("Chopping tree")
chopTree()
print("Recharging axe")
rechargeAxe()
else
print("Warning: No saplings were found in storage, waiting 5 minutes...")
sleep(5 * 60)
end
end
end
function init()
redstone.setOutput(settings.axeActivatorSide, false)
validatePeripheralType(settings.axeActivatorSide, settings.activatorPeripheralType)
validatePeripheralType(settings.meInterfaceSide, settings.meInterfacePeripheralType)
validatePeripheralType(settings.sonicSensorSide, settings.sensorPeripheralType)
if getCurrentBlockType() ~= nil then
error("Block at sapling planting location, cannot start")
end
end
function getLowestQuantityStack()
local me = peripheral.wrap(settings.meInterfaceSide)
local selectedStack = {}
local lowestQuantity = -1
for _, v in pairs(me.getAvailableItems()) do
if v["id"] == settings.saplingID then
if lowestQuantity == -1 or v["qty"] < lowestQuantity then
lowestQuantity = v["qty"]
selectedStack = {
id = v["id"],
dmg = v["dmg"],
qty = 1
}
end
end
end
if lowestQuantity ~= -1 then
return selectedStack
end
end
function checkBoneMealSupply()
local me = peripheral.wrap(settings.meInterfaceSide)
local quantity = getAEQuantity(me, settings.boneMealStack.id, settings.boneMealStack.dmg)
if quantity == 0 then
print("Bone meal supply depleted, crafting more...")
craftAEItem(me, settings.boneMealStack.id, settings.boneMealStack.dmg, settings.boneMealCraftingQuantity, true)
end
end
function transferStackToActivator(stack)
local me = peripheral.wrap(settings.meInterfaceSide)
local itemsExtracted = me.extractItem(stack, settings.meExtractDirection)
if itemsExtracted ~= 1 then
error("Unexpected result extracting stack ("..itemsExtracted..")")
end
end
function chopTree()
redstonePulse(settings.axeActivatorSide)
waitWhileBlockType("SOLID")
print("Block has disappeared, tree seems to have been chopped")
end
function rechargeAxe()
local axeActivator = peripheral.wrap(settings.axeActivatorSide)
if axeActivator.pushItem(settings.axeRechargerDirection, 1) ~= 1 then
print("Warning: Failed to push the axe into the charger")
end
print("Waiting for the axe to reappear in slot 1 of the activator...")
while axeActivator.getStackInSlot(1) == nil do
sleep(1)
end
end
function getCurrentBlockType()
return getBlockType(settings.sonicSensorSide, settings.plantingPosition)
end
function waitUntilBlockType(blockType)
print("Waiting for block type "..blockType.." to appear...")
while getCurrentBlockType() ~= blockType do
sleep(1)
end
end
function waitWhileBlockType(blockType)
print("Waiting for block type "..blockType.." to disappear...")
while getCurrentBlockType() == blockType do
sleep(1)
end
end
-- Generic functions
function getAEQuantity(peripheral, id, dmg)
local quantity = 0
for _, v in pairs(peripheral.getAvailableItems()) do
if v.id == id and v.dmg == dmg then
quantity = v.qty
end
end
return quantity
end
function craftAEItem(peripheral, id, dmg, quantity, wait)
local currentQuantity = getAEQuantity(peripheral, id, dmg)
local stack = {
id = id,
dmg = dmg,
qty = quantity
}
peripheral.requestCrafting(stack)
if wait then
local targetQuantity = currentQuantity + quantity
while currentQuantity < targetQuantity do
sleep(1)
currentQuantity = getAEQuantity(peripheral, id, dmg)
end
end
end
function getBlockType(sensorSide, position)
os.loadAPI("ocs/apis/sensor")
local s = sensor.wrap(sensorSide)
for _, v in pairs(s.getTargets()) do
if samePosition(v.Position, position) then
return v.Type
end
end
end
function samePosition(pos1, pos2)
return pos1.X == pos2.X and pos1.Y == pos2.Y and pos1.Z == pos2.Z
end
function validatePeripheralType(side, expectedType)
local actualType = peripheral.getType(side)
if actualType ~= expectedType then
error("Unexpected peripheral type on side '"..side.."': '"..actualType.."' (expected '"..expectedType.."')")
end
end
function redstonePulse(side)
redstone.setOutput(side, true)
sleep(1)
redstone.setOutput(side, false)
end
mainLoop()