Jump to content




Keeping track of turtle location


  • You cannot reply to this topic
4 replies to this topic

#1 Zamithal

  • Members
  • 28 posts

Posted 24 December 2012 - 04:04 PM

Since there is no call to get ahold of turtles locations, my idea was to store it to a variable for x coods and z coords. ex:
direction = 1
turtleOneXCoord = 0
turtleOneZCoord = 0

while true do

direction = 1
turtleOneXCoord = 0
turtleOneXCoord = 0

while true do
-- some sort of input to go forward 1 square
if direction == 1 then
turtle.forward()
turtleOneXCoord = turtleOneXCoord + 1
elseif direction == 2 then
turtle.forward()
turtleOneZCoord = turtleOneZCoord + 1
elseif direction == 3 then
turtle.forward()
turtleOneXCoord = turtleOneXCoord - 1
elseif direction == 4 then
turtle.forward()
turtleOneZCoord = turtleOneZCoord - 1
end
--some sort of input to turn right
direction = direction + 1
--some sort of input to turn left
direction = direction - 1
end

with this, I can note down coordinates important to the turtle (like a restock chest)

However, the problem with this is if the program is terminated, and the turtle is not at (0,0), (0,0) is then moved and all reference coordinates would be thrown off... so a few questions...

1. Is this the best way to keep track of the turtle?
2. Can i save information without storing it to a file with the vault API?
3. If no, can someone explain how to use the vault API a little bit more thoroughly to me?

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 24 December 2012 - 04:41 PM

You may wish to look into my location API. You'll need to save location data to a file, however you do that. With my location API it's easy:

local loc = location.new()
handle = io.open("/.loc", "w")
if handle then
  handle:write(tostring(loc))
  handle:close()
end

local loc
handle = io.open("/.loc", "r")
if handle then
  local x, y, z, h = string.match(handle:read(), "(%-?%d+),(%d+),(%-?%d+),(%d)")
  loc = location.new(tonumber(x), tonumber(y), tonumber(z), tonumber(h))
  handle:close()
end


#3 Zamithal

  • Members
  • 28 posts

Posted 24 December 2012 - 06:25 PM

View PostLyqyd, on 24 December 2012 - 04:41 PM, said:

You may wish to look into my location API. You'll need to save location data to a file, however you do that. With my location API it's easy:

local loc = location.new()
handle = io.open("/.loc", "w")
if handle then
  handle:write(tostring(loc))
  handle:close()
end

local loc
handle = io.open("/.loc", "r")
if handle then
  local x, y, z, h = string.match(handle:read(), "(%-?%d+),(%d+),(%-?%d+),(%d)")
  loc = location.new(tonumber(x), tonumber(y), tonumber(z), tonumber(h))
  handle:close()
end
Can you explain this bit of code a bit further? I could be wrong but the first one opens the data stored, then you do the action, then use the second one to save correct? I'm not really sure I understand the local variable part though.
function readLocation(loc)
  local loc = location.new()
  handle = io.open("/.loc", "r")
  if handle then
	local x, y, z, h = string.match(handle:read(), "(%-?%d+),(%d+),(%-?%d+),(%d)")
	loc = location.new(tonumber(x), tonumber(y), tonumber(z), tonumber(h))
	handle:close()
  end
end
-----------------------------------------------------
function saveLocation(loc)
  handle = io.open("/.loc", "w")
  if handle then
	handle:write(tostring(loc))
	handle:close()
  end
end
readLocation()
loc:back()
saveLocation()
print(loc)
This is my code, it prints properly, however if I look at the .loc file... it tells me the value is nil. However, this code works, but id much rather it be a function
local loc = location.new()
handle = io.open("/.loc", "r")
if handle then
  local x, y, z, h = string.match(handle:read(), "(%-?%d+),(%d+),(%-?%d+),(%d)")
  loc = location.new(tonumber(x), tonumber(y), tonumber(z), tonumber(h))
  handle:close()
end
print (loc)
loc:forward()
handle = io.open("/.loc", "w")
if handle then
  handle:write(tostring(loc))
  handle:close()
end
print (loc)


#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 24 December 2012 - 06:49 PM

Well, the local loc = location.new should be elsewhere (this is all assuming you're using my location API, of course). Then you could have a savelocation function vaguely thus:

function saveLocation(saveLoc, path)
    local handle = io.open(path, "w")
    if handle then
      handle:write(tostring(saveLoc))
      handle:close()
    end
end

You'd call that function with the variable containing your location and the file path: saveLocation(loc, "/.loc")

And a loading function:

function loadLocation(path)
    local loadLoc
    handle = io.open(path, "r")
    if handle then
      local x, y, z, h = string.match(handle:read(), "(%-?%d+),(%d+),(%-?%d+),(%d)")
      loadLoc = location.new(tonumber(x), tonumber(y), tonumber(z), tonumber(h))
      handle:close()
    end
    return loadLoc
end

You'd call that with the path and use the return value for your location: local loc = loadLocation("/.loc")

Of course, for the location to remain accurate, you must move your turtle using the location API commands:

local loc = location.new() --initializes loc at 0,0,0.  You can change this by providing arguments.
loc:forward() --moves the turtle forward, changes the values stored in loc.
loc:up --etc. for the other movement commands, up, down, forward, back, turnLeft (loc:left) turnRight (loc:right)


#5 Zamithal

  • Members
  • 28 posts

Posted 24 December 2012 - 07:06 PM

Ok, it works now :D, but for the sake of learning, why does it work? I never declare the variable "loc", yet I am able to print it.. shouldn't it just return nil?

Oh, nevermind, I just noticed that you mentioned loc:forward() changes the value of loc

Edited by Zamithal, 24 December 2012 - 07:11 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users