From 7a03a6f377d6a8249e396c7534cc50f3b5db19de Mon Sep 17 00:00:00 2001 From: Allanis Date: Mon, 1 Jul 2013 17:17:14 +0100 Subject: [PATCH] [Add] Now all ai include "ai/include/basic.lua" to share attack and runaway functions. --- scripts/ai/basic.lua | 59 ------------------------------------ scripts/ai/collective.lua | 27 ++--------------- scripts/ai/empire.lua | 27 ++--------------- scripts/ai/include/basic.lua | 57 ++++++++++++++++++++++++++++++++++ scripts/ai/merchant.lua | 23 ++------------ scripts/ai/militia.lua | 42 ++----------------------- scripts/ai/pirate.lua | 40 ++---------------------- 7 files changed, 67 insertions(+), 208 deletions(-) delete mode 100644 scripts/ai/basic.lua create mode 100644 scripts/ai/include/basic.lua diff --git a/scripts/ai/basic.lua b/scripts/ai/basic.lua deleted file mode 100644 index 263eddc..0000000 --- a/scripts/ai/basic.lua +++ /dev/null @@ -1,59 +0,0 @@ --- Required control rate. -control_rate = 2 - --- Required "control" function. -function control() - if taskname() == "none" then - pushtask(0, "fly") - end -end - --- Required "attacked" function. -function attacked(attacker) - if taskname() ~= "attack" and task ~= "runaway" then - -- Let's have some taunts. - if attacker == player then - local msg = rng(0,4) - if msg == 0 then say("You will never kill me!") - elseif msg == 1 then say("DIE!") - elseif msg == 2 then say("You won't survive!") - elseif msg == 3 then say("I hate you!") - end - end - pushtask(0, "attack", attacker) - end -end - --- Runs away. -function runaway() - local target = gettargetid() - local dir = face(target, 1) - accel() -end - --- Attack -function attack() - local target = gettargetid() - local dir = face(target) - local dist = getdist(getpos(target)) - - if parmour() < 70 then - poptask() - pushtask(0, "runaway", target) - elseif dir < 10 and dist > 300 then - accel() - elseif dir < 10 and dist < 300 then - shoot() - end -end - --- Fly to the player. -function fly() - local target = 0 - local dir = face(target) - local dist = getdist(getpos(target)) - if dir < 10 and dist > 300 then - accel() - end -end - diff --git a/scripts/ai/collective.lua b/scripts/ai/collective.lua index 2be053d..f9cba4d 100644 --- a/scripts/ai/collective.lua +++ b/scripts/ai/collective.lua @@ -1,3 +1,5 @@ +include("ai/include/basic.lua" + -- Required control rate. control_rate = 2 @@ -30,28 +32,3 @@ end function create() end -function attack() - target = ai.targetid() - - -- Make sure pilot exists. - if not ai.exists(target) then - ai.poptask() - return - end - - dir = ai.face(target) - dist = ai.dist(ai.pos(target)) - second = ai.secondary() - - if ai.secondary() == "Launcher" then - ai.settarget(target) - is.shoot(2) - end - - if dir < 10 and dist > 200 then - ai.accel() - elseif dir < 10 and dist < 200 then - ai.shoot() - end -end - diff --git a/scripts/ai/empire.lua b/scripts/ai/empire.lua index 7cfe3d7..a9d96f7 100644 --- a/scripts/ai/empire.lua +++ b/scripts/ai/empire.lua @@ -1,3 +1,5 @@ +include("ai/include/basic.lua") + -- Required control rate control_rate = 2 @@ -52,31 +54,6 @@ function taunt(target) if msg then ai.comm(attacker, msg) end end -function attack() - target = ai.targetid() - - -- Make sure pilot exists. - if not ai.exists(target) then - ai.poptask() - return - end - - dir = ai.face(target) - dist = ai.dist(ai.pos(target)) - second = ai.secondary() - - if ai.secondary() == "Launcher" then - ai.settarget(target) - ai.shoot(2) - end - - if dir < 10 and dist > 300 then - ai.accel() - elseif(dir < 10 or ai.hasturrets()) and dist < 300 then - ai.shoot() - end -end - function go() target = ai.target() dir = ai.face(target) diff --git a/scripts/ai/include/basic.lua b/scripts/ai/include/basic.lua new file mode 100644 index 0000000..8055a1f --- /dev/null +++ b/scripts/ai/include/basic.lua @@ -0,0 +1,57 @@ +[[ + Basic tasks for a pilot, no need to reinvent the wheel with these. + + The idea is to have it all here and only really work on the "control" + functions and such for each AI. +]] + +[[ + Attacks the current target, task pops when target is dead. +]] +function attack() + target = ai.targetid() + + -- Make sure pilot exists. + if not ai.exists(target) then + ai.poptask() + return + end + + dir = ai.face(target) + dist = ai.dist(ai.pos(target)) + second = ai.secondary() + + if ai.secondary() == "Launcher" then + ai.settarget(target) + ai.shoot(2) + end + + if dir < 10 and dist > 300 then + ai.accel() + elseif(dir < 10 or ai.hasturrets()) and dist < 300 then + ai.shoot() + end +end + +[[ + Attempts to run from the target. +]] +function runaway() + target = ai.targetid() + + if not ai.exists(target) then + ai.poptask() + return + end + + dir = ai.face(target, 1) + ai.accel() + if ai.hasturrets() then + dist = ai.dist(ai.pos(target)) + if dist < 300 then + ai.settarget(target) + ai.shoot() + end + end +end + diff --git a/scripts/ai/merchant.lua b/scripts/ai/merchant.lua index 4148668..ed3a228 100644 --- a/scripts/ai/merchant.lua +++ b/scripts/ai/merchant.lua @@ -1,3 +1,5 @@ +include("ai/include/basic.lua") + -- Required control rate. control_rate = 2 @@ -58,27 +60,6 @@ function create() ai.setcargo(cargo, rnd.int(0, ai.cargofree())) end --- Runs away. -function runaway() - target = ai.targetid() - - if not ai.exists(target) then - ai.pushtask() - ai.pushtask(0, "hyperspace") - return - end - - dir = ai.face(target, 1) - ai.accel() - if ai.hasturrets() then - dist = ai.dist(ai.pos(target)) - if dist < 300 then - ai.settarget(target) - ai.shoot() - end - end -end - -- Fly to the target. function go() target = ai.target() diff --git a/scripts/ai/militia.lua b/scripts/ai/militia.lua index 3234695..557c678 100644 --- a/scripts/ai/militia.lua +++ b/scripts/ai/militia.lua @@ -1,3 +1,5 @@ +include("ai/include/basic.lua") + -- Required control rate control_rate = 2 @@ -49,46 +51,6 @@ function taunt(target) if msg then ai.comm(attacker, msg) end end -function runaway() - target = ai.targetid() - - -- Make sure pilot exists. - if not ai.exists(target) then - ai.poptask() - return - end - - dir = ai.face(target, 1) - ai.accel() -end - -function attack() - target = ai.targetid() - - if not ai.exists(target) then - ai.poptask() - return - end - - dir = ai.face(target) - dist = ai.dist(ai.pos(target)) - second = ai.secondary() - - if ai.secondary() == "Launcher" then - ai.settarget(target) - ai.shoot(2) - end - - if ai.parmour() < 70 then - ai.poptask() - ai.pushtask(0, "runaway", target) - elseif dir < 10 and dist > 300 then - ai.accel() - elseif dir < 10 and dist < 300 then - ai.shoot() - end -end - function scan() target = ai.targetid() if not ai.exists(target) then diff --git a/scripts/ai/pirate.lua b/scripts/ai/pirate.lua index 96e8de0..2d1d908 100644 --- a/scripts/ai/pirate.lua +++ b/scripts/ai/pirate.lua @@ -1,3 +1,5 @@ +include("ai/include/basic.lua") + --Required control rate. control_rate = 2 @@ -72,44 +74,6 @@ function taunt(target) if msg then ai.comm(target, msg) end end --- Run away from the target. -function runaway() - target = ai.targetid() - - -- Ensure target exists. - if not ai.exists(target) then - ai.poptask() - return - end - - dir = ai.face(target, 1) - ai.accel() -end - --- Attack the target. -function attack() - target = ai.targetid() - - -- Ensure target exists. - if not ai.exists(target) then - ai.poptask() - return - end - - dir = ai.face(target) - dist = ai.dist(ai.pos(target)) - - -- We need to know when to run away. - if ai.parmour() < 70 then - ai.pushtask(0, "runaway", target) - -- Try to obliterate the target. - elseif dir < 10 and dist > 300 then - ai.accel() - elseif dir < 10 and dist < 300 then - ai.shoot() - end -end - -- Fly to the player. Pointless until hyperspace is implemented. function fly() target = player