From dc3bf1292c7340935a96f9b48c7a23b284935ecb Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Tue, 31 Dec 2013 06:08:01 +0000
Subject: [PATCH] [Add] Added escort control ai Lua bindings.

---
 src/ai.c     | 43 +++++++++++++++++++++++++++++++++++++++++++
 src/escort.c | 39 ++++++++++++++++++++++++++-------------
 src/escort.h |  8 ++++----
 src/input.c  |  2 +-
 4 files changed, 74 insertions(+), 18 deletions(-)

diff --git a/src/ai.c b/src/ai.c
index 63b49b6..b07ef5c 100644
--- a/src/ai.c
+++ b/src/ai.c
@@ -62,6 +62,7 @@
 #include "rng.h"
 #include "space.h"
 #include "faction.h"
+#include "escort.h"
 #include "llua.h"
 #include "lluadef.h"
 #include "llua_space.h"
@@ -150,6 +151,11 @@ static int ai_getrndplanet(lua_State* L);       /* Vec2 getrndplanet() */
 static int ai_getlandplanet(lua_State* L);      /* Vec2 getlandplanet() */
 static int ai_hyperspace(lua_State* L);         /* [number] hyperspace() */
 static int ai_stop(lua_State* L);               /* stop() */
+/* Escorts. */
+static int ai_e_attack(lua_State* L);           /* bool e_attack() */
+static int ai_e_hold(lua_State* L);             /* bool e_hold() */
+static int ai_e_clear(lua_State* L);            /* bool e_clear() */
+static int ai_e_return(lua_State* L);           /* bool e_return() */
 static int ai_dock(lua_State* L);               /* dock(number) */
 /* Combat. */
 static int ai_combat(lua_State* L);             /* combat(number) */
@@ -208,6 +214,11 @@ static const luaL_Reg ai_methods[] = {
   { "brake",                ai_brake            },
   { "stop",                 ai_stop             },
   { "hyperspace",           ai_hyperspace       },
+  /* Escorts. */
+  { "e_attack",             ai_e_attack         },
+  { "e_hold",               ai_e_hold           },
+  { "e_clear",              ai_e_clear          },
+  { "e_return",             ai_e_return         },
   { "dock",                 ai_dock             },
   /* Combat. */
   { "aim",                  ai_aim              },
@@ -1118,6 +1129,38 @@ static int ai_stop(lua_State* L) {
   return 0;
 }
 
+/* Tell the pilots escort to attack its target. */
+static int ai_e_attack(lua_State* L) {
+  int ret;
+  ret = escorts_attack(cur_pilot);
+  lua_pushboolean(L, !ret);
+  return 1;
+}
+
+/* Tell the pilots escort to hold position. */
+static int ai_e_hold(lua_State* L) {
+  int ret;
+  ret = escorts_hold(cur_pilot);
+  lua_pushboolean(L, !ret);
+  return 1;
+}
+
+/* Tell the pilots escort to clear orders. */
+static int ai_e_clear(lua_State* L) {
+  int ret;
+  ret = escorts_clear(cur_pilot);
+  lua_pushboolean(L, !ret);
+  return 1;
+}
+
+/* Tells the pilots escorts to return to dock. */
+static int ai_e_return(lua_State* L) {
+  int ret;
+  ret = escorts_return(cur_pilot);
+  lua_pushboolean(L, !ret);
+  return 1;
+}
+
 /* Docks the ship. */
 static int ai_dock(lua_State* L) {
   Pilot* p;
diff --git a/src/escort.c b/src/escort.c
index 5babe4f..56efba3 100644
--- a/src/escort.c
+++ b/src/escort.c
@@ -135,47 +135,60 @@ static int escort_command(Pilot* parent, int cmd, int param) {
 }
 
 /**
- * @fn void escorts_attack(Pilot* parent)
+ * @fn int escorts_attack(Pilot* parent)
  *
  * @brief HAve a pilot order it's escorts to attack its target.
  *    @param parent Pilot giving the order.
  */
-void escorts_attack(Pilot* parent) {
+int escorts_attack(Pilot* parent) {
+  int ret;
+  ret = 1;
   if(parent->target != parent->id)
-    if(escort_command(parent, ESCORT_ATTACK, parent->target)==0)
-      player_message("Escorts: Attacking %s.", pilot_get(parent->target)->name);
+    ret = escort_command(parent, ESCORT_ATTACK, parent->target);
+  if((ret == 0) && (parent == player))
+    player_message("Escorts: Attacking %s.", pilot_get(parent->target)->name);
+  return ret;
 }
 
 /**
- * @fn void escorts_hold(Pilot* parent)
+ * @fn int escorts_hold(Pilot* parent)
  *
  * @brief Have a pilot order its escorts to hold position.
  *    @param parent Pilot giving the order.
  */
-void escorts_hold(Pilot* parent) {
-  if(escort_command(parent, ESCORT_HOLD, -1)==0)
+int escorts_hold(Pilot* parent) {
+  int ret;
+  ret = escort_command(parent, ESCORT_HOLD, -1);
+  if((ret == 0) && (parent == player))
     player_message("Escorts: Holding position.");
+  return ret;
 }
 
 /**
- * @fn void escorts_return(Pilot* parent)
+ * @fn int escorts_return(Pilot* parent)
  *
  * @brief Have a pilot order its escorts to dock.
  *    @param parent Pilot giving the order.
  */
-void escorts_return(Pilot* parent) {
-  if(escort_command(parent, ESCORT_RETURN, -1)==0)
+int escorts_return(Pilot* parent) {
+  int ret;
+  ret = escort_command(parent, ESCORT_RETURN, -1);
+  if((ret == 0) && (parent == player))
     player_message("Escorts: Returning to ship.");
+  return ret;
 }
 
 /**
- * @fn void escorts_clear(Pilot* parent)
+ * @fn int escorts_clear(Pilot* parent)
  * 
  * @brief Have a pilot order its escorts to clear orders.
  *    @param parent Pilot giving the order.
  */
-void escorts_clear(Pilot* parent) {
-  if(escort_command(parent, ESCORT_CLEAR, -1)==0)
+int escorts_clear(Pilot* parent) {
+  int ret;
+  ret = escort_command(parent, ESCORT_CLEAR, -1);
+  if((ret == 0) && (parent == player))
     player_message("Escorts: Clearing orders.");
+  return ret;
 }
 
diff --git a/src/escort.h b/src/escort.h
index 94ebbc6..1b379b1 100644
--- a/src/escort.h
+++ b/src/escort.h
@@ -6,8 +6,8 @@ int escort_create(unsigned int parent, char* ship,
     Vec2* pos, Vec2* vel, int carried);
 
 /* Keybind commands. */
-void escorts_attack(Pilot* parent);
-void escorts_hold(Pilot* parent);
-void escorts_return(Pilot* parent);
-void escorts_clear(Pilot* parent);
+int escorts_attack(Pilot* parent);
+int escorts_hold(Pilot* parent);
+int escorts_return(Pilot* parent);
+int escorts_clear(Pilot* parent);
 
diff --git a/src/input.c b/src/input.c
index 68d01af..609c682 100644
--- a/src/input.c
+++ b/src/input.c
@@ -84,7 +84,7 @@ void input_setDefault(void) {
 
 
   /* Secondary weapon. */
-  input_setKeybind("secondary",       KEYBIND_KEYBOARD, SDLK_LSHIFT,      KMOD_ALL,   0);
+  input_setKeybind("secondary",       KEYBIND_KEYBOARD, SDLK_LALT,        KMOD_ALL,   0);
   input_setKeybind("secondary_next",  KEYBIND_KEYBOARD, SDLK_e,           KMOD_NONE,  0);
   /* Space */
   input_setKeybind("autonav",					KEYBIND_KEYBOARD, SDLK_j,					  KMOD_LCTRL, 0);