ENIGMA Forums

Sharing is caring => Tips, Tutorials, Examples => Topic started by: RetroX on October 08, 2008, 05:57:05 pm

Title: choose(), mean(), median()
Post by: RetroX on October 08, 2008, 05:57:05 pm
Well, I've looked at a C++ tutorial, and decided to finish up the math functions.

WARNING: I have not tested this, although I am pretty sure it will work.

First, go to fnames.txt and add the following to the bottom of the file:
int choose(int arg1, int arg2, ...)
int mean(int arg1, int arg2, ...)
int median(int arg1, int arg2, ...)


Now, go to GMmathnc.h in ENIGMAsystem/SHELL/Universal_System and add this to the bottom of the file:
// May not be necessary; just in case ENIGMA doesn't include them already
#include <stdarg.h>

int choose(int num1, int num2, ...)
 {
 va_list choices;
 int i;
 int j=0;
 int array[];
 va_start(choices,num1);
 for (i=arg1;i!=-1;i=va_arg(choices,int))
  {
  array[j]=i;
  j+=1;
  }
 va_end(ap);
 return array[random(j)];
 }

int mean(int num1, int num2, ...)
 {
 va_list choices;
 int i;
 int j=0;
 int total=0;
 va_start(choices,num1);
 for (i=arg1;i!=-1;i=va_arg(choices,int))
  {
  total+=i;
  j+=1;
  }
 va_end(ap);
 return total/j;
 }

int median(int num1, int num2, ...)
 {
 va_list choices;
 int i;
 int j=0;
 int array[];
 va_start(choices,num1);
 for (i=arg1;i!=-1;i=va_arg(choices,int))
  {
  array[j]=i;
  j+=1;
  }
 va_end(ap);
 return array[(int)((j-1)/2)];
 }

PLEASE NOTE:
 - The above functions only work with integers (no decimals/fractions).
 - While they may not work in the setup I have provided, I am very positive that the scripts will work, and Josh may feel free to implement these into R4.
Title: Re: choose(), mean(), median()
Post by: Rusky on October 09, 2008, 04:47:54 pm
they'd work with fractions if you used double:

double mean(double num1, double num2, ...)
{
    va_list choices;
    va_start(choices, num1);
    double total;
    double count;
   
    for (double i = arg1; i != -1; i = va_arg(choices, double))
    {
        total += i;
        count += 1;
    }
   
    va_end(choices);
    return total / j;
}
Title: Re: choose(), mean(), median()
Post by: RetroX on October 09, 2008, 07:33:51 pm
Yeah, but they don't work with all types.  Although if you include the script with the var structure in it before the scripts, you could use var and do it for all types.
Title: Re: choose(), mean(), median()
Post by: death-droid on October 14, 2008, 04:50:20 am
The functions dont actually compile under enigma
Title: Re: choose(), mean(), median()
Post by: Rusky on October 14, 2008, 06:12:26 pm
do you ever take the mean or median of anything but numbers? and with choose, do you do it with anything but numbers or objects, which are represented by numbers? why would you want to use var? you don't take the mean of strings. that just doesn't make sense. and if double works, why limit yourself to int just because you didn't use var?
seriously, what goes on in that head of yours? O_o

and they should compile if you set it up right.
Title: Re: choose(), mean(), median()
Post by: OniLink10 on October 15, 2008, 07:00:19 pm
The functions dont actually compile under enigma
R u saying that Enigma is Interpreted? Cause it's not.
Title: Re: choose(), mean(), median()
Post by: Josh @ Dreamland on October 17, 2008, 06:43:30 pm
I didn't look far into stdarg, but my main concerns were that it doesn't give a method of determining the number of arguments in the list.

I corrected that by parsing in the number of arguments as the first argument in the function. I just never got around to the coded part of that.
Title: Re: choose(), mean(), median()
Post by: RetroX on October 17, 2008, 09:49:04 pm
You could just do what Game Maker does, and include 64 arguments and just not require all of them somehow.
Title: Re: choose(), mean(), median()
Post by: score_under on October 26, 2008, 03:39:12 pm
You could just do what Game Maker does, and include 64 arguments and just not require all of them somehow.
As far as I know, it in fact passes a structure of 6 dwords (3 doubles?), 0xF (never seems to change for me), and another 6-dword structure to every function. This way it can use one of the dwords as an argument count, and the others as arguments.
Title: Re: choose(), mean(), median()
Post by: RetroX on November 17, 2008, 07:23:47 pm
Or, it just doesn't use any special code at all and checks for the number of commas used. =P
Title: Re: choose(), mean(), median()
Post by: score_under on November 19, 2008, 06:01:19 pm
Wrong.

I've actually placed a breakpoint on these functions to try this out, so I should know. And Game Maker compiles the functions into some sort of bytecode, like Java does.
Title: Re: choose(), mean(), median()
Post by: Josh @ Dreamland on November 29, 2008, 11:37:33 pm
GM "compiles" things into a token tree. At load time. Instead of beforehand. Or it'd be just slightly more secure.

Either way, stdargs can't take custom classes anyway, iirc. So even under ideal conditions, if I could determine arg count, this wouldn't work. =\
Title: Re: choose(), mean(), median()
Post by: score_under on November 30, 2008, 11:18:53 am
GM "compiles" things into a token tree. At load time. Instead of beforehand. Or it'd be just slightly more secure.
And this, my friends, is a huge spoiler on how to hack every game maker game in existence without using That One Program.
Title: Re: choose(), mean(), median()
Post by: Rusky on December 01, 2008, 05:23:37 pm
You didn't know that?
Title: Re: choose(), mean(), median()
Post by: score_under on December 03, 2008, 02:09:31 pm
I knew it, but most didn't.
Title: Re: choose(), mean(), median()
Post by: serprex on December 04, 2008, 12:25:49 am
I don't get the whole median thing. From how I'm reading it, you're selecting the middle argument passed? You're suppose to sort before selecting the middle. And you didn't declare bounds on your array
Title: Re: choose(), mean(), median()
Post by: RetroX on December 05, 2008, 04:00:56 pm
GM "compiles" things into a token tree. At load time. Instead of beforehand. Or it'd be just slightly more secure.
Which is why "place_free(x,y,)" works.
Title: Re: choose(), mean(), median()
Post by: Josh @ Dreamland on December 06, 2008, 05:48:09 am
O_o?
Title: Re: choose(), mean(), median()
Post by: RetroX on December 06, 2008, 11:34:13 am
Yes, it does.  I never noticed the typo in one of my games because it worked.
Title: Re: choose(), mean(), median()
Post by: Josh @ Dreamland on December 06, 2008, 06:13:27 pm
I know, I was wondering what that had to do with it being interpreted at load time.
Title: Re: choose(), mean(), median()
Post by: RetroX on December 07, 2008, 01:18:55 pm
It checks everything at load time, probably changing syntax like that into more readable syntax.  Hence why proper syntax is important; so GM doesn't have to change it for you.
Title: Re: choose(), mean(), median()
Post by: Josh @ Dreamland on December 08, 2008, 08:00:33 am
Actually, it turns it into... I spose I'll say a byte code, for lack of anything better to call it. It's pretty close to compiling, only it's compiling for itself to read later, not the system to read later.