Ill dump some of my core.cs which I copy paste between all my add-ons. I havent really documented the functions too well, some of them could be seriously optimized, and some of them might be broken.
function brickAngleToTransform(%angleID) {
switch(%angleID % 4) {
case 0: return "1 0 0 0";
case 1: return "0 0 1 90";
case 2: return "0 0 1 180";
case 3: return "0 0 -1 90";
}
}
function printNameToID(%printname) { //Credits to DrenDran from BVSS
if(%printname $= "" || %printname $= " ")
return 0;
%slashPos = strpos(%printname,"/");
%end = getSubStr(%printname, %slashPos, strlen(%printname)-%slashPos);
return $printNameTableLetters[%end];
}
function printIDToName(%id) { //Credits to Port, from forums
%texture = getPrintTexture(%id);
%path = getSubStr(%texture, 14, strLen(%texture));
%path = getSubStr(%path, 0, strPos(%path, "/"));
%path = getSubStr(%path, 0, strPos(%path, "_"));
return %path @ "/" @ fileBase(%texture);
}
function mLerp(%a, %b, %t) {
return %t * (%b-%a) + %a;
}
function vectorLerp(%init, %end, %t) {
return vectorAdd(%init, vectorScale(vectorSub(%end, %init), %t));
}
function vectorSlerp(%init, %end, %t) { //Thanks wikipedia! Returns spherical interpolation of two vectors
%ang = vectorAngleBetween(%init, %end);
//vecA * (sin((1-t)*a)/sin(a)) + vecB * (sin(t*a)/sin(a))
return vectorAdd(vectorScale(%init, mSin((1-%t) * %ang) / mSin(%ang)), vectorScale(%end, mSin(%t * %ang) / mSin(%ang)));
}
function vectorAngleBetween(%vec1, %vec2) { //Returns the angle between two vectors in radians
return mACos(vectorDot(%vec1, %vec2)/(vectorLen(%vec1) * vectorLen(%vec2)));
}
function vectorComponent(%vec1, %vec2) { //Returns the component of vec1 in the direction of vec2
return vectorDot(%vec1, vectorNormalize(%vec2));
}
function vectorProjection(%vec1, %vec2) { //Returns the vector projection of %vec1 in the direction of %vec2
return vectorScale(vectorNormalize(%vec2), vectorComponent(%vec1, %vec2));
}
function vectorRejection(%vec1, %vec2) { //Returns the vector rejection of vec1 from vec2
return vectorSub(%vec1, vectorProjection(%vec1, %vec2));
}
function eulerToVector(%ang) {
if(getWordCount(%ang) == 2) //Support for giving pitch and yaw, but not roll.
%ang = getWord(%ang, 0) SPC "0" SPC getWord(%ang, 1);
%yaw = mDegToRad(getWord(%ang, 2));
%pitch = mDegToRad(getWord(%ang, 0));
%x = mSin(%yaw) * mCos(%pitch) * -1;
%y = mCos(%yaw) * mCos(%pitch);
%z = mSin(%pitch);
return %x SPC %y SPC %z;
}
function axisToVector(%ang) {
return eulerToVector(axisToEuler(%ang));
}
function vectorToEuler(%vec) {
%vec = vectorNormalize(%vec);
%yaw = mRadToDeg(mATan(getWord(%vec, 0) * -1, getWord(%vec, 1)));
%pitch = mFloatLength(mRadToDeg(mATan(getWord(%vec, 2), vectorLen(getWords(%vec, 0, 1)))), 3);
return %pitch SPC "0" SPC %yaw;
}
function vectorToAxis(%vec) {
return eulerToAxis(vectorToEuler(%vec));
}
function vectorRotateVector(%vec1, %vec2) {
return vectorRotateAxis(%vec1, vectorToAxis(%vec2));
}
function vectorRotateEuler(%vec, %euler) {
return vectorRotateAxis(%vec, eulerToAxis(%euler));
}
function vectorRotateAxis(%vec, %axis) { //Epic function found online. Credits to Blocki <3. Id also like to thank Zeblote for finding this for me <3
%u["x"] = getword(%axis,0);
%u["y"] = getword(%axis,1);
%u["z"] = getword(%axis,2);
%angl = getword(%axis,3) * -1;
%cos = mcos(%angl);
%sin = msin(%angl);
%a[1,1] = %cos + (%u["x"] * %u["x"] * (1 - %cos));
%a[1,2] = (%u["x"] * %u["y"] * (1 - %cos)) - (%u["z"] * %sin);
%a[1,3] = (%u["x"] * %u["z"] * (1 - %cos)) + (%u["y"] * %sin);
%a[2,1] = (%u["y"] * %u["x"] * (1 - %cos)) + (%u["z"] * %sin);
%a[2,2] = %cos + (%u["y"] * %u["y"] * (1 - %cos));
%a[2,3] = (%u["y"] * %u["z"] * (1 - %cos)) - (%u["x"] * %sin);
%a[3,1] = (%u["z"] * %u["x"] * (1 - %cos)) - (%u["y"] * %sin);
%a[3,2] = (%u["z"] * %u["y"] * (1 - %cos)) + (%u["x"] * %sin);
%a[3,3] = %cos + (%u["z"] * %u["z"] * (1 - %cos));
%x = getWord(%vec, 0);
%y = getWord(%vec, 1);
%z = getWord(%vec, 2);
%newx = (%a[1,1] * %x) + (%a[1,2] * %y) + (%a[1,3] * %z);
%newy = (%a[2,1] * %x) + (%a[2,2] * %y) + (%a[2,3] * %z);
%newz = (%a[3,1] * %x) + (%a[3,2] * %y) + (%a[3,3] * %z);
%pos = %newx SPC %newy SPC %newz;
return %pos;
}
function isBL_IDAdmin(%id) {
//Check if player is online and is admin
if(isObject(%client = findClientByBL_ID(%id))) {
if(%client.isAdmin || %client.isSuperAdmin) //Check super admin now rather than calling the other function for efficiency
return true;
else
return false; //If theyre online and theyre not admin, assume theyre not supposed to be admin
}
//Check auto admin list
%count = getWordCount($Pref::Server::AutoAdminList);
for(%i=0; %i < %count; %i++) {
%guy = getWord($Pref::Server::AutoAdminList, %i);
if(%guy == %id)
return true;
}
return isBL_IDSuperAdmin(%id);
}
function isBL_IDSuperAdmin(%id) {
//Check if player is online and is super admin
if(isObject(%client = findClientByBL_ID(%id))) {
if(%client.isSuperAdmin)
return true;
else
return false;
}
//Check auto super admin list
%count = getWordCount($Pref::Server::AutoSuperAdminList);
for(%i=0; %i < %count; %i++) {
%guy = getWord($Pref::Server::AutoSuperAdminList, %i);
if(%guy == %id)
return true;
}
return isBL_IDHost(%id);
}
function isBL_IDHost(%id) { //Credits to Greek2me for this, on forums.
return (%client.isLocalConnection() || ($Server::LAN && $Server::Dedicated) || %client.getBLID() $= getNumKeyID());
}
function vectorAddMulti(%a, %b) { //Add up to infinite dimensional vectors
%lowest = getWordCount(%a) < getWordCount(%b) ? getWordCount(%a) : getWordCount(%b);
for(%i=0; %i < %lowest; %i++)
%vec = %vec SPC getWord(%a, %i) + getWord(%b, %i);
return trim(%vec);
}
function vectorSubMulti(%a, %b) {
%lowest = getWordCount(%a) < getWordCount(%b) ? getWordCount(%a) : getWordCount(%b);
for(%i=0; %i < %lowest; %i++)
%vec = %vec SPC getWord(%a, %i) - getWord(%b, %i);
return trim(%vec);
}
function vectorScaleMulti(%a, %b) {
for(%i=0; %i < getWordCount(%a); %i++)
%vec = %vec SPC getWord(%a, %i) * %b;
return trim(%vec);
}
function vectorMultiplyMulti(%a, %b) {
%lowest = getWordCount(%a) < getWordCount(%b) ? getWordCount(%a) : getWordCount(%b);
for(%i=0; %i < %lowest; %i++)
%vec = %vec SPC getWord(%a, %i) * getWord(%b, %i);
return trim(%vec);
}
function vectorDivideMulti(%a, %b) {
%lowest = getWordCount(%a) < getWordCount(%b) ? getWordCount(%a) : getWordCount(%b);
for(%i=0; %i < %lowest; %i++)
%vec = %vec SPC getWord(%a, %i) / getWord(%b, %i);
return trim(%vec);
}
function vectorLenMulti(%a) {
%wordCount = getWordCount(%a);
for(%i=0; %i < %wordCount; %i++)
%len += mPow(getWord(%a, %i), 2);
return mPow(%len, 0.5);
}
function vectorDistMulti(%a, %b) {
return vectorLenMulti(vectorSubMulti(%a, %b));
}
function VectorNormalizeMulti(%a) {
return vectorScaleMulti(%a, 1/vectorLenMulti(%a)); //Lazy method
}
function vectorFloatLength(%vec, %float) {
return mFloatLength(getWord(%vec, 0), %float) SPC mFloatLength(getWord(%vec, 1), %float) SPC mFloatLength(getWord(%vec, 2), %float);
}
function getClosestPaintColor(%rgba) {
%prevdist = 100000;
%colorMatch = 0;
for(%i = 0; %i < 64; %i++) {
%color = getColorIDTable(%i);
if(vectorDist(%rgba,getWords(%color,0,2)) < %prevdist && getWord(%rgba,3) - getWord(%color,3) < 0.3 && getWord(%rgba,3) - getWord(%color,3) > -0.3) {
%prevdist = vectorDist(%rgba,%color);
%colormatch = %i;
}
}
return %colormatch;
}
function mMod(%num, %a) { //Supports decimals unlike %
return ((%num / %a) - mFloor(%num / %a)) * %a;
}
function mFloorTo(%num, %a) { //Rounds down %num to the nearest %a
return mFloor(%num / %a) * %a;
}
function mCeilTo(%num, %a) {
return mCeil(%num / %a) * %a;
}
function mRound(%num) {
return mFloatLength(%num, 0);
}
function mRoundTo(%num, %a) {
return mRound(%num / %a) * %a;
}
function mIntLength(%num, %a) {
%num = mRound(%num);
if(%a >= 0) {
while(strLen(%num) < %a)
%num = 0 @ %num;
while(strLen(%num) > %a) {
%num = getSubStr(%num, 0, strLen(%num)-1);
%add0s++;
}
for(%i=1; %i <= %add0s; %i++)
%num = %num @ 0;
return %num;
}
}
function mInterpolate(%num, %mina, %maxa, %minb, %maxb) { //Similar to mLerp, but does a reverse lerp with the first 3 parameters
return (%num-%mina) / (%maxa-%mina) * (%maxb-%minb) + %minb;
}
//Credits to Trader for these off the forums.
function eulerToAxis(%euler) {
%euler = VectorScale(%euler,$pi / 180);
%matrix = MatrixCreateFromEuler(%euler);
return getWords(%matrix,3,6);
}
function axisToEuler(%axis) {
%angleOver2 = getWord(%axis,3) * 0.5;
%angleOver2 = -%angleOver2;
%sinThetaOver2 = mSin(%angleOver2);
%cosThetaOver2 = mCos(%angleOver2);
%q0 = %cosThetaOver2;
%q1 = getWord(%axis,0) * %sinThetaOver2;
%q2 = getWord(%axis,1) * %sinThetaOver2;
%q3 = getWord(%axis,2) * %sinThetaOver2;
%q0q0 = %q0 * %q0;
%q1q2 = %q1 * %q2;
%q0q3 = %q0 * %q3;
%q1q3 = %q1 * %q3;
%q0q2 = %q0 * %q2;
%q2q2 = %q2 * %q2;
%q2q3 = %q2 * %q3;
%q0q1 = %q0 * %q1;
%q3q3 = %q3 * %q3;
%m13 = 2.0 * (%q1q3 - %q0q2);
%m21 = 2.0 * (%q1q2 - %q0q3);
%m22 = 2.0 * %q0q0 - 1.0 + 2.0 * %q2q2;
%m23 = 2.0 * (%q2q3 + %q0q1);
%m33 = 2.0 * %q0q0 - 1.0 + 2.0 * %q3q3;
return mRadToDeg(mAsin(%m23)) SPC mRadToDeg(mAtan(-%m13, %m33)) SPC mRadToDeg(mAtan(-%m21, %m22));
}