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

Trouble writing my first turtle program :(

Started by AirChris117, 21 March 2018 - 08:20 PM
AirChris117 #1
Posted 21 March 2018 - 09:20 PM
So here is the deal :

I am trying to setup a turtle so that it can replant a complex field with several stories. I borrowed this little program by Acuena that I tried to edit to make it fit my fields but I don't know how to make it plant the 7x5 field then go down to the next field until it's done.

Here is my progress (The prints are in french sorry :P/>)


Spoiler

-- * Planteur de champs 0.1
-- * Made by: AirChris117
-- * Utilisation : Slot 1 = Fuel ; Slot 2 à 16 = Seeds

--------------------------------------- Variables --------------------------------------- (WORKING)

curslot = 2
fuelused = 0
blocksplaced = -1
totalnrblocks = 0
fields = 6
length = 7
width = 5

--------------------------------------- Functions --------------------------------------- (WORKING)

function moveforward()
		while not turtle.forward() do
				checkfuel()
		end

end
function moveback()
		while not turtle.back() do
				checkfuel()
		end

end
function checkfuel()
		if turtle.getFuelLevel() < 1 then
				turtle.select(1)
				if not turtle.refuel(1) then
						print("Pas de fuel dans le slot 1, en attente de fuel")
						while not turtle.refuel(1) do
						end
						print("Refuel avec succes, reprise de l'action")
				end
				fuelused = fuelused + 1
				turtle.select(curslot)
		end
end

function turnright()
		turtle.turnRight()
		moveforward()
		turtle.turnRight()
end

function turnleft()
		turtle.turnLeft()
		moveforward()
		turtle.turnLeft()
end

function checkblockcount()
		if turtle.getItemCount(curslot) == 0 then
				selnextslot()
		end
end

function selnextslot()
		curslot = curslot + 1
		if curslot > 16 then
				print("Plus de ressources, fin de l'action")
				error()
		end
		turtle.select(curslot)
end

function place()
		checkblockcount()
		turtle.placeDown()
		blocksplaced = blocksplaced + 1
end

function countblocks()
		for i = 2,16 do
				turtle.select(i)
				totalnrblocks = totalnrblocks + turtle.getItemCount(i)
		end
		if not totalnrblocks == 0 then
				totalnrblocks = totalnrblocks + 1
		end
		turtle.select(curslot)
end

function returntostart()
		if orient == true then
				turtle.turnRight()
				for i = 1, width - 1 do
						moveforward()
				end
				turtle.turnRight()
				moveback()
			elseif orient == false then
				turtle.turnLeft()
				for i = 1, width - 1 do
						moveforward()
				end
				turtle.turnRight()
								for i = 1, length do
										turtle.back()
								end
		end
end

--------------------------------------- Code --------------------------------------- (NOT FINISHED - WORK IN PROGRESS) - Here is where I got stuck :(/>/>

if totalnrblocks < need then
		print("Pas assez de ressources") -- Not enough fuel
		print("Nombre total: "..need)
		print("Nombre disponible: "..totalnrblocks)
		error()
end

moveforward()
place()

for xx = 1,width - 1  do
		for x = 1, length-1 do
		moveforward()
		place()
end

if orient == false then -- Should turn right
		turnright()
		place()
else -- Should turn left
		turnleft()
		place()
end

orient = not orient
end

for x = 1, length-1 do
		moveforward()
		place()
end

print("Orient: "..tostring(orient))
returntostart()
print("Fin du replantage")
print("Nombre de plantations: "..blocksplaced)
print("Consommation de fuel: "..fuelused)

Here is a screenshot of my fields :

https://imgur.com/a/dnlPf


Here is what I want it to do :

https://imgur.com/a/M5AYj
Edited on 22 March 2018 - 12:13 PM
Lupus590 #2
Posted 22 March 2018 - 11:13 AM
Errors help.

The order that you define things in Lua is important, Lua starts from the top of your file and reads it, it sees you defining the function moveForward and thinks ok I'm making a function, within that function it then sees checkfuel and then gets confused as it doesn't know what checkfuel is.

Solution, move function definitions above where they are called.

(Off topic, have the forums been updated? The link creation dialog is different)
Edited on 22 March 2018 - 10:13 AM
AirChris117 #3
Posted 22 March 2018 - 12:53 PM
Thank you for the reply Lupus590,

The functions in the code were taken from the platform making program I talked about and it works, I am just trying to edit the code that is located at the bottom so it does what I want it to do, that is to replant my complex fields layout (see the screenshot in 1st post).

The trouble I am having is to actually make that happen, I have no clue where to start or how to make it plant each layer then go down to the next.

P.S. : I edited the code to be a little more user friendly to read.
SuperDyl19 #4
Posted 23 March 2018 - 01:02 AM
The loop for one field is from

moveforward()
place()

for xx = 1,width - 1  do

to the "end" after

for x = 1, length-1 do

Start by adding a "for loop" starting before the one field loop. It should look like this:

for y = 1, fields - 1 do

The word "end" should be added at the end of the field loop which would be the line before

print("Orient: "..tostring(orient))

Finally, add this code in before the "end" you added in at the end of the field loop:

moveforward()

turtle.down()

I think that should work to make your program work, but I didn't check it to be certain. Let us know of any errors you get.

Also, Lupus590, it doesn't matter the order of the functions above because they're only called after being all initialized since they're all initialized before the first line of code is ran.
Edited on 23 March 2018 - 12:05 AM
Dog #5
Posted 23 March 2018 - 02:05 AM

The order that you define things in Lua is important, Lua starts from the top of your file and reads it, it sees you defining the function moveForward and thinks ok I'm making a function, within that function it then sees checkfuel and then gets confused as it doesn't know what checkfuel is.

Solution, move function definitions above where they are called.

Also, Lupus590, it doesn't matter the order of the functions above because they're only called after being all initialized since they're all initialized before the first line of code is ran.

I'm realize I'm being pedantic here, but that' s not entirely true in ComputerCraft Lua at least…the order of localized functions does matter. In the case of this script it doesn't matter since the functions aren't localized.
Bomb Bloke #6
Posted 23 March 2018 - 10:57 AM
More specifically, if you reference a variable that hasn't been defined yet (eg a variable that you intend to hold a function pointer later), then Lua will assume that reference should be within the global scope. If you later define that variable within the local scope instead, then the previous reference won't lead to it. It'll still search the global scope, and if there's no reference there, it'll pull back a value of nil.

This is true for Lua in general.

(Off topic, have the forums been updated? The link creation dialog is different)

lolno
AirChris117 #7
Posted 23 March 2018 - 05:06 PM
Thank you for all your responses, sorry taking so long to reply as I have to get my posts approved by a forum moderator.

I will try what you suggested SuperDyl19 and I will come back to you with the results!
AirChris117 #8
Posted 23 March 2018 - 05:58 PM
Alright so here is the deal :

I edited the program with the new code (I added the lines that SuperDyl19 suggested) and ran the turtle.

Here are the results :

https://imgur.com/a/HgtSx

Now here are the problems :

- First the turtle misses one line, instead of doing 7*5 it does 7*4 in the first field
- Then the turtle misses on block and start the next field
- The next field is not aligned with the previous one, the turtle should go back to the far left of the field where it started the one before
- Finally the turtle misses 1 field, when I told it to do 3 fields it only did 2.

I will try to figure out how to fix each issue while waiting on your answers !
Purple #9
Posted 24 March 2018 - 11:58 AM
Post your new code.
AirChris117 #10
Posted 24 March 2018 - 09:41 PM
Sorry I forgot the new code here it is :

Spoiler


-- * Planteur de champs 0.1
-- * Made by: AirChris117
-- * Utilisation : Slot 1 = Fuel ; Slot 2 à 16 = Seeds

-------------------- Variables --------------------

curslot = 2
fuelused = 0
blocksplaced = -1
totalnrblocks = 0

fields = 6
total = fields + 1
length = 7
width = 5

need = length * width * fields

-------------------- Functions --------------------

function moveforward()
		while not turtle.forward() do
				checkfuel()
		end
end

function moveback()
		while not turtle.back() do
				checkfuel()
		end
end

function checkfuel()
		if turtle.getFuelLevel() < 1 then
				turtle.select(1)
				if not turtle.refuel(1) then
						print("Pas de fuel dans le slot 1, en attente de fuel")
						while not turtle.refuel(1) do
						end
						print("Refuel avec succes, reprise de l'action")
				end
				fuelused = fuelused + 1
				turtle.select(curslot)
		end
end

function turnright()
		turtle.turnRight()
		moveforward()
		turtle.turnRight()
end

function turnleft()
		turtle.turnLeft()
		moveforward()
		turtle.turnLeft()
end

function checkblockcount()
		if turtle.getItemCount(curslot) == 0 then
				selnextslot()
		end
end

function selnextslot()
		curslot = curslot + 1
		if curslot > 16 then
				print("Plus de ressources, fin de l'action")
				error()
		end
		turtle.select(curslot)
end

function place()
		checkblockcount()
		turtle.placeDown()
		blocksplaced = blocksplaced + 1
end

function countblocks()
		for i = 2,16 do
				turtle.select(i)
				totalnrblocks = totalnrblocks + turtle.getItemCount(i)
		end
		if not totalnrblocks == 0 then
				totalnrblocks = totalnrblocks + 1
		end
		turtle.select(curslot)
end

function returntostart()
		if orient == true then
				turtle.turnRight()
				for i = 1, width - 1 do
						moveforward()
				end
				turtle.turnRight()
				moveback()
			elseif orient == false then
				turtle.turnLeft()
				for i = 1, width - 1 do
						moveforward()
				end
				turtle.turnRight()
								for i = 1, length do
										turtle.back()
								end
		end
end

-------------------- Code --------------------

countblocks()

if totalnrblocks < need then
		print("Pas assez de ressources")
		print("Nombre total: "..need)
		print("Nombre disponible: "..totalnrblocks)
		error()
end

moveforward()

for y = 1, total - 1 do
place()

for xx = 1,width - 1  do
  for x = 1, length-1 do
  moveforward()
  place()
end

if orient == false then  -- Should turn right
   turnright()
   place()
else	   -- Should turn left
   turnleft()
   place()
end

orient = not orient
end

for x = 1, length-1 do
   moveforward()
   place()
end

moveforward()
turtle.down()
turnleft()
moveforward(5)
turnright()

end

print("Orient: "..tostring(orient))
returntostart()
print("Fin du replantage")
print("Nombre de plantations: "..blocksplaced)
print("Consommation de fuel: "..fuelused)
Edited on 24 March 2018 - 08:43 PM
AirChris117 #11
Posted 25 March 2018 - 06:59 PM
Still wasn't able to fix most of my issues :(/>

I will come back with a .gif showing exactly what the turtle is doing.
SuperDyl19 #12
Posted 25 March 2018 - 11:11 PM
I messed up on a couple of those suggested changes, but it won't take much to fix it. First, let's change the lines

moveforward()
turtle.down()
turnleft()
moveforward5)
turnright()

Into

if orient == false then
      turnright()
else
      turnleft()
end

orient = not orient
turtle.down()
turtle.place()

If you still want the turtle to start from the same place each time, use this instead:

orient = false
turnright()

for i = 1, 6 do
      moveforward()
end

turtle.down()
turtle.turnRight()
turtle.turnRight()

For the second version, you'll also need to change

for y = 1, total - 1 do
place()

into

place()
for y = 1, total - 1 do

Good job fixing some of my failings. I should have looked at your diagram better.
Edited on 25 March 2018 - 09:12 PM
AirChris117 #13
Posted 26 March 2018 - 03:26 PM
Thanks for the suggestions.

Here is the result :

https://youtu.be/oq4pX-o90EM

The current code :

Spoiler

-- * Planteur de champs 1.1
-- * Made by: AirChris117
-- * Utilisation : Slot 1 = Fuel ; Slot 2 à 16 = Seeds

-------------------- Variables --------------------
curslot = 2
fuelused = 0
blocksplaced = -1
totalnrblocks = 0

fields = 6
total = fields + 1
length = 7
width = 5
need = length * width * fields

-------------------- Functions --------------------

function moveforward()
		while not turtle.forward() do
				checkfuel()
		end
end

function moveback()
		while not turtle.back() do
				checkfuel()
		end
end

function checkfuel()
		if turtle.getFuelLevel() < 1 then
				turtle.select(1)
				if not turtle.refuel(1) then
						print("Pas de fuel dans le slot 1, en attente de fuel")
						while not turtle.refuel(1) do
						end
						print("Refuel avec succes, reprise de l'action")
				end
				fuelused = fuelused + 1
				turtle.select(curslot)
		end
end

function turnright()
		turtle.turnRight()
		moveforward()
		turtle.turnRight()
end

function turnleft()
		turtle.turnLeft()
		moveforward()
		turtle.turnLeft()
end

function checkblockcount()
		if turtle.getItemCount(curslot) == 0 then
				selnextslot()
		end
end

function selnextslot()
		curslot = curslot + 1
		if curslot > 16 then
				print("Plus de ressources, fin de l'action")
				error()
		end
		turtle.select(curslot)
end

function place()
		checkblockcount()
		turtle.placeDown()
		blocksplaced = blocksplaced + 1
end

function countblocks()
		for i = 2,16 do
				turtle.select(i)
				totalnrblocks = totalnrblocks + turtle.getItemCount(i)
		end
		if not totalnrblocks == 0 then
				totalnrblocks = totalnrblocks + 1
		end
		turtle.select(curslot)
end

function returntostart()
		if orient == true then
				turtle.turnRight()
				for i = 1, width - 1 do
						moveforward()
				end
				turtle.turnRight()
				moveback()
			elseif orient == false then
				turtle.turnLeft()
				for i = 1, width - 1 do
						moveforward()
				end
				turtle.turnRight()
								for i = 1, length do
										turtle.back()
								end
		end
end

-------------------- Code --------------------

countblocks()

if totalnrblocks < need then
		print("Pas assez de ressources")
		print("Nombre total: "..need)
		print("Nombre disponible: "..totalnrblocks)
		error()
end

	moveforward()

  
for y = 1, total - 1 do

place()

	for xx = 1,width - 1  do
		for x = 1, length-1 do
		moveforward()
		place()
	end


	if orient == false then	 -- Should turn right
			turnright()
			place()
	else						-- Should turn left
			turnleft()
			place()
	end

orient = not orient
end

	for x = 1, length-1 do
			moveforward()
			place()
	end

orient = false
turnright()

for i = 1, 6 do
	  moveforward()
end

turtle.down()
turtle.turnRight()
turtle.turnRight()

end

print("Orient: "..tostring(orient))
returntostart()
print("Fin du replantage")
print("Nombre de plantations: "..blocksplaced)
print("Consommation de fuel: "..fuelused)

We are almost there, the last problem I have is that the fields the program is making are 5*7 and I want 7*5 (the length and the width are inverted). I followed your advice and changed the code except for the last part where you told me to move the place() out of the loop. (That fixed the issue where the turtle misses 1 block each level like in the video).
Edited on 27 March 2018 - 12:26 PM
SuperDyl19 #14
Posted 27 March 2018 - 01:37 PM
If the length and width are switched, you should switch their values at

length = 7
width = 5

so it reads

length = 5
width = 7
AirChris117 #15
Posted 27 March 2018 - 02:46 PM
Still not working like it should, I will try to re-write it using the platform maker again and the suggestions that you made, and I will come back to report on my progress.
AirChris117 #16
Posted 27 March 2018 - 05:16 PM
Got it working perfectly now thanks to you ! I even managed to make it go back to the starting position. :)/>


I will drop the code if anyone is interested.

Spoiler

-- * Field worker 2.0
-- * Made by: AirChris117
-- * Usage : Slot 1 = Fuel ; Slot 2 à 16 = Seeds

-------------------- Variables --------------------
curslot = 2
fuelused = 0
blocksplaced = -1
totalnrblocks = 0
fields = 8
length = 5
width = 7
need = length * width * fields
-------------------- Functions --------------------

function moveforward()
		while not turtle.forward() do
				checkfuel()
		end
end

function moveback()
		while not turtle.back() do
				checkfuel()
		end
end
function turnright()
		turtle.turnRight()
		moveforward()
		turtle.turnRight()
end

function turnleft()
		turtle.turnLeft()
		moveforward()
		turtle.turnLeft()
end
function checkfuel()
		if turtle.getFuelLevel() < 1 then
				turtle.select(1)
				if not turtle.refuel(1) then
						print("Pas de fuel dans le slot 1, en attente de fuel")
						while not turtle.refuel(1) do
						end
						print("Refuel avec succes, reprise de l'action")
				end
				fuelused = fuelused + 1
				turtle.select(curslot)
		end
end
function checkblockcount()
		if turtle.getItemCount(curslot) == 0 then
				selnextslot()
		end
end

function selnextslot()
		curslot = curslot + 1
		if curslot > 16 then
				print("Plus de ressources, fin de l'action")
				error()
		end
		turtle.select(curslot)
end

function place()
		checkblockcount()
		turtle.placeDown()
		blocksplaced = blocksplaced + 1
end

function countblocks()
		for i = 2,16 do
				turtle.select(i)
				totalnrblocks = totalnrblocks + turtle.getItemCount(i)
		end
		if not totalnrblocks == 0 then
				totalnrblocks = totalnrblocks + 1
		end
		turtle.select(curslot)
end
function returntostart()
for i = 1, fields do
  turtle.up()
end

turtle.turnLeft()

  for i = 1, width * fields do
						moveforward()
  end

   turtle.turnRight()
   moveback()		  
	  
end

-------------------- Code --------------------
orient=false
countblocks()

if totalnrblocks < need then
		print("Pas assez de ressources")
		print("Nombre total necessaire: "..need)
		print("Nombre disponible: "..totalnrblocks)
		error()
end
place()
moveforward()
for i = 1, fields do
place()

  for xx = 1,width - 1  do
   for x = 1, length-1 do
	moveforward()
	place()
   end

   if orient == false then -- Should turn right
	turnright()
	place()
  
   else	  -- Should turn left
	turnleft()
	place()
  
   end

  orient = not orient

  end

for x = 1, length-1 do
		moveforward()
		place()
end

turtle.turnRight()
moveforward()
turtle.down()
turtle.turnRight()

for y = 1, length-1 do
moveforward()
end

turtle.turnLeft()
turtle.turnLeft()
end
print("Orient: "..tostring(orient))
returntostart()
print("Fin du replantage")
print("Nombre de plantations: "..blocksplaced)
print("Consommation de fuel: "..fuelused)
Edited on 27 March 2018 - 03:17 PM