Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local sides = require("sides")
- local robot = require("robot")
- local os = require("os")
- local event = require("event")
- -- local modem = component.modem -- Disabled, no network functions yet in 2.0
- local inv = component.inventory_controller
- -- Settings --
- local farmHeight = 18
- local farmWidth = 18
- -- -------- --
- local function isOdd(number)
- -- returns true if number is odd
- if number % 2 ~= 0 then
- return true
- elseif number %2 == 0 then
- return false
- end
- end
- local function findEmptySlot()
- -- Returns first found empty slot from inventory beneath robot
- for slot = 1, inv.getInventorySize(sides.down) do
- if inv.getStackInSlot(sides.down, slot) == nil then
- return slot
- end
- end
- end
- local function cutPlant()
- robot.select(1) -- Select inventory slot 1, containing seeds
- robot.swingDown() -- Harvest plant beneath robot
- inv.equip() -- Equip seeds
- robot.useDown() -- Plant seeds
- inv.equip() -- Equip nothing, placing seeds back into slot 1
- end
- local function checkChest()
- -- Returns true if chest has any empty slots
- -- Currently unused function
- local isFull = findEmptySlot()
- if isFull == nil then
- return false
- else
- return true
- end
- end
- local function doColumn()
- -- Rules --
- -- The bottom-left tilled soil block is 1,1
- -- The charger is at 1,0
- -- This function shall be started at x,1 and run to x,farmHeight
- -- Another function after shall handle moving to next column
- local yval = 1
- while yval < farmHeight do
- cutPlant()
- robot.forward() -- Forward at the end of the function means
- yval = yval + 1 -- we must use lesser-than farmHeight
- end
- cutPlant()
- end
- local function nextColumn(xNumber)
- -- Given the column number, determine how to move to the next column
- -- On odd numbered columns, robot is facing y-pos and must move right
- -- On even numbered columns, robot is facing y-neg and must move left
- if isOdd(xNumber) == true then
- robot.turnRight()
- robot.forward()
- robot.turnRight()
- elseif isOdd(xNumber) == false then
- robot.turnLeft()
- robot.forward()
- robot.turnLeft()
- end
- xNumber = xNumber + 1
- return xNumber
- end
- local function emptyInv()
- -- Empty inventory into chest below robot
- local size = robot.inventorySize()
- local chestSize = inv.getInventorySize(sides.down)
- for i = 2, size do
- robot.select(i)
- local invCount = robot.count()
- local slot = findEmptySlot()
- if invCount ~= 0 and slot ~= nil then
- inv.dropIntoSlot(sides.down, slot)
- elseif slot == nil then
- print("Error! Deposit chest is full! Shutting down...")
- os.exit() -- Abort program is chest is full
- end
- end
- end
- local function backToCharger()
- -- Check whether farmWidth is odd or even and decide which way
- -- the robot must move based upon that, same as nextColumn()
- if isOdd(farmWidth) == true then
- robot.turnLeft()
- for i = farmWidth-1, 1, -1 do
- robot.forward()
- end
- robot.turnLeft()
- for i = farmHeight-1, 0, -1 do -- Going to 1,0 for charger
- robot.forward()
- end
- robot.turnAround() -- Turn around so we face the right direction
- elseif isOdd(farmWidth) == false then
- robot.turnRight()
- for i = farmWidth-1, 1, -1 do -- Starting from farmWidth, 1
- robot.forward() -- going to 1,1
- end
- robot.turnLeft() -- Facing 1,0
- robot.forward() -- Moving to 1,0
- robot.turnAround() -- Facing the right direction
- end
- end
- while true do
- robot.forward() -- Move off of charger at 1,0 to first farm block 1,1
- local xNumber = 1 -- Set column to 1
- while xNumber < farmWidth do
- doColumn()
- xNumber = nextColumn(xNumber) -- Used less-than so we can do this at the end
- end
- if xNumber == farmWidth then
- doColumn()
- backToCharger()
- end
- robot.up()
- robot.back()
- emptyInv()
- robot.forward()
- robot.down()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement