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

[Lua] Program is not looping

Started by Praetor22, 27 June 2013 - 08:16 AM
Praetor22 #1
Posted 27 June 2013 - 10:16 AM
Here is the functioning part of the code. I have no idea why it is not looping, it seems to me like it should but it does not. Any help would be appreciated.


if not (bm == y) or (bm == n) then
	term.clear()
	write("would you like to enable bonemeal refueling? (y/n): ")
	local bm = read()
	chop = 1
else
end
if chop == 1 then
	while true do
		if fe1 == 0 then
			fe = turtle.getFuelLevel()
		else
			end
		sleep(0)
		if turtle.getItemCount(1) < 2 then
			fg()
		else
			end
		sleep(0)
		if turtle.getFuelLevel() < 60 then
			rf()
		else
			end
		sleep(0)
		if (bm == y) and (turtle.getItemCount(16) < 1) then
			rb()
		else
			end
		sleep(0)
		if (z == 0) and (x == 0) and (finished == 0) and turtle.detect() then
			df()
			end
		while (finished == 0) and (z == 1) and turtle.detectUp() do
			du()
			end
		while (finished == 0) and (z == 1) and (x > 0) and not turtle.detectUp() and not turtle.detectDown() do
			turtle.down()
			end
		if (finished == 0) and (z == 1) and (x > 0) and not turtle.detectUp() and turtle.detectDown() then
			ft()
			end
		end
	end

If you think it might be the functions causing the problems, though I think they are fine, here they are.

Spoiler

local function rf()
	turtle.select(1)
	turtle.refuel(1)
	f = f + 1
	end
local function rb()
	turtle.select(16)
	turtle.suckUp(5)
	end

local function fg()
	turtle.select(1)
	turtle.turnLeft()
	turtle.suck(31)
		if false then
			term.clear()
			print("no charcoal available in fuel chest")
		else
			end
	turtle.turnRight()
	end
local function df()
	turtle.select(2)
	turtle.dig()
		if true then
			turtle.forward()
			if true then
				z = (z+1)
				end
			end
		end
local function du()
	turtle.digUp()
		if true then
			turtle.up()
			if true then
				x = (x+1)
				end
			end
		end
local function rn()
	turtle.back()
		if true then
			z = (z-1)
				if true then
					finished = 1
					end
				end
			end
local function w()
	while w == 1 do
		turtle.select(15)
		sleep(60)
			if not turtle.compare() then
				w = 0
				end
			end
		end
local function ft()
	rn()
	t = t + 1
	term.clear()
	print("".. t .. " tree(s) removed")
	tw = (tw + turtle.getItemCount(2))
	print("" .. tw .. " blocks of wood collected")
	print("" .. f .. " piece(s) of coal used")
	-- efficiency math
	ie = ((((tw/5)/5)*80)+ turtle.getFuelLevel())/(fe1 - turtle.getFuelLevel())
	if t == 1 then
		e = ie
		else
		e = ((((tw/5)/5)*80)+ turtle.getFuelLevel())/(fe1 + f - turtle.getFuelLevel())
		end
	-- end efficiency math
	print("reported efficiency rating of " .. e .. "")
	turtle.turnRight()
	turtle.select(2)
	turtle.drop(64)
	turtle.turnLeft()
	finished = 0
	turtle.select(15)
	turtle.place()
	turtle.select(16)
	if turtle.getItemCount(16) == 0 then
		w = 1
		w()
	else
		turtle.place()
		end
	end

Note: It does not give me errors, The program simply ends after it chops down the tree and posts up the stats.
apemanzilla #2
Posted 27 June 2013 - 10:20 AM
It just ends, no infinite looping or errors?
Praetor22 #3
Posted 27 June 2013 - 10:26 AM
Yep. It's pretty strange, just stops working and waits for additional input. Not what I intended it to do haha.
Engineer #4
Posted 27 June 2013 - 10:26 AM

if not (bm == y) or (bm == n) then

That if statement is not firing. There chop gets set to 1.

Please post your full code since I cant read your mind and know what those variables are
Praetor22 #5
Posted 27 June 2013 - 10:46 AM
Alright, here is the code in it's entirety:

Spoiler

local z = 0
local x = 0
local finished = 0
local t = 0
local f = 0
local fe1 = 0
local tw = 0
local ie = 0
local chop = 0
local bm = nil
local function rf()
    turtle.select(1)
    turtle.refuel(1)
    f = f + 1
    end
local function rb()
    turtle.select(16)
    turtle.suckUp(5)
    end

local function fg()
    turtle.select(1)
    turtle.turnLeft()
    turtle.suck(31)
	    if false then
		    term.clear()
		    print("no charcoal available in fuel chest")
	    else
		    end
    turtle.turnRight()
    end
local function df()
    turtle.select(2)
    turtle.dig()
	    if true then
		    turtle.forward()
		    if true then
			    z = (z+1)
			    end
		    end
	    end
local function du()
    turtle.digUp()
	    if true then
		    turtle.up()
		    if true then
			    x = (x+1)
			    end
		    end
	    end
local function rn()
    turtle.back()
	    if true then
		    z = (z-1)
			    if true then
				    finished = 1
				    end
			    end
		    end
local function w()
    while w == 1 do
	    turtle.select(15)
	    sleep(60)
		    if not turtle.compare() then
			    w = 0
			    end
		    end
	    end
local function ft()
    rn()
    t = t + 1
    term.clear()
    print("".. t .. " tree(s) removed")
    tw = (tw + turtle.getItemCount(2))
    print("" .. tw .. " blocks of wood collected")
    print("" .. f .. " piece(s) of coal used")
    -- efficiency math
    ie = ((((tw/5)/5)*80)+ turtle.getFuelLevel())/(fe1 - turtle.getFuelLevel())
    if t == 1 then
	    e = ie
	    else
	    e = ((((tw/5)/5)*80)+ turtle.getFuelLevel())/(fe1 + f - turtle.getFuelLevel())
	    end
    -- end efficiency math
    print("reported efficiency rating of " .. e .. "")
    turtle.turnRight()
    turtle.select(2)
    turtle.drop(64)
    turtle.turnLeft()
    finished = 0
    turtle.select(15)
    turtle.place()
    turtle.select(16)
    if turtle.getItemCount(16) == 0 then
	    w = 1
	    w()
    else
	    turtle.place()
	    end
    end

-- working program
if not (bm == y) or (bm == n) then
    term.clear()
    write("would you like to enable bonemeal refueling? (y/n): ")
    local bm = read()
    chop = 1
else
end
if chop == 1 then
    while true do
	    if fe1 == 0 then
		    fe = turtle.getFuelLevel()
	    else
		    end
	    sleep(0)
	    if turtle.getItemCount(1) < 2 then
		    fg()
	    else
		    end
	    sleep(0)
	    if turtle.getFuelLevel() < 60 then
		    rf()
	    else
		    end
	    sleep(0)
	    if (bm == y) and (turtle.getItemCount(16) < 1) then
		    rb()
	    else
		    end
	    sleep(0)
	    if (z == 0) and (x == 0) and (finished == 0) and turtle.detect() then
		    df()
		    end
	    while (finished == 0) and (z == 1) and turtle.detectUp() do
		    du()
		    end
	    while (finished == 0) and (z == 1) and (x > 0) and not turtle.detectUp() and not turtle.detectDown() do
		    turtle.down()
		    end
	    if (finished == 0) and (z == 1) and (x > 0) and not turtle.detectUp() and turtle.detectDown() then
		    ft()
		    end
	    end
    end

To clarify what the program does.

It is supposed to chop down a tree, return, place logs into a chest and refuel on bonemeal and charcoal.

variables:

z and x correspond to movement. Without these the turtle wouldn't move back to it's original spot.

t through ie are simply there to calculate efficiency and report back with some useful stats that have to do with a system the turtle is used in.
t = how many trees it has chopped down since running the program.
f = how much coal it has used since running the program
fe1 = fuel level
tw = total wood collected since running the program
ie = is the efficiency rating

chop lets the program know when to run

bm is the bonemeal count

I hadn't written any inline notes so I had to remember all of this. While scouring my code I noticed on lines 127 to 128


	    if fe1 == 0 then
		    fe = turtle.getFuelLevel()

it was setting a nonexistent variable, I think this should be:


	    if fe1 == 0 then
		    fe1 = turtle.getFuelLevel()

Now it successfully uses bonemeal on the sapling as it did not before. However, the program still does not loop and cut down another tree still stopping at 1.
Praetor22 #6
Posted 27 June 2013 - 01:24 PM
Bump :\
GopherAtl #7
Posted 27 June 2013 - 03:00 PM
you're comparing bm to the variables y and n, not the strings "y" and "n". Also, the logic in that if statement probably isn't working like you intend anyway; I assume you want it to run unless bm is already either "y" or "n", but if I add parenthesis to show the order lua is going to parse that, what you're doing is basically ((not bm) == y) or (bm==n)). "not bm" will become true if bm is nil, otherwise false, so then you're comparing false to y and bm to n.

When in doubt about the order things will evaluate, add parenthesis yourself. Also as I said change those y/n to "y"/"n".

That condition should be:

if not (bm=="y" or bm=="n") then
it could also be (and to me, this is more natural, but that's up to you)

if bm~="y" and bm~="n" then

There are other conditional expressions in your code that need similar fixes, I won't go through them all, but if you look through them all again with this new info and add ()s wherever you're unsure about the order, you should be able to fix 'em all!