[Change] Merged together some semi-redundant code in weapon_create.

This commit is contained in:
Allanis 2013-07-09 17:17:53 +01:00
parent dcf093b13e
commit 1f8e6a22fe

View File

@ -330,9 +330,12 @@ static void weapon_hit(Weapon* w, Pilot* p, WeaponLayer layer) {
static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2* pos, static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2* pos,
const Vec2* vel, unsigned int parent, const unsigned int target) { const Vec2* vel, unsigned int parent, const unsigned int target) {
Vec2 v; Vec2 v;
double mass = 1; /* Presumer lasers have a mass of 1. */ double mass, rdir;
double rdir = dir; /* Real direction (accuracy). */ Pilot* pilot_target;
Weapon* w = MALLOC_L(Weapon); Weapon* w;
/* Create basic features. */
w = MALLOC_L(Weapon);
w->faction = pilot_get(parent)->faction; /*Non-Changeable. */ w->faction = pilot_get(parent)->faction; /*Non-Changeable. */
w->parent = parent; /* Non-Changeable. */ w->parent = parent; /* Non-Changeable. */
w->target = target; /* Non-Changeable. */ w->target = target; /* Non-Changeable. */
@ -341,10 +344,21 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2*
w->think = NULL; w->think = NULL;
switch(outfit->type) { switch(outfit->type) {
/* Bolts treated together. */
case OUTFIT_TYPE_BOLT: case OUTFIT_TYPE_BOLT:
/* Need accuracy and speed based on player. -- Another contribution from VLack. */ case OUTFIT_TYPE_TURRET_BOLT:
rdir += RNG(-outfit->u.blt.accuracy/2., outfit->u.blt.accuracy/2.)/180.*M_PI; /* Only difference is the direction of fire. */
if((outfit->type == OUTFIT_TYPE_TURRET_BOLT) && (w->parent != w->target)) {
rdir = vect_angle(pos, &pilot_get(w->target)->solid->pos);
rdir += RNG(-outfit->u.blt.accuracy/2.,
outfit->u.blt.accuracy/2.)/180.*M_PI;
} else /* Fire straight. */
rdir = dir + RNG(-outfit->u.blt.accuracy/2.,
outfit->u.blt.accuracy/2.)/180.*M_PI;
if((rdir > 2.*M_PI) || (rdir < 0.)) rdir = fmod(rdir, 2.*M_PI); if((rdir > 2.*M_PI) || (rdir < 0.)) rdir = fmod(rdir, 2.*M_PI);
mass = 1; /* Lasers are presumed to have unitory mass. */
vectcpy(&v, vel); vectcpy(&v, vel);
vect_cadd(&v, outfit->u.blt.speed*cos(rdir), outfit->u.blt.speed*sin(rdir)); vect_cadd(&v, outfit->u.blt.speed*cos(rdir), outfit->u.blt.speed*sin(rdir));
w->timer += outfit->u.blt.range/outfit->u.blt.speed; w->timer += outfit->u.blt.range/outfit->u.blt.speed;
@ -353,42 +367,27 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2*
w->solid->pos.x, w->solid->pos.y, w->solid->pos.x, w->solid->pos.y,
w->solid->vel.x, w->solid->vel.y, w->outfit->u.blt.sound, 0); w->solid->vel.x, w->solid->vel.y, w->outfit->u.blt.sound, 0);
break; break;
/* Treat seekers togther. */
case OUTFIT_TYPE_MISSILE_SEEK_AMMO: case OUTFIT_TYPE_MISSILE_SEEK_AMMO:
mass = w->outfit->mass;
w->lockon = outfit->u.amm.lockon;
w->timer = outfit->u.amm.duration;
w->solid = solid_create(mass, dir, pos, vel);
w->think = think_seeker; /* Eeek!!! */
w->voice = sound_addVoice(VOICE_PRIORITY_AMMO,
w->solid->pos.x, w->solid->pos.y,
w->solid->vel.x, w->solid->vel.y, w->outfit->u.amm.sound, 0);
break;
case OUTFIT_TYPE_MISSILE_SEEK_SMART_AMMO: case OUTFIT_TYPE_MISSILE_SEEK_SMART_AMMO:
mass = w->outfit->mass; mass = w->outfit->mass;
w->lockon = outfit->u.amm.lockon; w->lockon = outfit->u.amm.lockon;
w->timer = outfit->u.amm.duration; w->timer = outfit->u.amm.duration;
w->solid = solid_create(mass, dir, pos, vel); w->solid = solid_create(mass, dir, pos, vel);
w->think = think_smart; /* Smartass. */
w->voice = sound_addVoice(VOICE_PRIORITY_AMMO, w->voice = sound_addVoice(VOICE_PRIORITY_AMMO,
w->solid->pos.x, w->solid->pos.y, w->solid->pos.x, w->solid->pos.y,
w->solid->vel.x, w->solid->vel.y, w->outfit->u.amm.sound, 0); w->solid->vel.x, w->solid->vel.y, w->outfit->u.amm.sound, 0);
/* Only diff is AI. */
if(outfit->type == OUTFIT_TYPE_MISSILE_SEEK_AMMO)
w->think = think_seeker;
else if(outfit->type == OUTFIT_TYPE_MISSILE_SEEK_SMART_AMMO)
w->think = think_smart;
break; break;
case OUTFIT_TYPE_TURRET_BOLT:
if(w->parent != w->target)
rdir = vect_angle(pos, &pilot_get(w->target)->solid->pos);
rdir += RNG(-outfit->u.blt.accuracy/2.,
outfit->u.blt.accuracy/2.)/180.*M_PI;
if((rdir > 2.*M_PI) || (rdir < 0.)) rdir = fmod(rdir, 2.*M_PI);
vectcpy(&v, vel);
vect_cadd(&v, outfit->u.blt.speed*cos(rdir), outfit->u.blt.speed*sin(rdir));
w->timer = outfit->u.blt.range / outfit->u.blt.speed;
w->solid = solid_create(mass, rdir, pos, &v);
w->voice = sound_addVoice(VOICE_PRIORITY_BOLT,
w->solid->pos.x, w->solid->pos.y,
w->solid->vel.x, w->solid->vel.y, w->outfit->u.blt.sound, 0);
break;
default:
/* Just dump it where the player is. */ /* Just dump it where the player is. */
default:
w->voice = NULL; w->voice = NULL;
w->solid = solid_create(mass, dir, pos, vel); w->solid = solid_create(mass, dir, pos, vel);
break; break;