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

Turtle map tracker

Started by crys17p, 14 September 2013 - 08:28 AM
crys17p #1
Posted 14 September 2013 - 10:28 AM
Title: Turtle map tacker

Hello guys i am new to the minecraft game and it is indeed an amazing game. A day ago i got into working with the turlte bots to automate some tasks. I am having some problems with a map tracker that i am making. Basically it records the steps it has done so far ( left, forward, etc.) and when the fuel level reaches a certain value and there is also no more items for fuel it retraises it's steps. i have made a small program for testing this mapping but am having a weird problem. It saves the moves correctly ( into the array "map")but when it reaches the fuel limit it does not retrace the last 5 steps ( this is a test program and i am only saving the last 5 steps). When i check the contents of the array (map) all the elements are now nil. I tried to narrow it down and the problem seems to lie in the if statement for the fuel or slot checker though i am not sure. Thank you for your respons in advance.


map = {}
i = 5


if turtle.getFuelLevel() <= 140 then

if turtle.getItemCount(1) == 0 then

for count = 1,5 do

if map == 1 then
turtle.back()
end

if map == 2 then
turtle.forward()
end

if map == 3 then
turtle.down()
end

if map == 4 then
turtle.up()
end

if map == 5 then
turtle.turnRight()
end

if map == 6 then
turtle.turnLeft()
end
i = i - 1
end

else
turtle.select(1)
turtle.refuel()
end


else
i = 5
turtle.forward()
map[1] = 1

turtle.up()
map[2] = 3


turtle.forward()
map[3] = 1


turtle.up()
map[4] = 3


turtle.back()
map[5] = 2


end
immibis #2
Posted 14 September 2013 - 10:58 PM
Here is your program as a list of English instructions.
  • Set 'map' to an empty table.
  • Check your fuel level.
  • If the fuel level is too low, then retrace the steps in 'map'. Otherwise move.

Lua runs your program in order from top-to-bottom (unless an if statement/loop/function call/etc tells it to do something else).
You will see that first it sets 'map' to an empty table, then it checks the fuel level, then if the fuel level is too low it retraces the steps in 'map' - which is an empty table, because that's what you just set it to.
crys17p #3
Posted 15 September 2013 - 04:12 AM
Aha so basically all i have to do is make an infinite loop after the decalration of the global variables so that it woun't reset the variables.
crys17p #4
Posted 23 September 2013 - 10:53 AM
Turtle wood chopper

Hello guys

I have stumbled upon another problem while developing my own turtle tree chopper. For those that haven't seen my previous post a recap and update to the current version: I am making a tree harvester robot. In short it is supposed to first check for the presence of the tree in front of it by going one block up and detecting blocks in front. Afterwards it will start chopping form the bottom up until the fuel reaches a certain level. During this time it will also store all the moves that it has made in an array "map" , in order to retrace them once it reaches the fuel limit.

On the first run everything is fine and it retraces the steps correctly , goes back to a chest and refuels and goes back to the tree again. Afterwards in the next run it goes further up than the first time ( this is because i have not implemented an upper roof limit or better fuel management) and when it starts retracing at one point it goes haywire (probably due to a wrong move or something). The funny thing is that this does not happen all the time, some time it works but most times the second or later runs it misses. I am using three counters for the elements for the map "i" for down-counting , "j" for the for loop and "e" for the storing of moves. I have tried to display some of the variables to see what is going on but i can't quiet see the problem. I thank you in advance for your response.





	map = {}
	e = 1
	j = e
	i = e
	m = 1
	step_count = 0
	flag = 0
	flag_2 = 0
	red_flag = 0
	hight = 0
	fuel_need = 0
	fuel_prev = 0

function turtle_sleep()
   os.sleep(30)  
end
function resuply()
	 turtle.turnRight()
	 turtle.turnRight()
	 turtle.forward()
	 turtle.forward()
	 turtle.forward()
	 turtle.forward()
	 turtle.forward()
	 turtle.select(2)
	 turtle.drop()
	 turtle.select(1)
	 turtle.suck()
	 turtle.turnRight()
	 turtle.turnRight()
	 turtle.forward()
	 turtle.forward()
	 turtle.forward()
	 turtle.forward()
	 turtle.forward()
	 fuel_need = fuel_prev - turtle.getFuelLevel()
	 turtle.select(1)
	 turtle.refuel(fuel_need)
	 red_flag = 0
	 step_count = 0
	
	
end
function refuel()
	if turtle.getFuelLevel() < 70 then
	
	   if turtle.getItemCount(1) == 0 then
		  red_flag = 1
		  flag = 0
		  e = 1
		  for count = 1,j do
			
			 if map[i] == 1 then
				turtle.back()
			 end
			
			 if map[i] == 2 then
				turtle.forward()  
			 end
			
			 if map[i] == 3 then
				turtle.down()  
			 end			  
			
			 if map[i] == 4 then
				turtle.up()  
			 end
		  
			 if map[i] == 5 then
				turtle.turnRight()  
			 end
	
			 if map[i] == 6 then
				turtle.turnLeft()  
			 end
			 i = i - 1
			 m = m + 1
			 print(" i = " .. i .. " m = " .. m.."j"..j)
		  
			 fuel_prev = turtle.getFuelLevel()
			 flag = 0
		  end  
	   else
		 turtle.select(1)
		 turtle.refuel()
	   end

		if red_flag == 1 then
		  
		   resuply()
		end
		return false
  else  
  return true  
  end
end

function move()
  if  step_count < 3 then
	  if turtle.detect() then
		 turtle.dig()
	  else
		 turtle.forward()
		 turtle.select(2)
		 turtle.suck()
		 map[e] = 1
		 step_count = step_count + 1
	  end
	
		
		
  else
	if turtle.detectUp() then
	   turtle.digUp()
	end
	  turtle.up()
	  map[e] = 3
	  e = e + 1
	  turtle.turnRight()
	  map[e] = 6
	
	  e = e + 1
	  turtle.turnRight()
	  map[e] = 6
	
	  step_count = 0

	  
  end

	
  e = e + 1
  j = e
  i = e
  m = 0
   print(" i = " .. i .."j"..j)


end

	
while true do

	
	
  if refuel() then
	 if flag == 0 then
		turtle.up()
	 end
	
	 if turtle.detect() then
		if flag == 0 then
		   flag = 1
		   turtle.down()
		end
	  
	 else
		if flag == 0 then
		   turtle.down()
		   turtle_sleep()
		end
	 end
	 if flag == 1 then
		move()
	 end
  end
	  
	
	

end
Edited by
Lyqyd #5
Posted 23 September 2013 - 02:55 PM
I've moved this into your existing post. Please use this topic for all questions about this program.