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

[Lua]Need help with a program I am attempting to code

Started by Praetor22, 13 May 2013 - 11:12 PM
Praetor22 #1
Posted 14 May 2013 - 01:12 AM
Everything is wrong, well, maybe not but still. I usually try and figure things out myself but I can't get this one.

Here is my code.

Spoiler

term.clear()
--Pre-operational message to go here
--ask how many trees or how long to run
--log of tree in 1, sapling in 2, bm in 3
--Memory
local bm = turtle.getItemCount(3)
local z = 0
local x = 0
local step = 0
local tc = nil
local t = 0
local pt = 0
--Memory Functions
local function df() -- Dig Forward
	pt = (t)
	turtle.dig()
	if true then
		turtle.forward()
		if true then
			z = (z+1)
			end
		end
	end
local function rn() -- Return
	turtle.back()
	if true then
		z = (z-1)
		end
	end
local function du() -- Dig Up
	turtle.digUp()
	if true then
		turtle.up()
		if true then
			x = (x+1)
			end
		end
	end
local function tc() -- Tree check
	turtle.select(1)
	if turtle.compare() then
		tc = 1
		end
	if not turtle.compare() then
		tc = 0
		end
	end
local function bt() -- Bonemeal true
	tc()
	if (tc == 0) then
		print("It appears there is no tree")
		turtle.select(2)
		if not turtle.compare() then
			turtle.place()
			print("Sapling Planted")
			turtle.select(3)
			turtle.place()
			print("Bonemeal Deployed")
			end
		if turtle.compare() then
			turtle.select(3)
			turtle.place()
			print("Bonemeal Deployed")
			return true
			end
		end
	if (tc == 1) then
		return true
		end
	end
local function bf() -- Bonemeal false
	tc()
	if (tc == 0) then
		print("It appears there is no tree")
		turtle.select(2)
		if not turtle.compare() then
			turtle.place()
			print("waiting for the tree to grow now, sir")
			sleep(300)
			end
		if turtle.compare() then
			print("waiting for the tree to grow, sir")
			sleep(300)
			end
		end
	if (tc == 1) then
		print("Tree has grown")
		end
	end
local function timber() -- Cut down tree
	if (step ~= 1) then
		print("something messed up")
		end
	if (z == 0) and (x == 0) and (step == 1) and (tc == 1) then
		df()
		step = 2
		end
	while (z == 1) and (step == 2) and turtle.detectUp() do
		du()
		end
	while (z == 1) and (step == 2) and not turtle.detectUp() and not turtle.detectDown() do
		turtle.down()
		t = (t+1)
		end
	if (z == 1) and turtle.detectDown() and not turtle.detectUp() and (t > pt) then
		rn()
		step = 3
		print("Haha, no more tree")
		print("I'm going to rest for a minute")
		sleep(60)
		end
	end
local function wl() -- Waiting loop
	tc()
	while (tc == 0) do
		sleep(300)
		tc()
		end
	if (tc == 1) then
		return true
		end
	end
--The actual lumbering part
if (bm > 2) and (z == 0) then
	bt()
	if true then
		step = 1
		timber()
		end
else
	print("At least 2 bonemeals are required to fertilize")
	end
if (bm < 2) and (z == 0) then
	bf()
	if (tc == 0) then
		wl()
		if true then
			step = 1
			timber()
			end
		end
	if (tc == 1) then
		step = 1
		timber()
		end
else
	print("Cannot operate, is there a foreign object in front of turtle?")
	print("Remember: Log in first slot, Sapling in second, and Bonemeal in third")
	end

Old Issue:

SpoilerThis issue has been SOLVED, NEW issue below

Currently I am getting '=' expected on line 8 "


local bm = turtle.getItemCount(3)

But obviously that is wrong

Current Issue:

Program does not function in it's current state, I feel it may be a syntax issue, but I can't figure it out.

I test the program by placing the materials in the correct slots, the turtle on flat ground and run the program.

What it should do is plant a sapling, and given there is enough bonemeal, fertilize it and then immediately cut it down.
If there is not enough bonemeal, it will plant a sapling and then wait until it grows.

I haven't gotten to a point where sapling metadata should be adversely affecting the program but I feel it should be relatively easy to fix, by making the turtle assign a value to a variable when it plants a sapling, to know that it has planted a sapling there.


But for some reason, there is something causing the program to fail.

Spoiler

I believe it is somewhere in this part of the code where everything goes to hell.

Spoiler

--The actual lumbering part
if (bm > 2) and (z == 0) then
		bt()
		if true then
				step = 1
				timber()
				end
else
		print("At least 2 bonemeals are required to fertilize")
		end
if (bm < 2) and (z == 0) then
		bf()
		if (tc == 0) then
				wl()
				if true then
						step = 1
						timber()
						end
				end
		if (tc == 1) then
				step = 1
				timber()
				end
else
		print("Cannot operate, is there a foreign object in front of turtle?")
		print("Remember: Log in first slot, Sapling in second, and Bonemeal in third")
		end
Edited by
Lyqyd #2
Posted 14 May 2013 - 02:50 PM
Split into new topic.

First line; `term.clear` needs to be `term.clear()`.
Praetor22 #3
Posted 14 May 2013 - 02:58 PM
I can't believe I am so dumb. Okay I fixed that now.

But now I am getting

At least 2 bonemeals are required to fertilize
Cannot operate, is there a foreign object in front of turtle?
Remember: Log in first slot, Sapling in second, and Bonemeal in third.


I must have messed up the syntax? :/
Lyqyd #4
Posted 14 May 2013 - 03:01 PM
Does your code depend on being able to compare the planted sapling with a sapling in inventory? That will not work, since sapling metadata changes over time. I'd have to look deeper if that isn't the case, which I won't be able to do right now.
Praetor22 #5
Posted 14 May 2013 - 06:01 PM
Huh, I did not know that. Only a small part of the program depends on that, but it isn't that which is causing the program to fail.

This is supposed to be a barebones framework for the actual chopping down and growing part of a tree farm.

I test the program by placing the materials in the correct slots, the turtle on flat ground and run the program.

What it should do is plant a sapling, and given there is enough bonemeal, fertilize it and then immediately cut it down.
If there is not enough bonemeal, it will plant a sapling and then wait until it grows.

I haven't gotten to a point where sapling metadata should be adversely affecting the program but I feel it should be relatively easy to fix, by making the turtle assign a value to a variable when it plants a sapling, to know that it has planted a sapling there.

But for some reason, there is something causing the program to fail.

also, I liked the Obi Wan part ;)/>.
Praetor22 #6
Posted 16 May 2013 - 02:51 PM
I hope you won't mind if I bump.
Lost Ninja #7
Posted 16 May 2013 - 08:44 PM
I always either use the log of the tree you're growing or a redstone BUD to signal the turtle that it needs to cut the tree down. Much simpler than comparing to saplings. The BUD option is great for trees like IC2 rubber trees or the Extra Biomes trees that have several block IDs in a single tree.

As for the rest I use something like this:
-- 1tree mk2
-- simple version
fs = 1  -- Fuel Slot
ss = 2  -- Sapling Slot
bs = 3  -- Bonemeal Slot
hgt = 0 -- height

function refuel()
	if (turtle.getFuelLevel() < 10) then
   	 turtle.select(fs)
		turtle.refuel(10)
	end
end

while true do
	work = redstone.getInput("front")  -- this is for the BUD method
	if work == true then
		while turtle.detect() do -- detecting in front
			refuel()
	   	 turtle.digUp()
	   	 turtle.up()
			hgt = hgt + 1
		end
		turtle.select((bs + 1))  -- for the logs
		for h = 1,hgt do
			refuel()
	   	 turtle.down() -- turtle generally does not mine the top most leaf block...
	   	 turtle.dig()
		end
		hgt = 0
		for i=(bs + 1),16 do -- empties out the logs
	   	 turtle.select(i)
			turtle.dropDown()
		end
		turtle.select(ss)
		turtle.place()
		turtle.select(bs)
		turtle.place()	  
	else
		sleep(30)
	end
end
((Untested, but based on a working program. Changed to remove API specific commands.))