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

KISS APIs - Keep it Simple, Stupid

Started by Bubba, 07 July 2013 - 12:23 PM
Bubba #1
Posted 07 July 2013 - 02:23 PM
Summary


All too often I find myself wanting an API that does what I need it to and nothing more - I don't need a lot of pretty wrapping paper, I just want the meat of the product. Hence, KISS (Keep It Simple Stupid) APIs.

Current Projects


An important note!
Do not use os.loadAPI to load these APIS. Use dofile([filename]) instead.
KISS Navigation
This is an efficient turtle navigation API. It will not make any unnecessary turns, and is only 6 lines in compressed form. You should note that the turtle will navigate first along the x-axis, then along the z-axis, and finally along the y-axis.
Usage:

turtle.goto(x,y,z)
turtle.turnTo([0-3])

Pastebin Compressed -> J2pcaLBW
Pastebin Uncompressed -> ruiUdCcH
GitHub Repository

Full Compressed Code
Spoiler

local t = turtle; local f=t.forward;t.forward=function() while not f() do end end; t.r,t.x,t.y,t.z=0,0,0,0; t.tr=t.turnRight;
t.tl=t.turnLeft; t.tt=function(r) local d=t.r>r and t.r-r or r-t.r; if (d==0) then return true end; if (d==2) then t.tr() t.tr() end;
if (d==1) then if (t.r>r) then t.tl() else t.tr() end end; if (d==3) then if (t.r>r) then t.tr() else t.tl() end end; t.r=r; end;
t.goto=function(x,y,z) local tX,tZ=(x>t.x and 1 or 3), (z>t.z and 0 or 2); local dX,dY,dZ=(x>t.x and 1 or -1), (y>t.y and 1 or -1),
(z>t.z and 1 or -1); while (t.x~=x) do t.tt(tX) t.forward() t.x=t.x+dX end; while (t.z~=z) do t.tt(tZ) t.forward()
t.z=t.z+dZ end; while (t.y~=y) do if (dY==1) then t.up() else t.down() end t.y=t.y+dY end; end; t.turnTo=t.tt;


KISS Button API
This button API is about as simple as it gets, and has no uncompressed version for easy perusal as I wrote it in the compressed form directly.
Usage:

#The button API has only three functions:
button:n(table) # Creates a new button
button:d() # Draws all the buttons
button:cc(int,int) # Checks if a button is clicked, and if so then returns a reference to the button

#The 'button:n()' function takes a table with these arguments:
{
	x=[x-pos];
	y=[y-pos];
	h=[height];
	t=[text];
	c=[color];
}

Pastebin Link-> F5AW5ZD3
Example program
GitHub Repository

Full Compressed Code
Spoiler
local t=term; local scp=t.setCursorPos; local tX,tY = t.getSize(); function cPP(te,x1,x2,y) scp(((x2-x1)/2+x1)-(#te/2),y)
t.write(te) end button={b={}; n=function(s,d) s.b[#s.b+1]=d end;d=function(s)for i=1,#s.b do local b=s.b[i]
for k=0,b.h-1 do t.setBackgroundColor(b.c) scp(b.x,b.y+k) t.write(string.rep(" ",#b.t+4)) end
cPP(b.t,b.x+2,b.x+#b.t+3,(b.y+(b.h/2))) end end; cc=function(s,x,y) for k=1,#s.b do local b=s.b[k] if
(x>=b.x and x<=(b.x+#b.t+3) and y>=b.y and y<=b.y+b.h-1) then return b end end end;}

Projects in the Works

Your name here: Your project here

Currently, I do not have any more KISS APIs in the works, but if you have any contributions or suggestions then I would be more than happy to take them!

General Q/A
Do you write all the code by hand?
Yes.
Even the compressed code? Yes.
Why do you compress the code? Mainly because I think it's fun. I know, I have a sick and twisted mind ;)/>
I found a bug. Can I take it upon myself to fix the bug and then submit it to your GitHub repository for a merge? Certainly! I would be glad to accept any contributions you may have.
I have a program that would fit into this repository nicely… It would be awesome if KISS APIs turned into a community project. If you have a program that does things efficiently and is simple to use, by all means submit it via the GitHub repository link below and notify me through either PM or a post in this thread.

GitHub Repository
diegodan1893 #2
Posted 08 July 2013 - 07:39 AM
The full compressed code in Kiss Navigation is from Kiss Button and the code in Kiss Button is from Kiss Navigation
Bubba #3
Posted 08 July 2013 - 11:43 AM
The full compressed code in Kiss Navigation is from Kiss Button and the code in Kiss Button is from Kiss Navigation

Oops! Thanks for pointing that out.
Symmetryc #4
Posted 08 July 2013 - 11:53 AM
Nice, I love it :)/>. Hopefully everyone can pitch in and expand this into something that is very useful.
oedze #5
Posted 22 July 2013 - 01:19 PM
i like it, but if i am playing, i wont use the navigation program, just because i dont want to get my base destroyed, can you make the program so its first goes up to like y=90(maby a config) and then goes x and z and then goes down/up to the right point???

just a feature i want to see :)/>
NOTUSEDPLEASEDELETE #6
Posted 02 August 2013 - 09:29 PM
Great work on your project!