This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jakemg's profile picture

turtle remember where it left off?

Started by jakemg, 02 May 2013 - 05:33 PM
jakemg #1
Posted 02 May 2013 - 07:33 PM
Hi i am trying to get my turtle to remember where it left off after coming "home" to empty.

basicly what the turtle is doing now is strip mining forward coming home then making tunnels where he has mined (so branch mining)
this part works fine but my problem is i want it to kind of quarry its way down so after it is done with strip mine and the tunnels it will come home go down and restart the process but the problem is that when i call him home after he is down a layer the goes back to the layer where he started and then doesnt remember what layer he was on so he will begin to dig air instead of going down more layers.


tdist = 0
dist = 0
depth = 0
local tArgs = {...}
local times = tArgs[1] and tonumber(tArgs[1]) or 1
  function down()
	turtle.turnRight()
	turtle.turnRight()
	turtle.digDown()
	turtle.down()
	depth = depth + 1
	turtle.digDown()
	turtle.down()
	depth = depth + 1
   end

  function turn()
  turtle.turnLeft()
  turtle.turnLeft()
  end
  


function tunnel()
go()
  go()
  dist = dist + 2
  turtle.turnLeft()
  for i=1,times do
  go()
  tdist = tdist + 1
  turtle.dig()
  turtle.digUp()
  turtle.digDown()
  end
end
function tunnelhome()
  turn()
  while tdist > 0 do
  go()
  tdist = tdist - 1
  end
  turtle.turnLeft()
end
  

  function go()
   while not turtle.forward() do
	turtle.dig()
   end
   sleep(0.8)
  end

  function dig()
   turtle.dig()
   turtle.digUp()
   turtle.digDown()
   turtle.turnRight()
   turtle.dig()
   go()
   turtle.digUp()
   turtle.digDown()
   turtle.turnLeft()
   turtle.turnLeft()
   go()
   turtle.dig()
   go()
   turtle.digUp()
   turtle.digDown()
   turtle.turnRight()
   turtle.turnRight()
   go()
   turtle.turnLeft()
   go()
   dist = dist + 1
  end

  function empty()
   if turtle.getItemCount(16) > 15 then
	turtle.turnLeft()
	turtle.turnLeft()
   while dist > 0 do
   go()
   end
   select(1)
   for i=1,16 do
   select(i)
   turtle.drop()
   end
   end
  end

  function home()
  print(dist)
  turtle.turnLeft()
  turtle.turnLeft()
  while dist > 0 do
  go()
  dist = dist - 1
  print(dist)
  end
  while depth > 0 do
  turtle.digUp()
  turtle.up()
  depth = depth - 1
  end
	for i=1,16 do
	  turtle.select(i)
	  turtle.drop()
	end
  end

function all()
for i=1,times do	
  for i=1,times do
   dig()

   empty()
  end

  home()

  turtle.turnLeft()
  turtle.turnLeft()

  for i=1,times do
  tunnel()
  tunnelhome()
  end
  home()
  down()
end
end
  all()

edit also i need it to remember its place when it empties too because when he gets down a couple a layers and his inventory gets full he will go back to the chest and continue his strip mine from there instead if going back down
The_Awe35 #2
Posted 02 May 2013 - 08:57 PM
It depends, do you just want it to find its place in the world, or the place in the code. if the first then you can just do

x,y,z = gps.locate()
if the latter, then there are a couple of options, none of them easy. You can completely sync your program with gps, so it will always find out where it is. example:


instead of:
for i = 1,10 do
turtle.forward()
end

while not x == 210 do -- whatever the spot it needs to be
x,y,z = gps.locate() -- can make this into a function
turtle.forward()
end

You can also use the fs api and get it to change some variables in another file, then on startup, it will find what those variables are. I think it is best to do this with the gps function, and then use this as a reference as to what you are doing. eg. mining, refueling, etc.

The last one I know of is to use the amount of fuel it has used to find its place in the code. Not easy because you would refuel sometimes, so it would have to be flexible. Not impossible, but still hard. If anyone one else knows of a better way, I would love to know.
jakemg #3
Posted 02 May 2013 - 09:09 PM
Yes but how would it rember where it is? How could I get it to save its coords then get back to there
Bubba #4
Posted 02 May 2013 - 09:20 PM
Yes but how would it rember where it is? How could I get it to save its coords then get back to there

Due to limits on rednet range, I prefer using a local positioning service over GPS + servers.

Here's how I do it in general:

Create your functions that load and write coordinates to a file, so that you can keep track of them

local currentX, currentY, currentZ,dir = 0,0,0,0 --Defaults to zero
local function load()
if fs.exists("your_coord_file") then
	local handle = fs.open("your_coord_file", "r")
	local content = handle.readAll()
	handle.close()
	local x,y,z, rot = content:match("^(.-),(.-),(.-),(.*)") --This is regex, which you check out on the Lua PIL (just google lua regex)
	currentX, currentY, currentZ, dir = tonumber(x) or 0, tonumber(y) or 0, tonumber(z) or 0, tonumber(rot) or 0
  end
end

local function save()
  local f = fs.open("your_coord_file", "w")
  f.write(coordX..","..coordY..","..coordZ..","..dir)
  f.close()
end

Now there are easier ways to go about this (by easier, I mean without regex), but I prefer it due to its readability and single line nature. Now whenever you move the turtle, increase the specified x, y, or z value and run the save function. On startup, you will just call load() and it will reset the coordinates to the turtle's previous location in relation to its starting point.

If you need to get back, all you need to do is determine the direction of the turtle and make the appropriate amount of turns to be facing back in towards 0,0,0. Once you have the turtle facing the correct direction, just use a while loop until it reaches the coordinates 0,0,0.