PDA

View Full Version : Some small functions I made


Chompy
04-29-2007, 05:13 PM
Well, I finnaly decided to release some simple functions I've been using lately helping me script stuff.. so here they are :o
PS: This is also my first release in the Code-Gallery :)

class

public function reverseDir(dir)
{
if (dir in |0, 3|)
return (dir+2)%4;
}
public function reversedeg(deg)
return (deg+180)%360;

public function reverserad(rad)
{
reverse = reversedeg(radtodeg(rad));
return degtorad(reverse);
}
public function percentage(of, value)
return ((value * of) / 100);

public function radianequaldir(dir)
return (pi/2)+(pi/2*dir);

They are currently serverside, but works as well on clientside

reverseDir():

Well, I made this function for knockback effects, because it gets the direction
behind the player, for example input 2 would be 0, 1 would be 3 etc..

example of use:


function onCreated()
join("ch_functions"); // this is what I named the class
//#CLIENTSIDE
function onCreated()
{
a = reversedir(player.dir);
setani("hurt", "");
power = 0.8; // knock-back tiles
for(i = power; i > 0; i -= power/3;) {
d = {vecx(a)*2, vecy(a)*2};
player.x += d[0]*i;
player.y += d[1]*i;
sleep(power / (35 + (power * 10)));
}
}

I know, poor example but I just wanted to clear how it could be used

reversedeg():

Well, this functions does almost the same as reversedir but reverses degrees,
for example: 360 would be 180, 180 would be 0 and 247 would be 67

Well, I haven't actually used this alot, but it could be used by projectiles or even spells (wall hit, reflect spells)

reverserad():
Yep, it reverses radians, actually it starts reversedeg() and then uses degtorad() to get output, can be used as reversedeg() can be used..

percentage(of, value)

Well, easy math thingy, gets a percentage amount of a value, for example


function onCreated()
{
join("ch_functions");

echo("P1: " @ percentage(47, 100));
echo("P2: " @ percentage(67, 847));
}


would output

P1: 47
P2: 567.49

Easy function, I've used this for alot (Spells taking a percentage as mana, damage bonuses etc...)

radianequaldir()

Basicly all it does it gets the radian value of the current direction of the player
I simple used this for a movement system, to get the start radian of the direction

Formula used:
The formula I've used to get the reverse is in this format:

(VALUE+HALF)%FULL
where VALUE is the float being reversed, FULL is the max value, for example 360,
while HALF is the half value of FULL, for example 180


Notes:
All examples are serverside (knockback isn't); the functions will work clientside if you edit the class containing the functions (moving the functions to the clientside)

Thanks Dusty for telling an easier formula :D

I'll release some "small"-functions later when I make them :O
And I also hope some other scripters will release some of their "nifty"-functions :)

ff7chocoboknight
04-29-2007, 05:39 PM
Holy ****!! I smell long equations.... oh wait nvm u made it simple

DustyPorViva
04-29-2007, 07:05 PM
No need for the absolute value in reversedir, modulus will make sure it doesn't go under 0. Same for reversedeg.

Also, why do dir%4?
I usually just do (dir+2)%4

Chompy
04-29-2007, 07:12 PM
No need for the absolute value in reversedir, modulus will make sure it doesn't go under 0. Same for reversedeg.

Also, why do dir%4?
I usually just do (dir+2)%4

:O didn't think of that
Thanks, will edit my post :)

Chandler
04-29-2007, 07:16 PM
You're aware that abs turns a negative value to a posative, right?

Chompy
04-29-2007, 07:20 PM
You're aware that abs turns a negative value to a posative, right?

Yes, but as Dusty said, I wasn't aware that modulus will make it so it won't go under 0

ff7chocoboknight
04-29-2007, 07:21 PM
You're aware that abs turns a negative value to a posative, right?
perhaps he is aware (or maybe he isn't?)