Guest User

farm2.lua

a guest
Jul 17th, 2017
4,182
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.83 KB | None | 1 0
  1. -- Farming drone originally by DrManganese ( https://github.com/DrManganese/OpenComputers-Projects/blob/master/Farming%20Drone/farm.lua )
  2. -- Modified by nzHook http://youtube.com/nzhook
  3.  
  4. --local docking = component.proxy(component.list("dock")())
  5. local drone = component.proxy(component.list("drone")())
  6. local nav = component.proxy(component.list("navigation")())
  7.  
  8. local baseSleep = 5
  9. local waypointLookRadius = 64
  10. local colours = {["travelling"] = 0xFFFFFF, ["farming"] = 0x332400, ["waiting"] = 0x0092FF, ["dropping"] = 0x33B640}
  11.  
  12. local cx, cy, cz
  13. local BASE
  14. local DROPOFF
  15. local FARMROWs
  16.  
  17. function getWaypoints()
  18.   BASE, DROPOFF, FARMROWs = {}, {}, {}
  19.   cx, cy, cz = 0, 0, 0
  20.   local waypoints = nav.findWaypoints(waypointLookRadius)
  21.   for i=1, waypoints.n do
  22.     if waypoints[i].label == "BASE" then
  23.       BASE.x = waypoints[i].position[1]
  24.       BASE.y = waypoints[i].position[2]
  25.       BASE.z = waypoints[i].position[3]
  26.     elseif waypoints[i].label == "DROPOFF" then
  27.       DROPOFF.x = waypoints[i].position[1]
  28.       DROPOFF.y = waypoints[i].position[2]
  29.       DROPOFF.z = waypoints[i].position[3]
  30.     elseif waypoints[i].label:find("FARMROW") == 1 then
  31.       local tempTable = {}
  32.       tempTable.x = waypoints[i].position[1]
  33.       tempTable.y = waypoints[i].position[2]
  34.       tempTable.z = waypoints[i].position[3]
  35.       tempTable.l = waypoints[i].label:match("LEN(%d+)")
  36.       table.insert(FARMROWs, tempTable)
  37.     end
  38.   end
  39. end
  40.  
  41. function colour(state)
  42.   drone.setLightColor(colours[state] or 0x000000)
  43. end
  44.  
  45. function move(tx, ty, tz)
  46.   local dx = tx - cx
  47.   local dy = ty - cy
  48.   local dz = tz - cz
  49.   drone.move(dx, dy, dz)
  50.   while drone.getOffset() > 0.7 or drone.getVelocity() > 0.7 do
  51.     computer.pullSignal(0.2)
  52.   end
  53.   cx, cy, cz = tx, ty, tz
  54. end
  55.  
  56. function getCharge()
  57.   return computer.energy()/computer.maxEnergy()
  58. end
  59.  
  60. function waitAtBase()
  61.     colour("travelling")
  62.     move(BASE.x, BASE.y+1, BASE.z)
  63. --    while docking.dock()~= true do computer.pullSignal(.1) end
  64.  
  65.     colour("waiting")
  66.     while getCharge() < 0.8 do
  67.         computer.pullSignal(1)
  68.     end
  69.     --poké-heal sound GGGEC
  70.     computer.beep(783.99) computer.pullSignal(.25) computer.beep(783.99) computer.pullSignal(.25) computer.beep(783.99) computer.beep(659.25) computer.beep(1046.5)
  71. --    docking.release()
  72. end
  73.  
  74. function findCrop()
  75.   for i=2, 5 do
  76.     local isBlock, type = drone.detect(i)
  77.     if type == "passable" then
  78.       return i
  79.     end
  80.   end
  81. end
  82.  
  83. function farm()
  84.   for i=1, #FARMROWs do
  85.     local row = FARMROWs[i]
  86.     colour("travelling")
  87.     move(row.x, row.y, row.z)
  88.     colour("farming")
  89.     local direction = findCrop()
  90.     local l, r, tx, tz
  91.     if direction == 2 then l,r,tx,tz = 4,5,0,-1 end
  92.     if direction == 3 then l,r,tx,tz = 5,4,0,1 end
  93.     if direction == 4 then l,r,tx,tz = 3,2,-1,0 end
  94.     if direction == 5 then l,r,tx,tz = 2,3,1,0 end
  95.  
  96.     drone.use(direction)
  97.     for i=1, row.l do
  98.       move(cx+tx, cy, cz+tz)
  99.       colour("farming")
  100.       drone.use(l)
  101.       drone.use(r)
  102.       if i < tonumber(row.l) then
  103.         drone.use(direction)
  104.       end
  105.     end
  106.   end
  107. end
  108.  
  109. function dropoff()
  110.   local havesome = false
  111.   for i=1, drone.inventorySize() do
  112.     if drone.count(i) > 0 then
  113.         havesome = true
  114.     end
  115.   end
  116.   if not havesome then
  117.     return
  118.   end
  119.  
  120.   colour("travelling")
  121.   move(DROPOFF.x, DROPOFF.y+1, DROPOFF.z)
  122. --  while docking.dock()~= true do computer.pullSignal(.1) end
  123.   colour("dropping")
  124.   for i=1, drone.inventorySize() do
  125. --    docking.dropItem(i, 64)
  126.     drone.select(i)
  127.     -- 0 = sides.bottom
  128.     drone.drop(0, 64)
  129.     computer.pullSignal(1)
  130.   end
  131. --  docking.release()
  132. end
  133.  
  134. function init()
  135.   getWaypoints()
  136.   waitAtBase()
  137.   while true do
  138.     farm()
  139.     dropoff()
  140.     if getCharge() < 0.8 then
  141.        getWaypoints()
  142.        waitAtBase()
  143.     end
  144.   end
  145. end
  146.  
  147. init()
Advertisement
Add Comment
Please, Sign In to add comment