From dd9bbfaa3bc1b64d32751136fe8119178ef96e5f Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Fri, 27 Dec 2013 23:35:19 +0000
Subject: [PATCH] [Add] First Lua template for real escorts.

---
 scripts/ai/escort_player.lua |  21 +------
 scripts/ai/tpl/escort.lua    | 104 +++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+), 19 deletions(-)
 create mode 100644 scripts/ai/tpl/escort.lua

diff --git a/scripts/ai/escort_player.lua b/scripts/ai/escort_player.lua
index 1b7fac3..25406e4 100644
--- a/scripts/ai/escort_player.lua
+++ b/scripts/ai/escort_player.lua
@@ -1,4 +1,4 @@
-include("../scripts/ai/tpl/generic.lua")
+include("../scripts/ai/tpl/escort.lua")
 
 -- Settings
 armour_run    = 40
@@ -6,23 +6,6 @@ armour_return = 70
 aggressive    = true
 
 function create()
-  -- Escorts have nothing on them.
-end
-
--- When pilot is idle she'll escort the player.
-function idle()
-  ai.pushtask(0, "escort", ai.getPlayer())
-end
-
--- Escort the target.
-function escort()
-  target = ai.targetid()
-
-  dir = ai.face(target)
-  dist = ai.dist(ai.pos(target))
-
-  if dir < 10 and dist > 200 then
-    ai.accel()
-  end
+  mem.escort = ai.getPlayer()
 end
 
diff --git a/scripts/ai/tpl/escort.lua b/scripts/ai/tpl/escort.lua
new file mode 100644
index 0000000..da1815d
--- /dev/null
+++ b/scripts/ai/tpl/escort.lua
@@ -0,0 +1,104 @@
+include("../scripts/ai/tpl/generic.lua") -- Simple create function.
+
+function create()
+  attack_choose()
+end
+
+-- Just tries to guard mem.escort.
+function idle()
+  ai.pushtask(0, "escort")
+end
+
+-- Escorts the target.
+function escort()
+  target = mem.escort
+
+  -- Will just float without a target to escort.
+  if target == nil then
+    return
+  end
+
+  dir = ai.face(target)
+  dist = ai.dist(ai.pos(target))
+  bdist = ai.mindbrakedist()
+
+  -- Brake.
+  if not ai.isstopped() and dist < bdist then
+    ai.pushtask(0, "brake")
+
+  -- Must approach.
+  elseif dir < 10 and dist > 200 then
+    ai.accel()
+  end
+end
+
+-- Just brakes.
+function brake()
+  ai.brake()
+  if ai.isstopped() then
+    ai.poptask()
+  end
+end
+
+-- Holds position.
+function hold()
+  if not ai.isstopped() then
+    ai.brake()
+  end
+end
+
+-- Tries to fly back to carrier.
+function flyback()
+  target = mem.escort
+
+  dir = ai.face(target)
+  dist = ai.dist(ai.pos(target))
+  bdist = ai.minbrakedist()
+
+  -- Try to brake.
+  if not ai.isstopped() and dist < bdist then
+    ai.pushtask(0, "brake")
+
+  -- Try to dock.
+  elseif ai.isstopped() and dist < 30 then
+    ai.dock(target)
+
+  -- Far away, must approach.
+  elseif dir < 10 and dist > 200 then
+    ai.accel()
+  end
+end
+
+--[[
+--  Escort commands.
+--]]
+--Attack target.
+function e_attack(target)
+  if mem.command then
+    ai.pushtask(0, "attack", target)
+  end
+end
+
+-- Hold position.
+function e_hold()
+  if mem.command then
+    ai.pushtask(0, "hold")
+  end
+end
+
+-- Return to carrier.
+function e_return()
+  if mem.command and mem.carrier then
+    ai.pushtask(0, "flyback")
+  end
+end
+
+-- Clear orders.
+function e_clear()
+  if mem.command then
+    while ai.taskname ~= "none" do
+      ai.poptask()
+    end
+  end
+end
+