2011-08-24 14:14:44 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2003 Parallel Realities
|
2015-03-01 21:37:32 +01:00
|
|
|
Copyright (C) 2011, 2012 Guus Sliepen
|
2019-05-22 00:47:32 +02:00
|
|
|
Copyright (C) 2015-2019 Julie Marchant <onpon4@riseup.net>
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
2015-02-26 17:20:36 +01:00
|
|
|
as published by the Free Software Foundation; either version 3
|
2011-08-24 14:14:44 +02:00
|
|
|
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
|
2015-02-26 17:20:36 +01:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2015-02-26 17:20:36 +01:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-24 14:14:44 +02:00
|
|
|
*/
|
|
|
|
|
2017-01-25 16:48:29 +01:00
|
|
|
#include <stdlib.h>
|
2016-11-26 00:01:36 +01:00
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
#include "structs.h"
|
|
|
|
|
|
|
|
#include "alien.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#include "game.h"
|
|
|
|
#include "gfx.h"
|
|
|
|
#include "player.h"
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2016-11-25 18:37:26 +01:00
|
|
|
void bullet_add(Object *theWeapon, Object *attacker, int y, int dy)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
2016-11-25 18:37:26 +01:00
|
|
|
Object *bullet;
|
Fixed more magic numbers.
God, this one was definitely the biggest headache of all of the
magic number erasing. Never did I expect such cryptic problems.
The first problem was that the entirety of the player's weapon
struct was a part of the save file, *including the weapon's "image
indexes"*. Since the indexes have been changed, and the originally
used one is now unavailable when it's requested, this was causing
a segfault later on. Had to fix this by setting the image index
when the game is loaded.
The second problem was related to another bug I've been confused
about for years: the one that causes mobile rays to fire 5 green
shots. The entire reason those shots were green was because
the weapon's image indexes were undefined, and *that was causing
them to default to 0*. 0 was simply the index of green plasma.
Of course, though, now attempting to use that image causes a
segfault, so for now, I've fixed this by changing the image index
of the mobile rays to the red plasma bolts.
There are still some magic numbers left, related to the intermission
screen. But the hardest part is now done, thank God.
2016-01-06 04:12:29 +01:00
|
|
|
int imageIndex;
|
2011-08-24 14:14:44 +02:00
|
|
|
int tempX, tempY, steps;
|
|
|
|
|
2019-05-23 17:25:54 +02:00
|
|
|
bullet = malloc(sizeof(*bullet));
|
2019-05-19 19:40:50 +02:00
|
|
|
if (bullet == NULL)
|
2019-05-23 17:25:54 +02:00
|
|
|
{
|
|
|
|
engine_warn("Failed to allocate memor for bullet");
|
2019-05-19 19:40:50 +02:00
|
|
|
return;
|
2019-05-23 17:25:54 +02:00
|
|
|
}
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
if (attacker == &player)
|
2015-05-21 01:41:43 +02:00
|
|
|
game.shots++;
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
bullet->next = NULL;
|
2016-11-19 17:43:50 +01:00
|
|
|
bullet->active = 1;
|
2015-03-11 20:11:06 +01:00
|
|
|
bullet->x = attacker->x + (attacker->image[0]->w / 2) -
|
|
|
|
(theWeapon->image[0]->w * attacker->face);
|
2011-08-24 14:14:44 +02:00
|
|
|
bullet->y = attacker->y + y;
|
|
|
|
bullet->flags = theWeapon->flags;
|
|
|
|
bullet->shield = 300; // bullets live for (approximately) 5 seconds
|
|
|
|
|
|
|
|
// Timed explosions live between 1 and 3 seconds
|
|
|
|
if (bullet->flags & WF_TIMEDEXPLOSION)
|
2016-11-28 21:50:47 +01:00
|
|
|
bullet->shield = RANDRANGE(60, 180);
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
if (attacker->face == 0)
|
|
|
|
{
|
|
|
|
bullet->dx = theWeapon->speed;
|
2015-05-21 01:41:43 +02:00
|
|
|
if ((game.area == MISN_ELLESH) ||
|
|
|
|
(game.area == MISN_MARS))
|
2015-02-27 05:23:08 +01:00
|
|
|
bullet->dx += fabsf(engine.ssx + engine.smx);
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-15 00:50:07 +02:00
|
|
|
bullet->dx = -theWeapon->speed;
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bullet->flags & WF_VARIABLE_SPEED)
|
|
|
|
{
|
2015-04-15 00:50:07 +02:00
|
|
|
bullet->dx = RANDRANGE(100, 200) / 10;
|
2011-08-24 14:14:44 +02:00
|
|
|
if (attacker->face == 1)
|
2015-04-15 00:50:07 +02:00
|
|
|
bullet->dx = -bullet->dx;
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bullet->dy = dy;
|
|
|
|
|
|
|
|
if (bullet->flags & WF_SCATTER)
|
|
|
|
{
|
2015-04-15 00:50:07 +02:00
|
|
|
bullet->dy = RANDRANGE(-200, 200) / 200;
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (attacker->flags & FL_WEAPCO)
|
2011-09-04 14:23:31 +02:00
|
|
|
bullet->flags |= WF_WEAPCO;
|
2011-08-24 14:14:44 +02:00
|
|
|
else
|
2011-09-04 14:23:31 +02:00
|
|
|
bullet->flags |= WF_FRIEND;
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
bullet->owner = attacker->owner;
|
|
|
|
|
|
|
|
bullet->id = theWeapon->id;
|
|
|
|
|
|
|
|
bullet->damage = theWeapon->damage;
|
|
|
|
|
|
|
|
if (bullet->id == WT_CHARGER)
|
|
|
|
{
|
2015-06-09 07:06:26 +02:00
|
|
|
if (game.difficulty == DIFFICULTY_ORIGINAL)
|
|
|
|
bullet->damage = attacker->ammo[1];
|
|
|
|
else
|
|
|
|
bullet->damage = attacker->ammo[1] / 2;
|
|
|
|
|
2015-03-01 02:20:36 +01:00
|
|
|
if (bullet->damage < 15)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
|
|
|
bullet->damage = 1;
|
|
|
|
bullet->id = WT_PLASMA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bullet->target = NULL;
|
|
|
|
|
|
|
|
if (attacker->flags & FL_FRIEND)
|
|
|
|
imageIndex = 0;
|
|
|
|
else
|
|
|
|
imageIndex = 1;
|
|
|
|
|
|
|
|
// Use the enemy's images if applicable
|
|
|
|
if (bullet->id != WT_ROCKET)
|
|
|
|
bullet->image[0] = theWeapon->image[imageIndex];
|
|
|
|
else
|
|
|
|
bullet->image[0] = theWeapon->image[attacker->face];
|
|
|
|
|
|
|
|
if (bullet->flags & WF_AIMED)
|
|
|
|
{
|
2011-08-24 22:43:54 +02:00
|
|
|
tempX = (int)fabsf(attacker->target->x - attacker->x);
|
|
|
|
tempY = (int)fabsf(attacker->target->y - attacker->y);
|
2015-03-17 21:54:00 +01:00
|
|
|
steps = MAX(tempX, tempY);
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2011-08-24 14:23:29 +02:00
|
|
|
if (steps < 12)
|
2011-08-24 14:14:44 +02:00
|
|
|
steps = 12;
|
|
|
|
|
|
|
|
if (!(bullet->flags & WF_TIMEDEXPLOSION))
|
|
|
|
steps /= 8;
|
|
|
|
else
|
2016-11-28 21:50:47 +01:00
|
|
|
steps /= RANDRANGE(6, 11);
|
2011-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
tempX = (int)(attacker->target->x - attacker->x);
|
|
|
|
tempY = (int)(attacker->target->y - attacker->y);
|
|
|
|
|
|
|
|
bullet->dx = tempX / steps;
|
|
|
|
bullet->dy = tempY / steps;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attacker->classDef == CD_ASTEROID)
|
|
|
|
{
|
2015-03-29 16:19:53 +02:00
|
|
|
bullet->dx = RANDRANGE(-20, 20);
|
|
|
|
bullet->dy = RANDRANGE(-20, 20);
|
Fixed more magic numbers.
God, this one was definitely the biggest headache of all of the
magic number erasing. Never did I expect such cryptic problems.
The first problem was that the entirety of the player's weapon
struct was a part of the save file, *including the weapon's "image
indexes"*. Since the indexes have been changed, and the originally
used one is now unavailable when it's requested, this was causing
a segfault later on. Had to fix this by setting the image index
when the game is loaded.
The second problem was related to another bug I've been confused
about for years: the one that causes mobile rays to fire 5 green
shots. The entire reason those shots were green was because
the weapon's image indexes were undefined, and *that was causing
them to default to 0*. 0 was simply the index of green plasma.
Of course, though, now attempting to use that image causes a
segfault, so for now, I've fixed this by changing the image index
of the mobile rays to the red plasma bolts.
There are still some magic numbers left, related to the intermission
screen. But the hardest part is now done, thank God.
2016-01-06 04:12:29 +01:00
|
|
|
bullet->image[0] = gfx_sprites[SP_SMALL_EXPLOSION];
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
engine.bulletTail->next = bullet;
|
|
|
|
engine.bulletTail = bullet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Used for homing missiles. When a missile is active and it is told to home in
|
|
|
|
on an enemy, it will attempt to randomly grab one every frame if it does not
|
|
|
|
already have a target. If the target it is currently chasing is killed, it will
|
|
|
|
begin to look for a new one (done in doBullets()). The homing missile will make
|
|
|
|
one attempt per call (one call per frame) to find a suitable target. If the target
|
|
|
|
it picks is dead or outside the screen range, then it returns NULL. A suitable
|
2016-11-25 18:37:26 +01:00
|
|
|
target will be returned as the Object address.
|
2011-08-24 14:14:44 +02:00
|
|
|
*/
|
2016-11-25 18:37:26 +01:00
|
|
|
Object *bullet_getTarget(Object *bullet)
|
2011-08-24 14:14:44 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (bullet->owner->flags & FL_WEAPCO)
|
|
|
|
{
|
2016-11-28 21:35:26 +01:00
|
|
|
if (CHANCE(0.1))
|
2011-08-24 14:14:44 +02:00
|
|
|
return &player;
|
|
|
|
}
|
|
|
|
|
2015-03-09 01:59:33 +01:00
|
|
|
i = rand() % ALIEN_MAX;
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2015-03-07 15:42:24 +01:00
|
|
|
if ((aliens[i].shield < 1) || (!aliens[i].active))
|
2011-08-24 14:14:44 +02:00
|
|
|
return NULL;
|
2015-03-07 18:34:07 +01:00
|
|
|
|
|
|
|
if (aliens[i].flags & FL_IMMORTAL)
|
|
|
|
return NULL;
|
2011-08-24 14:14:44 +02:00
|
|
|
|
2015-03-07 15:42:24 +01:00
|
|
|
if ((bullet->owner->flags & FL_WEAPCO) && (aliens[i].flags & FL_WEAPCO))
|
2011-08-24 14:14:44 +02:00
|
|
|
return NULL;
|
|
|
|
|
2015-03-07 15:42:24 +01:00
|
|
|
if ((bullet->owner->flags & FL_FRIEND) && (aliens[i].flags & FL_FRIEND))
|
2011-08-24 14:14:44 +02:00
|
|
|
return NULL;
|
|
|
|
|
2015-03-07 15:42:24 +01:00
|
|
|
if (abs((int)bullet->x - (int)aliens[i].target->x) > 800)
|
2011-08-24 14:14:44 +02:00
|
|
|
return NULL;
|
|
|
|
|
2015-03-07 15:42:24 +01:00
|
|
|
if (abs((int)bullet->y - (int)aliens[i].target->y) > 200)
|
2011-08-24 14:14:44 +02:00
|
|
|
return NULL;
|
|
|
|
|
2015-03-07 15:42:24 +01:00
|
|
|
return &aliens[i];
|
2011-08-24 14:14:44 +02:00
|
|
|
}
|
2015-09-25 22:28:09 +02:00
|
|
|
|
2016-11-25 18:37:26 +01:00
|
|
|
int bullet_collision(Object *bullet, Object *ship)
|
2015-09-25 22:28:09 +02:00
|
|
|
{
|
|
|
|
float x0 = bullet->x;
|
|
|
|
float y0 = bullet->y;
|
|
|
|
float w0 = bullet->image[0]->w;
|
|
|
|
float h0 = bullet->image[0]->h;
|
|
|
|
|
|
|
|
float x2 = ship->x;
|
|
|
|
float y2 = ship->y;
|
|
|
|
float w1 = ship->image[0]->w;
|
|
|
|
float h1 = ship->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);
|
|
|
|
}
|