Flatten objects which only contain static functions.

There is no reason to have objects which contain only static functions
and no variables. These functions have been moved to the global
namespace and are now defined in .cpp files. The math functions are very
small and have been put in math.h and made static inline.
This commit is contained in:
Guus Sliepen 2011-08-26 23:27:16 +02:00
parent 931d110692
commit 878e559b6a
20 changed files with 290 additions and 287 deletions

View File

@ -53,6 +53,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "init.h"
#include "intermission.h"
#include "loadSave.h"
#include "math.h"
#include "messages.h"
#include "misc.h"
#include "missions.h"

View File

@ -26,26 +26,26 @@ object enemy[MAX_ALIENS];
static bool placeAlien(object *theEnemy)
{
if (rand() % 2 == 0)
theEnemy->x = Math::rrand(800, 1600);
theEnemy->x = rrand(800, 1600);
else
theEnemy->x = Math::rrand(-800, 0);
theEnemy->x = rrand(-800, 0);
if (rand() % 2 == 0)
theEnemy->y = Math::rrand(600, 1200);
theEnemy->y = rrand(600, 1200);
else
theEnemy->y = Math::rrand(-600, 0);
theEnemy->y = rrand(-600, 0);
if (currentGame.area == 24)
{
theEnemy->x = 800;
theEnemy->y = Math::rrand(200, 400);
theEnemy->y = rrand(200, 400);
}
for (int i = 0 ; i < MAX_ALIENS ; i++)
{
if ((enemy[i].owner != theEnemy) && (enemy[i].shield > 0))
{
if (Collision::collision(theEnemy->x, theEnemy->y, theEnemy->image[0]->w, theEnemy->image[0]->h, enemy[i].x, enemy[i].y, enemy[i].image[0]->w, enemy[i].image[0]->h))
if (collision(theEnemy->x, theEnemy->y, theEnemy->image[0]->w, theEnemy->image[0]->h, enemy[i].x, enemy[i].y, enemy[i].image[0]->w, enemy[i].image[0]->h))
return false;
}
}
@ -251,7 +251,7 @@ bool addAlien()
enemy[index].deathCounter = 0 - (enemy[index].maxShield * 3);
enemy[index].hit = 0;
Math::limitInt(&enemy[index].deathCounter, -250, 0);
limitInt(&enemy[index].deathCounter, -250, 0);
// Attempts to place an alien. If it fails, the alien is deactivated.
for (int i = 0 ; i < 100 ; i++)
@ -272,8 +272,8 @@ bool addAlien()
if (enemy[index].classDef == CD_ESCORT)
enemy[index].shield = 50;
enemy[index].dx = Math::rrand(-2, 2);
enemy[index].dy = Math::rrand(-2, 2);
enemy[index].dx = rrand(-2, 2);
enemy[index].dy = rrand(-2, 2);
enemy[index].ammo[0] = 0;
@ -475,14 +475,14 @@ static void addFriendly(int type)
enemy[type].active = true;
if (rand() % 2 == 0)
enemy[type].x = Math::rrand(400, 550);
enemy[type].x = rrand(400, 550);
else
enemy[type].x = Math::rrand(250, 400);
enemy[type].x = rrand(250, 400);
if (rand() % 2 == 0)
enemy[type].y = Math::rrand(300, 450);
enemy[type].y = rrand(300, 450);
else
enemy[type].y = Math::rrand(150, 300);
enemy[type].y = rrand(150, 300);
if (type == FR_PHOEBE)
enemy[type].classDef = CD_PHOEBE;
@ -610,7 +610,7 @@ void initAliens()
{
enemy[i].systemPower = enemy[i].maxShield;
enemy[i].deathCounter = 0 - (enemy[i].maxShield * 3);
Math::limitInt(&enemy[i].deathCounter, -350, 0);
limitInt(&enemy[i].deathCounter, -350, 0);
}
// Set target energy meter
@ -826,7 +826,7 @@ static void moveAndSeparate(object *theEnemy)
continue;
}
if (Collision::collision(theEnemy, anEnemy))
if (collision(theEnemy, anEnemy))
{
if ((anEnemy->classDef == CD_BARRIER) && (anEnemy->owner != theEnemy))
{
@ -848,7 +848,7 @@ static void moveAndSeparate(object *theEnemy)
// Handle a collision with the player
if ((player.shield > 0) && (theEnemy->shield > 0) && (checkCollisions))
{
if (Collision::collision(theEnemy, &player))
if (collision(theEnemy, &player))
{
hasCollided = true;
@ -918,7 +918,7 @@ static void moveAndSeparate(object *theEnemy)
theEnemy->dx *= 0.2;
theEnemy->dy *= 0.2;
Math::limitInt(&theEnemy->thinktime, 0, 15);
limitInt(&theEnemy->thinktime, 0, 15);
}
}
}
@ -989,7 +989,7 @@ void doAliens()
canFire = true; // The alien is allowed to fire
Math::limitInt(&--theEnemy->thinktime, 0, 250);
limitInt(&--theEnemy->thinktime, 0, 250);
if (theEnemy->target->shield < 1)
theEnemy->target = theEnemy;
@ -1031,8 +1031,8 @@ void doAliens()
if (theEnemy->dx > 0) theEnemy->face = 1;
}
Math::limitFloat(&theEnemy->dx, 0 - theEnemy->speed, theEnemy->speed);
Math::limitFloat(&theEnemy->dy, 0 - theEnemy->speed, theEnemy->speed);
limitFloat(&theEnemy->dx, 0 - theEnemy->speed, theEnemy->speed);
limitFloat(&theEnemy->dy, 0 - theEnemy->speed, theEnemy->speed);
}
@ -1050,7 +1050,7 @@ void doAliens()
if (theEnemy->flags & FL_LEAVESECTOR)
{
Math::limitFloat(&(theEnemy->dx -= 0.5), 0, -15);
limitFloat(&(theEnemy->dx -= 0.5), 0, -15);
theEnemy->dy = 0;
theEnemy->thinktime = 999;
theEnemy->face = 0;
@ -1103,13 +1103,13 @@ void doAliens()
if (theEnemy->classDef == CD_MOBILESHIELD)
{
Math::limitInt(&(++enemy[WC_BOSS].shield), 0, enemy[WC_BOSS].maxShield);
limitInt(&(++enemy[WC_BOSS].shield), 0, enemy[WC_BOSS].maxShield);
}
// ----------------------------------------
Math::limitCharAdd(&theEnemy->reload[0], -1, 0, 999);
Math::limitCharAdd(&theEnemy->reload[1], -1, 0, 999);
limitCharAdd(&theEnemy->reload[0], -1, 0, 999);
limitCharAdd(&theEnemy->reload[1], -1, 0, 999);
if ((!(theEnemy->flags & FL_DISABLED)) && (!(theEnemy->flags & FL_NOFIRE)))
{
@ -1157,7 +1157,7 @@ void doAliens()
}
else
{
Math::limitCharAdd(&theEnemy->ammo[0], 1, 0, 250);
limitCharAdd(&theEnemy->ammo[0], 1, 0, 250);
}
// -------------------------------------------------------
@ -1188,7 +1188,7 @@ void doAliens()
if (theEnemy->hit)
shapeToUse += SHIP_HIT_INDEX;
Math::limitCharAdd(&theEnemy->hit, -1, 0, 100);
limitCharAdd(&theEnemy->hit, -1, 0, 100);
if ((theEnemy->x + theEnemy->image[0]->w > 0) && (theEnemy->x < 800) && (theEnemy->y + theEnemy->image[0]->h > 0) && (theEnemy->y < 600))
{

View File

@ -55,7 +55,7 @@ void addBullet(object *theWeapon, object *attacker, int y, int dy)
if (bullet->flags & WF_VARIABLE_SPEED)
{
bullet->dx = Math::rrand(100, 200);
bullet->dx = rrand(100, 200);
bullet->dx /= 10;
if (attacker->face == 1)
bullet->dx = 0 - bullet->dx;
@ -65,7 +65,7 @@ void addBullet(object *theWeapon, object *attacker, int y, int dy)
if (bullet->flags & WF_SCATTER)
{
bullet->dy = Math::rrand(-200, 200);
bullet->dy = rrand(-200, 200);
if (bullet->dy != 0)
bullet->dy /= 200;
}
@ -127,8 +127,8 @@ void addBullet(object *theWeapon, object *attacker, int y, int dy)
if (attacker->classDef == CD_ASTEROID)
{
bullet->dx = Math::rrand(-20, 20);
bullet->dy = Math::rrand(-20, 20);
bullet->dx = rrand(-20, 20);
bullet->dy = rrand(-20, 20);
bullet->image[0] = graphics.shape[4];
}
@ -405,7 +405,7 @@ char checkPlayerShockDamage(float x, float y)
player.shield -= (int)(10 - distX);
player.shield -= (int)(10 - distY);
Math::limitInt(&player.shield, 0, player.maxShield);
limitInt(&player.shield, 0, player.maxShield);
player.hit = 10;
return 1;
@ -441,7 +441,7 @@ void fireRay(object *attacker)
{
if (player.shield > 0)
{
if (Collision::collision(player.x, player.y, player.image[0]->w, player.image[0]->h, ray.x, ray.y, ray.w, ray.h) && (!engine.cheatShield))
if (collision(player.x, player.y, player.image[0]->w, player.image[0]->h, ray.x, ray.y, ray.w, ray.h) && (!engine.cheatShield))
{
if (player.shield > engine.lowShield)
{
@ -472,7 +472,7 @@ void fireRay(object *attacker)
if ((anEnemy->shield > 0) && (attacker != anEnemy) && (attacker->classDef != anEnemy->classDef))
{
if (Collision::collision(anEnemy->x, anEnemy->y, anEnemy->image[0]->w, anEnemy->image[0]->h, ray.x, ray.y, ray.w, ray.h))
if (collision(anEnemy->x, anEnemy->y, anEnemy->image[0]->w, anEnemy->image[0]->h, ray.x, ray.y, ray.w, ray.h))
{
anEnemy->shield--;
addExplosion(anEnemy->x, anEnemy->y, E_SMALL_EXPLOSION);
@ -547,7 +547,7 @@ void doBullets()
if (bullet->id == WT_CHARGER)
{
for (int i = 0 ; i < bullet->damage ; i++)
graphics.blit(bullet->image[0], (int)(bullet->x - Math::rrand(-(bullet->damage / 3), 0)), (int)(bullet->y + Math::rrand(-3, 3)));
graphics.blit(bullet->image[0], (int)(bullet->x - rrand(-(bullet->damage / 3), 0)), (int)(bullet->y + rrand(-3, 3)));
}
graphics.blit(bullet->image[0], (int)bullet->x, (int)bullet->y);
@ -557,18 +557,18 @@ void doBullets()
if (bullet->target != NULL)
{
if (bullet->x < bullet->target->x)
Math::limitFloat(&(bullet->dx += homingMissileSpeed), -15, 15);
limitFloat(&(bullet->dx += homingMissileSpeed), -15, 15);
if (bullet->x > bullet->target->x)
Math::limitFloat(&(bullet->dx -= homingMissileSpeed), -15, 15);
limitFloat(&(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)
Math::limitFloat(&(bullet->dy += homingMissileSpeed), -15, 15);
limitFloat(&(bullet->dy += homingMissileSpeed), -15, 15);
if (bullet->y > bullet->target->y)
Math::limitFloat(&(bullet->dy -= homingMissileSpeed), -15, 15);
limitFloat(&(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))
@ -602,7 +602,7 @@ void doBullets()
if (okayToHit)
{
if ((bullet->active) && (Collision::collision(bullet, theEnemy)))
if ((bullet->active) && (collision(bullet, theEnemy)))
{
if (bullet->owner == &player)
{
@ -623,7 +623,7 @@ void doBullets()
if (theEnemy->flags & FL_CANNOTDIE)
{
Math::limitInt(&theEnemy->shield, 1, theEnemy->maxShield);
limitInt(&theEnemy->shield, 1, theEnemy->maxShield);
if (theEnemy->shield == 1)
{
if (currentGame.area != 26)
@ -695,7 +695,7 @@ void doBullets()
// Check for bullets hitting player
if ((bullet->flags & WF_WEAPCO) || (bullet->id == WT_ROCKET) || (bullet->id == WT_LASER) || (bullet->id == WT_CHARGER))
{
if ((bullet->active) && (player.shield > 0) && (Collision::collision(bullet, &player)) && (bullet->owner != &player))
if ((bullet->active) && (player.shield > 0) && (collision(bullet, &player)) && (bullet->owner != &player))
{
if ((!engine.cheatShield) || (engine.missionCompleteTimer != 0))
{
@ -707,7 +707,7 @@ void doBullets()
}
}
player.shield -= bullet->damage;
Math::limitInt(&player.shield, 0, player.maxShield);
limitInt(&player.shield, 0, player.maxShield);
player.hit = 5;
}
@ -743,7 +743,7 @@ void doBullets()
theCargo = &cargo[j];
if (theCargo->active)
{
if (Collision::collision(bullet, theCargo))
if (collision(bullet, theCargo))
{
bullet->active = false;
addExplosion(bullet->x, bullet->y, E_SMALL_EXPLOSION);
@ -753,7 +753,7 @@ void doBullets()
theCargo->active = false;
playSound(SFX_EXPLOSION);
for (int i = 0 ; i < 10 ; i++)
addExplosion(theCargo->x + Math::rrand(-15, 15), theCargo->y + Math::rrand(-15, 15), E_BIG_EXPLOSION);
addExplosion(theCargo->x + rrand(-15, 15), theCargo->y + rrand(-15, 15), E_BIG_EXPLOSION);
updateMissionRequirements(M_PROTECT_PICKUP, P_CARGO, 1);
}
}
@ -772,7 +772,7 @@ void doBullets()
{
playSound(SFX_EXPLOSION);
for (int i = 0 ; i < 10 ; i++)
addExplosion(bullet->x + Math::rrand(-35, 35), bullet->y + Math::rrand(-35, 35), E_BIG_EXPLOSION);
addExplosion(bullet->x + rrand(-35, 35), bullet->y + rrand(-35, 35), E_BIG_EXPLOSION);
if (bullet->flags & WF_TIMEDEXPLOSION)
if (checkPlayerShockDamage(bullet->x, bullet->y))

View File

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

View File

@ -20,170 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern void showErrorAndExit(int errorId, const char *name);
class Collision {
private:
Collision(){}
public:
static signed char collision(float x0, float y0, int w0, int h0, float x2, float y2, int w1, int h1)
{
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
static signed char collision(object *object1, object *object2)
{
float x0 = object1->x;
float y0 = object1->y;
float w0 = object1->image[0]->w;
float h0 = object1->image[0]->h;
float x2 = object2->x;
float y2 = object2->y;
float w1 = object2->image[0]->w;
float h1 = object2->image[0]->h;
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
static signed char collision(collectables *object1, object *object2)
{
float x0 = object1->x;
float y0 = object1->y;
float w0 = object1->image->w;
float h0 = object1->image->h;
float x2 = object2->x;
float y2 = object2->y;
float w1 = object2->image[0]->w;
float h1 = object2->image[0]->h;
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
};
class Math {
private:
Math(){}
public:
static void limitChar(signed char *in, int low, int high)
{
if (*in < low)
*in = low;
if (*in > high)
*in = high;
}
static void limitChar(unsigned char *in, int low, int high)
{
if (*in < low)
*in = low;
if (*in > high)
*in = high;
}
static 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 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 void limitInt(int *in, int low, int high)
{
if (*in < low)
*in = low;
if (*in > high)
*in = high;
}
static void limitFloat(float *in, float low, float high)
{
if (*in < low)
*in = low;
if (*in > high)
*in = high;
}
static void wrapChar(signed char *in, signed char low, signed char high)
{
if (*in < low)
*in = high;
if (*in > high)
*in = low;
}
static void wrapInt(int *in, int low, int high)
{
if (*in < low)
*in = high;
if (*in > high)
*in = low;
}
static void wrapFloat(float *in, float low, float high)
{
if (*in < low)
*in = high;
if (*in > high)
*in = low;
}
static int rrand(int min, int max)
{
int r = min;
max++;
if ((max - min) == 0)
return min;
r += rand() % (max - min);
return r;
}
};
class Graphics {
unsigned long frameLimit;
int thirds;

View File

@ -93,11 +93,11 @@ void addCollectable(float x, float y, int type, int value, int life)
collectable->x = x;
collectable->y = y;
collectable->dx = Math::rrand(-100, 100);
collectable->dx = rrand(-100, 100);
if (collectable->dx != 0)
collectable->dx /= 100;
collectable->dy = Math::rrand(-100, 100);
collectable->dy = rrand(-100, 100);
if (collectable->dy != 0)
collectable->dy /= 100;
@ -185,7 +185,7 @@ void checkMineBulletCollisions(object *bullet)
if (collectable->type == P_MINE)
{
if (Collision::collision(collectable, bullet))
if (collision(collectable, bullet))
{
collectable->active = false;
@ -252,7 +252,7 @@ void doCollectables()
collectable->life--;
if ((player.shield > 0) && (Collision::collision(collectable, &player)))
if ((player.shield > 0) && (collision(collectable, &player)))
{
char temp[40];
switch(collectable->type)
@ -264,7 +264,7 @@ void doCollectables()
break;
case P_ROCKET:
Math::limitCharAdd(&player.ammo[1], collectable->value, 0, currentGame.maxRocketAmmo);
limitCharAdd(&player.ammo[1], collectable->value, 0, currentGame.maxRocketAmmo);
if (player.ammo[1] == currentGame.maxRocketAmmo)
sprintf(temp, "Rocket Ammo at Maximum");
else
@ -278,17 +278,17 @@ void doCollectables()
break;
case P_SHIELD:
Math::limitInt(&(player.shield += 10), 0, player.maxShield);
limitInt(&(player.shield += 10), 0, player.maxShield);
currentGame.shieldPickups ++;
sprintf(temp, "Restored 10 shield points");
break;
case P_PLASMA_RATE:
Math::limitCharAdd(&weapon[1].reload[0], -2, currentGame.maxPlasmaRate, 15);
limitCharAdd(&weapon[1].reload[0], -2, currentGame.maxPlasmaRate, 15);
player.weaponType[0] = 1;
if (player.ammo[0] < 50)
player.ammo[0] = 50;
Math::limitChar(&(player.ammo[0]), 0, currentGame.maxPlasmaAmmo);
limitChar(&(player.ammo[0]), 0, currentGame.maxPlasmaAmmo);
if (weapon[1].reload[0] == currentGame.maxPlasmaRate)
sprintf(temp, "Firing Rate at Maximum");
else
@ -297,10 +297,10 @@ void doCollectables()
break;
case P_PLASMA_SHOT:
Math::limitCharAdd(&weapon[1].ammo[0], 1, 1, currentGame.maxPlasmaOutput);
limitCharAdd(&weapon[1].ammo[0], 1, 1, currentGame.maxPlasmaOutput);
if (player.ammo[0] < 50)
player.ammo[0] = 50;
Math::limitChar(&(player.ammo[0]), 0, currentGame.maxPlasmaAmmo);
limitChar(&(player.ammo[0]), 0, currentGame.maxPlasmaAmmo);
if (weapon[1].ammo[0] == currentGame.maxPlasmaOutput)
sprintf(temp, "Plasma output at Maximum");
else
@ -310,10 +310,10 @@ void doCollectables()
break;
case P_PLASMA_DAMAGE:
Math::limitCharAdd(&weapon[1].damage, 1, 1, currentGame.maxPlasmaDamage);
limitCharAdd(&weapon[1].damage, 1, 1, currentGame.maxPlasmaDamage);
if (player.ammo[0] < 50)
player.ammo[0] = 50;
Math::limitChar(&(player.ammo[0]), 0, currentGame.maxPlasmaAmmo);
limitChar(&(player.ammo[0]), 0, currentGame.maxPlasmaAmmo);
if (weapon[1].damage == currentGame.maxPlasmaDamage)
sprintf(temp, "Plasma damage at Maximum");
else
@ -345,7 +345,7 @@ void doCollectables()
break;
case P_PLASMA_AMMO:
Math::limitCharAdd(&player.ammo[0], collectable->value, 0, currentGame.maxPlasmaAmmo);
limitCharAdd(&player.ammo[0], collectable->value, 0, currentGame.maxPlasmaAmmo);
if (player.ammo[0] == currentGame.maxPlasmaAmmo)
sprintf(temp, "Plasma cells at Maximum");
else

View File

@ -136,7 +136,7 @@ void doComms(SDL_Surface *comms)
{
for (int i = 0 ; i < 4 ; i++)
{
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 170, 180 + (i * 60), 430, 50))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 170, 180 + (i * 60), 430, 50))
{
createMissionDetailSurface(comms, i);
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = 0;
@ -145,7 +145,7 @@ void doComms(SDL_Surface *comms)
}
else
{
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 170, 440, 160, 20))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 170, 440, 160, 20))
{
createCommsSurface(comms);
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = 0;

View File

@ -29,8 +29,8 @@ void addDebris(int x, int y, int amount)
object *debris;
amount = Math::rrand(3, rand() % amount);
Math::limitInt(&amount, 3, 8);
amount = rrand(3, rand() % amount);
limitInt(&amount, 3, 8);
for (int i = 0 ; i < amount ; i++)
{
@ -40,10 +40,10 @@ void addDebris(int x, int y, int amount)
debris->x = x;
debris->y = y;
debris->thinktime = Math::rrand(60, 180);
debris->thinktime = rrand(60, 180);
debris->dx = Math::rrand(-500, 500);
debris->dy = Math::rrand(-500, 500);
debris->dx = rrand(-500, 500);
debris->dy = rrand(-500, 500);
if (debris->dx != 0)
debris->dx /= 100;
@ -75,7 +75,7 @@ void doDebris()
debris->x += debris->dx;
debris->y += debris->dy;
addExplosion(debris->x + Math::rrand(-10, 10), debris->y + Math::rrand(-10, 10), E_BIG_EXPLOSION);
addExplosion(debris->x + rrand(-10, 10), debris->y + rrand(-10, 10), E_BIG_EXPLOSION);
}
if (debris->thinktime < 1)

View File

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

View File

@ -187,7 +187,7 @@ int mainGameLoop()
}
else if ((currentGame.area == 26) && (engine.musicVolume > 0))
{
Math::limitFloat(&(engine.musicVolume -= 0.2), 0, 100);
limitFloat(&(engine.musicVolume -= 0.2), 0, 100);
Mix_VolumeMusic((int)engine.musicVolume);
}
else
@ -202,7 +202,7 @@ int mainGameLoop()
}
else
{
Math::limitFloat(&(engine.musicVolume -= 0.2), 0, 100);
limitFloat(&(engine.musicVolume -= 0.2), 0, 100);
Mix_VolumeMusic((int)engine.musicVolume);
if (SDL_GetTicks() >= engine.missionCompleteTimer)
{
@ -226,7 +226,7 @@ int mainGameLoop()
doExplosions();
doInfo();
Math::wrapChar(&(--engine.eventTimer), 0, 60);
wrapChar(&(--engine.eventTimer), 0, 60);
while (engine.paused)
{
@ -237,12 +237,12 @@ int mainGameLoop()
if ((currentGame.area == 24) && (engine.addAliens > -1))
{
if ((rand() % 10) == 0)
addCollectable(Math::rrand(800, 100), player.y, P_MINE, 25, 180 + rand() % 60);
addCollectable(rrand(800, 100), player.y, P_MINE, 25, 180 + rand() % 60);
}
if (engine.addAliens > -1)
{
Math::wrapInt(&(--engine.addAliens), 0, currentMission.addAliens);
wrapInt(&(--engine.addAliens), 0, currentMission.addAliens);
if ((engine.addAliens == 0) && (allowableAliens > 0))
{
allowableAliens -= addAlien();

View File

@ -23,6 +23,59 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Graphics graphics;
Star star[200];
bool collision(float x0, float y0, int w0, int h0, float x2, float y2, int w1, int h1)
{
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
bool collision(object *object1, object *object2)
{
float x0 = object1->x;
float y0 = object1->y;
float w0 = object1->image[0]->w;
float h0 = object1->image[0]->h;
float x2 = object2->x;
float y2 = object2->y;
float w1 = object2->image[0]->w;
float h1 = object2->image[0]->h;
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
bool collision(collectables *object1, object *object2)
{
float x0 = object1->x;
float y0 = object1->y;
float w0 = object1->image->w;
float h0 = object1->image->h;
float x2 = object2->x;
float y2 = object2->y;
float w1 = object2->image[0]->w;
float h1 = object2->image[0]->h;
float x1 = x0 + w0;
float y1 = y0 + h0;
float x3 = x2 + w1;
float y3 = y2 + h1;
return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
}
SDL_Surface *loadImage(const char *filename)
{
SDL_Surface *image, *newImage;
@ -79,8 +132,8 @@ void doStarfield()
else if (star[i].speed == 1)
color = graphics.darkGrey;
Math::wrapFloat(&(star[i].x += (engine.ssx * star[i].speed)), 0, 799);
Math::wrapFloat(&(star[i].y += (engine.ssy * star[i].speed)), 0, 599);
wrapFloat(&(star[i].x += (engine.ssx * star[i].speed)), 0, 799);
wrapFloat(&(star[i].y += (engine.ssy * star[i].speed)), 0, 599);
graphics.putpixel(graphics.screen, (int)star[i].x, (int)star[i].y, color);
r.x = (int)star[i].x;

View File

@ -21,6 +21,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern Graphics graphics;
extern Star star[200];
extern bool collision(float x0, float y0, int w0, int h0, float x2, float y2, int w1, int h1);
extern bool collision(object *object1, object *object2);
extern bool collision(collectables *object1, object *object2);
extern SDL_Surface *loadImage(const char *filename);
extern void doStarfield();
extern int isOnScreen(int x, int y, int w, int h);

View File

@ -27,8 +27,8 @@ static void doCursor()
{
getPlayerInput();
Math::limitInt(&engine.cursor_x, 10, 790);
Math::limitInt(&engine.cursor_y, 10, 590);
limitInt(&engine.cursor_x, 10, 790);
limitInt(&engine.cursor_y, 10, 590);
graphics.blit(graphics.shape[0], engine.cursor_x, engine.cursor_y);
}
@ -265,7 +265,7 @@ static bool showSystem(float x, float y)
r.y -= (systemPlanet[planet].image->h / 2);
graphics.blit(systemPlanet[planet].image, r.x, r.y);
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, r.x, r.y, systemPlanet[planet].image->w, systemPlanet[planet].image->h))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, r.x, r.y, systemPlanet[planet].image->w, systemPlanet[planet].image->h))
{
graphics.drawString(systemPlanet[planet].name, -1, 545, FONT_WHITE);
if ((engine.keyState[SDLK_LCTRL]) || (engine.keyState[SDLK_RCTRL]))
@ -395,14 +395,14 @@ static void showOptions(SDL_Surface *optionsSurface)
{
if ((engine.keyState[SDLK_LCTRL]) || (engine.keyState[SDLK_RCTRL]))
{
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 172, 45, 22))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 172, 45, 22))
engine.useSound = true;
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 478, 172, 45, 22))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 478, 172, 45, 22))
engine.useSound = false;
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 222, 45, 22))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 222, 45, 22))
{
engine.useMusic = true;
if (engine.useAudio)
@ -414,14 +414,14 @@ static void showOptions(SDL_Surface *optionsSurface)
}
}
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 478, 222, 45, 22))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 478, 222, 45, 22))
{
engine.useMusic = false;
if (engine.useAudio)
Mix_PauseMusic();
}
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 272, 45, 22))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 272, 45, 22))
{
if (!engine.fullScreen)
{
@ -436,7 +436,7 @@ static void showOptions(SDL_Surface *optionsSurface)
}
}
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 478, 272, 45, 22))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 478, 272, 45, 22))
{
if (engine.fullScreen)
{
@ -451,9 +451,9 @@ static void showOptions(SDL_Surface *optionsSurface)
}
}
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 322, 100, 22))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 417, 322, 100, 22))
{
Math::wrapChar(&(++currentGame.autoSaveSlot), -1, 4);
wrapChar(&(++currentGame.autoSaveSlot), -1, 4);
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = 0;
}
@ -637,8 +637,8 @@ int galaxyMap()
if (rand() % 1000 < 2)
{
engine.ssx = Math::rrand(100, 100);
engine.ssy = Math::rrand(100, 100);
engine.ssx = rrand(100, 100);
engine.ssy = rrand(100, 100);
engine.ssx /= 100;
engine.ssy /= 100;
}
@ -801,7 +801,7 @@ int galaxyMap()
graphics.blit(graphics.shape[i + 1], 80 + (i * 90), 500);
}
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 80 + (i * 90), 500, 32, 32))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 80 + (i * 90), 500, 32, 32))
{
if (i != 0)
{

View File

@ -221,7 +221,7 @@ int showSaveSlots(SDL_Surface *savesSurface, signed char saveSlot)
{
for (int i = 0 ; i < 5 ; i++)
{
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, r.x, r.y, r.w, r.h))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, r.x, r.y, r.w, r.h))
{
clickedSlot = i;
createSavesSurface(savesSurface, i);
@ -229,16 +229,16 @@ int showSaveSlots(SDL_Surface *savesSurface, signed char saveSlot)
r.y += 30;
}
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 215, 365, 100, 25))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 215, 365, 100, 25))
{
saveGame(saveSlot + 1);
createSavesSurface(savesSurface, -10);
}
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 335, 365, 100, 25))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 335, 365, 100, 25))
createSavesSurface(savesSurface, -1);
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 453, 365, 100, 25))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 453, 365, 100, 25))
{
char filename[PATH_MAX];
sprintf(filename, "%ssave%.2d.dat", engine.userHomeDirectory, (saveSlot + 1));

109
code/math.h Normal file
View File

@ -0,0 +1,109 @@
/*
Copyright (C) 2003 Parallel Realities
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 2
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
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)
*in = high;
if (*in > high)
*in = low;
}
static inline void wrapInt(int *in, int low, int high)
{
if (*in < low)
*in = high;
if (*in > high)
*in = low;
}
static inline void wrapFloat(float *in, float low, float high)
{
if (*in < low)
*in = high;
if (*in > high)
*in = low;
}
static inline int rrand(int min, int max)
{
int r = min;
max++;
if ((max - min) == 0)
return min;
r += rand() % (max - min);
return r;
}

View File

@ -378,7 +378,7 @@ void doInfo()
return;
if ((!engine.keyState[SDLK_SPACE]) && (player.weaponType[1] == W_LASER) && (engine.eventTimer % 8 == 1))
Math::limitCharAdd(&player.ammo[1], -1, 1, 255);
limitCharAdd(&player.ammo[1], -1, 1, 255);
if ((engine.eventTimer < 30) && (player.shield <= engine.lowShield))
return;

View File

@ -109,7 +109,7 @@ void doPlayer()
{
if (engine.keyState[SDLK_SPACE])
{
Math::limitCharAdd(&player.ammo[1], 1, 0, 200);
limitCharAdd(&player.ammo[1], 1, 0, 200);
}
else
{
@ -195,8 +195,8 @@ void doPlayer()
engine.keyState[SDLK_LSHIFT] = engine.keyState[SDLK_RSHIFT] = 0;
}
Math::limitCharAdd(&player.reload[0], -1, 0, 999);
Math::limitCharAdd(&player.reload[1], -1, 0, 999);
limitCharAdd(&player.reload[0], -1, 0, 999);
limitCharAdd(&player.reload[1], -1, 0, 999);
if (engine.keyState[SDLK_UP])
{
@ -268,8 +268,8 @@ void doPlayer()
if (engine.done == 0)
{
Math::limitFloat(&player.x, 100, 700);
Math::limitFloat(&player.y, 100, 500);
limitFloat(&player.x, 100, 700);
limitFloat(&player.y, 100, 500);
}
if (player.shield > engine.lowShield)
@ -280,11 +280,11 @@ void doPlayer()
if (player.hit)
shapeToUse += SHIP_HIT_INDEX;
Math::limitCharAdd(&player.hit, -1, 0, 100);
limitCharAdd(&player.hit, -1, 0, 100);
graphics.blit(graphics.shipShape[shapeToUse], (int)player.x, (int)player.y);
if ((player.shield <= engine.lowShield) && (rand() % 5 < 1))
addExplosion(player.x + Math::rrand(-10, 10), player.y + Math::rrand(-10, 20), E_SMOKE);
addExplosion(player.x + rrand(-10, 10), player.y + rrand(-10, 20), E_SMOKE);
}
else
{
@ -308,14 +308,14 @@ void doPlayer()
engine.keyState[SDLK_UP] = engine.keyState[SDLK_DOWN] = engine.keyState[SDLK_LEFT] = engine.keyState[SDLK_RIGHT] = 0;
if ((rand() % 3) == 0)
addExplosion(player.x + Math::rrand(-10, 10), player.y + Math::rrand(-10, 10), E_BIG_EXPLOSION);
addExplosion(player.x + rrand(-10, 10), player.y + rrand(-10, 10), E_BIG_EXPLOSION);
if (player.shield == -99)
addDebris((int)player.x, (int)player.y, player.maxShield);
}
}
Math::limitFloat(&engine.ssx, -3, 3);
Math::limitFloat(&engine.ssy, -3, 3);
limitFloat(&engine.ssx, -3, 3);
limitFloat(&engine.ssy, -3, 3);
// Specific for the mission were you have to chase the Executive Transport
if ((currentGame.area == 18) && (enemy[WC_BOSS].shield > 0) && (player.shield > 0))

View File

@ -115,8 +115,8 @@ void checkScriptEvents()
else
{
enemy[gameEvent[i].entity].active = true;
enemy[gameEvent[i].entity].x = Math::rrand((int)player.x + 400, (int)player.x + 800);
enemy[gameEvent[i].entity].y = Math::rrand((int)player.y - 400, (int)player.y + 800);
enemy[gameEvent[i].entity].x = rrand((int)player.x + 400, (int)player.x + 800);
enemy[gameEvent[i].entity].y = rrand((int)player.y - 400, (int)player.y + 800);
}
}

View File

@ -507,7 +507,7 @@ static void buy(int i)
case 3:
if (player.ammo[0] == currentGame.maxPlasmaAmmo)
{shopSelectedItem = -4; return;}
Math::limitCharAdd(&player.ammo[0], 10, 0, currentGame.maxPlasmaAmmo);
limitCharAdd(&player.ammo[0], 10, 0, currentGame.maxPlasmaAmmo);
break;
case 4:
if ((player.weaponType[1] == W_CHARGER) || (player.weaponType[1] == W_LASER))
@ -546,7 +546,7 @@ static void buy(int i)
case 8:
if (currentGame.maxPlasmaAmmo == 250)
{shopSelectedItem = -3; return;}
Math::limitCharAdd(&currentGame.maxPlasmaAmmo, 10, 0, 250);
limitCharAdd(&currentGame.maxPlasmaAmmo, 10, 0, 250);
break;
case 9:
if ((player.weaponType[1] == W_CHARGER) || (player.weaponType[1] == W_LASER))
@ -578,7 +578,7 @@ static void buy(int i)
if (player.weaponType[1] == W_HOMING_MISSILE)
{shopSelectedItem = -8; return;}
player.weaponType[1] = W_HOMING_MISSILE;
Math::limitChar(&player.ammo[1], 0, 20);
limitChar(&player.ammo[1], 0, 20);
shopSelectedItem = -1;
break;
case 14:
@ -592,14 +592,14 @@ static void buy(int i)
if (player.weaponType[1] == W_DOUBLE_HOMING_MISSILES)
{shopSelectedItem = -8; return;}
player.weaponType[1] = W_DOUBLE_HOMING_MISSILES;
Math::limitChar(&player.ammo[1], 0, 10);
limitChar(&player.ammo[1], 0, 10);
shopSelectedItem = -1;
break;
case 16:
if (player.weaponType[1] == W_MICRO_HOMING_MISSILES)
{shopSelectedItem = -8; return;}
player.weaponType[1] = W_MICRO_HOMING_MISSILES;
Math::limitChar(&player.ammo[1], 0, 10);
limitChar(&player.ammo[1], 0, 10);
shopSelectedItem = -1;
break;
}
@ -631,7 +631,7 @@ static void sell(int i)
if (player.ammo[0] == 0)
{shopSelectedItem = -6; return;}
if (player.ammo[0] > 9)
Math::limitCharAdd(&player.ammo[0], -10, 0, currentGame.maxPlasmaAmmo);
limitCharAdd(&player.ammo[0], -10, 0, currentGame.maxPlasmaAmmo);
else
player.ammo[0] = 0;
break;
@ -659,13 +659,13 @@ static void sell(int i)
if (currentGame.maxPlasmaAmmo == 100)
{shopSelectedItem = -1; return;}
currentGame.maxPlasmaAmmo -= 10;
Math::limitChar(&player.ammo[0], 0, currentGame.maxPlasmaAmmo);
limitChar(&player.ammo[0], 0, currentGame.maxPlasmaAmmo);
break;
case 9:
if (currentGame.maxRocketAmmo == 0)
{shopSelectedItem = -1; return;}
currentGame.maxRocketAmmo -= 5;
Math::limitChar(&player.ammo[1], 0, currentGame.maxRocketAmmo);
limitChar(&player.ammo[1], 0, currentGame.maxRocketAmmo);
break;
case 10:
if (player.weaponType[1] != W_DOUBLE_ROCKETS)
@ -747,7 +747,7 @@ void showShop()
{
for (int i = 0 ; i < icons ; i++)
{
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, shopItems[i].x, shopItems[i].y, 32, 25))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, shopItems[i].x, shopItems[i].y, 32, 25))
{
shopSelectedItem = i;
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = 0;
@ -757,14 +757,14 @@ void showShop()
if (shopSelectedItem > -1)
{
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 60, 350, 24, 16))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 60, 350, 24, 16))
{
buy(shopSelectedItem);
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = 0;
drawShop();
}
if (Collision::collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 700, 350, 24, 16))
if (collision(engine.cursor_x + 13, engine.cursor_y + 13, 6, 6, 700, 350, 24, 16))
{
sell(shopSelectedItem);
engine.keyState[SDLK_LCTRL] = engine.keyState[SDLK_RCTRL] = 0;

View File

@ -308,7 +308,7 @@ int doTitle()
if (engine.keyState[SDLK_UP])
{
engine.keyState[SDLK_UP] = 0;
Math::wrapChar(&(--selectedOption), 1, listLength);
wrapChar(&(--selectedOption), 1, listLength);
if (menuType == 0)
if ((selectedOption == 2) || (selectedOption == 3))
if (continueSaveSlot == 0)
@ -317,7 +317,7 @@ int doTitle()
if (engine.keyState[SDLK_DOWN])
{
engine.keyState[SDLK_DOWN] = 0;
Math::wrapChar(&(++selectedOption), 1, listLength);
wrapChar(&(++selectedOption), 1, listLength);
if (menuType == 0)
if ((selectedOption == 2) || (selectedOption == 3))
if (continueSaveSlot == 0)
@ -421,7 +421,7 @@ int doTitle()
#endif
}
else if (selectedOption == 4)
Math::wrapChar(&(++currentGame.autoSaveSlot), -1, 4);
wrapChar(&(++currentGame.autoSaveSlot), -1, 4);
else if (selectedOption == listLength)
{menuType = 0; selectedOption = 1;}
createOptionsMenu();
@ -601,8 +601,8 @@ void gameover()
graphics.updateScreen();
graphics.unBuffer();
x = ((800 - gameover->w) / 2) - Math::rrand(-2, 2);
y = ((600 - gameover->h) / 2) - Math::rrand(-2, 2);
x = ((800 - gameover->w) / 2) - rrand(-2, 2);
y = ((600 - gameover->h) / 2) - rrand(-2, 2);
graphics.blit(gameover, x, y);
graphics.delayFrame();