|
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.
|