[Change] Sanitized the pilot_get* a bit.

This commit is contained in:
Allanis 2013-08-03 15:45:56 +01:00
parent c58e73ade2
commit 82b210e6cc
4 changed files with 42 additions and 14 deletions

View File

@ -911,7 +911,7 @@ static int ai_shoot(lua_State* L) {
/* Get the nearest enemy. */ /* Get the nearest enemy. */
static int ai_getenemy(lua_State* L) { static int ai_getenemy(lua_State* L) {
lua_pushnumber(L,pilot_getNearest(cur_pilot)); lua_pushnumber(L,pilot_getNearestEnemy(cur_pilot));
return 1; return 1;
} }

View File

@ -25,7 +25,8 @@ static Keybind** input_keybinds; /* Contains the players keybindings. */
/* Name of each keybinding. */ /* Name of each keybinding. */
const char* keybindNames[] = { const char* keybindNames[] = {
"accel", "left", "right", "reverse", /* Movement. */ "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. */ "secondary", "secondary_next", /* Secondary weapons. */
"target_planet", "land", "thyperspace","starmap", "jump", /* Navigation. */ "target_planet", "land", "thyperspace","starmap", "jump", /* Navigation. */
"mapzoomin", "mapzoomout", "screenshot", "pause", "menu", "info", /* Misc. */ "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("left", KEYBIND_KEYBOARD, SDLK_a, 0);
input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, 0); input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, 0);
input_setKeybind("reverse", KEYBIND_KEYBOARD, SDLK_s, 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. */ /* Combat. */
input_setKeybind("primary", KEYBIND_KEYBOARD, SDLK_SPACE, 0); 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("face", KEYBIND_KEYBOARD, SDLK_f, 0);
input_setKeybind("board", KEYBIND_KEYBOARD, SDLK_b, 0); input_setKeybind("board", KEYBIND_KEYBOARD, SDLK_b, 0);
/* Secondary weapon. */ /* Secondary weapon. */
@ -198,10 +200,13 @@ static void input_key(int keynum, double value, int kabs) {
} }
/* Targetting. */ /* Targetting. */
else if(INGAME() && KEY("target")) { 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")) { 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. */ /* Face the target. */
else if(KEY("face")) { else if(KEY("face")) {

View File

@ -62,7 +62,7 @@ static void pilot_dead(Pilot* p);
static int pilot_oquantity(Pilot* p, PilotOutfit* w); static int pilot_oquantity(Pilot* p, PilotOutfit* w);
/* Get the next pilot based on id. */ /* 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. */ /* Binary search. */
int l, m, h; int l, m, h;
l = 0; 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. */ /* 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; unsigned int tp;
int i; int i;
double d, td; double d, td;
@ -96,11 +96,14 @@ unsigned int pilot_getNearest(const Pilot* p) {
} }
/* Get the nearest hostile enemy to the player. */ /* Get the nearest hostile enemy to the player. */
unsigned pilot_getHostile(void) { unsigned pilot_getNearestHostile(void) {
unsigned int tp; unsigned int tp;
int i; int i;
double d, td; 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)) { if(pilot_isFlag(pilot_stack[i], PILOT_HOSTILE)) {
td = vect_dist(&pilot_stack[i]->solid->pos, &player->solid->pos); td = vect_dist(&pilot_stack[i]->solid->pos, &player->solid->pos);
if(!pilot_isDisabled(pilot_stack[i]) && ((tp == PLAYER_ID) || (td < d))) { if(!pilot_isDisabled(pilot_stack[i]) && ((tp == PLAYER_ID) || (td < d))) {
@ -111,6 +114,25 @@ unsigned pilot_getHostile(void) {
return tp; 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. */ /* Pull a pilot out of the pilot_stack based on id. */
Pilot* pilot_get(const unsigned int id) { Pilot* pilot_get(const unsigned int id) {
if(id == PLAYER_ID) return player; /* Special case player. */ if(id == PLAYER_ID) return player; /* Special case player. */

View File

@ -138,13 +138,14 @@ typedef struct Fleet_ {
/* Grabing pilot crap. */ /* Grabing pilot crap. */
extern Pilot* player; /* The player. */ extern Pilot* player; /* The player. */
Pilot* pilot_get(unsigned int id); Pilot* pilot_get(unsigned int id);
unsigned int pilot_getNext(const unsigned int id); unsigned int pilot_getNextID(const unsigned int id);
unsigned int pilot_getNearest(const Pilot* p); unsigned int pilot_getNearestEnemy(const Pilot* p);
unsigned int pilot_getHostile(void); /* Only for the player. */ unsigned int pilot_getNearestHostile(void); /* Only for the player. */
unsigned int pilot_getNearestPilot(const Pilot* p);
Fleet* fleet_get(const char* name); Fleet* fleet_get(const char* name);
int pilot_getJumps(const Pilot* p); int pilot_getJumps(const Pilot* p);
/* MISC. */ /* Misc. */
void pilot_shoot(Pilot* p, const unsigned int target, const int secondary); void pilot_shoot(Pilot* p, const unsigned int target, const int secondary);
void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter, void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter,
const DamageType dtype, const double damage); const DamageType dtype, const double damage);