Reformatted treefarm script, added minifier script.
This commit is contained in:
parent
4a6d2f815c
commit
2565984a92
File diff suppressed because it is too large
Load Diff
|
@ -1,6 +1,18 @@
|
|||
--Robotic Tree Farmer
|
||||
-- Copyright 2018 Andrew Lalis. All rights reserved.
|
||||
--[[
|
||||
Author: Andrew Lalis
|
||||
File: TreeFarm.lua
|
||||
Version: 1.0
|
||||
Last Modified: 12-06-2018
|
||||
|
||||
Description:
|
||||
This script lets a robot, equipped with a tractor_beam and inventory controller
|
||||
module, chop trees with a Tinker's Construct lumber axe. It will automatically
|
||||
stop when it runs out of saplings or bonemeal, or when its axe is close to
|
||||
breaking. It also can either chop a certain number of trees, or simply chop
|
||||
until its resources are depleted.
|
||||
--]]
|
||||
|
||||
--Require statements and componenent definitions.
|
||||
local robot = require("robot")
|
||||
local component = require("component")
|
||||
local tractor_beam = component.tractor_beam
|
||||
|
@ -16,15 +28,23 @@ local BONEMEAL_DATA = 15
|
|||
--Flag for if program should run until out of resources.
|
||||
local continuous = false
|
||||
|
||||
--Exit the program.
|
||||
--[[
|
||||
Exits the program.
|
||||
--]]
|
||||
local function quit()
|
||||
print("#--------------------------------#")
|
||||
print("# Tree Chopping Program exited. #")
|
||||
os.exit()
|
||||
end
|
||||
|
||||
--Select a slot containing the specified item.
|
||||
local function selectItemByName(item_name, item_data)
|
||||
--[[
|
||||
Select an item, given its name and damage value.
|
||||
item_name - string: The id string for an item.
|
||||
item_data - number: The damage value, or variation of an item. Defaults to zero.
|
||||
return - boolean: True if at least one slot contains the item. That slot is now
|
||||
selected.
|
||||
--]]
|
||||
local function selectItemByName(item_name, item_data=0)
|
||||
for i=1,16 do
|
||||
local stack = ic.getStackInInternalSlot(i)
|
||||
if (stack ~= nil and stack.name == item_name and stack.damage == item_data) then
|
||||
|
@ -35,8 +55,17 @@ local function selectItemByName(item_name, item_data)
|
|||
return false
|
||||
end
|
||||
|
||||
--Select a slot with an item, and if it can't be found, ask the user to add it.
|
||||
local function selectSafely(item_name, item_data)
|
||||
--[[
|
||||
Select an item, similar to selectItemByName, but if the item cannot be found,
|
||||
the user will be prompted to add it to the robot's inventory and press enter to
|
||||
continue.
|
||||
item_name - string: The id string for an item.
|
||||
item_data - number: The damage value, or variation of an item. Defaults to zero.
|
||||
return - nil: If set to be continuous, then if the item cannot be found, then
|
||||
the program will exit. If not, it will loop until the item is provided by the
|
||||
user.
|
||||
--]]
|
||||
local function selectSafely(item_name, item_data=0)
|
||||
local success = selectItemByName(item_name, item_data)
|
||||
if continuous and not success then
|
||||
print("Out of "..item_name..", exiting.")
|
||||
|
@ -49,7 +78,10 @@ local function selectSafely(item_name, item_data)
|
|||
end
|
||||
end
|
||||
|
||||
--Plants a sapling.
|
||||
--[[
|
||||
Plants a sapling, and if it can't place one at first, loops until it is
|
||||
possible.
|
||||
--]]
|
||||
local function plantSapling()
|
||||
selectSafely(SAPLING_NAME, SAPLING_DATA)
|
||||
local success = robot.place()
|
||||
|
@ -60,7 +92,10 @@ local function plantSapling()
|
|||
end
|
||||
end
|
||||
|
||||
--Repeatedly applies bonemeal until a tree has grown.
|
||||
--[[
|
||||
Repeatedly applies bonemeal to the sapling until either the sapling has grown,
|
||||
or the robot runs out of bonemeal.
|
||||
--]]
|
||||
local function applyBonemeal()
|
||||
local success, block_type = robot.detect()
|
||||
while block_type ~= "solid" do
|
||||
|
@ -70,7 +105,10 @@ local function applyBonemeal()
|
|||
end
|
||||
end
|
||||
|
||||
--Chops a tree, first checking the status of the tool.
|
||||
--[[
|
||||
Uses the robot's axe to chop a tree, and quits if the lumber axe provided has
|
||||
less than 10% durability.
|
||||
--]]
|
||||
local function chopTree()
|
||||
local durability = robot.durability()
|
||||
if continuous and (durability == nil or durability < 0.1) then
|
||||
|
@ -85,7 +123,10 @@ local function chopTree()
|
|||
robot.swing()
|
||||
end
|
||||
|
||||
--Uses the tractor_beam component to pick up nearby items.
|
||||
--[[
|
||||
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
|
||||
|
@ -93,11 +134,18 @@ local function pickupItems()
|
|||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
Grows a tree by planting a sapling and applying bonemeal until it is grown.
|
||||
--]]
|
||||
local function growTree()
|
||||
plantSapling()
|
||||
applyBonemeal()
|
||||
end
|
||||
|
||||
--[[
|
||||
The entire cycle of the farm. Grows a tree, harvests the wood, and picks up
|
||||
the items.
|
||||
--]]
|
||||
local function farmTree()
|
||||
growTree()
|
||||
chopTree()
|
||||
|
@ -105,6 +153,9 @@ local function farmTree()
|
|||
pickupItems()
|
||||
end
|
||||
|
||||
--[[
|
||||
Main function in which the iterations of the cycle are performed.
|
||||
--]]
|
||||
local function main()
|
||||
print("# Andrew's Tree Chopping Program #")
|
||||
print("# Copyright 2018 Andrew Lalis #")
|
Loading…
Reference in New Issue