From 9c6567195c7c4eb9308b4ec71addd530b7c3d26d Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sat, 27 Jul 2013 18:02:14 +0100
Subject: [PATCH] [Add] space.faction to get the faction of a star system.

---
 src/llua.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/src/llua.c b/src/llua.c
index 0f85c82..005e22c 100644
--- a/src/llua.c
+++ b/src/llua.c
@@ -29,12 +29,14 @@ static int space_getSystem(lua_State* L);
 static int space_landName(lua_State* L);
 static int space_systemName(lua_State* L);
 static int space_jumpDist(lua_State* L);
+static int space_faction(lua_State* L);
 static const luaL_reg space_methods[] = {
   { "getPlanet",    space_getPlanet   },
   { "getSystem",    space_getSystem   },
   { "landName",     space_landName    },
   { "system",       space_systemName  },
   { "jumpDist",     space_jumpDist    },
+  { "spaceFaction", space_faction     },
   { 0, 0 }
 };
 
@@ -288,6 +290,31 @@ static int space_jumpDist(lua_State* L) {
   return 1;
 }
 
+static int space_faction(lua_State* L) {
+  int i;
+  StarSystem* s;
+
+  /* Get the system. */
+  if(lua_isstring(L, 2))
+    s = system_get(lua_tostring(L, 1));
+  else s = cur_system;
+
+  /* Check if valid. */
+  if(s == NULL) {
+    LLUA_DEBUG("Invalid system!");
+    return 0;
+  }
+
+  /* Return the result in table. */
+  lua_newtable(L);
+  for(i = 0; i < s->nplanets; i++) {
+    lua_pushnumber(L, i+1); /* Index. */
+    lua_pushstring(L, faction_name(s->planets[i].faction)); /* Value. */
+    lua_rawset(L, -3); /* Store in table. */
+  }
+  return 1;
+}
+
 /* -- Time. -- */
 static int time_get(lua_State* L) {
   lua_pushnumber(L, ltime_get());