naev 0.12.5
weapon.c File Reference

Handles all the weapons in game. More...

#include "weapon.h"
#include "ai.h"
#include "array.h"
#include "camera.h"
#include "collision.h"
#include "damagetype.h"
#include "gui.h"
#include "input.h"
#include "intlist.h"
#include "log.h"
#include "nlua_outfit.h"
#include "nlua_pilot.h"
#include "nlua_vec2.h"
#include "ntracing.h"
#include "opengl.h"
#include "pilot.h"
#include "player.h"
#include "quadtree.h"
#include "rng.h"
#include "sound.h"
#include "spfx.h"
Include dependency graph for weapon.c:

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...

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 CollPolyView *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, const vec2 *center)
 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
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.

Function Documentation

◆ beam_end()

void beam_end ( unsigned int beam)

Ends a beam weapon.

Parameters
beamID of the beam to destroy.

Definition at line 2779 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 2755 of file weapon.c.

◆ think_beam()

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 486 of file weapon.c.

◆ think_seeker()

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 346 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 2689 of file weapon.c.

◆ weapon_aimTurret()

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 2244 of file weapon.c.

◆ weapon_aimTurretAngle()

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 2107 of file weapon.c.

◆ weapon_aimTurretStatic()

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 2278 of file weapon.c.

◆ weapon_checkCanHit()

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 938 of file weapon.c.

◆ weapon_clear()

void weapon_clear ( void )

Clears all the weapons, does NOT free the layers.

Definition at line 2849 of file weapon.c.

◆ weapon_cmp()

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

Compare id (for use with bsearch)

Definition at line 179 of file weapon.c.

◆ weapon_collideQuery()

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

Definition at line 2889 of file weapon.c.

◆ weapon_collideQueryIL()

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

Definition at line 2895 of file weapon.c.

◆ weapon_computeTimes()

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 2312 of file weapon.c.

◆ weapon_create()

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 2542 of file weapon.c.

◆ weapon_createAmmo()

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 2431 of file weapon.c.

◆ weapon_createBolt()

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 2351 of file weapon.c.

◆ weapon_damage()

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

Applies damage to a weapon.

Parameters
wWeapon being damaged.
dmgDamage being applied.

Definition at line 1900 of file weapon.c.

◆ weapon_destroy()

void weapon_destroy ( Weapon * w)
static

Destroys a weapon.

Parameters
wWeapon to destroy.

Definition at line 2803 of file weapon.c.

◆ weapon_exit()

void weapon_exit ( void )

Destroys all the weapons and frees it all.

Definition at line 2870 of file weapon.c.

◆ weapon_free()

void weapon_free ( Weapon * w)
static

Frees the weapon.

Parameters
wWeapon to free.

Definition at line 2814 of file weapon.c.

◆ weapon_getID()

Weapon * weapon_getID ( unsigned int id)

Gets a weapon by ID.

Definition at line 190 of file weapon.c.

◆ weapon_getStack()

Weapon * weapon_getStack ( void )

Gets the weapon stack. Do not manipulate directly.

Definition at line 154 of file weapon.c.

◆ weapon_hit()

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 1748 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 1522 of file weapon.c.

◆ weapon_hitBeam()

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 1946 of file weapon.c.

◆ weapon_hitExplode()

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

A weapon hit something and decided to explode.

Parameters
wWeapon exploding.
dmgDamage it does.
radiusRadius of the explosion.
centerCenter of the explosion caused by the weapon (likely from collisions).

Definition at line 1573 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 2051 of file weapon.c.

◆ weapon_init()

void weapon_init ( void )

Initializes the weapon stuff.

Definition at line 144 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 226 of file weapon.c.

◆ weapon_miss()

void weapon_miss ( Weapon * w)
static

Weapon missed and is due to be destroyed.

Parameters
wWeapon that missed.

Definition at line 1827 of file weapon.c.

◆ weapon_newSystem()

void weapon_newSystem ( void )

Sets up collision stuff for a new system.

Definition at line 203 of file weapon.c.

◆ weapon_render()

void weapon_render ( Weapon * w,
double dt )
static

Renders an individual weapon.

Parameters
wWeapon to render.
dtCurrent delta tick.

Definition at line 833 of file weapon.c.

◆ weapon_renderBeam()

void weapon_renderBeam ( Weapon * w,
double dt )
static

Definition at line 775 of file weapon.c.

◆ weapon_sample_trail()

void weapon_sample_trail ( Weapon * w)
static

Updates the animated trail for a weapon.

Definition at line 1483 of file weapon.c.

◆ weapon_setAccel()

void weapon_setAccel ( Weapon * w,
double accel )
static

Sets the weapon's accel.

Definition at line 327 of file weapon.c.

◆ weapon_setTurn()

void weapon_setTurn ( Weapon * w,
double turn )
static

Sets the weapon's turn.

Definition at line 335 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 2715 of file weapon.c.

◆ weapon_testCollision()

int weapon_testCollision ( const WeaponCollision * wc,
const glTexture * ctex,
int csx,
int csy,
const Solid * csol,
const CollPolyView * 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 1012 of file weapon.c.

◆ weapon_update()

void weapon_update ( Weapon * w,
double dt )
static

Updates an individual weapon.

Parameters
wWeapon to update.
dtCurrent delta tick.

Definition at line 1436 of file weapon.c.

◆ weapon_updateCollide()

void weapon_updateCollide ( Weapon * w,
double dt )
static

Updates an individual weapon.

Parameters
wWeapon to update.
dtCurrent delta tick.

Definition at line 1187 of file weapon.c.

◆ weapon_updateVBO()

void weapon_updateVBO ( void )
static

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

Definition at line 162 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 762 of file weapon.c.

◆ weapons_update()

void weapons_update ( double dt)

Updates all the weapons.

Parameters
dtCurrent delta tick.

Definition at line 742 of file weapon.c.

◆ weapons_updateCollide()

void weapons_updateCollide ( double dt)

Handles weapon collisions.

Definition at line 658 of file weapon.c.

◆ weapons_updatePurge()

void weapons_updatePurge ( void )

Purges unnecessary weapons.

Definition at line 609 of file weapon.c.

Variable Documentation

◆ qt_init

int qt_init = 0
static

Whether or not the quadtree was created.

Definition at line 82 of file weapon.c.

◆ weapon_idgen

unsigned int weapon_idgen = 0
static

Weapon identifier generator.

Definition at line 81 of file weapon.c.

◆ weapon_qtexp

IntList weapon_qtexp
static

For querying collisions from explosions.

Definition at line 85 of file weapon.c.

◆ weapon_qtquery

IntList weapon_qtquery
static

For querying collisions.

Definition at line 84 of file weapon.c.

◆ weapon_quadtree

Quadtree weapon_quadtree
static

Quadtree for weapons.

Definition at line 83 of file weapon.c.

◆ weapon_stack

Weapon* weapon_stack
static
Initial value:
=
NULL

All the weapon munitions are piled up here.

Definition at line 72 of file weapon.c.

◆ weapon_vbo

gl_vbo* weapon_vbo = NULL
static

Weapon VBO.

Definition at line 76 of file weapon.c.

◆ weapon_vboData

GLfloat* weapon_vboData = NULL
static

Data of weapon VBO.

Definition at line 77 of file weapon.c.

◆ weapon_vboSize

size_t weapon_vboSize = 0
static

Size of the VBO.

Definition at line 78 of file weapon.c.