Posted 02 June 2015 - 12:31 PM
Hey,
I started teaching myself Lua the other day and have enjoyed making my turtle move around. My current goal is a turtle that is able to clear a wooded area of trees so that I can deploy a Buildcraft quarry. If you're wondering why not use a mining turtle it's because I like recycling the junk to turn into UU matter with IC2.
My question is how do I have the turtle compare what it has detected against a list of things? My current rather inelegant solution is below but this will make for a rather wide and unwieldy script.
If it helps my background is self taught intermediate Powershell and the script in full is below:
Any help is very much appreciated :)/>
I started teaching myself Lua the other day and have enjoyed making my turtle move around. My current goal is a turtle that is able to clear a wooded area of trees so that I can deploy a Buildcraft quarry. If you're wondering why not use a mining turtle it's because I like recycling the junk to turn into UU matter with IC2.
My question is how do I have the turtle compare what it has detected against a list of things? My current rather inelegant solution is below but this will make for a rather wide and unwieldy script.
if string.match(data.name, "dirt") or string.match(data.name, "grass") or string.match(data.name, "stone") or string.match(data.name, "sand") then
if not turtle.up() then
turtle.digUp()
end
do return end
end
If it helps my background is self taught intermediate Powershell and the script in full is below:
Spoiler
print("Felling start")
function CheckFuel()
if turtle.getFuelLevel() <= 100 then
turtle.refuel(1)
print ("Turtle refuelled by 1")
print("Turtle fuel level: ", turtle.getFuelLevel())
end
end
--[[function TurtleInspect(direction)
if direction == "down" then
local Inspect, data = turtle.inspectDown()
end
end]]
function CheckGround()
local GroundPresent, data = turtle.inspectDown()
while not GroundPresent do
GroundPresent, data = turtle.inspectDown()
turtle.down()
end
end
function ObstacleTest()
if string.match(data.name, "log") then
print("Tree found")
CutTree()
end
if string.match(data.name, "leaves") then
turtle.dig()
do return end
end
local ObstacleUp, dataUp = turtle.inspectUp()
if ObstacleUp then
if string.match(dataUp.name, "leaves") then
turtle.digUp()
do return end
end
end
if string.match(data.name, "dirt") or string.match(data.name, "grass") or string.match(data.name, "stone") or string.match(data.name, "sand") then
if not turtle.up() then
turtle.digUp()
end
do return end
end
if not turtle.up() then
turtle.dig()
end
end
function AvoidObstacle()
--local AvoidItems = {"dirt", "grass", "stone"}
ObstaclePresent, data = turtle.inspect()
while ObstaclePresent do
CheckFuel()
ObstacleTest()
ObstaclePresent, data = turtle.inspect()
end
turtle.forward()
print("Obstacle avoided")
end
function CutTree()
CheckFuel()
turtle.dig()
turtle.forward()
local TreeDown, data = turtle.inspectDown()
local k = 0
while string.match(data.name, "log") do
turtle.digDown()
turtle.down()
k = k+1
TreeDown, data = turtle.inspectDown()
end
while k > 0 do
turtle.up()
k = k-1
end
local TreeUp, data = turtle.inspectUp()
while TreeUp do
if string.match(data.name, "log") then
turtle.digUp()
turtle.up()
else
do break end
end
TreeUp, data = turtle.inspectUp()
end
CheckGround()
CheckFuel()
print("Tree cut down")
end
function ScanRow()
for i=1,50 do
CheckFuel()
CheckGround()
local TreeDown, data = turtle.inspectDown()
if TreeDown and string.match(data.name, "log") then
print("Tree found")
CutTree()
end
local detected, data = turtle.inspect()
if detected then
if string.match(data.name, "log") then
print("Tree found")
CutTree()
else
AvoidObstacle()
end
else
turtle.forward()
end
end
end
for j=1,50,2 do
CheckFuel()
ScanRow()
print("j = ", j)
turtle.turnRight()
turtle.forward()
turtle.turnRight()
CheckFuel()
ScanRow()
print("j = ", j+1)
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
end
Any help is very much appreciated :)/>