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

MackCode [1.0]

Started by Mackan90096, 14 June 2013 - 04:30 AM
Mackan90096 #1
Posted 14 June 2013 - 06:30 AM
Hi! I've had this project going on for a little while now..

My first API.

Download in-game: pastebin get VXCPhReN MackCode

Load it in your program with:

os.loadAPI("MackCode")

Functions:

MackCode.cls() this will clear the screen.
MackCode.txtCol(col) this will set the text color to "col"
MackCode.bgCol(col) same as above but background
MackCode.bgcolc(col) same as bgCol but will also clear the screen.
MackCode.getVer() Prints the version
MackCode.saveFile(filename, mode, text) will open the file "filename" in mode "mode" and write "text".
MackCode.del(filename) will delete "filename"
MackCode.size() Gets the size of screen in w, h
MackCode.cursor(x, y) sets the cursor position to x, y
MackCode.centerPrint(string, x, y) prints the string in the center of screen on y level. For best results make x 2


Leave what you think and suggestions and stuff below.

Thanks // Mackan90096
Kingdaro #2
Posted 14 June 2013 - 08:35 AM
This is pretty pointless. A lot of these functions are just wrappers, or simplify very easy processes. Your centerPrint() function won't work, because it tries to use the string API while having "string" as a variable, and your size() function shouldn't set global variables, as not everyone uses w and h for screen size. Lastly, this is mostly my opinion, but I don't think "easter egg" functions belong in APIs.
theoriginalbit #3
Posted 14 June 2013 - 08:42 AM
and the "easter egg" is recursive so would crash after some time too.

EDIT:

Also what is the point of supplying an X for a centre print function?

a better centre print:

function centerWrite( msg, y )
  local sw, sh = term.getSize()
  term.setCursorPos( math.floor( ( sw - #msg ) / 2 ) + ( #msg % 2 == 0 and 1 or 0 ), y or sh / 2 )
  write( msg )
end

function centerPrint( msg, y )
  centerWrite( msg..'\n', y )
end
Edited on 14 June 2013 - 06:45 AM
Kingdaro #4
Posted 14 June 2013 - 08:56 AM
The math there is a bit complicated. Using math.ceil rather than adding 1 if the length of the string is positive is much simpler.


function centerWrite( msg, y )
  local sw, sh = term.getSize()
  term.setCursorPos( math.ceil( ( sw - #msg ) / 2 ), y or sh / 2 )
  write( msg )
end
theoriginalbit #5
Posted 14 June 2013 - 09:01 AM
The math there is a bit complicated. Using math.ceil rather than adding 1 if the length of the string is positive is much simpler.
I remember math.ceil didn't work in 1 particular case, can't remember what it was now, but I remember I picked the +%2 for a reason.
jesusthekiller #6
Posted 14 June 2013 - 12:20 PM
Instead of math.ceil do

local function ceil(n)
  return n - n%1
end
Broken with n < 0 tho…
ElvishJerricco #7
Posted 14 June 2013 - 01:22 PM
Instead of math.ceil do

local function ceil(n)
  return n - n%1
end
Broken with n < 0 tho…

What's the difference between this and math.floor?
jesusthekiller #8
Posted 14 June 2013 - 02:43 PM
IDK, ask TOBIT. He says that math.ceil is broken… :P/>
Mackan90096 #9
Posted 14 June 2013 - 04:09 PM
… I'm confused now…
brett122798 #10
Posted 14 June 2013 - 04:23 PM
I do agree that this API is semi-useless. There are some functions that are shortcuts, but they're mostly copies of the term API. I say, if you want to make an API everyone will love, work on making shortcuts for commonly used and long things.
jesusthekiller #11
Posted 15 June 2013 - 12:32 AM
Agreed :P/>
ElvishJerricco #12
Posted 15 June 2013 - 02:22 AM
IDK, ask TOBIT. He says that math.ceil is broken… :P/>/>

Math.ceil isn't broken. Like most floating point math operations, there's one specific number that doesn't work with math.ceil. And you didn't write a working ceil function. You wrote a working floor function, which is exactly the opposite. Also it doesn't break when you go to negatives, it works just as standard math.floor works.
jesusthekiller #13
Posted 15 June 2013 - 12:44 PM
Herp derp true :P/>


local function ceil(n)
  return n+1-n%1
end

It's ceil now ^_^/>
DeweySalt #14
Posted 16 June 2013 - 09:36 PM
Most of these functions already exist in the term API
Dave-ee Jones #15
Posted 20 June 2013 - 02:19 AM
Most of these functions already exist in the term API

^What he said. At least I have functions that do not exist in any other default API in QuickTils. :P/>