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

Felling program - need to seperate two loops.

Started by vainot4u, 13 April 2013 - 08:40 PM
vainot4u #1
Posted 13 April 2013 - 10:40 PM
Okay so I am VERY noobish at this Lua and Computercraft stuff and although I am NOT a programer I am pretty good with technical stuff…So I have been bashing my head against the wall on this one. I have a turtle program that keeps the turtle going forward until it detects a block at which point it "tests" to see if its a wood or grass (based on compare comands and slots 1-5 …1-4 are wood, 5 is grass). If it sees wood it fells it and moves up until no more wood then back down it goes and continues searching. If it sees grass it turns right or left, forward, and right or left ready to process the next row. it alternates between rights and lefts as to keep it in a pattern that will completely cover the intended area…think of it as mowing the grass…

Okay my problem is that IF it sees a tree because of how the code is it can cause a reverse in the "pattern" as it will skip to the next section after cutting the tree down. I think I need to find a way to have the pattern in a function of its own and call on the function to cut down trees as needed…but maybe there is a simplier way?

Here is the code in it's entirety:


function turtleFarmWood1()			-- What this does is selects slot 1
	while true do					-- which has a wood type in it. If
		turtle.select(1)			-- the turtle has this type of wood
		if turtle.compare() then	-- in front of it, it will mine it
			turtle.dig()			-- and a space above the turtle and
			turtle.digUp()			-- then move upwards, otherwise it
			turtle.up()				-- breaks the loop. Wood2-4 do same
		else						-- thing but for slots 2-4 on turtle.
			break
		end
	end
end

function turtleFarmWood2()
	while true do
		turtle.select(2)
		if turtle.compare() then
			turtle.dig()
			turtle.digUp()
			turtle.up()
		else
			break
		end
	end
end

function turtleFarmWood3()
	while true do
		turtle.select(3)
		if turtle.compare() then
			turtle.dig()
			turtle.digUp()
			turtle.up()
		else
			break
		end
	end
end

function turtleFarmWood4()
	while true do
		turtle.select(4)
		if turtle.compare() then
			turtle.dig()
			turtle.digUp()
			turtle.up()
		else
			break
		end
	end
end

function turtleDoneFarm()				-- Once turtle has colected all the wood
	while true do						-- it will come bac down until is detects
		turtle.select(5)				-- slot 5 which will be grass. This will
		if turtle.compareDown() then	-- make sure the turtle comes to ground
			break						-- level.
		else
			turtle.digDown()
			turtle.down()
		end
	end
end


function turtlelj()		--This function combines the above functions into one
	turtleFarmWood1()
	turtleFarmWood2()
	turtleFarmWood3()
	turtleFarmWood4()
	turtleDoneFarm()
end


function turtleLeft()				-- Once our turtle detects a block in front it
	while true do					-- will test to see if it is wood (see above).
		turtle.select(5)			-- this will test to see if it is grass in front
		if turtle.compare() then	-- and force the turtle to turn around in the row
			turtle.turnLeft()		-- next to its current location.
			turtlelj()
			turtle.dig()
			turtle.digUp()
			turtle.forward()
			turtle.digUp()
			turtle.turnLeft()
		else
			turtle.dig()
			turtle.digUp()
			break		
		end
	end
end


function turtlelRight()				-- this does the same as Left but goes right.
	while true do
		turtle.select(5)
		if turtle.compare() then
			turtle.turnRight()
			turtlelj()
			turtle.dig()
			turtle.digUp()
			turtle.forward()
			turtle.digUp()
			turtle.turnRight()
		else
			turtle.dig()
			turtle.digUp()
			break		
		end
	end
end


while true do								-- This is where it comes together. Loop
	local fuel = turtle.getFuelLevel()		-- entire program, loop the program while
	while fuel > 100 do						-- it has fuel to run.
		while not turtle.detect() do		-- no block in front then loop
			if turtle.detectDown() then		-- see if a block is beneath it if it is:
				turtle.forward()			-- go forward
			else							-- if there isnt a block bellow:
				turtle.back()				-- pretty self explanitory, go back, turn
				turtle.turnRight()			-- right check, for trees, dig forward and
				turtlelj()					-- up, go forward, dig above self, and turn
				turtle.dig()				-- right. At this point if it sees a block
				turtle.digUp()				-- it breaks the loop.
				turtle.forward()
				turtle.digUp()
				turtle.turnRight()
				if turtle.detect() then
					break
				end
			end
		end
		turtlelj()							-- above loop was broken because it found a
		turtleRight()						-- block in front so it runs tree test and
		while not turtle.detect() do		-- mines or find grass and goes right
			if turtle.detectDown() then
				turtle.forward()			-- looping again and looking for blocks in front
			else
				turtle.back()
				turtle.turnLeft()
				turtlelj()
				turtle.dig()
				turtle.digUp()
				turtle.forward()
				turtle.digUp()
				turtle.turnLeft()
				if turtle.detect() then
					break
				end
			end
		end
		turtlelj()							--same as before but this time it goes left
		turtleLeft()
	end
end
Lyqyd #2
Posted 14 April 2013 - 01:03 AM
Split into new topic.
Bubba #3
Posted 14 April 2013 - 06:02 AM
I'm a bit confused by what you are doing here. Are you using an actual felling turtle as opposed to a mining turtle thereby requiring that you go around obstacles as opposed to just breaking it and continuing on? If so, then I would actually suggest just using a mining turtle due to the fact that at this time, there is no difference in speeds between the mining/felling turtles and the mining turtle can dig through more blocks.

If that's not the reason you are avoiding obstacles, then there are easier ways to go about this.

local turnStatus = false -- This is to keep track of whether the turtle has turned or not
while true do
  if not turtle.detect() then --If there is no block ahead of the turtle...
	turtle.forward()
	if turnStatus == "right" then --If we have previously set the turnStatus to something, this will turn the turtle back to its original heading
	   turtle.turnLeft()
	   turnStatus = false
	elseif turnStatus == "left" then
	   turtle.turnRight()
	   turnStatus = false
	end
  elseif turtle.compare() then --Assuming that you have the slot selected that has wood in it
	digTree() --Your dig tree function
  else
	if turnStatus == "right" then --If we have previously set the heading to "right" and there is a block that is not wood in front of it, we will turn 2 left and check that way
	   turnStatus = "left"
	   turtle.turnLeft()
	   turtle.turnLeft()
	elseif not turnStatus --If turnStatus is false, turn right and check the block there
	   turnStatus = "right"
	   turtle.turnRight()
	else --If you've turned right and left and there are blocks in both places, end the program
	  print("I've checked both ways and there are blocks there. Ending the program")
	  break
	end
  end
end

Untested, but the logic should be valid.
vainot4u #4
Posted 14 April 2013 - 08:14 PM
Thanks Bubba I'll give your code a try.

The reason I'm developing this is to come up with a felling turtle that doesn't collect any grass at all. I'm trying to make it search within an area that has grass on the ground and tress (typical tree biome). However due to the fact that some areas do have flowers, ground foilage and what not I have to test what it runs into with using compare to make sure its not grass before just going and hacking away plus this also allows me to set boundries manually if so desired. This would be so much easier if someone would develop an API that can call on the vanilla block ID's…not even sure if thats possible…

Regardless thanks, I'll let you know how the tests go! :)/>