From 9a31850ce708c48fda9046b0264989c00ea457d7 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Wed, 11 Dec 2013 22:57:48 +0000
Subject: [PATCH] [Add] Actually chance system faction on planet
 addition/removal.

---
 src/space.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/space.c b/src/space.c
index 658638d..0a59686 100644
--- a/src/space.c
+++ b/src/space.c
@@ -88,6 +88,7 @@ static int systems_load(void);
 static StarSystem* system_parse(StarSystem* system, const xmlNodePtr parent);
 static void systemJumps(const xmlNodePtr parent);
 /* Misc. */
+static void system_setFaction(StarSystem* sys);
 static void space_renderStars(const double dt);
 static void space_addFleet(Fleet* fleet, int init);
 static PlanetClass planetclass_get(const char a);
@@ -764,6 +765,8 @@ int system_addPlanet(StarSystem* sys, char* planetname) {
   planetname_stack[spacename_nstack-1] = planet->name;
   systemname_stack[spacename_nstack-1] = sys->name;
 
+  system_setFaction(sys);
+
   return 0;
 }
 
@@ -816,6 +819,8 @@ int system_rmPlanet(StarSystem* sys, char* planetname) {
     WARN("Unable to find planet '%s' and system '%s' in planet<->system stack.",
         planetname, sys->name);
 
+  system_setFaction(sys);
+
   return 0;
 }
 
@@ -878,6 +883,7 @@ static StarSystem* system_parse(StarSystem* sys, const xmlNodePtr parent) {
 
   /* Clear memory for sane defaults. */
   memset(sys, 0, sizeof(StarSystem));
+  sys->faction = 1;
   planet  = NULL;
   size    = 0;
 
@@ -967,13 +973,24 @@ static StarSystem* system_parse(StarSystem* sys, const xmlNodePtr parent) {
 #undef MELEMENT
 
   /* Post processing. */
-  if(sys->nplanets > 0)
-    /**< @todo Make dependant on overall planet faction. */
-    sys->faction = sys->planets[0]->faction;
+  system_setFaction(sys);
 
   return 0;
 }
 
+/**
+ * @fn 
+ */
+static void system_setFaction(StarSystem* sys) {
+  int i;
+  sys->faction = -1;
+  for(i = 0; i < sys->nplanets; i++) /** @todo Handle multiple different factions. */
+    if(sys->planets[i]->faction > 0) {
+      sys->faction = sys->planets[i]->faction;
+      break;
+    }
+}
+
 /* Load the jumps into a system. */
 static void system_parseJumps(const xmlNodePtr parent) {
   int i;