From 56d10e603bdd7b579e161b6f780905138231ec84 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sat, 9 Nov 2013 23:57:03 +0000
Subject: [PATCH] [Add] attack_closestenemy to basic.lua. Pushed the land
 function into basic.lua.

---
 scripts/ai/draktharr.lua     |   3 +-
 scripts/ai/empire.lua        |   3 +-
 scripts/ai/include/basic.lua | 106 ++++++++++++++++++++++++++++++-----
 scripts/ai/merchant.lua      |   3 +-
 4 files changed, 97 insertions(+), 18 deletions(-)

diff --git a/scripts/ai/draktharr.lua b/scripts/ai/draktharr.lua
index 0a30cb7..027dd3c 100644
--- a/scripts/ai/draktharr.lua
+++ b/scripts/ai/draktharr.lua
@@ -17,7 +17,8 @@ function control()
   if planet == nil then
    ai.pushtask(0, "hyperspace")
   else
-   ai.pushtask(0, "go", planet)
+    ai.pushtask(0, "hyperspace")
+    ai.pushtask(0, "land", planet)
   end
  end
 end
diff --git a/scripts/ai/empire.lua b/scripts/ai/empire.lua
index 8db8a34..494d9ff 100644
--- a/scripts/ai/empire.lua
+++ b/scripts/ai/empire.lua
@@ -23,7 +23,8 @@ function control()
   if planet == nil then
    ai.pushtask(0, "hyperspace")
   else
-   ai.pushtask(0, "go", planet)
+    ai.pushtask(0, "hyperspace")
+    ai.pushtask(0, "land", planet)
   end
  end
 end
diff --git a/scripts/ai/include/basic.lua b/scripts/ai/include/basic.lua
index 4f8d7b0..38f769f 100644
--- a/scripts/ai/include/basic.lua
+++ b/scripts/ai/include/basic.lua
@@ -5,11 +5,29 @@
 --  functions and such for each AI.
 --]]
 
+--[[
+--  Replaces the current target with a closer one if possible.
+--]]
+function attack_closestenemy()
+  local task = ai.taskname()
+
+  if task == "attack" then
+    local enemy = ai.getenemy()
+    local target = ai.targetid()
+
+    if enemy ~= target then
+      ai.poptask()
+      ai.pushtask(0, "attack", enemy)
+    end
+  end
+end
+
 --[[
 --  Attacks the current target, task pops when target is dead.
 --]]
-function attack()
-  target = ai.targetid()
+function attack_default()
+  local target = ai.targetid()
+  ai.hostile(target) -- Mark as hostile.
 
   -- Make sure pilot exists.
   if not ai.exists(target) then
@@ -20,15 +38,14 @@ function attack()
   ai.settarget(target)
 
   -- Get stats about enemy.
-  dist = ai.dist(ai.pos(target))   -- Get distance.
-
-  range = ai.getweaprange()
+  local dist = ai.dist(ai.pos(target))   -- Get distance.
+  local range = ai.getweaprange()
 
   -- We first bias towards range.
   if dist > range then 
-    dir = ai.face(target) -- Normal face the target.
+    local dir = ai.face(target) -- Normal face the target.
 
-    secondary, special = ai.secondary("Launcher")
+    local secondary, special = ai.secondary("Launcher")
 
     -- Shoot missiles if in range.
     if secondary == "Launcher" and
@@ -49,13 +66,13 @@ function attack()
 
   -- Close enough to melee.
   else
-    secondary, special = ai.secondary("Weapon")
-    dir = ai.aim(target) -- we aim instead of face.
+    local secondary, special = ai.secondary("Weapon")
+    local dir = ai.aim(target) -- we aim instead of face.
 
     -- Fire non-smart secondary weapons.
     if(secondary == "Launcher" and special ~= "Smart") or
-        secondary == "Weapon" then
-      if dir < 10 then -- Need good acuracy.
+        secondary == "Beam Weapon" then
+      if dir < 10 or special == "Turret" then -- Need good acuracy.
         ai.shoot(2)
       end
     end
@@ -66,19 +83,78 @@ function attack()
   end
 end
 
+--[[
+--  Set attack function to be default. If you want to override use:
+--  attack = attack_<type>
+--  Right after including this file.
+--]]
+attack = attack_default
+
+--[[
+--  Attempt to land on a planet.
+--]]
+function land()
+  local target = ai.target()
+  local dir = ai.face(target)
+  local dist = ai.dist(target)
+  local bdist = ai.minbrakedist()
+
+  -- Need to get closer.
+  if dir < 10 and dist > bdist then
+    ai.accel()
+  -- Need to start braking.
+  elseif dist < bdist then
+    ai.poptask()
+    ai.pushtask(0, "landstop", target)
+  end
+end
+
+function landstop()
+  ai.brake()
+
+  if ai.isstopped() then
+    local target = ai.target()
+    ai.stop() -- Will stop the pilot if below err vel.
+    ai.settime(0, rnd.int(8000, 15000)) -- We wait during a while.
+    ai.poptask()
+    ai.pushtask(0, "landwait", target)
+  end
+end
+
+function landwait()
+  local target = ai.target()
+  local dist = ai.dist(target)
+  
+  -- In case for some reason landed far away..
+  if dist > 50 then
+    ai.poptask()
+    ai.pushtask(0, "land", target)
+
+  elseif ai.timeup(0) then
+    ai.poptask() -- Ready to do whatever we were doing before.
+  end
+end
+
 --[[
 --  Attempts to run from the target.
 --]]
 function runaway()
-  target = ai.targetid()
+  local target = ai.targetid()
 
   if not ai.exists(target) then
     ai.poptask()
     return
   end
 
-  dir = ai.face(target, 1)
+  local dir = ai.face(target, 1)
   ai.accel()
+
+  --[[
+  --  TODO: Afterburner handling.
+  --  if ai.hasafterburner() then
+  --    ai.afterburn(true)
+  --  end
+  --]]
   if ai.hasturrets() then
     dist = ai.dist(ai.pos(target))
     if dist < ai.getweaprange() then
@@ -93,13 +169,13 @@ end
 --
 -- Will need the following in control() to work:
 --
--- task = ai.taskname()
+-- local task = ai.taskname()
 -- if task == "hyperspace" then
 --   ai.hyperspace() -- Try to hyperspace.
 -- end
 --]]
 function hyperspace()
-  dir = ai.facr(-1) -- Face away from (0,0).
+  local dir = ai.face(-1) -- Face away from (0,0).
   if(dir < 10) then
     ai.accel()
   end
diff --git a/scripts/ai/merchant.lua b/scripts/ai/merchant.lua
index f29c84c..b848c54 100644
--- a/scripts/ai/merchant.lua
+++ b/scripts/ai/merchant.lua
@@ -32,7 +32,8 @@ function control()
   if planet == nil then
    ai.pushtask(0, "hyperspace")
   else
-     ai.pushtask(0, "go", planet)
+    ai.pushtask(0, "hyperspace")
+    ai.pushtask(0, "land", planet)
   end
   end
 end