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

[Lua] [Solved] Variable Room weird movements

Started by valithor, 05 June 2013 - 09:49 PM
valithor #1
Posted 05 June 2013 - 11:49 PM
So i created a short program to test out a couple of file sharing functions i have created. This program will dig a x amount of blocks in width, z in length, and y in height. While it is digging it will save all of its coords when moving with its starting point being 0, 0 ,0 facing 0 (north). The program begins in the top right corner of the area you wish to clear. My problem is somewhere in the banking command and after about a hour of trying to find the error i have given up completely stumped. What it does is it banks what it has then will return to the location it was before it banked. It then will get stuck going forward after it has returned to its position before banking. I can not locate why it does this. I get no error message. From what i can tell if i let it it would go until it went into a unloaded chunk.


code:
Spoiler
local x = 0
local y = 0
local z = 0
local direction = 0
local width = 0
local length = 0
local height = 0
local oldx = 0
local oldz = 0
local oldy = 0
local olddir = 0
function file()
  if not fs.exists("coord") then
	w = fs.open("coord","w")
	w.writeLine("0")
	w.writeLine("0")
	w.writeLine("0")
	w.writeLine("0")
	w.writeLine("0")
	w.writeLine("0")
	w.writeLine("0")
	w.writeLine("0")
	w.writeLine("-- 1 = x, 2 = z, 3 = y, 4 = direction")
	w.writeLine("-- 5 = oldx, 6 = oldz, 7 = oldy, 8 = olddir")
	w.close()
  end
end
function readsave()
  file()
  r = fs.open("coord","r")
  x = tonumber(r.readLine(1))
  z = tonumber(r.readLine(2))
  y = tonumber(r.readLine(3))
  direction = tonumber(r.readLine(4))
  oldx = tonumber(r.readLine(5))
  oldz = tonumber(r.readLine(6))
  oldy = tonumber(r.readLine(7))
  olddir = tonumber(r.readLine(8))
  r.close()
end
function save()
  s = fs.open("coord","w")
  s.writeLine(x)
  s.writeLine(z)
  s.writeLine(y)
  s.writeLine(direction)
  s.writeLine(oldx)
  s.writeLine(oldz)
  s.writeLine(oldy)
  s.writeLine(olddir)
  s.close()
end
function forward()
  if turtle.forward()==true then
	if direction==0 then
	  z = z+1
	  save()
	elseif direction==1 then
	  x = x-1
	  save()
	elseif direction==2 then
	  z = z-1
	  save()
	elseif direction==3 then
	  x = x+1
	  save()
	end
  else
	print("something is blocking my way")
	turtle.dig()
	turtle.attack()
	sleep(1)
	forward()
  end
end
function dig()
  turtle.dig()
  forward()
end
function down()
  if turtle.down()==true then
	y = y-1
	save()
  else
	while turtle.down()==false do
	  turtle.attack()
	  turtle.digDown()
	end
	y = y-1
	save()
  end
end
function up()
  if turtle.up()==true then
	y = y+1
	save()
  else
	while turtle.up()==false do
	  turtle.attack()
	  turtle.digUp()
	end
	y = y+1
	save()
  end
end
function Left()
  if turtle.turnLeft()==true then
	direction = direction - 1
	direction = direction % 4
	save()
  end
end
function Right()
  if turtle.turnRight()==true then
	direction = direction + 1
	direction = direction % 4
	save()
  end
end
file()
readsave()
save()
print("enter desired width:")
wid = io.read()
print("enter desired length:")
len = io.read()
print("enter desired height:")
high = io.read()
function length()
  for i = 2, len do
	dig()
	bank()
  end
  Left()
  Left()
  for i = 2, len do
	dig()
  end
  Right()
  Right()
end
function width()
  for j = 2, wid do
	Left()
	dig()
	Right()
	length()
  end
end
function height()
  for m = 2, high do
	length()
	width()
	Right()
	for i = 1, x do
	  dig()
	end
  Left()
  turtle.digDown()
  down()
  end
  length()
  width()
end
function face0()
  readsave()
	while direction~=0 do
	  Right()
	  readsave()
	  print(direction)
	end
  print("facing direction 0")
end
function face1()
  readsave()
	while direction~=1 do
	  Right()
	  readsave()
	  print(direction)
	end
  print("facing direction 1")
end
function face2()
  readsave()
	while direction~=2 do
	  Right()
	  readsave()
	  print(direction)
	end
  print("facing direction 2")
end
function face3()
  readsave()
	while direction~=3 do
	  Right()
	  readsave()
	  print(direction)
	end
  print("facing direction 3")
end
function xto0()
  readsave()
	if x>0 then
	  face1()
	  while x~=0 do
		forward()
		readsave()
		print("current cords".."("..x..","..z..")")
	  end
	face0()
	print("arrived at 0 x coord")
	elseif x<0 then
	  face3()
	  while x~=0 do
		forward()
		readsave()
		print("current cords".."("..x..","..z..")")
	  end
	face0()
	print("arrived at 0 x coord")
	elseif x==0 then
	  face0()
	  print("turtle already at x=0")
	end
end
function zto0()
  readsave()
	if z>0 then
	  face2()
	  while z~=0 do
		forward()
		readsave()
		print("current cords".."("..x..","..z..")")
	  end
	face0()
	print("arrived at z=0 coord")
	elseif z<0 then
	  face0()
	  while z~=0 do
		forward()
		readsave()
		print("current cords".."("..x..","..z..")")
	  end
	face0()
	print("arrived at z=0 coord")
	elseif z==0 then
	  face0()
	  print("turtle already at z=0")
	end
end
function yto0()
  readsave()
	if y>0 then
	  while y~=0 do
		down()
		readsave()
		print("y ="..y)
	  end
	print("arrived at y=0")
	elseif y<0 then
	  while y~=0 do
		up()
		readsave()
		print("y ="..y)
	  end
	print("arrived at y = 0")
	elseif y==0 then
	  print("arived at y = 0")
	end
end
function bank()
  readsave()
	if turtle.getItemCount(16)>=1 then
	  oldx = x
	  oldz = z
	  oldy = y
	  olddir = direction
	  save()
	  print(oldx..oldz..oldy)
	  print(x..z..y)
	  xto0()
	  zto0()
	  yto0()
	  face2()
	  for banker = 1, 16 do
		turtle.select(banker)
		turtle.drop()
	  end
	  face0()
	end
  backoldx()
  backoldz()
  backoldy()
  backolddir()
  turtle.select(1)
end
function backoldx()
readsave()
  if oldx>0 then
	face3()
	while x~=oldx do
	  forward()
	  readsave()
	end
  elseif oldx<0 then
	face1()
	while x~=oldx do
	  forward()
	  readsave()
	end
  end
end
function backoldz()
readsave()
  if oldz>0 then
	face0()
	while z~=oldz do
	  forward()
	  readsave()
	end
  elseif oldz<0 then
	face2()
	while z~=oldz do
	  forward()
	  readsave()
	end
  end
end
function backoldy()
readsave()
  if oldy>0 then
	while y~=oldy do
	  up()
	  readsave()
	end
  elseif oldy<0 then
	while y~=oldy do
	  down()
	  readsave()
	end
  end
end
function backolddir()
readsave()
  while direction~=olddir do
	Left()
  end
end
height()
xto0()
zto0()
yto0()

http://pastebin.com/a1ci8vVP

Thank you for your time
Edited on 05 June 2013 - 11:47 PM
Bomb Bloke #2
Posted 06 June 2013 - 12:04 AM
In eg your "backoldx()" function (line 330), you have a while block that looks like this:

    while x~=oldx do
      forward()
      readsave()
    end

Go take a look at what the "readsave()" function is doing and you'll see why the turtle never stops. A quick guess tells me you wanted to call "save()" instead.

This is not the only spot you appear to've made this error.
valithor #3
Posted 06 June 2013 - 12:26 AM
From what i can tell the only part of the back functions that don't work is the backoldz() function. Both of the other functions return the turtle to the correct coordinates and are basically exact copies of each other

I tested the turtle again to return accurate information

The coordinate folder that the program makes starts out as
Spoiler
0.0		-- x coord
0.0		-- z coord
0.0		-- y coord
0.0		-- direction facing
0.0		-- oldx
0.0		-- oldz
0.0		-- oldy
0.0		-- olddirection


What is turns out as when i speed up the banking function is
Spoiler
3.0		-- x coord  -- this one is correct
13.0		-- z coord   -- this one is wrong
0.0		-- y coord
0.0		-- direction facing
3.0		-- oldx
1.0		-- oldz
0.0		-- oldy
0.0		-- olddirection

in every test i have the only back command that messes up is the backoldz() function

The fact that only the one function messes up is what confuses me
valithor #4
Posted 06 June 2013 - 01:05 AM
I have found the problem after some more testing with the script the actual error is coming from this function
Spoiler
function forward()
  if turtle.forward()==true then
	    if direction==0 then
		  z = z+1
		  save()
	    elseif direction==1 then
		  x = x-1
		  save()
	    elseif direction==2 then
		  z = z-1
		  save()
	    elseif direction==3 then
		  x = x+1
		  save()
	    end
  else
	    print("something is blocking my way")
	    turtle.dig()
	    turtle.attack()
	    sleep(1)
	    forward()
  end
end

The banking function will finish then what happens is the forward function repeats itself i have slightly edited it in a attempt to remove this error but it has not worked

here is the edited code
Spoiler

function forward()
  if turtle.forward()==true then
    if direction==0 then
      z = z+1
      save()
    elseif direction==1 then
      x = x-1
      save()
    elseif direction==2 then
      z = z-1
      save()
    elseif direction==3 then
      x = x+1
      save()
    end
  else
    print("something is blocking my way")
    turtle.dig()
    turtle.attack()
    if forward()==false then
      os.sleep(1)
      forward()
    end
  end
end


This did not work will continue to try but a help is appreciated
valithor #5
Posted 06 June 2013 - 01:47 AM
I finally found the problem after about another 2 hours of messing with it
Spoiler


function bank()
  readsave()
    if turtle.getItemCount(16)>=1 then
      oldx = x
      oldz = z
      oldy = y
      olddir = direction
      save()
      print(oldx..oldz..oldy)
      print(x..z..y)
      xto0()
      zto0()
      yto0()
      face2()
      for banker = 1, 16 do
        turtle.select(banker)
        turtle.drop()
      end
   end
      face0()
  backoldy()
  backoldx()
  backoldz()
  backolddir()
  turtle.select(1)
end

on that part of code i ended the if loop to early it should have ended after the back functions

Spoiler
function bank()
  readsave()
    if turtle.getItemCount(16)>=1 then
	  oldx = x
	  oldz = z
	  oldy = y
	  olddir = direction
	  save()
	  print(oldx..oldz..oldy)
	  print(x..z..y)
	  xto0()
	  zto0()
	  yto0()
	  face2()
	  for banker = 1, 16 do
	    turtle.select(banker)
	    turtle.drop()
	  end
	  face0()
  backoldy()
  backoldx()
  backoldz()
  backolddir()
  turtle.select(1)
  end
end