Advertisement
knayvik

OC Farming robot

Jun 6th, 2022 (edited)
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.57 KB | None | 0 0
  1. local component = require("component")
  2. local nav = component.navigation
  3. local directions = { north = 2, south = 3, west = 4, east = 5 }
  4. local robot = require("robot")
  5. local sides = require("sides")
  6. local invControl = component.inventory_controller
  7.  
  8. function distanceFrom(waypointName)
  9.     local waypoints = nav.findWaypoints(20)
  10.     for i = 1,#waypoints do
  11.         wp = waypoints[i]
  12.         if wp.label:find(waypointName) then
  13.             return table.unpack(wp.position)
  14.         end
  15.     end
  16. end
  17.  
  18. function goToWaypoint(waypointName)
  19.     x, y, z = distanceFrom(waypointName)
  20.     if (x > 0) then
  21.     local direction = nav.getFacing()
  22.         if (direction == directions.south) then
  23.             robot.turnLeft()
  24.         end
  25.         if (direction == directions.north) then
  26.             robot.turnRight()
  27.         end
  28.         if (direction == directions.west) then
  29.             robot.turnAround()
  30.         end
  31.         while x > 0 do
  32.             if (robot.forward() == true) then
  33.                 x = x - 1
  34.             end
  35.         end
  36.     end
  37.    
  38.     if (z > 0) then
  39.     local direction = nav.getFacing()
  40.         if (direction == directions.west) then
  41.             robot.turnLeft()
  42.         end
  43.         if (direction == directions.east) then
  44.             robot.turnRight()
  45.         end
  46.         if (direction == directions.north) then
  47.             robot.turnAround()
  48.         end
  49.         while z > 0 do
  50.             if (robot.forward() == true) then
  51.                 z = z - 1
  52.             end
  53.         end
  54.     end
  55. end
  56.  
  57. function goHome()
  58.     goToWaypoint("Charger")
  59. end
  60.  
  61. function faceNorth(direction)
  62.     if (direction == directions.east) then
  63.         robot.turnLeft()
  64.     end
  65.     if (direction == directions.west) then
  66.         robot.turnRight()
  67.     end
  68.     if (direction == directions.south) then
  69.         robot.turnAround()
  70.     end
  71. end
  72.  
  73. function harvest()
  74.     local direction = nav.getFacing()
  75.     local notEnd = true
  76.    
  77.     while notEnd do
  78.        
  79.         local direction = nav.getFacing()
  80.         faceNorth(direction)
  81.         if (direction ~= directions.south) then
  82.             robot.forward()
  83.         end
  84.         robot.forward()
  85.         robot.forward()
  86.         if (direction == directions.south) then
  87.             robot.turnLeft()
  88.         else
  89.             faceNorth(direction)
  90.         end
  91.        
  92.         for i = 1, 9 do
  93.             robot.useDown()
  94.             if (robot.forward() ~= true) then
  95.                 i = i - 1
  96.             end
  97.         end
  98.         local x, y, z = distanceFrom("Path End")
  99.         if (x + y + z == 0) then
  100.             notEnd = false
  101.         end
  102.     end
  103. end
  104.  
  105. --[[
  106. function sprinkle()
  107.     goToWaypoint("Center")
  108.     robot.select(1)
  109.     for i = 1, 100 do
  110.         robot.place()
  111.         os.sleep(1/20)
  112.     end
  113. end
  114. ]]--
  115.  
  116. function deposit()
  117.     local slot = 1
  118.     for i = 1, 16 do
  119.         robot.select(i)
  120.         while (invControl.dropIntoSlot(sides.top, slot) == false)
  121.         and (invControl.getStackInInternalSlot() ~= nil) do
  122.             slot = slot + 1
  123.         end
  124.     end
  125. end
  126.  
  127. while true do
  128.     goHome()
  129.     harvest()
  130.     goHome()
  131.     deposit()
  132.     os.sleep(120)
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement