Advertisement
Guest User

gfarm

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