Jump to content




A turtle home block()


  • You cannot reply to this topic
20 replies to this topic

#1 iceman11a

  • Members
  • 68 posts
  • LocationUSA

Posted 27 April 2014 - 01:48 PM

If when it happens, A player or a animal get in the way of the turtle. It stops. Even if the program is still running. The turtle doesn't do any thing. This is from testing I been doing and this is the result.

My idea was to add a new home block. This block gets put under the turtle. This is home position for the turtle. The home block can be right clicked on and one option. A number, the player adds a number to this home block.

In the lua code. 2 new commands.

if turtle.still() then
turtle.home(#)
end

The turtle.still checks to see if the turlte is moving. This returns true or false. If not. Then the turtle will go to the home starting block using the # in the turtle.home(#) command.

#2 apemanzilla

  • Members
  • 1,421 posts

Posted 27 April 2014 - 01:57 PM

A teleportation block seems overpowered to me. Additionally, any of the turtle's moving functions will return false if they cannot move for any reason - including a player being in the way.

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 27 April 2014 - 02:27 PM

Commands such as turtle.forward() return information about whether or not they were successful (a boolean set to either true or false, in this case). It is hence possible to know when the turtle has failed to go forward (eg due to a player or animal), and program it to react accordingly.

For example:

while not turtle.forward() do
  turtle.dig()
  turtle.attack()
end

The above code will make the turtle move once, and the script won't carry on until that action has completed successfully. Every attempt that ends in failure will lead to the turtle swinging whatever weapon/tool it has around before it makes the next one.

It's also worth reading up on the GPS API. This allows a system to determine where in the world it's located - great if you want to code turtles to navigate themselves to a certain location regardless of their starting point.

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 27 April 2014 - 02:33 PM

View Posticeman11a, on 27 April 2014 - 01:48 PM, said:

turtle.home(#)
this is invalid Lua syntax...

#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 27 April 2014 - 02:39 PM

Hash in this instance is being used as a "insert number here" symbol. As in, you'd type the ID of the home block you wanted the turtle to go to in place of the #.

#6 iceman11a

  • Members
  • 68 posts
  • LocationUSA

Posted 27 April 2014 - 06:22 PM

View Postapemanzilla, on 27 April 2014 - 01:57 PM, said:

A teleportation block seems overpowered to me. Additionally, any of the turtle's moving functions will return false if they cannot move for any reason - including a player being in the way.

Well I don't mean any thing like that. Just a block that the turtle returns too. I don't have an option to handle errors like this. If that's what you want to call it. This means I will have to build a wall a round where the turtle is just to keep players and animals out.

View PostBomb Bloke, on 27 April 2014 - 02:27 PM, said:

Commands such as turtle.forward() return information about whether or not they were successful (a boolean set to either true or false, in this case). It is hence possible to know when the turtle has failed to go forward (eg due to a player or animal), and program it to react accordingly.

For example:

while not turtle.forward() do
  turtle.dig()
  turtle.attack()
end

The above code will make the turtle move once, and the script won't carry on until that action has completed successfully. Every attempt that ends in failure will lead to the turtle swinging whatever weapon/tool it has around before it makes the next one.

It's also worth reading up on the GPS API. This allows a system to determine where in the world it's located - great if you want to code turtles to navigate themselves to a certain location regardless of their starting point.

Yes, That's right. How ever when this happens the turtle just sits there and doesn't nothing. Theres no way to program the turtle to say hey, Your not moving, so do some thing. Or have the turtle react to a problem. You can only program it for very basic stuff. Unless you wanted to sit a round and watch it.

View Posttheoriginalbit, on 27 April 2014 - 02:33 PM, said:

View Posticeman11a, on 27 April 2014 - 01:48 PM, said:

turtle.home(#)
this is invalid Lua syntax...

Yes I know. That's because it hasn't been added yet.

View PostBomb Bloke, on 27 April 2014 - 02:39 PM, said:

Hash in this instance is being used as a "insert number here" symbol. As in, you'd type the ID of the home block you wanted the turtle to go to in place of the #.

Yes, That's correct.

if turtle.still() then -- this would check if the turtle is moving. If not. the code below runs.
turtle.home(1) -- this would send the turtle back to block 1.
end

#7 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 27 April 2014 - 06:56 PM

View Posticeman11a, on 27 April 2014 - 06:22 PM, said:

How ever when this happens the turtle just sits there and doesn't nothing.

You do realize that you don't really need turtle.still because a Turtle is not moving if you are not calling any turtle move functions (turtle.forward, turtle.up, turtle.down). Also, you can make your code react if the Turtle cannot move for quite some time. I think this is one of the main ideas behind ComputerCraft: you have to fight with problems you struggle with using programming.

View Posticeman11a, on 27 April 2014 - 06:22 PM, said:

Theres no way to program the turtle to say hey, Your not moving, so do some thing.

BombBloke's code does just that: if the Turtle cannot move it tries to dig a block and fight it's way through.

#8 iceman11a

  • Members
  • 68 posts
  • LocationUSA

Posted 27 April 2014 - 09:17 PM

View PostMKlegoman357, on 27 April 2014 - 06:56 PM, said:

View Posticeman11a, on 27 April 2014 - 06:22 PM, said:

How ever when this happens the turtle just sits there and doesn't nothing.

You do realize that you don't really need turtle.still because a Turtle is not moving if you are not calling any turtle move functions (turtle.forward, turtle.up, turtle.down). Also, you can make your code react if the Turtle cannot move for quite some time. I think this is one of the main ideas behind ComputerCraft: you have to fight with problems you struggle with using programming.

View Posticeman11a, on 27 April 2014 - 06:22 PM, said:

Theres no way to program the turtle to say hey, Your not moving, so do some thing.

BombBloke's code does just that: if the Turtle cannot move it tries to dig a block and fight it's way through.

I don't think you have the idea. I mean the turtle stops when an player or animal or player block it's path. When this happens the turtle just stops all together. Even when the program is still running. I tested this and that's what happens. The idea is to check this and take some new action. I would have to build a wall a round the area I have the turlte running. and I didn't want to do that. I guess I'll have too.

#9 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 27 April 2014 - 09:29 PM

View Posticeman11a, on 27 April 2014 - 09:17 PM, said:

I don't think you have the idea. I mean the turtle stops when an player or animal or player block it's path. When this happens the turtle just stops all together. Even when the program is still running. I tested this and that's what happens. The idea is to check this and take some new action. I would have to build a wall a round the area I have the turlte running. and I didn't want to do that. I guess I'll have too.

How did you test this then? All turtle move functions return true if the turtle has moved or false if not:

if turtle.forward() then --// if the turtle moved forward
  print("Turtle has moved forward successfuly")
else --// if the turtle couldn't move forward
  if turtle.detect() then --// if there is a block in front of the turtle
    print("A block is blocking the turtle")
  else --// there are no blocks in front of the turtle, it means that a player is blocking the way
    print("A player or a mob is blocking the turtle")
  end
end

Edited by MKlegoman357, 27 April 2014 - 09:47 PM.


#10 augustas656

  • Members
  • 158 posts

Posted 27 April 2014 - 09:45 PM

View PostMKlegoman357, on 27 April 2014 - 09:29 PM, said:

View Posticeman11a, on 27 April 2014 - 09:17 PM, said:

I don't think you have the idea. I mean the turtle stops when an player or animal or player block it's path. When this happens the turtle just stops all together. Even when the program is still running. I tested this and that's what happens. The idea is to check this and take some new action. I would have to build a wall a round the area I have the turlte running. and I didn't want to do that. I guess I'll have too.

How did you test this then? All turtle move functions return true if the turtle has moved or not:

if turtle.forward() then --// if the turtle moved forward
  print("Turtle has moved forward successfuly")
else --// if the turtle couldn't move forward
  if turtle.detect() then --// if there is a block in front of the turtle
	print("A block is blocking the turtle")
  else --// there are no blocks in front of the turtle, it means that a player is blocking the way
	print("A player or a mob is blocking the turtle")
  end
end

Maybe I've understood you wrong, but if a turtle function fails it will return false, if it succeeds it will return true. It's not always going to be true, for example if you type turtle.forward() this will return true if the turtle actually moves, if it doesn't the function will return false. But from your answer I just read "All turtle move functions return true if the turtle has moved or not"

#11 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 27 April 2014 - 09:50 PM

View Postaugustas656, on 27 April 2014 - 09:45 PM, said:

-snip-

This is what happens when I write long posts with my phone... fixed now.

#12 iceman11a

  • Members
  • 68 posts
  • LocationUSA

Posted 28 April 2014 - 12:16 AM

View PostMKlegoman357, on 27 April 2014 - 09:29 PM, said:

View Posticeman11a, on 27 April 2014 - 09:17 PM, said:

I don't think you have the idea. I mean the turtle stops when an player or animal or player block it's path. When this happens the turtle just stops all together. Even when the program is still running. I tested this and that's what happens. The idea is to check this and take some new action. I would have to build a wall a round the area I have the turlte running. and I didn't want to do that. I guess I'll have too.

How did you test this then? All turtle move functions return true if the turtle has moved or false if not:

if turtle.forward() then --// if the turtle moved forward
  print("Turtle has moved forward successfuly")
else --// if the turtle couldn't move forward
  if turtle.detect() then --// if there is a block in front of the turtle
	print("A block is blocking the turtle")
  else --// there are no blocks in front of the turtle, it means that a player is blocking the way
	print("A player or a mob is blocking the turtle")
  end
end

Just set up a while loop to for forward and block the turtles path. He will stop and not go on. Get the idea. If you setup some thing for the turtle to go forward. Then you have to tell the turtle how far to go. If some thing got in the turtles way. Then the turtle can wait until that person or animal moves out of the way. There's no way to check for this. If you stop the turtle before in completes it program. The turtle stops. You have to play with it to get it to go back to the starting position or destroy it. and start all over.

get the idea.

View Postaugustas656, on 27 April 2014 - 09:45 PM, said:

View PostMKlegoman357, on 27 April 2014 - 09:29 PM, said:

View Posticeman11a, on 27 April 2014 - 09:17 PM, said:

I don't think you have the idea. I mean the turtle stops when an player or animal or player block it's path. When this happens the turtle just stops all together. Even when the program is still running. I tested this and that's what happens. The idea is to check this and take some new action. I would have to build a wall a round the area I have the turlte running. and I didn't want to do that. I guess I'll have too.

How did you test this then? All turtle move functions return true if the turtle has moved or not:

if turtle.forward() then --// if the turtle moved forward
  print("Turtle has moved forward successfuly")
else --// if the turtle couldn't move forward
  if turtle.detect() then --// if there is a block in front of the turtle
	print("A block is blocking the turtle")
  else --// there are no blocks in front of the turtle, it means that a player is blocking the way
	print("A player or a mob is blocking the turtle")
  end
end

Maybe I've understood you wrong, but if a turtle function fails it will return false, if it succeeds it will return true. It's not always going to be true, for example if you type turtle.forward() this will return true if the turtle actually moves, if it doesn't the function will return false. But from your answer I just read "All turtle move functions return true if the turtle has moved or not"

Sorry your not under standing me. If you start a turtle on a route. and then some thing stops it. Then what. Even when the program is still running. The turtle sits and does nothing.

That's my point. The idea is to set up some thing that will tell the turtle to return to its starting point, If some thing like this happen or for any other reason.

As long as every one says out of the way. the turtle runs fine. The problem is that is it gets stopped. nothing happen.

#13 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 28 April 2014 - 12:29 AM

Hey, you know what might be fun? Try putting, say, a fueled melee turtle down, standing in front of it, and running that code I gave you earlier. See whether or not the turtle somehow manages to push past you. You might be surprised.

#14 iceman11a

  • Members
  • 68 posts
  • LocationUSA

Posted 28 April 2014 - 01:01 AM

View PostBomb Bloke, on 28 April 2014 - 12:29 AM, said:

Hey, you know what might be fun? Try putting, say, a fueled melee turtle down, standing in front of it, and running that code I gave you earlier. See whether or not the turtle somehow manages to push past you. You might be surprised.

lol, well the idea is for the tree turtle to do it's job, that's all I wanted to do. I do have a new question for you.

I need to do some thing like this. I just can't get it to work. It tells me that "for" requires a number.

function chek(num)

for i =1, num do
turtle.forward()
end

end

I knwo how to pass values from the funtion to the code. I just can't get it to work the other way a round. Any ideas.

#15 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 28 April 2014 - 01:05 AM

That's fine, assuming you're calling that function in a manner such as "chek(4)".

Eg, like this, or this.

#16 iceman11a

  • Members
  • 68 posts
  • LocationUSA

Posted 28 April 2014 - 02:41 AM

I am, See the sampel below

local function goForward(moveAmount)
for i=1,moveAmount do
while not turtle.forward() do
turtle.attack()
turtle.dig()
end
end
end

I tried some thing like this and it didn't work. I got an error saying that "for" has to be a number.

function check(num)

for i = 1, num do
--do some thing here
end

end

when I tried to do what I have above. I still got the same error.

#17 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 28 April 2014 - 06:35 AM

We'd have to see the rest of the code to tell you for sure what's wrong. I see you have a topic over in Ask a Pro. It would be very helpful for those trying to help you if you were to post the rest of the code.

#18 iceman11a

  • Members
  • 68 posts
  • LocationUSA

Posted 28 April 2014 - 11:50 AM

I did, The rest of the code is there. Just look at the top of the post. I know it's there. I remeber posting it.

#19 sk8mn

  • Members
  • 26 posts

Posted 31 May 2014 - 11:43 PM

while not turtle.forward() do
  turtle.dig()
  turtle.attack()
end

I am writing a program to excavate a underground area and I was wondering weather that would be an easier way because could I do
turtle.forward()
turtle.forward()
Enough times with the other loop rather than
turtle.dig()
turtle.forward()
turtle.dig()
Over and over

Edited by sk8mn, 31 May 2014 - 11:45 PM.


#20 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 01 June 2014 - 01:36 AM

What "other loop"?

Generally speaking, if you're typing out the same function calls over and over in your script in a similar pattern, then you're doing it wrong and should be implementing while/for/repeat loops instead.

The code block I provided makes the turtle attempt to go forward, and if that fails, it will attempt to clear the way in front of it and try again as many times as need be. If there's a stack of gravel in front of it, for eg, then those four lines will clear the lot and move forward once that's done.

You'll find most digging scripts use nearly identical constructs, though the addition of fuel checks is common.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users