From 8b0fbb8772e4fb076b02fb486ff679040b0fc767 Mon Sep 17 00:00:00 2001 From: Allanis Date: Sun, 29 Dec 2013 22:13:28 +0000 Subject: [PATCH] [Change] Moving to target on pilot structure. --- src/ai.c | 43 +++++++++++++++++++++++-------------------- src/board.c | 14 ++++++-------- src/pilot.c | 2 ++ src/pilot.h | 6 ++++++ src/player.c | 38 +++++++++++++++++++------------------- src/weapon.c | 7 ++----- 6 files changed, 58 insertions(+), 52 deletions(-) diff --git a/src/ai.c b/src/ai.c index 6ae8a80..4daa080 100644 --- a/src/ai.c +++ b/src/ai.c @@ -102,14 +102,14 @@ static int ai_accel(lua_State* L); /* Accelerate. */ static void ai_run(lua_State* L, const char* funcname); static int ai_loadProfile(char* filename); static void ai_freetask(Task* t); -static void ai_setmemory(void); +static void ai_setMemory(void); static void ai_create(Pilot* pilot, char* param); /* External C routines. */ void ai_attacked(Pilot* attacked, const unsigned int attacker); /* weapon.c */ /* C routines made external. */ -int ai_pinit(Pilot* p, char* ai); -void ai_destroy(Pilot* p); -void ai_think(Pilot* pilot); +int ai_pinit(Pilot* p, char* ai); /* pilot.c */ +void ai_destroy(Pilot* p); /* pilot.c */ +void ai_think(Pilot* pilot); /* escort.c */ /* Ai routines for Lua. */ /* 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_turn = 0.; /**< Current pilots turning. */ 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. */ #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. */ /** - * @fn static void ai_setmemory(void) + * @fn static void ai_setMemory(void) * * @brief Set the cur_pilots ai. */ -static void ai_setmemory(void) { +static void ai_setMemory(void) { lua_State* L; L = cur_pilot->ai->L; @@ -257,6 +256,16 @@ static void ai_setmemory(void) { 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) * @@ -266,9 +275,6 @@ static void ai_setmemory(void) { * @param[in] funcname Function to run. */ static void ai_run(lua_State* L, const char* funcname) { - /* Set the pilot's memory. */ - ai_setmemory(); - lua_getglobal(L, funcname); if(lua_pcall(L, 0, 0, 0)) /* Errors accured. */ @@ -492,14 +498,14 @@ void ai_exit(void) { void ai_think(Pilot* pilot) { 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. */ /* Clean up some variables. */ pilot_acc = 0.; pilot_turn = 0.; pilot_flags = 0; - pilot_target = 0; + cur_pilot->target = cur_pilot->id; /* Control function if pilot is idle or tick is up. */ if((cur_pilot->tcontrol < SDL_GetTicks()) || (cur_pilot->task == NULL)) { @@ -523,8 +529,8 @@ void ai_think(Pilot* pilot) { cur_pilot->solid->dir); /* Fire weapons if needs be. */ - if(ai_isFlag(AI_PRIMARY)) pilot_shoot(pilot, pilot_target, 0); /* Primary. */ - if(ai_isFlag(AI_SECONDARY)) pilot_shoot(pilot, pilot_target, 1); /* Secondary. */ + if(ai_isFlag(AI_PRIMARY)) pilot_shoot(pilot, cur_pilot->target, 0); /* Primary. */ + 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) { lua_State* L; - cur_pilot = attacked; + ai_setPilot(attacked); L = cur_pilot->ai->L; lua_getglobal(L, "attacked"); 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) { lua_State* L; - cur_pilot = pilot; + ai_setPilot(pilot); L = cur_pilot->ai->L; - /* Set the pilots memory. */ - ai_setmemory(); - /* Set creation mode. */ ai_status = AI_STATUS_CREATE; @@ -1179,7 +1182,7 @@ static int ai_settarget(lua_State* L) { LLUA_MIN_ARGS(1); if(lua_isnumber(L,1)) { - pilot_target = (int)lua_tonumber(L,1); + cur_pilot->target = (int)lua_tonumber(L,1); return 1; } diff --git a/src/board.c b/src/board.c index c67dd51..38172b6 100644 --- a/src/board.c +++ b/src/board.c @@ -12,8 +12,6 @@ #define BOARDING_WIDTH 300 /** Boarding window width. */ #define BOARDING_HEIGHT 200 /** boarding window height. */ -extern unsigned int player_target; - static unsigned int board_wid = 0; /** Boarding window identifier. */ static void board_exit(char* str); @@ -32,12 +30,12 @@ static void board_update(void); void player_board(void) { Pilot* p; - if(player_target == PLAYER_ID) { + if(player->target == PLAYER_ID) { player_message("You need a target to board first!"); return; } - p = pilot_get(player_target); + p = pilot_get(player->target); if(!pilot_isDisabled(p)) { player_message("You cannot board a ship that is not disabled!"); @@ -110,7 +108,7 @@ static void board_stealCreds(char* str) { (void)str; Pilot* p; - p = pilot_get(player_target); + p = pilot_get(player->target); if(p->credits == 0) { /* Can't steal from the poor. ;) */ @@ -137,7 +135,7 @@ static void board_stealCargo(char* str) { int q; Pilot* p; - p = pilot_get(player_target); + p = pilot_get(player->target); if(p->ncommodities==0) { /* No cargo. */ @@ -173,7 +171,7 @@ static void board_stealCargo(char* str) { static int board_fail(void) { Pilot* p; - p = pilot_get(player_target); + p = pilot_get(player->target); /* Fail chance. */ if(RNG(0, 100) > (int)(50. * @@ -203,7 +201,7 @@ static void board_update(void) { char cred[10]; Pilot* p; - p = pilot_get(player_target); + p = pilot_get(player->target); credits2str(cred, p->credits, 2); diff --git a/src/pilot.c b/src/pilot.c index be2442e..b68ee6b 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -1206,6 +1206,7 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, int faction, else pilot->id = ++pilot_id; /* New unique pilot id based on pilot_id, Can't be 0. */ + /* Basic information. */ pilot->ship = ship; 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; /* AI. */ + pilot->target = pilot->id; /* Self = no target. */ if(ai != NULL) ai_pinit(pilot, ai); /* Must run before ai_create. */ } diff --git a/src/pilot.h b/src/pilot.h index 803f09c..3a7d789 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -162,7 +162,13 @@ typedef struct Pilot_ { int hook_type[PILOT_HOOKS]; /**< Type of the hook atached to the pilot. */ 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. */ + unsigned int target; /**< AI target. */ AI_Profile* ai; /**< Ai personality profile. */ unsigned int tcontrol; /**< Timer for control tick. */ unsigned int timer[MAX_AI_TIMERS]; /**< Timers for AI. */ diff --git a/src/player.c b/src/player.c index 14f6705..d92ecbb 100644 --- a/src/player.c +++ b/src/player.c @@ -74,7 +74,6 @@ unsigned int player_flags = 0; /**< Player flags. */ /* Input.c */ double player_turn = 0.; /**< Turn 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 */ 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. */ void player_clear(void) { - player_target = PLAYER_ID; + if(player != NULL) + player->target = PLAYER_ID; planet_target = -1; hyperspace_target = -1; } @@ -800,10 +800,10 @@ void player_render(void) { if((player != NULL) && !player_isFlag(PLAYER_CREATING)) { /* 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; 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 { /* There is still a pilot target. */ if(pilot_isDisabled(p)) c = &cInert; @@ -900,7 +900,7 @@ void player_renderGUI(void) { /* Render the pilots. */ for(j = 0, i = 1; i < pilot_nstack; i++) { /* 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]); } /* Render the targetted pilot. */ @@ -1016,8 +1016,8 @@ void player_renderGUI(void) { } /* Target. */ - if(player_target != PLAYER_ID) { - p = pilot_get(player_target); + if(player->target != PLAYER_ID) { + p = pilot_get(player->target); /* Blit the pilot target. */ 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); /* 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_isFlag(p, PILOT_HOSTILE)) col = &cHostile; else col = faction_getColour(p->faction); @@ -1539,10 +1539,10 @@ void player_think(Pilot* pplayer) { /* PLAYER_FACE will take over navigation. */ if(player_isFlag(PLAYER_FACE)) { - if(player_target != PLAYER_ID) + if(player->target != PLAYER_ID) pilot_face(pplayer, vect_angle(&player->solid->pos, - &pilot_get(player_target)->solid->pos)); + &pilot_get(player->target)->solid->pos)); else if(planet_target != -1) pilot_face(pplayer, vect_angle(&player->solid->pos, @@ -1564,7 +1564,7 @@ void player_think(Pilot* pplayer) { /* Primary weapon. */ if(player_isFlag(PLAYER_PRIMARY)) { - pilot_shoot(pplayer, player_target, 0); + pilot_shoot(pplayer, player->target, 0); player_setFlag(PLAYER_PRIMARY_L); } else if(player_isFlag(PLAYER_PRIMARY_L)) { @@ -1580,7 +1580,7 @@ void player_think(Pilot* pplayer) { outfit_isBeam(pplayer->secondary->outfit)) { pilot_shootStop(pplayer, 1); } else - pilot_shoot(pplayer, player_target, 1); + pilot_shoot(pplayer, player->target, 1); player_setFlag(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_target = tp; + player->target = tp; } /** @@ -1951,9 +1951,9 @@ void player_targetHostile(void) { * @brief Cycles to next target. */ 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); } @@ -1965,10 +1965,10 @@ void player_targetNext(void) { void player_targetNearest(void) { unsigned int t; - t = player_target; - player_target = pilot_getNearestPilot(player); + t = player->target; + 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); } diff --git a/src/weapon.c b/src/weapon.c index 7ab629c..e792873 100644 --- a/src/weapon.c +++ b/src/weapon.c @@ -41,9 +41,6 @@ extern double gui_xoff, gui_yoff; extern Pilot** pilot_stack; extern int pilot_nstack; -/* Player stuff. */ -extern unsigned int player_target; - /* Ai stuff. */ /**< Triggers the 'attacked' function in the ai. */ 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) { /* Someone should let the ai know it's been attacked. */ 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) && (!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.5))) { /* 50% chance. */ 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. */ 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) && (!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.5))) { /* 50% chance. */ faction_modPlayer(p->faction, -1); /* Slowly lower faction. */