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

Programmatically Determine A Turtle's Type

Started by SierraNine, 25 July 2013 - 09:03 AM
SierraNine #1
Posted 25 July 2013 - 11:03 AM
There does not seem to be anything in the turtle nor peripheral APIs that would allow you to determine the type of turtle.

I'm trying to save me from myself and prevent certain turtle from running certain programs.

For example, if I can check the turtle type, I can allow only WrenchTurtles( id: 201) to attack Redstone Energy Cells in my automated REC charging script. Bad Things™ happen when I use a mining turtle by mistake.
SierraNine #2
Posted 25 July 2013 - 12:31 PM
There does not seem to be anything in the turtle nor the peripheral APIs that would allow you to determine the type of turtle.

I'm trying to save me from myself and prevent certain turtles from running certain programs.

For example, if I can check the turtle type, I can allow only WrenchTurtles( id: 201) to attack Redstone Energy Cells in my automated REC charging script. Bad Things™ happen when I use a mining turtle by mistake.
jllloyd #3
Posted 25 July 2013 - 12:58 PM
I'm confused why can't you just make sure you place the right turtle, or only put the program on the specific turtle (why would it be on the wrong one in the first place)
SierraNine #4
Posted 25 July 2013 - 01:08 PM
The human is usually the weakness part of any system.

You mean you've NEVER pastebin'd in the wrong script?

What about your 7 or 13 year old sons? :)/>

What about the kid half way around the world using your paste bin scripts?

It would be nice, if the programmer were to able to alert the user, total noob or computercraft guru, that they're asking the wrong kind of turtle to do a specific task.

I can ( and most often ) do a fuel check at the start of a turtle script, why not a turtle type check as well?
panicmore #5
Posted 25 July 2013 - 01:18 PM
The human is usually the weakness part of any system.

You mean you've NEVER pastebin'd in the wrong script?

What about your 7 or 13 year old sons? :)/>

What about the kid half way around the world using your paste bin scripts?

It would be nice, if the programmer were to able to alert the user, total noob or computercraft guru, that they're asking the wrong kind of turtle to do a specific task.

I can ( and most often ) do a fuel check at the start of a turtle script, why not a turtle type check as well?
try using a function only available to that turtle, that's best I can think of other than that just asking the user to double check? Mining turtles also don't break blocks unless issued the dig() command so that shouldn't be a problem. There's not a lot of turtles that can break things when given the attack() command if any.
GopherAtl #6
Posted 25 July 2013 - 01:20 PM
you can't identify the 5 basic tools (pick, axe, sword, shovel, hoe), but peripherals can be detected easily, just do peripheral.getType("right") and, if it's using a 2nd peripheral instead of a basic tool, peripheral.getType("left")

Also, this should be in AaP. Reporting for move.
theoriginalbit #7
Posted 25 July 2013 - 01:26 PM
Well there is no way at the moment. Sadly tools are not peripherals for the turtles, so we have no way of detecting… However any peripherals added to turtles by MiscPeripherals or the such there is a solution:

Note: This solution does not work for all turtles! Only peripheral turtles, it cannot detect tools (i.e. wrenches, tree taps, shears, etc)

Using the peripheral API we can use the function `getType` to determine the type of peripheral… for example the following code checks to see if the turtle has a crafting table


if not (peripheral.getType("left") == "workbench" or peripheral.getType("right") == "workbench") then
  error("Turtle is not a crafty turtle!", 0)
end

obviously the easier solution for the above is


if not turtle.craft then
  error("Turtle is not a crafty turtle", 0)
end

Obviously the logic from the first one can be continued for any peripheral…. example: MiscPeripheral's Inventory Turtle will contain a peripheral on the left or right with the type "inventory".

The second logic can be followed for very few turtles as to check existence of a function you must first wrap the peripheral, meaning you know it's there… of course you could do something like this (assuming a chatbox turtle):


local left = peripheral.wrap("left") or {} --# creating the table is important to not have it error on line 3
local right = peripheral.wrap("right") or {}
if not (left.say or right.say) then
  error("No chatbox attached", 0)
end
but again, the above method would just be easier to use the first way of just checking the peripheral's existence.

And of course sadly the problem with attempting to check the tools, lots of them use turtle.attack() to interact and some use turtle.dig() too. So since they have no unique function there is no sure way to check and know what they are.
SierraNine #8
Posted 25 July 2013 - 01:33 PM
try using a function only available to that turtle, that's best I can think of other than that just asking the user to double check? Mining turtles also don't break blocks unless issued the dig() command so that shouldn't be a problem. There's not a lot of turtles that can break things when given the attack() command if any.


I understand I was less than crystal clear when I flippantly said "attacking" in the original post.

WrenchTurtles use the dig command, same as MiningTurtles to interact with the appropriate blocks.

When recovering an REC, a WrenchTurtle is given the turtle.dig() command, and usually, ideally, will have said REC in it's inventory to do something else with.

A MiningTurtle will take the exact same dig command and destroy said Redstone Energy Cell, and lose any stored energy in the process.

A little type checking would go along way towards assisting a conscientious programmer to help the end user, even if it is himself, avoid making such a mistake.

There are many other such examples. I really don't understand why a little bit of conditions checking is being so vehemently opposed.
SierraNine #9
Posted 25 July 2013 - 01:36 PM
Well there is no way at the moment. Sadly tools are not peripherals for the turtles, so we have no way of detecting… However any peripherals added to turtles by MiscPeripherals or the such there is a solution:

Note: This solution does not work for all turtles! Only peripheral turtles, it cannot detect tools (i.e. wrenches, tree taps, shears, etc)

…snip…

Many thanks. I thought I was correct in the lack of a testable condition.

I agree with your examples concerning peripherals, and that it will not solve the base question, but certainly can in some edge cases.
Lyqyd #10
Posted 25 July 2013 - 01:56 PM
Moved to Ask a Pro and merged.
GopherAtl #11
Posted 25 July 2013 - 05:33 PM
I thought "tool" peripherals were still peripherals, even if they had no methods? :checks: And I was wrong. Sorry about that!
theoriginalbit #12
Posted 25 July 2013 - 08:52 PM
Many thanks. I thought I was correct in the lack of a testable condition.

I agree with your examples concerning peripherals, and that it will not solve the base question, but certainly can in some edge cases.
No problems.

Yeh and that is the exact reason I outlined it, because then where appropriate you can at least limit it…

I thought "tool" peripherals were still peripherals, even if they had no methods? :checks: And I was wrong. Sorry about that!
Yeh I originally thought that too, thought all of MiscPeripehrals would work, decided to check first, damn good thing I did xD