[Add] keybinding 'r' to grab nearest enemy target -- Tamir's request.

This commit is contained in:
Allanis 2013-02-10 04:12:05 +00:00
parent 2d428e83f1
commit f6f1677614
7 changed files with 61 additions and 32 deletions

View File

@ -16,6 +16,7 @@
</Alliances> </Alliances>
<Enemies> <Enemies>
<enemies> <enemies>
<enemy type = "faction">Player</enemy>
<enemy type = "alliance">Neutral</enemy> <enemy type = "alliance">Neutral</enemy>
<enemy type = "faction">Pirate</enemy> <enemy type = "faction">Pirate</enemy>
</enemies> </enemies>

View File

@ -48,7 +48,9 @@
// Call the AI function with name f. // Call the AI function with name f.
#define AI_LCALL(f) (lua_getglobal(L, f), lua_pcall(L, 0, 0, 0)) #define AI_LCALL(f) (lua_getglobal(L, f), lua_pcall(L, 0, 0, 0))
// Register a number constant n to name s (syntax is just like lua_register).
#define lua_regnumber(l,s,n) (lua_pushnumber(l,n), lua_setglobal(l,s)) #define lua_regnumber(l,s,n) (lua_pushnumber(l,n), lua_setglobal(l,s))
// L state, void* buf, int n size, char* s identifier.
#define luaL_dobuffer(L,b,n,s) (luaL_loadbuffer(L,b,n,s) || lua_pcall(L, 0, LUA_MULTRET, 0)) #define luaL_dobuffer(L,b,n,s) (luaL_loadbuffer(L,b,n,s) || lua_pcall(L, 0, LUA_MULTRET, 0))
// Don't run the function if (n) params aren't passed. // Don't run the function if (n) params aren't passed.
@ -589,17 +591,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) {
int i, p; lua_pushnumber(L,pilot_getNearest(cur_pilot));
double d, td;
for(p = -1, i = 0; i < pilots; i++)
if(areEnemies(cur_pilot->faction, pilot_stack[i]->faction)) {
td = vect_dist(&pilot_stack[i]->solid->pos, &cur_pilot->solid->pos);
if((p == -1) || (td < d)) {
d = td;
p = pilot_stack[i]->id;
}
}
lua_pushnumber(L,p);
return 1; return 1;
} }
@ -634,7 +626,7 @@ static int ai_createvect(lua_State* L) {
vect_cset(v, x, y); vect_cset(v, x, y);
lua_pushlightuserdata(L, (void*)v); lua_pushlightuserdata(L, v);
return 1; return 1;
} }

View File

@ -80,13 +80,14 @@ int main(int argc, char** argv) {
// input. // input.
input_init(); input_init();
input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, 0); input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, 0);
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("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", KEYBIND_KEYBOARD, SDLK_TAB, 0);
input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_UP, 0); input_setKeybind("target_nearest", KEYBIND_KEYBOARD, SDLK_r, 0);
input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_DOWN, 0); input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_UP, 0);
input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_DOWN, 0);
// Use Lua to parse configuration file. // Use Lua to parse configuration file.
lua_State* L = luaL_newstate(); lua_State* L = luaL_newstate();

View File

@ -249,7 +249,7 @@ gl_texture* gl_loadImage(SDL_Surface* surface) {
gl_texture* gl_newImage(const char* path) { gl_texture* gl_newImage(const char* path) {
SDL_Surface* tmp, *surface; SDL_Surface* tmp, *surface;
gl_texture* t; gl_texture* t;
void* trans = NULL; uint8_t* trans = NULL;
uint32_t filesize; uint32_t filesize;
char* buf = pack_readfile(DATA, (char*)path, &filesize); char* buf = pack_readfile(DATA, (char*)path, &filesize);
if(buf == NULL) { if(buf == NULL) {

View File

@ -48,16 +48,28 @@ unsigned int pilot_getNext(const unsigned int id) {
for(i = 0; i < pilots; i++) for(i = 0; i < pilots; i++)
if(pilot_stack[i]->id == id) if(pilot_stack[i]->id == id)
break; break;
// Dichotomical search.
//int i, n;
//for(i = 0, n = pilots/2; n > 0; n /= 2)
//i += (pilot_stack[i+n]->id > id) ? 0 : n;
if(i == pilots-1) return 0; if(i == pilots-1) return 0;
return pilot_stack[i+1]->id; return pilot_stack[i+1]->id;
} }
// Get the nearest enemy to the pilot -- Tamir's (insightful) request.
unsigned int pilot_getNearest(Pilot* p) {
int i, tp;
double d, td;
for(tp = -1, i = 0; i < pilots; i++)
if(areEnemies(p->faction, pilot_stack[i]->faction)) {
td = vect_dist(&pilot_stack[i]->solid->pos, &p->solid->pos);
if((tp == -1) || (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) {
// Regular search. // Regular search.

View File

@ -78,6 +78,7 @@ typedef struct {
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(unsigned int id); unsigned int pilot_getNext(unsigned int id);
unsigned int pilot_getNearest(Pilot* p);
Fleet* fleet_get(const char* name); Fleet* fleet_get(const char* name);
// MISC. // MISC.

View File

@ -24,7 +24,7 @@ typedef struct {
static Keybind** player_input; // Contains the players keybindings. static Keybind** player_input; // Contains the players keybindings.
// Name of each keybinding. // Name of each keybinding.
const char* keybindNames[] = { "accel", "left", "right", "primary", "target", const char* keybindNames[] = { "accel", "left", "right", "primary", "target",
"mapzoomin", "mapzoomout" }; "target_nearest", "mapzoomin", "mapzoomout" };
// Player stuff. // Player stuff.
Pilot* player = NULL; // extern in pilot.h Pilot* player = NULL; // extern in pilot.h
@ -81,7 +81,8 @@ typedef struct {
} Rect; } Rect;
typedef struct { typedef struct {
// graphics. // Graphics.
gl_font smallFont;
gl_texture* gfx_frame; gl_texture* gfx_frame;
gl_texture* gfx_targetPilot, *gfx_targetPlanet; gl_texture* gfx_targetPilot, *gfx_targetPlanet;
Radar radar; Radar radar;
@ -90,7 +91,7 @@ typedef struct {
Vec2 pos_frame; Vec2 pos_frame;
Vec2 pos_radar; Vec2 pos_radar;
Vec2 pos_shield, pos_armor, pos_energy; Vec2 pos_shield, pos_armor, pos_energy;
Vec2 pos_target, pos_target_health; Vec2 pos_target, pos_target_health, pos_target_name, pos_target_faction;
Vec2 pos_msg; Vec2 pos_msg;
} GUI; } GUI;
@ -222,15 +223,21 @@ void player_render(void) {
p = pilot_get(player_target); p = pilot_get(player_target);
gl_blitStatic(p->ship->gfx_target, &gui.pos_target); gl_blitStatic(p->ship->gfx_target, &gui.pos_target);
// Target name.
gl_print(NULL, &gui.pos_target_name, "%s", p->name);
gl_print(&gui.smallFont, &gui.pos_target_faction, "%s", p->faction->name);
// Target status.
if(p->armor < p->armor_max * PILOT_DISABLED) if(p->armor < p->armor_max * PILOT_DISABLED)
// Disable the pilot. // Disable the pilot.
gl_print(NULL, &gui.pos_target_health, "Disabled"); gl_print(&gui.smallFont, &gui.pos_target_health, "Disabled");
else if(p->shield > p->shield_max / 100.) else if(p->shield > p->shield_max / 100.)
// On shields. // On shields.
gl_print(NULL, &gui.pos_target_health, "%s: %.0f%%", "Shield", p->shield/p->shield_max*100.); gl_print(&gui.smallFont, &gui.pos_target_health, "%s: %.0f%%", "Shield", p->shield/p->shield_max*100.);
else else
// On armor. // On armor.
gl_print(NULL, &gui.pos_target_health, "%s: %.0f%%", "Armor", p->armor/p->armor_max*100.); gl_print(&gui.smallFont, &gui.pos_target_health, "%s: %.0f%%", "Armor", p->armor/p->armor_max*100.);
} }
// Messages. // Messages.
VX(v) = VX(gui.pos_msg); VX(v) = VX(gui.pos_msg);
@ -292,6 +299,8 @@ void gui_renderBar(Color* c, Vec2* p, Rect* r, double w) {
// Init GUI. // Init GUI.
int gui_init(void) { int gui_init(void) {
// Font.
gl_fontInit(&gui.smallFont, NULL, 10);
// -- Targeting. // -- Targeting.
gui.gfx_targetPilot = gl_newSprite(GFX_GUI_TARG_PILOT, 2, 2); gui.gfx_targetPilot = gl_newSprite(GFX_GUI_TARG_PILOT, 2, 2);
gui.gfx_targetPlanet = gl_newSprite(GFX_GUI_TARG_PLANET, 2, 2); gui.gfx_targetPlanet = gl_newSprite(GFX_GUI_TARG_PLANET, 2, 2);
@ -331,9 +340,17 @@ int gui_init(void) {
VX(gui.pos_frame) + 10, VX(gui.pos_frame) + 10,
VY(gui.pos_frame) + gui.gfx_frame->h - 256 - SHIP_TARGET_H); VY(gui.pos_frame) + gui.gfx_frame->h - 256 - SHIP_TARGET_H);
vect_csetmin(&gui.pos_target_name,
VX(gui.pos_target) + 10,
VY(gui.pos_target) + SHIP_TARGET_H - 10 - gl_defFont.h);
vect_csetmin(&gui.pos_target_faction,
VX(gui.pos_target_name),
VY(gui.pos_target_name) - gui.smallFont.h - 4);
vect_csetmin(&gui.pos_target_health, vect_csetmin(&gui.pos_target_health,
VX(gui.pos_frame) + 10 + 10, VX(gui.pos_target) + 10,
VY(gui.pos_frame) + gui.gfx_frame->h - 256 - SHIP_TARGET_H + 10); VY(gui.pos_target) +10);
// Message system. // Message system.
vect_csetmin(&gui.pos_msg, 20, 30); vect_csetmin(&gui.pos_msg, 20, 30);
@ -345,6 +362,8 @@ int gui_init(void) {
// Free the GUI. // Free the GUI.
void gui_free(void) { void gui_free(void) {
gl_freeFont(&gui.smallFont);
gl_freeTexture(gui.gfx_frame); gl_freeTexture(gui.gfx_frame);
gl_freeTexture(gui.gfx_targetPilot); gl_freeTexture(gui.gfx_targetPilot);
gl_freeTexture(gui.gfx_targetPlanet); gl_freeTexture(gui.gfx_targetPlanet);
@ -432,6 +451,9 @@ static void input_key(int keynum, double value, int abs) {
else if(strcmp(player_input[keynum]->name, "target")==0) { else if(strcmp(player_input[keynum]->name, "target")==0) {
if(value == KEY_PRESS) player_target = pilot_getNext(player_target); if(value == KEY_PRESS) player_target = pilot_getNext(player_target);
} }
else if(strcmp(player_input[keynum]->name, "target_nearest")==0) {
if(value == KEY_PRESS) player_target = pilot_getNearest(player);
}
// Zoom in. // Zoom in.
else if(strcmp(player_input[keynum]->name, "mapzoomin")==0) { else if(strcmp(player_input[keynum]->name, "mapzoomin")==0) {
if(value == KEY_PRESS && gui.radar.res < RADAR_RES_MAX) if(value == KEY_PRESS && gui.radar.res < RADAR_RES_MAX)