Advertisement
Guest User

farmibot.lua

a guest
Aug 17th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.63 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.             robot.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 nowx, nowy, nowz = nav.getPosition()
  106.     if nowx < gox then
  107.         repeat
  108.             faceEast()
  109.             robot.forward()
  110.       local nowx, nowy, nowz = nav.getPosition()
  111.         until nowx == gox
  112.     elseif nowx > gox then
  113.         repeat
  114.             faceWest()
  115.             robot.forward()
  116.       local nowx, nowy, nowz = nav.getPosition()
  117.         until nowx == gox
  118.     end
  119.     if nowz < goz then
  120.         repeat
  121.             faceSouth()
  122.             robot.forward()
  123.       local nowx, nowy, nowz = nav.getPosition()
  124.         until nowz == goz
  125.     elseif nowz > goz then
  126.         repeat
  127.             faceNorth()
  128.             robot.forward()
  129.       local nowx, nowy, nowz = nav.getPosition()
  130.         until nowz == goz
  131.     end
  132.     return true
  133. end
  134. local function charge()
  135.     goThere(startx, startz)
  136.     faceNorth()
  137.     local shestside = checkChestSide()
  138.     local chestsize = getInventorySize(chestside)
  139.     local slot = 0
  140.     while not slot == chestsize do
  141.         slot = slot + 1
  142.         select(slot)
  143.         dropIntoSlot(chestside, slot)
  144.     end
  145.     while not computer.energy == computer.maxEnergy do
  146.         os.sleep(100)
  147.     end
  148. end
  149. local function chargeTest()
  150.     local chargenow = tonumber(computer.energy())
  151.     local invslot = 0
  152.     local freeslots = robot.inventorySize()
  153.   local slot = 1
  154.     if chargenow / tonumber(computer.maxEnergy()) <= 0.1 then
  155.         charge()
  156.         return true
  157.     else
  158.         repeat
  159.             slot = slot + 1
  160.             robot.select(slot)
  161.             if robot.count(slot) ~= 0 then
  162.                 freeslots = freeslots -1
  163.             end
  164.  
  165.         until slot == robot.inventorySize()
  166.         if freeslots <= 2 then
  167.             charge()
  168.             return true
  169.         end
  170.     end
  171. end
  172. local function checkChestSide()
  173.     local chestSide
  174.     faceNorth()
  175.     if not getInventorySize(0) == "no inventory" then
  176.         chestSide = 0
  177.     elseif not getInventorySize(1) == "no inventory" then
  178.         chestSide = 1
  179.     elseif not getInventorySize(2) == "no inventory" then
  180.         chestSide = 2
  181.     elseif not getInventorySize(3) == "no inventory" then
  182.         chestSide = 3
  183.     elseif not getInventorySize(4) == "no inventory" then
  184.         chestSide = 4
  185.     elseif not getInventorySize(5) == "no inventory" then
  186.         chestSide = 5
  187.     end
  188.     return chestSide
  189. end
  190. local function farm(fieldsize)
  191.     faceNorth()
  192.     local endx = startx + fieldsize -1
  193.     local endz = startz - fieldsize -1
  194.     local currentx = startx
  195.     local currentz = startz
  196.     local currenty = 0
  197.  
  198.     repeat
  199.         if not currentx == startx then
  200.             faceNorth()
  201.             robot.forward()
  202.     currentx, currenty, currentz = nav.getPosition()
  203.         end
  204.         while not currentx == startx do
  205.             faceEast()
  206.             robot.forward()
  207.             currentx, currenty, currentz = nav.getPosition()
  208.         end
  209.         repeat
  210.             harvest()
  211.             plant()
  212.             faceEast()
  213.             robot.forward()
  214.             currentx, currenty, currentz = nav.getPosition()
  215.         until currentx == endx
  216.         chargeTest()
  217.     until currentz == endz
  218.     charge()
  219. end
  220.  
  221. print("Enter Field Size: ")
  222. local fieldsizein = tonumber(io.read())
  223. while true do
  224.     farm(fieldsizein)
  225. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement