Posted 08 July 2017 - 10:09 PM
I am working on a program that uses turtles of all different types to do work for me, like this here turtle butler program. Here's my code so far:
turtleClient.lua:
turtleClient.lua:
-- Compatibility: Lua-5.0
function Split(str, delim, maxNb)
-- Eliminate bad cases...
if string.find(str, delim) == nil then
return { str }
end
if maxNb == nil or maxNb < 1 then
maxNb = 0 -- No limit
end
local result = {}
local pat = "(.-)" .. delim .. "()"
local nb = 0
local lastPos
for part, pos in string.gfind(str, pat) do
nb = nb + 1
result[nb] = part
lastPos = pos
if nb == maxNb then
break
end
end
-- Handle the last field
if nb ~= maxNb then
result[nb + 1] = string.sub(str, lastPos)
end
return result
end
function look(direction)
while direction ~= orientations[orientation] do
right()
end
end
function left()
orientation = (orientation - 2) % 4
orientation = orientation + 1
turtle.turnLeft()
end
function right()
orientation = orientation % 4
orientation = orientation + 1
turtle.turnRight()
end
function moveForward()
xCoord = xCoord + xDiff[orientation]
zCoord = zCoord + zDiff[orientation]
turtle.dig()
moved = false
while not(moved) do
moved = turtle.forward()
end
end
function moveUp()
yCoord = yCoord + 1
turtle.digUp()
moved=false
while not(moved) do
moved = turtle.up()
end
end
function moveDown()
yCoord = yCoord - 1
turtle.digDown()
moved=false
while not(moved) do
moved = turtle.down()
end
end
function goTo(x,y,z)
while yCoord < y do
moveUp()
end
while yCoord > y do
moveDown()
end
if x < xCoord then
look("west")
while x < xCoord do
moveForward()
end
end
else if x > xCoord then
look("east")
while x > xCoord do
moveBackward()
end
end
if z < zCoord then
look("north")
while z < zCoord do
moveForward()
end
end
else if z > zCoord then
look("south")
while z > zCoord do
moveForward()
end
end
end
local moved,ID,x,y,z,free,xDiff,zDiff,orientation,orientations,xCoord,yCoord,zCoord = false,0,0,0,0,true,{0,1,0,-1},{-1,0,1,0},1,{"north", "east", "south", "west"},gps.locate()
rednet.open("top")
print("Looking for a server...")
while ID == nil do
rednet.broadcast("TurtleConnect")
ID, message, _ = rednet.receive(5)
end
while true do
CID, message, _ = rednet.receive()
if CID ~= nil then
if CID ~= ID then
print("ERROR: Message recieved from id "..CID..", expected "..ID" \""..message.."\"")
exit()
else if message == "Request" and free then
rednet.send(ID, "Ready")
CID, message, _ = rednet.recieve()
if CID == ID then
x,y,z = Split(message, ",", 3)
goto(x,y,z)
end
end
end
end
end
What am I doing wrong?