From 72d88644b6f57affd507d06a820083e33cc4fb79 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sun, 26 Oct 2014 18:51:42 +0000
Subject: [PATCH] [Change] Using either Fleets or FleetGroups when loading
 starsystems.

---
 src/space.c | 89 +++++++++++++++++++++++++++++++++++++++++++++--------
 src/space.h |  2 ++
 2 files changed, 79 insertions(+), 12 deletions(-)

diff --git a/src/space.c b/src/space.c
index 5f18c53..69b19a7 100644
--- a/src/space.c
+++ b/src/space.c
@@ -990,11 +990,61 @@ int system_rmFleet(StarSystem* sys, SystemFleet* fleet) {
   return 0;
 }
 
+/**
+ * @brief Add a FleetGroup to a star system.
+ *    @param sys Star System to add fleet to.
+ *    @param fltgrp FleetGroup to add.
+ *    @return 0 on success.
+ */
+int system_addFleetGroup(StarSystem* sys, FleetGroup* fltgrp) {
+  int i;
+  SystemFleet fleet;
+
+  if(sys == NULL)
+    return -1;
+
+  /* Add all the fleets. */
+  for(i = 0; i < fltgrp->nfleets; i++) {
+    fleet.fleet = fltgrp->fleets[i];
+    fleet.chance = fltgrp->chance[i];
+    if(system_addFleet(sys, &fleet))
+      return -1;
+  }
+
+  return 0;
+}
+
+/**
+ * @brief Removes a fleetgroup from a star system.
+ *    @param sys Star System to remove fleet from.
+ *    @param fltgrp FleetGroup to remove.
+ *    @return 0 on success.
+ */
+int system_rmFleetGroup(StarSystem* sys, FleetGroup* fltgrp) {
+  int i;
+  SystemFleet fleet;
+
+  if(sys == NULL)
+    return -1;
+
+  /* Add all the fleets. */
+  for(i = 0; i < fltgrp->nfleets; i++) {
+    fleet.fleet = fltgrp->fleets[i];
+    fleet.chance = fltgrp->chance[i];
+    if(system_rmFleet(sys, &fleet))
+      return -1;
+  }
+
+  return 0;
+}
+
 /* Parse node 'parent' which should be the node of a system. */
 /* Return the StarSystem fully loaded. */
 static StarSystem* system_parse(StarSystem* sys, const xmlNodePtr parent) {
   Planet* planet;
   SystemFleet fleet;
+  Fleet* flt;
+  FleetGroup* fltgrp;
   char* ptrc;
   xmlNodePtr cur, node;
   uint32_t flags;
@@ -1061,21 +1111,36 @@ static StarSystem* system_parse(StarSystem* sys, const xmlNodePtr parent) {
       cur = node->children;
       do {
         if(xml_isNode(cur, "fleet")) {
-          memset(&fleet, 0, sizeof(SystemFleet));
+          /* Load the fleet. */
+          flt = fleet_get(xml_get(cur));
+          if(flt != NULL) {
+            /* Get the fleet. */
+            fleet.fleet = flt;
 
-          fleet.fleet = fleet_get(xml_get(cur));
-          if(fleet.fleet == NULL)
-            WARN("Fleet %s for Star System %s not found", xml_get(cur), sys->name);
+            /* Get the chance. */
+            xmlr_attr(cur, "chance", ptrc); /* Mallocs ptrc */
+            fleet.chance = (ptrc == NULL) ? 0 : atoi(ptrc);
+            if(fleet.chance == 0)
+              WARN("Fleet '%s' for Star System '%s' has 0%% chance to appear",
+                  fleet.fleet->name, sys->name);
 
-          xmlr_attr(cur, "chance", ptrc); /* mallocs ptrc. */
-          if(ptrc == NULL) fleet.chance = 0; /* Gives warning. */
-          else fleet.chance = atoi(ptrc);
-          if(fleet.chance == 0)
-            WARN("Fleet %s for Star System %s has 0%% chance to appear",
-                 fleet.fleet->name, sys->name);
-          if(ptrc) free(ptrc); /* Free the ptrc. */
+            if(ptrc)
+              free(ptrc); /* Free the ptrc */
 
-          system_addFleet(sys, &fleet);
+            /* Add the fleet. */
+            system_addFleet(sys, &fleet);
+          } else {
+            /* Try to load it as a FleetGroup. */
+            fltgrp = fleet_getGroup(xml_get(cur));
+            if(fltgrp == NULL) {
+              WARN("Fleet '%s' for Star System '%s' not found",
+                  xml_get(cur), sys->name);
+              continue;
+            }
+
+            /* Add the fleetgroup. */
+            system_addFleetGroup(sys, fltgrp);
+          }
         }
       } while(xml_nextNode(cur));
     }
diff --git a/src/space.h b/src/space.h
index 6d16622..aacb53c 100644
--- a/src/space.h
+++ b/src/space.h
@@ -158,6 +158,8 @@ int system_addPlanet(StarSystem* sys, char* planetname);
 int system_rmPlanet(StarSystem* sys, char* planetname);
 int system_addFleet(StarSystem* sys, SystemFleet* fleet);
 int system_rmFleet(StarSystem* sys, SystemFleet* fleet);
+int system_addFleetGroup(StarSystem* sys, FleetGroup* fltgrp);
+int system_rmFleetGroup(StarSystem* sys, FleetGroup* fltgrp);
 
 /* Render. */
 void space_render(const double dt);