IsmAvatar
|
|
Posted on: November 03, 2010, 10:44:10 am |
|
|
LateralGM Developer
Location: Pennsylvania/USA Joined: Apr 2008
Posts: 877
|
I'm trying to write up the action_move function, since it doesn't really have a direct GML equivalent, due to the direction argument being multiple-choice which then gets randomly selected. I'm also working on an educated assumption here, trying to recall from when I still had access to GM, how it behaved.
Assumption 1: It will randomly select one of the depressed directional buttons, but will not select a between value (so the resulting direction will always be a multiple of 45). Assumption 2: The directional string directly corresponds with the directions in the dirs array. (I've done a bit of testing, so I think it is correct)
void action_move(const char dir[9], int argspeed) { int dirs[9] = { 225, 270, 315, 180, -1, 0, 135, 90, 45 }; int chosendirs[9]; int choices = 0; for (int i = 0; i < 9; i++) if (dir[i] == '1') chosendirs[choices++] = dirs[i]; if (choices == 0) return; choices = int(random(choices)); //choices is now chosen
//We use rval.d for efficiency, so hspeed/vspeed aren't set twice. const double newdir = ((enigma::object_planar*)enigma::instance_event_iterator->inst)->direction.rval.d = chosendirs[choices]; if (argument_relative) argspeed += ((enigma::object_planar*)enigma::instance_event_iterator->inst)->speed; const double newspd = ((enigma::object_planar*)enigma::instance_event_iterator->inst)->speed.rval.d = chosendirs[choices] == -1 ? 0 : argspeed; ((enigma::object_planar*)enigma::instance_event_iterator->inst)->hspeed.rval.d = newspd * cos(newdir); ((enigma::object_planar*)enigma::instance_event_iterator->inst)->vspeed.rval.d = newspd * sin(newdir); } Note 1: The first argument should always be an array of characters of size 9. Longer strings are fine, but only the first 9 characters will be examined. Shorter strings may cause this to look at memory beyond the string. Note 2: This code makes use of the random(x) function.
Note 3: A few minor optimizations can be made around random. 3a: To avoid the use of random when there's only 1 choice. Replace: choices = int(random(choices)); //choices is now chosen With
if (choices == 1) choices = 0; else choices = int(random(choices)); //choices is now chosen
3b: Replace random with randomInt if it's more efficient (and if it exists). 3c: Replace random with integrated choose() function once it's implemented, if it's more efficient.
|
|
« Last Edit: November 12, 2010, 12:17:35 pm by IsmAvatar »
|
Logged
|
|
|
|
|
|
|
Josh @ Dreamland
|
|
Reply #4 Posted on: November 04, 2010, 12:06:06 am |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
I don't know off the top of my head the implications of taking const char[9] as a parameter; I am relatively certain that's the same as just const char*. Whatever the actual case, you really wanted const char[10] for the NULL at the end of the literal. My other beef is that, despite this being a function instead of a macro, it does not take advantage of non-EDL optimizations; namely setting direction.dval and speed.dval manually, then manipulating hspeed.dval and vspeed.dval manually. I'm actually too tired and lazy right now to do the trivial amount of text insertion required to put that to code. >.< -sigh- const double dir = ((enigma::object_planar*)enigma::instance_event_iterator->inst)->direction.dval = chosendirs[choices]; ((enigma::object_planar*)enigma::instance_event_iterator->inst)->speed.dval = chosendirs[choices] == -1 ? 0 : argspeed; hspeed.dval = speed.dval * cos(dir), vspeed.dval = speed.dval * sin(dir);
Assuming all the rest of your code is right, and that I'm remembering the name of my own class member correctly.
|
|
« Last Edit: November 04, 2010, 12:07:41 am by Josh @ Dreamland »
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
|
Josh @ Dreamland
|
|
Reply #6 Posted on: November 04, 2010, 12:17:03 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
- Like I said, shouldn't matter. - Until choose is implemented, the macro isn't possible. - Well, now you know. Have you implemented it? -
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
|
|
|