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.
This commit is contained in:
onpon4 2015-03-17 19:00:13 -04:00
parent 05c370feb4
commit ad8a2f2ad9
12 changed files with 51 additions and 94 deletions

View File

@ -813,7 +813,7 @@ bool alien_add()
aliens[index].deathCounter = 0 - (aliens[index].maxShield * 3); aliens[index].deathCounter = 0 - (aliens[index].maxShield * 3);
aliens[index].hit = 0; 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. // Attempts to place an alien. If it fails, the alien is deactivated.
for (int i = 0 ; i < 100 ; i++) for (int i = 0 ; i < 100 ; i++)

View File

@ -200,7 +200,7 @@ char checkPlayerShockDamage(float x, float y)
player.shield -= (int)(10 - distX); player.shield -= (int)(10 - distX);
player.shield -= (int)(10 - distY); player.shield -= (int)(10 - distY);
limitInt(&player.shield, 0, player.maxShield); LIMIT(player.shield, 0, player.maxShield);
player.hit = 10; player.hit = 10;
return 1; return 1;
@ -278,18 +278,18 @@ void doBullets()
if (bullet->target != NULL) if (bullet->target != NULL)
{ {
if (bullet->x < bullet->target->x) if (bullet->x < bullet->target->x)
limitFloat(&(bullet->dx += homingMissileSpeed), -15, 15); LIMIT_ADD(bullet->dx, homingMissileSpeed, -15, 15);
if (bullet->x > bullet->target->x) else if (bullet->x > bullet->target->x)
limitFloat(&(bullet->dx -= homingMissileSpeed), -15, 15); LIMIT_ADD(bullet->dx, -homingMissileSpeed, -15, 15);
//Rocket is (more or less) inline with target. Fly straight //Rocket is (more or less) inline with target. Fly straight
if ((bullet->x > bullet->target->x - 1) && (bullet->x < bullet->target->x + 5)) if ((bullet->x > bullet->target->x - 1) && (bullet->x < bullet->target->x + 5))
bullet->dx = 0; bullet->dx = 0;
if (bullet->y < bullet->target->y) if (bullet->y < bullet->target->y)
limitFloat(&(bullet->dy += homingMissileSpeed), -15, 15); LIMIT_ADD(bullet->dy, homingMissileSpeed, -15, 15);
if (bullet->y > bullet->target->y) else if (bullet->y > bullet->target->y)
limitFloat(&(bullet->dy -= homingMissileSpeed), -15, 15); LIMIT_ADD(bullet->dy, -homingMissileSpeed, -15, 15);
//Rocket is (more or less) inline with target. Fly straight //Rocket is (more or less) inline with target. Fly straight
if ((bullet->y > bullet->target->y - 1) && (bullet->y < bullet->target->y + 5)) 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); setInfoLine("!!! WARNING: SHIELD LOW !!!", FONT_RED);
player.shield -= bullet->damage; player.shield -= bullet->damage;
limitInt(&player.shield, 0, player.maxShield); LIMIT(player.shield, 0, player.maxShield);
player.hit = 5; player.hit = 5;
} }

View File

@ -101,8 +101,8 @@ void doCargo()
cargo[i].x += engine.ssx + engine.smx; cargo[i].x += engine.ssx + engine.smx;
cargo[i].y += engine.ssy + engine.smy; cargo[i].y += engine.ssy + engine.smy;
limitFloat(&cargo[i].x, cargo[i].owner->x - 50, cargo[i].owner->x + 50); LIMIT(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].y, cargo[i].owner->y - 50, cargo[i].owner->y + 50);
dx = (cargo[i].x - cargo[i].owner->x) / 10; dx = (cargo[i].x - cargo[i].owner->x) / 10;
dy = (cargo[i].y - cargo[i].owner->y) / 10; dy = (cargo[i].y - cargo[i].owner->y) / 10;

View File

@ -329,7 +329,7 @@ void doCollectables()
break; break;
case P_ROCKET: case P_ROCKET:
limitCharAdd(&player.ammo[1], collectable->value, 0, LIMIT_ADD(player.ammo[1], collectable->value, 0,
currentGame.maxRocketAmmo); currentGame.maxRocketAmmo);
if (player.ammo[1] == currentGame.maxRocketAmmo) if (player.ammo[1] == currentGame.maxRocketAmmo)
sprintf(temp, "Rocket Ammo at Maximum"); sprintf(temp, "Rocket Ammo at Maximum");
@ -344,7 +344,7 @@ void doCollectables()
break; break;
case P_SHIELD: case P_SHIELD:
limitInt(&(player.shield += 10), 0, player.maxShield); LIMIT_ADD(player.shield, 10, 0, player.maxShield);
currentGame.shieldPickups ++; currentGame.shieldPickups ++;
sprintf(temp, "Restored 10 shield points"); sprintf(temp, "Restored 10 shield points");
break; break;
@ -405,7 +405,8 @@ void doCollectables()
sprintf(temp, "Plasma cells already at Maximum"); sprintf(temp, "Plasma cells already at Maximum");
else else
{ {
limitCharAdd(&player.ammo[0], collectable->value, 0, currentGame.maxPlasmaAmmo); LIMIT_ADD(player.ammo[0], collectable->value,
0, currentGame.maxPlasmaAmmo);
if (collectable->value > 1) if (collectable->value > 1)
{ {
sprintf(temp, "Got %d plasma cells", collectable->value); sprintf(temp, "Got %d plasma cells", collectable->value);

View File

@ -29,7 +29,7 @@ void addDebris(int x, int y, int amount)
object *debris; object *debris;
amount = rrand(3, rand() % amount); amount = rrand(3, rand() % amount);
limitInt(&amount, 3, 8); LIMIT(amount, 3, 8);
for (int i = 0 ; i < amount ; i++) for (int i = 0 ; i < amount ; i++)
{ {

View File

@ -22,6 +22,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(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 // ALL
#define NONE 0 #define NONE 0

View File

@ -395,7 +395,7 @@ int mainGameLoop()
{ {
aliens[i].systemPower = aliens[i].maxShield; aliens[i].systemPower = aliens[i].maxShield;
aliens[i].deathCounter = 0 - (aliens[i].maxShield * 3); aliens[i].deathCounter = 0 - (aliens[i].maxShield * 3);
limitInt(&aliens[i].deathCounter, -350, 0); LIMIT(aliens[i].deathCounter, -350, 0);
} }
// Set target energy meter // Set target energy meter
@ -536,7 +536,7 @@ int mainGameLoop()
} }
else if ((currentGame.area == 26) && (engine.musicVolume > 0)) 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); audio_setMusicVolume((int)engine.musicVolume);
} }
else else
@ -551,7 +551,7 @@ int mainGameLoop()
} }
else else
{ {
limitFloat(&(engine.musicVolume -= 0.2), 0, 100); LIMIT_ADD(engine.musicVolume, -0.2, 0, 100);
audio_setMusicVolume((int)engine.musicVolume); audio_setMusicVolume((int)engine.musicVolume);
if (SDL_GetTicks() >= engine.missionCompleteTimer) if (SDL_GetTicks() >= engine.missionCompleteTimer)
{ {
@ -615,7 +615,7 @@ int mainGameLoop()
canFire = true; // The alien is allowed to fire 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) if (alien->target->shield < 1)
alien->target = alien; alien->target = alien;
@ -659,8 +659,8 @@ int mainGameLoop()
alien->face = (alien->dx > 0); alien->face = (alien->dx > 0);
} }
limitFloat(&alien->dx, 0 - alien->speed, alien->speed); LIMIT(alien->dx, -alien->speed, alien->speed);
limitFloat(&alien->dy, 0 - alien->speed, alien->speed); LIMIT(alien->dy, -alien->speed, alien->speed);
} }
@ -678,7 +678,11 @@ int mainGameLoop()
if (alien->flags & FL_LEAVESECTOR) 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->dy = 0;
alien->thinktime = 999; alien->thinktime = 999;
alien->face = 0; alien->face = 0;
@ -728,12 +732,12 @@ int mainGameLoop()
if (alien->classDef == CD_MOBILESHIELD) if (alien->classDef == CD_MOBILESHIELD)
{ {
limitInt(&(++aliens[ALIEN_BOSS].shield), 0, LIMIT_ADD(aliens[ALIEN_BOSS].shield, 1, 0,
aliens[ALIEN_BOSS].maxShield); aliens[ALIEN_BOSS].maxShield);
} }
limitCharAdd(&alien->reload[0], -1, 0, 999); LIMIT_ADD(alien->reload[0], -1, 0, 999);
limitCharAdd(&alien->reload[1], -1, 0, 999); LIMIT_ADD(alien->reload[1], -1, 0, 999);
if ((!(alien->flags & FL_DISABLED)) && if ((!(alien->flags & FL_DISABLED)) &&
(!(alien->flags & FL_NOFIRE))) (!(alien->flags & FL_NOFIRE)))
@ -788,7 +792,7 @@ int mainGameLoop()
} }
else else
{ {
limitCharAdd(&alien->ammo[0], 1, 0, 250); LIMIT_ADD(alien->ammo[0], 1, 0, 250);
} }
if (alien->flags & FL_FIRELASER) if (alien->flags & FL_FIRELASER)
@ -818,7 +822,7 @@ int mainGameLoop()
if (alien->hit) if (alien->hit)
shapeToUse += SHIP_HIT_INDEX; 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) && if ((alien->x + alien->image[0]->w > 0) &&
(alien->x < screen->w) && (alien->x < screen->w) &&

View File

@ -33,8 +33,8 @@ static void doCursor()
{ {
getPlayerInput(); getPlayerInput();
limitInt(&engine.cursor_x, 10, screen->w - 10 - shape[0]->w); LIMIT(engine.cursor_x, 10, screen->w - 10 - shape[0]->w);
limitInt(&engine.cursor_y, 10, screen->h - 10 - shape[0]->h); LIMIT(engine.cursor_y, 10, screen->h - 10 - shape[0]->h);
blit(shape[0], engine.cursor_x, engine.cursor_y); blit(shape[0], engine.cursor_x, engine.cursor_y);
} }

View File

@ -20,58 +20,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef MATH_H #ifndef MATH_H
#define 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) static inline void wrapChar(signed char *in, signed char low, signed char high)
{ {
if (*in < low) if (*in < low)

View File

@ -389,7 +389,7 @@ void doInfo()
return; return;
if ((!engine.keyState[KEY_ALTFIRE]) && (player.weaponType[1] == W_LASER) && (engine.eventTimer % 8 == 1)) 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)) if ((engine.eventTimer < 30) && (player.shield <= engine.lowShield))
return; return;

View File

@ -133,7 +133,7 @@ void doPlayer()
{ {
if (!charger_fired) if (!charger_fired)
{ {
limitCharAdd(&player.ammo[1], 1, 0, 150); LIMIT_ADD(player.ammo[1], 1, 0, 150);
if (player.ammo[1] >= 150) if (player.ammo[1] >= 150)
{ {
ship_fireBullet(&player, 1); ship_fireBullet(&player, 1);
@ -171,8 +171,8 @@ void doPlayer()
engine.keyState[KEY_SWITCH] = 0; engine.keyState[KEY_SWITCH] = 0;
} }
limitCharAdd(&player.reload[0], -1, 0, 999); LIMIT_ADD(player.reload[0], -1, 0, 999);
limitCharAdd(&player.reload[1], -1, 0, 999); LIMIT_ADD(player.reload[1], -1, 0, 999);
if (engine.keyState[KEY_UP]) if (engine.keyState[KEY_UP])
{ {
@ -299,7 +299,7 @@ void doPlayer()
if (player.hit) if (player.hit)
shapeToUse += SHIP_HIT_INDEX; 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); 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) &&
@ -334,8 +334,8 @@ void doPlayer()
} }
} }
limitFloat(&engine.ssx, -cameraMaxSpeed, cameraMaxSpeed); LIMIT(engine.ssx, -cameraMaxSpeed, cameraMaxSpeed);
limitFloat(&engine.ssy, -cameraMaxSpeed, cameraMaxSpeed); LIMIT(engine.ssy, -cameraMaxSpeed, cameraMaxSpeed);
// Specific for the mission were you have to chase the Executive Transport // Specific for the mission were you have to chase the Executive Transport
if ((currentGame.area == 18) && (aliens[ALIEN_BOSS].shield > 0) && if ((currentGame.area == 18) && (aliens[ALIEN_BOSS].shield > 0) &&

View File

@ -549,7 +549,7 @@ static void buy(int i)
shopSelectedItem = -4; shopSelectedItem = -4;
return; return;
} }
limitCharAdd(&player.ammo[0], 10, 0, currentGame.maxPlasmaAmmo); LIMIT_ADD(player.ammo[0], 10, 0, currentGame.maxPlasmaAmmo);
break; break;
case SHOP_ROCKET_AMMO: case SHOP_ROCKET_AMMO:
@ -668,7 +668,7 @@ static void buy(int i)
} }
shop_sellSecondaryWeapon(); shop_sellSecondaryWeapon();
player.weaponType[1] = W_DOUBLE_ROCKETS; player.weaponType[1] = W_DOUBLE_ROCKETS;
limitChar(&currentGame.maxRocketAmmo, 5, 50); LIMIT(currentGame.maxRocketAmmo, 5, 50);
shopSelectedItem = -1; shopSelectedItem = -1;
break; break;
@ -680,7 +680,7 @@ static void buy(int i)
} }
shop_sellSecondaryWeapon(); shop_sellSecondaryWeapon();
player.weaponType[1] = W_MICRO_ROCKETS; player.weaponType[1] = W_MICRO_ROCKETS;
limitChar(&currentGame.maxRocketAmmo, 5, 50); LIMIT(currentGame.maxRocketAmmo, 5, 50);
shopSelectedItem = -1; shopSelectedItem = -1;
break; break;
@ -713,7 +713,7 @@ static void buy(int i)
while (currentGame.maxRocketAmmo > maxHoming) while (currentGame.maxRocketAmmo > maxHoming)
sell(SHOP_ROCKET_MAX_AMMO); sell(SHOP_ROCKET_MAX_AMMO);
limitChar(&currentGame.maxRocketAmmo, 5, maxHoming); LIMIT(currentGame.maxRocketAmmo, 5, maxHoming);
shopSelectedItem = -1; shopSelectedItem = -1;
break; break;
@ -746,7 +746,7 @@ static void buy(int i)
while (currentGame.maxRocketAmmo > maxDoubleHoming) while (currentGame.maxRocketAmmo > maxDoubleHoming)
sell(SHOP_ROCKET_MAX_AMMO); sell(SHOP_ROCKET_MAX_AMMO);
limitChar(&currentGame.maxRocketAmmo, 5, maxDoubleHoming); LIMIT(currentGame.maxRocketAmmo, 5, maxDoubleHoming);
shopSelectedItem = -1; shopSelectedItem = -1;
break; break;
@ -762,7 +762,7 @@ static void buy(int i)
while (currentGame.maxRocketAmmo > maxMicroHoming) while (currentGame.maxRocketAmmo > maxMicroHoming)
sell(SHOP_ROCKET_MAX_AMMO); sell(SHOP_ROCKET_MAX_AMMO);
limitChar(&currentGame.maxRocketAmmo, 5, maxMicroHoming); LIMIT(currentGame.maxRocketAmmo, 5, maxMicroHoming);
shopSelectedItem = -1; shopSelectedItem = -1;
break; break;
} }