Useful ACS Scripts
Posted: Mon Jun 04, 2012 8:29 am
Since we're all lazy bums and don't want to do things over again, I think it'd be a good idea to have a thread of useful ACS functions/scripts. Because I'm not a prick, FUNCTIONS LOL.
Most of these are small functions, but they really do make scripts more readable when you use them. And you only have to implement it once - what more do you want?
abs, mod, pow(Float):[spoiler][/spoiler]
min, max, middle:[spoiler][/spoiler]
Check if key is up, down, or pressed:[spoiler][/spoiler]
This was originally one function in Python, adjust. It worked like this:
However, since ACS doesn't support returning multiple arguments, nor does it support structs, what we get is me splitting this into two functions. Yeah, not a very good compromise, but what else could I choose?
Range adjusters:[spoiler][/spoiler]
Quadratic formula crap:[spoiler][/spoiler]
itof, ftoi:[spoiler][/spoiler]
AddAmmoCapacity:[spoiler][/spoiler]
These functions used to have a disclaimer saying they only worked with StrParam, but as you can probably guess, that's no longer an issue, eh?
String padding, slicing, etc.[spoiler][/spoiler]
Most of these are small functions, but they really do make scripts more readable when you use them. And you only have to implement it once - what more do you want?
abs, mod, pow(Float):[spoiler]
Code: Select all
function int abs(int x)
{
if (x < 0) { return -x; }
return x;
}
function int mod(int x, int y)
{
int ret = x - ((x / y) * y);
if (ret < 0) { ret += y; }
return ret;
}
function int pow(int x, int y)
{
int n = 1;
while (y-- > 0) { n *= x; }
return n;
}
function int powFloat(int x, int y)
{
int n = 1.0;
while (y-- > 0) { n = FixedMul(n, x); }
return n;
}
min, max, middle:[spoiler]
Code: Select all
function int min(int x, int y)
{
if (x < y) { return x; }
return y;
}
function int max (int x, int y)
{
if (x > y) { return x; }
return y;
}
function int middle(int x, int y, int z)
{
if ((x < z) && (y < z)) { return max(x, y); }
return max(min(x, y), z);
}
Check if key is up, down, or pressed:[spoiler]
Code: Select all
function int keyUp(int key)
{
int buttons = GetPlayerInput(-1, INPUT_BUTTONS);
if (~buttons & key) { return 1; }
return 0;
}
function int keyDown(int key)
{
int buttons = GetPlayerInput(-1, INPUT_BUTTONS);
if (buttons & key) { return 1; }
return 0;
}
function int keyPressed(int key)
{
int buttons = GetPlayerInput(-1, INPUT_BUTTONS);
int oldbuttons = GetPlayerInput(-1, INPUT_OLDBUTTONS);
int newbuttons = (buttons ^ oldbuttons) & buttons;
if (newbuttons & key) { return 1; }
return 0;
}
This was originally one function in Python, adjust. It worked like this:
Code: Select all
>>> adjust(4, 7, 5)
(4, 7)
>>> adjust(4, 7, 12)
(9, 12)
>>> adjust (4, 7, 2)
(2, 5)
Range adjusters:[spoiler]
Code: Select all
function int adjustBottom(int tmin, int tmax, int i)
{
if (i < tmin) { tmin = i; }
if (i > tmax) { tmin += (i - tmax); }
return tmin;
}
function int adjustTop(int tmin, int tmax, int i)
{
if (i < tmin) { tmax -= (tmin - i); }
if (i > tmax) { tmax = i; }
return tmax;
}
Quadratic formula crap:[spoiler]
Code: Select all
function int sqrt(int number)
{
if (number == 1.0) { return 1.0; }
if (number <= 0) { return 0; }
int val = 150.0;
for (int i=0; i<15; i++) { val = (val + FixedDiv(number, val)) >> 1; }
return val;
}
function int quadPos(int a, int b, int c)
{
int s1 = sqrt(FixedMul(b, b)-(4*FixedMul(a, c)));
int s2 = (2 * a);
int b1 = FixedDiv(-b + s1, s2);
return b1;
}
function int quadNeg(int a, int b, int c)
{
int s1 = sqrt(FixedMul(b, b)-(4*FixedMul(a, c)));
int s2 = (2 * a);
int b1 = FixedDiv(-b - s1, s2);
return b1;
}
function int quad(int a, int b, int c, int y)
{
return FixedMul(a, FixedMul(y, y)) + FixedMul(b, y) + y;
}
function int quadHigh(int a, int b, int c, int x)
{
return quadPos(a, b, c-x);
}
function int quadLow(int a, int b, int c, int x)
{
return quadNeg(a, b, c-x);
}
itof, ftoi:[spoiler]
Code: Select all
function int itof(int x) { return x << 16; }
function int ftoi(int x) { return x >> 16; }
AddAmmoCapacity:[spoiler]
Code: Select all
function void AddAmmoCapacity(int type, int add)
{
SetAmmoCapacity(type, GetAmmoCapacity(type) + add);
}
These functions used to have a disclaimer saying they only worked with StrParam, but as you can probably guess, that's no longer an issue, eh?
String padding, slicing, etc.[spoiler]
Code: Select all
function int inRange(int low, int high, int x)
{
return ((x >= low) && (x < high));
}
function int padStringR(int baseStr, int padChar, int len)
{
int baseStrLen = StrLen(baseStr);
int pad = "";
int padLen; int i;
if (baseStrLen >= len)
{
return baseStr;
}
padChar = GetChar(padChar, 0);
padLen = len - baseStrLen;
for (i = 0; i < padLen; i++)
{
pad = StrParam(s:pad, c:padChar);
}
return StrParam(s:baseStr, s:pad);
}
function int padStringL(int baseStr, int padChar, int len)
{
int baseStrLen = StrLen(baseStr);
int pad = "";
int padLen; int i;
if (baseStrLen >= len)
{
return baseStr;
}
padChar = GetChar(padChar, 0);
padLen = len - baseStrLen;
for (i = 0; i < padLen; i++)
{
pad = StrParam(s:pad, c:padChar);
}
return StrParam(s:pad, s:baseStr);
}
function int changeString(int string, int repl, int where)
{
int i; int j; int k;
int ret = "";
int len = StrLen(string);
int rLen = StrLen(repl);
if ((where + rLen < 0) || (where >= len))
{
return string;
}
for (i = 0; i < len; i++)
{
if (inRange(where, where+rLen, i))
{
ret = StrParam(s:ret, c:GetChar(repl, i-where));
}
else
{
ret = StrParam(s:ret, c:GetChar(string, i));
}
}
return ret;
}
function int sliceString(int string, int start, int end)
{
int len = StrLen(string);
int ret = "";
int i;
if (start < 0)
{
start = len + start;
}
if (end <= 0)
{
end = len + end;
}
start = max(0, start);
end = min(end, len-1);
for (i = start; i < end; i++)
{
ret = StrParam(s:ret, c:GetChar(string, i));
}
return ret;
}