From ad8a2f2ad9a2ad51da2e35a55530ef28eab8c2c8 Mon Sep 17 00:00:00 2001 From: onpon4 Date: Tue, 17 Mar 2015 19:00:13 -0400 Subject: [PATCH] Replaced various "limit" functions with LIMIT and LIMIT_ADD macros. This is mainly because the old "limit" functions were all restricted to certain types, which is incredibly silly given how simple they are. Macros are much simpler, and a warning gets raised if they're used improperly with multiple types, anyway. In the process, I also found and fixed a bug: it seems the original author intended for escaping enemies to gradually accelerate to fleeing speed, but the low value was indicated as the max value, and the way limitFloat was written, that caused the max value to be used (it was supposed to reduce the speed to a minimum of -15, but it instead effectively assigned the speed to -15). It might be a good idea to re-implement the old buggy behavior intentionally; depends on whether the acceleration of jumping looks better or worse than just immediately going to jump speed. --- src/alien.cpp | 2 +- src/bullet.cpp | 16 +++++++------- src/cargo.cpp | 4 ++-- src/collectable.cpp | 7 +++--- src/debris.cpp | 2 +- src/defs.h | 4 ++++ src/game.cpp | 28 ++++++++++++++---------- src/intermission.cpp | 4 ++-- src/math.h | 52 -------------------------------------------- src/misc.cpp | 2 +- src/player.cpp | 12 +++++----- src/shop.cpp | 12 +++++----- 12 files changed, 51 insertions(+), 94 deletions(-) diff --git a/src/alien.cpp b/src/alien.cpp index 284dd98..0b0f3d2 100644 --- a/src/alien.cpp +++ b/src/alien.cpp @@ -813,7 +813,7 @@ bool alien_add() aliens[index].deathCounter = 0 - (aliens[index].maxShield * 3); aliens[index].hit = 0; - limitInt(&aliens[index].deathCounter, -250, 0); + LIMIT(aliens[index].deathCounter, -250, 0); // Attempts to place an alien. If it fails, the alien is deactivated. for (int i = 0 ; i < 100 ; i++) diff --git a/src/bullet.cpp b/src/bullet.cpp index e6c3383..ca68793 100644 --- a/src/bullet.cpp +++ b/src/bullet.cpp @@ -200,7 +200,7 @@ char checkPlayerShockDamage(float x, float y) player.shield -= (int)(10 - distX); player.shield -= (int)(10 - distY); - limitInt(&player.shield, 0, player.maxShield); + LIMIT(player.shield, 0, player.maxShield); player.hit = 10; return 1; @@ -278,18 +278,18 @@ void doBullets() if (bullet->target != NULL) { if (bullet->x < bullet->target->x) - limitFloat(&(bullet->dx += homingMissileSpeed), -15, 15); - if (bullet->x > bullet->target->x) - limitFloat(&(bullet->dx -= homingMissileSpeed), -15, 15); + LIMIT_ADD(bullet->dx, homingMissileSpeed, -15, 15); + else if (bullet->x > bullet->target->x) + LIMIT_ADD(bullet->dx, -homingMissileSpeed, -15, 15); //Rocket is (more or less) inline with target. Fly straight if ((bullet->x > bullet->target->x - 1) && (bullet->x < bullet->target->x + 5)) bullet->dx = 0; if (bullet->y < bullet->target->y) - limitFloat(&(bullet->dy += homingMissileSpeed), -15, 15); - if (bullet->y > bullet->target->y) - limitFloat(&(bullet->dy -= homingMissileSpeed), -15, 15); + LIMIT_ADD(bullet->dy, homingMissileSpeed, -15, 15); + else if (bullet->y > bullet->target->y) + LIMIT_ADD(bullet->dy, -homingMissileSpeed, -15, 15); //Rocket is (more or less) inline with target. Fly straight if ((bullet->y > bullet->target->y - 1) && (bullet->y < bullet->target->y + 5)) @@ -388,7 +388,7 @@ void doBullets() setInfoLine("!!! WARNING: SHIELD LOW !!!", FONT_RED); player.shield -= bullet->damage; - limitInt(&player.shield, 0, player.maxShield); + LIMIT(player.shield, 0, player.maxShield); player.hit = 5; } diff --git a/src/cargo.cpp b/src/cargo.cpp index b9be49d..9b16f07 100644 --- a/src/cargo.cpp +++ b/src/cargo.cpp @@ -101,8 +101,8 @@ void doCargo() cargo[i].x += engine.ssx + engine.smx; cargo[i].y += engine.ssy + engine.smy; - limitFloat(&cargo[i].x, cargo[i].owner->x - 50, cargo[i].owner->x + 50); - limitFloat(&cargo[i].y, cargo[i].owner->y - 50, cargo[i].owner->y + 50); + LIMIT(cargo[i].x, cargo[i].owner->x - 50, cargo[i].owner->x + 50); + LIMIT(cargo[i].y, cargo[i].owner->y - 50, cargo[i].owner->y + 50); dx = (cargo[i].x - cargo[i].owner->x) / 10; dy = (cargo[i].y - cargo[i].owner->y) / 10; diff --git a/src/collectable.cpp b/src/collectable.cpp index 8479d11..7c361b4 100644 --- a/src/collectable.cpp +++ b/src/collectable.cpp @@ -329,7 +329,7 @@ void doCollectables() break; case P_ROCKET: - limitCharAdd(&player.ammo[1], collectable->value, 0, + LIMIT_ADD(player.ammo[1], collectable->value, 0, currentGame.maxRocketAmmo); if (player.ammo[1] == currentGame.maxRocketAmmo) sprintf(temp, "Rocket Ammo at Maximum"); @@ -344,7 +344,7 @@ void doCollectables() break; case P_SHIELD: - limitInt(&(player.shield += 10), 0, player.maxShield); + LIMIT_ADD(player.shield, 10, 0, player.maxShield); currentGame.shieldPickups ++; sprintf(temp, "Restored 10 shield points"); break; @@ -405,7 +405,8 @@ void doCollectables() sprintf(temp, "Plasma cells already at Maximum"); else { - limitCharAdd(&player.ammo[0], collectable->value, 0, currentGame.maxPlasmaAmmo); + LIMIT_ADD(player.ammo[0], collectable->value, + 0, currentGame.maxPlasmaAmmo); if (collectable->value > 1) { sprintf(temp, "Got %d plasma cells", collectable->value); diff --git a/src/debris.cpp b/src/debris.cpp index db132ff..a592fa6 100644 --- a/src/debris.cpp +++ b/src/debris.cpp @@ -29,7 +29,7 @@ void addDebris(int x, int y, int amount) object *debris; amount = rrand(3, rand() % amount); - limitInt(&amount, 3, 8); + LIMIT(amount, 3, 8); for (int i = 0 ; i < amount ; i++) { diff --git a/src/defs.h b/src/defs.h index 3dcb3d3..a76c58b 100644 --- a/src/defs.h +++ b/src/defs.h @@ -22,6 +22,10 @@ along with this program. If not, see . #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define LIMIT(x, a, b) x = ((x) < (b) ? ((x) > (a) ? (x) : (a)) : (b)) +#define LIMIT_ADD(x, y, a, b) x = (((x) + (y)) < (b) ? \ + (((x) + (y)) > (a) ? \ + ((x) + (y)) : (a)) : (b)) // ALL #define NONE 0 diff --git a/src/game.cpp b/src/game.cpp index d1c07be..0d98977 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -395,7 +395,7 @@ int mainGameLoop() { aliens[i].systemPower = aliens[i].maxShield; aliens[i].deathCounter = 0 - (aliens[i].maxShield * 3); - limitInt(&aliens[i].deathCounter, -350, 0); + LIMIT(aliens[i].deathCounter, -350, 0); } // Set target energy meter @@ -536,7 +536,7 @@ int mainGameLoop() } else if ((currentGame.area == 26) && (engine.musicVolume > 0)) { - limitFloat(&(engine.musicVolume -= 0.2), 0, 100); + LIMIT_ADD(engine.musicVolume, -0.2, 0, 100); audio_setMusicVolume((int)engine.musicVolume); } else @@ -551,7 +551,7 @@ int mainGameLoop() } else { - limitFloat(&(engine.musicVolume -= 0.2), 0, 100); + LIMIT_ADD(engine.musicVolume, -0.2, 0, 100); audio_setMusicVolume((int)engine.musicVolume); if (SDL_GetTicks() >= engine.missionCompleteTimer) { @@ -615,7 +615,7 @@ int mainGameLoop() canFire = true; // The alien is allowed to fire - limitInt(&--alien->thinktime, 0, 250); + LIMIT_ADD(alien->thinktime, -1, 0, 250); if (alien->target->shield < 1) alien->target = alien; @@ -659,8 +659,8 @@ int mainGameLoop() alien->face = (alien->dx > 0); } - limitFloat(&alien->dx, 0 - alien->speed, alien->speed); - limitFloat(&alien->dy, 0 - alien->speed, alien->speed); + LIMIT(alien->dx, -alien->speed, alien->speed); + LIMIT(alien->dy, -alien->speed, alien->speed); } @@ -678,7 +678,11 @@ int mainGameLoop() if (alien->flags & FL_LEAVESECTOR) { - limitFloat(&(alien->dx -= 0.5), 0, -15); + // Note: The original version of this line incorrectly + // specified -15 as the *maximum* instead of the + // *minimum*, which at the time was equivalent to + // ``alien->dx = -15``. + LIMIT_ADD(alien->dx, -0.5, -15, 0); alien->dy = 0; alien->thinktime = 999; alien->face = 0; @@ -728,12 +732,12 @@ int mainGameLoop() if (alien->classDef == CD_MOBILESHIELD) { - limitInt(&(++aliens[ALIEN_BOSS].shield), 0, + LIMIT_ADD(aliens[ALIEN_BOSS].shield, 1, 0, aliens[ALIEN_BOSS].maxShield); } - limitCharAdd(&alien->reload[0], -1, 0, 999); - limitCharAdd(&alien->reload[1], -1, 0, 999); + LIMIT_ADD(alien->reload[0], -1, 0, 999); + LIMIT_ADD(alien->reload[1], -1, 0, 999); if ((!(alien->flags & FL_DISABLED)) && (!(alien->flags & FL_NOFIRE))) @@ -788,7 +792,7 @@ int mainGameLoop() } else { - limitCharAdd(&alien->ammo[0], 1, 0, 250); + LIMIT_ADD(alien->ammo[0], 1, 0, 250); } if (alien->flags & FL_FIRELASER) @@ -818,7 +822,7 @@ int mainGameLoop() if (alien->hit) shapeToUse += SHIP_HIT_INDEX; - limitCharAdd(&alien->hit, -1, 0, 100); + LIMIT_ADD(alien->hit, -1, 0, 100); if ((alien->x + alien->image[0]->w > 0) && (alien->x < screen->w) && diff --git a/src/intermission.cpp b/src/intermission.cpp index 86d8adf..0e69c8a 100644 --- a/src/intermission.cpp +++ b/src/intermission.cpp @@ -33,8 +33,8 @@ static void doCursor() { getPlayerInput(); - limitInt(&engine.cursor_x, 10, screen->w - 10 - shape[0]->w); - limitInt(&engine.cursor_y, 10, screen->h - 10 - shape[0]->h); + LIMIT(engine.cursor_x, 10, screen->w - 10 - shape[0]->w); + LIMIT(engine.cursor_y, 10, screen->h - 10 - shape[0]->h); blit(shape[0], engine.cursor_x, engine.cursor_y); } diff --git a/src/math.h b/src/math.h index b618191..14e4df1 100644 --- a/src/math.h +++ b/src/math.h @@ -20,58 +20,6 @@ along with this program. If not, see . #ifndef MATH_H #define MATH_H -static inline void limitChar(signed char *in, int low, int high) -{ - if (*in < low) - *in = low; - if (*in > high) - *in = high; -} - -static inline void limitChar(unsigned char *in, int low, int high) -{ - if (*in < low) - *in = low; - if (*in > high) - *in = high; -} - -static inline void limitCharAdd(signed char *in, int add, int low, int high) -{ - int tmp = (int)*in + add; - if (tmp < low) - tmp = low; - if (tmp > high) - tmp = high; - *in = tmp; -} - -static inline void limitCharAdd(unsigned char *in, int add, int low, int high) -{ - int tmp = (int)*in + add; - if (tmp < low) - tmp = low; - if (tmp > high) - tmp = high; - *in = tmp; -} - -static inline void limitInt(int *in, int low, int high) -{ - if (*in < low) - *in = low; - if (*in > high) - *in = high; -} - -static inline void limitFloat(float *in, float low, float high) -{ - if (*in < low) - *in = low; - if (*in > high) - *in = high; -} - static inline void wrapChar(signed char *in, signed char low, signed char high) { if (*in < low) diff --git a/src/misc.cpp b/src/misc.cpp index 8489807..cafd04e 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -389,7 +389,7 @@ void doInfo() return; if ((!engine.keyState[KEY_ALTFIRE]) && (player.weaponType[1] == W_LASER) && (engine.eventTimer % 8 == 1)) - limitCharAdd(&player.ammo[1], -1, 1, 255); + LIMIT_ADD(player.ammo[1], -1, 1, 255); if ((engine.eventTimer < 30) && (player.shield <= engine.lowShield)) return; diff --git a/src/player.cpp b/src/player.cpp index 94df121..3c9a372 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -133,7 +133,7 @@ void doPlayer() { if (!charger_fired) { - limitCharAdd(&player.ammo[1], 1, 0, 150); + LIMIT_ADD(player.ammo[1], 1, 0, 150); if (player.ammo[1] >= 150) { ship_fireBullet(&player, 1); @@ -171,8 +171,8 @@ void doPlayer() engine.keyState[KEY_SWITCH] = 0; } - limitCharAdd(&player.reload[0], -1, 0, 999); - limitCharAdd(&player.reload[1], -1, 0, 999); + LIMIT_ADD(player.reload[0], -1, 0, 999); + LIMIT_ADD(player.reload[1], -1, 0, 999); if (engine.keyState[KEY_UP]) { @@ -299,7 +299,7 @@ void doPlayer() if (player.hit) shapeToUse += SHIP_HIT_INDEX; - limitCharAdd(&player.hit, -1, 0, 100); + LIMIT_ADD(player.hit, -1, 0, 100); blit(shipShape[shapeToUse], (int)player.x, (int)player.y); if ((player.maxShield > 1) && (player.shield <= engine.lowShield) && @@ -334,8 +334,8 @@ void doPlayer() } } - limitFloat(&engine.ssx, -cameraMaxSpeed, cameraMaxSpeed); - limitFloat(&engine.ssy, -cameraMaxSpeed, cameraMaxSpeed); + LIMIT(engine.ssx, -cameraMaxSpeed, cameraMaxSpeed); + LIMIT(engine.ssy, -cameraMaxSpeed, cameraMaxSpeed); // Specific for the mission were you have to chase the Executive Transport if ((currentGame.area == 18) && (aliens[ALIEN_BOSS].shield > 0) && diff --git a/src/shop.cpp b/src/shop.cpp index 5c4cb3e..35ff3d8 100644 --- a/src/shop.cpp +++ b/src/shop.cpp @@ -549,7 +549,7 @@ static void buy(int i) shopSelectedItem = -4; return; } - limitCharAdd(&player.ammo[0], 10, 0, currentGame.maxPlasmaAmmo); + LIMIT_ADD(player.ammo[0], 10, 0, currentGame.maxPlasmaAmmo); break; case SHOP_ROCKET_AMMO: @@ -668,7 +668,7 @@ static void buy(int i) } shop_sellSecondaryWeapon(); player.weaponType[1] = W_DOUBLE_ROCKETS; - limitChar(¤tGame.maxRocketAmmo, 5, 50); + LIMIT(currentGame.maxRocketAmmo, 5, 50); shopSelectedItem = -1; break; @@ -680,7 +680,7 @@ static void buy(int i) } shop_sellSecondaryWeapon(); player.weaponType[1] = W_MICRO_ROCKETS; - limitChar(¤tGame.maxRocketAmmo, 5, 50); + LIMIT(currentGame.maxRocketAmmo, 5, 50); shopSelectedItem = -1; break; @@ -713,7 +713,7 @@ static void buy(int i) while (currentGame.maxRocketAmmo > maxHoming) sell(SHOP_ROCKET_MAX_AMMO); - limitChar(¤tGame.maxRocketAmmo, 5, maxHoming); + LIMIT(currentGame.maxRocketAmmo, 5, maxHoming); shopSelectedItem = -1; break; @@ -746,7 +746,7 @@ static void buy(int i) while (currentGame.maxRocketAmmo > maxDoubleHoming) sell(SHOP_ROCKET_MAX_AMMO); - limitChar(¤tGame.maxRocketAmmo, 5, maxDoubleHoming); + LIMIT(currentGame.maxRocketAmmo, 5, maxDoubleHoming); shopSelectedItem = -1; break; @@ -762,7 +762,7 @@ static void buy(int i) while (currentGame.maxRocketAmmo > maxMicroHoming) sell(SHOP_ROCKET_MAX_AMMO); - limitChar(¤tGame.maxRocketAmmo, 5, maxMicroHoming); + LIMIT(currentGame.maxRocketAmmo, 5, maxMicroHoming); shopSelectedItem = -1; break; }