Jayex_Designs

drone

Jun 21st, 2021 (edited)
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.70 KB | None | 0 0
  1. rednet.open("right")
  2.  
  3. DeliverHeight = 85
  4. ReturnHeight = 86
  5. Direction = nil
  6. StartDirection = nil
  7. SLOT_COUNT = 16
  8.  
  9.  
  10.  
  11. function CheckFuel()
  12.     if(turtle.getFuelLevel() < 50) then
  13.         for slot = 1, SLOT_COUNT, 1 do
  14.             turtle.select(slot)
  15.             if(turtle.refuel()) then
  16.                 NoFuel = false
  17.                 return true
  18.             end
  19.         end
  20.         return false
  21.     else
  22.         NoFuel = false
  23.         return true
  24.     end
  25. end
  26.  
  27. function StringSplit(input, sep)
  28.     if sep == nil then
  29.         sep = "%s"
  30.     end
  31.     local t={}
  32.     for str in string.gmatch(input, "([^"..sep.."]+)") do
  33.         table.insert(t, str)
  34.     end
  35.     return t
  36. end
  37.  
  38. function GetItemIndex(itemName)
  39.     for slot = 1, SLOT_COUNT, 1 do
  40.         local item = turtle.getItemDetail(slot)
  41.         if(item ~= nil) then
  42.             if(item["name"] == itemName) then
  43.                 return slot
  44.             end
  45.         end
  46.     end
  47.     return nil
  48. end
  49.  
  50. function LookAt(direction)
  51.     local directions = {
  52.         [1] = "north",
  53.         [2] = "east",
  54.         [3] = "south",
  55.         [4] = "west",
  56.     }
  57.     local currentDirection
  58.     for k, v in pairs(directions) do
  59.         if Direction == v then
  60.             currentDirection = k
  61.         end
  62.     end
  63.     while directions[currentDirection] ~= direction do
  64.         currentDirection = currentDirection + 1
  65.         turtle.turnRight()
  66.         if currentDirection > 4 then
  67.             currentDirection = 1
  68.         end
  69.     end
  70.     Direction = directions[currentDirection]
  71. end
  72.  
  73.  
  74.  
  75. local id, message, protocol = rednet.receive()
  76. if protocol == "droneStart" then
  77.     local function repeatAction(action, times)
  78.         for i = 1, times, 1 do
  79.             action()
  80.         end
  81.     end
  82.  
  83.     if not CheckFuel() then
  84.         rednet.send(id, "", "droneEnd")
  85.     else
  86.         local data = StringSplit(message)
  87.         local x = tonumber(data[1])
  88.         local y = tonumber(data[2])
  89.         local z = tonumber(data[3])
  90.         local quantity = tonumber(data[4])
  91.  
  92.         repeatAction(turtle.up, 6)
  93.         repeatAction(turtle.turnRight, 2)
  94.  
  95.         local startX, _, startZ = gps.locate()
  96.         turtle.forward()
  97.         local endX, _, endZ = gps.locate()
  98.         if startX > endX then
  99.             Direction = "south"
  100.             StartDirection = "south"
  101.         elseif startX < endX then
  102.             Direction = "north"
  103.             StartDirection = "north"
  104.         elseif startZ > endZ then
  105.             Direction = "west"
  106.             StartDirection = "west"
  107.         elseif startZ < endZ then
  108.             Direction = "east"
  109.             StartDirection = "east"
  110.         end
  111.  
  112.         turtle.turnLeft()
  113.         repeatAction(turtle.forward, 5)
  114.         turtle.turnLeft()
  115.         for i = 1, math.floor(quantity / 64) do
  116.             turtle.suck(64)
  117.         end
  118.         turtle.suck(quantity % 64)
  119.         turtle.turnLeft()
  120.         repeatAction(turtle.forward, 2)
  121.         turtle.turnRight()
  122.         turtle.forward()
  123.         turtle.turnLeft()
  124.         repeatAction(turtle.forward, 3)
  125.         repeatAction(turtle.up, 4)
  126.         repeatAction(turtle.forward, 2)
  127.  
  128.         local currentX, currentY, currentZ = gps.locate()
  129.         repeatAction(turtle.up, DeliverHeight - currentY)
  130.         if currentX > x then
  131.             LookAt("east")
  132.         elseif currentX < x then
  133.             LookAt("west")
  134.         end
  135.         repeatAction(turtle.forward, math.abs(currentX - x))
  136.         if currentZ > z then
  137.             LookAt("south")
  138.         elseif currentZ < z then
  139.             LookAt("north")
  140.         end
  141.         repeatAction(turtle.forward, math.abs(currentZ - z))
  142.  
  143.         local steps = 0
  144.         while not turtle.detectDown() do
  145.             turtle.down()
  146.             steps = steps + 1
  147.         end
  148.         while true do
  149.             local slot = GetItemIndex("minecraft:baked_potato")
  150.             if slot then
  151.                 turtle.select(slot)
  152.                 turtle.drop()
  153.             else
  154.                 break
  155.             end
  156.         end
  157.  
  158.         repeatAction(turtle.up, steps + 1)
  159.         if currentX > x then
  160.             LookAt("west")
  161.         elseif currentX < x then
  162.             LookAt("east")
  163.         end
  164.         repeatAction(turtle.forward, math.abs(currentX - x))
  165.         if currentZ > z then
  166.             LookAt("north")
  167.         elseif currentZ < z then
  168.             LookAt("south")
  169.         end
  170.         repeatAction(turtle.forward, math.abs(currentZ - z))
  171.  
  172.         repeatAction(turtle.down, ReturnHeight - currentY)
  173.  
  174.         LookAt(StartDirection)
  175.         repeatAction(turtle.turnLeft, 2)
  176.  
  177.         repeatAction(turtle.forward, 2)
  178.         repeatAction(turtle.down, 10)
  179.  
  180.         rednet.send(id, "", "droneEnd")
  181.     end
  182. end
Advertisement
Add Comment
Please, Sign In to add comment