Improved script for use with vacuum blocks. Increases speed dramatically.

This commit is contained in:
Andrew Lalis 2018-11-04 10:39:10 +01:00
parent 401c1fe65d
commit 6c04533dbb
1 changed files with 36 additions and 75 deletions

View File

@ -1,12 +1,13 @@
--[[ --[[
Author: Andrew Lalis Author: Andrew Lalis
File: lumber_farm.lua File: lumber_farm.lua
Version: 1.0 Version: 2.0
Last Modified: 30-09-2018 Last Modified: 04-11-2018
Description: Description:
This script will automate the farming of large spruce trees, and manages an This script will automate the farming of large spruce trees, and will chop and
array of 2x2 trees which it will replant and collect the saplings of. replant them, but not pick up items, since there are many mod items available
which can do this more efficiently.
The robot should be given an 'unbreakable' tool with 5 reinforced upgrades. The robot should be given an 'unbreakable' tool with 5 reinforced upgrades.
--]] --]]
@ -15,7 +16,6 @@ The robot should be given an 'unbreakable' tool with 5 reinforced upgrades.
local robot = require("robot") local robot = require("robot")
local component = require("component") local component = require("component")
local ms = require("movescript") local ms = require("movescript")
local tractor_beam = component.tractor_beam
local ic = component.inventory_controller local ic = component.inventory_controller
local move_to_start = "5F" local move_to_start = "5F"
@ -24,7 +24,6 @@ local return_from_start = "5B"
local ROWS = 3 local ROWS = 3
local COLS = 2 local COLS = 2
local TREE_SPACING = 3 local TREE_SPACING = 3
local DELAY = 15
-- Global counter. -- Global counter.
local TREES_CHOPPED = 0 local TREES_CHOPPED = 0
@ -93,39 +92,6 @@ local function isTreeGrown()
return (success and str == "solid") return (success and str == "solid")
end end
--[[
Uses the tractor_beam module to repeatedly pick up items until there are no
more to pick up.
--]]
local function pickupItems()
local success = tractor_beam.suck()
while success do
success = tractor_beam.suck()
end
end
--[[
Uses the robot's axe to chop a tree, and quits if the lumber axe provided has
less than 10% durability.
return - integer: 1 if the tree was chopped, 0 otherwise.
--]]
local function chopTree()
if (isTreeGrown()) then
local durability = robot.durability()
while (durability == nil) or (durability < 0.1) do
print("Please ensure that a lumber axe with at least 10% durability is equipped in the tool slot, and press enter.")
io.read()
durability = robot.durability()
end
ms.execute("SF")
os.sleep(1)
pickupItems()
ms.execute("B")
return 1
end
return 0
end
--[[ --[[
Plants a sapling, and if it can't place one at first, loops until it is Plants a sapling, and if it can't place one at first, loops until it is
possible. Assumes the robot is at the starting position: possible. Assumes the robot is at the starting position:
@ -135,12 +101,15 @@ R
Where O=dirt, R=robot. Where O=dirt, R=robot.
--]] --]]
local function plantTree() local function plantTree()
--Pick up any remaining items.
pickupItems()
local success, str = robot.detect() local success, str = robot.detect()
if (success and (str == "passable" or str == "solid" or str == "replaceable")) then if (success and (str == "passable" or str == "solid" or str == "replaceable")) then
return return
end end
local saplings_needed = 4
if (getItemCount(SAPLING_NAME, SAPLING_DATA) < saplings_needed) then
print("Not enough saplings. Needed: "..saplings_needed..". Add some and press ENTER.")
io.read()
end
selectSafely(SAPLING_NAME, SAPLING_DATA, 1) selectSafely(SAPLING_NAME, SAPLING_DATA, 1)
ms.execute("2FRP") ms.execute("2FRP")
selectSafely(SAPLING_NAME, SAPLING_DATA, 1) selectSafely(SAPLING_NAME, SAPLING_DATA, 1)
@ -151,6 +120,20 @@ local function plantTree()
ms.execute("LBP") ms.execute("LBP")
end end
--[[
Uses the robot's axe to chop a tree, and quits if the lumber axe provided has
less than 10% durability.
return - integer: 1 if the tree was chopped, 0 otherwise.
--]]
local function chopTree()
if (isTreeGrown()) then
ms.execute("S")
plantTree()
return 1
end
return 0
end
--[[ --[[
Moves to the next tree in a row. Moves to the next tree in a row.
current_col - integer: The current column. current_col - integer: The current column.
@ -213,43 +196,21 @@ local function chopOrchard(rows, cols)
end end
--[[ --[[
Collects items from an array of trees. Reads any given arguments and uses them for program constants instead of
default values.
args - table: the arguments passed to the program.
--]] --]]
local function collectItems(rows, cols) local function getSettingsFromArgs(args)
doForEachTree(rows, cols, pickupItems) ROWS = tonumber(args[1])
COLS = tonumber(args[2])
TREE_SPACING = tonumber(args[3])
move_to_start = args[4]
return_from_start = args[5]
end end
--[[ local args = {...}
Plants saplings for the array of trees. if (#args == 5) then
--]] getSettingsFromArgs(args)
local function plantSaplings(rows, cols)
local saplings_needed = TREES_CHOPPED * 4
if (getItemCount(SAPLING_NAME, SAPLING_DATA) < saplings_needed) then
print("Not enough saplings. Needed: "..saplings_needed..". Add some and press ENTER.")
io.read()
end
doForEachTree(rows, cols, plantTree)
TREES_CHOPPED = 0
end
--[[
Deposits all items into a chest below the robot.
--]]
local function depositItems()
for i=1,16 do
robot.select(i)
robot.dropDown()
end
robot.select(1)
end end
chopOrchard(ROWS, COLS) chopOrchard(ROWS, COLS)
depositItems()
print("Orchard chopped. Waiting 2.5 min before collecting saplings...")
for i=1,(DELAY) do
os.sleep(10)
print(i*10)
end
collectItems(ROWS, COLS)
plantSaplings(ROWS, COLS)
depositItems()