From 576d254e4ac133302f2312fe91fe8db9448ee26b Mon Sep 17 00:00:00 2001 From: onpon4 Date: Sun, 29 Mar 2015 10:19:53 -0400 Subject: [PATCH] Replaced rrand function with RANDRANGE macro. --- src/Starfighter.h | 1 - src/alien.cpp | 26 +++++++++----------------- src/bullet.cpp | 28 ++++++++++++++-------------- src/collectable.cpp | 4 ++-- src/debris.cpp | 10 +++++----- src/defs.h | 1 + src/explosions.cpp | 2 +- src/game.cpp | 8 +++++++- src/intermission.cpp | 9 +++++++-- src/math.h | 37 ------------------------------------- src/player.cpp | 6 ++++-- src/script.cpp | 6 ++++-- src/title.cpp | 4 ++-- 13 files changed, 56 insertions(+), 86 deletions(-) delete mode 100644 src/math.h diff --git a/src/Starfighter.h b/src/Starfighter.h index 6ffade6..fe53e85 100644 --- a/src/Starfighter.h +++ b/src/Starfighter.h @@ -50,7 +50,6 @@ along with this program. If not, see . #include "init.h" #include "intermission.h" #include "loadSave.h" -#include "math.h" #include "messages.h" #include "misc.h" #include "missions.h" diff --git a/src/alien.cpp b/src/alien.cpp index 7b9f263..7cd8473 100644 --- a/src/alien.cpp +++ b/src/alien.cpp @@ -831,8 +831,8 @@ bool alien_add() if (aliens[index].classDef == CD_ESCORT) aliens[index].shield = 50; - aliens[index].dx = rrand(-2, 2); - aliens[index].dy = rrand(-2, 2); + aliens[index].dx = RANDRANGE(-2, 2); + aliens[index].dy = RANDRANGE(-2, 2); aliens[index].ammo[0] = 0; @@ -915,16 +915,8 @@ void alien_addFriendly(int type) aliens[type].owner = &aliens[type]; aliens[type].target = &aliens[type]; aliens[type].active = true; - - if (rand() % 2 == 0) - 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)); + aliens[type].x = RANDRANGE((screen->w / 2) - 150, (screen->w / 2) + 150); + aliens[type].y = RANDRANGE((screen->h / 2) - 150, (screen->h / 2) + 150); if (type == ALIEN_PHOEBE) aliens[type].classDef = CD_PHOEBE; @@ -940,19 +932,19 @@ void alien_addFriendly(int type) bool alien_place(object *alien) { if (rand() % 2 == 0) - alien->x = rrand(screen->w, screen->w * 2); + alien->x = RANDRANGE(screen->w, screen->w * 2); else - alien->x = rrand(-screen->w, 0); + alien->x = RANDRANGE(-screen->w, 0); if (rand() % 2 == 0) - alien->y = rrand(screen->h, screen->h * 2); + alien->y = RANDRANGE(screen->h, screen->h * 2); else - alien->y = rrand(-screen->h, 0); + alien->y = RANDRANGE(-screen->h, 0); if (currentGame.area == 24) { 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++) diff --git a/src/bullet.cpp b/src/bullet.cpp index 0a60851..e2a88f3 100644 --- a/src/bullet.cpp +++ b/src/bullet.cpp @@ -55,7 +55,7 @@ void bullet_add(object *theWeapon, object *attacker, int y, int dy) if (bullet->flags & WF_VARIABLE_SPEED) { - bullet->dx = rrand(100, 200); + bullet->dx = RANDRANGE(100, 200); bullet->dx /= 10; if (attacker->face == 1) 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) { - bullet->dy = rrand(-200, 200); + bullet->dy = RANDRANGE(-200, 200); if (bullet->dy != 0) bullet->dy /= 200; } @@ -127,8 +127,8 @@ void bullet_add(object *theWeapon, object *attacker, int y, int dy) if (attacker->classDef == CD_ASTEROID) { - bullet->dx = rrand(-20, 20); - bullet->dy = rrand(-20, 20); + bullet->dx = RANDRANGE(-20, 20); + bullet->dy = RANDRANGE(-20, 20); bullet->image[0] = shape[4]; } @@ -267,8 +267,8 @@ void doBullets() { for (int i = 0 ; i < bullet->damage * 2 ; i++) blit(bullet->image[0], - (int)(bullet->x - rrand(-(bullet->damage * 2 / 3), 0)), - (int)(bullet->y + rrand(-3, 3))); + (int)(bullet->x - RANDRANGE(-(bullet->damage * 2 / 3), 0)), + (int)(bullet->y + RANDRANGE(-3, 3))); } blit(bullet->image[0], (int)bullet->x, (int)bullet->y); @@ -353,8 +353,8 @@ void doBullets() bullet->shield = 0; audio_playSound(SFX_EXPLOSION, bullet->x); for (int i = 0 ; i < 10 ; i++) - addExplosion(bullet->x + rrand(-35, 35), - bullet->y + rrand(-35, 35), + addExplosion(bullet->x + RANDRANGE(-35, 35), + bullet->y + RANDRANGE(-35, 35), E_BIG_EXPLOSION); } } @@ -405,8 +405,8 @@ void doBullets() bullet->shield = 0; audio_playSound(SFX_EXPLOSION, bullet->x); for (int i = 0 ; i < 10 ; i++) - addExplosion(bullet->x + rrand(-35, 35), - bullet->y + rrand(-35, 35), E_BIG_EXPLOSION); + addExplosion(bullet->x + RANDRANGE(-35, 35), + bullet->y + RANDRANGE(-35, 35), E_BIG_EXPLOSION); } } else @@ -443,8 +443,8 @@ void doBullets() theCargo->active = false; audio_playSound(SFX_EXPLOSION, theCargo->x); for (int i = 0 ; i < 10 ; i++) - addExplosion(theCargo->x + rrand(-15, 15), - theCargo->y + rrand(-15, 15), + addExplosion(theCargo->x + RANDRANGE(-15, 15), + theCargo->y + RANDRANGE(-15, 15), E_BIG_EXPLOSION); updateMissionRequirements(M_PROTECT_PICKUP, P_CARGO, 1); @@ -465,8 +465,8 @@ void doBullets() { audio_playSound(SFX_EXPLOSION, bullet->x); for (int i = 0 ; i < 10 ; i++) - addExplosion(bullet->x + rrand(-35, 35), - bullet->y + rrand(-35, 35), E_BIG_EXPLOSION); + addExplosion(bullet->x + RANDRANGE(-35, 35), + bullet->y + RANDRANGE(-35, 35), E_BIG_EXPLOSION); if (checkPlayerShockDamage(bullet->x, bullet->y)) setInfoLine("Warning: Missile Shockwave Damage!!", diff --git a/src/collectable.cpp b/src/collectable.cpp index b074f49..db5a8f3 100644 --- a/src/collectable.cpp +++ b/src/collectable.cpp @@ -136,11 +136,11 @@ void addCollectable(float x, float y, int type, int value, int life) collectable->x = x; collectable->y = y; - collectable->dx = rrand(-100, 100); + collectable->dx = RANDRANGE(-100, 100); if (collectable->dx != 0) collectable->dx /= 100; - collectable->dy = rrand(-100, 100); + collectable->dy = RANDRANGE(-100, 100); if (collectable->dy != 0) collectable->dy /= 100; diff --git a/src/debris.cpp b/src/debris.cpp index a592fa6..7fdd46e 100644 --- a/src/debris.cpp +++ b/src/debris.cpp @@ -28,7 +28,7 @@ void addDebris(int x, int y, int amount) object *debris; - amount = rrand(3, rand() % amount); + amount = RANDRANGE(3, rand() % amount); LIMIT(amount, 3, 8); for (int i = 0 ; i < amount ; i++) @@ -39,10 +39,10 @@ void addDebris(int x, int y, int amount) debris->x = x; debris->y = y; - debris->thinktime = rrand(60, 180); + debris->thinktime = RANDRANGE(60, 180); - debris->dx = rrand(-500, 500); - debris->dy = rrand(-500, 500); + debris->dx = RANDRANGE(-500, 500); + debris->dy = RANDRANGE(-500, 500); if (debris->dx != 0) debris->dx /= 100; @@ -74,7 +74,7 @@ void doDebris() debris->x += debris->dx; 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) diff --git a/src/defs.h b/src/defs.h index 318a71e..4335459 100644 --- a/src/defs.h +++ b/src/defs.h @@ -29,6 +29,7 @@ along with this program. If not, see . #define WRAP_ADD(x, y, a, b) x = (((x) + (y)) + \ ((x) + (y) < (a) ? ((b) - (a)) : 0) + \ ((x) + (y) > (b) ? ((a) - (b)) : 0)) +#define RANDRANGE(x, y) (((x) < (y)) ? ((x) + (rand() % (1 + (y) - (x)))) : (x)) // ALL #define NONE 0 diff --git a/src/explosions.cpp b/src/explosions.cpp index f4a8017..d4de258 100644 --- a/src/explosions.cpp +++ b/src/explosions.cpp @@ -53,7 +53,7 @@ void addEngine(object *craft) float x = craft->x + (craft->engineX * craft->face); float y = craft->y + craft->engineY; - y += rrand(-3, 3); + y += RANDRANGE(-3, 3); addExplosion(x, y, E_TINY_EXPLOSION); } diff --git a/src/game.cpp b/src/game.cpp index 4c163ec..17705ed 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -920,7 +920,13 @@ int mainGameLoop() if ((currentGame.area == 24) && (engine.addAliens > -1)) { 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); } diff --git a/src/intermission.cpp b/src/intermission.cpp index 4c8ce96..7aeea38 100644 --- a/src/intermission.cpp +++ b/src/intermission.cpp @@ -628,8 +628,13 @@ int galaxyMap() if (rand() % 1000 < 2) { - engine.ssx = rrand(100, 100); - engine.ssy = rrand(100, 100); + // XXX: This code originally had the range set to [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.ssy /= 100; } diff --git a/src/math.h b/src/math.h deleted file mode 100644 index f774378..0000000 --- a/src/math.h +++ /dev/null @@ -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 . -*/ - -#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 diff --git a/src/player.cpp b/src/player.cpp index 8ff47a5..9d50562 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -308,7 +308,8 @@ void doPlayer() blit(shipShape[shapeToUse], (int)player.x, (int)player.y); if ((player.maxShield > 1) && (player.shield <= engine.lowShield) && (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 { @@ -332,7 +333,8 @@ void doPlayer() engine.keyState[KEY_UP] = engine.keyState[KEY_DOWN] = engine.keyState[KEY_LEFT] = engine.keyState[KEY_RIGHT] = 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) addDebris((int)player.x, (int)player.y, player.maxShield); } diff --git a/src/script.cpp b/src/script.cpp index 8537f6a..91ceffc 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -103,8 +103,10 @@ void checkScriptEvents() else { aliens[gameEvent[i].entity].active = true; - aliens[gameEvent[i].entity].x = rrand((int)player.x + 400, (int)player.x + 800); - aliens[gameEvent[i].entity].y = rrand((int)player.y - 400, (int)player.y + 800); + aliens[gameEvent[i].entity].x = ((int)player.x + + RANDRANGE(400, 800)); + aliens[gameEvent[i].entity].y = ((int)player.y + + RANDRANGE(-400, 800)); } } diff --git a/src/title.cpp b/src/title.cpp index 5af6c19..e9214b5 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -649,8 +649,8 @@ void gameover() updateScreen(); unBuffer(); - x = ((screen->w - gameover->w) / 2) - rrand(-2, 2); - y = ((screen->h - gameover->h) / 2) - rrand(-2, 2); + x = ((screen->w - gameover->w) / 2) - RANDRANGE(-2, 2); + y = ((screen->h - gameover->h) / 2) - RANDRANGE(-2, 2); blit(gameover, x, y); delayFrame();