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

How to make a turtle do math to figure out how many blocks travelled?

Started by gamr101, 06 September 2012 - 12:20 AM
gamr101 #1
Posted 06 September 2012 - 02:20 AM
I was wondering how I make a turtle solve an equation so it can go back to my base when it's done shaft mining. I pretty much want to make it so it solves how many blocks it has travelled and then go back that many blocks to deposit the ore.
NIN3 #2
Posted 06 September 2012 - 02:38 AM
Depends on how your mining. By shaft mining, your thinking a straght, 2 by 1 hole that you can walk through? If so, then you can probably take what ever counter made you move forwards, and make another count you back. I would have to see code to give you any better advice….
Pharap #3
Posted 06 September 2012 - 03:17 AM
Assuming it is only travelling in a straight line, make it count each move it makes to a variable.
So have a variable called Cnt (Count of blocks travelled) or something, everytime it moves (let's say digging forward) do cnt = cnt + 1; then have something test if the inventory is full (or whatever the stop condition is), then make it come back, this time not counting and when it reaches where it started, have it deposit it's stuff, print(tostring(Cnt)) and then when you next see it it will tell you how far it got before returning.

If you want anything differing from this, be more specific on what you want with your reply.
D3matt #4
Posted 06 September 2012 - 06:37 AM
The simplest (imo) way to do this is to have custom move commands that keep track of the turtle's position in the x,y,z and then just have the turtle move until x, then y, then z all = 0.

Here's a really quick example of what I'd do for a custom move command:


function doForward()
  while turtle.forward() == false do
    os.sleep(0)
    turtle.dig()
  end
  xpos = xpos + 1
  return true
end
Your final program would have to account for turns to determine which way its facing and which variable to increment (or decrement), but hopefully this at least gives you come idea of what you want to do. Basically this code just keeps trying to move until it actually moves (in case there's a mob or a block like sand in the way), then add 1 to xpos (which won't work in this snippet because I didn't define xpos, but again, it's a starting point).