From 47a8efa9ee7d1bfcf1b9825e4633a4c58ed1fba6 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Tue, 19 Nov 2013 21:51:04 +0000
Subject: [PATCH] [Add] Pilot:alive() and Pilot:broadcast() to Lua API.

---
 scripts/ai/escort_player.lua |  2 +-
 src/llua_pilot.c             | 73 ++++++++++++++++++++++++++++++++----
 2 files changed, 67 insertions(+), 8 deletions(-)

diff --git a/scripts/ai/escort_player.lua b/scripts/ai/escort_player.lua
index a9c0a7b..1b7fac3 100644
--- a/scripts/ai/escort_player.lua
+++ b/scripts/ai/escort_player.lua
@@ -1,4 +1,4 @@
-include("../scripts/tpl/generic.lua")
+include("../scripts/ai/tpl/generic.lua")
 
 -- Settings
 armour_run    = 40
diff --git a/src/llua_pilot.c b/src/llua_pilot.c
index 8fdee1f..10d4f02 100644
--- a/src/llua_pilot.c
+++ b/src/llua_pilot.c
@@ -21,24 +21,29 @@ 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[] = {
-  { "add",          pilot_addFleet },
-  { "clear",        pilot_clear },
+  { "add",          pilot_addFleet    },
+  { "clear",        pilot_clear       },
   { "toggleSpawn",  pilot_toggleSpawn },
   { "clear",        pilot_toggleSpawn },
   { 0, 0 }
 }; /**< Pilot lua methods. */
 
+/* Pilot metatable methods. */
 static int pilotL_eq(lua_State* L);
 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_warp(lua_State* L);
+static int pilotL_broadcast(lua_State* L);
 static const luaL_reg pilotL_methods[] = {
-  { "__eq",   pilotL_eq },
-  { "name",   pilotL_name },
-  { "rename", pilotL_rename },
-  { "pos",    pilotL_position },
-  { "warp",   pilotL_warp },
+  { "__eq",       pilotL_eq         },
+  { "name",       pilotL_name       },
+  { "alive",      pilotL_alive      },
+  { "rename",     pilotL_rename     },
+  { "pos",        pilotL_position   },
+  { "warp",       pilotL_warp       },
+  { "broadcast",  pilotL_broadcast  },
   { 0, 0 }
 }; /**< Pilot metatable methods. */
 
@@ -361,6 +366,29 @@ static int pilotL_name(lua_State* L) {
   return 1;
 }
 
+/**
+ * @fn static int pilotL_alive(lua_State* L)
+ * @ingroup META_PILOT
+ *
+ * @brief bool alive(nil)
+ *
+ * Checks to see if pilot is still alive.
+ *    @return true if pilot is still alive.
+ */
+static int pilotL_alive(lua_State* L) {
+  LLUA_MIN_ARGS(1);
+  LuaPilot* lp;
+  Pilot* p;
+
+  /* Parse parameters. */
+  lp = lua_topilot(L, 1);
+  p = pilot_get(lp->pilot);
+
+  /* Check if is alive. */
+  lua_pushboolean(L, p != NULL);
+  return 1;
+}
+
 /**
  * @fn static int pilotL_rename(lua_State* L)
  *
@@ -449,3 +477,34 @@ static int pilotL_warp(lua_State* L) {
   return 0;
 }
 
+/**
+ * @fn static int pilotL_broadcast(lua_State* L)
+ * @ingroup META_PILOT
+ *
+ * @brief broadcast(string msg)
+ *
+ * Make the pilot broadcast a message.
+ *    @param msg Message to broadcast.
+ */
+static int pilotL_broadcast(lua_State* L) {
+  LLUA_MIN_ARGS(2);
+  Pilot* p;
+  LuaPilot* lp;
+  char* msg;
+
+  /* Parse arguments. */
+  lp = lua_topilot(L, 1);
+  if(lua_isstring(L, 2))
+    msg = (char*)lua_tostring(L, 2);
+  else LLUA_INVALID_PARAMETER();
+
+  /* Check to see if pilot is valid. */
+  p = pilot_get(lp->pilot);
+  if(p == NULL)
+    return 0;
+
+  /* Broadcast message. */
+  player_message("Broadcast %s> \"%s\"", p->name, msg);
+  return 0;
+}
+