Robot farming program
Code: Select all
local args = { ... }
local crops =
{
["farming:barley_7"] = { sow = { "farming:seed_barley" } },
["farming:beanpole_5"] = { sow = { "farming:beanpole", "farming:beans" } },
["farming:beetroot_5"] = { sow = { "farming:beetroot" } },
["farming:blueberry_4"] = { sow = { "farming:blueberries" } },
["farming:carrot_8"] = { sow = { "farming:carrot" } },
["farming:chili_8"] = { sow = { "farming:chili_pepper" } },
["farming:cocoa_4"] = { sow = { "farming:cocoa_beans" } },
["farming:coffee_5"] = { sow = { "farming:coffee_beans" } },
["farming:corn_8"] = { sow = { "farming:corn" } },
["farming:cotton_8"] = { sow = { "farming:seed_cotton" } },
["farming:cucumber_4"] = { sow = { "farming:cucumber" } },
["farming:garlic_5"] = { sow = { "farming:garlic_clove" } },
["farming:grapes_8"] = { sow = { "farming:trellis", "farming:grapes" } },
["farming:hemp_8"] = { sow = { "farming:seed_hemp" } },
["farming:melon_8"] = { sow = { "farming:melon_slice" } },
["farming:onion_5"] = { sow = { "farming:onion" } },
["farming:pea_5"] = { sow = { "farming:pea_pod" } },
["farming:pepper_5"] = { sow = { "farming:peppercorn" } },
["farming:pepper_6"] = { sow = { "farming:peppercorn" } },
["farming:pepper_7"] = { sow = { "farming:peppercorn" } },
["farming:pineapple_8"] = { sow = { "farming:pineapple_top" }, craft = { "farming:pineapple_ring" } },
["farming:potato_4"] = { sow = { "farming:potato" } },
["farming:pumpkin_8"] = { sow = { "farming:pumpkin_slice" } },
["farming:raspberry_4"] = { sow = { "farming:raspberries" } },
["farming:rice_8"] = { sow = { "farming:seed_rice" } },
["farming:rhubarb_3"] = { sow = { "farming:rhubarb" } },
["farming:rye_8"] = { sow = { "farming:seed_rye" } },
["farming:tomato_8"] = { sow = { "farming:tomato" } },
["farming:wheat_8"] = { sow = { "farming:seed_wheat" } },
["farming:oat_8"] = { sow = { "farming:seed_oat" } },
["farming:mint_4"] = { sow = { "farming:seed_mint" } },
["farming:cabbage_6"] = { sow = { "farming:cabbage" } },
["farming:artichoke_5"] = { sow = { "farming:artichoke" } },
["farming:blackberry_4"] = { sow = { "farming:blackberry" } },
["farming:lettuce_5"] = { sow = { "farming:lettuce" } },
["farming:parsley_3"] = { sow = { "farming:parsley" } },
["farming:soy_7"] = { sow = { "farming:soy_pod" } },
["farming:vanilla_8"] = { sow = { "farming:vanilla" } },
}
local farm = { }
-- constructor
function farm:new (length, width, delay)
local obj = { }
setmetatable(obj, self)
self.__index = self
obj.help = length and length == "-h"
obj.length = tonumber (length or 0) or 0
obj.width = tonumber (width or 0) or 0
obj.delay = tonumber (delay or 0) or 0
obj.harvest_now = true
return obj
end
function farm:harvest ()
local node = robot.detect_down ()
if node then
local crop = crops[node]
if crop then
if robot.dig_down () then
for i = 1, #crop.sow do
local sow = crop.sow[i]
local craft = (crop.craft and crop.craft[i]) or crop.sow[i]
if not robot.contains (sow) then
robot.craft (craft)
end
if not robot.contains (sow) then
print ("\r \rout of %s\n", sow)
return
end
robot.place_down (sow, "down")
end
end
end
end
end
function farm:check_quit ()
local event = { os.peek_event ("key") }
if event and event[2] == keys.KEY_ESCAPE then
os.get_event ("key")
print ("\r \rStopped\n")
return true
end
return false
end
function farm:do_harvest ()
if os.peek_event ("timer") then
os.get_event ("timer")
self.harvest_now = true
end
if self.harvest_now then
self.harvest_now = false
print ("\r \rRunning")
return true
else
os.sleep (2.0)
end
return false
end
function farm:set_timer ()
if self.delay > 0 then
os.start_timer (self.delay)
print ("\r \rWaiting")
return true
end
print ("\r \rFinished\n")
return false
end
function farm:move_front ()
if not robot.move_front () then
print ("\r \rCan't move\n")
return false
end
return true
end
function farm:move_back ()
if not robot.move_back () then
print ("\r \rCan't move\n")
return false
end
return true
end
function farm:discharge ()
local inv = robot.find_inventory ("main")
if inv and inv.front then
local slots = robot.slots ()
for s = 1, slots do
robot.put_front (s)
end
robot.pull_front ("farming:cutting_board")
end
end
function farm:run ()
if self.help or self.length < 1 or self.width < 1 then
print (" farm [-h] length width [delay]\n"..
"Harvest and plant a rectangle patch.\n"..
" -h show help\n"..
" length nodes forward from robot\n"..
" width nodes width, to right of robot\n"..
" delay if given, number of seconds before\n"..
" re-harvest. if not harvests once\n"..
"place robot one back from start position\n"..
"escape to quit\n")
return (self.help and 0) or 1
end
while true do
if self:check_quit () then
return 0
end
if self:do_harvest () then
local length = self.length - 1
local width = self.width
if not self:move_front () then
return 1
end
self:harvest ()
for w = 1, width do
for l = 1, length do
if not self:move_front () then
return 1
end
self:harvest ()
if self:check_quit () then
return 0
end
end
farm:discharge ()
if self:check_quit () then
return 0
end
if w < width then
if (w % 2) == 1 then
robot.turn_right ()
if not self:move_front () then
return 1
end
robot.turn_right ()
self:harvest ()
else
robot.turn_left ()
if not self:move_front () then
return 1
end
robot.turn_left ()
self:harvest ()
end
end
end
if (width % 2) == 1 then
robot.turn_right ()
robot.turn_right ()
for l = 1, length do
if not self:move_front () then
return 1
end
end
end
robot.turn_right ()
for w = 1, width - 1 do
self:move_front ()
end
robot.turn_right ()
if not self:move_back () then
return 1
end
if not self:set_timer () then
return 0
end
end
end
return 0
end
return farm:new (unpack (args)):run ()
If a chest is at the end of a run in a front direction, the robot will empty its storage into it.
If harvesting pumpkin or melon a cutting board needs to be in the robot's storage.
Create the program file:
- boot robot on LOS disk.
- copy the example program and paste it into a clipboard item.
- place the clipboard item into a slot in the robot.
- on the terminal, from root, enter edit farm.
- press ctrl+alt+V.
- press ctrl+S.
- press ctrl+Q.
To run the program:
farm [-h] length width [delay]
-h - Show help.
length - The node length of the farm patch.
width - The node width of the farm patch.
delay - Seconds to wait until running again, if omitted or zero runs once.