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

turtle doing strange things

Started by ackley14, 24 February 2014 - 07:09 AM
ackley14 #1
Posted 24 February 2014 - 08:09 AM
so im writing a program with the intention of having a turtle dig a stair well down or up (user specifies) and then how many down or up to actually go . i have certain failure prevention functions to avoid the turtle either running out of inventory space, or hitting a gravity block like gravel. those functions are contained within the digcheck, digcheckup, inventory up, and go back commands. now my problem is that if it dosn't have a chest and decides to run the goback function, instead it simply digs a 3x1xinfinity tunnel in whatever direction the staircase was headed. for example, it fills up when you set it to make a staircase down, now it has a staircase leading to a pit to bedrock, or if you said for it to go up, its stuck at the highest point in the world by the end. at this point im absolutely baffled as to why that is. if someone could take a look. what the go back command SHOULD do is, send the turtle back as many steps as its currently gone forward, the variable curr, simply adds 1 every time the stairdown or up commands loop, that way the go back command knows how far into the task the turtle is and can go back the appropriate amount of spaces. however its simply not doing that.

tArgs={...}
if #tArgs<2 then
print("Stairs <number of stairs> <up or down>")
end
x = tArgs[1]
ud = tArgs[2]

function goBack()
turtle.turnRight()
turtle.turnRight()
if ud == down then
  for i = 1, curr do
  print(curr)
  turtle.forward()
  turtle.up()
  end
elseif ud == up then
print(curr)
  for i = 1, curr do
  turtle.down()
  turtle.forward()
  end
end
turtle.forward()
end
function digcheck()
local a = turtle.detect()
while a == true do
turtle.dig()
a = turtle.detect()
end
end
function digCheckUp()
local b = turtle.detectUp()
while b == true do
turtle.digUp()
b = turtle.detectUp()
end
end
function stairDown(x)
curr = 0
for i = 1,x do
  curr = curr + 1
  digcheck()
  turtle.digDown()
  turtle.down()
  digcheck()
  turtle.forward()
  inventoryOk()
end
turtle.turnRight()
turtle.turnRight()
for i = 1,x do
  turtle.forward()
  turtle.up()
end
turtle.forward()
end
function stairUp(x)
curr = 0
for i = 1,x do
  curr = curr+1
  digcheck()
  turtle.forward()
  digCheckUp()
  turtle.up()
  digCheckUp()
  inventoryOk()
end
turtle.turnRight()
turtle.turnRight()
for i = 1,x do
  turtle.down()
  turtle.forward()
end
turtle.forward()
end
function inventoryOk()
local inv = turtle.getItemCount(15)
if inv >= 1 then
  invDump()
end
end
function invDump()
local chest = turtle.getItemCount(16)
if chest >= 1 then
  turtle.select(16)
  digCheckUp()
  turtle.placeUp()
for i = 1 , 15 do
  turtle.select(i)
  turtle.dropUp()
end
  turtle.select(16)
  turtle.digUp()
  turtle.select(1)
else
  goBack()
  term.clear()
  term.setCursorPos(1,1)
  print("no more space master")
  return
end
end

if ud == "up" then
stairUp(x)
elseif ud=="down" then
stairDown(x)
end

at this point im so stuck, i started lua friday of last week so im obviously missing something important here. a helpful response will be so so appreciated. thanks again to anybody willing to take a look. also please, try it out for yourselves. see what results you get, its very odd D:

edit- wrong code was put in (earlier version) -most recent version input (same problems)
Edited on 24 February 2014 - 07:11 AM
Bomb Bloke #2
Posted 24 February 2014 - 05:28 PM
Arguments passed to the script come in as strings; you likely want "x = tonumber(tArgs[1])" up the top there instead of just "x = tArgs[1]".

"while" loops, "if" checks etc already look to see if a given statement resolves as "true". Hence you don't need to write code such as "while a == true do" - just "while a do" would suffice.

Though really chunks such as:

function digcheck()
	local a = turtle.detect()
	while a == true do
		turtle.dig()
		a = turtle.detect()
	end
end

Could just be trimmed to:

function digcheck()
	while turtle.detect() do
		turtle.dig()
	end
end

Once the turtle has "gone back", the invDump() function "returns", giving control back to the inventoryOk() function which called it. That then finishes up and gives control to either stairDown() or stairUp(), which tries to continue the staircase, notices that the inventory is still full, tries to go back again… etc.

Instead of:

		print("no more space master")
		return

Try:

		error("no more space master")

This actually halts the script with a message explaining why.