From f7c10bf329937f2fd9da71ea2694f4bc660ad351 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Tue, 9 Jul 2013 18:53:55 +0100
Subject: [PATCH] [Add] Pilots now keep track of how many weapons are locked
 onto them.

---
 src/outfit.c |  9 +++++++++
 src/outfit.h |  1 +
 src/pilot.h  |  3 ++-
 src/weapon.c | 14 ++++++++++++++
 4 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/src/outfit.c b/src/outfit.c
index 993c16d..d9aabe7 100644
--- a/src/outfit.c
+++ b/src/outfit.c
@@ -235,6 +235,15 @@ double outfit_range(const Outfit* o) {
   return -1.;
 }
 
+int outfit_isSeeker(const Outfit* o) {
+  if((o->type == OUTFIT_TYPE_MISSILE_SEEK_AMMO) ||
+      (o->type == OUTFIT_TYPE_MISSILE_SEEK_SMART_AMMO) ||
+      (o->type == OUTFIT_TYPE_MISSILE_SWARM_AMMO) ||
+      (o->type == OUTFIT_TYPE_MISSILE_SWARM_SMART_AMMO))
+    return 1;
+  return 0;
+}
+
 const char* outfit_typename[] = {
   "NULL",
   "Bolt Cannon",
diff --git a/src/outfit.h b/src/outfit.h
index 426420f..d85a6a5 100644
--- a/src/outfit.h
+++ b/src/outfit.h
@@ -146,6 +146,7 @@ DamageType outfit_damageType(const Outfit* o);
 int outfit_delay(const Outfit* o);
 double outfit_energy(const Outfit* o);
 double outfit_range(const Outfit* o);
+int outfit_isSeeker(const Outfit* o);
 
 /* Load/free outfit stack. */
 int outfit_load(void);
diff --git a/src/pilot.h b/src/pilot.h
index 52cb153..60d4a56 100644
--- a/src/pilot.h
+++ b/src/pilot.h
@@ -103,8 +103,9 @@ typedef struct Pilot_ {
   int cargo_free;
 
   /* Misc. */
-  uint32_t flags; /* Used for AI etc. */
+  uint32_t flags;       /* Used for AI etc. */
   unsigned int ptimer;  /* Generic timer for internal pilot use. */
+  int lockons;          /* Stores how many seeking weapons are targetting pilot. */
 
   /* Hook attached to the pilot. */
   int hook_type;
diff --git a/src/weapon.c b/src/weapon.c
index 1f05c3c..6463a04 100644
--- a/src/weapon.c
+++ b/src/weapon.c
@@ -379,6 +379,11 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2*
                               w->solid->pos.x, w->solid->pos.y,
                               w->solid->vel.x, w->solid->vel.y, w->outfit->u.amm.sound, 0);
 
+    /* If they are seeking a pilot, increment lockon counter. */
+    pilot_target = pilot_get(target);
+    if(pilot_target != NULL)
+      pilot_target->lockons++;
+
     /* Only diff is AI. */
     if(outfit->type == OUTFIT_TYPE_MISSILE_SEEK_AMMO)
       w->think = think_seeker;
@@ -449,6 +454,15 @@ static void weapon_destroy(Weapon* w, WeaponLayer layer) {
   int i;
   Weapon** wlayer;
   int* nlayer;
+  Pilot* pilot_target;
+
+  /* Decrement target lockons if needed. */
+  if(outfit_isSeeker(w->outfit)) {
+    pilot_target = pilot_get(w->target);
+    if(pilot_target != NULL)
+      pilot_target->lockons--;
+  }
+
   switch(layer) {
   case WEAPON_LAYER_BG:
     wlayer = wbackLayer;