From 014b71e8d63b0ec69569fe94c6c51125ae28a525 Mon Sep 17 00:00:00 2001 From: Allanis Date: Sat, 4 Jan 2014 15:33:18 +0000 Subject: [PATCH] [Add] Added target_prev keybind. --- src/input.c | 6 ++++- src/pilot.c | 67 +++++++++++++++++++++++++++++++++++++--------------- src/pilot.h | 1 + src/player.c | 10 ++++++++ src/player.h | 1 + 5 files changed, 65 insertions(+), 20 deletions(-) diff --git a/src/input.c b/src/input.c index 4e8d436..1521365 100644 --- a/src/input.c +++ b/src/input.c @@ -36,7 +36,7 @@ const char* keybindNames[] = { /* Movement. */ "accel", "left", "right", "reverse", "afterburn", /* Targetting. */ - "target", "target_nearest", "target_hostile", + "target", "target_prev", "target_nearest", "target_hostile", /* Fighting. */ "primary", "face", "board", /* Escorts. */ @@ -73,6 +73,7 @@ void input_setDefault(void) { input_setKeybind("reverse", KEYBIND_KEYBOARD, SDLK_s, KMOD_ALL, 0); /* Targetting. */ input_setKeybind("target", KEYBIND_KEYBOARD, SDLK_TAB, KMOD_NONE, 0); + input_setKeybind("target_prev", KEYBIND_KEYBOARD, SDLK_TAB, KMOD_RCTRL, 0); input_setKeybind("target_nearest", KEYBIND_KEYBOARD, SDLK_t, KMOD_NONE, 0); input_setKeybind("target_hostile", KEYBIND_KEYBOARD, SDLK_r, KMOD_NONE, 0); /* Combat. */ @@ -285,6 +286,9 @@ static void input_key(int keynum, double value, int kabs) { else if(INGAME() && NODEAD() && KEY("target")) { if(value == KEY_PRESS) player_targetNext(); } + else if(INGAME() && NODEAD() && KEY("target_prev")) { + if(value == KEY_PRESS) player_targetPrev(); + } else if(INGAME() && NODEAD() && KEY("target_nearest")) { if(value == KEY_PRESS) player_targetNearest(); } diff --git a/src/pilot.c b/src/pilot.c index b5b3dfe..f12487f 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -58,6 +58,7 @@ extern void player_dead(void); extern void player_destroyed(void); extern int gui_load(const char* name); /* Internal. */ +static int pilot_getStackPos(const unsigned int id); static void pilot_shootWeapon(Pilot* p, PilotOutfit* w); static void pilot_update(Pilot* pilot, const double dt); static void pilot_hyperspace(Pilot* pilot); @@ -68,14 +69,14 @@ static Fleet* fleet_parse(const xmlNodePtr parent); static void pilot_dead(Pilot* p); /** - * @fn unsinged int pilot_getNextID(const unsigned int id) + * @fn static int pilot_getStackPos(const unsigned int id) * - * @brief Get the next pilot on id. - * @param id ID of current pilot. - * @return ID of next pilot of PLAYER_ID if no next pilot. + * @brief Get the pilots position in the stack. + * @param id ID of the pilot to get. + * @return Position of pilot in stack or -1 if not found. */ -unsigned int pilot_getNextID(const unsigned int id) { - /* Binary search. */ +static int pilot_getStackPos(const unsigned int id) { + /* Binary search. */ int l, m, h; l = 0; h = pilot_nstack-1; @@ -83,13 +84,44 @@ unsigned int pilot_getNextID(const unsigned int id) { m = (l+h) >> 1; /* For impossible overflow returning negative value. */ if(pilot_stack[m]->id > id) h = m-1; else if(pilot_stack[m]->id < id) l = m+1; - else break; + else return m; } - if(m == (pilot_nstack-1)) return PLAYER_ID; + /* Not found. */ + return -1; +} + +/** + * @fn unsinged int pilot_getNextID(const unsigned int id) + * + * @brief Get the next pilot on id. + * @param id ID of current pilot. + * @return ID of next pilot of PLAYER_ID if no next pilot. + */ +unsigned int pilot_getNextID(const unsigned int id) { + int m; + m = pilot_getStackPos(id); + + if((m == (pilot_nstack-1)) || (m == -1)) return PLAYER_ID; else return pilot_stack[m+1]->id; } +/** + * @fn unsigned int pilot_getPrevID(const unsigned int id) + * + * @brief Get the previous pilot based on ID. + * @param id ID of the current pilot. + * @return ID of previous pilot or PLAYER_ID if no previous pilot. + */ +unsigned int pilot_getPrevID(const unsigned int id) { + int m; + m = pilot_getStackPos(id); + + if(m == -1) return PLAYER_ID; + else if(m == 0) return pilot_stack[pilot_nstack-1]->id; + else return pilot_stack[m-1]->id; +} + /** * @brief Get the nearest enemy to the pilot. * @param p Pilot to get his nearest enemy. @@ -139,19 +171,16 @@ unsigned int pilot_getNearestPilot(const Pilot* p) { /* Pull a pilot out of the pilot_stack based on id. */ Pilot* pilot_get(const unsigned int id) { + int m; + if(id == PLAYER_ID) return player; /* Special case player. */ - /* Binary search. */ - int l, m, h; - l = 0; - h = pilot_nstack-1; - while(l <= h) { - m = (l+h)>>1; - if(pilot_stack[m]->id > id) h = m-1; - else if(pilot_stack[m]->id < id) l = m+1; - else return pilot_stack[m]; - } - return NULL; + m = pilot_getStackPos(id); + + if(m == -1) + return NULL; + else + return pilot_stack[m]; } /* Grab a fleet out of the stack. */ diff --git a/src/pilot.h b/src/pilot.h index 35531e2..606ac70 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -222,6 +222,7 @@ typedef struct Fleet_ { extern Pilot* player; /* The player. */ Pilot* pilot_get(unsigned int id); unsigned int pilot_getNextID(const unsigned int id); +unsigned int pilot_getPrevID(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); diff --git a/src/player.c b/src/player.c index 2e1f7e0..addc9de 100644 --- a/src/player.c +++ b/src/player.c @@ -1975,6 +1975,16 @@ void player_targetNext(void) { player_playSound(snd_target, 1); } +/** + * @fn + */ +void player_targetPrev(void) { + player->target = pilot_getPrevID(player->target); + + if(player->target != PLAYER_ID) + player_playSound(snd_target, 1); +} + /** * @fn player_targetNearest(void) * diff --git a/src/player.h b/src/player.h index 8d1d636..45495c9 100644 --- a/src/player.h +++ b/src/player.h @@ -74,6 +74,7 @@ int player_missionAlreadyDone(int id); /* Keybind actions. */ void player_targetHostile(void); void player_targetNext(void); +void player_targetPrev(void); void player_targetNearest(void); void player_setRadarRel(int mod); void player_secondaryNext(void);