From 82b210e6cc97f69299250228c10b3750180dfaa4 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sat, 3 Aug 2013 15:45:56 +0100
Subject: [PATCH] [Change] Sanitized the pilot_get* a bit.

---
 src/ai.c    |  2 +-
 src/input.c | 15 ++++++++++-----
 src/pilot.c | 30 ++++++++++++++++++++++++++----
 src/pilot.h |  9 +++++----
 4 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/src/ai.c b/src/ai.c
index c36ef30..829249f 100644
--- a/src/ai.c
+++ b/src/ai.c
@@ -911,7 +911,7 @@ static int ai_shoot(lua_State* L) {
 
 /* Get the nearest enemy. */
 static int ai_getenemy(lua_State* L) {
-  lua_pushnumber(L,pilot_getNearest(cur_pilot));
+  lua_pushnumber(L,pilot_getNearestEnemy(cur_pilot));
   return 1;
 }
 
diff --git a/src/input.c b/src/input.c
index bceac48..580500c 100644
--- a/src/input.c
+++ b/src/input.c
@@ -25,7 +25,8 @@ static Keybind** input_keybinds; /* Contains the players keybindings. */
 /* Name of each keybinding. */
 const char* keybindNames[] = {
   "accel", "left", "right", "reverse", /* Movement. */
-  "primary", "target", "target_nearest", "face", "board", /* Combat. */
+  "target", "target_nearest", "target_hostile", /* Targetting. */
+  "primary", "face", "board", /* Fighting. */
   "secondary", "secondary_next", /* Secondary weapons. */
   "target_planet", "land", "thyperspace","starmap", "jump", /* Navigation. */
   "mapzoomin", "mapzoomout", "screenshot", "pause", "menu", "info", /* Misc. */
@@ -49,10 +50,11 @@ void input_setDefault(void) {
   input_setKeybind("left",            KEYBIND_KEYBOARD, SDLK_a,       0);
   input_setKeybind("right",           KEYBIND_KEYBOARD, SDLK_d,       0);
   input_setKeybind("reverse",         KEYBIND_KEYBOARD, SDLK_s,       0);
+  input_setKeybind("target",          KEYBIND_KEYBOARD, SDLK_TAB,     0);
+  input_setKeybind("target_nearest",  KEYBIND_KEYBOARD, SDLK_t,       0);
+  input_setKeybind("target_hostile",  KEYBIND_KEYBOARD, SDLK_r,       0);
   /* Combat. */
   input_setKeybind("primary",         KEYBIND_KEYBOARD, SDLK_SPACE,   0);
-  input_setKeybind("target",          KEYBIND_KEYBOARD, SDLK_TAB,     0);
-  input_setKeybind("target_nearest",  KEYBIND_KEYBOARD, SDLK_r,       0);
   input_setKeybind("face",            KEYBIND_KEYBOARD, SDLK_f,       0);
   input_setKeybind("board",           KEYBIND_KEYBOARD, SDLK_b,       0);
   /* Secondary weapon. */
@@ -198,10 +200,13 @@ static void input_key(int keynum, double value, int kabs) {
   }
   /* Targetting. */
   else if(INGAME() && KEY("target")) {
-    if(value == KEY_PRESS) player_target = pilot_getNext(player_target);
+    if(value == KEY_PRESS) player_target = pilot_getNextID(player_target);
   }
   else if(INGAME() && KEY("target_nearest")) {
-    if(value == KEY_PRESS) player_target = pilot_getHostile();
+    if(value == KEY_PRESS) player_target = pilot_getNearestPilot(player);
+  }
+  else if(INGAME() && KEY("target_hostile")) {
+    if(value == KEY_PRESS) player_target = pilot_getNearestHostile();
   }
   /* Face the target. */
   else if(KEY("face")) {
diff --git a/src/pilot.c b/src/pilot.c
index 138999d..1b3ee03 100644
--- a/src/pilot.c
+++ b/src/pilot.c
@@ -62,7 +62,7 @@ static void pilot_dead(Pilot* p);
 static int pilot_oquantity(Pilot* p, PilotOutfit* w);
 
 /* Get the next pilot based on id. */
-unsigned int pilot_getNext(const unsigned int id) {
+unsigned int pilot_getNextID(const unsigned int id) {
   /* Binary search. */
   int l, m, h;
   l = 0;
@@ -79,7 +79,7 @@ unsigned int pilot_getNext(const unsigned int id) {
 }
 
 /* Get the nearest enemy to the pilot -- Tamir's (insightful) request. */
-unsigned int pilot_getNearest(const Pilot* p) {
+unsigned int pilot_getNearestEnemy(const Pilot* p) {
   unsigned int tp;
   int i;
   double d, td;
@@ -96,11 +96,14 @@ unsigned int pilot_getNearest(const Pilot* p) {
 }
 
 /* Get the nearest hostile enemy to the player. */
-unsigned pilot_getHostile(void) {
+unsigned pilot_getNearestHostile(void) {
   unsigned int tp;
   int i;
   double d, td;
-  for(tp = PLAYER_ID, d = 0., i = 0; i < pilot_nstack; i++)
+
+  tp = PLAYER_ID;
+  d = 0;
+  for(i = 0; i < pilot_nstack; i++)
     if(pilot_isFlag(pilot_stack[i], PILOT_HOSTILE)) {
       td = vect_dist(&pilot_stack[i]->solid->pos, &player->solid->pos);
       if(!pilot_isDisabled(pilot_stack[i]) && ((tp == PLAYER_ID) || (td < d))) {
@@ -111,6 +114,25 @@ unsigned pilot_getHostile(void) {
   return tp;
 }
 
+/* Get the nearest pilot. */
+unsigned int pilot_getNearestPilot(const Pilot* p) {
+  unsigned int tp;
+  int i;
+  double d, td;
+
+  tp = PLAYER_ID;
+  d = 0;
+  for(i = 0; i < pilot_nstack; i++)
+    if(pilot_stack[i] != p) {
+      td = vect_dist(&pilot_stack[i]->solid->pos, &player->solid->pos);
+      if(!pilot_isDisabled(pilot_stack[i]) && ((tp == PLAYER_ID) || (td < d))) {
+        d = td;
+        tp = pilot_stack[i]->id;
+      }
+    }
+  return tp;
+}
+
 /* Pull a pilot out of the pilot_stack based on id. */
 Pilot* pilot_get(const unsigned int id) {
   if(id == PLAYER_ID) return player; /* Special case player. */
diff --git a/src/pilot.h b/src/pilot.h
index 60d4a56..50556ba 100644
--- a/src/pilot.h
+++ b/src/pilot.h
@@ -138,13 +138,14 @@ typedef struct Fleet_ {
 /* Grabing pilot crap. */
 extern Pilot* player; /* The player. */
 Pilot* pilot_get(unsigned int id);
-unsigned int pilot_getNext(const unsigned int id);
-unsigned int pilot_getNearest(const Pilot* p);
-unsigned int pilot_getHostile(void); /* Only for the player. */
+unsigned int pilot_getNextID(const unsigned int id);
+unsigned int pilot_getNearestEnemy(const Pilot* p);
+unsigned int pilot_getNearestHostile(void); /* Only for the player. */
+unsigned int pilot_getNearestPilot(const Pilot* p);
 Fleet* fleet_get(const char* name);
 int pilot_getJumps(const Pilot* p);
 
-/* MISC. */
+/* Misc. */
 void pilot_shoot(Pilot* p, const unsigned int target, const int secondary);
 void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter,
     const DamageType dtype, const double damage);