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

Moving Turtles? How to..

Started by Paullie, 26 February 2015 - 09:49 AM
Paullie #1
Posted 26 February 2015 - 10:49 AM
Hi I am trying to figure out how to constantly monitor the turtles movement like the X,Y,Z

I have added some code for you to have a look at and hopefully help me with


-- Turtle Monitoring Programme
--Monitoring X,Y,Z
local x = 0
local y = 0
local z = 0
local right = 0
local left = 0
while true do
if turtle.forward() == true then
  x = x + 1
  print("I have moved "..x.." block(s) away from home.")
end

if turtle.turnRight() == true then
  right = right + 1
end

if turtle.turnLeft() == true then
  left = left + 1
end

if turtle.up() == true then
  z = z + 1
end

if turtle.down() == true then
  z = z - 1
end

end
Bomb Bloke #2
Posted 26 February 2015 - 10:57 AM
Use one variable for direction - make it go up when the turtle turns right, and make it go down when the turtle turns left. If it gets higher than four, set it to one; if it goes lower than one, set it to four.

Depending on which number this variable is set to - possible values being 1 through to 4 - you can then determine whether making the turtle go forward should add one to x, subtract one from x, add one to y, or subtract one from y. Likewise when the turtle goes backwards.
Paullie #3
Posted 26 February 2015 - 01:12 PM
So something like this? i'm not very good with the ">" "<" signs or how to use them when coding


-- Turtle Monitoring Programme
--Monitoring Direction and Space
local dir = 1
-- 1 = Facing Front
-- 2 = Facing Right
-- 3 = Facing Back
-- 4 = Facing Left
while true do
if turtle.turnRight() == true then
  if dir <= 4 and >=1 then
   dir = dir + 1
  else
   dir = 1
  end --If
end --If

if turtle.turnLeft() == true then
  if dir <= 4 and >=1 then
   dir = dir - 1
  else
   dir = 4
  end --If
end --If
end --While


Thanks Paul
Bomb Bloke #4
Posted 26 February 2015 - 01:36 PM
Er, not quite. If you want to perform two comparisons against dir, then you need to explicitly list them as two comparisons:

if dir <= 4 and dir >= 1 then

Even then, you don't want to add one to dir if it's already 4 - you'd only want to do that if it's less than four. Ditch the "less or equal to" sign, and just use "less than". Whether dir is greater than one is also irrelevant to this check:

if dir < 4 then
  dir = dir + 1
  etc

Likewise when subtracting, all that matters is that you're not already down to one:

if dir > 1 then
  dir = dir - 1
  etc

Another thing - "if" statements already check to see if the condition you pass them resolves to true or false. Thus testing something like turtle.turnRight() == true is redundant, because you end up checking whether "turtle.turnRight() is true is true". "if turtle.turnRight() then" will suffice.
Paullie #5
Posted 27 February 2015 - 09:52 AM
So ive read through what you are trying to explain to me and i believe i have sorted it


--Monitoring Direction and Space
local dir = 1
-- 1 = Facing Front
-- 2 = Facing Right
-- 3 = Facing Back
-- 4 = Facing Left
while true do
if turtle.turnRight() then
  if dir < 4 and dir >=1 then
   dir = dir + 1
  else
   dir = 1
  end --If
end --If

if turtle.turnLeft() then
  if dir < 4 and >=1 then
   dir = dir - 1
  else
   dir = 4
  end --If
end --If
print(dir)

end --While
Bomb Bloke #6
Posted 27 February 2015 - 11:45 AM
Putting aside for the moment that those changes only bear a slight resemblance to what I was talking about, it strikes me that you might not be using this code as "an example of how you intend to rig your replacement movement functions", and that you're operating under the impression that each "if" block will automatically trigger whenever some other script tries to call the regular movement functions.

Just so we're on the same page, are you familiar with what functions are, how to define them, how to deal with their return values and how to make them return stuff?

If you're at all unsure, start reading.
Paullie #7
Posted 01 March 2015 - 09:10 PM
I just use functions to contain certain codes, which i could recall say if i wanted to create a turtle that farmed for me


local seed = 1 --Inventory slot the seeds are in
local bone = 2 --Inventory slot the bonemeal is in
local fuel = 3 --Inventory slot the fuel is in
if turtle.getFuelLevel() < 10 then --something like that
turtle.select(fuel)
turtle.refuel(2)
end
function tL()
turtle.turnLeft()
end
function tR()
turtle.turnRight()
end
function tA()
turtle.turnLeft()
turtle.turnLeft()
end
function placeDownS()
turtle.select(seed)
turtle.forward()
turtle.placeDown()
end
function placeDownB()
turtle.select(bone)
turtle.forward()
turtle.placeDown()
end
--Then I can call the functions here in the main script.

i dont know how to use them to the extent that you do but i wouldnt at the same time call myself an expert coder, if anything I am barely intermediate at coding
Bomb Bloke #8
Posted 02 March 2015 - 12:37 AM
That's closer to the sort of thing you'll be wanting for your tracking code. For example, you might expand that to:

function tL()
	while not turtle.turnLeft() do end  -- Call it until it works.
	
	dir = dir - 1
	if dir < 1 then dir = 4 end
end

function tR()
	while not turtle.turnRight() do end
	
	dir = dir + 1
	if dir > 4 then dir = 1 end
end

function tA()
	tL()
	tL()
end

See if you can figure out what mF() and mB() might look like. :)/>
Paullie #9
Posted 02 March 2015 - 09:29 AM
Looking at the code above I believe it would be

--Setting the Variable
local moves = 0 --Moves forward and backward
function mF()
  while not turtle.forward() do
    moves = moves + 1
  end
end
function mB()
  while not turtle.back() do
    moves = moves - 1
  end
end
mF()
mB()
thanks Bomb
Bomb Bloke #10
Posted 02 March 2015 - 10:59 PM
Not exactly - let's say mU() does this:

function mU()
	while not turtle.up() do end
	
	y = y + 1
end

What would mF() do with x and z, given the current value of dir?
Paullie #11
Posted 17 March 2015 - 01:09 AM
Getting there sorry for the very very late reply have been dotting about the UK lately.. so havent had much time till now


Got the code working as have just tested it on EthosLPPack haha

--Turtle Programme to
local h = 0
local x = 0
local y = 0
local dir = 1
-- if dir = 1 then its facing the way it was placed
-- if dir = 2 then its facing right from the right
-- if dir = 3 then its facing the opposite way from the way it was placed
-- if dir = 4 then its facing left from the way it was placed
function tL()
	    while not turtle.turnLeft() do end  -- Call it until it works.
	   
	    dir = dir - 1
	    if dir < 1 then dir = 4 end
  print(dir)
end
function tR()
	    while not turtle.turnRight() do end
	   
	    dir = dir + 1
	    if dir > 4 then dir = 1 end
  print(dir)
end
function mU()
  while not turtle.up() do end
 
  h = h + 1
  print(h)
end
function mD()
while not turtle.down() do end

h = h - 1
print(h)
end
function mF()
while not turtle.forward() do end

x = x + 1
print("x: "..x)
if dir == 2 then
  y = y + 1
  print("y: "..y)
elseif dir == 3 then
  x = x - 1
  print("x: "..x)
elseif dir == 4 then
  y = y - 1
  print("y: "..y)
end
end
Lyqyd #12
Posted 17 March 2015 - 04:43 AM
Your forward function isn't going to work correctly, written that way. You add one to x, then check the direction and make another change if it isn't the +x direction.

You might have better luck using something like my old location API, or at least the movement methods may provide some guidance on how to track the changes more effectively.
Bomb Bloke #13
Posted 17 March 2015 - 05:59 AM
It's pretty close to functional as it is, though - you're just forgetting to check for the situation where dir == 1 when going forward.