forked from Starkus/quarry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquarry.lua
More file actions
157 lines (132 loc) · 5.12 KB
/
quarry.lua
File metadata and controls
157 lines (132 loc) · 5.12 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
-- Load required APIs
os.loadAPI("inv") -- Assuming you have an inventory management API
os.loadAPI("t") -- Assuming you have a turtle movement API
-- Constants
local MAX_X = 16 -- Maximum distance to travel in the X direction
local MAX_Y = 16 -- Maximum distance to travel in the Y direction
local MAX_Z = 64 -- Maximum distance to travel in the Z direction
local FUEL_THRESHOLD = 10 -- Threshold to check fuel level
local USE_MODEM = true -- Use modem for communication (change to true if needed)
local CHANNEL = 123 -- Specify the desired channel number
-- Flags
local CHARCOAL_ONLY = false
-- Function to print messages with turtle's current position and additional information
local function printWithPositionAndInfo(message, info)
local positionString = "[" .. t.getX() .. ", " .. t.getY() .. ", " .. t.getZ() .. "]"
local fuelString = "Fuel: " .. turtle.getFuelLevel()
local inventorySpace = "Inventory Space: " .. (16 - inv.getOccupiedSlots()) .. "/16"
print(message .. " @ " .. positionString .. " - " .. fuelString .. " - " .. inventorySpace .. " - " .. info)
if USE_MODEM then
rednet.broadcast(message .. " " .. positionString .. " " .. fuelString .. " " .. inventorySpace .. " " .. info, "miningTurtle")
end
end
-- Function to drop items in a chest
local function dropInChest()
t.turnLeft()
local success, data = turtle.inspect()
if success and data.name == "minecraft:chest" then
printWithPositionAndInfo("Dropping items in chest", "Action: DropInventory")
for i = 1, 16 do
turtle.select(i)
local item = turtle.getItemDetail()
if item and (not CHARCOAL_ONLY or (item.name == "minecraft:coal" and item.damage == 1)) then
turtle.drop()
end
end
end
t.turnRight()
end
-- Function to return to the starting point
local function returnToStart()
printWithPositionAndInfo("Returning to starting point", "Action: ReturnToStart")
-- Face towards the starting point
if t.isFacingForward() then
t.turnAround()
end
-- Move to the starting point
t.fw(t.getX())
-- Turn back to the original orientation
if t.isFacingForward() then
t.turnAround()
end
end
-- Function to refuel and handle full inventory
local function refuelAndHandleInventory()
if inv.isInventoryFull() then
printWithPositionAndInfo("Dropping trash", "Action: DropTrash")
inv.dropThrash()
printWithPositionAndInfo("Stacking items", "Action: StackItems")
inv.stackItems()
if inv.isInventoryFull() then
printWithPositionAndInfo("Full inventory!", "Action: Stop")
return "FULL_INVENTORY"
end
end
if turtle.getFuelLevel() <= t.fuelNeededToGoBack() then
if not t.refuel() then
printWithPositionAndInfo("Out of fuel!", "Action: Stop")
return "OUT_OF_FUEL"
end
end
return nil
end
-- Function to move horizontally
local function moveHorizontally()
local errorCode = t.moveH(MAX_X, MAX_Y)
if errorCode == "BLOCKED_MOVEMENT" then
printWithPositionAndInfo("Hit bedrock, can't keep going", "Action: Stop")
return errorCode
elseif errorCode == "LAYER_COMPLETE" then
return errorCode
else
return errorCode
end
end
-- Function to pause and provide options to the user
local function pauseAndPrompt()
printWithPositionAndInfo("Mining paused. Options: (R)esume, (P)ause, (D)rop and Resume, (S)top", "Action: PausePrompt")
while true do
local _, key = os.pullEvent("char")
if key == "r" then
printWithPositionAndInfo("Resuming mining", "Action: Resume")
break
elseif key == "p" then
printWithPositionAndInfo("Mining paused", "Action: Pause")
returnToStart()
break
elseif key == "d" then
printWithPositionAndInfo("Dropping inventory and resuming", "Action: DropAndResume")
dropInChest()
break
elseif key == "s" then
printWithPositionAndInfo("Returning to starting point, dropping inventory, and stopping", "Action: ReturnDropAndStop")
returnToStart()
dropInChest()
break
end
end
end
-- Initial setup with a specific channel
if USE_MODEM then
rednet.open("right")
rednet.host("miningTurtle", "miningTurtle") -- Set the turtle as a host on the network
end
printWithPositionAndInfo("\n\n\n-- WELCOME TO THE MINING TURTLE --\n\n", "Action: Start")
-- Mining process
while true do
local result = goDown()
if result == "OUT_OF_FUEL" or result == "LAYER_COMPLETE" then
break
end
local inventoryError = refuelAndHandleInventory()
if inventoryError == "FULL_INVENTORY" then
printWithPositionAndInfo("Full inventory! Returning to surface.", "Action: ReturnAndStop")
break
end
mainLoop()
dropInChest()
end
-- Cleanup
if USE_MODEM then
rednet.close("right")
end