Advertisement
Guest User

Untitled

a guest
Mar 18th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 KB | None | 0 0
  1. local component = require("component")
  2. local sides = require("sides")
  3. local robot = require("robot")
  4. local os = require("os")
  5. local event = require("event")
  6. -- local modem = component.modem    -- Disabled, no network functions yet in 2.0
  7. local inv = component.inventory_controller
  8.  
  9. -- Settings --
  10. local farmHeight = 18
  11. local farmWidth = 18
  12. -- -------- --
  13.  
  14. local function isOdd(number)
  15. -- returns true if number is odd
  16.     if number % 2 ~= 0 then
  17.         return true
  18.     elseif number %2 == 0 then
  19.         return false
  20.     end
  21. end
  22.  
  23. local function findEmptySlot()
  24. -- Returns first found empty slot from inventory beneath robot
  25.     for slot = 1, inv.getInventorySize(sides.down) do
  26.         if inv.getStackInSlot(sides.down, slot) == nil then
  27.             return slot
  28.         end
  29.     end
  30. end
  31.  
  32. local function cutPlant()
  33.     robot.select(1)     --  Select inventory slot 1, containing seeds
  34.     robot.swingDown()   --  Harvest plant beneath robot
  35.     inv.equip()         --  Equip seeds
  36.     robot.useDown()     --  Plant seeds
  37.     inv.equip()         --  Equip nothing, placing seeds back into slot 1
  38. end
  39.  
  40. local function checkChest()
  41. -- Returns true if chest has any empty slots
  42. -- Currently unused function
  43.     local isFull = findEmptySlot()
  44.     if isFull == nil then
  45.         return false
  46.     else
  47.         return true
  48.     end
  49. end
  50.  
  51. local function doColumn()
  52. --  Rules   --
  53. --  The bottom-left tilled soil block is 1,1
  54. --  The charger is at 1,0
  55. --  This function shall be started at x,1 and run to x,farmHeight
  56. --  Another function after shall handle moving to next column
  57.     local yval = 1
  58.     while yval < farmHeight do
  59.         cutPlant()
  60.         robot.forward() -- Forward at the end of the function means
  61.         yval = yval + 1 -- we must use lesser-than farmHeight
  62.     end
  63.     cutPlant()
  64. end
  65.  
  66. local function nextColumn(xNumber)
  67. --  Given the column number, determine how to move to the next column
  68. --  On odd numbered columns, robot is facing y-pos and must move right
  69. --  On even numbered columns, robot is facing y-neg and must move left
  70.     if isOdd(xNumber) == true then
  71.         robot.turnRight()
  72.         robot.forward()
  73.         robot.turnRight()
  74.     elseif isOdd(xNumber) == false then
  75.         robot.turnLeft()
  76.         robot.forward()
  77.         robot.turnLeft()
  78.     end
  79.     xNumber = xNumber + 1
  80.     return xNumber
  81. end
  82.  
  83. local function emptyInv()
  84. -- Empty inventory into chest below robot
  85.     local size = robot.inventorySize()
  86.     local chestSize = inv.getInventorySize(sides.down)
  87.     for i = 2, size do
  88.         robot.select(i)
  89.         local invCount = robot.count()
  90.         local slot = findEmptySlot()
  91.         if invCount ~= 0 and slot ~= nil then
  92.             inv.dropIntoSlot(sides.down, slot)
  93.         elseif slot == nil then
  94.             print("Error! Deposit chest is full! Shutting down...")
  95.             os.exit()   -- Abort program is chest is full
  96.         end
  97.     end
  98. end
  99.  
  100. local function backToCharger()
  101. --  Check whether farmWidth is odd or even and decide which way
  102. --  the robot must move based upon that, same as nextColumn()
  103.     if isOdd(farmWidth) == true then
  104.         robot.turnLeft()
  105.         for i = farmWidth-1, 1, -1 do
  106.             robot.forward()
  107.         end
  108.         robot.turnLeft()
  109.         for i = farmHeight-1, 0, -1 do -- Going to 1,0 for charger
  110.             robot.forward()
  111.         end
  112.         robot.turnAround()  -- Turn around so we face the right direction
  113.     elseif isOdd(farmWidth) == false then
  114.         robot.turnRight()
  115.         for i = farmWidth-1, 1, -1 do   -- Starting from farmWidth, 1
  116.             robot.forward()             -- going to 1,1
  117.         end
  118.         robot.turnLeft()                -- Facing 1,0
  119.         robot.forward()                 -- Moving to 1,0
  120.         robot.turnAround()              -- Facing the right direction
  121.     end
  122. end
  123.  
  124. while true do
  125.     robot.forward() -- Move off of charger at 1,0 to first farm block 1,1
  126.     local xNumber = 1       -- Set column to 1
  127.     while xNumber < farmWidth do
  128.         doColumn()
  129.         xNumber = nextColumn(xNumber)   -- Used less-than so we can do this at the end
  130.     end
  131.     if xNumber == farmWidth then
  132.         doColumn()
  133.         backToCharger()
  134.     end
  135.     robot.up()
  136.     robot.back()
  137.     emptyInv()
  138.     robot.forward()
  139.     robot.down()
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement