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

[help][lua-API][not solved] my "T" API, Goto("ChargeCheck") is not working!

Started by Goof, 08 February 2013 - 09:35 AM
Goof #1
Posted 08 February 2013 - 10:35 AM
Hello.. im a bit lazy now, and i dont have that much time so, just read the pastebin code ( all informations is showed there.. )


quick explanation:

when typing "goto("ChargeCheck")" it finely moves to that position it had to, but it seems like after the "goto1" function, which moves to the Chordinates, is just, going all around the IF statement.. why.. i dont see any returning scripts in the "goto1" function

Please help!

Pastebin

Thanks in advance:D
Goof #2
Posted 09 February 2013 - 03:00 AM
No help?

please. I really need help to get this api working!

thanks.
remiX #3
Posted 09 February 2013 - 02:34 PM
How are you calling the whole program?

Is your goto function supposed to be so big? (300+ lines)

Spoiler
goto = function( ... )
	term.clear() -- clear screen
	term.setCursorPos(1,1)
	-- Variables --
	digIsPossible = true
	runUp = 0
	runDown = 0
	args = { ... } -- get the arguments for the chordinates.

	Facing = { -- just quick info. --
	"North South East West",
	"  2     0    3    1"
	}

	-- Waypoints --
	Waypoint = { -- the ChargeCheck waypoint is working fine. (all in this table is actually working fine.)
		["Home"] = {
			["sPos"] = {wX = 801, wY = 68, wZ = -154},
			["ePos"] = {wX = 801, wY = 63, wZ = -154}
		},
		["ChargePad1"] = {
			["sPos"] = {wX = -290, wY = 80, wZ = 362},
			["ePos"] = {wX = -290, wY = 77, wZ = 362}
		},
		["ChargePad2"] = {
			["sPos"] = {wX = -289, wY = 80, wZ = 362},
			["ePos"] = {wX = -289, wY = 77, wZ = 362}
		},
		["ChargePad3"] = {
			["sPos"] = {wX = -289, wY = 80, wZ = 363},
			["ePos"] = {wX = -289, wY = 77, wZ = 363}
		},
		["ChargePad4"] = {
			["sPos"] = {wX = -290, wY = 80, wZ = 363},
			["ePos"] = {wX = -290, wY = 77, wZ = 363}
		},
		["ChargeCheck"] = {
			["sPos"] = {wX = -290, wY = 80, wZ = 362},
			["ePos"] = {wX = -290, wY = 78, wZ = 362}
		}
	}

	--[[ Call
	xPos = Waypoint.Home.sPos.wX
	--]]

	printUsage = function() --Prints an usage, if choordinates, or input is defined wrong...
		print("Usage: [X] [Y] [Z]")
		print("\n"..Facing[1])
		print(Facing[2])
	end

	refuel = function()
		while turtle.getFuelLevel() < 100 do
		term.setCursorPos(1,2)
		term.clearLine()
		term.setCursorPos(1,2)
		print("Need fuel")
			for itemSlots = 1, 16 do
				turtle.select(itemSlots)
				if turtle.refuel(1) then itemSlots = 16 end
			end
		sleep(0)
		end
		term.setCursorPos(1,2)
		term.clearLine()
		term.setCursorPos(1,2)
		print("Refueled.")
	end

	up = function()
	refuel()
		while not turtle.up() do
			if digIsPossible then
					if not turtle.digUp() then turtle.attackUp() end
			end
		end	
	end

	down = function()
	refuel()
		while not turtle.down() do
			if digIsPossible then
					if not turtle.digDown() then turtle.attackDown() end
			end
		end	
	end

	fd = function()
	refuel()	
		while not turtle.forward() do
			if digIsPossible then
				if not turtle.dig() then turtle.attack() end
			else
				refuel()
				if up() then 
					runUp = runUp + 1
				else down()			
					runDown = runDown + 1
				end
			end	
		end
		while runUp > 0 do
			refuel()
			down()
			runUp = runUp - 1
		end	
		while runDown > 0 do
			refuel()
			up()
			runDown = runDown - 1
		end		
	end

	bk = function()
	refuel()	
		while not turtle.back() do
			if digIsPossible then
				turtle.turnRight()
				turtle.turnRight()
				if not turtle.dig() then turtle.attack() end
				turtle.turnRight()
				turtle.turnRight()
			end	
		end
	end

	getLoc = function(_nWrit)
		rednet.open("right")
		x,y,z = gps.locate(10)
		term.setCursorPos(1,1)
		if not _nWrit then
			print(x.." , "..y.." , "..z)
		end
	end

	getDir = function()
		getLoc()
		curx, cury, curz = x, y, z
		fd()
		getLoc()
		if curz > z then 
			dir = 0 
			dirN = "South"
		elseif curz < z then
			dir = 2
			dirN = "North"
		elseif curx < x then
			dir = 1
			dirN = "West"
		elseif curx > x then
			dir = 3
			dirN = "East"
		end
		term.setCursorPos(1,2)
		term.clearLine()
		print(dir.." , "..dirN)
		bk()
		getLoc()
	end

	turn = function(d)
		getDir()
		term.setCursorPos(1,2)
		term.clearLine()
		term.setCursorPos(1,3)
		term.clearLine()
		term.setCursorPos(1,2)
		print("Finding direction...")
		print(dirN.." , "..d)
		while dir ~= d do
			turtle.turnRight()
			dir = dir + 1
			if dir == 4 then dir = 0 end
		end
	end

	goto1 = function(gox,goy,goz) -- in this function it looks like it just 
		Xmove = function()
			term.setCursorPos(1,2)
			term.clearLine()
			term.setCursorPos(1,3)
			term.clearLine()
			term.setCursorPos(1,2)
			print("Moving in the X Axis.")
			getLoc()
			if gox > x then
				turn(1)
				for i = x, gox-1 do
					fd()
					x = x + 1
				end
			elseif gox < x then
				turn(3)
				for i = gox+1, x do
					fd()
					x = x - 1
				end
			end
		end
		Zmove = function()
			term.setCursorPos(1,2)
			term.clearLine()
			term.setCursorPos(1,3)
			term.clearLine()
			term.setCursorPos(1,2)
			print("Moving in the Z Axis.")
			getLoc()
			if goz > z then
				turn(2)
				for j = z, goz-1 do
					fd()
					z = z + 1			
				end
			elseif goz < z then
				turn(0)
				for j = goz+1, z do
					fd()
					z = z - 1
				end
			end	
		end
		Ymove = function()
			term.setCursorPos(1,2)
			term.clearLine()
			term.setCursorPos(1,3)
			term.clearLine()
			term.setCursorPos(1,2)
			print("Moving in the Y Axis.")
			getLoc()
			if goy > y then
				for k = y, goy-1 do
					up()
					y = y + 1
				end
			elseif goy < y then
				for k = goy+1, y do
					down()
					y = y - 1
				end		
			end
		end
		getLoc()
		if goy > y then
			Ymove()
			Xmove()
			Zmove()
		else
			Xmove()
			Zmove()
			Ymove()
		end
	end
	gpsRead = function()
		try = 0
		tArg = args
		arg = #args	
		if arg == 3 then
			getLoc()
			goto1(tonumber(tArg[1]), tonumber(tArg[2]), tonumber(tArg[3]))
		elseif arg == 1 and tArg[1] ~= "ChargeCheck" then
			dest = Waypoint[tArg[1]].sPos
			endDest = Waypoint[tArg[1]].ePos
			goto1(dest.wX, dest.wY, dest.wZ)
			goto1(endDest.wX, endDest.wY, endDest.wZ)
		elseif arg == 1 and tArg[1] == "ChargeCheck" then
			dest = Waypoint["ChargeCheck"].sPos
			endDest = Waypoint["ChargeCheck"].ePos
			goto1(dest.wX, dest.wY, dest.wZ)
			goto1(endDest.wX, endDest.wY, endDest.wZ) -- here it quits? its just, like going around this if statement. how and why?
			if turtle.detectDown() then -- it Does NOT reach this part.. i dont know why.. i dont see any "return false" or "return" in the goto1 function..
				fd(1)
				if turtle.detectDown() then
					rt(1)
					fd(1)
					if turtle.detectDown() then
						rt(1)
						fd(1)
						if not turtle.detectDown() then
							goto("ChargePad4")
						end	
					elseif not turtle.detectDown() then		
						goto("ChargePad3")
					end
				elseif not turtle.detectDown() then
					goto("ChargePad2")
				end
			elseif not turtle.detectDown() then
				goto("ChargePad1")
			end
		else
			printUsage()
			return false
		end
	end
	-- main --
	rednet.open("right")
	term.clear()
	term.setCursorPos(1,1)
	gpsRead()
	term.clear()
	term.setCursorPos(1,1)
	getLoc(1)
	print("i moved to: ") -- this is what it returns, after the :
	-- goto1(dest.wX, dest.wY, dest.wZ)
	-- goto1(endDest.wX, endDest.wY, endDest.wZ)
	-- and thats not good :(/>
	print("X: "..x.." Y: "..y.." Z: "..z)
end

Are there any functions you can maybe move out of it so it's easier to read o_O
Goof #4
Posted 09 February 2013 - 08:43 PM
Well i can try moving the move ( fd() rt() ) etc. But then the code would still be long.
But do you know why the goto1() is going around the if statements?

because i dont understand why it doesnt go into the if statements. Thats maybe a bug?

thanks :D/>
remiX #5
Posted 10 February 2013 - 12:12 AM
Add some debug lines?

Just before calling the function that 'quits'
print(endDest.wX .. " " .. endDest.wY .. " " .. endDest.wZ)

Maybe change your getLoc function to return the values


getLoc = function(_nWrit)
    rednet.open("right")
    local mx, my, mz = gps.locate(10)
    term.setCursorPos(1,1)
    if not _nWrit then
        print(x.." , "..y.." , "..z)
    end
    if not mx or not my or not mz then error("Failed to receive co-ordinates") end -- failsafe
    return mx, my, mz -- returns the values
end

call like this:
x, y, z = getLoc()

I honestly don't know what the problem is o_O
Goof #6
Posted 10 February 2013 - 01:07 AM
I will try. I really Hope that it will work.
Goof #7
Posted 10 February 2013 - 02:56 AM
Remix, if i could, i would give you 100 + ups…

you solved my issue!!!

when removing all extra functions which was another place in the api, everything worked! You Are the best!

:D/>/>
:)/>/>
;)/>
:D/>
B)/>

thanks!
remiX #8
Posted 10 February 2013 - 03:19 AM
Nice :P/>

Never knew my solution would completely fix the issue, haha. But that's good :P/> Enjoy!
Goof #9
Posted 10 February 2013 - 04:15 AM
well. i actually got an error. (rednet error) but that was just my GPS system that was totally sending wrong info to the turtle xD

BUT im having an extra question…
i am making a fuel checker, which constant checks the fuel level until X seconds has quit. (ehm quit? i dunno what to say there)

in console i type in:


>lua
Lua bla. bla. bla.
Lua> t.cFuel(10)
this is what i should do. then it returns this:

Fuel level is: (fuel level)
(seconds) seconds left.
but.. i dont want to add an extra "or i == 10" etc.. so i want to make a table.

a table which takes the entered seconds, and defining them in 10, 20 30, 40, 50 etc… (10 = 1 sec. (10 ticks = 1 sec(im not totally sure xD dont cut me down on that.))
like this:



function cFuel(_nTime)
_nSecTimers = { ["time"] = {_nTime/_nTime = (the seconds... like if i say seconds == 10 then it will say that the _nTime/_nTime is equal to 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 )} }

for k,v in ipars(_nSecTimers) do .... etc.


end
end

Do you have an idea for that?

Current code:

cFuel = function(_nTime)
	print("Checking fuel level for ".._nTime.." seconds.")
	sleep(1.5)
	_sCheck = true
	_nSec = _nTime
	_tTime = _nTime*10
	_mTime = {}
	term.clear()
	
	for i = 1, _tTime do
		_tFuel = turtle.getFuelLevel()
		term.setCursorPos(1,1)
		term.clearLine()
		print("Fuel Level: ".._tFuel)
		print(_nSec.." seconds left!")
			if i == 10 or i == 20 or i == 30 or i == 40 or i == 50 or i == 60 then -- here i did have to enter all the numbers, from 10 to _nTime * 10. so that would not be nice, if i entered the _nTime to be 1000 seconds :o/>/>
				_nSec = _nSec - 1
			end
		sleep(0.1)
	end
	
	term.setCursorPos(1,2)
	print("Done! Fuel Level is now: ".._tFuel)
	return true
end

Thanks :D/>
remiX #10
Posted 10 February 2013 - 04:43 AM
Hmm, I don't really understand.
This is what I understand (?):
So you basically want it to check the fuel to X amount of seconds, displaying the amount of fuel it has?

But each second it prints it, the fuel level is the same all the time, so I don't understand what you're trying to get out of this o_O
Goof #11
Posted 10 February 2013 - 05:10 AM
Oh… xD.. no it doesnt have the same fuel all the time ( only if you dont use the "ChargePads" ) (which is in the miscPeripherals addon)

its because in my Miscperipherals i am making the turtle to goto the chargepad, system, which constantly charges all turtles on top of em. so thats why im doing this, so i can see how fast im collecting fuel.. ( and see what to have xD )

and for this :

"So you basically want it to check the fuel to X amount of seconds, displaying the amount of fuel it has?" Yes.


sorry if i you didnt know the miscperipherals i had installed too.

But now you should know that. i hope you can help me, now :D/>
remiX #12
Posted 10 February 2013 - 05:57 AM
Oh okay, I've never used the miscPeripherals addon before…

Hmm, I'm not sure what you were trying to do but try this


local function cFuel(_nTime)
	term.clear()
	term.setCursorPos(1, 1)
	
	_nTime = _nTime or 10 -- if you don't input a value, then it will default to 10 seconds
	
	print("Checking fuel for a total of " .. _nTime .. " seconds."_
	
	for i = _nTime, 1, -1 do -- starts at the max seconds and count's down (-1 is the step)
		term.setCursorPos(1, 2)
		term.clearLine()
		print("Fuel level: " .. turtle.getFuelLevel())
		term.clearLine()
		print( (#tostring(i) == 1 and "0" or "") .. i .. " " .. (#tostring(i) == 1 and "second" or "seconds") .. " left!") -- adds a leading zero if the time is in the tens, if i is 1, types second or seconds.
		sleep(1) -- sleep for a second
	end
	
	term.clear() term.setCursorPos(1, 1)
	print("Done!\nFuel level is now: " .. turtle.getFuelLevel())
end

It's not necessary to return true/false if you're not checking if it does or doesn't.
Goof #13
Posted 10 February 2013 - 06:06 AM
Well.. that script/code only checks the fuel level every 1 second.. i want it to check it every 0.1 second… so the fuel level will take about 10 fuels until the second changes.. maybe a parallel?
wait.. i think i will try to post a code later, that uses the parallel or anything…
thanks :D/>


but do you know a way to detect if _nTime is in the tenth of the number?
remiX #14
Posted 10 February 2013 - 06:11 AM
Why would you want it to check every 0.1 seconds o_O

Then change the loop to this:
for i = _nTime*10, 1, -1 do -- starts at the max seconds and count's down (-1 is the step)
    term.setCursorPos(1, 2)
    term.clearLine()
    print("Fuel level: " .. turtle.getFuelLevel())
    term.clearLine()
    print( (#tostring(i) == 1 and "0" or "") .. i .. " " .. (#tostring(i) == 1 and "second" or "seconds") .. " left!") -- adds a leading zero if the time is in the tens, if i is 1, types second or seconds.
    sleep(0.1) -- sleep for a second
end
Goof #15
Posted 10 February 2013 - 06:12 AM
edit: but do you know a way to know when a specific number is in the 10, 20, 30, 40, 50 state?

like only when i == 10 or 20 or 30 or 40 or 50. then do something… ( but only with one code. ( not just to type 10, 20, 30, 40, 50, 60, 70, 80, 90, 100) ) ?
Goof #16
Posted 10 February 2013 - 06:29 AM
Never mind.. figured it out.. :



cFuel = function( _nTime )
	term.clear()
	term.setCursorPos(1,1)
	
	local _tNum = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300}
	
	_nTime = _nTime or 10
	_sTime = _nTime
	print("Checking... ")
	sleep(1)
	function printTime()
		term.setCursorPos(1,2)
		term.clearLine()
		print(_sTime.." Seconds left!")
	end
	function main()
		printTime()
		for i = 1, _nTime*10 do
			term.setCursorPos(1,1)
			term.clearLine()
			print("Fuel Level: "..math.random(1,9))
			for k, v in pairs(_tNum) do
				if i == v then
					_sTime = _sTime - 1
					printTime()
				end
			end
			sleep(0.1)
		end
	end
	
	main()
	
	
end

remiX #17
Posted 10 February 2013 - 07:07 AM
I still have no clue but what you're doing like "print("Fuel Level: "..math.random(1,9))" but anyway, what you WERE looking for was Modulus xD

Modulus is used to check if X is divisible by Y:
(not sure you will put this so here is an example

_nTime = 30 -- 30 seconds

if _nTime % 10 == 0 then
    -- What it does:
    -- If _nTime / 10 returns NO remainders, then do this (therefore it is divisible.
end
Goof #18
Posted 10 February 2013 - 09:35 AM
ahh,.. Thank You :D/>

that would work :D/>

Thanks :D/>