[Change] Moving to target on pilot structure.

This commit is contained in:
Allanis 2013-12-29 22:13:28 +00:00
parent 4e1fee762b
commit 8b0fbb8772
6 changed files with 58 additions and 52 deletions

View File

@ -102,14 +102,14 @@ static int ai_accel(lua_State* L); /* Accelerate. */
static void ai_run(lua_State* L, const char* funcname); static void ai_run(lua_State* L, const char* funcname);
static int ai_loadProfile(char* filename); static int ai_loadProfile(char* filename);
static void ai_freetask(Task* t); static void ai_freetask(Task* t);
static void ai_setmemory(void); static void ai_setMemory(void);
static void ai_create(Pilot* pilot, char* param); static void ai_create(Pilot* pilot, char* param);
/* External C routines. */ /* External C routines. */
void ai_attacked(Pilot* attacked, const unsigned int attacker); /* weapon.c */ void ai_attacked(Pilot* attacked, const unsigned int attacker); /* weapon.c */
/* C routines made external. */ /* C routines made external. */
int ai_pinit(Pilot* p, char* ai); int ai_pinit(Pilot* p, char* ai); /* pilot.c */
void ai_destroy(Pilot* p); void ai_destroy(Pilot* p); /* pilot.c */
void ai_think(Pilot* pilot); void ai_think(Pilot* pilot); /* escort.c */
/* Ai routines for Lua. */ /* Ai routines for Lua. */
/* Tasks. */ /* Tasks. */
@ -235,7 +235,6 @@ static Pilot* cur_pilot = NULL; /**< Current pilot. All functions use this.
static double pilot_acc = 0.; /**< Current pilots acceleration. */ static double pilot_acc = 0.; /**< Current pilots acceleration. */
static double pilot_turn = 0.; /**< Current pilots turning. */ static double pilot_turn = 0.; /**< Current pilots turning. */
static int pilot_flags = 0; /**< Handle stuff like weapon firing. */ static int pilot_flags = 0; /**< Handle stuff like weapon firing. */
static int pilot_target = 0; /**< Indicate the target to aim at for the pilot. */
/* Ai status: 'Create' functions that can't be used elsewhere. */ /* Ai status: 'Create' functions that can't be used elsewhere. */
#define AI_STATUS_NORMAL 1 /**< Normal AI function behaviour. */ #define AI_STATUS_NORMAL 1 /**< Normal AI function behaviour. */
@ -243,11 +242,11 @@ static int pilot_target = 0; /**< Indicate the target to aim at for the p
static int ai_status = AI_STATUS_NORMAL; /**< Current AI run statis. */ static int ai_status = AI_STATUS_NORMAL; /**< Current AI run statis. */
/** /**
* @fn static void ai_setmemory(void) * @fn static void ai_setMemory(void)
* *
* @brief Set the cur_pilots ai. * @brief Set the cur_pilots ai.
*/ */
static void ai_setmemory(void) { static void ai_setMemory(void) {
lua_State* L; lua_State* L;
L = cur_pilot->ai->L; L = cur_pilot->ai->L;
@ -257,6 +256,16 @@ static void ai_setmemory(void) {
lua_setglobal(L, "mem"); lua_setglobal(L, "mem");
} }
/**
* @fn void ai_setPilot(Pilot* p)
*
* @brief Set the pilot for further AI calls.
*/
void ai_setPilot(Pilot* p) {
cur_pilot = p;
ai_setMemory();
}
/** /**
* @fn static void ai_run(lua_State* L, const char* funcname) * @fn static void ai_run(lua_State* L, const char* funcname)
* *
@ -266,9 +275,6 @@ static void ai_setmemory(void) {
* @param[in] funcname Function to run. * @param[in] funcname Function to run.
*/ */
static void ai_run(lua_State* L, const char* funcname) { static void ai_run(lua_State* L, const char* funcname) {
/* Set the pilot's memory. */
ai_setmemory();
lua_getglobal(L, funcname); lua_getglobal(L, funcname);
if(lua_pcall(L, 0, 0, 0)) if(lua_pcall(L, 0, 0, 0))
/* Errors accured. */ /* Errors accured. */
@ -492,14 +498,14 @@ void ai_exit(void) {
void ai_think(Pilot* pilot) { void ai_think(Pilot* pilot) {
lua_State* L; lua_State* L;
cur_pilot = pilot; /* Set current pilot being processed. */ ai_setPilot(pilot);
L = cur_pilot->ai->L; /* Set the AI profile to the current pilot's. */ L = cur_pilot->ai->L; /* Set the AI profile to the current pilot's. */
/* Clean up some variables. */ /* Clean up some variables. */
pilot_acc = 0.; pilot_acc = 0.;
pilot_turn = 0.; pilot_turn = 0.;
pilot_flags = 0; pilot_flags = 0;
pilot_target = 0; cur_pilot->target = cur_pilot->id;
/* Control function if pilot is idle or tick is up. */ /* Control function if pilot is idle or tick is up. */
if((cur_pilot->tcontrol < SDL_GetTicks()) || (cur_pilot->task == NULL)) { if((cur_pilot->tcontrol < SDL_GetTicks()) || (cur_pilot->task == NULL)) {
@ -523,8 +529,8 @@ void ai_think(Pilot* pilot) {
cur_pilot->solid->dir); cur_pilot->solid->dir);
/* Fire weapons if needs be. */ /* Fire weapons if needs be. */
if(ai_isFlag(AI_PRIMARY)) pilot_shoot(pilot, pilot_target, 0); /* Primary. */ if(ai_isFlag(AI_PRIMARY)) pilot_shoot(pilot, cur_pilot->target, 0); /* Primary. */
if(ai_isFlag(AI_SECONDARY)) pilot_shoot(pilot, pilot_target, 1); /* Secondary. */ if(ai_isFlag(AI_SECONDARY)) pilot_shoot(pilot, cur_pilot->target, 1); /* Secondary. */
} }
/** /**
@ -538,7 +544,7 @@ void ai_think(Pilot* pilot) {
void ai_attacked(Pilot* attacked, const unsigned int attacker) { void ai_attacked(Pilot* attacked, const unsigned int attacker) {
lua_State* L; lua_State* L;
cur_pilot = attacked; ai_setPilot(attacked);
L = cur_pilot->ai->L; L = cur_pilot->ai->L;
lua_getglobal(L, "attacked"); lua_getglobal(L, "attacked");
lua_pushnumber(L, attacker); lua_pushnumber(L, attacker);
@ -559,12 +565,9 @@ void ai_attacked(Pilot* attacked, const unsigned int attacker) {
static void ai_create(Pilot* pilot, char* param) { static void ai_create(Pilot* pilot, char* param) {
lua_State* L; lua_State* L;
cur_pilot = pilot; ai_setPilot(pilot);
L = cur_pilot->ai->L; L = cur_pilot->ai->L;
/* Set the pilots memory. */
ai_setmemory();
/* Set creation mode. */ /* Set creation mode. */
ai_status = AI_STATUS_CREATE; ai_status = AI_STATUS_CREATE;
@ -1179,7 +1182,7 @@ static int ai_settarget(lua_State* L) {
LLUA_MIN_ARGS(1); LLUA_MIN_ARGS(1);
if(lua_isnumber(L,1)) { if(lua_isnumber(L,1)) {
pilot_target = (int)lua_tonumber(L,1); cur_pilot->target = (int)lua_tonumber(L,1);
return 1; return 1;
} }

View File

@ -12,8 +12,6 @@
#define BOARDING_WIDTH 300 /** Boarding window width. */ #define BOARDING_WIDTH 300 /** Boarding window width. */
#define BOARDING_HEIGHT 200 /** boarding window height. */ #define BOARDING_HEIGHT 200 /** boarding window height. */
extern unsigned int player_target;
static unsigned int board_wid = 0; /** Boarding window identifier. */ static unsigned int board_wid = 0; /** Boarding window identifier. */
static void board_exit(char* str); static void board_exit(char* str);
@ -32,12 +30,12 @@ static void board_update(void);
void player_board(void) { void player_board(void) {
Pilot* p; Pilot* p;
if(player_target == PLAYER_ID) { if(player->target == PLAYER_ID) {
player_message("You need a target to board first!"); player_message("You need a target to board first!");
return; return;
} }
p = pilot_get(player_target); p = pilot_get(player->target);
if(!pilot_isDisabled(p)) { if(!pilot_isDisabled(p)) {
player_message("You cannot board a ship that is not disabled!"); player_message("You cannot board a ship that is not disabled!");
@ -110,7 +108,7 @@ static void board_stealCreds(char* str) {
(void)str; (void)str;
Pilot* p; Pilot* p;
p = pilot_get(player_target); p = pilot_get(player->target);
if(p->credits == 0) { if(p->credits == 0) {
/* Can't steal from the poor. ;) */ /* Can't steal from the poor. ;) */
@ -137,7 +135,7 @@ static void board_stealCargo(char* str) {
int q; int q;
Pilot* p; Pilot* p;
p = pilot_get(player_target); p = pilot_get(player->target);
if(p->ncommodities==0) { if(p->ncommodities==0) {
/* No cargo. */ /* No cargo. */
@ -173,7 +171,7 @@ static void board_stealCargo(char* str) {
static int board_fail(void) { static int board_fail(void) {
Pilot* p; Pilot* p;
p = pilot_get(player_target); p = pilot_get(player->target);
/* Fail chance. */ /* Fail chance. */
if(RNG(0, 100) > (int)(50. * if(RNG(0, 100) > (int)(50. *
@ -203,7 +201,7 @@ static void board_update(void) {
char cred[10]; char cred[10];
Pilot* p; Pilot* p;
p = pilot_get(player_target); p = pilot_get(player->target);
credits2str(cred, p->credits, 2); credits2str(cred, p->credits, 2);

View File

@ -1206,6 +1206,7 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, int faction,
else else
pilot->id = ++pilot_id; /* New unique pilot id based on pilot_id, Can't be 0. */ pilot->id = ++pilot_id; /* New unique pilot id based on pilot_id, Can't be 0. */
/* Basic information. */
pilot->ship = ship; pilot->ship = ship;
pilot->name = strdup((name == NULL) ? ship->name : name); pilot->name = strdup((name == NULL) ? ship->name : name);
@ -1260,6 +1261,7 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, int faction,
pilot->update = pilot_update; pilot->update = pilot_update;
/* AI. */ /* AI. */
pilot->target = pilot->id; /* Self = no target. */
if(ai != NULL) if(ai != NULL)
ai_pinit(pilot, ai); /* Must run before ai_create. */ ai_pinit(pilot, ai); /* Must run before ai_create. */
} }

View File

@ -162,7 +162,13 @@ typedef struct Pilot_ {
int hook_type[PILOT_HOOKS]; /**< Type of the hook atached to the pilot. */ int hook_type[PILOT_HOOKS]; /**< Type of the hook atached to the pilot. */
int hook[PILOT_HOOKS]; /**< Hook id. */ int hook[PILOT_HOOKS]; /**< Hook id. */
/* Escort stuff. */
unsigned int parent; /**< Pilots parent. */
unsigned int* escort; /**< Pilots escorts. */
int nescorts; /**< Number of pilot escorts. */
/* AI. */ /* AI. */
unsigned int target; /**< AI target. */
AI_Profile* ai; /**< Ai personality profile. */ AI_Profile* ai; /**< Ai personality profile. */
unsigned int tcontrol; /**< Timer for control tick. */ unsigned int tcontrol; /**< Timer for control tick. */
unsigned int timer[MAX_AI_TIMERS]; /**< Timers for AI. */ unsigned int timer[MAX_AI_TIMERS]; /**< Timers for AI. */

View File

@ -74,7 +74,6 @@ unsigned int player_flags = 0; /**< Player flags. */
/* Input.c */ /* Input.c */
double player_turn = 0.; /**< Turn velocity from input. */ double player_turn = 0.; /**< Turn velocity from input. */
static double player_acc = 0.; /**< Accel velocity from input. */ static double player_acc = 0.; /**< Accel velocity from input. */
unsigned int player_target = PLAYER_ID; /**< Targetted pilot. PLAYER_ID is none. */
/* Internal */ /* Internal */
int planet_target = -1; /**< Targetted planet. -1 is none. */ int planet_target = -1; /**< Targetted planet. -1 is none. */
@ -675,7 +674,8 @@ void player_warp(const double x, const double y) {
* @brief Clear the targets. * @brief Clear the targets.
*/ */
void player_clear(void) { void player_clear(void) {
player_target = PLAYER_ID; if(player != NULL)
player->target = PLAYER_ID;
planet_target = -1; planet_target = -1;
hyperspace_target = -1; hyperspace_target = -1;
} }
@ -800,10 +800,10 @@ void player_render(void) {
if((player != NULL) && !player_isFlag(PLAYER_CREATING)) { if((player != NULL) && !player_isFlag(PLAYER_CREATING)) {
/* Render the player target graphics. */ /* Render the player target graphics. */
if(player_target != PLAYER_ID) p = pilot_get(player_target); if(player->target != PLAYER_ID) p = pilot_get(player->target);
else p = NULL; else p = NULL;
if((p == NULL) || pilot_isFlag(p, PILOT_DEAD)) if((p == NULL) || pilot_isFlag(p, PILOT_DEAD))
player_target = PLAYER_ID; /* No more pilot target. */ player->target = PLAYER_ID; /* No more pilot target. */
else { else {
/* There is still a pilot target. */ /* There is still a pilot target. */
if(pilot_isDisabled(p)) c = &cInert; if(pilot_isDisabled(p)) c = &cInert;
@ -900,7 +900,7 @@ void player_renderGUI(void) {
/* Render the pilots. */ /* Render the pilots. */
for(j = 0, i = 1; i < pilot_nstack; i++) { for(j = 0, i = 1; i < pilot_nstack; i++) {
/* Skip the player. */ /* Skip the player. */
if(pilot_stack[i]->id == player_target) j = i; if(pilot_stack[i]->id == player->target) j = i;
else gui_renderPilot(pilot_stack[i]); else gui_renderPilot(pilot_stack[i]);
} }
/* Render the targetted pilot. */ /* Render the targetted pilot. */
@ -1016,8 +1016,8 @@ void player_renderGUI(void) {
} }
/* Target. */ /* Target. */
if(player_target != PLAYER_ID) { if(player->target != PLAYER_ID) {
p = pilot_get(player_target); p = pilot_get(player->target);
/* Blit the pilot target. */ /* Blit the pilot target. */
gl_blitStatic(p->ship->gfx_target, gui.target.x, gui.target.y, NULL); gl_blitStatic(p->ship->gfx_target, gui.target.x, gui.target.y, NULL);
@ -1161,7 +1161,7 @@ static void gui_renderPilot(const Pilot* p) {
glBegin(GL_QUADS); glBegin(GL_QUADS);
/* Colours. */ /* Colours. */
if(p->id == player_target) col = &cRadar_targ; if(p->id == player->target) col = &cRadar_targ;
else if(pilot_isDisabled(p)) col = &cInert; else if(pilot_isDisabled(p)) col = &cInert;
else if(pilot_isFlag(p, PILOT_HOSTILE)) col = &cHostile; else if(pilot_isFlag(p, PILOT_HOSTILE)) col = &cHostile;
else col = faction_getColour(p->faction); else col = faction_getColour(p->faction);
@ -1539,10 +1539,10 @@ void player_think(Pilot* pplayer) {
/* PLAYER_FACE will take over navigation. */ /* PLAYER_FACE will take over navigation. */
if(player_isFlag(PLAYER_FACE)) { if(player_isFlag(PLAYER_FACE)) {
if(player_target != PLAYER_ID) if(player->target != PLAYER_ID)
pilot_face(pplayer, pilot_face(pplayer,
vect_angle(&player->solid->pos, vect_angle(&player->solid->pos,
&pilot_get(player_target)->solid->pos)); &pilot_get(player->target)->solid->pos));
else if(planet_target != -1) else if(planet_target != -1)
pilot_face(pplayer, pilot_face(pplayer,
vect_angle(&player->solid->pos, vect_angle(&player->solid->pos,
@ -1564,7 +1564,7 @@ void player_think(Pilot* pplayer) {
/* Primary weapon. */ /* Primary weapon. */
if(player_isFlag(PLAYER_PRIMARY)) { if(player_isFlag(PLAYER_PRIMARY)) {
pilot_shoot(pplayer, player_target, 0); pilot_shoot(pplayer, player->target, 0);
player_setFlag(PLAYER_PRIMARY_L); player_setFlag(PLAYER_PRIMARY_L);
} }
else if(player_isFlag(PLAYER_PRIMARY_L)) { else if(player_isFlag(PLAYER_PRIMARY_L)) {
@ -1580,7 +1580,7 @@ void player_think(Pilot* pplayer) {
outfit_isBeam(pplayer->secondary->outfit)) { outfit_isBeam(pplayer->secondary->outfit)) {
pilot_shootStop(pplayer, 1); pilot_shootStop(pplayer, 1);
} else } else
pilot_shoot(pplayer, player_target, 1); pilot_shoot(pplayer, player->target, 1);
player_setFlag(PLAYER_SECONDARY_L); player_setFlag(PLAYER_SECONDARY_L);
} }
else if(player_isFlag(PLAYER_SECONDARY_L)) { else if(player_isFlag(PLAYER_SECONDARY_L)) {
@ -1939,10 +1939,10 @@ void player_targetHostile(void) {
} }
} }
if((tp != PLAYER_ID) && (tp != player_target)) if((tp != PLAYER_ID) && (tp != player->target))
player_playSound(snd_target, 1); player_playSound(snd_target, 1);
player_target = tp; player->target = tp;
} }
/** /**
@ -1951,9 +1951,9 @@ void player_targetHostile(void) {
* @brief Cycles to next target. * @brief Cycles to next target.
*/ */
void player_targetNext(void) { void player_targetNext(void) {
player_target = pilot_getNextID(player_target); player->target = pilot_getNextID(player->target);
if(player_target != PLAYER_ID) if(player->target != PLAYER_ID)
player_playSound(snd_target, 1); player_playSound(snd_target, 1);
} }
@ -1965,10 +1965,10 @@ void player_targetNext(void) {
void player_targetNearest(void) { void player_targetNearest(void) {
unsigned int t; unsigned int t;
t = player_target; t = player->target;
player_target = pilot_getNearestPilot(player); player->target = pilot_getNearestPilot(player);
if((player_target != PLAYER_ID) && (t != player_target)) if((player->target != PLAYER_ID) && (t != player->target))
player_playSound(snd_target, 1); player_playSound(snd_target, 1);
} }

View File

@ -41,9 +41,6 @@ extern double gui_xoff, gui_yoff;
extern Pilot** pilot_stack; extern Pilot** pilot_stack;
extern int pilot_nstack; extern int pilot_nstack;
/* Player stuff. */
extern unsigned int player_target;
/* Ai stuff. */ /* Ai stuff. */
/**< Triggers the 'attacked' function in the ai. */ /**< Triggers the 'attacked' function in the ai. */
extern void ai_attacked(Pilot* attacked, const unsigned int attacker); extern void ai_attacked(Pilot* attacked, const unsigned int attacker);
@ -598,7 +595,7 @@ static void weapon_update(Weapon* w, const double dt, WeaponLayer layer) {
static void weapon_hit(Weapon* w, Pilot* p, WeaponLayer layer, Vec2* pos) { static void weapon_hit(Weapon* w, Pilot* p, WeaponLayer layer, Vec2* pos) {
/* Someone should let the ai know it's been attacked. */ /* Someone should let the ai know it's been attacked. */
if(!pilot_isPlayer(p)) { if(!pilot_isPlayer(p)) {
if((player_target == p->id) || (RNGF() > 0.33)) { /* 33% chance. */ if((player->target == p->id) || (RNGF() > 0.33)) { /* 33% chance. */
if((w->parent == PLAYER_ID) && if((w->parent == PLAYER_ID) &&
(!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.5))) { /* 50% chance. */ (!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.5))) { /* 50% chance. */
faction_modPlayer(p->faction, -1); /* Slowly lower faction. */ faction_modPlayer(p->faction, -1); /* Slowly lower faction. */
@ -637,7 +634,7 @@ static void weapon_hitBeam(Weapon* w, Pilot* p, WeaponLayer layer,
/* Inform the ai it has been attacked, useless if player. */ /* Inform the ai it has been attacked, useless if player. */
if(!pilot_isPlayer(p)) { if(!pilot_isPlayer(p)) {
if((player_target == p->id) || (RNGF() < 0.30*dt)) { /* 30% chance per second. */ if((player->target == p->id) || (RNGF() < 0.30*dt)) { /* 30% chance per second. */
if((w->parent == PLAYER_ID) && if((w->parent == PLAYER_ID) &&
(!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.5))) { /* 50% chance. */ (!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.5))) { /* 50% chance. */
faction_modPlayer(p->faction, -1); /* Slowly lower faction. */ faction_modPlayer(p->faction, -1); /* Slowly lower faction. */