[Change] Swithced weapon timer methods to something a little more sane.

This commit is contained in:
Allanis 2013-07-05 15:33:07 +01:00
parent 8442fd00b4
commit 90707480d9
5 changed files with 34 additions and 65 deletions

View File

@ -397,9 +397,9 @@ static void outfit_parseSAmmo(Outfit* tmp, const xmlNodePtr parent) {
xmlr_float(node, "speed", tmp->u.amm.speed); xmlr_float(node, "speed", tmp->u.amm.speed);
xmlr_float(node, "energy", tmp->u.amm.energy); xmlr_float(node, "energy", tmp->u.amm.energy);
if(xml_isNode(node, "duration")) if(xml_isNode(node, "duration"))
tmp->u.amm.duration = (unsigned int)1000.*xml_getFloat(node); tmp->u.amm.duration = xml_getFloat(node);
else if(xml_isNode(node, "lockon")) else if(xml_isNode(node, "lockon"))
tmp->u.amm.lockon = (unsigned int)1000.*xml_getFloat(node); tmp->u.amm.lockon = xml_getFloat(node);
else if(xml_isNode(node, "gfx")) { else if(xml_isNode(node, "gfx")) {
snprintf(str, strlen(xml_get(node))+sizeof(OUTFIT_GFX)+10, snprintf(str, strlen(xml_get(node))+sizeof(OUTFIT_GFX)+10,
OUTFIT_GFX"space/%s.png", xml_get(node)); OUTFIT_GFX"space/%s.png", xml_get(node));

View File

@ -81,7 +81,8 @@ typedef struct Outfit_ {
char* ammo; char* ammo;
} lau; } lau;
struct { /* Ammo. */ struct { /* Ammo. */
unsigned int duration; /* Duration. */ double duration; /* Duration. */
double lockon; /* Time it takes to lock on the target. */
double speed; /* Max speed. */ double speed; /* Max speed. */
double turn; /* Turn vel. */ double turn; /* Turn vel. */
double thrust; /* Acceleration. */ double thrust; /* Acceleration. */
@ -92,7 +93,6 @@ typedef struct Outfit_ {
glTexture* gfx_space; glTexture* gfx_space;
ALuint sound; /* Sound to play. */ ALuint sound; /* Sound to play. */
int spfx; /* Special effect on hit. */ int spfx; /* Special effect on hit. */
unsigned int lockon; /* Time taken to lock on the target. */
} amm; } amm;
struct { /* Modification. */ struct { /* Modification. */
/* Movement. */ /* Movement. */

View File

@ -25,7 +25,6 @@ void pause_game(void) {
if(paused) return; /* Well well.. We are paused already. */ if(paused) return; /* Well well.. We are paused already. */
pilot_nstack_pause(); pilot_nstack_pause();
weapons_pause();
spfx_pause(); spfx_pause();
spawn_timer -= SDL_GetTicks(); spawn_timer -= SDL_GetTicks();
@ -36,7 +35,6 @@ void unpause_game(void) {
if(!paused) return; /* We are unpaused already. */ if(!paused) return; /* We are unpaused already. */
pilot_nstack_unpause(); pilot_nstack_unpause();
weapons_unpause();
spfx_unpause(); spfx_unpause();
spawn_timer += SDL_GetTicks(); spawn_timer += SDL_GetTicks();
@ -46,7 +44,6 @@ void unpause_game(void) {
/* Set the timers back. */ /* Set the timers back. */
void pause_delay(unsigned int delay) { void pause_delay(unsigned int delay) {
pilot_nstack_delay(delay); pilot_nstack_delay(delay);
weapons_delay(delay);
spfx_delay(delay); spfx_delay(delay);
} }

View File

@ -37,16 +37,15 @@ typedef struct Weapon_ {
unsigned int parent; /* The pilot that just shot at you! */ unsigned int parent; /* The pilot that just shot at you! */
unsigned int target; /* Target to hit. Only used by seeking stuff. */ unsigned int target; /* Target to hit. Only used by seeking stuff. */
const Outfit* outfit; /* Related outfit that fired. */ const Outfit* outfit; /* Related outfit that fired. */
unsigned int timer; /* Mainly used to see when the weapon was fired. */
double lockon; /* Some weapons have a lockon delay. */
double timer; /* Mainly used to see when the weapon was fired. */
alVoice* voice; /* Virtual voise. */ alVoice* voice; /* Virtual voise. */
/* Update position and render. */ /* Update position and render. */
void(*update)(struct Weapon_*, const double, WeaponLayer); /* Position update and render. */ void(*update)(struct Weapon_*, const double, WeaponLayer); /* Position update and render. */
void(*think)(struct Weapon_*, const double); /* Some missiles need to be inteligent.a */ void(*think)(struct Weapon_*, const double); /* Some missiles need to be inteligent.a */
double pid_last;
double pid_int;
} Weapon; } Weapon;
/* Behind Pilot layer. */ /* Behind Pilot layer. */
@ -101,34 +100,6 @@ void weapon_minimap(const double res, const double w,
} }
#undef PIXEL #undef PIXEL
/* Pause/Unpause the weapon system. */
void weapons_pause(void) {
int i;
unsigned int t = SDL_GetTicks();
for(i = 0; i < nwbackLayer; i++)
wbackLayer[i]->timer -= t;
for(i = 0; i < nwfrontLayer; i++)
wfrontLayer[i]->timer -= t;
}
void weapons_unpause(void) {
int i;
unsigned int t = SDL_GetTicks();
for(i = 0; i < nwbackLayer; i++)
wbackLayer[i]->timer += t;
for(i = 0; i < nwfrontLayer; i++)
wfrontLayer[i]->timer += t;
}
void weapons_delay(unsigned int delay) {
int i;
for(i = 0; i < nwbackLayer; i++)
wbackLayer[i]->timer += delay;
for(i = 0; i < nwfrontLayer; i++)
wfrontLayer[i]->timer += delay;
}
/* Seeker brain, You get what you pay for. :) */ /* Seeker brain, You get what you pay for. :) */
static void think_seeker(Weapon* w, const double dt) { static void think_seeker(Weapon* w, const double dt) {
double diff; double diff;
@ -141,7 +112,7 @@ static void think_seeker(Weapon* w, const double dt) {
} }
/* Ammo isn't locked on yet.. */ /* Ammo isn't locked on yet.. */
if(SDL_GetTicks() > (w->timer + w->outfit->u.amm.lockon)) { if(SDL_GetTicks() > (w->outfit->u.amm.lockon)) {
diff = angle_diff(w->solid->dir, vect_angle(&w->solid->pos, &p->solid->pos)); diff = angle_diff(w->solid->dir, vect_angle(&w->solid->pos, &p->solid->pos));
w->solid->dir_vel = 10 * diff * w->outfit->u.amm.turn; w->solid->dir_vel = 10 * diff * w->outfit->u.amm.turn;
/* Face the target. */ /* Face the target. */
@ -172,7 +143,8 @@ static void think_smart(Weapon* w, const double dt) {
return; return;
} }
if(SDL_GetTicks() > (w->timer + w->outfit->u.amm.lockon)) { /* Ammo isn't locked on yet. */
if(w->lockon < 0.) {
vect_cset(&tv, VX(p->solid->pos) + dt*VX(p->solid->vel), vect_cset(&tv, VX(p->solid->pos) + dt*VX(p->solid->vel),
VY(p->solid->pos) + dt*VY(p->solid->vel)); VY(p->solid->pos) + dt*VY(p->solid->vel));
@ -226,16 +198,15 @@ static void weapons_updateLayer(const double dt, const WeaponLayer layer) {
case OUTFIT_TYPE_MISSILE_SEEK_SMART_AMMO: case OUTFIT_TYPE_MISSILE_SEEK_SMART_AMMO:
case OUTFIT_TYPE_MISSILE_SWARM_AMMO: case OUTFIT_TYPE_MISSILE_SWARM_AMMO:
case OUTFIT_TYPE_MISSILE_SWARM_SMART_AMMO: case OUTFIT_TYPE_MISSILE_SWARM_SMART_AMMO:
if(SDL_GetTicks() > if(wlayer[i]->lockon > 0.) /* Decrement lockon. */
(wlayer[i]->timer + wlayer[i]->outfit->u.amm.duration)) { wlayer[i]->lockon -= dt;
weapon_destroy(wlayer[i], layer); /* Purpose fallthrough. */
continue;
} /* Bolts too. */
break;
case OUTFIT_TYPE_BOLT: case OUTFIT_TYPE_BOLT:
/* Check see if it exceeds distance. */
case OUTFIT_TYPE_TURRET_BOLT: case OUTFIT_TYPE_TURRET_BOLT:
if(SDL_GetTicks() > wlayer[i]->timer) { wlayer[i]->timer -= dt;
if(wlayer[i]->timer < 0.) {
weapon_destroy(wlayer[i],layer); weapon_destroy(wlayer[i],layer);
continue; continue;
} }
@ -297,12 +268,14 @@ static void weapon_update(Weapon* w, const double dt, WeaponLayer layer) {
if(w->parent == pilot_stack[i]->id) continue; /* Hey! That's you. */ if(w->parent == pilot_stack[i]->id) continue; /* Hey! That's you. */
/* Smart weapons only collide with their target. */
if((weapon_isSmart(w)) && (pilot_stack[i]->id == w->target) && if((weapon_isSmart(w)) && (pilot_stack[i]->id == w->target) &&
CollideSprite(gfx, wsx, wsy, &w->solid->pos, CollideSprite(gfx, wsx, wsy, &w->solid->pos,
pilot_stack[i]->ship->gfx_space, psx, psy, &pilot_stack[i]->solid->pos)) { pilot_stack[i]->ship->gfx_space, psx, psy, &pilot_stack[i]->solid->pos)) {
weapon_hit(w, pilot_stack[i], layer); weapon_hit(w, pilot_stack[i], layer);
return; return;
} }
/* Dumb weapons hit anything not of the same faction. */
else if(!weapon_isSmart(w) && else if(!weapon_isSmart(w) &&
!areAllies(pilot_get(w->parent)->faction, pilot_stack[i]->faction) && !areAllies(pilot_get(w->parent)->faction, pilot_stack[i]->faction) &&
CollideSprite(gfx, wsx, wsy, &w->solid->pos, CollideSprite(gfx, wsx, wsy, &w->solid->pos,
@ -312,6 +285,7 @@ static void weapon_update(Weapon* w, const double dt, WeaponLayer layer) {
} }
} }
/* Smart weapons also get to think their next move. */
if(weapon_isSmart(w)) (*w->think)(w,dt); if(weapon_isSmart(w)) (*w->think)(w,dt);
(*w->solid->update)(w->solid, dt); (*w->solid->update)(w->solid, dt);
@ -359,7 +333,6 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2*
w->target = target; /* Non-Changeable. */ w->target = target; /* Non-Changeable. */
w->outfit = outfit; /* Non-Changeable. */ w->outfit = outfit; /* Non-Changeable. */
w->update = weapon_update; w->update = weapon_update;
w->timer = SDL_GetTicks();
w->think = NULL; w->think = NULL;
switch(outfit->type) { switch(outfit->type) {
@ -369,7 +342,7 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2*
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);
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 += 1000*(unsigned int)outfit->u.blt.range/outfit->u.blt.speed; w->timer += outfit->u.blt.range/outfit->u.blt.speed;
w->solid = solid_create(mass, rdir, pos, &v); w->solid = solid_create(mass, rdir, pos, &v);
w->voice = sound_addVoice(VOICE_PRIORITY_BOLT, w->voice = sound_addVoice(VOICE_PRIORITY_BOLT,
w->solid->pos.x, w->solid->pos.y, w->solid->pos.x, w->solid->pos.y,
@ -377,6 +350,8 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2*
break; break;
case OUTFIT_TYPE_MISSILE_SEEK_AMMO: case OUTFIT_TYPE_MISSILE_SEEK_AMMO:
mass = w->outfit->mass; 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->solid = solid_create(mass, dir, pos, vel);
w->think = think_seeker; /* Eeek!!! */ w->think = think_seeker; /* Eeek!!! */
w->voice = sound_addVoice(VOICE_PRIORITY_AMMO, w->voice = sound_addVoice(VOICE_PRIORITY_AMMO,
@ -385,6 +360,8 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2*
break; 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->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->think = think_smart; /* Smartass. */
w->voice = sound_addVoice(VOICE_PRIORITY_AMMO, w->voice = sound_addVoice(VOICE_PRIORITY_AMMO,
@ -399,7 +376,7 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2*
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);
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 += 1000*(unsigned int)outfit->u.blt.range / outfit->u.blt.speed; w->timer += outfit->u.blt.range / outfit->u.blt.speed;
w->solid = solid_create(mass, rdir, pos, &v); w->solid = solid_create(mass, rdir, pos, &v);
w->voice = sound_addVoice(VOICE_PRIORITY_BOLT, w->voice = sound_addVoice(VOICE_PRIORITY_BOLT,
w->solid->pos.x, w->solid->pos.y, w->solid->pos.x, w->solid->pos.y,

View File

@ -8,11 +8,6 @@ void weapon_add(const Outfit* outfit, const double dir, const Vec2* pos,
const Vec2* vel, unsigned int parent, const Vec2* vel, unsigned int parent,
const unsigned int target); const unsigned int target);
/* Pausing. */
void weapons_pause(void);
void weapons_unpause(void);
void weapons_delay(unsigned int delay);
/* Update. */ /* Update. */
void weapons_update(const double dt); void weapons_update(const double dt);
void weapons_render(const WeaponLayer layer); void weapons_render(const WeaponLayer layer);