Replaced rrand function with RANDRANGE macro.

This commit is contained in:
onpon4 2015-03-29 10:19:53 -04:00
parent b00bf9d7b2
commit 576d254e4a
13 changed files with 56 additions and 86 deletions

View File

@ -50,7 +50,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "init.h" #include "init.h"
#include "intermission.h" #include "intermission.h"
#include "loadSave.h" #include "loadSave.h"
#include "math.h"
#include "messages.h" #include "messages.h"
#include "misc.h" #include "misc.h"
#include "missions.h" #include "missions.h"

View File

@ -831,8 +831,8 @@ bool alien_add()
if (aliens[index].classDef == CD_ESCORT) if (aliens[index].classDef == CD_ESCORT)
aliens[index].shield = 50; aliens[index].shield = 50;
aliens[index].dx = rrand(-2, 2); aliens[index].dx = RANDRANGE(-2, 2);
aliens[index].dy = rrand(-2, 2); aliens[index].dy = RANDRANGE(-2, 2);
aliens[index].ammo[0] = 0; aliens[index].ammo[0] = 0;
@ -915,16 +915,8 @@ void alien_addFriendly(int type)
aliens[type].owner = &aliens[type]; aliens[type].owner = &aliens[type];
aliens[type].target = &aliens[type]; aliens[type].target = &aliens[type];
aliens[type].active = true; aliens[type].active = true;
aliens[type].x = RANDRANGE((screen->w / 2) - 150, (screen->w / 2) + 150);
if (rand() % 2 == 0) aliens[type].y = RANDRANGE((screen->h / 2) - 150, (screen->h / 2) + 150);
aliens[type].x = rrand((int)(screen->w / 2), (int)(screen->w / 2) + 150);
else
aliens[type].x = rrand((int)(screen->w / 2) - 150, (int)(screen->w / 2));
if (rand() % 2 == 0)
aliens[type].y = rrand((int)(screen->h / 2), (int)(screen->h / 2) + 150);
else
aliens[type].y = rrand((int)(screen->h / 2) - 150, (int)(screen->h / 2));
if (type == ALIEN_PHOEBE) if (type == ALIEN_PHOEBE)
aliens[type].classDef = CD_PHOEBE; aliens[type].classDef = CD_PHOEBE;
@ -940,19 +932,19 @@ void alien_addFriendly(int type)
bool alien_place(object *alien) bool alien_place(object *alien)
{ {
if (rand() % 2 == 0) if (rand() % 2 == 0)
alien->x = rrand(screen->w, screen->w * 2); alien->x = RANDRANGE(screen->w, screen->w * 2);
else else
alien->x = rrand(-screen->w, 0); alien->x = RANDRANGE(-screen->w, 0);
if (rand() % 2 == 0) if (rand() % 2 == 0)
alien->y = rrand(screen->h, screen->h * 2); alien->y = RANDRANGE(screen->h, screen->h * 2);
else else
alien->y = rrand(-screen->h, 0); alien->y = RANDRANGE(-screen->h, 0);
if (currentGame.area == 24) if (currentGame.area == 24)
{ {
alien->x = screen->w; alien->x = screen->w;
alien->y = rrand(screen->h / 3, (2 * screen->h) / 3); alien->y = RANDRANGE(screen->h / 3, (2 * screen->h) / 3);
} }
for (int i = 0 ; i < ALIEN_MAX ; i++) for (int i = 0 ; i < ALIEN_MAX ; i++)

View File

@ -55,7 +55,7 @@ void bullet_add(object *theWeapon, object *attacker, int y, int dy)
if (bullet->flags & WF_VARIABLE_SPEED) if (bullet->flags & WF_VARIABLE_SPEED)
{ {
bullet->dx = rrand(100, 200); bullet->dx = RANDRANGE(100, 200);
bullet->dx /= 10; bullet->dx /= 10;
if (attacker->face == 1) if (attacker->face == 1)
bullet->dx = 0 - bullet->dx; bullet->dx = 0 - bullet->dx;
@ -65,7 +65,7 @@ void bullet_add(object *theWeapon, object *attacker, int y, int dy)
if (bullet->flags & WF_SCATTER) if (bullet->flags & WF_SCATTER)
{ {
bullet->dy = rrand(-200, 200); bullet->dy = RANDRANGE(-200, 200);
if (bullet->dy != 0) if (bullet->dy != 0)
bullet->dy /= 200; bullet->dy /= 200;
} }
@ -127,8 +127,8 @@ void bullet_add(object *theWeapon, object *attacker, int y, int dy)
if (attacker->classDef == CD_ASTEROID) if (attacker->classDef == CD_ASTEROID)
{ {
bullet->dx = rrand(-20, 20); bullet->dx = RANDRANGE(-20, 20);
bullet->dy = rrand(-20, 20); bullet->dy = RANDRANGE(-20, 20);
bullet->image[0] = shape[4]; bullet->image[0] = shape[4];
} }
@ -267,8 +267,8 @@ void doBullets()
{ {
for (int i = 0 ; i < bullet->damage * 2 ; i++) for (int i = 0 ; i < bullet->damage * 2 ; i++)
blit(bullet->image[0], blit(bullet->image[0],
(int)(bullet->x - rrand(-(bullet->damage * 2 / 3), 0)), (int)(bullet->x - RANDRANGE(-(bullet->damage * 2 / 3), 0)),
(int)(bullet->y + rrand(-3, 3))); (int)(bullet->y + RANDRANGE(-3, 3)));
} }
blit(bullet->image[0], (int)bullet->x, (int)bullet->y); blit(bullet->image[0], (int)bullet->x, (int)bullet->y);
@ -353,8 +353,8 @@ void doBullets()
bullet->shield = 0; bullet->shield = 0;
audio_playSound(SFX_EXPLOSION, bullet->x); audio_playSound(SFX_EXPLOSION, bullet->x);
for (int i = 0 ; i < 10 ; i++) for (int i = 0 ; i < 10 ; i++)
addExplosion(bullet->x + rrand(-35, 35), addExplosion(bullet->x + RANDRANGE(-35, 35),
bullet->y + rrand(-35, 35), bullet->y + RANDRANGE(-35, 35),
E_BIG_EXPLOSION); E_BIG_EXPLOSION);
} }
} }
@ -405,8 +405,8 @@ void doBullets()
bullet->shield = 0; bullet->shield = 0;
audio_playSound(SFX_EXPLOSION, bullet->x); audio_playSound(SFX_EXPLOSION, bullet->x);
for (int i = 0 ; i < 10 ; i++) for (int i = 0 ; i < 10 ; i++)
addExplosion(bullet->x + rrand(-35, 35), addExplosion(bullet->x + RANDRANGE(-35, 35),
bullet->y + rrand(-35, 35), E_BIG_EXPLOSION); bullet->y + RANDRANGE(-35, 35), E_BIG_EXPLOSION);
} }
} }
else else
@ -443,8 +443,8 @@ void doBullets()
theCargo->active = false; theCargo->active = false;
audio_playSound(SFX_EXPLOSION, theCargo->x); audio_playSound(SFX_EXPLOSION, theCargo->x);
for (int i = 0 ; i < 10 ; i++) for (int i = 0 ; i < 10 ; i++)
addExplosion(theCargo->x + rrand(-15, 15), addExplosion(theCargo->x + RANDRANGE(-15, 15),
theCargo->y + rrand(-15, 15), theCargo->y + RANDRANGE(-15, 15),
E_BIG_EXPLOSION); E_BIG_EXPLOSION);
updateMissionRequirements(M_PROTECT_PICKUP, updateMissionRequirements(M_PROTECT_PICKUP,
P_CARGO, 1); P_CARGO, 1);
@ -465,8 +465,8 @@ void doBullets()
{ {
audio_playSound(SFX_EXPLOSION, bullet->x); audio_playSound(SFX_EXPLOSION, bullet->x);
for (int i = 0 ; i < 10 ; i++) for (int i = 0 ; i < 10 ; i++)
addExplosion(bullet->x + rrand(-35, 35), addExplosion(bullet->x + RANDRANGE(-35, 35),
bullet->y + rrand(-35, 35), E_BIG_EXPLOSION); bullet->y + RANDRANGE(-35, 35), E_BIG_EXPLOSION);
if (checkPlayerShockDamage(bullet->x, bullet->y)) if (checkPlayerShockDamage(bullet->x, bullet->y))
setInfoLine("Warning: Missile Shockwave Damage!!", setInfoLine("Warning: Missile Shockwave Damage!!",

View File

@ -136,11 +136,11 @@ void addCollectable(float x, float y, int type, int value, int life)
collectable->x = x; collectable->x = x;
collectable->y = y; collectable->y = y;
collectable->dx = rrand(-100, 100); collectable->dx = RANDRANGE(-100, 100);
if (collectable->dx != 0) if (collectable->dx != 0)
collectable->dx /= 100; collectable->dx /= 100;
collectable->dy = rrand(-100, 100); collectable->dy = RANDRANGE(-100, 100);
if (collectable->dy != 0) if (collectable->dy != 0)
collectable->dy /= 100; collectable->dy /= 100;

View File

@ -28,7 +28,7 @@ void addDebris(int x, int y, int amount)
object *debris; object *debris;
amount = rrand(3, rand() % amount); amount = RANDRANGE(3, rand() % amount);
LIMIT(amount, 3, 8); LIMIT(amount, 3, 8);
for (int i = 0 ; i < amount ; i++) for (int i = 0 ; i < amount ; i++)
@ -39,10 +39,10 @@ void addDebris(int x, int y, int amount)
debris->x = x; debris->x = x;
debris->y = y; debris->y = y;
debris->thinktime = rrand(60, 180); debris->thinktime = RANDRANGE(60, 180);
debris->dx = rrand(-500, 500); debris->dx = RANDRANGE(-500, 500);
debris->dy = rrand(-500, 500); debris->dy = RANDRANGE(-500, 500);
if (debris->dx != 0) if (debris->dx != 0)
debris->dx /= 100; debris->dx /= 100;
@ -74,7 +74,7 @@ void doDebris()
debris->x += debris->dx; debris->x += debris->dx;
debris->y += debris->dy; debris->y += debris->dy;
addExplosion(debris->x + rrand(-10, 10), debris->y + rrand(-10, 10), E_BIG_EXPLOSION); addExplosion(debris->x + RANDRANGE(-10, 10), debris->y + RANDRANGE(-10, 10), E_BIG_EXPLOSION);
} }
if (debris->thinktime < 1) if (debris->thinktime < 1)

View File

@ -29,6 +29,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define WRAP_ADD(x, y, a, b) x = (((x) + (y)) + \ #define WRAP_ADD(x, y, a, b) x = (((x) + (y)) + \
((x) + (y) < (a) ? ((b) - (a)) : 0) + \ ((x) + (y) < (a) ? ((b) - (a)) : 0) + \
((x) + (y) > (b) ? ((a) - (b)) : 0)) ((x) + (y) > (b) ? ((a) - (b)) : 0))
#define RANDRANGE(x, y) (((x) < (y)) ? ((x) + (rand() % (1 + (y) - (x)))) : (x))
// ALL // ALL
#define NONE 0 #define NONE 0

View File

@ -53,7 +53,7 @@ void addEngine(object *craft)
float x = craft->x + (craft->engineX * craft->face); float x = craft->x + (craft->engineX * craft->face);
float y = craft->y + craft->engineY; float y = craft->y + craft->engineY;
y += rrand(-3, 3); y += RANDRANGE(-3, 3);
addExplosion(x, y, E_TINY_EXPLOSION); addExplosion(x, y, E_TINY_EXPLOSION);
} }

View File

@ -920,7 +920,13 @@ int mainGameLoop()
if ((currentGame.area == 24) && (engine.addAliens > -1)) if ((currentGame.area == 24) && (engine.addAliens > -1))
{ {
if ((rand() % 10) == 0) if ((rand() % 10) == 0)
addCollectable(rrand(800, 100), player.y, P_MINE, 25, // XXX: The originally specified range for x was [800, 100],
// which with the original rrand function caused the result
// returned to be `800 + rand() % -699`. Clearly a mistake,
// but I'm not entirely sure what the original intention was.
// For now, I've set the range to [800, 1500], which
// approximately replicates the original's results.
addCollectable(RANDRANGE(800, 1500), player.y, P_MINE, 25,
180 + rand() % 60); 180 + rand() % 60);
} }

View File

@ -628,8 +628,13 @@ int galaxyMap()
if (rand() % 1000 < 2) if (rand() % 1000 < 2)
{ {
engine.ssx = rrand(100, 100); // XXX: This code originally had the range set to [100, 100],
engine.ssy = rrand(100, 100); // which effectively always caused the star speed to be set to
// 1. I don't think this was the intention, so I changed the
// minimum from 100 to -100, which is what I think was probably
// intended.
engine.ssx = RANDRANGE(-100, 100);
engine.ssy = RANDRANGE(-100, 100);
engine.ssx /= 100; engine.ssx /= 100;
engine.ssy /= 100; engine.ssy /= 100;
} }

View File

@ -1,37 +0,0 @@
/*
Copyright (C) 2003 Parallel Realities
Copyright (C) 2011 Guus Sliepen
Copyright (C) 2015 Julian Marchant
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MATH_H
#define MATH_H
static inline int rrand(int min, int max)
{
int r = min;
max++;
if ((max - min) == 0)
return min;
r += rand() % (max - min);
return r;
}
#endif

View File

@ -308,7 +308,8 @@ void doPlayer()
blit(shipShape[shapeToUse], (int)player.x, (int)player.y); blit(shipShape[shapeToUse], (int)player.x, (int)player.y);
if ((player.maxShield > 1) && (player.shield <= engine.lowShield) && if ((player.maxShield > 1) && (player.shield <= engine.lowShield) &&
(rand() % 5 < 1)) (rand() % 5 < 1))
addExplosion(player.x + rrand(-10, 10), player.y + rrand(-10, 20), E_SMOKE); addExplosion(player.x + RANDRANGE(-10, 10),
player.y + RANDRANGE(-10, 20), E_SMOKE);
} }
else else
{ {
@ -332,7 +333,8 @@ void doPlayer()
engine.keyState[KEY_UP] = engine.keyState[KEY_DOWN] = engine.keyState[KEY_LEFT] = engine.keyState[KEY_RIGHT] = 0; engine.keyState[KEY_UP] = engine.keyState[KEY_DOWN] = engine.keyState[KEY_LEFT] = engine.keyState[KEY_RIGHT] = 0;
if ((rand() % 3) == 0) if ((rand() % 3) == 0)
addExplosion(player.x + rrand(-10, 10), player.y + rrand(-10, 10), E_BIG_EXPLOSION); addExplosion(player.x + RANDRANGE(-10, 10),
player.y + RANDRANGE(-10, 10), E_BIG_EXPLOSION);
if (player.shield == -99) if (player.shield == -99)
addDebris((int)player.x, (int)player.y, player.maxShield); addDebris((int)player.x, (int)player.y, player.maxShield);
} }

View File

@ -103,8 +103,10 @@ void checkScriptEvents()
else else
{ {
aliens[gameEvent[i].entity].active = true; aliens[gameEvent[i].entity].active = true;
aliens[gameEvent[i].entity].x = rrand((int)player.x + 400, (int)player.x + 800); aliens[gameEvent[i].entity].x = ((int)player.x +
aliens[gameEvent[i].entity].y = rrand((int)player.y - 400, (int)player.y + 800); RANDRANGE(400, 800));
aliens[gameEvent[i].entity].y = ((int)player.y +
RANDRANGE(-400, 800));
} }
} }

View File

@ -649,8 +649,8 @@ void gameover()
updateScreen(); updateScreen();
unBuffer(); unBuffer();
x = ((screen->w - gameover->w) / 2) - rrand(-2, 2); x = ((screen->w - gameover->w) / 2) - RANDRANGE(-2, 2);
y = ((screen->h - gameover->h) / 2) - rrand(-2, 2); y = ((screen->h - gameover->h) / 2) - RANDRANGE(-2, 2);
blit(gameover, x, y); blit(gameover, x, y);
delayFrame(); delayFrame();