From 27780b01bacb7c5a5f52655a0705f48406da9937 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sat, 31 May 2014 19:07:00 +0100
Subject: [PATCH] [Add] Have player keep track of hostiles it has in the
 system.

---
 src/ai.c     |  2 +-
 src/pilot.c  | 24 ++++++++++++++++++++++++
 src/pilot.h  |  1 +
 src/player.c | 24 +++++++++++++-----------
 src/player.h |  1 +
 src/space.c  |  3 +++
 src/weapon.c |  4 ++--
 7 files changed, 45 insertions(+), 14 deletions(-)

diff --git a/src/ai.c b/src/ai.c
index 00aba6f..f919f7e 100644
--- a/src/ai.c
+++ b/src/ai.c
@@ -1519,7 +1519,7 @@ static int ai_hostile(lua_State* L) {
   LLUA_MIN_ARGS(1);
 
   if(lua_isnumber(L,1) && ((unsigned int)lua_tonumber(L,1) == PLAYER_ID))
-    pilot_setFlag(cur_pilot, PILOT_HOSTILE);
+    pilot_setHostile(cur_pilot);
 
   return 0;
 }
diff --git a/src/pilot.c b/src/pilot.c
index 3280350..26c5361 100644
--- a/src/pilot.c
+++ b/src/pilot.c
@@ -45,6 +45,7 @@ static int pilot_mstack = 0; /** Memory allocated for pilot_stack. */
 extern Pilot* player;
 extern double player_crating; /**< Players combat rating. */
 extern void player_abortAutonav(char* reason);
+extern int player_enemies;
 
 /* Stack of fleets. */
 static Fleet* fleet_stack = NULL; /** Fleet stack. */
@@ -216,6 +217,17 @@ double pilot_face(Pilot* p, const double dir) {
   return diff;
 }
 
+/**
+ * @brief Mark a pilot as hostile to player.
+ *    @param p Pilot to mark as hostile.
+ */
+void pilot_setHostile(Pilot* p) {
+  if(!pilot_isFlag(p, PILOT_HOSTILE)) {
+    player_enemies++;
+    pilot_setFlag(p, PILOT_HOSTILE);
+  }
+}
+
 /* Get the amount of jumps the pilot has left. */
 int pilot_getJumps(const Pilot* p) {
   return (int)(p->fuel) / HYPERSPACE_FUEL;
@@ -820,6 +832,12 @@ static void pilot_update(Pilot* pilot, const double dt) {
       (pilot->armour < PILOT_DISABLED_ARMOUR*pilot->armour_max)) { /* Disabled. */
     /* First time pilot is disabled. */
     if(!pilot_isFlag(pilot, PILOT_DISABLED)) {
+      /* If hostile, must remove counter. */
+      if(pilot_isFlag(pilot, PILOT_HOSTILE)) {
+        player_enemies--;
+        pilot_rmFlag(pilot, PILOT_HOSTILE);
+      }
+      
       pilot_setFlag(pilot, PILOT_DISABLED); /* Set as disabled. */
       /* Run hook. */
       pilot_runHook(pilot, PILOT_HOOK_DISABLE);
@@ -1733,6 +1751,12 @@ void pilot_free(Pilot* p) {
     if(p->hook_type[i] != PILOT_HOOK_NONE)
       hook_rm(p->hook[i]);
 
+  /* If hostile, must remove counter. */
+  if(pilot_isFlag(p, PILOT_HOSTILE)) {
+    player_enemies--;
+    pilot_rmFlag(p, PILOT_HOSTILE);
+  }
+
   /* Remove outfits. */
   while(p->outfits != NULL)
     pilot_rmOutfit(p, p->outfits[0].outfit, p->outfits[0].quantity);
diff --git a/src/pilot.h b/src/pilot.h
index 25ab28a..7df7c30 100644
--- a/src/pilot.h
+++ b/src/pilot.h
@@ -235,6 +235,7 @@ Fleet* fleet_get(const char* name);
 int pilot_getJumps(const Pilot* p);
 
 /* Misc. */
+void pilot_setHostile(Pilot* p);
 void pilot_shoot(Pilot* p, const int secondary);
 void pilot_shootStop(Pilot* p, const int secondary);
 void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter,
diff --git a/src/player.c b/src/player.c
index aa52a10..3c38423 100644
--- a/src/player.c
+++ b/src/player.c
@@ -57,19 +57,21 @@
 #define INTERFERENCE_CHANGE_DT   0.1  /**< Speed to change at. */
 
 /* Player stuff. */
-Pilot* player = NULL;                   /**< The player. */
-static Ship* player_ship = NULL;        /**< Temp ship to hold when naming it. */
-static double player_px;  /**< Temp X position. */
-static double player_py;  /**< Temp Y position. */
-static double player_vx;  /**< Temp X velocity. */
-static double player_vy;  /**< Temp Y velocity. */
-static double player_dir; /**< Temp direction. */
-static int player_credits = 0;          /**< Temp hack on create. */
-static char* player_mission = NULL;     /**< More hack. */
+Pilot* player                 = NULL; /**< The player. */
+static Ship* player_ship      = NULL; /**< Temp ship to hold when naming it. */
+static double player_px       = 0.;   /**< Temp X position. */
+static double player_py       = 0.;   /**< Temp Y position. */
+static double player_vx       = 0.;   /**< Temp X velocity. */
+static double player_vy       = 0.;   /**< Temp Y velocity. */
+static double player_dir      = 0.;   /**< Temp direction. */
+static int player_credits     = 0;    /**< Temp hack on create. */
+static char* player_mission   = NULL; /**< More hack. */
+int player_enemies = 0;;              /**< Number of enemies player has in system. */
+
 
 /* Licenses. */
-static char** player_licenses = NULL; /**< Licenses player has. */
-static int player_nlicenses = 0;      /**< Number of licenses player has. */
+static char** player_licenses   = NULL; /**< Licenses player has. */
+static int player_nlicenses     = 0;    /**< Number of licenses player has. */
 
 /*
  * Player sounds.
diff --git a/src/player.h b/src/player.h
index 7e5999d..7cb1fe2 100644
--- a/src/player.h
+++ b/src/player.h
@@ -26,6 +26,7 @@ extern Pilot* player;               /**< The player. */
 extern char* player_name;           /**< Player's name. */
 extern unsigned int player_flags;   /**< Player's flags. */
 extern double player_crating;       /**< Player's combat rating. */
+extern int player_enemies;          /**< Amount of enemies player has. */
 
 /* Enums. */
 
diff --git a/src/space.c b/src/space.c
index d5d1cab..a31fbb2 100644
--- a/src/space.c
+++ b/src/space.c
@@ -628,6 +628,9 @@ void space_init(const char* sysname) {
       }
     }
   }
+  /* Reset player enemies. */
+  player_enemies = 0;
+
   /* Set up fleets -> pilots. */
   for(i = 0; i < cur_system->nfleets; i++)
     if(RNG(0,100) <= (cur_system->fleets[i].chance/2)) /* Fleet check (50% chance). */
diff --git a/src/weapon.c b/src/weapon.c
index a9bcb6b..96d7143 100644
--- a/src/weapon.c
+++ b/src/weapon.c
@@ -637,7 +637,7 @@ static void weapon_hit(Weapon* w, Pilot* p, WeaponLayer layer, Vec2* pos) {
           (!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.5))) { /* 50% chance. */
         faction_modPlayer(p->faction, -1.); /* Slowly lower faction. */
       }
-      pilot_setFlag(p, PILOT_HOSTILE);
+      pilot_setHostile(p);
       pilot_rmFlag(p, PILOT_BRIBED);
     }
     ai_attacked(p, w->parent);
@@ -684,8 +684,8 @@ static void weapon_hitBeam(Weapon* w, Pilot* p, WeaponLayer layer,
           (!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.50*dt))) { /* 50% chance. */
         faction_modPlayer(p->faction, -1.); /* Slowly lower faction. */
       }
+      pilot_setHostile(p);
       pilot_rmFlag(p, PILOT_BRIBED);
-      pilot_setFlag(p, PILOT_HOSTILE);
     }
     ai_attacked(p, w->parent);