From 9bce55471c11ea0e856096c11ddcf9ec47f6cda8 Mon Sep 17 00:00:00 2001 From: Allanis Date: Tue, 5 Aug 2014 22:22:36 +0100 Subject: [PATCH] [Change] More robust checking of ship hostilenes. --- dat/missions/empire/collective/ec05.lua | 3 ++ dat/missions/empire/collective/ec06.lua | 12 +++++- src/comm.c | 4 +- src/gui.c | 7 +++- src/llua_pilot.c | 43 ++++++++++++++----- src/pilot.c | 56 ++++++++++++++++++++++++- src/pilot.h | 15 +++++-- src/player.c | 3 +- src/weapon.c | 4 +- 9 files changed, 121 insertions(+), 26 deletions(-) diff --git a/dat/missions/empire/collective/ec05.lua b/dat/missions/empire/collective/ec05.lua index 4a60f3e..96a6d65 100644 --- a/dat/missions/empire/collective/ec05.lua +++ b/dat/missions/empire/collective/ec05.lua @@ -195,18 +195,21 @@ function add_escorts() enter_vect:add(rnd.int(-50,50), rnd.int(-50,50)) paci = pilot.add("Empire Pacifier", "escorts_player", enter_vect) paci = paci[1] + paci:setFriendly() hook.pilot(paci, "death", "paci_dead") end if esc_lancelot1 then enter_vect:add(rnd.int(-50,50), rnd.int(-50,50)) lance1 = pilot.add("Empire Lancelot", "escort_player", enter_vect) lance1 = lance1[1] + lance1:setFriendly() hook.pilot(lance1, "death", "lance1_dead") end if esc_lancelot2 then enter_vect:add(rnd.int(-50,50), rnd.int(-50,50)) lance2 = pilot_add("Empire Lancelot", "escort_player", enter_vect) lance2 = lance[2] + lance2:setFriendly() hook.pilot(lance2, "death", "lance2_dead") end end diff --git a/dat/missions/empire/collective/ec06.lua b/dat/missions/empire/collective/ec06.lua index b5ad6fe..8634d73 100644 --- a/dat/missions/empire/collective/ec06.lua +++ b/dat/missions/empire/collective/ec06.lua @@ -106,7 +106,10 @@ function jump() -- Add pilots. for k,v in ipairs(emp_fleets) do spawn_vect:add(rnd.int(-offset, offset), rnd.int(-offset, offset)) - pilot.add(v, "def", spawn_vect) + pilots = pilot.add(v, "def", spawn_vect) + for k, v in ipairs(pilots) do + v:setFriendly() + end end -- Collective. col_fleets = {} @@ -123,6 +126,7 @@ function jump() pilots = pilot.add(v, "def", spawn_vect) col_alive = col_alive + #pilots for k,v in ipairs(pilots) do + v:setHostile() hook.pilot( v, "disable", "col_dead" ) end end @@ -148,7 +152,10 @@ function jump() -- Add pilots. for k,v in ipairs(emp_fleets) do spawn_vect:add(rnd.int(-offset,offset), rnd.int(-offset, offset)) - pilot.add(v, "def", spawn_vect) + pilots = pilot.add(v, "def", spawn_vect) + for k, v in ipairs(pilot) do + v:setFriendly() + end end -- Collective. col_fleets = {} @@ -177,6 +184,7 @@ function jump() -- Count amount created. col_alive = col_alive + #pilots for k,v in ipairs(pilots) do + v:setHostile() hook.pilot( v, "disable", "col_dead" ) end end diff --git a/src/comm.c b/src/comm.c index 704ada1..70da62c 100644 --- a/src/comm.c +++ b/src/comm.c @@ -56,7 +56,7 @@ int comm_open(unsigned int pilot) { logo = faction_logoSmall(comm_pilot->faction); name = comm_pilot->name; /* Get standing colour / text. */ - if(pilot_isFlag(comm_pilot, PILOT_HOSTILE)) { + if(pilot_isHostile(comm_pilot)) { stand = "Hostile"; c = &cHostile; } else { @@ -104,7 +104,7 @@ int comm_open(unsigned int pilot) { if(!pilot_isFlag(comm_pilot, PILOT_BRIBED) && /* Not already bribed. */ ((faction_getPlayer(comm_pilot->faction) < 0) || /* Hostile. */ - pilot_isFlag(comm_pilot, PILOT_HOSTILE))) + pilot_isHostile(comm_pilot))) window_addButton(wid, -20, 20 + 2*BUTTON_HEIGHT + 40, BUTTON_WIDTH, BUTTON_HEIGHT, "btnBribe", "Bribe", comm_bribe); else diff --git a/src/gui.c b/src/gui.c index 05715d7..cf7d766 100644 --- a/src/gui.c +++ b/src/gui.c @@ -277,8 +277,10 @@ void gui_renderTarget(double dt) { c = &cInert; else if(pilot_isFlag(p, PILOT_BRIBED)) c = &cNeutral; - else if(pilot_isFlag(p, PILOT_HOSTILE)) + else if(pilot_isHostile(p)) c = &cHostile; + else if(pilot_isFriendly(p)) + c = &cFriend; else c = faction_getColour(p->faction); @@ -744,7 +746,8 @@ static void gui_renderPilot(const Pilot* p) { if(p->id == player->target) col = &cRadar_tPilot; else if(pilot_isDisabled(p)) col = &cInert; else if(pilot_isFlag(p, PILOT_BRIBED)) col = &cNeutral; - else if(pilot_isFlag(p, PILOT_HOSTILE)) col = &cHostile; + else if(pilot_isHostile(p)) col = &cHostile; + else if(pilot_isFriendly(p)) col = &cFriend; else col = faction_getColour(p->faction); ACOLOUR(*col, 1-interference_alpha); /**< Makes it much harder to see. */ diff --git a/src/llua_pilot.c b/src/llua_pilot.c index 6f9dc08..7dbcdfa 100644 --- a/src/llua_pilot.c +++ b/src/llua_pilot.c @@ -48,17 +48,19 @@ static int pilotL_warp(lua_State* L); static int pilotL_broadcast(lua_State* L); static int pilotL_setFaction(lua_State* L); static int pilotL_setHostile(lua_State* L); +static int pilotL_setFriendly(lua_State* L); static const luaL_reg pilotL_methods[] = { - { "__eq", pilotL_eq }, - { "name", pilotL_name }, - { "alive", pilotL_alive }, - { "rename", pilotL_rename }, - { "pos", pilotL_position }, - { "vel", pilotL_velocity }, - { "warp", pilotL_warp }, - { "broadcast", pilotL_broadcast }, - { "setFaction", pilotL_setFaction }, - { "setHostile", pilotL_setHostile }, + { "__eq", pilotL_eq }, + { "name", pilotL_name }, + { "alive", pilotL_alive }, + { "rename", pilotL_rename }, + { "pos", pilotL_position }, + { "vel", pilotL_velocity }, + { "warp", pilotL_warp }, + { "broadcast", pilotL_broadcast }, + { "setFaction", pilotL_setFaction }, + { "setHostile", pilotL_setHostile }, + { "setFriendly", pilotL_setFriendly }, { 0, 0 } }; /**< Pilot metatable methods. */ @@ -657,3 +659,24 @@ static int pilotL_setHostile(lua_State* L) { return 0; } +/** + * @ingroup META_PILOT + * + * @brief Set the pilot as friendly to player. + * @luafunc setHostile() + */ +static int pilotL_setFriendly(lua_State* L) { + LuaPilot* lp; + Pilot* p; + + /* Get the pilot. */ + lp = lua_topilot(L, 1); + p = pilot_get(lp->pilot); + if(p == NULL) return 0; + + /* Remove hostile and mark as friendly. */ + pilot_setFriendly(p); + + return 0; +} + diff --git a/src/pilot.c b/src/pilot.c index 3ba3d5c..994ff91 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -201,6 +201,41 @@ Fleet* fleet_get(const char* name) { return NULL; } +/** + * @brief Check to see if pilot is hostile to the player. + * @param p Player to see if is hostile. + * @return 1 if pilot is hostile to the player. + */ +int pilot_isHostile(const Pilot* p) { + if(pilot_isFlag(p, PILOT_HOSTILE) || + areEnemies(FACTION_PLAYER, p->faction)) + return 1; + return 0; +} + +/** + * @brief Check to see if pilot is neutral to the player. + * @param p Player to see if is neutral. + * @return 1 if pilot is neutral to the player. + */ +int pilot_isNeutral(const Pilot* p) { + if(!pilot_isHostile(p) && !pilot_isFriendly(p)) + return 1; + return 0; +} + +/** + * @brief Check to see if pilot is friendly to the player. + * @param p Player to see if friendly. + * @return 1 if pilot is friendly. + */ +int pilot_isFriendly(const Pilot* p) { + if(pilot_isFlag(p, PILOT_FRIENDLY) || + areAllies(FACTION_PLAYER, p->faction)) + return 1; + return 0; +} + /* Attempt to turn the pilot to face dir. */ double pilot_face(Pilot* p, const double dir) { double diff, turn; @@ -233,8 +268,8 @@ void pilot_setHostile(Pilot* p) { } /** - * @brief Unmark a pilot as neutral to player. - * @param p Pilot to mark as neutral. + * @brief Unmark a pilot as hostile to player. + * @param p Pilot to mark as hostile. */ void pilot_rmHostile(Pilot* p) { if(pilot_isFlag(p, PILOT_HOSTILE)) { @@ -247,6 +282,23 @@ void pilot_rmHostile(Pilot* p) { } } +/** + * @brief Mark a pilot as friendly to player. + * @param p Pilot to mark as friendly to player. + */ +void pilot_setFriendly(Pilot* p) { + pilot_rmHostile(p); + pilot_setFlag(p, PILOT_FRIENDLY); +} + +/** + * @brief Unmark a pilot as friendly to player. + * @param p Pilot to mark as friendly to player. + */ +void pilot_rmFriendly(Pilot* p) { + pilot_rmFlag(p, PILOT_FRIENDLY); +} + /* Get the amount of jumps the pilot has left. */ int pilot_getJumps(const Pilot* p) { return (int)(p->fuel) / HYPERSPACE_FUEL; diff --git a/src/pilot.h b/src/pilot.h index a19b1a5..9d36cc3 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -48,8 +48,9 @@ #define PILOT_HASBEAMS (1<<10) /**< Pilot has beam weapons. */ /* Dynamic. */ #define PILOT_HOSTILE (1<<11) /**< Pilot is hostile to the player. */ -#define PILOT_COMBAT (1<<12) /**< Pilot is engaged in combat. */ -#define PILOT_AFTERBURNER (1<<13) /**< Pilot has her afterburner activated. */ +#define PILOT_FRIENDLY (1<<12) /**< Pilot is friendly to the player. */ +#define PILOT_COMBAT (1<<13) /**< Pilot is engaged in combat. */ +#define PILOT_AFTERBURNER (1<<14) /**< Pilot has her afterburner activated. */ #define PILOT_HYP_PREP (1<<15) /**< Pilot is getting ready for hyperspace. */ #define PILOT_HYP_BEGIN (1<<16) /**< Pilot is starting engines. */ #define PILOT_HYPERSPACE (1<<17) /**< Pilot is in hyperspace. */ @@ -235,8 +236,6 @@ Fleet* fleet_get(const char* name); int pilot_getJumps(const Pilot* p); /* Misc. */ -void pilot_setHostile(Pilot* p); -void pilot_rmHostile(Pilot* p); void pilot_shoot(Pilot* p, const int secondary); void pilot_shootStop(Pilot* p, const int secondary); void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter, @@ -295,6 +294,14 @@ void pilot_free(Pilot* p); int fleet_load(void); void fleet_free(void); +void pilot_setHostile(Pilot* p); +void pilot_rmHostile(Pilot* p); +void pilot_setFriendly(Pilot* p); +void pilot_rmFriendly(Pilot* p); +int pilot_isHostile(const Pilot* p); +int pilot_isNeutral(const Pilot* p); +int pilot_isFriendly(const Pilot* p); + /* Update. */ void pilots_update(double dt); void pilots_render(void); diff --git a/src/player.c b/src/player.c index fb8f0a2..80bb21a 100644 --- a/src/player.c +++ b/src/player.c @@ -1205,8 +1205,7 @@ void player_targetHostile(void) { continue; /* Normbal unbribed. */ - if(pilot_isFlag(pilot_stack[i], PILOT_HOSTILE) || - areEnemies(FACTION_PLAYER, pilot_stack[i]->faction)) { + if(pilot_isHostile(pilot_stack[i])) { td = vect_dist(&pilot_stack[i]->solid->pos, &player->solid->pos); if(!pilot_isDisabled(pilot_stack[i]) && ((tp == PLAYER_ID) || (td < d))) { d = td; diff --git a/src/weapon.c b/src/weapon.c index fac3872..a38f4f1 100644 --- a/src/weapon.c +++ b/src/weapon.c @@ -635,7 +635,7 @@ static void weapon_hit(Weapon* w, Pilot* p, WeaponLayer layer, Vec2* pos) { ((player->target == p->id) || (RNGF() > 0.33))) { /* 33% chance. */ parent = pilot_get(w->parent); if((parent != NULL) && (parent->faction == FACTION_PLAYER) && - (!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.5))) { /* 50% chance. */ + (!pilot_isHostile(p) || (RNGF() < 0.5))) { /* 50% chance. */ faction_modPlayer(p->faction, -1.); /* Slowly lower faction. */ } pilot_setHostile(p); @@ -682,7 +682,7 @@ static void weapon_hitBeam(Weapon* w, Pilot* p, WeaponLayer layer, ((player->target == p->id) || (RNGF() < 0.30*dt))) { /* 30% chance per second. */ parent = pilot_get(w->parent); if((parent != NULL) && (parent->faction == FACTION_PLAYER) && - (!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.50*dt))) { /* 50% chance. */ + (!pilot_isHostile(p) || (RNGF() < 0.50*dt))) { /* 50% chance. */ faction_modPlayer(p->faction, -1.); /* Slowly lower faction. */ } pilot_setHostile(p);