Vice City Multiplayer

VC:MP => mIRC/pawn Scripting => ShowRoom (pawn) => Topic started by: Ettans on February 06, 2010, 01:44:21 PM

Title: [INC] Some math functions
Post by: Ettans on February 06, 2010, 01:44:21 PM
QuoteMathematical functions

* Written by Ettans ([email protected])
* Last modified 02/07/2010
* If you use this include, please give proper credit.

This include (na_math.inc) is written for SA:MP and VC:MP (PAWN server), adding additional mathematical functions
for any scripter to use. For an example, you can use these functions to add mathematical minigames to your servers and reward
players when they answer correctly.

Note: I'm really looking forward to decent suggestions, as I intend to keep this updated.

Setup:

Extract na_math_include.zip and copy the na_math.inc to your /pawno/includes/ folder. Open your gamemode and add #include <na_math> to the top. You're all done!

Functions:

Description: Determines the greatest common divider of two numbers using Euclid's algorithm

Example usage: euclid_gcd(6,12); // Will return 6
Example usage: euclid_gcd(25,5); // Will return 5

Description: Determines the least common multiple of two numbers using Euclid's algorithm

Example usage: euclid_lcm(4,8); // Will return 8
Example usage: euclid_lcm(12,6); // Will return 12

Description: Returns the absolute value of an integer

Example usage: abs(4);  // Will return 4
Example usage: abs(-6);  // Will return 6

Description: Returns the value of E^x, where E is Euler's constant and x is the number passed to it

Example usage: exp(-1);  // Will return 0.3678794
Example usage: exp(5);  // Will return 148.4131591

Description: Convert a degree to a radian number

Example usage: deg2rad(6);  // Will return 0.1047198
Example usage: deg2rad(10);  // Will return 0.1745329
Example usage: deg2rad(10.5);  // Will return 0.1832596

Description: Convert a radian number to a degree

Example usage: rad2deg(2);  // Will return 114.59156
Example usage: rad2deg(5);  // Will return 286.4789
Example usage: rad2deg(2.4);  // Will return 137.509872

Example commands:

> May need editing, as it's meant for SA:MP.


printf("Euler's constant (1) %f",exp(1));
printf("5 Degrees to radians %f",deg2rad(10));


/*
* Requires you to have string pre-defined somewhere in your script, mostly at the top of OnPlayerCommandText.
* Simply copy these commands to your OnPlayerCommandText callback.
*/

// Greatest common divider
if(strcmp(cmd, "/euclid_gcd", true) == 0)
{
   new val_x1 = strval(cmdtext[12]);
   new val_x2 = strval(cmdtext[14]);
   
   format(string,sizeof(string),"(Greatest common divider) Result: %d",euclid_gcd(val_x1,val_x2));
   SendClientMessage(playerid,0xFFFFFFFF,string);
   return 1;
}

// Least common multiple
if(strcmp(cmd, "/euclid_lcm", true) == 0)
{
   new val1 = strval(cmdtext[12]);
   new val2 = strval(cmdtext[14]);
   
   format(string,sizeof(string),"(Least common multiple) Result: %d",euclid_lcm(val1,val2));
   SendClientMessage(playerid,0xFFFFFFFF,string);
   return 1;
}

// Absolute value
if(strcmp(cmd, "/abs", true) == 0)
{
   new val = strval(cmdtext[9]);
   
   format(string,sizeof(string),"Absolue value: %d",abs(val));
   SendClientMessage(playerid,0xFFFFFFFF,string);
   return 1;
}


Download link of the include: uploadFFS (Contains the include and a Readme document) (http://files.uploadffs.com/d/2/d66f2a36/na_math_include.zip)
Title: Re: Some math functions
Post by: [AoD]NC on February 06, 2010, 02:38:23 PM
Good, but how can this be useful in game?

QuotePM >>> To register type /c register YOUR PASS and what is the absolute value of -2
:D
Title: Re: Some math functions
Post by: Ettans on February 06, 2010, 02:41:48 PM
Math games and a lot of other areas.
Title: Re: Some math functions
Post by: Boss on February 06, 2010, 02:44:19 PM
Quote from: Ettans on February 06, 2010, 02:41:48 PM
Math games and a lot of other areas.
lol?
Title: Re: Some math functions
Post by: Ettans on February 06, 2010, 02:49:49 PM
As in minigames? Don't tell me this is the first time you hear about something like that...  :-\
Title: Re: Some math functions
Post by: Skirmant on February 06, 2010, 04:12:49 PM
Quote from: Ettans on February 06, 2010, 02:41:48 PM
Math games and a lot of other areas.

The only "math games" I know are Poker, Chess and Monopoly ;D
There all about strategy mostly. I would like to see a REAL math game on a vcmp server.
Title: Re: Some math functions
Post by: Pussycat_Dolls on February 06, 2010, 04:19:26 PM
math games,yeah I've noticed that in Sa:MP,but I never tried to answer....
Title: Re: Some math functions
Post by: SilenusShar on February 07, 2010, 01:33:37 AM
this is just what we need, why doesn't pawn have these as standard libraries though ?
Title: Re: Some math functions
Post by: thijn on February 07, 2010, 09:54:14 AM
I don't see why this could be usefull
Title: Re: Some math functions
Post by: SilenusShar on February 07, 2010, 11:27:45 AM
Quote from: thijn on February 07, 2010, 09:54:14 AM
I don't see why this could be usefull

how are you calculating, distance, height, speed, velocity, altitude and any other maths calculations thijn? with thin air ?

if its not using any maths functions, please share it with us...
Title: Re: Some math functions
Post by: [AoD]NC on February 07, 2010, 12:55:59 PM
Distance, height or speed dont need so advanced maths. It needs only +, -, * and / :p.
Title: Re: Some math functions
Post by: Ettans on February 07, 2010, 08:02:59 PM
Euclid's algorithm or absolute value of a number is definately NOT advanced mathematics, in fact, it's basic and you learn it in pre-high. To the people who don't understand the need of basic mathematical functions in PAWN: If you have no need for them, then of course you don't see the potential use of them. In a 3D game, math has a very important part and you can use it to create highly-complex functions.
Title: Re: Some math functions
Post by: Ettans on February 08, 2010, 08:00:36 AM
Updated the first post! Added 3 new functions: exp, deg2rad and rad2deg.
Title: Re: Some math functions
Post by: Javi on February 08, 2010, 01:30:10 PM
I've not done anything with Pawn yet, but I'm curious, is there a way of controling decimals to a division? This is what I mean:

2 / 3 = 0.666666...

Is there a way of making it return 2 decimals as an example?
Title: Re: Some math functions
Post by: Boss on February 08, 2010, 01:36:05 PM
Quote from: Forze on February 08, 2010, 01:30:10 PM
I've not done anything with Pawn yet, but I'm curious, is there a way of controling decimals to a division? This is what I mean:

2 / 3 = 0.666666...

Is there a way of making it return 2 decimals as an example?
I didn't quite understand what do you mean, but you can decide the amount of numbers following the dot like this:

format(str,sizeof(str),"%.1f",1.6666); // formats str as "1.6"


Or a mathematical way:

new Float:fl = 1.6666;
fl = fl*10; // fl is 16.666
fl = floatround(fl); // fl is 17
fl = fl/10; // fl is 1.7
Title: Re: Some math functions
Post by: thijn on February 08, 2010, 10:59:24 PM
Quote from: SilenusShar on February 07, 2010, 11:27:45 AM
Quote from: thijn on February 07, 2010, 09:54:14 AM
I don't see why this could be usefull

how are you calculating, distance, height, speed, velocity, altitude and any other maths calculations thijn? with thin air ?
nope, But definitely not with these functions.
Title: Re: Some math functions
Post by: SilenusShar on February 09, 2010, 01:23:39 AM
Quote from: thijn on February 07, 2010, 09:54:14 AM
I don't see why this could be usefull

whats the point of stating this anyway ? surely people have minds of their own.

Those who find a need for it will use it, those who don't, "just walk by" as the expression goes.