naev 0.11.5
weapon.c File Reference

Handles all the weapons in game. More...

#include "weapon.h"
#include "array.h"
#include "ai.h"
#include "camera.h"
#include "collision.h"
#include "explosion.h"
#include "gui.h"
#include "log.h"
#include "nstring.h"
#include "nlua_pilot.h"
#include "nlua_vec2.h"
#include "nlua_outfit.h"
#include "opengl.h"
#include "pilot.h"
#include "player.h"
#include "rng.h"
#include "spfx.h"
#include "intlist.h"

Go to the source code of this file.

Data Structures

struct  WeaponCollision
 Struct useful for generalization of weapno collisions. More...
 
struct  WeaponHit
 Represents a weapon hitting something. More...
 

Macros

#define QUADRATURE(ref, v)
 

Functions

static void weapon_updateVBO (void)
 Checks to see if we have to update the VBO size.
 
static double weapon_aimTurretAngle (const Outfit *outfit, const Pilot *parent, const Target *target, const vec2 *pos, const vec2 *vel, double dir, double time)
 Gets the aim direction of a turret weapon.
 
static double weapon_aimTurret (const Outfit *outfit, const Pilot *parent, const Target *target, const vec2 *pos, const vec2 *vel, double dir, double time)
 Gets the aim position of a turret weapon.
 
static double weapon_aimTurretStatic (const vec2 *target_pos, const vec2 *pos, double dir, double swivel)
 Gets the aim position of a turret weapon.
 
static void weapon_createBolt (Weapon *w, const Outfit *outfit, double T, double dir, const vec2 *pos, const vec2 *vel, const Pilot *parent, double time, int aim)
 Creates the bolt specific properties of a weapon.
 
static void weapon_createAmmo (Weapon *w, const Outfit *outfit, double T, double dir, const vec2 *pos, const vec2 *vel, const Pilot *parent, double time, int aim)
 Creates the ammo specific properties of a weapon.
 
static int weapon_create (Weapon *w, PilotOutfitSlot *po, const Outfit *ref, double T, double dir, const vec2 *pos, const vec2 *vel, const Pilot *parent, const Target *target, double time, int aim)
 Creates a new weapon.
 
static double weapon_computeTimes (double rdir, double rx, double ry, double dvx, double dvy, double pxv, double vmin, double acc, double *tt)
 Computes precisely interception times for propelled weapons (rockets).
 
static void weapon_render (Weapon *w, double dt)
 Renders an individual weapon.
 
static void weapon_updateCollide (Weapon *w, double dt)
 Updates an individual weapon.
 
static void weapon_update (Weapon *w, double dt)
 Updates an individual weapon.
 
static void weapon_sample_trail (Weapon *w)
 Updates the animated trail for a weapon.
 
static void weapon_destroy (Weapon *w)
 Destroys a weapon.
 
static void weapon_free (Weapon *w)
 Frees the weapon.
 
static int weapon_checkCanHit (const Weapon *w, const Pilot *p)
 Checks to see if the weapon can hit the pilot.
 
static void weapon_damage (Weapon *w, const Damage *dmg)
 Applies damage to a weapon.
 
static void weapon_hit (Weapon *w, const WeaponHit *hit)
 A bolt/launcher weapon hit something.
 
static void weapon_hitBeam (Weapon *w, const WeaponHit *hit, double dt)
 A beam weapon hit something.
 
static void weapon_miss (Weapon *w)
 Weapon missed and is due to be destroyed.
 
static int weapon_testCollision (const WeaponCollision *wc, const glTexture *ctex, int csx, int csy, const Solid *csol, const CollPoly *cpol, double cradius, vec2 crash[2])
 Tests to see if a weapon collides with a ship.
 
static void think_seeker (Weapon *w, double dt)
 The AI of seeker missiles.
 
static void think_beam (Weapon *w, double dt)
 The pseudo-ai of the beam weapons.
 
void weapon_minimap (double res, double w, double h, const RadarShape shape, double alpha)
 Draws the minimap weapons (used in player.c).
 
static void weapon_setAccel (Weapon *w, double accel)
 Sets the weapon's accel.
 
static void weapon_setTurn (Weapon *w, double turn)
 Sets the weapon's turn.
 
void weapon_init (void)
 Initializes the weapon stuff.
 
Weaponweapon_getStack (void)
 Gets the weapon stack. Do not manipulate directly.
 
static int weapon_cmp (const void *ptr1, const void *ptr2)
 Compare id (for use with bsearch)
 
Weaponweapon_getID (unsigned int id)
 Gets a weapon by ID.
 
void weapon_newSystem (void)
 Sets up collision stuff for a new system.
 
void weapons_updatePurge (void)
 Purges unnecessary weapons.
 
void weapons_updateCollide (double dt)
 Handles weapon collisions.
 
void weapons_update (double dt)
 Updates all the weapons.
 
void weapons_render (const WeaponLayer layer, double dt)
 Renders all the weapons in a layer.
 
static void weapon_renderBeam (Weapon *w, double dt)
 
void weapon_hitAI (Pilot *p, const Pilot *shooter, double dmg)
 Informs the AI if needed that it's been hit.
 
static void weapon_hitExplode (Weapon *w, const Damage *dmg, double radius)
 A weapon hit something and decided to explode.
 
int weapon_inArc (const Outfit *o, const Pilot *parent, const Target *target, const vec2 *pos, const vec2 *vel, double dir, double time)
 Gets the aim position of a turret weapon.
 
Weaponweapon_add (PilotOutfitSlot *po, const Outfit *ref, double dir, const vec2 *pos, const vec2 *vel, const Pilot *parent, const Target *target, double time, int aim)
 Creates a new weapon.
 
double weapon_targetFlyTime (const Outfit *o, const Pilot *p, const Target *t)
 Gets the fly time for a weapon target.
 
unsigned int beam_start (PilotOutfitSlot *po, double dir, const vec2 *pos, const vec2 *vel, const Pilot *parent, const Target *target, int aim)
 Starts a beam weapon.
 
void beam_end (unsigned int beam)
 Ends a beam weapon.
 
void weapon_clear (void)
 Clears all the weapons, does NOT free the layers.
 
void weapon_exit (void)
 Destroys all the weapons and frees it all.
 
const IntListweapon_collideQuery (int x1, int y1, int x2, int y2)
 
void weapon_collideQueryIL (IntList *il, int x1, int y1, int x2, int y2)
 

Variables

static Weaponweapon_stack = NULL
 
static gl_vbo * weapon_vbo = NULL
 
static GLfloat * weapon_vboData = NULL
 
static size_t weapon_vboSize = 0
 
static unsigned int weapon_idgen = 0
 
static int qt_init = 0
 
static Quadtree weapon_quadtree
 
static IntList weapon_qtquery
 
static IntList weapon_qtexp
 

Detailed Description

Handles all the weapons in game.

Weapons are what gets created when a pilot shoots. They are based on the outfit that created them.

Definition in file weapon.c.

Macro Definition Documentation

◆ QUADRATURE

#define QUADRATURE ( ref,
v )
Value:
((v).x * (-(ref).y) + (v).y * (ref).x)

Function Documentation

◆ beam_end()

void beam_end ( unsigned int beam)

Ends a beam weapon.

Parameters
beamID of the beam to destroy.

Definition at line 2608 of file weapon.c.

◆ beam_start()

unsigned int beam_start ( PilotOutfitSlot * po,
double dir,
const vec2 * pos,
const vec2 * vel,
const Pilot * parent,
const Target * target,
int aim )

Starts a beam weapon.

Parameters
poOutfit slot which spawns the weapon.
dirDirection of the shooter.
posPosition of the slot (absolute).
velVelocity of the slot (absolute).
parentPilot shooter.
targetTarget that is getting shot.
aimWhether or not to aim.
Returns
The identifier of the beam weapon.
See also
beam_end

Definition at line 2584 of file weapon.c.

◆ think_beam()

static void think_beam ( Weapon * w,
double dt )
static

The pseudo-ai of the beam weapons.

Parameters
wWeapon to do the thinking.
dtCurrent delta tick.

Definition at line 465 of file weapon.c.

◆ think_seeker()

static void think_seeker ( Weapon * w,
double dt )
static

The AI of seeker missiles.

Parameters
wWeapon to do the thinking.
dtCurrent delta tick.

Definition at line 316 of file weapon.c.

◆ weapon_add()

Weapon * weapon_add ( PilotOutfitSlot * po,
const Outfit * ref,
double dir,
const vec2 * pos,
const vec2 * vel,
const Pilot * parent,
const Target * target,
double time,
int aim )

Creates a new weapon.

Parameters
poOutfit slot which spawns the weapon.
refReference outfit to use for computing damage and properties.
dirDirection of the shooter.
posPosition of the slot (absolute).
velVelocity of the slot (absolute).
parentPilot ID of the shooter.
targetTarget ID that is getting shot.
timeExpected flight time.
aimWhether or not to aim.

Definition at line 2511 of file weapon.c.

◆ weapon_aimTurret()

static double weapon_aimTurret ( const Outfit * outfit,
const Pilot * parent,
const Target * target,
const vec2 * pos,
const vec2 * vel,
double dir,
double time )
static

Gets the aim position of a turret weapon.

Parameters
outfitWeapon outfit.
parentParent of the weapon.
targetTarget of the weapon.
posPosition of the turret.
velVelocity of the turret.
dirDirection facing parent ship and turret.
timeExpected flight time.
Returns
The direction to aim.

Definition at line 2080 of file weapon.c.

◆ weapon_aimTurretAngle()

static double weapon_aimTurretAngle ( const Outfit * outfit,
const Pilot * parent,
const Target * target,
const vec2 * pos,
const vec2 * vel,
double dir,
double time )
static

Gets the aim direction of a turret weapon.

Definition at line 1948 of file weapon.c.

◆ weapon_aimTurretStatic()

static double weapon_aimTurretStatic ( const vec2 * target_pos,
const vec2 * pos,
double dir,
double swivel )
static

Gets the aim position of a turret weapon.

Parameters
target_posTarget of the weapon.
posPosition of the turret.
dirDirection facing parent ship and turret.
swivelMaximum angle between weapon and straight ahead.

Definition at line 2113 of file weapon.c.

◆ weapon_checkCanHit()

static int weapon_checkCanHit ( const Weapon * w,
const Pilot * p )
static

Checks to see if the weapon can hit the pilot.

Parameters
wWeapon to check if hits pilot.
pPilot to check if is hit by weapon.
Returns
1 if can be hit, 0 if can't.

Definition at line 912 of file weapon.c.

◆ weapon_clear()

void weapon_clear ( void )

Clears all the weapons, does NOT free the layers.

Definition at line 2680 of file weapon.c.

◆ weapon_cmp()

static int weapon_cmp ( const void * ptr1,
const void * ptr2 )
static

Compare id (for use with bsearch)

Definition at line 163 of file weapon.c.

◆ weapon_collideQuery()

const IntList * weapon_collideQuery ( int x1,
int y1,
int x2,
int y2 )

Definition at line 2715 of file weapon.c.

◆ weapon_collideQueryIL()

void weapon_collideQueryIL ( IntList * il,
int x1,
int y1,
int x2,
int y2 )

Definition at line 2721 of file weapon.c.

◆ weapon_computeTimes()

static double weapon_computeTimes ( double rdir,
double rx,
double ry,
double dvx,
double dvy,
double pxv,
double vmin,
double acc,
double * tt )
static

Computes precisely interception times for propelled weapons (rockets).

Parameters
rdirtried shooting angle.
rxRelative position x.
ryRelative position y.
dvxRelative velocity x.
dvyRelative velocity y.
pxvCross product between relative position and relative velocity
vminmin ammo velocity.
accammo acceleration.
[out]ttTime for the target to reach the interception point.

Definition at line 2146 of file weapon.c.

◆ weapon_create()

static int weapon_create ( Weapon * w,
PilotOutfitSlot * po,
const Outfit * ref,
double T,
double dir,
const vec2 * pos,
const vec2 * vel,
const Pilot * parent,
const Target * target,
double time,
int aim )
static

Creates a new weapon.

Parameters
wWeapon to create.
poOutfit slot which spawned the weapon.
refReference outfit to use, does not have to be the outfit in the slot, but will default to it if set to NULL.
Ttemperature of the shooter.
dirDirection the shooter is facing.
posPosition of the slot (absolute).
velVelocity of the slot (absolute).
parentShooter.
targetTarget ID of the shooter.
timeExpected flight time.
aimWhether or not to aim.
Returns
A pointer to the newly created weapon.

< Needs a mass.

Definition at line 2367 of file weapon.c.

◆ weapon_createAmmo()

static void weapon_createAmmo ( Weapon * w,
const Outfit * outfit,
double T,
double dir,
const vec2 * pos,
const vec2 * vel,
const Pilot * parent,
double time,
int aim )
static

Creates the ammo specific properties of a weapon.

Parameters
wWeapon to create ammo specific properties of.
outfitOutfit which spawned the weapon.
Ttemperature of the shooter.
dirDirection the shooter is facing.
posPosition of the slot (absolute).
velVelocity of the slot (absolute).
parentShooter.
timeExpected flight time.
aimWhether or not to aim.

Definition at line 2258 of file weapon.c.

◆ weapon_createBolt()

static void weapon_createBolt ( Weapon * w,
const Outfit * outfit,
double T,
double dir,
const vec2 * pos,
const vec2 * vel,
const Pilot * parent,
double time,
int aim )
static

Creates the bolt specific properties of a weapon.

Parameters
wWeapon to create bolt specific properties of.
outfitOutfit which spawned the weapon.
Ttemperature of the shooter.
dirDirection the shooter is facing.
posPosition of the slot (absolute).
velVelocity of the slot (absolute).
parentShooter.
timeExpected flight time.
aimWhether or not to aim.

Definition at line 2179 of file weapon.c.

◆ weapon_damage()

static void weapon_damage ( Weapon * w,
const Damage * dmg )
static

Applies damage to a weapon.

Parameters
wWeapon being damaged.
dmgDamage being applied.

Definition at line 1764 of file weapon.c.

◆ weapon_destroy()

static void weapon_destroy ( Weapon * w)
static

Destroys a weapon.

Parameters
wWeapon to destroy.

Definition at line 2632 of file weapon.c.

◆ weapon_exit()

void weapon_exit ( void )

Destroys all the weapons and frees it all.

Definition at line 2696 of file weapon.c.

◆ weapon_free()

static void weapon_free ( Weapon * w)
static

Frees the weapon.

Parameters
wWeapon to free.

Definition at line 2643 of file weapon.c.

◆ weapon_getID()

Weapon * weapon_getID ( unsigned int id)

Gets a weapon by ID.

Definition at line 174 of file weapon.c.

◆ weapon_getStack()

Weapon * weapon_getStack ( void )

Gets the weapon stack. Do not manipulate directly.

Definition at line 138 of file weapon.c.

◆ weapon_hit()

static void weapon_hit ( Weapon * w,
const WeaponHit * hit )
static

A bolt/launcher weapon hit something.

Parameters
wWeapon involved in the collision.
hitThe actual hit.

Definition at line 1614 of file weapon.c.

◆ weapon_hitAI()

void weapon_hitAI ( Pilot * p,
const Pilot * shooter,
double dmg )

Informs the AI if needed that it's been hit.

Parameters
pPilot being hit.
shooterPilot that shot.
dmgDamage done to p.

Definition at line 1429 of file weapon.c.

◆ weapon_hitBeam()

static void weapon_hitBeam ( Weapon * w,
const WeaponHit * hit,
double dt )
static

A beam weapon hit something.

Parameters
wWeapon involved in the collision.
hitThe actual hit.
dtCurrent delta tick.

Definition at line 1789 of file weapon.c.

◆ weapon_hitExplode()

static void weapon_hitExplode ( Weapon * w,
const Damage * dmg,
double radius )
static

A weapon hit something and decided to explode.

Parameters
wWeapon exploding.
dmgDamage it does.
radiusRadius of the explosion.

Definition at line 1472 of file weapon.c.

◆ weapon_inArc()

int weapon_inArc ( const Outfit * o,
const Pilot * parent,
const Target * target,
const vec2 * pos,
const vec2 * vel,
double dir,
double time )

Gets the aim position of a turret weapon.

Parameters
oWeapon outfit.
parentParent of the weapon.
targetTarget of the weapon.
posPosition of the turret.
velVelocity of the turret.
dirDirection facing parent ship and turret.
timeExpected flight time.
Returns
0 if not in arc, 1 if in arc.

Definition at line 1888 of file weapon.c.

◆ weapon_init()

void weapon_init ( void )

Initializes the weapon stuff.

Definition at line 128 of file weapon.c.

◆ weapon_minimap()

void weapon_minimap ( double res,
double w,
double h,
const RadarShape shape,
double alpha )

Draws the minimap weapons (used in player.c).

Parameters
resMinimap resolution.
wWidth of minimap.
hHeight of minimap.
shapeShape of the minimap.
alphaAlpha to draw points at.

Definition at line 204 of file weapon.c.

◆ weapon_miss()

static void weapon_miss ( Weapon * w)
static

Weapon missed and is due to be destroyed.

Parameters
wWeapon that missed.

Definition at line 1691 of file weapon.c.

◆ weapon_newSystem()

void weapon_newSystem ( void )

Sets up collision stuff for a new system.

Definition at line 186 of file weapon.c.

◆ weapon_render()

static void weapon_render ( Weapon * w,
double dt )
static

Renders an individual weapon.

Parameters
wWeapon to render.
dtCurrent delta tick.

Definition at line 777 of file weapon.c.

◆ weapon_renderBeam()

static void weapon_renderBeam ( Weapon * w,
double dt )
static

Definition at line 721 of file weapon.c.

◆ weapon_sample_trail()

static void weapon_sample_trail ( Weapon * w)
static

Updates the animated trail for a weapon.

Definition at line 1396 of file weapon.c.

◆ weapon_setAccel()

static void weapon_setAccel ( Weapon * w,
double accel )
static

Sets the weapon's accel.

Definition at line 297 of file weapon.c.

◆ weapon_setTurn()

static void weapon_setTurn ( Weapon * w,
double turn )
static

Sets the weapon's turn.

Definition at line 305 of file weapon.c.

◆ weapon_targetFlyTime()

double weapon_targetFlyTime ( const Outfit * o,
const Pilot * p,
const Target * t )

Gets the fly time for a weapon target.

Definition at line 2538 of file weapon.c.

◆ weapon_testCollision()

static int weapon_testCollision ( const WeaponCollision * wc,
const glTexture * ctex,
int csx,
int csy,
const Solid * csol,
const CollPoly * cpol,
double cradius,
vec2 crash[2] )
static

Tests to see if a weapon collides with a ship.

Parameters
wcWeapon collision data.
ctexCollision target texture.
csxCollision target texture x sprite.
csyCollision target texture y sprite.
csolCollision target solid.
cpolCollision target collision polygon (NULL if none).
cradiusCollision radius fallback (if no texture and polygon are provided).
[out]crashCrash location, which is only set if collision is detected.
Returns
Number of collisions detected (0 to 2)

Definition at line 985 of file weapon.c.

◆ weapon_update()

static void weapon_update ( Weapon * w,
double dt )
static

Updates an individual weapon.

Parameters
wWeapon to update.
dtCurrent delta tick.

Definition at line 1375 of file weapon.c.

◆ weapon_updateCollide()

static void weapon_updateCollide ( Weapon * w,
double dt )
static

Updates an individual weapon.

Parameters
wWeapon to update.
dtCurrent delta tick.

Definition at line 1144 of file weapon.c.

◆ weapon_updateVBO()

static void weapon_updateVBO ( void )
static

Checks to see if we have to update the VBO size.

Definition at line 146 of file weapon.c.

◆ weapons_render()

void weapons_render ( const WeaponLayer layer,
double dt )

Renders all the weapons in a layer.

Parameters
layerLayer to render.
dtCurrent delta tick.

Definition at line 712 of file weapon.c.

◆ weapons_update()

void weapons_update ( double dt)

Updates all the weapons.

Parameters
dtCurrent delta tick.

Definition at line 696 of file weapon.c.

◆ weapons_updateCollide()

void weapons_updateCollide ( double dt)

Handles weapon collisions.

Definition at line 629 of file weapon.c.

◆ weapons_updatePurge()

void weapons_updatePurge ( void )

Purges unnecessary weapons.

Definition at line 585 of file weapon.c.

Variable Documentation

◆ qt_init

int qt_init = 0
static

Whether or not the quadtree was created.

Definition at line 75 of file weapon.c.

◆ weapon_idgen

unsigned int weapon_idgen = 0
static

Weapon identifier generator.

Definition at line 74 of file weapon.c.

◆ weapon_qtexp

IntList weapon_qtexp
static

For querying collisions from explosions.

Definition at line 78 of file weapon.c.

◆ weapon_qtquery

IntList weapon_qtquery
static

For querying collisions.

Definition at line 77 of file weapon.c.

◆ weapon_quadtree

Quadtree weapon_quadtree
static

Quadtree for weapons.

Definition at line 76 of file weapon.c.

◆ weapon_stack

Weapon* weapon_stack = NULL
static

All the weapon munitions are piled up here.

Definition at line 66 of file weapon.c.

◆ weapon_vbo

gl_vbo* weapon_vbo = NULL
static

Weapon VBO.

Definition at line 69 of file weapon.c.

◆ weapon_vboData

GLfloat* weapon_vboData = NULL
static

Data of weapon VBO.

Definition at line 70 of file weapon.c.

◆ weapon_vboSize

size_t weapon_vboSize = 0
static

Size of the VBO.

Definition at line 71 of file weapon.c.