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

Mining Turtle Persistence (No Gps)

Started by campicus, 27 August 2013 - 08:32 PM
campicus #1
Posted 27 August 2013 - 10:32 PM
Hi guys, I am in need of your expert knowledge (once again).

My mining turtle is working well now (finally) and I have managed to build in a tracking system, so that if the server crashes, they can figure out where they were before and continue on.

The problem I am having is in my 'goForward' function (below). I think my if statement, where it figures out what way it is facing etc., takes too long. So the server can crash between the turtle moving forward and updating its position.


function goForward()
  refuel()
  while (not turtle.forward()) do
    cChest()
    turtle.dig()
    turtle.attack()
    sleep(0.2)
  end
  trackPos()
  if facing == "N" then
    yPos = yPos + 1
  elseif facing == "S" then
    yPos = yPos - 1
  elseif facing == "E" then
    xPos = xPos - 1
  elseif facing == "W" then
    xPos = xPos + 1
  end
  trackPos()
  nMoved = nMoved + 1
end

Hopefully my problem makes sense, let me know if you have any suggestions!

Note: I don't want to use a gps system
ElvishJerricco #2
Posted 27 August 2013 - 11:33 PM
You're worrying about something that's not likely at all to happen. I couldn't give you any statistics but if it even happened once I'd be very surprised. Also, you can't really trim down the if statement for speed. If you're really worried about it, compare to numbers, not strings. So 1 means north, 2 means west, etc. But again, that code is so short, and gets run with gaps so much bigger than the runtime, that it probably doesn't need to be worried about.
campicus #3
Posted 27 August 2013 - 11:40 PM
You're worrying about something that's not likely at all to happen. I couldn't give you any statistics but if it even happened once I'd be very surprised. Also, you can't really trim down the if statement for speed. If you're really worried about it, compare to numbers, not strings. So 1 means north, 2 means west, etc. But again, that code is so short, and gets run with gaps so much bigger than the runtime, that it probably doesn't need to be worried about.

It actually does happen a fair bit when I'm testing it in my 'SSP test world' haha. I would have thought the same as you :P/> If there is no way to make it shorter I guess i just cross my fingers and hope it doesnt happen :P/> Or move to a gps tracking system (which I don't think I will do haha)

Thanks
immibis #4
Posted 28 August 2013 - 04:48 AM
You could just accept that this is going to happen, and the position could be off by a few blocks.
Then when the turtle restarts, it can try to go back to (0,0). In reality it could be at say (2,1).
Then it gropes around for some landmark, like a redstone signal, a block, or a peripheral.
When it finds the landmark it resets its saved position and goes back to mining.

Or instead of looking for a landmark, you could use a short-range GPS system that would only have to cover the area near (0,0).
Ordinator #5
Posted 28 August 2013 - 05:53 AM
You could just accept that this is going to happen, and the position could be off by a few blocks.
Then when the turtle restarts, it can try to go back to (0,0). In reality it could be at say (2,1).
Then it gropes around for some landmark, like a redstone signal, a block, or a peripheral.
When it finds the landmark it resets its saved position and goes back to mining.


What if the turtle is quite far from 0,0 can we track the direction reliably? Are turtle.turnLeft() and turtle.turnRight() guaranteed to complete? If not, then we may also have our dierction of by 1 or 2 and when try to go back toward 0,0 we maybe headed 90 or 180 degrees the wrong way.
campicus #6
Posted 28 August 2013 - 06:58 AM
What if the turtle is quite far from 0,0 can we track the direction reliably? Are turtle.turnLeft() and turtle.turnRight() guaranteed to complete? If not, then we may also have our dierction of by 1 or 2 and when try to go back toward 0,0 we maybe headed 90 or 180 degrees the wrong way.

This is indeed the biggest problem, when you are mining an area 150 wide, you can have a turtle a fair way off where it is supposed to be haha

I think the only real solution is to use gps or some sort of landmark system,,, or for it to realise it is going the wrong way when it hits a wall (that shouldnt be there)
Ordinator #7
Posted 28 August 2013 - 08:19 AM
What if the turtle is quite far from 0,0 can we track the direction reliably? Are turtle.turnLeft() and turtle.turnRight() guaranteed to complete? If not, then we may also have our dierction of by 1 or 2 and when try to go back toward 0,0 we maybe headed 90 or 180 degrees the wrong way.

This is indeed the biggest problem, when you are mining an area 150 wide, you can have a turtle a fair way off where it is supposed to be haha

I think the only real solution is to use gps or some sort of landmark system,,, or for it to realise it is going the wrong way when it hits a wall (that shouldnt be there)

That's what I guessed. And true, to get position form GPS is easy.

But to get direction we'd have to take a gps reading, move, then take another gps reading then presumably, move back. And those moves might be blocked so you'd have to dig or path find. A complicated, intrusive and slow bit of code just to know direction.

But If we knew that turtle.turn() always succeeds the instant it is called, we could just save our direction prior to calling it. Or if we know it always fails if it is inturpted by game shutdown/chunk unload then, then we could save out direction just after calling it. So one line of code and a file write rather than many.
floppyjack #8
Posted 28 August 2013 - 03:32 PM
A turn always succeeds, just found that there:
http://www.computercraft.info/forums2/index.php?/topic/14961-turtleturn-and-chunk-unload/
campicus #9
Posted 28 August 2013 - 08:40 PM
But If we knew that turtle.turn() always succeeds the instant it is called, we could just save our direction prior to calling it. Or if we know it always fails if it is inturpted by game shutdown/chunk unload then, then we could save out direction just after calling it. So one line of code and a file write rather than many.

Ok so I have changed this (for example):


      turtle.turnLeft()
      facing = "W"
      trackPos()

to this:


      facing = "W"
      trackPos()
      turtle.turnLeft()

Perhaps I will need to rework my code to return a forwards coordinate before actually moving (as long as I know the turtle.forward will be true…). Maybe something like:


if turtle.detect() then
    dig()
else
    --figure out new x/y
    trackPos()
    turtle.forward()