From 169e9da1aa2a8e0028c9f8296dd0c2b681da149a Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Tue, 4 Mar 2014 02:12:13 +0000
Subject: [PATCH] [Add] New Lua bindings.

---
 src/llua_pilot.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++-
 src/llua_space.c | 19 +++++++++++
 2 files changed, 101 insertions(+), 1 deletion(-)

diff --git a/src/llua_pilot.c b/src/llua_pilot.c
index 0bfb842..22fcb54 100644
--- a/src/llua_pilot.c
+++ b/src/llua_pilot.c
@@ -19,10 +19,12 @@
 
 static int pilotL_createmetatable(lua_State* L);
 /* Pilots. */
+static int pilot_getPlayer(lua_State* L);
 static int pilot_addFleet(lua_State* L);
 static int pilot_clear(lua_State* L);
 static int pilot_toggleSpawn(lua_State* L);
 static const luaL_reg pilot_methods[] = {
+  { "player",       pilot_getPlayer   },
   { "add",          pilot_addFleet    },
   { "clear",        pilot_clear       },
   { "toggleSpawn",  pilot_toggleSpawn },
@@ -36,18 +38,22 @@ static int pilotL_name(lua_State* L);
 static int pilotL_alive(lua_State* L);
 static int pilotL_rename(lua_State* L);
 static int pilotL_position(lua_State* L);
+static int pilotL_velocity(lua_State* L);
 static int pilotL_warp(lua_State* L);
 static int pilotL_broadcast(lua_State* L);
 static int pilotL_setFaction(lua_State* L);
+static int pilotL_setName(lua_State* L);
 static const luaL_reg pilotL_methods[] = {
   { "__eq",       pilotL_eq         },
   { "name",       pilotL_name       },
   { "alive",      pilotL_alive      },
   { "rename",     pilotL_rename     },
   { "pos",        pilotL_position   },
+  { "vel",        pilotL_velocity   },
   { "warp",       pilotL_warp       },
   { "broadcast",  pilotL_broadcast  },
   { "setFaction", pilotL_setFaction },
+  { "setName",    pilotL_setName    },
   { 0, 0 }
 }; /**< Pilot metatable methods. */
 
@@ -171,6 +177,27 @@ int lua_ispilot(lua_State* L, int ind) {
   return ret;
 }
 
+/**
+ * @ingroup PILOT
+ *
+ * @brief pilot player(nil)
+ *
+ * Get the players pilot.
+ *    @return Pilot pointing to the player.
+ */
+static int pilot_getPlayer(lua_State* L) {
+  LuaPilot lp;
+
+  if(player == NULL) {
+    lua_pushnil(L);
+    return 1;
+  }
+
+  lp.pilot = player->id;
+  lua_pushpilot(L, lp);
+  return 1;
+}
+
 /**
  * @fn static int pilot_addFleet(lua_State* L)
  * @ingroup PILOT
@@ -430,7 +457,7 @@ static int pilotL_rename(lua_State* L) {
 /**
  * @fn static int pilotL_position(lua_State* L)
  *
- * @brief Vec2 position( nil )
+ * @brief Vec2 pos( nil )
  *
  * Get the pilots position.
  *    @return The pilots current position.
@@ -454,6 +481,34 @@ static int pilotL_position(lua_State* L) {
   return 1;
 }
 
+/**
+ * @fn static int pilotL_velocity(lua_State* L)
+ * @ingroup META_PILOT
+ *
+ * @brief Vec2 vel(nil)
+ *
+ * Gets the pilots velocity.
+ *    @return The pilots current velocity.
+ */
+static int pilotL_velocity(lua_State* L) {
+  LLUA_MIN_ARGS(1);
+  LuaPilot* p1;
+  Pilot* p;
+  LuaVector v;
+
+  /* Parse parameters. */
+  p1 = lua_topilot(L, 1);
+  p = pilot_get(p1->pilot);
+
+  /* Pilot must exist.. */
+  if(p == NULL) return 0;
+
+  /* Push velocity. */
+  vectcpy(&v.vec, &p->solid->vel);
+  lua_pushvector(L, v);
+  return 1;
+}
+
 /**
  * @fn static int pilotL_warp(lua_State* L)
  * @ingroup META_PILOT
@@ -547,3 +602,29 @@ static int pilotL_setFaction(lua_State* L) {
   return 0;
 }
 
+/**
+ *
+ */
+static int pilotL_setName(lua_State* L) {
+  LLUA_MIN_ARGS(2);
+  LuaPilot* lp;
+  Pilot* p;
+  char* name;
+
+  /* Get the parameters. */
+  lp = lua_topilot(L, 1);
+  if(lua_isstring(L, 2))
+    name = (char*)lua_tostring(L, 2);
+  else LLUA_INVALID_PARAMETER();
+
+  p = pilot_get(lp->pilot);
+  if(p == NULL) return 0;
+
+  /* Set the name. */
+  if(p->name != NULL)
+    free(p->name);
+  p->name = strdup(name);
+
+  return 0;
+}
+
diff --git a/src/llua_space.c b/src/llua_space.c
index 9d2c20d..553733c 100644
--- a/src/llua_space.c
+++ b/src/llua_space.c
@@ -62,6 +62,7 @@ static int vectorL_div(lua_State* L);
 static int vectorL_get(lua_State* L);
 static int vectorL_set(lua_State* L);
 static int vectorL_distance(lua_State* L);
+static int vectorL_mod(lua_State* L);
 static const luaL_reg vector_methods[] = {
   { "new",    vectorL_new },
   { "__add",  vectorL_add },
@@ -73,6 +74,7 @@ static const luaL_reg vector_methods[] = {
   { "get",    vectorL_get },
   { "set",    vectorL_set },
   { "dist",   vectorL_distance },
+  { "mod",    vectorL_mod },
   { 0, 0 }
 }; /**< Vector metatable methods. */
 
@@ -766,3 +768,20 @@ static int vectorL_distance(lua_State* L) {
   return 1;
 }
 
+/**
+ * @ingroup META_VECTOR.
+ *
+ * @brief number mod(nil)
+ *
+ * Gets the modulus of the vector.
+ *    @return The modulus of the vector.
+ */
+static int vectorL_mod(lua_State* L) {
+  LLUA_MIN_ARGS(1);
+  LuaVector* v;
+
+  v = lua_tovector(L, 1);
+  lua_pushnumber(L, VMOD(v->vec));
+  return 1;
+}
+