From eee760f4119414c08afaa646daf49a7b8275db5d Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Thu, 23 Jan 2014 23:05:05 +0000
Subject: [PATCH] [Fix] Fixed major bugs in flag usage and also weapon_hit
 stuff.

---
 src/pilot.h  | 17 +++++++++--------
 src/player.c | 32 ++++++++++++++++++++------------
 src/player.h |  6 +++---
 src/space.h  |  6 +++---
 src/weapon.c | 10 ++++++----
 5 files changed, 41 insertions(+), 30 deletions(-)

diff --git a/src/pilot.h b/src/pilot.h
index 606ac70..a2b22d5 100644
--- a/src/pilot.h
+++ b/src/pilot.h
@@ -35,9 +35,9 @@
 #define PILOT_HOOK_JUMP           4     /**< Pilot jumped. */
 
 /* Flags. */
-#define pilot_isFlag(p,f)   (p->flags & (f))  /**< Check if flag f is set on pilot p. */
-#define pilot_setFlag(p,f)  (p->flags |= (f)) /**< Set flag f on pilot p. */
-#define pilot_rmFlag(p,f)   (p->flags ^= (f)) /**< Remove flag f on pilot p. */
+#define pilot_isFlag(p,f)   (p->flags & (f))      /**< Check if flag f is set on pilot p. */
+#define pilot_setFlag(p,f)  (p->flags |= (f))     /**< Set flag f on pilot p. */
+#define pilot_rmFlag(p,f)   ((p)->flags &= ~(f))  /**< Remove flag f on pilot p. */
 /* Creation. */
 #define PILOT_PLAYER        (1<<0)    /**< Pilot is a player. */
 #define PILOT_ESCORT        (1<<1)    /**< Pilot is an escort. */
@@ -54,11 +54,12 @@
 #define PILOT_HYP_BEGIN     (1<<16)   /**< Pilot is starting engines. */
 #define PILOT_HYPERSPACE    (1<<17)   /**< Pilot is in hyperspace. */
 #define PILOT_BOARDED       (1<<18)   /**< Pilot has been boarded already! */
-#define PILOT_DISABLED      (1<<19)   /**< Pilot is disabled. */
-#define PILOT_DEAD          (1<<20)   /**< Pilot is on it's death bed. */
-#define PILOT_DEATH_SOUND   (1<<21)   /**< Pilot just did death explosion. */
-#define PILOT_EXPLODED      (1<<22)   /**< Pilot did final death explosion. */
-#define PILOT_DELETE        (1<<25)   /**< Pilot will get delete asap. */
+#define PILOT_BRIBED        (1<<19)   /**< Pilot has been bribed already. */
+#define PILOT_DISABLED      (1<<16)   /**< Pilot is disabled. */
+#define PILOT_DEAD          (1<<27)   /**< Pilot is on it's death bed. */
+#define PILOT_DEATH_SOUND   (1<<28)   /**< Pilot just did death explosion. */
+#define PILOT_EXPLODED      (1<<29)   /**< Pilot did final death explosion. */
+#define PILOT_DELETE        (1<<30)   /**< Pilot will get delete asap. */
 
 /* Just makes life simpler. */
 #define pilot_isPlayer(p)   ((p)->flags & PILOT_PLAYER)   /**< Check if pilot is a player. */
diff --git a/src/player.c b/src/player.c
index d7fa324..cb994d8 100644
--- a/src/player.c
+++ b/src/player.c
@@ -811,6 +811,7 @@ void player_render(void) {
     else {
       /* There is still a pilot target. */
       if(pilot_isDisabled(p)) c = &cInert;
+      else if(pilot_isFlag(p, PILOT_BRIBED)) c = &cNeutral;
       else if(pilot_isFlag(p, PILOT_HOSTILE)) c = &cHostile;
       else c = faction_getColour(p->faction);
 
@@ -1164,18 +1165,19 @@ static void gui_renderPilot(const Pilot* p) {
   }
 
   glBegin(GL_QUADS);
-  /* Colours. */
-  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);
-  COLOUR(*col);
+    /* Colours. */
+    if(p->id == player->target) col = &cRadar_targ;
+    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 col = faction_getColour(p->faction);
+    COLOUR(*col);
 
-  /* Image. */
-  glVertex2d(MAX(x-sx, -w), MIN(y+sy,  h)); /* Top left. */
-  glVertex2d(MIN(x+sx,  w), MIN(y+sy,  h)); /* Top right. */
-  glVertex2d(MIN(x+sx,  w), MAX(y-sy, -h)); /* Bottom right. */
-  glVertex2d(MAX(x-sx, -w), MAX(y-sy, -h)); /* Bottom left. */
+    /* Image. */
+    glVertex2d(MAX(x-sx, -w), MIN(y+sy,  h)); /* Top left. */
+    glVertex2d(MIN(x+sx,  w), MIN(y+sy,  h)); /* Top right. */
+    glVertex2d(MIN(x+sx,  w), MAX(y-sy, -h)); /* Bottom right. */
+    glVertex2d(MAX(x-sx, -w), MAX(y-sy, -h)); /* Bottom left. */
   glEnd();
 }
 
@@ -1954,7 +1956,12 @@ void player_targetHostile(void) {
 
   tp = PLAYER_ID;
   d = 0;
-  for(i = 0; i < pilot_nstack; i++)
+  for(i = 0; i < pilot_nstack; i++) {
+    /* Don't get if bribed. */
+    if(pilot_isFlag(pilot_stack[i], PILOT_BRIBED))
+      continue;
+
+    /* Normbal unbribed. */
     if(pilot_isFlag(pilot_stack[i], PILOT_HOSTILE) ||
         areEnemies(FACTION_PLAYER, pilot_stack[i]->faction)) {
       td = vect_dist(&pilot_stack[i]->solid->pos, &player->solid->pos);
@@ -1963,6 +1970,7 @@ void player_targetHostile(void) {
         tp = pilot_stack[i]->id;
       }
     }
+  }
 
   if((tp != PLAYER_ID) && (tp != player->target))
     player_playSound(snd_target, 1);
diff --git a/src/player.h b/src/player.h
index 45495c9..edc3e87 100644
--- a/src/player.h
+++ b/src/player.h
@@ -17,9 +17,9 @@
 #define PLAYER_AUTONAV      (1<<17)   /**< Player has autonaviagation on. */
 
 /* Flag functions. */
-#define player_isFlag(f)  (player_flags  & f)   /**< Check for a player flag. */
-#define player_setFlag(f) if(!player_isFlag(f)) (player_flags |= f) /**< Set a player flag. */
-#define player_rmFlag(f)  if(player_isFlag(f)) (player_flags ^= f)  /**< Remove a player flag. */
+#define player_isFlag(f)  (player_flags  & (f))   /**< Check for a player flag. */
+#define player_setFlag(f) (player_flags |= (f)) /**< Set a player flag. */
+#define player_rmFlag(f)  (player_flags &= ~(f))  /**< Remove a player flag. */
 
 /* The player. */
 extern Pilot* player;               /**< The player. */
diff --git a/src/space.h b/src/space.h
index 9ec1e96..3272996 100644
--- a/src/space.h
+++ b/src/space.h
@@ -82,9 +82,9 @@ typedef struct Planet_ {
 /* Star system flags. */
 #define SYSTEM_KNOWN      (1<<0)    /**< System is known. */
 #define SYSTEM_MARKED     (1<<1)    /**< System is marked by a mission. */
-#define sys_isFlag(s,f)   ((s)->flags & (f))  /**< Check system flag. */
-#define sys_setFlag(s,f)  if(!sys_isFlag(s,f)) (s)->flags |= (f)  /**< Set a system flag. */
-#define sys_rmFlag(s,f)   if(sys_isFlag(s,f))  (s)->flags ^= (f)  /**< Remove a system flag. */
+#define sys_isFlag(s,f)   ((s)->flags & (f))    /**< Check system flag. */
+#define sys_setFlag(s,f)  ((s)->flags |= (f))   /**< Set a system flag. */
+#define sys_rmFlag(s,f)   ((s)->flags &= ~(f))  /**< Remove a system flag. */
 #define sys_isKnown(s)    sys_isFlag(s, SYSTEM_KNOWN)   /**< Check if system is known. */
 #define sys_isMarked(s)   sys_isFlag(s, SYSTEM_MARKED)  /**< Check if system is marked. */
 
diff --git a/src/weapon.c b/src/weapon.c
index 9348072..1ddf2da 100644
--- a/src/weapon.c
+++ b/src/weapon.c
@@ -597,14 +597,15 @@ 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 != NULL) &&
+    if((player != NULL) && (w->parent == player->id) &&
         ((player->target == p->id) || (RNGF() > 0.33))) { /* 33% chance. */
       parent = pilot_get(w->parent);
       if((parent->faction == FACTION_PLAYER) &&
           (!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.5))) { /* 50% chance. */
         faction_modPlayer(p->faction, -1.); /* Slowly lower faction. */
-        pilot_setFlag(p, PILOT_HOSTILE);
       }
+      pilot_setFlag(p, PILOT_HOSTILE);
+      pilot_rmFlag(p, PILOT_BRIBED);
       ai_attacked(p, w->parent);
     }
     spfx_add(outfit_spfx(w->outfit), pos->x, pos->y,
@@ -639,14 +640,15 @@ 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 != NULL) &&
+    if((player != NULL) && (w->parent == player->id) &&
           ((player->target == p->id) || (RNGF() < 0.30*dt))) { /* 30% chance per second. */
       parent = pilot_get(w->parent);
       if((parent->faction == FACTION_PLAYER) &&
           (!pilot_isFlag(p, PILOT_HOSTILE) || (RNGF() < 0.5))) { /* 50% chance. */
         faction_modPlayer(p->faction, -1.); /* Slowly lower faction. */
-        pilot_setFlag(p, PILOT_HOSTILE);
       }
+      pilot_rmFlag(p, PILOT_BRIBED);
+      pilot_setFlag(p, PILOT_HOSTILE);
       ai_attacked(p, w->parent);
     }
     if(w->lockon == -1.) { /* Code to signal create explosions. */