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

variable room coordinate saving

Started by valithor, 05 June 2013 - 12:38 PM
valithor #1
Posted 05 June 2013 - 02:38 PM
I am writing a program to store a mining turtles location and this is a by product of the program i am working on. To use the program place the turtle in the top right corner of the area you wish to be cleared, make sure there is not a file named "coord" if there is use "rm coord" before beginning. This is a unfinished script but works well not most efficiently but well for the task it is made to preform.

local x = 0
local y = 0
local z = 0
local direction = 0
local width = 0
local length = 0
local height = 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("-- 1 = x, 2 = z, 3 = y, 4 = direction")
	w.close()
  end
end

function readsave()
  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))
  r.close()
end

function save()
  s = fs.open("coord","w")
  s.writeLine(x)
  s.writeLine(z)
  s.writeLine(y)
  s.writeLine(direction)
  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
  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()
  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
height()

feed back is welcome.

while reading this script keep in mind i have no programming experience, and this is one of my first scripts.

I plan on adding automatic features to this program such as follows:
turtle return to startup location,
turtle resume if chunk is unloaded,
depositing any mined items to a chest,
making it more efficient by mining 3 lines at a time

pastebin link
http://pastebin.com/0F66Z2Ui
Edited on 05 June 2013 - 01:26 PM
superaxander #2
Posted 05 June 2013 - 02:38 PM
Please use pastebin