Blackhome

BoneMealTransport

Jun 5th, 2025 (edited)
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.95 KB | None | 0 0
  1. -- pastebin get tDYAq4GS BoneMealTransporter
  2.  
  3. local distanceX, distanceZ = ...
  4.  
  5. if not distanceX then distanceX = -11 end
  6. if not distanceZ then distanceZ = 8 end
  7.  
  8. distanceX = tonumber(distanceX)
  9. distanceZ = tostring(distanceZ)
  10.  
  11. local turtlePos, direction
  12. local startPos, startDir
  13.  
  14.  
  15. local function getGPSPosition()
  16.     local x, y, z = gps.locate()
  17.     if not x then print("Error: GPS not available") return nil end
  18.     return {x = math.floor(x), y = math.floor(y), z = math.floor(z)}
  19. end
  20.  
  21. local function negative(vec)
  22.     return {x = -vec.x, y = -vec.y, z = -vec.z}
  23. end
  24.  
  25. local function add(vec1, vec2)
  26.     return {x = vec1.x + vec2.x, y = vec1.y + vec2.y, z = vec1.z + vec2.z}
  27. end
  28. local function sub(vec1, vec2)
  29.     return add(vec1, negative(vec2))
  30. end
  31.  
  32. local function multiply(s, vec)
  33.     return {x = s * vec.x, y = s * vec.y, z = s * vec.z}
  34. end
  35.  
  36. local function getSign(number)
  37.     return number / math.abs(number)
  38. end
  39.  
  40. local function isEqual(vec1, vec2)
  41.     return vec1.x == vec2.x and vec1.y == vec2.y and vec1.z == vec2.z
  42. end
  43.  
  44.  
  45. -- === Basic Movement ===
  46.  
  47. local function moveForward()
  48.     if turtle.forward() then
  49.         turtlePos = add(turtlePos, direction)
  50.         return true
  51.     end
  52.     return false
  53. end
  54. local function moveUp()
  55.     if turtle.up() then
  56.         turtlePos.y = turtlePos.y + 1
  57.         return true
  58.     end
  59.     return false
  60. end
  61.  
  62. local function moveDown()
  63.     if turtle.down() then
  64.         turtlePos.y = turtlePos.y - 1
  65.         return true
  66.     end
  67.     return false
  68. end
  69.  
  70.  
  71. -- === Basic Turning ===
  72.  
  73. local function turnInDirection(dir)
  74.     if not direction then return end
  75.     if dir == "left" then
  76.         turtle.turnLeft()
  77.         direction = {x = direction.z, y = 0, z = -direction.x}
  78.     elseif dir == "right" then
  79.         turtle.turnRight()
  80.         direction = {x = -direction.z, y = 0, z = direction.x}
  81.     elseif dir == "around" then
  82.         turtle.turnLeft()
  83.         turtle.turnLeft()
  84.         direction = {x = -direction.x, y = 0, z = -direction.z}
  85.     end
  86. end
  87.  
  88. local function getTurnDirection(from, to)
  89.     local function vectorsEqual(a, b)
  90.         return a.x == b.x and a.z == b.z
  91.     end
  92.  
  93.     if vectorsEqual(from, to) then return nil end
  94.     local left  = {x = from.z, y = 0, z = -from.x}
  95.     local right = {x = -from.z, y = 0, z = from.x}
  96.     local back  = {x = -from.x, y = 0, z = -from.z}
  97.     if vectorsEqual(to, left) then
  98.         return "left"
  99.     elseif vectorsEqual(to, right) then
  100.         return "right"
  101.     elseif vectorsEqual(to, back) then
  102.         return "around"
  103.     else
  104.         error("Invalid target direction")
  105.     end
  106. end
  107.  
  108. local function getDirection()
  109.     local pos1 = getGPSPosition()
  110.     if not pos1 then error("GPS failure, cannot get direction") end
  111.  
  112.     for i = 1, 4 do
  113.         if turtle.forward() then
  114.             local pos2 = getGPSPosition()
  115.             if not pos2 then error("GPS failure after move") end
  116.             -- turtlePos = pos2
  117.             -- Richtung berechnen
  118.             direction = sub(pos2, pos1)
  119.  
  120.             -- zurück zur ursprünglichen Position
  121.             turnInDirection("around")
  122.             moveForward()
  123.  
  124.             -- Sicherheitsprüfung
  125.             local posBack = getGPSPosition()
  126.             if not isEqual(pos1, posBack) then
  127.                 error("Warnung: Position hat sich beim Rückweg verschoben!")
  128.                 turtlePos = posBack -- explizit wieder auf GPS setzen
  129.             else
  130.                 turtlePos = pos1
  131.             end
  132.  
  133.             turnInDirection("around")
  134.             return
  135.         else
  136.             turtle.turnLeft()
  137.         end
  138.     end
  139.  
  140.     error("Unable to move forward in any direction for direction detection")
  141. end
  142.  
  143.  
  144. -- === Positioning ===
  145.  
  146. local function goToLift()
  147.     local coord = add(startPos, startDir)
  148.  
  149.     local deltaX = coord.x - turtlePos.x
  150.     local deltaZ = coord.z - turtlePos.z
  151.  
  152.     local dirX = startDir.x
  153.     local dirZ = startDir.z
  154.  
  155.     local function moveToEqualX()
  156.         while not (deltaX == 0) do
  157.             turnInDirection(getTurnDirection(direction, {x = getSign(deltaX), y = 0, z = 0}))
  158.             moveForward()
  159.             deltaX = coord.x - turtlePos.x
  160.         end
  161.     end
  162.  
  163.     local function moveToEqualZ()
  164.         while not (deltaZ == 0) do
  165.             turnInDirection(getTurnDirection(direction, {x = 0, y = 0, z = getSign(deltaZ)}))
  166.             moveForward()
  167.             deltaZ = coord.z - turtlePos.z
  168.         end
  169.     end
  170.  
  171.     if dirX == 0 then
  172.         moveToEqualX()
  173.         moveToEqualZ()
  174.     elseif dirZ == 0 then
  175.         moveToEqualZ()
  176.         moveToEqualX()
  177.     end
  178. end
  179.  
  180. local function goToPos(coord)
  181.     local deltaX = coord.x - turtlePos.x
  182.     local deltaY = coord.y - turtlePos.y
  183.     local deltaZ = coord.z - turtlePos.z
  184.  
  185.     local dirX = startDir.x
  186.     local dirZ = startDir.z
  187.  
  188.     local function moveToEqualX()
  189.         while not (deltaX == 0) do
  190.             turnInDirection(getTurnDirection(direction, {x = getSign(deltaX), y = 0, z = 0}))
  191.             moveForward()
  192.             deltaX = coord.x - turtlePos.x
  193.         end
  194.     end
  195.  
  196.     local function moveToEqualZ()
  197.         while not (deltaZ == 0) do
  198.             turnInDirection(getTurnDirection(direction, {x = 0, y = 0, z = getSign(deltaZ)}))
  199.             moveForward()
  200.             deltaZ = coord.z - turtlePos.z
  201.         end
  202.     end
  203.  
  204.     local function moveToEqualY()
  205.         while not (deltaY == 0) do
  206.             if deltaY > 0 then
  207.                 moveUp()
  208.             elseif deltaY < 0 then
  209.                 moveDown()
  210.             end
  211.             deltaY = coord.y - turtlePos.y
  212.         end
  213.     end
  214.  
  215.     if not (deltaY == 0) then
  216.         goToLift()
  217.         moveToEqualY()
  218.         deltaX = coord.x - turtlePos.x
  219.         deltaZ = coord.z - turtlePos.z
  220.     end
  221.     if dirX == 0 then
  222.         moveToEqualZ()
  223.         moveToEqualX()
  224.     elseif dirZ == 0 then
  225.         moveToEqualX()
  226.         moveToEqualZ()
  227.     end
  228. end
  229.  
  230.  
  231.  
  232. -- === Initialization ===
  233.  
  234. local function saveStartingParameters()
  235.     local parameterList = {pos = turtlePos, dir = direction, diffX = distanceX, diffZ = distanceZ}
  236.  
  237.     local file = fs.open("startingParameters.txt", "w")
  238.     file.write(textutils.serialize(parameterList))
  239.     file.close()
  240. end
  241.  
  242. local function loadStartingParameters()
  243.     if not fs.exists("startingParameters.txt") then return false end
  244.  
  245.     local file = fs.open("startingParameters.txt", "r")
  246.     local data = textutils.unserialize(file.readAll())
  247.  
  248.     file.close()
  249.  
  250.     startPos = data.pos
  251.     startDir = data.dir
  252.     distanceX = data.diffX
  253.     distanceZ = data.diffZ
  254.     return true
  255. end
  256.  
  257. local function initProgram()
  258.     turtlePos = getGPSPosition()
  259.     if not turtlePos then error("GPS failure, cannot run the program") end
  260.    
  261.     getDirection()
  262.  
  263.     if not loadStartingParameters() then
  264.         startPos = turtlePos
  265.         startDir = direction
  266.         saveStartingParameters()
  267.     end
  268. end
  269.  
  270.  
  271. -- === Inventory ===
  272. local function isBoneMeal(item)
  273.     if not item or not item.name then return false end
  274.     return item.name == "minecraft:bone_meal"
  275. end
  276. local function getBoneMealCount()
  277.     local num = 0
  278.     for i = 1, 16 do
  279.         local item = turtle.getItemDetail(i)
  280.        
  281.         if isBoneMeal(item) then
  282.             num = num + turtle.getItemCount(i)
  283.         end
  284.     end
  285.     return num
  286. end
  287. local function selectBoneMealSlot()
  288.     for i = 1, 16 do
  289.         local item = turtle.getItemDetail(i)
  290.        
  291.         if isBoneMeal(item) then
  292.             turtle.select(i)
  293.             return true
  294.         end
  295.     end
  296.     return false
  297. end
  298.  
  299. local function compactInventory()
  300.     for i = 1, 16 do
  301.         local itemI = turtle.getItemDetail(i)
  302.         if itemI and itemI.count < 64 then
  303.             for j = i + 1, 16 do
  304.                 local itemJ = turtle.getItemDetail(j)
  305.                 if itemJ and itemJ.name == itemI.name then
  306.                     turtle.select(j)
  307.                     turtle.transferTo(i)
  308.                    
  309.                     -- Aktuellen Slot updaten
  310.                     itemI = turtle.getItemDetail(i)
  311.                     if not itemI or itemI.count == 64 then
  312.                         break
  313.                     end
  314.                 end
  315.             end
  316.         end
  317.     end
  318.     turtle.select(1) -- zum Standard zurückkehren
  319. end
  320.  
  321. local function dumpInventory()
  322.     local success, chest = turtle.inspect()
  323.     while not (success and chest.name == "minecraft:chest") do
  324.         print("No chest found. Pls place one in front.")
  325.         print("Press enter to continue.")
  326.         read()
  327.         success, chest = turtle.inspect()
  328.     end
  329.  
  330.     for slot = 1, 16 do
  331.         local item = turtle.getItemDetail(slot)
  332.         if item then
  333.             turtle.select(slot)
  334.             while not turtle.drop() do sleep(5) end
  335.         end
  336.     end
  337.     turtle.select(1)
  338. end
  339.  
  340. local function turtleNeedsFuel()
  341.     local fuelLevel = turtle.getFuelLevel()
  342.  
  343.     if fuelLevel < 1000 then
  344.         if not isEqual(startPos, turtlePos) then
  345.             goToPos(startPos)
  346.         end
  347.         print("Turtle needs more fuel")
  348.         return true
  349.     end
  350.     return false
  351. end
  352.  
  353.  
  354.  
  355. local function main()
  356.     local diffToFinish = {x = distanceX, y = 0, z = distanceZ}
  357.     local finishPos = add(startPos, diffToFinish)
  358.     while true do
  359.         if turtleNeedsFuel() then
  360.             return
  361.         end
  362.  
  363.         local bEnoughBoneMeal = true
  364.         goToPos(startPos)
  365.         for i=1, 16 do
  366.             turtle.select(i)
  367.             turtle.suckDown()
  368.             if not selectBoneMealSlot() then
  369.                 bEnoughBoneMeal = false
  370.             end
  371.         end
  372.         if not (bEnoughBoneMeal and getBoneMealCount() >= 64) then
  373.             bEnoughBoneMeal = false
  374.             sleep(60)
  375.         end
  376.  
  377.         if bEnoughBoneMeal then
  378.             compactInventory()
  379.             goToPos(finishPos)
  380.             turnInDirection(getTurnDirection(direction, startDir))
  381.             dumpInventory()
  382.         end
  383.     end
  384. end
  385.  
  386. initProgram()
  387. main()
Advertisement
Add Comment
Please, Sign In to add comment