2018-01-22 09:27:08 +01:00
|
|
|
/*
|
|
|
|
Copyright (C) 2018 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "weapons.h"
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
Bullet *createBaseBullet(Unit *owner);
|
2018-01-22 22:47:04 +01:00
|
|
|
|
|
|
|
static int bulletSprite[2];
|
|
|
|
static int plasmaSprite[2];
|
|
|
|
static int aimedSprite;
|
|
|
|
static int spreadShotSprite;
|
|
|
|
static int alienSpreadShotSprite;
|
|
|
|
static int laserSprite[2];
|
|
|
|
static int grenadeSprite;
|
|
|
|
static int alienGrenadeSprite;
|
|
|
|
static int shotgunPelletSprite;
|
|
|
|
static int missileSprite[2];
|
|
|
|
|
2018-01-29 23:12:18 +01:00
|
|
|
static void tick(void);
|
|
|
|
static void touch(Entity *other);
|
|
|
|
|
2018-01-22 22:47:04 +01:00
|
|
|
void initWeapons(void)
|
|
|
|
{
|
|
|
|
bulletSprite[0] = getSpriteIndex("BulletRight");
|
|
|
|
bulletSprite[1] = getSpriteIndex("BulletLeft");
|
|
|
|
|
|
|
|
plasmaSprite[0] = getSpriteIndex("PlasmaRight");
|
|
|
|
plasmaSprite[1] = getSpriteIndex("PlasmaLeft");
|
|
|
|
|
|
|
|
aimedSprite = getSpriteIndex("AimedShot");
|
|
|
|
|
|
|
|
spreadShotSprite = getSpriteIndex("SpreadShot");
|
|
|
|
alienSpreadShotSprite = getSpriteIndex("AlienSpreadShot");
|
|
|
|
|
|
|
|
laserSprite[0] = getSpriteIndex("Laser");
|
|
|
|
laserSprite[1] = getSpriteIndex("AlienLaser");
|
|
|
|
|
|
|
|
grenadeSprite = getSpriteIndex("Grenade");
|
|
|
|
alienGrenadeSprite = getSpriteIndex("AlienGrenade");
|
|
|
|
|
|
|
|
shotgunPelletSprite = getSpriteIndex("ShotgunPellet");
|
|
|
|
|
|
|
|
missileSprite[0] = getSpriteIndex("MissileRight");
|
|
|
|
missileSprite[1] = getSpriteIndex("MissileLeft");
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
void firePistol(Unit *owner)
|
2018-01-22 22:47:04 +01:00
|
|
|
{
|
2018-01-27 09:13:50 +01:00
|
|
|
Bullet *bullet;
|
2018-01-22 22:47:04 +01:00
|
|
|
|
|
|
|
bullet = createBaseBullet(owner);
|
|
|
|
bullet->weaponType = WPN_PISTOL;
|
|
|
|
bullet->facing = owner->facing;
|
|
|
|
bullet->sprite[0] = bulletSprite[0];
|
|
|
|
bullet->sprite[1] = bulletSprite[1];
|
|
|
|
|
|
|
|
owner->reload = 20;
|
|
|
|
|
|
|
|
playSound(SND_PISTOL, CH_PLAYER);
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
void fireAimedShot(Unit *owner)
|
2018-01-22 22:47:04 +01:00
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
float dx, dy;
|
2018-01-27 09:13:50 +01:00
|
|
|
Bullet *bullet;
|
2018-01-22 22:47:04 +01:00
|
|
|
|
|
|
|
x = (int) (world.bob->x + rrnd(-8, 24));
|
|
|
|
y = (int) (world.bob->y + rrnd(-8, 24));
|
|
|
|
|
|
|
|
getSlope(x, y, owner->x, owner->y, &dx, &dy);
|
|
|
|
|
|
|
|
bullet = createBaseBullet(owner);
|
|
|
|
bullet->weaponType = WPN_AIMED_PISTOL;
|
|
|
|
bullet->dx = dx * 6;
|
|
|
|
bullet->dy = dy * 6;
|
|
|
|
bullet->sprite[0] = bullet->sprite[1] = aimedSprite;
|
|
|
|
bullet->health *= 2;
|
|
|
|
|
|
|
|
owner->reload = 15;
|
|
|
|
|
|
|
|
playSound(SND_PISTOL, CH_WEAPON);
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
void fireMachineGun(Unit *owner)
|
2018-01-22 22:47:04 +01:00
|
|
|
{
|
2018-01-27 09:13:50 +01:00
|
|
|
Bullet *bullet;
|
2018-01-22 22:47:04 +01:00
|
|
|
|
|
|
|
bullet = createBaseBullet(owner);
|
|
|
|
bullet->weaponType = WPN_MACHINE_GUN;
|
|
|
|
bullet->sprite[0] = bulletSprite[0];
|
|
|
|
bullet->sprite[1] = bulletSprite[1];
|
|
|
|
owner->reload = 8;
|
|
|
|
|
|
|
|
playSound(SND_MACHINE_GUN, CH_WEAPON);
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
void firePlasma(Unit *owner)
|
2018-01-22 22:47:04 +01:00
|
|
|
{
|
2018-01-27 09:13:50 +01:00
|
|
|
Bullet *bullet;
|
2018-01-22 22:47:04 +01:00
|
|
|
|
|
|
|
bullet = createBaseBullet(owner);
|
|
|
|
bullet->weaponType = WPN_PLASMA;
|
|
|
|
bullet->sprite[0] = plasmaSprite[0];
|
|
|
|
bullet->sprite[1] = plasmaSprite[1];
|
|
|
|
bullet->damage = 2;
|
|
|
|
|
|
|
|
owner->reload = owner->type == ET_BOB ? 4 : 8;
|
|
|
|
|
|
|
|
playSound(SND_PLASMA, owner->type == ET_BOB ? CH_PLAYER : CH_WEAPON);
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
void fireSpread(Unit *owner, int numberOfShots)
|
2018-01-22 22:47:04 +01:00
|
|
|
{
|
2018-01-27 09:13:50 +01:00
|
|
|
Bullet *bullet;
|
2018-01-22 22:47:04 +01:00
|
|
|
int i;
|
|
|
|
float dy;
|
|
|
|
|
|
|
|
dy = -(numberOfShots / 2) * 3;
|
|
|
|
|
|
|
|
for (i = 0 ; i < numberOfShots ; i++)
|
|
|
|
{
|
|
|
|
bullet = createBaseBullet(owner);
|
|
|
|
bullet->weaponType = WPN_SPREAD;
|
|
|
|
bullet->sprite[0] = bullet->sprite[1] = owner->type == ET_BOB ? spreadShotSprite : alienSpreadShotSprite;
|
|
|
|
bullet->dx = owner->facing == FACING_RIGHT ? 15 : -15;
|
|
|
|
bullet->dy = dy;
|
|
|
|
|
|
|
|
dy += 3;
|
|
|
|
|
|
|
|
owner->reload = 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
playSound(SND_SPREAD, owner->type == ET_BOB ? CH_PLAYER : CH_WEAPON);
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
void fireLaser(Unit *owner)
|
2018-01-22 22:47:04 +01:00
|
|
|
{
|
2018-01-27 09:13:50 +01:00
|
|
|
Bullet *laser;
|
2018-01-22 22:47:04 +01:00
|
|
|
|
|
|
|
laser = createBaseBullet(owner);
|
|
|
|
laser->x = owner->x + owner->w / 2;
|
|
|
|
laser->y = owner->y + owner->h / 2;
|
|
|
|
laser->facing = owner->facing;
|
|
|
|
laser->dx = owner->facing == FACING_RIGHT ? 20 : -20;
|
|
|
|
laser->health = FPS * 3;
|
|
|
|
laser->sprite[0] = laser->sprite[1] = (owner->type == ET_BOB) ? laserSprite[0] : laserSprite[1];
|
|
|
|
|
2018-01-29 23:12:18 +01:00
|
|
|
initLaser(laser);
|
|
|
|
|
2018-01-22 22:47:04 +01:00
|
|
|
owner->reload = owner->type == ET_BOB ? FPS / 2 : FPS;
|
|
|
|
|
|
|
|
playSound(SND_LASER, owner->type == ET_BOB ? CH_PLAYER : CH_WEAPON);
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
void fireGrenade(Unit *owner)
|
2018-01-22 22:47:04 +01:00
|
|
|
{
|
2018-01-27 09:13:50 +01:00
|
|
|
Bullet *grenade;
|
2018-01-22 22:47:04 +01:00
|
|
|
|
|
|
|
grenade = createBaseBullet(owner);
|
|
|
|
grenade->x = owner->x + owner->w / 2;
|
|
|
|
grenade->y = owner->y;
|
|
|
|
grenade->facing = owner->facing;
|
|
|
|
grenade->health = FPS * 3;
|
|
|
|
grenade->dx = owner->facing == FACING_RIGHT ? 8 : -8;
|
|
|
|
grenade->sprite[0] = grenade->sprite[1] = (owner->type == ET_BOB) ? grenadeSprite : alienGrenadeSprite;
|
|
|
|
grenade->dy = -6;
|
|
|
|
|
2018-01-29 23:12:18 +01:00
|
|
|
initGrenade(grenade);
|
|
|
|
|
2018-01-22 22:47:04 +01:00
|
|
|
owner->reload = FPS / 2;
|
|
|
|
|
|
|
|
playSound(SND_THROW, owner->type == ET_BOB ? CH_PLAYER : CH_WEAPON);
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
void fireShotgun(Unit *owner)
|
2018-01-22 22:47:04 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
float dx, dy;
|
2018-01-27 09:13:50 +01:00
|
|
|
Bullet *bullet;
|
2018-01-22 22:47:04 +01:00
|
|
|
|
|
|
|
for (i = 0 ; i < 8 ; i++)
|
|
|
|
{
|
|
|
|
getSlope(world.bob->x + rrnd(0, 40), world.bob->y + rrnd(0, 40), owner->x, owner->y, &dx, &dy);
|
|
|
|
|
|
|
|
bullet = createBaseBullet(owner);
|
|
|
|
bullet->weaponType = WPN_SHOTGUN;
|
|
|
|
bullet->x = owner->x + (owner->w / 2) + rrnd(-8, 8);
|
|
|
|
bullet->y = owner->y + (owner->h / 2) + rrnd(-8, 8);
|
|
|
|
bullet->dx = dx * 12;
|
|
|
|
bullet->dy = dy * 12;
|
|
|
|
bullet->damage = 2;
|
|
|
|
bullet->sprite[0] = bullet->sprite[1] = shotgunPelletSprite;
|
|
|
|
|
|
|
|
owner->reload = 15;
|
|
|
|
}
|
|
|
|
|
|
|
|
playSound(SND_SHOTGUN, CH_WEAPON);
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
void fireMissile(Unit *owner)
|
2018-01-22 22:47:04 +01:00
|
|
|
{
|
2018-01-27 09:13:50 +01:00
|
|
|
Bullet *missile;
|
2018-01-22 22:47:04 +01:00
|
|
|
|
|
|
|
missile = createBaseBullet(owner);
|
|
|
|
missile->x = owner->x + owner->w / 2;
|
|
|
|
missile->y = owner->y + 10;
|
|
|
|
missile->facing = owner->facing;
|
|
|
|
missile->dx = owner->facing == FACING_RIGHT ? 10 : -10;
|
|
|
|
missile->health = FPS * 3;
|
|
|
|
missile->sprite[0] = missileSprite[0];
|
|
|
|
missile->sprite[1] = missileSprite[1];
|
|
|
|
|
2018-01-29 23:12:18 +01:00
|
|
|
initMissile(missile);
|
|
|
|
|
2018-01-22 22:47:04 +01:00
|
|
|
owner->reload = FPS / 2;
|
|
|
|
|
|
|
|
playSound(SND_MISSILE, CH_WEAPON);
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
Bullet *createBaseBullet(Unit *owner)
|
2018-01-22 22:47:04 +01:00
|
|
|
{
|
2018-01-27 09:13:50 +01:00
|
|
|
Bullet *bullet;
|
|
|
|
|
|
|
|
bullet = malloc(sizeof(Bullet));
|
|
|
|
memset(bullet, 0, sizeof(Bullet));
|
2018-01-28 09:30:53 +01:00
|
|
|
world.entityTail->next = (Entity*)bullet;
|
|
|
|
world.entityTail = (Entity*)bullet;
|
2018-01-29 23:12:18 +01:00
|
|
|
|
|
|
|
initEntity((Entity*)bullet);
|
2018-01-22 22:47:04 +01:00
|
|
|
|
|
|
|
bullet->x = (owner->x + owner->w / 2);
|
|
|
|
bullet->y = (owner->y + owner->h / 2) - 3;
|
|
|
|
bullet->dx = owner->facing == FACING_RIGHT ? 15 : -15;
|
|
|
|
bullet->facing = owner->facing;
|
|
|
|
bullet->damage = 1;
|
2018-01-28 09:30:53 +01:00
|
|
|
bullet->owner = (Entity*)owner;
|
2018-01-22 22:47:04 +01:00
|
|
|
bullet->health = FPS * 3;
|
2018-01-27 09:28:50 +01:00
|
|
|
bullet->flags |= EF_WEIGHTLESS | EF_IGNORE_BULLETS | EF_NO_ENVIRONMENT | EF_KILL_OFFSCREEN | EF_NO_TELEPORT;
|
2018-01-22 22:47:04 +01:00
|
|
|
|
2018-01-29 23:12:18 +01:00
|
|
|
bullet->tick = tick;
|
|
|
|
bullet->touch = touch;
|
|
|
|
|
2018-01-22 22:47:04 +01:00
|
|
|
return bullet;
|
|
|
|
}
|
|
|
|
|
2018-01-29 23:12:18 +01:00
|
|
|
static void tick(void)
|
|
|
|
{
|
|
|
|
Bullet *b;
|
|
|
|
|
|
|
|
b = (Bullet*)self;
|
|
|
|
|
|
|
|
b->health--;
|
|
|
|
|
|
|
|
if (b->x <= 0 || b->y <= 0 || b->x >= (MAP_WIDTH * MAP_TILE_SIZE) - b->w || b->y >= (MAP_HEIGHT * MAP_TILE_SIZE) - b->h)
|
|
|
|
{
|
|
|
|
b->alive = ALIVE_DEAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't allow the player to kill everything on the map by firing
|
|
|
|
// constantly
|
|
|
|
if (b->owner->type == ET_BOB)
|
|
|
|
{
|
|
|
|
if (b->x < camera.x || b->y < camera.y || b->x > camera.x + SCREEN_WIDTH || b->y > camera.y + SCREEN_HEIGHT)
|
|
|
|
{
|
|
|
|
b->alive = ALIVE_DEAD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void touch(Entity *other)
|
|
|
|
{
|
|
|
|
Bullet *b;
|
|
|
|
|
|
|
|
b = (Bullet*)self;
|
|
|
|
|
|
|
|
if (b->alive == ALIVE_ALIVE)
|
|
|
|
{
|
|
|
|
if (other == NULL)
|
|
|
|
{
|
|
|
|
addSparkParticles(b->x, b->y);
|
|
|
|
|
|
|
|
b->alive = ALIVE_DEAD;
|
|
|
|
|
|
|
|
if (rand() % 2)
|
|
|
|
{
|
|
|
|
playSound(SND_RICO_1, CH_ANY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
playSound(SND_RICO_2, CH_ANY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (other != b->owner && (!(other->flags & EF_IGNORE_BULLETS)) && b->owner->type != other->type)
|
|
|
|
{
|
|
|
|
other->applyDamage(b->damage);
|
|
|
|
|
|
|
|
if (other->flags & EF_EXPLODES)
|
|
|
|
{
|
|
|
|
playSound(SND_METAL_HIT, CH_ANY);
|
|
|
|
|
|
|
|
addSparkParticles(b->x, b->y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
playSound(SND_FLESH_HIT, CH_ANY);
|
|
|
|
|
|
|
|
addSmallFleshChunk(b->x, b->y);
|
|
|
|
}
|
|
|
|
|
|
|
|
b->alive = ALIVE_DEAD;
|
|
|
|
|
|
|
|
if (b->owner->type == world.bob->type)
|
|
|
|
{
|
|
|
|
game.statShotsHit[b->weaponType]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 09:27:08 +01:00
|
|
|
int getRandomPlayerWeapon(int excludeGrenades)
|
|
|
|
{
|
|
|
|
if (excludeGrenades)
|
|
|
|
{
|
|
|
|
return rand() % WPN_GRENADES;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rand() % WPN_ANY;
|
|
|
|
}
|