Advertisement
DabDaddy6223

mine_client

Apr 14th, 2025 (edited)
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.10 KB | None | 0 0
  1. SERVER_ID = 0
  2.  
  3. FUEL_COUNT = -1
  4. FUEL_INDEX = -1
  5. ACCEPTABLE_FUEL = {"minecraft:coal"}
  6.  
  7. BLOCK_COUNT = -1
  8. BLOCK_INDEX = -1
  9. BUILDING_BLOCKS = {"minecraft:cobblestone", "minecraft:dirt", "minecraft:andesite", "minecraft:granite", "minecraft:diorite", "minecraft:cobbled_deepslate", "minecraft:tuff"}
  10.  
  11. function arrHasValue(arr, val)
  12.     for index, value in ipairs(arr) do
  13.         if value == val then
  14.             return true
  15.         end
  16.     end
  17.  
  18.     return false
  19. end
  20.  
  21. function hasFuel()
  22.     if FUEL_INDEX == -1 then
  23.         for i=1, 16 do
  24.             turtle.select(i)
  25.            
  26.             local slotData = turtle.getItemDetail()
  27.  
  28.             if slotData ~= nil then
  29.                 if arrHasValue(ACCEPTABLE_FUEL, slotData["name"]) == true then
  30.                     FUEL_INDEX = i
  31.                     FUEL_COUNT = slotData["count"]
  32.                     break
  33.                 end
  34.             end
  35.         end
  36.     end
  37.  
  38.     return FUEL_COUNT > 0
  39. end
  40.  
  41. function shouldRefuel()
  42.     return turtle.getFuelLevel() <= 0
  43. end
  44.  
  45. function refuel()
  46.     turtle.select(FUEL_INDEX)
  47.     turtle.refuel(1)
  48.     FUEL_COUNT = FUEL_COUNT - 1
  49. end
  50.  
  51. function getBlockIndex()
  52.     if BLOCK_COUNT == 0 then
  53.         BLOCK_INDEX = -1
  54.     end
  55.  
  56.     if BLOCK_INDEX == -1 then
  57.         for i=1, 16 do
  58.             turtle.select(i)
  59.            
  60.             local slotData = turtle.getItemDetail()
  61.  
  62.             if slotData ~= nil then
  63.                 if arrHasValue(BUILDING_BLOCKS, slotData["name"]) == true then
  64.                     BLOCK_INDEX = i
  65.                     BLOCK_COUNT = slotData["count"]
  66.                     break
  67.                 end
  68.             end
  69.         end
  70.     end
  71.  
  72.     return BLOCK_INDEX
  73. end
  74.  
  75. function dumpDown()
  76.     for i=1, 16 do
  77.         turtle.select(i)
  78.         local slotData = turtle.getItemDetail()
  79.  
  80.         if slotData ~= nil then
  81.             if arrHasValue(ACCEPTABLE_FUEL, slotData["name"]) == false then
  82.                 turtle.dropDown(slotData["count"])
  83.             end
  84.         end
  85.     end
  86. end
  87.  
  88. -- +x -> east
  89. -- -x -> west
  90. -- -z -> north
  91. -- +z -> south
  92. function getFacingDirection(x, z, prevX, prevZ)
  93.     if x < prevX then
  94.         return "west"
  95.     elseif x > prevX then
  96.         return "east"
  97.     end
  98.  
  99.     if z < prevZ then
  100.         return "north"
  101.     elseif z > prevZ then
  102.         return "south"
  103.     end
  104. end
  105.  
  106. -- +x -> east = 0
  107. -- -z -> north = 1
  108. -- -x -> west = 2
  109. -- +z -> south = 3
  110. function rotateToFace(currentFacing, target)
  111.     local turning = {
  112.         east = 0,
  113.         north = 1,
  114.         west = 2,
  115.         south = 3
  116.     }
  117.    
  118.     if currentFacing == target then
  119.         return
  120.     end
  121.  
  122.     local currFacingVal = turning[currentFacing]
  123.     local targFacingVal = turning[target]
  124.     local offset = targFacingVal - currFacingVal
  125.  
  126.     if offset < 0 then
  127.         offset = offset + 3
  128.     end
  129.  
  130.     local moveFacing = currentFacing
  131.     for i=0, offset do
  132.         moveFacing = left(moveFacing)
  133.     end
  134. end
  135.  
  136. -- +x -> east
  137. -- -x -> west
  138. -- -z -> north
  139. -- +z -> south
  140. function incrementByFacing(facing, x, z, increment)
  141.     if facing == "west" then
  142.         return (x - increment), z
  143.     elseif facing == "east" then
  144.         return (x + increment), z
  145.     elseif facing == "north" then
  146.         return x, (z - increment)
  147.     elseif facing == "south" then
  148.         return x, (z + increment)
  149.     end
  150. end
  151.  
  152. function left(facing)
  153.     turtle.turnLeft()
  154.     if facing == "west" then
  155.         return "south"
  156.     elseif facing == "east" then
  157.         return "north"
  158.     elseif facing == "north" then
  159.         return "west"
  160.     elseif facing == "south" then
  161.         return "east"
  162.     end
  163. end
  164.  
  165. function right(facing)
  166.     turtle.turnRight()
  167.     if facing == "west" then
  168.         return "north"
  169.     elseif facing == "east" then
  170.         return "south"
  171.     elseif facing == "north" then
  172.         return "east"
  173.     elseif facing == "south" then
  174.         return "west"
  175.     end
  176. end
  177.  
  178. function moveToward(x, z, facing)
  179.     local currX, currY, currZ = gps.locate()
  180.  
  181.     -- Change facing direction if we need to
  182.     local newFacing = facing
  183.     if currX < x then
  184.         rotateToFace(facing, "east")
  185.         newFacing = "east"
  186.     elseif currX > x then
  187.         rotateToFace(facing, "west")
  188.         newFacing = "west"
  189.     else
  190.         if currZ < z then
  191.             rotateToFace(facing, "south")
  192.             newFacing = "south"
  193.         elseif currZ > z then
  194.             rotateToFace(facing, "north")
  195.             newFacing = "north"
  196.         end
  197.     end
  198.  
  199.     -- Dig the block in front
  200.     if turtle.detect() == true then
  201.         turtle.dig()
  202.     end
  203.  
  204.     -- Move forward
  205.     turtle.forward()
  206.  
  207.     -- If there is no block below, place one if possible
  208.     if turtle.detectDown() == false then
  209.         local blockIndex = getBlockIndex()
  210.         if blockIndex ~= -1 then
  211.             turtle.select(blockIndex)
  212.             turtle.placeDown()
  213.         end
  214.     end
  215.  
  216.     -- Dig the block above
  217.     if turtle.detectUp() == true then
  218.         turtle.digUp()
  219.     end
  220.  
  221.     return newFacing
  222. end
  223.  
  224. function moveBackTo(x, z, facing)
  225.     local currX, currY, currZ = gps.locate()
  226.  
  227.     local newFacing = facing
  228.     if currZ < z then
  229.         rotateToFace(facing, "south")
  230.         newFacing = "south"
  231.     elseif currZ > z then
  232.         rotateToFace(facing, "north")
  233.         newFacing = "north"
  234.     else
  235.         if currX < x then
  236.             rotateToFace(facing, "east")
  237.             newFacing = "east"
  238.         elseif currX > x then
  239.             rotateToFace(facing, "west")
  240.             newFacing = "west"
  241.         end
  242.     end
  243.  
  244.     local success = turtle.forward()
  245.     if success == false then
  246.         return ""
  247.     end
  248.  
  249.     -- Pick up any blocks that may have dropped on the ground (e.g. gravel)
  250.     turtle.suck()
  251.  
  252.     return newFacing
  253. end
  254.  
  255. function reachedTarget(x, z, targetX, targetZ)
  256.     return x == targetX and z == targetZ
  257. end
  258.  
  259. function loop(targetX, targetZ, facing)
  260.     if shouldRefuel() == true then
  261.         if hasFuel() == true then
  262.             refuel()
  263.         else
  264.             print("Not enough fuel!")
  265.             rednet.send(SERVER_ID, "out_of_fuel", "minenettoserver")
  266.             return ""
  267.         end
  268.     end
  269.  
  270.     return moveToward(targetX, targetZ, facing)
  271. end
  272.  
  273. function returnTo(targetX, targetZ, facing)
  274.     if shouldRefuel() == true then
  275.         if hasFuel() == true then
  276.             refuel()
  277.         else
  278.             print("Not enough fuel!")
  279.             rednet.send(SERVER_ID, "out_of_fuel", "minenettoserver")
  280.             return ""
  281.         end
  282.     end
  283.  
  284.     return moveBackTo(targetX, targetZ, facing)
  285. end
  286.  
  287. function prepare()
  288.     print("Connecting...")
  289.     rednet.broadcast("turtle", "minenettoserver")
  290.  
  291.     local id, message = rednet.receive("minenettoclient")
  292.     SERVER_ID = id
  293.  
  294.     print("Connected!")
  295.  
  296.     local id, distance = rednet.receive("minenettoclient")
  297.  
  298.     if shouldRefuel() == true then
  299.         if hasFuel() == true then
  300.             refuel()
  301.         else
  302.             print("Not enough fuel!")
  303.             rednet.send(SERVER_ID, "out_of_fuel", "minenettoserver")
  304.             return nil
  305.         end
  306.     end
  307.  
  308.     return distance
  309. end
  310.  
  311. function main()
  312.     rednet.open("left")
  313.     if rednet.isOpen("left") ~= true then
  314.         print("Failed to start modem!")
  315.         return
  316.     end
  317.  
  318.     local distance = prepare()
  319.  
  320.     if distance ~= nil then
  321.         local currentX, currentY, currentZ = gps.locate()
  322.         local startX, startZ = currentX, currentZ
  323.        
  324.         if turtle.detect() == true then
  325.             turtle.dig()
  326.         end
  327.        
  328.         turtle.forward()
  329.         local nextX, nextY, nextZ = gps.locate()
  330.         turtle.back()
  331.        
  332.         if turtle.detectUp() == true then
  333.             turtle.digUp()
  334.         end
  335.  
  336.         local facing = getFacingDirection(nextX, nextZ, currentX, currentZ)
  337.         local targetX, targetZ = incrementByFacing(facing, currentX, currentZ, distance)
  338.  
  339.         print("Heading to target: " .. targetX .. ", " .. targetZ)
  340.  
  341.         while reachedTarget(currentX, currentZ, targetX, targetZ) ~= true do
  342.             facing = loop(targetX, targetZ, facing)
  343.             currentX, currentY, currentZ = gps.locate()
  344.  
  345.             if facing == "" then
  346.                 break
  347.             end
  348.         end
  349.  
  350.         rednet.send(SERVER_ID, "returning", "minenettoserver")
  351.  
  352.         while reachedTarget(currentX, currentZ, startX, startZ) ~= true do
  353.             facing = returnTo(startX, startZ, facing)
  354.             currentX, currentY, currentZ = gps.locate()
  355.  
  356.             if facing == "" then
  357.                 break
  358.             end
  359.         end
  360.  
  361.         rednet.send(SERVER_ID, "returned", "minenettoserver")
  362.        
  363.         while reachedTarget(currentX, currentZ, startX, startZ) ~= true do
  364.             facing = returnTo(startX, startZ, facing)
  365.             currentX, currentY, currentZ = gps.locate()
  366.        
  367.             if facing == "" then
  368.                 break
  369.             end
  370.         end
  371.        
  372.         dumpDown()
  373.         left(facing)
  374.         left(facing)
  375.         rednet.send(SERVER_ID, "dumped", "minenettoserver")
  376.     end
  377.  
  378.     rednet.close("left")
  379. end
  380.  
  381. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement