Posted 12 February 2013 - 06:45 AM
Title: Need help with my custom turtle mining application
Hi there,
so, I've basically finished coding my custom turtle mining system which consists of a computer with a wireless modem, a wireless mining turtle (or more) and a GPS tower. Everything works, basically: the turtle goes to a coordinate which you've either typed into the computer by hand or wrote on a disc which is connected to the pc, starts my modified "excavate" script, returns to the base if the inventory is full to empty it out, and returns to the base and stops the program, waiting for new coordinates when it reached the bedrock.
There's only one problem.
I can't seem to manage to edit the excavate script so that the turtle won't, upon arriving at the hole it dug, start from the beginning and slowly work its way down, basically taking ages. Also, the turtle will sometimes start the excavate script into "another direction" than before, so the hole get's messed up.
If anybody has an idea what's wrong with my code, that would be amazing.
So, here it is. Bear in mind that my only experience with programming are a high school course in Delphi, and that this is kind of my first lua program. It's probably a terrible mess and doesn't use functions or anything like that. But it does work except for the problem described above.
The code for the master computer:
All it can do is navigate the user through a menu, read coordinates from disks, change a disks name, and send coordinates and commands to the turtle via rednet. I will modify this code to work with more than one turtle which shouldn't be a huge problem.
This is the code for a turtle:
As you can see, all it does is basically telling the master computer that it can accept commands, and then receive coordinates, which are then sent to either a "goto" script or my modified "excavate" script.
Here's the excavate script:
It's basically the standard script that comes with CC, but modified so that it saves the turtles coordinates at the beginning of the hole, and then returns there after unloading it's contents at the main base.
Any idea how I could code my requests above? Thanks a lot!
Jason
Hi there,
so, I've basically finished coding my custom turtle mining system which consists of a computer with a wireless modem, a wireless mining turtle (or more) and a GPS tower. Everything works, basically: the turtle goes to a coordinate which you've either typed into the computer by hand or wrote on a disc which is connected to the pc, starts my modified "excavate" script, returns to the base if the inventory is full to empty it out, and returns to the base and stops the program, waiting for new coordinates when it reached the bedrock.
There's only one problem.
I can't seem to manage to edit the excavate script so that the turtle won't, upon arriving at the hole it dug, start from the beginning and slowly work its way down, basically taking ages. Also, the turtle will sometimes start the excavate script into "another direction" than before, so the hole get's messed up.
If anybody has an idea what's wrong with my code, that would be amazing.
So, here it is. Bear in mind that my only experience with programming are a high school course in Delphi, and that this is kind of my first lua program. It's probably a terrible mess and doesn't use functions or anything like that. But it does work except for the problem described above.
The code for the master computer:
rednet.open("right")
function clear() term.clear() term.setCursorPos(1, 1) end
while true do
clear()
print("waiting for turtle")
id, msg = rednet.receive() --Wait for a turtle to say it is "Listening"
if msg == "Listening" then
print("turtle found, please enter command: goto, excavate, favorites, gohome")
command = io.read()
if command == "goto" then
clear()
rednet.send(id,"goto")
print("Enter X")
local gox = tonumber(io.read())
clear()
print("Enter Y")
local goy = tonumber(io.read())
clear()
print("Enter Z")
local goz = tonumber(io.read())
clear()
print("Coordinates are: ")
print("X: ",x)
print("Y: ",y)
print("Z: ",z)
print("Sending coordinates")
rednet.send(id, tostring(gox))
sleep(0.5)
rednet.send(id, tostring(goy))
sleep(0.5)
rednet.send(id, tostring(goz))
else
if command == "gohome" then
rednet.send(id,"goto")
rednet.send(id,tostring(638))
sleep(0.5)
rednet.send(id,tostring(79))
sleep(0.5)
rednet.send(id,tostring(552))
sleep(0.5)
else
if command == "excavate" then
clear()
rednet.send(id,"excavate")
print("Enter X")
local gox = tonumber(io.read())
clear()
print("Enter Y")
local goy = tonumber(io.read())
clear()
print("Enter Z")
local goz = tonumber(io.read())
clear()
print("Coordinates are: ")
print("X: ",x)
print("Y: ",y)
print("Z: ",z)
print("Sending coordinates")
rednet.send(id, tostring(gox))
sleep(0.5)
rednet.send(id, tostring(goy))
sleep(0.5)
rednet.send(id, tostring(goz))
else
if command == "favorites" then
clear()
side = "top"
while not disk.isPresent(side) do
clear()
print("no disk found")
print("please enter the side of your disk drive")
side = io.read()
end
clear()
print("disk drive initialized.")
sleep(0.5)
clear()
print("disk drive initialized..")
sleep(0.5)
clear()
print("disk drive initialized...")
sleep(0.5)
clear()
if disk.hasData(side) == true then
local label = disk.getLabel
print("disk '",label,"' found")
print("Please enter a command: rename, showfavs, addfav, mainmenu")
command2 = io.read()
if command2 == "rename" then
print("Please enter the new disk label")
newname = io.read()
disk.setLabel(side,newname)
print("Label ",newname," sucessfully written to the disk")
sleep(2)
else
if command2 == "showfavs" then
print("Checking for valid favorites file on disk...")
sleep(1)
mmpath = "disk/favorites"
mxpath = "disk/x"
mypath = "disk/y"
mzpath = "disk/z"
if fs.exists(mmpath) == true then
print("favorites file found on disk")
h = fs.open(mmpath,"r")
sleep(2)
else
print("no file found, returning to main menu")
sleep(2)
os.reboot()
end
clear()
print("favorites on disk:")
favs = h.readAll()
print(favs)
h.close()
print("/n")
print("would you like to send this coordinate to the turtle?")
input = io.read()
if input == "yes" then
x = fs.open(mxpath,"r")
y = fs.open(mypath,"r")
z = fs.open(mzpath,"r")
xx = x.readAll(mxpath,"r")
yy = y.readAll(mypath,"r")
zz = z.readAll(mzpath,"z")
xx = tostring(xx)
yy = tostring(yy)
zz = tostring(zz)
print("should the turtle excavate there?")
input = io.read()
if input == "yes" then
print("sending coordinates")
rednet.send(id,"excavate")
rednet.send(id,xx)
sleep(0.5)
rednet.send(id,yy)
sleep(0.5)
rednet.send(id,zz)
print("coordinates sent to turtle")
sleep(2)
end
if input == "no" then
print("sending coordinates")
rednet.send(id,"goto")
rednet.send(id,xx)
sleep(0.5)
rednet.send(id,yy)
sleep(0.5)
rednet.send(id,zz)
print("coordinates sent to turtle")
sleep(2)
end
x.close()
y.close()
z.close()
end
end
end
sleep(2)
else
print("No valid disk found, returning to main menu")
sleep(2)
end
end
end
end
end
end
end
All it can do is navigate the user through a menu, read coordinates from disks, change a disks name, and send coordinates and commands to the turtle via rednet. I will modify this code to work with more than one turtle which shouldn't be a huge problem.
This is the code for a turtle:
rednet.open("right")
function clear() term.clear() term.setCursorPos(1, 1) end
while true do
clear()
print("listening on all IDs, os.ID: ", os.computerID(), ". rednet broadcasts and direct messages accepted")
rednet.broadcast("Listening")
local id, msg = rednet.receive(10)
if msg == "goto" then
print("received goto signal from main base (", id,"). attempting to get requested coordinates")
local id, msg = rednet.receive()
gox = tonumber(msg)
local id, msg = rednet.receive()
goy = tonumber(msg)
local id, msg = rednet.receive()
goz = tonumber(msg)
print("coordinates recived, going to ", gox, " ", goy, " ", goz)
shell.run("goto", gox, goy, goz) --Run the goto program with the recived coordinates
else
if msg == "excavate" then
print("received excavate signal from main base (", id,"). attempting to get requested coordinates")
local id, msg = rednet.receive()
gox = tonumber(msg)
local id, msg = rednet.receive()
goy = tonumber(msg)
local id, msg = rednet.receive()
goz = tonumber(msg)
print("coordinates recived, going to ", gox, " ", goy, " ", goz)
shell.run("goto", gox, goy, goz) --Run the goto program with the recived coordinates
shell.run("excavater", 10)
shell.run("goto", 638,79,552)
end
end
end
As you can see, all it does is basically telling the master computer that it can accept commands, and then receive coordinates, which are then sent to either a "goto" script or my modified "excavate" script.
Here's the excavate script:
local tArgs = { ... }
if #tArgs ~= 1 then
print( "Usage: excavate <diameter>" )
return
end
-- Mine in a quarry pattern until we hit something we can't dig
local size = tonumber( tArgs[1] )
if size < 1 then
print( "Excavate diameter must be positive" )
return
end
xx,yy,zz = gps.locate(5)
xx = tonumber(xx)
yy = tonumber(yy)
zz = tonumber(zz)
local depth = 0
local unloaded = 0
local collected = 0
local xPos,zPos = 0,0
local xDir,zDir = 0,1
local goTo -- Filled in further down
local refuel -- Filled in further down
local function unload()
print( "Unloading items..." )
shell.run("goto", 638,79,552)
for n=1,16 do
unloaded = unloaded + turtle.getItemCount(n)
turtle.select(n)
turtle.drop()
end
collected = 0
turtle.select(1)
end
local function returnSupplies()
local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
print( "Returning to surface..." )
goTo( 0,0,0,0,-1 )
local fuelNeeded = x+y+z + x+y+z + 1
if not refuel( fuelNeeded ) then
unload()
print( "Waiting for fuel" )
while not refuel( fuelNeeded ) do
sleep(1)
end
else
unload()
end
print( "Resuming mining..." )
shell.run("goto", xx,yy,zz)
end
local function collect()
local bFull = true
local nTotalItems = 0
for n=1,16 do
local nCount = turtle.getItemCount(n)
if nCount == 0 then
bFull = false
end
nTotalItems = nTotalItems + nCount
end
if nTotalItems > collected then
collected = nTotalItems
if math.fmod(collected + unloaded, 50) == 0 then
print( "Mined "..(collected + unloaded).." items." )
end
end
if bFull then
print( "No empty slots left." )
return false
end
return true
end
function refuel( ammount )
local fuelLevel = turtle.getFuelLevel()
if fuelLevel == "unlimited" then
return true
end
local needed = ammount or (xPos + zPos + depth + 1)
if turtle.getFuelLevel() < needed then
local fueled = false
for n=1,16 do
if turtle.getItemCount(n) > 0 then
turtle.select(n)
if turtle.refuel(1) then
while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
turtle.refuel(1)
end
if turtle.getFuelLevel() >= needed then
turtle.select(1)
return true
end
end
end
end
turtle.select(1)
return false
end
return true
end
local function tryForwards()
if not refuel() then
print( "Not enough Fuel" )
returnSupplies()
end
while not turtle.forward() do
if turtle.detect() then
if turtle.dig() then
if not collect() then
returnSupplies()
end
else
return false
end
elseif turtle.attack() then
if not collect() then
returnSupplies()
end
else
sleep( 0.5 )
end
end
xPos = xPos + xDir
zPos = zPos + zDir
return true
end
local function tryDown()
if not refuel() then
print( "Not enough Fuel" )
returnSupplies()
end
while not turtle.down() do
if turtle.detectDown() then
if turtle.digDown() then
if not collect() then
returnSupplies()
end
else
return false
end
elseif turtle.attackDown() then
if not collect() then
returnSupplies()
end
else
sleep( 0.5 )
end
end
depth = depth + 1
if math.fmod( depth, 10 ) == 0 then
print( "Descended "..depth.." metres." )
end
return true
end
local function turnLeft()
turtle.turnLeft()
xDir, zDir = -zDir, xDir
end
local function turnRight()
turtle.turnRight()
xDir, zDir = zDir, -xDir
end
function goTo( x, y, z, xd, zd )
while depth > y do
if turtle.up() then
depth = depth - 1
elseif turtle.digUp() or turtle.attackUp() then
collect()
else
sleep( 0.5 )
end
end
if xPos > x then
while xDir ~= -1 do
turnLeft()
end
while xPos > x do
if turtle.forward() then
xPos = xPos - 1
elseif turtle.dig() or turtle.attack() then
collect()
else
sleep( 0.5 )
end
end
elseif xPos < x then
while xDir ~= 1 do
turnLeft()
end
while xPos < x do
if turtle.forward() then
xPos = xPos + 1
elseif turtle.dig() or turtle.attack() then
collect()
else
sleep( 0.5 )
end
end
end
if zPos > z then
while zDir ~= -1 do
turnLeft()
end
while zPos > z do
if turtle.forward() then
zPos = zPos - 1
elseif turtle.dig() or turtle.attack() then
collect()
else
sleep( 0.5 )
end
end
elseif zPos < z then
while zDir ~= 1 do
turnLeft()
end
while zPos < z do
if turtle.forward() then
zPos = zPos + 1
elseif turtle.dig() or turtle.attack() then
collect()
else
sleep( 0.5 )
end
end
end
while depth < y do
if turtle.down() then
depth = depth + 1
elseif turtle.digDown() or turtle.attackDown() then
collect()
else
sleep( 0.5 )
end
end
while zDir ~= zd or xDir ~= xd do
turnLeft()
end
end
if not refuel() then
print( "Out of Fuel" )
return
end
print( "Excavating..." )
local reseal = false
turtle.select(1)
if turtle.digDown() then
reseal = true
end
local alternate = 0
local done = false
while not done do
for n=1,size do
for m=1,size-1 do
if not tryForwards() then
done = true
break
end
end
if done then
break
end
if n<size then
if math.fmod(n + alternate,2) == 0 then
turnLeft()
if not tryForwards() then
done = true
break
end
turnLeft()
else
turnRight()
if not tryForwards() then
done = true
break
end
turnRight()
end
end
end
if done then
break
end
if size > 1 then
if math.fmod(size,2) == 0 then
turnRight()
else
if alternate == 0 then
turnLeft()
else
turnRight()
end
alternate = 1 - alternate
end
end
if not tryDown() then
done = true
break
end
end
print( "Returning to surface..." )
-- Return to where we started
goTo( 0,0,0,0,-1 )
unload()
goTo( 0,0,0,0,1 )
-- Seal the hole
if reseal then
turtle.placeDown()
end
print( "Mined "..(collected + unloaded).." items total." )
It's basically the standard script that comes with CC, but modified so that it saves the turtles coordinates at the beginning of the hole, and then returns there after unloading it's contents at the main base.
Any idea how I could code my requests above? Thanks a lot!
Jason