# Mining Turtle Reworked

Jun 24th, 2024
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. local args = {...}
2. -- north 0 osten 1 süden 2 westen 3  | n z+1 o x+1 s z-1 w x-1 | hoch y+1 runter y-1
3. local function createVector(setx,sety,setz)
4.     return {x=setx,y=sety,z=setz}
5. end
6.
7. function VectorAreEqual(vec1, vec2)
8.     return vec1.x == vec2.x and vec1.y == vec2.y and vec1.z == vec2.z
9. end
10.
11. rows = 0
12. columns = 0
13. atHome = false
14. maxSlots = 16
15. facing = nil
16. actualCoordinates = createVector(0,0,0)
17. nextCheckPoint = createVector(0,0,0)
18. neededStepsForCheckPoint = 0
19. startCoordinates = createVector(0,0,0)
20. for key, value in pairs(startCoordinates) do
21.     print(key .. ": " .. value)
22. end
23.
24. function initilise()
25.     if #args == 2 then
26.         columns = tonumber(args[1])-1
27.         rows = tonumber(args[2])-1
28.     end
29.
30.     if columns == 0 then
31.         term.clear()
32.         term.setCursorPos(1, 1)
33.         io.write("Columns: ")
35.     end
36.
37.     if rows == 0 then
38.         term.clear()
39.         term.setCursorPos(1, 1)
40.         io.write("Rows: ")
42.     end
43.
44.     term.clear()
45.     term.setCursorPos(1, 1)
46.
47.     --end initial fase
48. end
49.
50. function goTo(position)
51.     local x1, y1, z1 = gps.locate(5)
52.     while actualCoordinates.y < position.y do
53.         if tryUpwards() then
54.             --y1 = y1 +1
55.         else
56.             return true
57.         end
58.     end
59.     while actualCoordinates.y > position.y do
60.         if tryDownwards() then
61.             --y1 = y1 -1
62.         else
63.             return true
64.         end
65.     end
66.     while actualCoordinates.z < position.z do
67.         --nach süden
68.         faceTo(2)
69.         tryForwards()
70.     end
71.     while actualCoordinates.z > position.z do
72.         --nach norden
73.         faceTo(0)
74.         tryForwards()
75.     end
76.     while actualCoordinates.x > position.x do
77.         --nach westen
78.         faceTo(3)
79.         tryForwards()
80.     end
81.     while actualCoordinates.x < position.x do
82.         --nach osten
83.         faceTo(1)
84.         tryForwards()
85.     end
86. end
87.
88. function faceTo(sideToFace)
89.     local diff = (sideToFace - facing) % 4
90.     if diff == 0 then
91.         return  -- Already facing the desired direction
92.     elseif diff == 1 then
93.         turtle.turnRight()
94.     elseif diff == 2 then
95.         turtle.turnRight()
96.         turtle.turnRight()
97.     elseif diff == 3 then
98.         turtle.turnLeft()
99.     end
100.     facing = sideToFace
101. end
102.
103. local function turnLeft()
104. end
105.
106. local function turnRight()
107. end
108.
109. local function mine()
110. end
111.
112. function checkFuel()
113.     local minFuelLevel = tonumber(startCoordinates.x) + tonumber(startCoordinates.y) + tonumber(startCoordinates.z) + 1
114.     if minFuelLevel < 100 then
115.         minFuelLevel = 100
116.     end
117.     if turtle.getFuelLevel() <= minFuelLevel  then
118.         refuelFromChest()
119.         checkFuel()
120.     end
121. end
122.
123. function returnHome()
124.     goTo(startCoordinates)
125.     print("Finished")
126.     os.exit()
127. end
128.
129. function refuelFromChest()
130.     local search = maxSlots
131.     if not pullInventarToChest() then
132.         for search = maxSlots, 3, -1 do
133.             turtle.select(search)
134.             turtle.refuel(turtle.getItemCount(search))
135.         end
136.         statusText = "Inventar full return home"
137.         info()
138.         returnHome()
139.         do return end
140.     end
141.     turtle.select(2)
142.     turtle.place()
143.     turtle.select(3)
144.     turtle.suck()
145.     turtle.refuel(turtle.getItemCount(3))
146.     turtle.drop()
147.     turtle.select(2)
148.     turtle.dig()
149. end
150.
151. function proofBedrockDown()
152.     local succes, block = turtle.inspectDown()
153.     if succes then
154.         if block.name == "minecraft:bedrock" then
155.             return true
156.         else
157.             return false
158.         end
159.     else
160.         return false
161.     end
162. end
163.
164. function proofBedrockFront()
165.     local succes, block = turtle.inspect()
166.     if succes then
167.         if block.name == "minecraft:bedrock" then
168.             return true
169.         else
170.             return false
171.         end
172.     else
173.         return false
174.     end
175. end
176.
177. function pullInventarToChest()
178.     local search = 0
179.     turtle.dig()
180.     turtle.dig()
181.     turtle.select(1)
182.     turtle.place()
183.     for search = maxSlots, 3, -1 do
184.         turtle.select(search)
185.         turtle.drop()
186.     end
187.
188.     turtle.select(1)
189.     turtle.dig()
190.
191.     --Wenn inventar nicht geleert werden kann zurueck zum Anfang
192.     if turtle.getItemCount(16) ~= 0 and turtle.getItemCount(3) ~= 0 then
193.         statusText = "Inventar full return home"
194.         return false
195.     end
196.     return true
197. end
198.
199. local function checkFull()
200.     local returnment = false
201.     if turtle.getItemCount(16) ~= 0 then
202.         returnment = not pullInventarToChest()
203.     end
204.     if returnment then
205.         pullInventarToChest()
206.     end
207. end
208.
209. function tryForwards()
210.     --Nachtanken wenn benötigt
211.     checkFuel()
212.     if proofBedrockFront() then
213.         returnHome()
214.         return false
215.     end
216.     while not turtle.forward() do
217.         if turtle.detect() then
218.             if turtle.dig() then
219.                 checkFull()
220.             end
221.         elseif turtle.attack() then
222.             checkFull()
223.             --wenn kein block vor der turtle ist dann ist es ein mob
224.         else
225.             sleep(0.5)
226.         end
227.     end
228.
229.     --positionAktualisieren
230.     if facing == 0 then
231.         actualCoordinates.z = actualCoordinates.z - 1
232.     elseif facing == 1 then
233.         actualCoordinates.x = actualCoordinates.x + 1
234.     elseif facing == 2 then
235.         actualCoordinates.z = actualCoordinates.z + 1
236.     elseif facing == 3 then
237.         actualCoordinates.x = actualCoordinates.x - 1
238.     end
239.
240.     turtle.digUp()
241.     turtle.digDown()
242.     checkFull()
243.     info()
244.     return true
245. end
246.
247. function tryBackwards()
248.     checkFuel()
249.     if not turtle.back() then
250.         turtle.turnRight()
251.         turtle.turnRight()
252.         tryForwards()
253.         turtle.turnRight()
254.         turtle.turnRight()
255.     end
256.     if facing == 0 then
257.         actualCoordinates.z = actualCoordinates.z + 1
258.     elseif facing == 1 then
259.         actualCoordinates.x = actualCoordinates.x - 1
260.     elseif facing == 2 then
261.         actualCoordinates.z = actualCoordinates.z - 1
262.     elseif facing == 3 then
263.         actualCoordinates.x = actualCoordinates.x + 1
264.     end
265.     return true
266. end
267.
268. function tryUpwards()
269.     checkFuel()
270.     while not turtle.up() do
271.         if turtle.detectUp() then
272.             if turtle.digUp() then
273.                 checkFull()
274.             end
275.         elseif turtle.attackUp() then
276.             checkFull()
277.         else
278.             sleep(0.5)
279.         end
280.     end
281.     actualCoordinates.y = actualCoordinates.y +1
282.     return true
283. end
284.
285. function tryDownwards()
286.     checkFuel()
287.     if proofBedrockDown() then
288.         returnHome()
289.         return false
290.     end
291.     while not turtle.down() do
292.         if turtle.detectDown() then
293.             if turtle.digDown() then
294.                 checkFull()
295.             end
296.         elseif turtle.attackDown() then
297.             checkFull()
298.         else
299.             sleep(0.5)
300.         end
301.     end
302.     actualCoordinates.y = actualCoordinates.y -1
303.     return true
304. end
305.
306. function getFacing()
307.     local x1, y1, z1 = gps.locate()
308.     sleep(0.5)
309.     tryForwards()
310.     sleep(0.5)
311.     local x2,y2,z2 = gps.locate()
312.     tryBackwards()
313.     if z1 > z2 then
314.         facing = 0    --norden
315.     elseif z1 < z2 then
316.         facing = 2    --süden
317.     elseif x1 < x2 then
318.         facing = 1    --osten
319.     elseif x1 > x2 then
320.         facing = 3    --westen
321.     end
322.     sleep(0.2)
323.     return facing
324. end
325.
326. function mineTurnRight()
327.     local neededFacing = facing + 1
328.     if neededFacing == 4 then
329.         neededFacing = 0
330.     end
331.     faceTo(neededFacing)
332. end
333.
334. function mineTurnLeft()
335.     local neededFacing = facing -1
336.     if facing == 0 then
337.         neededFacing = 3
338.     end
339.     faceTo(neededFacing)
340. end
341.
342. function mineArea(width, length, reversed)
343.
344.     --goTo(createVector(targetX,actualCoordinates.y,targetZ))
345.     if (width % 2) == 1 then
346.         reversed = 1
347.     end
348.     local reducedLength = length-1
349.     tryDownwards()
350.     tryUpwards()
351.     local startFacing = facing
352.     local actualWidth = 0
353.     while actualWidth < width do
354.         --print(actualWidth)
355.         local axis = facing % 2
356.         local multiplyer = -1
357.         if axis == 0 then
358.             multiplyer = 1
359.         end
360.         multiplyer = multiplyer
361.         local targetZ = actualCoordinates.z + (facing == 0 and (multiplyer * -reducedLength) or facing == 2 and (multiplyer * reducedLength) or 0)
362.         local targetX = actualCoordinates.x + (facing == 1 and (multiplyer * -reducedLength) or facing == 3 and (multiplyer * reducedLength) or 0)
363.         goTo(createVector(targetX,actualCoordinates.y,targetZ))
364.         if actualWidth < (width-1) then
365.             targetZ = actualCoordinates.z + (startFacing == 1 and reversed * 1 or startFacing == 3 and reversed * -1 or 0)
366.             targetX = actualCoordinates.x + (startFacing == 0 and reversed * 1 or startFacing == 2 and reversed * -1 or 0)
367.             goTo(createVector(targetX,actualCoordinates.y,targetZ))
368.             if actualWidth % 2 == 0 then
369.                 if reversed == 1 then
370.                     mineTurnRight()
371.                 else
372.                     mineTurnLeft()
373.                 end
374.             else
375.                 if reversed == 1 then
376.                     mineTurnLeft()
377.                 else
378.                     mineTurnRight()
379.                 end
380.             end
381.         end
382.         actualWidth = actualWidth +1
383.     end
384.     reversed = reversed * -1
385.     mineTurnRight()
386.     mineTurnRight()
387.     local x1, y1, z1 = gps.locate(5)
388.     actualCoordinates = createVector(x1,y1,z1)
389.     goTo(createVector(x1,y1-3,z1))
390.     mineArea(width,length,reversed)
391. end
392.
393. function info()
394.     term.clear()
395.     term.setCursorPos(1, 1)
396.     print("---------------------------------------")
398.     print("---------------------------------------")
399.     print("Mining size: " .. rows + 1 .. " by " .. columns + 1)
400.     print("")
401.     print("X: " .. actualCoordinates.x .. " Y: " .. actualCoordinates.y .. " Z: " .. actualCoordinates.z)
402.     print("Fuel level: " .. turtle.getFuelLevel())
403.     print("")
404.     --print("Status: " .. statusText)
405. end
406.
407.
408. if #args == 2 then
409.     columns = tonumber(args[1])-1
410.     rows = tonumber(args[2])-1
411. end
412.
413. if columns == 0 then
414.     term.clear()
415.     term.setCursorPos(1, 1)
416.     io.write("Width: ")
418. end
419.
420. if rows == 0 then
421.     term.clear()
422.     term.setCursorPos(1, 1)
423.     io.write("Depth: ")
425. end
426.
427. term.clear()
428. term.setCursorPos(1, 1)
429.
430.
431. while turtle.getItemCount(1) ~= 1 do
432.     term.setCursorPos(1, 1)
433.     print("pleas place a enderchest with clear invertory into the first slot")
434.     sleep(5)
435. end
436. while turtle.getItemCount(2) ~= 1 do
437.     term.setCursorPos(1, 1)
438.     print("pleas place a enderchest with coal in it into the second slot")
439.     sleep(5)
440. end
441. local x, y, z = gps.locate(5)
442. if not x then
443.     print("Missing GPS")
444. else
445.     startCoordinates = createVector(x,y,z)
446.     actualCoordinates = createVector(x,y,z)
447.     print(tostring(startCoordinates.x) .. "|" .. tostring(startCoordinates.y) .. "|" .. tostring(startCoordinates.z))
448.     getFacing()
449.     print(facing)
450.     faceTo(0)
451.     --actualCoordinates.x = actualCoordinates.x +1
452.     --print(tostring(startCoordinates.x) .. "|" .. tostring(startCoordinates.y) .. "|" .. tostring(startCoordinates.z))
453.     --print(tostring(actualCoordinates.x) .. "|" .. tostring(actualCoordinates.y) .. "|" .. tostring(actualCoordinates.z))
454.     --goTo(createVector(125,89,177))
455.     --print(tostring(actualCoordinates.x) .. "|" .. tostring(actualCoordinates.y) .. "|" .. tostring(actualCoordinates.z))
456.     mineArea(columns,rows,1)
457.     --tryForwards()
458. end
459. --initilise()
Tags: