Jump to content




Tunnel/stairs program gravel removal help


56 replies to this topic

#21 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 07 October 2012 - 12:07 PM

many people are confused about the
while not turtle.forward() do
turtle.dig()
end
basically I am telling it to loop through the code between the 'do' and the 'end' as long as the turtle.forward() command returns false or nothing

local function tunnel(length,torches)
  turtle.select(1) --select slot 1 where the torches are
  for done=1,tonumber(length) do
   while not turtle.forward() do turtle.dig() end --keep calling the forward function and every time it fails dig, if it succeeds then end (for gravel)
   while turtle.detectUp() do turtle.digUp() end --as long as something is above the turtle dig it out, once nothing is there end (for gravel)
   if torches~='' and done%tonumber(torches)==0 then turtle.back() turtle.placeUp() turtle.forward() end --if torches is not a blank string (what the read command returns when they enter nothing) then check if the amount of the tunnel done is divisible by 5, if it is devisible then move back and place a torch and move forward again
  end
  turtle.turnLeft()
  turtle.turnLeft()
  for done=1,tonumber(length) do
   while not turtle.forward() do turtle.dig() end --move back along the tunnel, digging out anything in the way (for gravel)
  end
  turtle.turnRight()
  for slot=2,16 do
   turtle.select(slot)
   turtle.drop()
  end
  turtle.turnRight()
  return true
end
term.clear()
term.setCursorPos(1,1)
print('How long should your tunnel be?')
write('length: ')
local len=read()
print('how often should torches be placed (blank for none)')
local torches=read()
tunnel(len,torches)

if there is still anything you do not understand feel free to ask

#22 SilverEyes

  • New Members
  • 30 posts

Posted 08 October 2012 - 06:11 AM

 KaoS, on 07 October 2012 - 12:07 PM, said:

many people are confused about the
while not turtle.forward() do
turtle.dig()
end
basically I am telling it to loop through the code between the 'do' and the 'end' as long as the turtle.forward() command returns false or nothing

Spoiler

if there is still anything you do not understand feel free to ask
uh I don't get this part
  for done=1,tonumber(length) do
and what does this mean?

tunnel(len,torches)

#23 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 08 October 2012 - 06:34 AM

ah, the for loop. very useful

if I were to use the code
for i=1,5 do
  print(i)
end
it would print
1
2
3
4
5
basically it loops through the code between 'do' and 'end', it sets the variable 'i' (you can use any variable) to be the first number (1 in this case) and then keeps looping through the code, adding 1 to the variable each time until it gets to the second number.
this code can be used to effectively run a segment a specific number of times. there are also many other properties of the command but we will only go into that if necessary

#24 SilverEyes

  • New Members
  • 30 posts

Posted 08 October 2012 - 07:28 AM

 KaoS, on 08 October 2012 - 06:34 AM, said:

ah, the for loop. very useful

if I were to use the code
for i=1,5 do
  print(i)
end
it would print
1
2
3
4
5
basically it loops through the code between 'do' and 'end', it sets the variable 'i' (you can use any variable) to be the first number (1 in this case) and then keeps looping through the code, adding 1 to the variable each time until it gets to the second number.
this code can be used to effectively run a segment a specific number of times. there are also many other properties of the command but we will only go into that if necessary
Ahh ok, does the "tonumber(length)" mean that it starts at 1 and goes to whatever number was put in for length?

#25 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 08 October 2012 - 07:55 AM

exactly. perfect for executing the code a certain number of times

#26 SilverEyes

  • New Members
  • 30 posts

Posted 08 October 2012 - 08:17 AM

 KaoS, on 08 October 2012 - 07:55 AM, said:

exactly. perfect for executing the code a certain number of times
What about this?
torches~='' and done%tonumber(torches)==0
Does it mean if torches isn't = to blank then something?

#27 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 08 October 2012 - 08:28 AM

in an if statement you can have multiple things to check simultaneously. example:
if 1<2 and 2>0 then
  print('successfull')
end
would print successful
you can also use OR
if 1==2 or 1==1 then
would also work

what that code is doing is checking if the variable 'torches' is NOT a blank string (the read command returns a blank string if you enter nothing) and it also checks if the number value of torches is perfectly divisible by 5

the % operator is something I learned about fairly recently, it returns the remainder of a division so 3%2==1 because 3/2=1 and remainder 1
4%2==0 because there is no remainder, you can use bignumber%smallnumber==0 to check if they are mutiples

#28 SilverEyes

  • New Members
  • 30 posts

Posted 08 October 2012 - 08:36 AM

 KaoS, on 08 October 2012 - 08:28 AM, said:

in an if statement you can have multiple things to check simultaneously. example:
if 1<2 and 2>0 then
  print('successfull')
end
would print successful
you can also use OR
if 1==2 or 1==1 then
would also work

what that code is doing is checking if the variable 'torches' is NOT a blank string (the read command returns a blank string if you enter nothing) and it also checks if the number value of torches is perfectly divisible by 5

the % operator is something I learned about fairly recently, it returns the remainder of a division so 3%2==1 because 3/2=1 and remainder 1
4%2==0 because there is no remainder, you can use bignumber%smallnumber==0 to check if they are mutiples
So if torches isn't blank and done/spacing of torches has no remainder it puts down the torches?

#29 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 08 October 2012 - 08:45 AM

exactly, so you understand it now?

#30 SilverEyes

  • New Members
  • 30 posts

Posted 08 October 2012 - 08:48 AM

 KaoS, on 08 October 2012 - 08:45 AM, said:

exactly, so you understand it now?
Yeh, I think so. :D/> Thanks!

#31 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 08 October 2012 - 09:10 AM

no problem, try building your own version of the code without looking at the old one, once you can do that then you're all good. enjoy the mod

#32 SilverEyes

  • New Members
  • 30 posts

Posted 08 October 2012 - 09:58 AM

 KaoS, on 08 October 2012 - 09:10 AM, said:

no problem, try building your own version of the code without looking at the old one, once you can do that then you're all good. enjoy the mod
Ahaha I'll give it a shot. :D/> Thanks for the help

#33 SilverEyes

  • New Members
  • 30 posts

Posted 08 October 2012 - 11:26 AM

 KaoS, on 08 October 2012 - 09:10 AM, said:

no problem, try building your own version of the code without looking at the old one, once you can do that then you're all good. enjoy the mod
One more thing,
  turtle.turnRight()
  return true
What does the return true mean?
And is there any way we could add each other over something so we could respond quicker?
Do you have Steam or something?

#34 SilverEyes

  • New Members
  • 30 posts

Posted 08 October 2012 - 11:40 AM

Ok I tried it myself I did look at yours a bit but here it is. I do have one error though not sure why?
It says "bios:206: [string "mytun"]:2: '(' expected
local function Tunneltest(length,torch)
turtle.select(1)
for i=1, tonumber(length) do
while not turtle.forward() do
turtle.dig()
while turtle.detectUp() do
turtle.digUp()
if torch~='' and i%(torches)==0 then
turtle.back()
turtle.placeUp()
turtle.forward()
end
turtle.turnLeft()
turtle.turnLeft()
for i = 1, tonumber(length) do
while not turtle.forward() do
turtle.dig()
end
turtle.turnRight()
for slot 2, 16 do
turtle.select(slot)
turtle.drop()
end
turtle.turnRight()
end
term.clear()
term.SetCurserPos(1,1)
print("How long do you want the tunnel?")
local length = read()
print("What block distance between torches? (Leave blank for none)")
local torch = read()
Tunneltest(length,torch)


#35 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 08 October 2012 - 11:43 AM

that error says it is on line 2, there are no issues on line 2 so don't know about that, I know you are not closing your while true loops and the for loop for dropping needs and '=' between slot and 2

EDIT: you also didn't close the second for loop.... try laddering your code to make it easier to see and use notepad++ syntax highlighting

#36 SilverEyes

  • New Members
  • 30 posts

Posted 08 October 2012 - 11:47 AM

 KaoS, on 08 October 2012 - 11:43 AM, said:

that error says it is on line 2, there are no issues on line 2 so don't know about that, I know you are not closing your while true loops and the for loop for dropping needs and '=' between slot and 2

EDIT: you also didn't close the second for loop.... try laddering your code to make it easier to see and use notepad++ syntax highlighting
Yeh I thought it meant there was an error on line 2 but I couldn't find it so yeh, I've fixed the ones you've pointed out I'll see how it runs now.

#37 SilverEyes

  • New Members
  • 30 posts

Posted 08 October 2012 - 11:53 AM

 KaoS, on 08 October 2012 - 11:43 AM, said:

that error says it is on line 2, there are no issues on line 2 so don't know about that, I know you are not closing your while true loops and the for loop for dropping needs and '=' between slot and 2

EDIT: you also didn't close the second for loop.... try laddering your code to make it easier to see and use notepad++ syntax highlighting
I'm using notpad++ but it doesn't highlight anything? What's laddering my code?

#38 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 08 October 2012 - 12:10 PM

go to languages->L->lua and it will use syntax highlighting, when I say ladder I mean this
for i=1,5 do
    print(i)
end
rather than this
for i=1,5 do
print(i)
end

so you can see when you need an 'end'

#39 SilverEyes

  • New Members
  • 30 posts

Posted 08 October 2012 - 12:23 PM

 KaoS, on 08 October 2012 - 12:10 PM, said:

go to languages->L->lua and it will use syntax highlighting, when I say ladder I mean this
for i=1,5 do
	print(i)
end
rather than this
for i=1,5 do
print(i)
end

so you can see when you need an 'end'
Ok I've done that turns out I forgot to close alot :D/> and I spelt cursor wrong. Testing it again now though ;)/>

#40 SilverEyes

  • New Members
  • 30 posts

Posted 08 October 2012 - 12:25 PM

Ok, I've tested it after that. It works!!! :D/> It digs the gravel right and everything. ;)/> Well I've gotta go to bed here so I'll talk again tomorrow.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users