From 90707480d97bb79432b55f905b445f1f90627927 Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 5 Jul 2013 15:33:07 +0100 Subject: [PATCH] [Change] Swithced weapon timer methods to something a little more sane. --- src/outfit.c | 4 +-- src/outfit.h | 4 +-- src/pause.c | 3 -- src/weapon.c | 83 +++++++++++++++++++--------------------------------- src/weapon.h | 5 ---- 5 files changed, 34 insertions(+), 65 deletions(-) diff --git a/src/outfit.c b/src/outfit.c index 76f5f66..ac464fd 100644 --- a/src/outfit.c +++ b/src/outfit.c @@ -397,9 +397,9 @@ static void outfit_parseSAmmo(Outfit* tmp, const xmlNodePtr parent) { xmlr_float(node, "speed", tmp->u.amm.speed); xmlr_float(node, "energy", tmp->u.amm.energy); 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")) - tmp->u.amm.lockon = (unsigned int)1000.*xml_getFloat(node); + tmp->u.amm.lockon = xml_getFloat(node); else if(xml_isNode(node, "gfx")) { snprintf(str, strlen(xml_get(node))+sizeof(OUTFIT_GFX)+10, OUTFIT_GFX"space/%s.png", xml_get(node)); diff --git a/src/outfit.h b/src/outfit.h index f76ad15..1c9232b 100644 --- a/src/outfit.h +++ b/src/outfit.h @@ -81,7 +81,8 @@ typedef struct Outfit_ { char* ammo; } lau; struct { /* Ammo. */ - unsigned int duration; /* Duration. */ + double duration; /* Duration. */ + double lockon; /* Time it takes to lock on the target. */ double speed; /* Max speed. */ double turn; /* Turn vel. */ double thrust; /* Acceleration. */ @@ -92,7 +93,6 @@ typedef struct Outfit_ { glTexture* gfx_space; ALuint sound; /* Sound to play. */ int spfx; /* Special effect on hit. */ - unsigned int lockon; /* Time taken to lock on the target. */ } amm; struct { /* Modification. */ /* Movement. */ diff --git a/src/pause.c b/src/pause.c index 58c4259..b3e3f2c 100644 --- a/src/pause.c +++ b/src/pause.c @@ -25,7 +25,6 @@ void pause_game(void) { if(paused) return; /* Well well.. We are paused already. */ pilot_nstack_pause(); - weapons_pause(); spfx_pause(); spawn_timer -= SDL_GetTicks(); @@ -36,7 +35,6 @@ void unpause_game(void) { if(!paused) return; /* We are unpaused already. */ pilot_nstack_unpause(); - weapons_unpause(); spfx_unpause(); spawn_timer += SDL_GetTicks(); @@ -46,7 +44,6 @@ void unpause_game(void) { /* Set the timers back. */ void pause_delay(unsigned int delay) { pilot_nstack_delay(delay); - weapons_delay(delay); spfx_delay(delay); } diff --git a/src/weapon.c b/src/weapon.c index 308bbe5..b9b1bd5 100644 --- a/src/weapon.c +++ b/src/weapon.c @@ -34,19 +34,18 @@ extern void ai_attacked(Pilot* attacked, const unsigned int attacker); typedef struct Weapon_ { Solid* solid; /* Actually has its own solid. :D */ - unsigned int parent; /* The pilot that just shot at you! */ - unsigned int target; /* Target to hit. Only used by seeking stuff. */ + unsigned int parent; /* The pilot that just shot at you! */ + unsigned int target; /* Target to hit. Only used by seeking stuff. */ 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. */ /* Update position 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 */ - - double pid_last; - double pid_int; } Weapon; /* Behind Pilot layer. */ @@ -101,34 +100,6 @@ void weapon_minimap(const double res, const double w, } #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. :) */ static void think_seeker(Weapon* w, const double dt) { double diff; @@ -141,7 +112,7 @@ static void think_seeker(Weapon* w, const double dt) { } /* 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)); w->solid->dir_vel = 10 * diff * w->outfit->u.amm.turn; /* Face the target. */ @@ -172,7 +143,8 @@ static void think_smart(Weapon* w, const double dt) { 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), 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_SWARM_AMMO: case OUTFIT_TYPE_MISSILE_SWARM_SMART_AMMO: - if(SDL_GetTicks() > - (wlayer[i]->timer + wlayer[i]->outfit->u.amm.duration)) { - weapon_destroy(wlayer[i], layer); - continue; - } - break; + if(wlayer[i]->lockon > 0.) /* Decrement lockon. */ + wlayer[i]->lockon -= dt; + /* Purpose fallthrough. */ + + /* Bolts too. */ case OUTFIT_TYPE_BOLT: - /* Check see if it exceeds distance. */ 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); 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. */ + /* Smart weapons only collide with their target. */ if((weapon_isSmart(w)) && (pilot_stack[i]->id == w->target) && CollideSprite(gfx, wsx, wsy, &w->solid->pos, pilot_stack[i]->ship->gfx_space, psx, psy, &pilot_stack[i]->solid->pos)) { weapon_hit(w, pilot_stack[i], layer); return; } + /* Dumb weapons hit anything not of the same faction. */ else if(!weapon_isSmart(w) && !areAllies(pilot_get(w->parent)->faction, pilot_stack[i]->faction) && 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); (*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->outfit = outfit; /* Non-Changeable. */ w->update = weapon_update; - w->timer = SDL_GetTicks(); w->think = NULL; 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); vectcpy(&v, vel); 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->voice = sound_addVoice(VOICE_PRIORITY_BOLT, w->solid->pos.x, w->solid->pos.y, @@ -377,17 +350,21 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2* break; case OUTFIT_TYPE_MISSILE_SEEK_AMMO: mass = w->outfit->mass; - w->solid = solid_create(mass, dir, pos, vel); - w->think = think_seeker; /* Eeek!!! */ - w->voice = sound_addVoice(VOICE_PRIORITY_AMMO, + 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: mass = w->outfit->mass; - w->solid = solid_create(mass, dir, pos, vel); - w->think = think_smart; /* Smartass. */ - w->voice = sound_addVoice(VOICE_PRIORITY_AMMO, + w->lockon = outfit->u.amm.lockon; + w->timer = outfit->u.amm.duration; + w->solid = solid_create(mass, dir, pos, vel); + w->think = think_smart; /* Smartass. */ + 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; @@ -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); vectcpy(&v, vel); 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->voice = sound_addVoice(VOICE_PRIORITY_BOLT, w->solid->pos.x, w->solid->pos.y, diff --git a/src/weapon.h b/src/weapon.h index 93456f4..4da5364 100644 --- a/src/weapon.h +++ b/src/weapon.h @@ -8,11 +8,6 @@ void weapon_add(const Outfit* outfit, const double dir, const Vec2* pos, const Vec2* vel, unsigned int parent, const unsigned int target); -/* Pausing. */ -void weapons_pause(void); -void weapons_unpause(void); -void weapons_delay(unsigned int delay); - /* Update. */ void weapons_update(const double dt); void weapons_render(const WeaponLayer layer);