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

API variables and program simplicity.

Started by PonyKuu, 29 September 2012 - 10:14 AM
PonyKuu #1
Posted 29 September 2012 - 12:14 PM
OK, this one more difficult, I think… basically, I have two questions.

This is the "navi" api - simple turtle navigation:
Spoiler

x = 0
y = 0
z = 0
xDir = 1
zDir = 0

function simpleForward ()
	if turtle.forward () then
		x = x + xDir
		z = z + zDir
		return true
	else
		return false
	end
end

function simpleUp ()
	if turtle.up () then
		y = y + 1
		return true
	else
		return false
	end
end

function simpleDown ()
	if turtle.down () then
		y = y - 1
		return true
	else
		return false
	end
end

function destructiveForward ()
	while not simpleForward () do
		if turtle.detect () then
			if not turtle.dig () then
				return false
			end
		else
			turtle.attack ()
		end
	end
	return true
end

function destructiveUp ()
	while not simpleUp () do
		if turtle.detectUp () then
			if not turtle.digUp () then
				return false
			end
		else
			turtle.attackUp ()
		end
	end
	return true
end

function destructiveDown ()
	while not simpleDown () do
		if turtle.detectDown () then
			if not turtle.digDown () then
				return false
			end
		else
			turtle.attackDown ()
		end
	end
	return true
end

up = simpleUp
down = simpleDown
forward = simpleForward

function setMode (mode)
	if mode == "simple" then
		up = simpleUp
		down = simpleDown
		forward = simpleForward
		return true
	elseif mode == "destructive" then
		up = destructiveUp
		down = destructiveDown
		forward = destructiveForward
		return true
	else
		return false
	end
end

function turnLeft ()
	turtle.turnLeft ()
	xDir, zDir = zDir, -xDir
end

function turnRight ()
	turtle.turnRight ()
	xDir, zDir = -zDir, xDir
end

function turnTo (xd, zd)
	if zd+zDir == 0 and xd+xDir == 0 then
		turnLeft ()
		turnLeft ()
	elseif xDir == -zd and zDir == xd then
		turnLeft ()
	elseif xDir == zd and zDir == -xd then
		turnRight ()
	end
end

function refuelSlot (amount, slot)
	if turtle.getFuelLevel () >= amount then
		return true
	end
	if turtle.getItemCount (slot) > 0 then
		turtle.select (slot)
		while turtle.getFuelLevel () < amount do
			if not turtle.refuel (1) then
				turtle.select (1)
				return false
			end
		end
		turtle.select (1)
		return true
	else
		turtle.select (1)
		return false
	end
end

function refuelAll (amount)
	for i = 1,16 do
		if refuelSlot (amount, i) then
			return true
		end
	end
	return false
end

function goTo (nx, ny, nz, nxd, nzd)
	if y < ny then
		print ("Going to y = "..ny)
		while y < ny do
			if not up () then
				print ("Cannot move up in current mode! Aborting...")
				return false
			end
		end
	end
  
	if x < nx then
		turnTo (1, 0)
		print ("Going to x = "..nx)
		while x < nx do
			if not forward () then
				print ("Cannot move forward in current mode! Aborting...")
				return false
			end
		end
	end
  
	if z < nz then
		turnTo (0, 1)
		print ("Going to z = "..nz)
		while z < nz do
			if not forward () then
				print ("Cannot move forward in current mode! Aborting!")
				return false
			end
		end
	end
  
	if z > nz then
		turnTo (0, -1)
		print ("Going to z = "..nz)
		while z > nz do
			if not forward () then
				print ("Cannot move forward in current mode! Aborting!")
				return false
			end
		end
	end

	if x > nx then
		turnTo (-1, 0)
		print ("Going to x = "..nx)
		while x > nx do
			if not forward () then
				print ("Cannot move forward in current mode! Aborting!")
				return false
			end
		end
	end

	if y > ny then
		print ("Going to y = "..ny)
		while y > ny do
			if not down () then
				print ("Cannot move down in current mode! Aborting!")
				return false
			end
		end
	end
	turnTo (nxd, nzd)
	return true
end
And I don't like it. That goTo function is ugly… I have written it that way because I wanted turtle to follow the same path if I have it move to one location an then move it back to the initial location. Is there any way to clean this code up a bit?

And the other one: I am trying to make an excavation program using that API. It seems like if I call navi.simpleForward (), it doesn't update the turtle coordinates. Am I doing wrong something?
Doyle3694 #2
Posted 29 September 2012 - 12:19 PM
z = z + zDir(0)…. That is your bug I think
PonyKuu #3
Posted 29 September 2012 - 12:23 PM
Are you sure? I can't find it… which line?
Doyle3694 #4
Posted 29 September 2012 - 12:32 PM
I mean, you declare zDir as 0
then when you call simpleForward() you say z = z + zDir
PonyKuu #5
Posted 29 September 2012 - 12:35 PM
Oh, yes, that's intended. But navi.x doesn't update too.
PonyKuu #6
Posted 29 September 2012 - 01:25 PM
Any ideas? I can write a function navi.getX() but it is even more uglier, I think…
I have done some experiments: all functions inside "navi" API are getting normal values, but if I try to read navi.x, for example, I always get 0. But navi.goTo handles x value correctly. It must be something with variable scopes or something similar to that, I just don't know what is it…
Doyle3694 #7
Posted 29 September 2012 - 01:28 PM
I don't know with global variables, but your program might have all local variables. what happends if you put "global" in front of all declarations?
PonyKuu #8
Posted 29 September 2012 - 01:31 PM
They are already global, since I haven't declared them as local.
Doyle3694 #9
Posted 29 September 2012 - 01:34 PM
k, don't know how global anc local works…