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

[RELAYS] XtraTurtle API

Started by negamartin, 31 May 2014 - 01:48 AM
negamartin #1
Posted 31 May 2014 - 03:48 AM
This is the XT API. I keep rewriting it every time I've got to use it, so I decided to polish it up a little and upload it.
Now, even though the name is XtraTurtle, it´s not only for turtles, it´s also got a wireless part which can be used in computers.
Briefly, the features of this API are:
  • Position/orientation tracking and autosaving. Autosave will save every time it moves the position to the disk in case of shutdowns. It can be disabled.
  • Utility functions to face and go to positions
  • Relaying messages with Relays to extend modem range.
  • Calibrating turtle's position and angle if in range of GPS
Downloads:
SpoilerXT API:

V1.2

*Fixed error in position awarement.

Run in CraftOS shell with http enabled:
pastebin get zCXeSLkA xt

V1.1

*Changed relayed_message to give payload as table instead of string.
+Now the XT special functions replace the turtle's. To disable set replaceTurtle to false before loading. Original functions are in turtle.orig.

Run in CraftOS shell with http enabled:
pastebin get T3cgQ5ib xt

V1.0

Run in CraftOS shell with http enabled:
pastebin get VXdZghFj xt



RELAY:

V1.0

Run in CraftOS shell with htp enabled:
pastebin get AHNTNntv startup



So, full documentation:

WIRELESS PART:
Spoiler-xt.replaceRednet()
Will replace the rednet with the XT relaying events(xt.run). rednet.receive() will not work until xt.finish() is called.

-xt.finish()
Will stop xt.run and restore the rednet if xt.replaceRednet was used to start it.

-xt.run()
Needs to be run in parallel with your programs with the parallel API. Wrap your code in a function and then do:

function main()
	--Your code here
end
parallel.waitForAny(main,xt.run);
It will produce the "relayed_message" event for relaying.

-xt.connectModem()
Will connect a modem if connected to the computer. It will prefer wireless ones before wired ones, to allow things such as connecting alot of computers to one wireless modem. The connected modem is stored in xt.modem.

-xt.openChannel(number channel)
Will open the channel in the currently connected modem. It will open a modem if none is open. It will also open the channel for all connected relays.

-xt.transmit(number channel,number replyChannel,table payload)
Will send a TABLE (pay attention, table) through the relays.


TURTLE PART:

Spoiler-xt.fw(amount)
Amount is optional and can be negative, defaults to 1. It will go forward amount spaces. If negative will go the other way amount spaces.

-xt.bk(amount)
Amount is optional and can be negative, defaults to 1. It will go back amount spaces. If negative will go the other way amount spaces.

-xt.up(amount)
Amount is optional and can be negative, defaults to 1. It will go up amount spaces. If negative will go the other way amount spaces.

-xt.dn(amount)
Amount is optional and can be negative, defaults to 1. It will go down amount spaces. If negative will go the other way amount spaces.

-xt.left(amount)
Amount is optional and can be negative, defaults to 1. It will turn left amount times. If negative will go the other way amount spaces.

-xt.right(amount)
Amount is optional and can be negative, defaults to 1. It will turn right amount spaces. If negative will go the other way amount spaces.

-xt.sel(slot)
It will select the turtle´s slot slot.

-xt.locate()
Will try to use the GPS to update the position.
Returns true if it could or false if it couldn’t.

-xt.calibrate()
Will try to use GPS to update facing. Will also update the position in the process.
Returns true if it could update or false if it couldn´t.
Requires an empty space in front or behind the turtle. Also requires 2 fuel units.

-xt.face(nf)
nf is needed. It is the direction to face.
This will face nf the quickliest way possible.

-xt.invertFace(f)
f is optional. Defaults to xt.f.
This will return the facing opposite to the f passed in.
xt.face(xt.invertFace()) will do a turn-around.

-xt.gotoX(nx)
nx is needed. It is the x-coordinate to go.
This will go to the specified X coordinate.

-xt.gotoY(ny)
ny is needed. It is the y-coordinate to go.
This will go to the specified Y coordinate.

-xt.gotoZ(nz)
nz is needed. It is the z-coordinate to go.
This will go to the specified Z coordinate.

Also every function the turtle api has xt will have, just use the same name.
EG: turtle.getFuelLevel==xt.getFuelLevel

You are free to edit, crop, add, modify, delete, etc any part of the API, but please don't delete that little first line "XT by negamartin"
Sorry if my grammar/spelling is not correct, english is not my first language.
Edited on 21 December 2014 - 07:47 PM
SquidDev #2
Posted 31 May 2014 - 10:34 AM
This looks pretty cool, why does it write to so many files though? Could you not serialize all the data and store it to one file? You could also override the turtle API so other programs would automatically work with it.
negamartin #3
Posted 31 May 2014 - 06:14 PM
I tried racing a turtle without XT and one with it and the one without was going a tiny little bit faster. If I would save everything every time the turtle moves it would go alot slower.
I thought about it replacing the turtle api, but somehow I forgot it. I'm adding it.
SquidDev #4
Posted 01 June 2014 - 03:54 PM
I tried racing a turtle without XT and one with it and the one without was going a tiny little bit faster. If I would save everything every time the turtle moves it would go alot slower.
I thought about it replacing the turtle api, but somehow I forgot it. I'm adding it.

I was talking about lines 154-199. Each position is saved to a different file, the t position looks like it is saved to xtdata/y, and so on. Could you not save everything to one file?
negamartin #5
Posted 02 June 2014 - 11:41 PM
The saveXZ/Y/F/S functions are used by the movement functions to save positions. Maybe saving more data at a time would slow down movement.

EDIT: And, btw I posted V1.1, even though it is mostly things i forgotten to do before.
Edited on 02 June 2014 - 09:42 PM