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

Tortoise API (better turtles)

Started by HDeffo, 27 February 2016 - 04:06 PM
HDeffo #1
Posted 27 February 2016 - 05:06 PM
Yet another API designed to make turtle functions more robust. Includes functionality improvements on native functions, additional functions, facing and position memorization, and much more



pastebin get 34Lb30Dj tortoise





documentation
Improved functionsAll native functions will continue working how they did previously. All changes are merely additional and optional functionality built on top of each function.

forward, back, up, and downAll of these now optionally take a number telling how far for the turtle to go. If given a distance and the turtle is unable to move the full distance it will return false, distance moved

equipLeft and equipRightCan now optionally be given an argument to tell what slot to equip. This can be the slot number or the name of the item. putting equipLeft("minecraft:diamond_sword") will equip a diamond sword if the turtle has one.


Any function that takes a slot number as an argumentCan now also take an item name as an argument instead syntax is "mod:item" will select the slot that item is in if available. You can also pass "empty" which will select the first empty slot in the turtle's inventory.


drop and placeNow accept an optional slot argument as a second argument. Like all other functions this can also be an item's name


refuelnow takes an optional slot or name in the second argument

(please let me know if I forgot to put anything here. I kind of forget all the things I changed)
New functions
strafeLeftthe turtle will move to the left either one block or the number passed to the function as an argument.


strafeRightthe turtle will move to the right either one block or the number passed to the function as an argument.

reverseturtle will turn left twice. Saves you one whole line of code!

compareUpTo and compareDownToup and down variations of native function compareTo


hasreturns slot number if the item name passed as an argument exists in the turtles inventory otherwise returns false


transferFrominverse of transferTo accepts the same arguments.


transferBetweenaccepts arguments slot from, slot to, and quantity. moves items from the first slot and to the second passed as arguments.


forwardForce, backForce, upForce, downForce, strafeLeftForce, strafeRightForcesame as their corresponding functions but will always move. Will break blocks and kill mobs in the turtles way until it reaches the distance given.


undoundoes that last movement by the turtle. If given a number as an argument then it will undo that many movements.


undoAllundoes all movements in the turtle's log.


clearLogclears all past movements. Used with undo and undoAll


gotogoes to position x, y, z. breaks all blocks in the way

digAllmines all blocks that are together of the same block name. Great for trees and ore veins.
PersistanceThe turtle will now remember its facing, position, inventory, equipment and past movements, even through restarts. the corresponding functions to get all of these are below.


tortoise.pos() --#position
tortoise.getLeft() --#whats in left hand
tortoise.getRight() --#whats in right hand
tortoise.getEquip() --#whats in both hands
tortoise.facing() --#direction turtle is facing
tortoise.inventory() --#all items in the turtle's inventory

I will separate the most important function here.

Using any of the native turtle functions will mess up some of the persistent data of tortoise. To prevent this use the function

tortoise.becomeTurtle()
this will load all tortoise functions as turtle functions and replace the native API.

The only reason this doesn't have more functions is because I don't know what else to add. If theres something else you want please let me know :D/>

I will not add pathfinding. That requires large algorithms and several functions on its own. Something like that deserves its own API entirely.





Credit to kingofgamesyami as this started out as a simple rewrite of LAMA
Edited on 20 November 2016 - 12:57 PM
Diaqon #2
Posted 29 February 2016 - 03:46 PM
Nice API overall, but your "tortoise.becomeTurtle()" function seems to break refueling the turtle
Unable to use default "refuel" program, and can't refuel using lua interpreter either.
HDeffo #3
Posted 29 February 2016 - 07:55 PM
hmm it was working when I tested it last. I will see if I can fix that thanks Diaqon!
HDeffo #4
Posted 29 February 2016 - 08:10 PM
Bug fixed :D/> …..I messed up a single if statement in a previous update and broke everything >.>
HDeffo #5
Posted 01 March 2016 - 03:33 AM
just added a new function digAll. Made specifically for things like trees and ore veins!
Lupus590 #6
Posted 01 March 2016 - 10:38 AM
This sounds like something which could have a lot of use in hive. What license are you releasing this under? http://choosealicense.com/
HDeffo #7
Posted 01 March 2016 - 06:17 PM
check my signature, it is my attempt to avoid the whole licensing thing. I am writing this for a game so I don't see a reason to get all ownership conscious of it. This counts as an API. If a license is really important just treat it like MIT
HDeffo #8
Posted 01 March 2016 - 06:34 PM
New Update
  • new undoForce and undoAllForce functions
  • all functions which could previously take a name can now also take damage as another variable. This lets you specify even different types of logs and such.
  • digAll, digAllUp, digAllDown are all slightly faster by at least 25% (in some cases more so)
Edited on 02 March 2016 - 04:03 AM
taz4hvn #9
Posted 05 July 2016 - 11:28 AM
Maybe you can help, I'm new to CC, lua and even more APIs !
I made:

"pastebin get HC5SwGdv tortoise" in my turtle, got it

I made a simple script to test:

os.loadAPI("tortoise")
tortoise.forwardForce()
tortoise.backForce()


forward worked and then I get a:

tortoise:218: attempt to index ? (a nil value)

Did I miss smthg ?
Lupus590 #10
Posted 05 July 2016 - 01:26 PM
-snip-

can we see your code?

edit: note to self, read
Edited on 05 July 2016 - 06:11 PM
taz4hvn #11
Posted 05 July 2016 - 05:03 PM
Thanks for the answer.

There's no more code to show you really, I made a simple script with these three lines:

os.loadAPI("tortoise")
tortoise.forwardForce()
tortoise.backForce()


and got a:

tortoise:218: attempt to index ? (a nil value)

I checked the file, this line is in the tortoise.backForce() function.
Lupus590 #12
Posted 05 July 2016 - 08:22 PM
edit the tortoise api and add this between lines 217 and 218


--#this is the new 218 line
if tTurtleData == nil then error(" tTurtleData is nil")
if tFacing == nil then error("tFacing is nil")

tell me which of the error messages turns up
Gorzoid #13
Posted 17 July 2016 - 03:10 PM
Thanks for the answer.

There's no more code to show you really, I made a simple script with these three lines:

os.loadAPI("tortoise")
tortoise.forwardForce()
tortoise.backForce()


and got a:

tortoise:218: attempt to index ? (a nil value)

I checked the file, this line is in the tortoise.backForce() function.
Typo in line 761 of his code, change sFaceing to sFacing
Edited on 17 July 2016 - 01:11 PM
HDeffo #14
Posted 05 August 2016 - 01:15 PM
Oops haha thought I fully tested all functions before uploading. I'll update and correct this bug in a few hours when I'm home
Mantas #15
Posted 08 August 2016 - 10:45 AM
Simple to use, and extremely useful. I don't think I'm ever using a turtle again without this API.
block12345 #16
Posted 12 October 2016 - 11:04 PM
got an error but fixed it by putting south instead of South now it works fine :)/> cool api love it
Edited on 12 October 2016 - 09:13 PM
Sewbacca #17
Posted 13 October 2016 - 06:59 PM
I wanted to code something like that, but now i don't have to =)
One suggestion:
I would love turtle.redirect etc. functions.
I will, no i am going to use your API =)
Sewbacca
Erzengel #18
Posted 16 November 2016 - 02:52 AM
Bug: If you have turtles set to infinite fuel, iFuel in data is set to "unlimited" and thus anywhere that iFuel is treated like a number the program crashes. For example, I initially get "tortoise:193: attempt to compare number with string expected, got number". I modified the code on my side to add the following function:

function enoughFuel(iNum)
if tTurtleData.iFuel == "unlimited" or iNum <= tTurtleData.iFuel then
  return true
else
  return false
end
end
and replaced every comparison against tTurtleData.iFuel with "enoughFuel(iNum)" or "not enoughFueld(iNum)" and now it works wonderfully.
dropdead #19
Posted 19 November 2016 - 04:42 PM
First: How do I know what Minecraft/CC version to use? It is hardly mentioned so I can't help the feeling this is common knowledge.

I don't really understand how to load this up. I'v tried it with MC 1.8.9 and CC 1.78.
I got the program connecting to pastebin (pastebin get HC5SwGdv tortoise) and tried

os.loadAPI("tortoise")
and it asked for the coordinates. After that I tried
tortoise.becomeTurtle()
but this error occured:
lua:1: attempt to index ? (a nil value)

Am I missing any other files? Any tips how to load it properly?

Maybe I don't understand something essential about using programs for the turtles. Lots of stuff just do not work and throws colorful error messages at me. I understand a bit of Java, PHP, Perl and other languages but Lua goes straight over my head because I either don't get the error messages at all or I look at the code and see nothing wrong anyway.

Help please?

P.S. If I sound in anyway offensive or my grammar/spelling is atrocious: English is not my native language.
HDeffo #20
Posted 19 November 2016 - 07:39 PM
-snip-

Oh gosh I haven't been to this website in ages and it looks like I picked a good time! A long time back there was a rollback on the forums and it appears this was affected. The version posted is broken and should've been replaced by a fixed version. :3 I'll upload the correct version by tomorrow morning for you
dropdead #21
Posted 19 November 2016 - 09:16 PM
-snip-

Oh gosh I haven't been to this website in ages and it looks like I picked a good time! A long time back there was a rollback on the forums and it appears this was affected. The version posted is broken and should've been replaced by a fixed version. :3 I'll upload the correct version by tomorrow morning for you

Thank you so much, that's great!
And I'm wondering what was stopping me this time. Like I said damn Lua, don't know why it is somewhat of a mystery to me… :blink:/> I really need every help I can get. :)/>
HDeffo #22
Posted 19 November 2016 - 09:51 PM
Honestly that error is likely a fault on my part not yours and I'm almost positive I remember where I just can't look yet cause I'm working
dropdead #23
Posted 20 November 2016 - 12:38 AM
That's no problem. I guess were in different time zones anyway. I'm overdue for bed - it's 1:36 AM and I can't see straight anymore. Take your time, I am glad you happened to be here at all if you didn't visit the forum for quite some time.
HDeffo #24
Posted 20 November 2016 - 01:58 PM
The program should be updated and fixed. I havent had a free moment to test it yet and see what damage the rollback had done and in 1129 lines its entirely possible something else is wrong but the attempt to index issue is fixed
dropdead #25
Posted 20 November 2016 - 04:08 PM
Great, thank you so much. I'll try my luck with this version.
I guess nothing was wrong how I tried to get it to work so I'll just do the same steps again.
dropdead #26
Posted 20 November 2016 - 06:36 PM
I meant to edit my post but obviously it's not just Lua I don't get. <headdesk>

I've tried and although it installed, took my coordinates and didn't throw an error after


turtoise.becomeTurtle()

it still doesn't seem to recognize any of the new commands. If I change to Lua and try a command starting with tortoise.do_sth() it doesn't do anything but say

lua:1: attempt to index ? (a nil value)

As far as I can tell it doesn't get that tortoise means itself. The old commands still work like turtle.do_sth().
And of course I didn't tell it do_sth() but a valid command. :D/>

If you feel like looking at it again and again and maybe fix it I will check for an answer. No hard feelings if you don't want to or don't have the time.

Thanks!
Edited on 20 November 2016 - 05:37 PM