diff --git a/src/pilot.c b/src/pilot.c index 4ff118e..e1e91d8 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -33,6 +33,7 @@ extern void ai_think(Pilot* pilot); // Ai.c extern void player_think(Pilot* pilot); // Player.c extern int gui_load(const char* name); // Player.c // Internal. +static void pilot_shootWeapon(Pilot* p, PilotOutfit* w, const unsigned int t); static void pilot_update(Pilot* pilot, const double dt); void pilot_render(Pilot* pilot); static void pilot_free(Pilot* p); @@ -108,37 +109,50 @@ Pilot* pilot_get(const unsigned int id) { // Mkay, this is how we shoot. Listen up. void pilot_shoot(Pilot* p, const unsigned int target, const int secondary) { int i; + + if(!p->outfits) return; // No outfits. + if(!secondary) { // Primary weapons. - if(!p->outfits) return; // No outfits. - - for(i = 0; i < p->noutfits; i++) // Cycle through outfits to find weapons. - if(outfit_isWeapon(p->outfits[i].outfit)) // Are we a weapon? - // Ready to shoot again. - if((SDL_GetTicks()-p->outfits[i].timer) > (p->outfits[i].outfit->delay/p->outfits[i].quantity)) - // Different weapons have different behaviours. - switch(p->outfits[i].outfit->type) { - case OUTFIT_TYPE_BOLT: - weapon_add(p->outfits[i].outfit, p->solid->dir, &p->solid->pos, - &p->solid->vel, p->id, target, (p==player) ? WEAPON_LAYER_FG : WEAPON_LAYER_BG); - p->outfits[i].timer = SDL_GetTicks(); // Let's not try this again for a while. - break; - default: - break; - } + for(i = 0; i < p->noutfits; i++) + // Cycle through outfits to find primary weapons. + if(!outfit_isProp(p->outfits[i].outfit, OUTFIT_PROP_WEAP_SECONDARY)) + pilot_shootWeapon(p, &p->outfits[i], target); } else { if(!p->secondary) return; // No secondary weapon. + pilot_shootWeapon(p, p->secondary, target); + } +} - if(outfit_isLauncher(p->secondary->outfit)) { - if(((SDL_GetTicks()-p->secondary->timer) > - (p->secondary->outfit->delay/p->secondary->quantity)) && - p->ammo && (p->ammo->quantity > 0)) { - weapon_add(p->ammo->outfit, p->solid->dir, &p->solid->pos, &p->solid->vel, - p->id, target, (p==player) ? WEAPON_LAYER_FG : WEAPON_LAYER_BG); +static void pilot_shootWeapon(Pilot* p, PilotOutfit* w, const unsigned int t) { + // WElll... Trying to shoot when you have no ammo?? FUUU + int quantity = (outfit_isAmmo(w->outfit) && p->secondary) ? + p->secondary->quantity : w->quantity; + // Check to see if weapon is ready. + if((SDL_GetTicks() - w->timer) < (w->outfit->delay / quantity)) return; - p->secondary->timer = SDL_GetTicks(); // Let's not try this again for a while. - p->ammo->quantity -= 1; // No getting this one back. - } + // Regular weapons. + if(outfit_isWeapon(w->outfit)) { + // Different weapons. + switch(w->outfit->type) { + case OUTFIT_TYPE_BOLT: + weapon_add(w->outfit, p->solid->dir, &p->solid->pos, &p->solid->vel, p->id, t); + + // Can't shoot for a while. + w->timer = SDL_GetTicks(); + break; + default: + break; + } + } + // Missile launchers. + // Must be secondary weapon, Shootee can't be the target. + else if(outfit_isLauncher(w->outfit) && (w == p->secondary) && (p->id != t)) { + if(p->ammo && (p->ammo->quantity > 0)) { + weapon_add(p->ammo->outfit, p->solid->dir, &p->solid->pos, &p->solid->vel, p->id, t); + + w->timer = SDL_GetTicks(); // Can't shoot for a while. + p->ammo->quantity -= 1; // There's no getting this one back. } } } diff --git a/src/player.c b/src/player.c index 6281a93..63a6db1 100644 --- a/src/player.c +++ b/src/player.c @@ -767,7 +767,7 @@ void player_think(Pilot* player) { player->solid->dir_vel -= player->ship->turn * player_turn; if(player_isFlag(PLAYER_PRIMARY)) pilot_shoot(player, 0, 0); - if(player_isFlag(PLAYER_SECONDARY) && (player_target != PLAYER_ID)) // Needs a target. + if(player_isFlag(PLAYER_SECONDARY)) // Needs a target. pilot_shoot(player, player_target, 1); vect_pset(&player->solid->force, player->ship->thrust * player_acc, player->solid->dir); diff --git a/src/weapon.c b/src/weapon.c index f96c681..d975393 100644 --- a/src/weapon.c +++ b/src/weapon.c @@ -276,13 +276,14 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2* // Add a new weapon. void weapon_add(const Outfit* outfit, const double dir, const Vec2* pos, const Vec2* vel, - unsigned int parent, unsigned int target, const WeaponLayer layer) { + unsigned int parent, unsigned int target) { if(!outfit_isWeapon(outfit) && !outfit_isAmmo(outfit)) { ERR("Trying to create a weapon from a non-Weapon type Outfit"); return; } + WeaponLayer layer = (parent == PLAYER_ID) ? WEAPON_LAYER_FG : WEAPON_LAYER_BG; Weapon* w = weapon_create(outfit, dir, pos, vel, parent, target); // Set the propper layer. diff --git a/src/weapon.h b/src/weapon.h index 9356d79..9ee5b82 100644 --- a/src/weapon.h +++ b/src/weapon.h @@ -4,9 +4,8 @@ typedef enum { WEAPON_LAYER_BG, WEAPON_LAYER_FG } WeaponLayer; -void weapon_add(const Outfit* outfit, const double dir, - const Vec2* pos, const Vec2* vel, unsigned int parent, - const unsigned int target, const WeaponLayer layer); +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);