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

miner turtle code problem

Started by TyCamden, 18 November 2014 - 09:10 PM
TyCamden #1
Posted 18 November 2014 - 10:10 PM
I cant find it. It is probably simple. Can anyone help.

Error: " bios:366: [string "miner"]:157: 'end' expected (to close 'for' at line 125) "

Code:


-- before running program, be sure...
--	coal is in slot 16
--	cobble in slot 15
--	put an actual chest ABOVE the turtle that will be on the mission (empty) (for the loot)
--	put an actual chest BEHIND the turtle that will be on the mission (with more coal in it)
ypos = 0 -- This tracks how far down turtle is from its starting position
facing = 0 -- This tracks direction turtle is facing 0=original-front 1=left 2=right 3=back
mission = 1 -- Current status of mission.
			   -- 0 means mission completed
			   -- 1 means operational mission, still running.
			   -- 2 means that it is time to drop off stuff then return to where u left off
			   -- 3 means that it is time to refuel
while mission ~= 0 do
	  FuelLeft = turtle.getFuelLevel() + 1
	  if FuelLeft < 65 then -- if there is less than 65 fuel in the turtle...
		 mission = 3
		 if turtle.getItemCount(16) > 0 then -- if there is at least 1 item in coal-slot 16...
			turtle.select(16) -- choose slot 16 on turtle
			turtle.refuel(1) -- refuel turtle with 1 item from slot 16
			turtle.select(1) -- choose slot 1 on turtle (default?)
			mission = 1
		 else
			 -- coal slot is empty so go back and get more fuel into slot 16
			 yrem = ypos -- remember ypos before returning for fuel
			 facrem = facing -- remember facing before returning for fuel
			 if ypos == 0 then -- you are back at start
				-- get turtle facing the chest (backwards from original direction
				if facing == 0 then -- facing forward
				   turtle.turnLeft() -- turn left
				   turtle.turnLeft() -- turn left
				   facing = 3
				end
				if facing == 1 then -- facing left
				   turtle.turnLeft() -- turn left
				   facing = 3
				end
				if facing == 2 then -- facing right
				   turtle.turnRight() -- turn right
				   facing = 3
				end
				-- see if fuel chest is empty or not and get more fuel
				turtle.select(16) -- choose slot 16 on turtle
				turtle.suck() -- suck fuel out of chest into slot 16 if possible
				turtle.turnRight() -- turning turtle back towards facing the front
				   facing = 1
				turtle.turnRight() -- finishing turning turtle to facing the front
				   facing = 0
				turtle.refuel(1) -- refuel turtle with 1 item from slot 16
				turtle.select(1) -- choose slot 1 on turtle (default?)
				FuelLeft = turtle.getFuelLevel() + 1
				if FuelLeft < 65 then -- still less than 65 fuel after refueling attempt
				   print "Not enough fuel to continue mission."
				else
					-- RETURN TO POSITION PRIOR TO NEEDING FUEL IF NECESSARY
					while yrem ~= ypos do
					   turtle.down()
					   ypos = ypos - 1
					end
					while facrem ~= facing do
						  turtle.turnRight()
						  if facing == 0 then -- 0=original-front 1=left 2=right 3=back
							 facing = 2
						  else
							  if facing == 1 then
								 facing = 0
							  else
								  if facing == 2 then
									 facing = 3
								  else
									  -- facing = 3
									  facing = 1
								  end
							  end
						  end
					end
					mission = 1
				end
			 else
				 -- move back up to start
				 while ypos<0 and ypos~=0 do
					   turtle.up()
					   ypos = ypos + 1
				 end
			 end
		 end
	  else  -- if there is NOT less than 65 fuel in the turtle...
			if turtle.down() then -- it was able to move down
			   ypos = ypos - 1
			else -- it cannot move down
				-- find out what is below it and act accordingly
				local b,t = turtle.inspectDown()
				local x = data.name
				if x == "minecraft:chest" then
				   turtle.suckDown()
				   turtle.select(15)
				   turtle.placeDown()
				   turtle.select(1)
				   mission = 2 -- drop off all new stuff after grabbing a rare chest
				else
					if x == "minecraft:bedrock" then -- bottom of the world
					   mission = 0
					   while ypos ~= 0 do
							 turtle.up()
							 ypos = ypos + 1
							 turtle.select(15)
							 turtle.placeDown()
							 turtle.select(1)
					   end
					else
						turtle.digDown()
						turtle.down()
						ypos = ypos - 1
						-- turtle.select(15) Decided not to fill hole above turtle when going down
						-- turtle.placeUp()
						-- turtle.select(1)
					end
				end
			end
			if mission ~= 2 then
			   -- seal any holes around this location (main concern: lava)
			   for i = 1, 4 do
				   if turtle.detect()=="false" then
				   -- there is a hole
				   turtle.select(15)
				   turtle.place()
				   turtle.select(1)
				   turtle.turnLeft()
			   end
			   -- look around for goodies
			   flag = 0
			   for i = 1, 4 do
				   local b,t = turtle.inspect()
				   local x = data.name
				   if x == "minecraft:stone" then
					  flag = 1
				   end
				   if x == "minecraft:cobblestone" then
					  flag = 1
				   end
				   if x == "minecraft:dirt" then
					  flag = 1
				   end
				   -- if the flag is still zero, it may be valuable
				   --  and just get rid of gravel
				   if flag == 0 then
					  turtle.dig()
					  turtle.select(15)
					  turtle.place()
					  turtle.select(1)
				   end
				   turtle.turnLeft()
			   end
			else
				-- mission = 2 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
				--	2 means that it is time to drop off stuff then return to where u left off
				-- there is a chest ABOVE the turtle start position for the loot
				yrem = ypos -- remember ypos before returning for fuel
				while ypos ~= 0 do
					  turtle.up()
					  ypos = ypos + 1
				end
				for i = 1, 14 do
					turtle.select(i)
					turtle.dropUp()
				end
				turtle.select(1)
				while yrem ~= ypos do
					  turtle.down()
					  ypos = ypos - 1
				end
			end
	  end
end
valithor #2
Posted 18 November 2014 - 11:44 PM
-snip

Should note that you got the error msg wrong. It is for loop on line 121 end expected on line 153. Your problem is that on line 121 you have a for loop. Inside that for loop you have a if statement that you never end.

for i = 1, 4 do
    if turtle.detect()=="false" then   <---- if statement with no end
    -- there is a hole
    turtle.select(15)
    turtle.place()
    turtle.select(1)
    turtle.turnLeft()
end
Edited on 18 November 2014 - 10:46 PM
KingofGamesYami #3
Posted 18 November 2014 - 11:56 PM
The error message was correct, because it interprets the for loop's and as the if statement's end, and on down the line…
valithor #4
Posted 19 November 2014 - 12:05 AM
The error message was correct, because it interprets the for loop's and as the if statement's end, and on down the line…

I ran it myself… it gave a different error msg which led me to the correct spot.

The error msg i got actually made sense. The one he got points to a turtle.place() and him defining a variable.

edit:
In my experience that error always points to where you start something that actually needs a end statement, which is why i said that. The one i got was different and pointed to what i expected.
Edited on 18 November 2014 - 11:08 PM
TyCamden #5
Posted 19 November 2014 - 02:36 PM
Thank you for finding that. Maybe you all could help with a new problem I got after I fixed and ran it…

" miner:95: attempt to index ? (a nil value) "

I think it relates to this section of the code, but I am not sure how to deal with the error…


			    -- find out what is below it and act accordingly
			    local b,t = turtle.inspectDown()
			    local x = data.name
			    if x == "minecraft:chest" then
				   turtle.suckDown()
KingofGamesYami #6
Posted 19 November 2014 - 03:23 PM
You didn't define data, which you then attempt to index with data.name… Giving you that error. I'm assuming you expected the result from turtle.inspectDown().


local bSuccess, tData = turtle.inspectDown()
if bSuccess and tData.name == "minecraft:chest" then
   turtle.suckDown()
end
TyCamden #7
Posted 19 November 2014 - 04:11 PM
It's fixed and working, but it does something unexpected (due to my flawed planning, probably).

When it is done and it returns to starting position, it still mines and places blocks around that position.

Any thoughts on how to prevent that. (Hopefully an easy minor fix)

Here is current code:


-- before running program, be sure...
--    coal is in slot 16
--    cobble in slot 15
--    put an actual chest ABOVE the turtle that will be on the mission (empty) (for the loot)
--    put an actual chest BEHIND the turtle that will be on the mission (with more coal in it)
ypos = 0 -- This tracks how far down turtle is from its starting position
facing = 0 -- This tracks direction turtle is facing 0=original-front 1=left 2=right 3=back
mission = 1 -- Current status of mission.
			   -- 0 means mission completed
			   -- 1 means operational mission, still running.
			   -- 2 means that it is time to drop off stuff then return to where u left off
			   -- 3 means that it is time to refuel
while mission ~= 0 do
	  FuelLeft = turtle.getFuelLevel() + 1
	  if FuelLeft < 65 then -- if there is less than 65 fuel in the turtle...
		 mission = 3
		 if turtle.getItemCount(16) > 0 then -- if there is at least 1 item in coal-slot 16...
		    turtle.select(16) -- choose slot 16 on turtle
		    turtle.refuel(1) -- refuel turtle with 1 item from slot 16
		    turtle.select(1) -- choose slot 1 on turtle (default?)
		    mission = 1
		 else
			 -- coal slot is empty so go back and get more fuel into slot 16
			 yrem = ypos -- remember ypos before returning for fuel
			 facrem = facing -- remember facing before returning for fuel
			 if ypos == 0 then -- you are back at start
			    -- get turtle facing the chest (backwards from original direction
			    if facing == 0 then -- facing forward
				   turtle.turnLeft() -- turn left
				   turtle.turnLeft() -- turn left
				   facing = 3
			    end
			    if facing == 1 then -- facing left
				   turtle.turnLeft() -- turn left
				   facing = 3
			    end
			    if facing == 2 then -- facing right
				   turtle.turnRight() -- turn right
				   facing = 3
			    end
			    -- see if fuel chest is empty or not and get more fuel
			    turtle.select(16) -- choose slot 16 on turtle
			    turtle.suck() -- suck fuel out of chest into slot 16 if possible
			    turtle.turnRight() -- turning turtle back towards facing the front
				   facing = 1
			    turtle.turnRight() -- finishing turning turtle to facing the front
				   facing = 0
			    turtle.refuel(1) -- refuel turtle with 1 item from slot 16
			    turtle.select(1) -- choose slot 1 on turtle (default?)
			    FuelLeft = turtle.getFuelLevel() + 1
			    if FuelLeft < 65 then -- still less than 65 fuel after refueling attempt
				   print "Not enough fuel to continue mission."
			    else
				    -- RETURN TO POSITION PRIOR TO NEEDING FUEL IF NECESSARY
				    while yrem ~= ypos do
					   turtle.down()
					   ypos = ypos - 1
				    end
				    while facrem ~= facing do
						  turtle.turnRight()
						  if facing == 0 then -- 0=original-front 1=left 2=right 3=back
							 facing = 2
						  else
							  if facing == 1 then
								 facing = 0
							  else
								  if facing == 2 then
									 facing = 3
								  else
									  -- facing = 3
									  facing = 1
								  end
							  end
						  end
				    end
				    mission = 1
			    end
			 else
				 -- move back up to start
				 while ypos<0 and ypos~=0 do
					   turtle.up()
					   ypos = ypos + 1
				 end
			 end
		 end
	  else  -- if there is NOT less than 65 fuel in the turtle...
		    if turtle.down() then -- it was able to move down
			   ypos = ypos - 1
		    else -- it cannot move down
			    -- find out what is below it and act accordingly
			    local bSuccess, tData = turtle.inspectDown()
			    if bSuccess and tData.name == "minecraft:chest" then
				   turtle.suckDown()
				   turtle.select(15)
				   turtle.placeDown()
				   turtle.select(1)
				   mission = 2 -- drop off all new stuff after grabbing a rare chest
			    else
				    if bSuccess and tData.name == "minecraft:bedrock" then -- bottom of the world
					   mission = 0
					   while ypos ~= 0 do
							 turtle.up()
							 ypos = ypos + 1
							 turtle.select(15)
							 turtle.placeDown()
							 turtle.select(1)
					   end
				    else
					    turtle.digDown()
					    turtle.down()
					    ypos = ypos - 1
					    -- turtle.select(15) Decided not to fill hole above turtle when going down
					    -- turtle.placeUp()
					    -- turtle.select(1)
				    end
			    end
		    end
		    if mission ~= 2 then
			   -- seal any holes around this location (main concern: lava)
			   for i = 1, 4 do
				   if turtle.detect()=="false" then
					  -- there is a hole
					  turtle.select(15)
					  turtle.place()
					  turtle.select(1)
					  turtle.turnLeft()
				   end
			   end
			   -- look around for goodies
			   flag = 0
			   for i = 1, 4 do
				   local bSuccess, tData = turtle.inspect()
				   if bSuccess and tData.name == "minecraft:stone" then
					  flag = 1
				   end
				   if bSuccess and tData.name == "minecraft:cobblestone" then
					  flag = 1
				   end
				   if bSuccess and tData.name == "minecraft:dirt" then
					  flag = 1
				   end
				   -- if the flag is still zero, it may be valuable
				   --  and just get rid of gravel
				   if flag == 0 then
					  turtle.dig()
					  turtle.select(15)
					  turtle.place()
					  turtle.select(1)
				   end
				   turtle.turnLeft()
			   end
		    else
			    -- mission = 2
			    --    2 means that it is time to drop off stuff then return to where u left off
			    -- there is a chest ABOVE the turtle start position for the loot
			    yrem = ypos -- remember ypos before returning for fuel
			    while ypos ~= 0 do
					  turtle.up()
					  ypos = ypos + 1
			    end
			    for i = 1, 14 do
				    turtle.select(i)
				    turtle.dropUp()
			    end
			    turtle.select(1)
			    while yrem ~= ypos do
					  turtle.down()
					  ypos = ypos - 1
			    end
		    end
	  end
end
TyCamden #8
Posted 20 November 2014 - 02:30 PM
I found a workaround…

I changed


		    end
		    if mission~=2 then
			   -- seal any holes around this location (main concern: lava)

to


		    end
		    if mission~=2 and mission~=0 then
			   -- seal any holes around this location (main concern: lava)
TyCamden #9
Posted 21 November 2014 - 08:37 PM
Here was the final program:


-- before running program, be sure...
--    coal is in slot 16
--    cobble in slot 15
--    put an actual chest ABOVE the turtle that will be on the mission (empty) (for the loot)
--    put an actual chest BEHIND the turtle that will be on the mission (with more coal in it)
ypos = 0 -- This tracks how far down turtle is from its starting position
facing = 0 -- This tracks direction turtle is facing 0=original-front 1=left 2=right 3=back
mission = 1 -- Current status of mission.
			   -- 0 means mission completed
			   -- 1 means operational mission, still running.
			   -- 2 means that it is time to drop off stuff then return to where u left off
			   -- 3 means that it is time to refuel
while mission ~= 0 do
	  FuelLeft = turtle.getFuelLevel() + 1
	  if FuelLeft < 65 then -- if there is less than 65 fuel in the turtle...
		 mission = 3
		 if turtle.getItemCount(16) > 0 then -- if there is at least 1 item in coal-slot 16...
		    turtle.select(16) -- choose slot 16 on turtle
		    turtle.refuel(1) -- refuel turtle with 1 item from slot 16
		    turtle.select(1) -- choose slot 1 on turtle (default?)
		    mission = 1
		 else
			 -- coal slot is empty so go back and get more fuel into slot 16
			 yrem = ypos -- remember ypos before returning for fuel
			 facrem = facing -- remember facing before returning for fuel
			 if ypos == 0 then -- you are back at start
			    -- get turtle facing the chest (backwards from original direction
			    if facing == 0 then -- facing forward
				   turtle.turnLeft() -- turn left
				   turtle.turnLeft() -- turn left
				   facing = 3
			    end
			    if facing == 1 then -- facing left
				   turtle.turnLeft() -- turn left
				   facing = 3
			    end
			    if facing == 2 then -- facing right
				   turtle.turnRight() -- turn right
				   facing = 3
			    end
			    -- see if fuel chest is empty or not and get more fuel
			    turtle.select(16) -- choose slot 16 on turtle
			    turtle.suck() -- suck fuel out of chest into slot 16 if possible
			    turtle.turnRight() -- turning turtle back towards facing the front
				   facing = 1
			    turtle.turnRight() -- finishing turning turtle to facing the front
				   facing = 0
			    turtle.refuel(1) -- refuel turtle with 1 item from slot 16
			    turtle.select(1) -- choose slot 1 on turtle (default?)
			    FuelLeft = turtle.getFuelLevel() + 1
			    if FuelLeft < 65 then -- still less than 65 fuel after refueling attempt
				   print "Not enough fuel to continue mission."
			    else
				    -- RETURN TO POSITION PRIOR TO NEEDING FUEL IF NECESSARY
				    while yrem ~= ypos do
					   turtle.down()
					   ypos = ypos - 1
				    end
				    while facrem ~= facing do
						  turtle.turnRight()
						  if facing == 0 then -- 0=original-front 1=left 2=right 3=back
							 facing = 2
						  else
							  if facing == 1 then
								 facing = 0
							  else
								  if facing == 2 then
									 facing = 3
								  else
									  -- facing = 3
									  facing = 1
								  end
							  end
						  end
				    end
				    mission = 1
			    end
			 else
				 -- move back up to start
				 while ypos<0 and ypos~=0 do
					   turtle.up()
					   ypos = ypos + 1
				 end
			 end
		 end
	  else  -- if there is NOT less than 65 fuel in the turtle...
		    if turtle.down() then -- it was able to move down
			   ypos = ypos - 1
		    else -- it cannot move down
			    -- find out what is below it and act accordingly
			    local bSuccess, tData = turtle.inspectDown()
			    if bSuccess and tData.name == "minecraft:chest" then
				   turtle.suckDown()
				   turtle.select(15)
				   turtle.placeDown()
				   turtle.select(1)
				   mission = 2 -- drop off all new stuff after grabbing a rare chest
			    else
				    if bSuccess and tData.name == "minecraft:bedrock" then -- bottom of the world
					   mission = 0
					   while ypos ~= 0 do
							 turtle.up()
							 ypos = ypos + 1
							 -- fill below turtle with cobble IF there is cobble in slot 15
							 cobcnt = turtle.getItemCount(15)
							 if cobcnt <1 then
							    for i = 1, 14 do
								    sltcnt = turtle.getItemCount(i)
								    if sltcnt > 0 then
									   wut = turtle.getItemDetail(i)
									   if wut.name == "minecraft:cobblestone" then
										  turtle.select(i)
										  turtle.transferTo(15)
										  turtle.select(1)
									   end
								    end
							    end
							 end
							 turtle.select(15)
							 turtle.placeDown()
							 turtle.select(1)
					   end
				    else
					    turtle.digDown()
					    turtle.down()
					    ypos = ypos - 1
				    end
			    end
		    end
		    if mission~=2 and mission~=0 then
			   -- seal any holes around this location (main concern: lava)
			   for i = 1, 4 do
				   if turtle.detect()=="false" then
					  -- there is a hole
					  turtle.select(15)
					  turtle.place()
					  turtle.select(1)
					  turtle.turnLeft()
				   end
			   end
			   -- look around for goodies
			   flag = 0
			   for i = 1, 4 do
				   local bSuccess, tData = turtle.inspect()
				   if bSuccess and tData.name == "minecraft:stone" then
					  flag = 1
				   end
				   if bSuccess and tData.name == "minecraft:cobblestone" then
					  flag = 1
				   end
				   if bSuccess and tData.name == "minecraft:dirt" then
					  flag = 1
				   end
				   -- if the flag is still zero, it may be valuable
				   --  and just get rid of gravel
				   if flag == 0 then
					  turtle.dig()
					  turtle.select(15)
					  turtle.place()
					  turtle.select(1)
				   end
				   turtle.turnLeft()
				   flag = 0
			   end
		    else
			    -- mission = 2 (or 0)
			    --    2 means that it is time to drop off stuff then return to where u left off
			    -- there is a chest ABOVE the turtle start position for the loot
			    yrem = ypos -- remember ypos before returning for fuel
			    while ypos ~= 0 do
					  turtle.up()
					  ypos = ypos + 1
			    end
			    -- top off slot 15 (cobble) to 64 items - if possible from goodies collected
			    cobb = turtle.getItemCount(15)
			    if cobb < 64 then
					  for i = 1, 14 do
						  ctslt = turtle.getItemCount(i)
						  if ctslt ~= 0 then
							 wut = turtle.getItemDetail(i)
							 if wut.name == "minecraft:cobblestone" then
							    turtle.select(i)
							    turtle.transferTo(15)
							    end
						  end
					  end
			    end
			    turtle.select(1)
			    -- see if there is a chest above the turtle
			    local bSuccess, tData = turtle.inspectUp()
			    if bSuccess and tData.name == "minecraft:chest" then
				   -- dump everything in slots 1-14 in the goodies chest
				   for i = 1, 14 do
					   turtle.select(i)
					   turtle.dropUp()
				   end
			    end
			    turtle.select(1)
			    while yrem ~= ypos do
					  turtle.down()
					  ypos = ypos - 1
			    end
			    if mission ~= 0 then -- if mission is not complete
				   -- mission = 2 -- it just finished dropping and returning
				   mission = 1
			    end
		    end
	  end
end