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

CC System API

Started by LicenseTooThrill, 07 August 2015 - 12:05 AM
LicenseTooThrill #1
Posted 07 August 2015 - 02:05 AM
I was wondering if anyone knew the language that the APIs are written in so I can start writing my own. It seems different than Lua - like a much lower-level code.

I just have some functions I'd like to integrate into the OS so that I can run something like|

"fwd 10" or "fwd * "

straight from the console to get the result,

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

That way I can control the turtle from the command line with little work. It makes my code more concise and faster to type while allowing me to control the turtles without scripts.

For example, an API program that would advance the turtle forward until it hit a wall, or advance it as far as you'd like:
(can be used in both command line and a program)
———————————-
function fwd(num)
if num >=1 then
for i = 1, num do
turtle.forward()
end
else
while not turtle.detect() do
turtle.forward()
end
end
end
–[[omitting input checks for the purpose of clarity]]
input = read()
fwd(input)\
———————————–

then, at the command line, I can type 'fwd()' to move the turtle as far forward as possible, or 'fwd(5)' to advance it 5 blocks.

I have a long list of function like this that I made for cleaner code such as,

bck()
fwd()
rite
left
spin
lstrafe
rstrafe
fly
fall
clr (clear)
scan (enter a block)
crawl (strafes along the wall and scans)
return(x,y) – input whatever block counters you define at the beginning, and it returns to the start

I just like to reuse pieces of code like these so I can focus more time on engineering and less time on actual typing.
I'm hoping to implement these functions and more into a higher level turtle API for personal use, although I'd be thrilled to share it on the forums if people show an interest.

And, no, I don't want to use "import" all the time. I'd like to create a resource pack that plops it directly in the main APIs so that every function is accessible all of the time.
Obviously, running fwd(5) from command line would look like : fwd 5, but that's nothing copy and paste can't fix.
KingofGamesYami #2
Posted 07 August 2015 - 02:50 AM
Actually, most APIs are written in Lua. There are a few java-side ones, like http, turtle, and peripheral, but all user-created APIs are written in lua.

APIs are just normal programs that do nothing but define variables.
Bomb Bloke #3
Posted 07 August 2015 - 08:42 AM
Indeed - you yourself can only make/modify ComputerCraft APIs using Lua.

http://www.computercraft.info/forums2/index.php?/topic/15052-api-basics/
Astrophylite #4
Posted 07 August 2015 - 06:07 PM
Quick comment, can you please put code in the CODE tags. Just so it looks nicer ;)/>
Anyway, I have coded a turtle API that is available for download, it is only usable in programs at the moment, but I can whip up a quick program that will take all them commands for you.

EDIT: API Link: http://pastebin.com/w7DnM2Rt
EDIT2: So my program that I am about to give you works, download that API and put it into the directory "/apis/" with the name "turtle"
EDIT3: Nice and coded! Download it here: http://pastebin.com/JEs97fAU
Edited on 07 August 2015 - 04:27 PM