[Change] More robust checking of ship hostilenes.

This commit is contained in:
Allanis 2014-08-05 22:22:36 +01:00
parent c6c26f1050
commit 9bce55471c
9 changed files with 121 additions and 26 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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. */

View File

@ -48,6 +48,7 @@ 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 },
@ -59,6 +60,7 @@ static const luaL_reg pilotL_methods[] = {
{ "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;
}

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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);