Advertisement
Guest User

farmbot.lua

a guest
Aug 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.41 KB | None | 0 0
  1. local component = require("component")
  2. local robot = require("robot")
  3. local computer = require("computer")
  4. local inv = component.inventory_controller
  5. local nav = component.navigation
  6. local geo = component.geolyzer
  7. local vector = require("vector")
  8. local side = require("sides")
  9. local slot = 1
  10. local startx, starty, startz = nav.getPosition()
  11.  
  12. local function harvest()
  13.     if geo.analyze(0).growth == 1 then
  14.         robot.swingDown()
  15.         robot.suckDown()
  16.         return true
  17.     else
  18.         return false
  19.     end
  20. end
  21. local function plant()
  22. print("trying to plant")
  23. local slot = 1
  24. if inv.getStackInInternalSlot() then
  25.     while not inv.getStackInInternalSlot().label == "Seeds" do
  26.             if not slot == 16 then
  27.       slot = slot + 1
  28.    else
  29.        slot = 1
  30.    end
  31.             robot.select(slot)
  32.     end
  33. end
  34. if inv.getStackInInternalSlot(slot) then
  35.     if inv.getStackInInternalSlot(slot).label == "Seeds" then
  36.         inv.equip()
  37.         robot.placeDown()
  38. return true
  39.    
  40.     else
  41.         return false
  42.     end
  43. end
  44.     return true
  45. end
  46. local function faceNorth()
  47.     local face = nav.getFacing()
  48.     if not face == 2 then
  49.         elseif face == 3 then
  50.             robot.turnRight()
  51.       robot.turnRight()
  52.         elseif face == 4 then
  53.             robot.turnRight()
  54.         elseif face == 5 then
  55.             robotapi.turnLeft()
  56.         else
  57.         return false
  58.         end
  59.     return true
  60. end
  61. local function faceSouth()
  62.     local face = nav.getFacing()
  63.     if not face == 3 then
  64.         elseif face == 2 then
  65.             robot.turnAround()
  66.         elseif face == 5 then
  67.             robot.turnRight()
  68.         elseif face == 4 then
  69.             robot.turnLeft()
  70.         else
  71.         return false
  72.         end
  73.     return true
  74. end
  75. local function faceEast()
  76.     local face = nav.getFacing()
  77.     if not face == 5 then
  78.         print("Faces East")
  79.         elseif face == 4 then
  80.             robot.turnAround()
  81.         elseif face == 2 then
  82.             robot.turnRight()
  83.         elseif face == 3 then
  84.             robot.turnLeft()
  85.         else
  86.         return false
  87.     end
  88.     return true
  89. end
  90. local function faceWest()
  91.     local face = nav.getFacing()
  92.     if not face == 4 then
  93.         elseif face == 5 then
  94.             robot.turnAround()
  95.         elseif face == 3 then
  96.             robot.turnRight()
  97.         elseif face == 2 then
  98.             robot.turnLeft()
  99.         else
  100.             return false
  101.         end
  102.     return true
  103. end
  104. local function goThere(gox, goz)
  105.     local beginx, beginy, beginz = nav.getPosition()
  106.     if beginx < gox then
  107.         repeat
  108.             faceEast()
  109.             robot.forward()
  110.         until beginx == gox
  111.     elseif beginx > gox then
  112.         repeat
  113.             faceWest()
  114.             robot.forward()
  115.         until beginx == gox
  116.     end
  117.     if beginz < goz then
  118.         repeat
  119.             faceSouth()
  120.             robot.forward()
  121.         until beginz == goz
  122.     elseif beginz > goz then
  123.         repeat
  124.             faceNorth()
  125.             robot.forward()
  126.         until beginz == goz
  127.     end
  128.     return true
  129. end
  130. local function chargeTest()
  131.     local charge = tonumber(computer.energy())
  132.     local invslot = 0
  133.     local freeslots = robot.inventorySize()
  134.   local slot = 1
  135.     if charge / tonumber(computer.maxEnergy()) <= 0.1 then
  136.         charge()
  137.         return true
  138.     else
  139.         repeat
  140.             slot = slot + 1
  141.             robot.select(slot)
  142.             if robot.count(slot) ~= 0 then
  143.                 freeslots = freeslots -1
  144.             end
  145.  
  146.         until slot == robot.inventorySize()
  147.         if freeslots <= 2 then
  148.             charge()
  149.             return true
  150.         end
  151.     end
  152. end
  153. local function checkChestSide()
  154.     local chestSide
  155.     faceNorth()
  156.     if not getInventorySize(0) == "no inventory" then
  157.         chestSide = 0
  158.     elseif not getInventorySize(1) == "no inventory" then
  159.         chestSide = 1
  160.     elseif not getInventorySize(2) == "no inventory" then
  161.         chestSide = 2
  162.     elseif not getInventorySize(3) == "no inventory" then
  163.         chestSide = 3
  164.     elseif not getInventorySize(4) == "no inventory" then
  165.         chestSide = 4
  166.     elseif not getInventorySize(5) == "no inventory" then
  167.         chestSide = 5
  168.     end
  169.     return chestSide
  170. end
  171. local function charge()
  172.     gothere(startx, startz)
  173.     faceNorth()
  174.     local shestside = checkChestSide()
  175.     local chestsize = getInventorySize(chestside)
  176.     local slot = 0
  177.     while not slot == chestsize do
  178.         slot = slot + 1
  179.         select(slot)
  180.         dropIntoSlot(chestside, slot)
  181.     end
  182.     while not computer.energy == computer.maxEnergy do
  183.         os.sleep(100)
  184.     end
  185. end
  186. local function farm(fieldsize)
  187.     faceNorth()
  188.     local endx = startx + fieldsize -1
  189.     local endz = startz - fieldsize -1
  190.     local currentx = startx
  191.     local currentz = startz
  192.     local currenty = 0
  193.  
  194.     repeat
  195.         if not currentx == startx then
  196.             faceNorth()
  197.             robot.forward()
  198.         end
  199.         while not currentx == startx do
  200.             faceEast()
  201.             robot.forward()
  202.             currentx, currenty, currentz = nav.getPosition()
  203.         end
  204.         repeat
  205.             harvest()
  206.             plant()
  207.             faceEast()
  208.             robot.forward()
  209.             currentx, currenty, currentz = nav.getPosition()
  210.         until currentx == endx
  211.         chargeTest()
  212.     until currentz == endz
  213.     charge()
  214. end
  215.  
  216. print("Enter Field Size: ")
  217. local fieldsizein = tonumber(io.read())
  218. while true do
  219.     farm(fieldsizein)
  220. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement