[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>
<Enemies>
<enemies>
<enemy type = "faction">Player</enemy>
<enemy type = "alliance">Neutral</enemy>
<enemy type = "faction">Pirate</enemy>
</enemies>

View File

@ -48,7 +48,9 @@
// Call the AI function with name f.
#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))
// 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))
// 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.
static int ai_getenemy(lua_State* L) {
int i, p;
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);
lua_pushnumber(L,pilot_getNearest(cur_pilot));
return 1;
}
@ -634,7 +626,7 @@ static int ai_createvect(lua_State* L) {
vect_cset(v, x, y);
lua_pushlightuserdata(L, (void*)v);
lua_pushlightuserdata(L, v);
return 1;
}

View File

@ -85,6 +85,7 @@ int main(int argc, char** argv) {
input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, 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("mapzoomin", KEYBIND_KEYBOARD, SDLK_UP, 0);
input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_DOWN, 0);

View File

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

View File

@ -48,16 +48,28 @@ unsigned int pilot_getNext(const unsigned int id) {
for(i = 0; i < pilots; i++)
if(pilot_stack[i]->id == id)
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;
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.
Pilot* pilot_get(const unsigned int id) {
// Regular search.

View File

@ -78,6 +78,7 @@ typedef struct {
extern Pilot* player; // The player.
Pilot* pilot_get(unsigned int id);
unsigned int pilot_getNext(unsigned int id);
unsigned int pilot_getNearest(Pilot* p);
Fleet* fleet_get(const char* name);
// MISC.

View File

@ -24,7 +24,7 @@ typedef struct {
static Keybind** player_input; // Contains the players keybindings.
// Name of each keybinding.
const char* keybindNames[] = { "accel", "left", "right", "primary", "target",
"mapzoomin", "mapzoomout" };
"target_nearest", "mapzoomin", "mapzoomout" };
// Player stuff.
Pilot* player = NULL; // extern in pilot.h
@ -81,7 +81,8 @@ typedef struct {
} Rect;
typedef struct {
// graphics.
// Graphics.
gl_font smallFont;
gl_texture* gfx_frame;
gl_texture* gfx_targetPilot, *gfx_targetPlanet;
Radar radar;
@ -90,7 +91,7 @@ typedef struct {
Vec2 pos_frame;
Vec2 pos_radar;
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;
} GUI;
@ -222,15 +223,21 @@ void player_render(void) {
p = pilot_get(player_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)
// 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.)
// 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
// 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.
VX(v) = VX(gui.pos_msg);
@ -292,6 +299,8 @@ void gui_renderBar(Color* c, Vec2* p, Rect* r, double w) {
// Init GUI.
int gui_init(void) {
// Font.
gl_fontInit(&gui.smallFont, NULL, 10);
// -- Targeting.
gui.gfx_targetPilot = gl_newSprite(GFX_GUI_TARG_PILOT, 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,
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,
VX(gui.pos_frame) + 10 + 10,
VY(gui.pos_frame) + gui.gfx_frame->h - 256 - SHIP_TARGET_H + 10);
VX(gui.pos_target) + 10,
VY(gui.pos_target) +10);
// Message system.
vect_csetmin(&gui.pos_msg, 20, 30);
@ -345,6 +362,8 @@ int gui_init(void) {
// Free the GUI.
void gui_free(void) {
gl_freeFont(&gui.smallFont);
gl_freeTexture(gui.gfx_frame);
gl_freeTexture(gui.gfx_targetPilot);
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) {
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.
else if(strcmp(player_input[keynum]->name, "mapzoomin")==0) {
if(value == KEY_PRESS && gui.radar.res < RADAR_RES_MAX)