From d246c2cbe275c74e3fc8500588b7d55c3d61308b Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Wed, 13 Mar 2013 22:39:52 +0000
Subject: [PATCH] [Add] More thingies.. Factions!!

---
 dat/faction.xml           | 26 +++++++++++++++++---
 dat/fleet.xml             | 24 +++++++++++++++---
 dat/ssys.xml              | 23 +++++++++++++++++
 scripts/ai/collective.lua | 52 +++++++++++++++++++++++++++++++++++++++
 src/map.c                 |  3 ++-
 src/space.c               |  6 ++++-
 src/space.h               |  2 ++
 7 files changed, 127 insertions(+), 9 deletions(-)
 create mode 100644 scripts/ai/collective.lua

diff --git a/dat/faction.xml b/dat/faction.xml
index 3391bc3..2db1e22 100644
--- a/dat/faction.xml
+++ b/dat/faction.xml
@@ -10,20 +10,40 @@
   </faction>
 	<faction name = "Militia">
 	</faction>
+	<faction name = "Empire">
+	</faction>
+	<faction name = "Collective">
+	</faction>
   <Alliances>
     <alliance name = "Neutral">
       <ally>Independent</ally>
       <ally>Merchant</ally>
 			<ally>Militia</ally>
     </alliance>
+		<alliance name = "Empire United">
+			<ally>Empire</ally>
+			<ally>Merchant</ally>
+		</alliance>
   </Alliances>
   <Enemies>
     <enemies>
-      <enemy type = "alliance">Neutral</enemy>
-      <enemy type = "faction">Pirate</enemy>
-    </enemies>
+			<enemy type="faction">Collective</enemy>
+      <enemy type="alliance">Neutral</enemy>
+		</enemies>
+		<enemies>
+			<enemy type="faction">Collective</enemy>
+			<enemy type="faction">Player</enemy>
+		</enemies>
+		<enemies>
+			<enemy type="alliance">Neutral</enemy>
+			<enemy type="faction">Pirate</enemy>
+		</enemies>
     <enemies>
+			<enemy type="alliance">Empire United</enemy>
       <enemy type="faction">Pirate</enemy>
+		</enemies>
+		<enemies>
+			<enemy type="faction">Pirate</enemy>
       <enemy type="faction">Player</enemy>
     </enemies>
   </Enemies>
diff --git a/dat/fleet.xml b/dat/fleet.xml
index 082ed17..43a29de 100644
--- a/dat/fleet.xml
+++ b/dat/fleet.xml
@@ -25,10 +25,7 @@
     <ai>merchant</ai>
     <faction>Merchant</faction>
     <pilots>
-      <pilot chance='80'>Merchant Ship</pilot>
-      <pilot chance='80'>Merchant Ship</pilot>
-			<pilot chance='80'>Merchant Mule</pilot>
-			<pilot chance='80'>Merchant Mule</pilot>
+      <pilot chance='100'>Merchant Ship</pilot>
     </pilots>
   </fleet>
   <fleet name="Pirate">
@@ -40,4 +37,23 @@
      <pilot chance='80'>Leapard</pilot>
     </pilots>
   </fleet>
+	<fleet name="Collective Drone">
+		<ai>collective</ai>
+		<faction>Collective</faction>
+		<pilots>
+			<pilot chance='100'>Drone</pilot>
+		</pilots>
+	</fleet>
+	<fleet name="Collective Sml Swarm">
+		<ai>collective</ai>
+		<faction>Collective</faction>
+		<pilots>
+			<pilot chance='100'>Drone</pilot>
+			<pilot chance='90'>Drone</pilot>
+			<pilot chance='80'>Drone</pilot>
+			<pilot chance='70'>Drone</pilot>
+			<pilot chance='60'>Drone</pilot>
+			<pilot chance='50'>Drone</pilot>
+		</pilots>
+	</fleet>
 </Fleets>
diff --git a/dat/ssys.xml b/dat/ssys.xml
index f667698..dd775ed 100644
--- a/dat/ssys.xml
+++ b/dat/ssys.xml
@@ -94,6 +94,29 @@
 		</fleets>
     <jumps>
 			<jump>NCG-7291</jump>
+			<jump>C-59</jump>
+    </jumps>
+	</ssys>
+	<ssys name="C-59">
+		<pos>
+			<x>90</x>
+			<y>50</y>
+		</pos>
+		<general>
+			<stars>200</stars>
+			<asteroids>0</asteroids>
+			<interference>0</interference>
+		</general>
+		<planets>
+		</planets>
+		<fleets>
+			<fleet chance="80">Collective Drone</fleet>
+			<fleet chance="80">Collective Drone</fleet>
+			<fleet chance="80">Collective Drone</fleet>
+			<fleet chance="60">Collective Sml Swarm</fleet>
+		</fleets>
+    <jumps>
+			<jump>NCG-7292</jump>
     </jumps>
 	</ssys>
 </Systems>
diff --git a/scripts/ai/collective.lua b/scripts/ai/collective.lua
new file mode 100644
index 0000000..6bdc98a
--- /dev/null
+++ b/scripts/ai/collective.lua
@@ -0,0 +1,52 @@
+-- Required control rate.
+control_rate = 2
+
+function control()
+	if ai.taskname() == "none" then
+		enemy = ai.getenemy()
+
+		if enemey ~= 0 then
+			ai.pushtask(0, "attack", enemy)
+		end
+	end
+end
+
+-- Required "attacked" function.
+function attacked(attacker)
+	task = ai.taskname()
+	if task ~= "attack" then
+		-- Now pilot fights back.
+		ai.pushtask(0, "attack", attacker)
+	
+	elseif task == "attack" then
+		if ai.targetid() ~= attacker then
+			ai.pushtask(0, "attack", attacker)
+		end
+	end
+end
+
+function attack()
+	target = ai.targetid()
+
+	-- Make sure pilot exists.
+	if not ai.exists(target) then
+		ai.poptask()
+		return
+	end
+
+	dir = ai.face(target)
+	dist = ai.dist(ai.pos(target))
+	second = ai.secondary()
+
+	if ai.secondary() == "Launcher" then
+		ai.settarget(target)
+		is.shoot(2)
+	end
+
+	if dir < 10 and dist > 300 then
+		ai.accel()
+	elseif dir < 10 and dist < 300 then
+		ai.shoot()
+	end
+end
+
diff --git a/src/map.c b/src/map.c
index 84deca4..63e2d30 100644
--- a/src/map.c
+++ b/src/map.c
@@ -152,7 +152,8 @@ static void map_render(double bx, double by, double w, double h) {
 
 		// Draw the system.
 		if(sys == cur_system) COLOUR(cRadar_targ);
-		else if(sys->nplanets == 0) COLOUR(cInert);
+		else if(sys->nplanets==0) COLOUR(cInert); // TODO: dependant on planet type.
+		else if(areEnemies(player->faction, sys->faction)) COLOUR(cRed);
 		else COLOUR(cYellow);
 		
 		gl_drawCircleInRect(x + sys->pos.x*map_zoom,
diff --git a/src/space.c b/src/space.c
index 7389d3d..5adc680 100644
--- a/src/space.c
+++ b/src/space.c
@@ -479,7 +479,11 @@ static StarSystem* system_parse(const xmlNodePtr parent) {
   MELEMENT(flags&FLAG_ASTEROIDSSET, "asteroids"); // Can be 0.
   MELEMENT(flags&FLAG_INTEFERENCESET, "inteference");
 #undef MELEMENT
-
+	
+	// Post processing.
+	if(tmp->nplanets > 0)
+		// TODO: Make dependant on overall planet faction.
+		tmp->faction = tmp->planets[0].faction;
   return tmp;
 }
 
diff --git a/src/space.h b/src/space.h
index e3ce525..a8bf050 100644
--- a/src/space.h
+++ b/src/space.h
@@ -67,6 +67,8 @@ typedef struct StarSystem_ {
   int stars, asteroids; // Un numero!
   double interference; // Un uh.. Percentage.
 
+	Faction* faction; // Overall faction.
+
   Planet* planets; // Planets.
   int nplanets; // Total number of planets.