Advertisement
Dimasw

OC sugarcane farm 1.7.10

May 19th, 2025 (edited)
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.02 KB | None | 0 0
  1. local robot = require("robot")
  2. local component = require("component")
  3. local nav = component.navigation
  4.  
  5. local cx, cy, cz
  6. local BASE
  7. local DROPOFF
  8.  
  9. function GetWayPoints()
  10.   BASE, DROPOFF = {}, {}
  11.   cx, cy, cz = 0, 0, 0
  12.   local waypoints = nav.findWaypoints(64)
  13.   for i=1, 2 do
  14.     if waypoints[i].label == "BASE" then
  15.       BASE.x = waypoints[i].position[1]
  16.       BASE.y = waypoints[i].position[2]
  17.       BASE.z = waypoints[i].position[3]
  18.     elseif waypoints[i].label == "DROPOFF" then
  19.       DROPOFF.x = waypoints[i].position[1]
  20.       DROPOFF.y = waypoints[i].position[2]
  21.       DROPOFF.z = waypoints[i].position[3]
  22.     end
  23.   end
  24. end
  25.  
  26. function rotate(direction)
  27.   local target = 2
  28.   if direction == "SOUTH" then
  29.     target = 3
  30.   elseif direction == "WEST" then
  31.     target = 4
  32.   elseif direction == "EAST" then
  33.     target = 5
  34.   else
  35.     target = 2
  36.   end
  37.   local cur_dir = nav.getFacing()
  38.   while not (cur_dir == target) do
  39.     robot.turnRight()
  40.     cur_dir = nav.getFacing()
  41.   end
  42. end
  43.  
  44. function MoveToWaypoint(target)
  45.   local x,y,z = target.x, target.y, target.z
  46.   if x >= 1 then
  47.     rotate("EAST")
  48.     for i=1, x do
  49.       if(not robot.forward()) then
  50.         robot.swing()
  51.         robot.forward()
  52.       end
  53.     end
  54.   elseif x <= 0 then
  55.     rotate("WEST")
  56.     for i=1, x*(-1) do
  57.       if(not robot.forward()) then
  58.         robot.swing()
  59.         robot.forward()
  60.       end
  61.     end
  62.   end
  63.   if z > 1 then
  64.     rotate("SOUTH")
  65.     for i=1, z do
  66.       if(not robot.forward()) then
  67.         robot.swing()
  68.         robot.forward()
  69.       end
  70.     end
  71.   elseif z < 0 then
  72.     rotate("NORTH")
  73.     for i=1, z*(-1) do
  74.       if(not robot.forward()) then
  75.         robot.swing()
  76.         robot.forward()
  77.       end
  78.     end
  79.   end
  80. end
  81.  
  82. function MoveWhileFarming()
  83.   while true do
  84.     if(not robot.forward()) then
  85.       local isBlock, type = robot.detect()
  86.       if type == "solid" then
  87.         return
  88.       else
  89.         robot.swing()
  90.       end
  91.     end
  92.   end
  93. end
  94.  
  95. function moveSingle()
  96.   print("moving")
  97.   if(not robot.forward()) then
  98.     local isBlock, type = robot.detect()
  99.     if type == "passable" then
  100.       robot.swing()
  101.       robot.forward()
  102.     elseif type == "solid" then
  103.       return "blocked"
  104.     end
  105.   end
  106. end
  107.  
  108. function farm()
  109.   while true do
  110.     print("farming")
  111.     MoveWhileFarming()
  112.     robot.turnLeft()
  113.     if moveSingle() == "blocked" then break end
  114.     if moveSingle() == "blocked" then break end
  115.     robot.turnLeft()
  116.     MoveWhileFarming()
  117.     robot.turnRight()
  118.     if moveSingle() == "blocked" then break end
  119.     robot.turnRight()
  120.   end
  121. end
  122.  
  123. function dropOff()
  124.   for i=1, robot.inventorySize() do
  125.     if robot.count(i) > 0 then
  126.        robot.select(i)
  127.        robot.dropDown()
  128.     end
  129.   end
  130. end
  131.  
  132.  
  133. function init()
  134.   while true do
  135.     GetWayPoints()
  136.     MoveToWaypoint(BASE)
  137.     rotate("WEST")
  138.     farm()
  139.     GetWayPoints()
  140.     MoveToWaypoint(DROPOFF)
  141.     dropOff()
  142.     GetWayPoints()
  143.     MoveToWaypoint(BASE)
  144.     os.sleep(60)
  145.   end
  146. end
  147.  
  148. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement