Reformatted treefarm script, added minifier script.

This commit is contained in:
Andrew Lalis 2018-06-12 14:39:59 +02:00
parent 4a6d2f815c
commit 2565984a92
2 changed files with 3289 additions and 11 deletions

3227
minify.lua Normal file

File diff suppressed because it is too large Load Diff

View File

@ -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 robot = require("robot")
local component = require("component") local component = require("component")
local tractor_beam = component.tractor_beam local tractor_beam = component.tractor_beam
@ -16,15 +28,23 @@ local BONEMEAL_DATA = 15
--Flag for if program should run until out of resources. --Flag for if program should run until out of resources.
local continuous = false local continuous = false
--Exit the program. --[[
Exits the program.
--]]
local function quit() local function quit()
print("#--------------------------------#") print("#--------------------------------#")
print("# Tree Chopping Program exited. #") print("# Tree Chopping Program exited. #")
os.exit() os.exit()
end 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 for i=1,16 do
local stack = ic.getStackInInternalSlot(i) local stack = ic.getStackInInternalSlot(i)
if (stack ~= nil and stack.name == item_name and stack.damage == item_data) then 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 return false
end 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) local success = selectItemByName(item_name, item_data)
if continuous and not success then if continuous and not success then
print("Out of "..item_name..", exiting.") print("Out of "..item_name..", exiting.")
@ -49,7 +78,10 @@ local function selectSafely(item_name, item_data)
end end
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() local function plantSapling()
selectSafely(SAPLING_NAME, SAPLING_DATA) selectSafely(SAPLING_NAME, SAPLING_DATA)
local success = robot.place() local success = robot.place()
@ -60,7 +92,10 @@ local function plantSapling()
end end
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 function applyBonemeal()
local success, block_type = robot.detect() local success, block_type = robot.detect()
while block_type ~= "solid" do while block_type ~= "solid" do
@ -70,7 +105,10 @@ local function applyBonemeal()
end end
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 function chopTree()
local durability = robot.durability() local durability = robot.durability()
if continuous and (durability == nil or durability < 0.1) then if continuous and (durability == nil or durability < 0.1) then
@ -85,7 +123,10 @@ local function chopTree()
robot.swing() robot.swing()
end 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 function pickupItems()
local success = tractor_beam.suck() local success = tractor_beam.suck()
while success do while success do
@ -93,11 +134,18 @@ local function pickupItems()
end end
end end
--[[
Grows a tree by planting a sapling and applying bonemeal until it is grown.
--]]
local function growTree() local function growTree()
plantSapling() plantSapling()
applyBonemeal() applyBonemeal()
end end
--[[
The entire cycle of the farm. Grows a tree, harvests the wood, and picks up
the items.
--]]
local function farmTree() local function farmTree()
growTree() growTree()
chopTree() chopTree()
@ -105,6 +153,9 @@ local function farmTree()
pickupItems() pickupItems()
end end
--[[
Main function in which the iterations of the cycle are performed.
--]]
local function main() local function main()
print("# Andrew's Tree Chopping Program #") print("# Andrew's Tree Chopping Program #")
print("# Copyright 2018 Andrew Lalis #") print("# Copyright 2018 Andrew Lalis #")