Lephisto/scripts/ai/tpl/generic.lua
2014-05-18 21:43:51 +01:00

130 lines
3.1 KiB
Lua

include("../scripts/ai/include/basic.lua")
include("../scripts/ai/include/attack.lua")
--[[
-- Variables to adjust AI.
--
-- These variables can be used to adjust the generic AI to suit other roles.
--]]
armour_run = 0 -- At which damage to run at.
armour_return = 0 -- At which armour to return to combat.
shield_run = 0 -- At which shield to run.
shield_return = 0 -- At which shield to return to combat.
aggressive = false -- Should pilot actively attack enemies?
safe_distance = 300 -- Safe distance from enemies to jump.
land_planet = true -- Should land on planets?
control_rate = 2
function control()
task = ai.taskname()
enemy = ai.getenemy()
-- Get new task.
if task == "none" then
-- We'll first check enemy.
if enemy ~= nil and aggressive then
taunt(enemy, true)
ai.pushtask(0, "attack", enemy)
else
idle()
end
-- Think for attacking.
elseif task == "attack" then
target = ai.target()
-- Needs to have a target.
if target == nil then
ai.poptask()
end
-- Runaway if needed.
if(shield_run > 0 and ai.pshield() < shield_run
and ai.pshield() < ai.pshield(target)) or
(armour_run > 0 and ai.parmour() < armour_run
and ai.parmour() < ai.parmour(target)) then
ai.pushtask(0, "runaway", ai.target())
-- Think like normal.
else
attack_think()
end
-- Pilot is running away.
elseif task == "runaway" then
target = ai.target()
dist = ai.dist(target)
if aggressive and((shield_return > 0 and ai.pshield() >= shield_return) or
(armour_return > 0 and ai.parmour() >= armour_return)) then
ai.poptask() -- "attack" should be above "runaway"
-- Try to jump now.
elseif dist > safe_distance then
ai.hyperspace()
end
-- Enemy sighted, handled after running away.
elseif enemy ~= nil and aggressive then
taunt(enemy, true)
ai.pushtask(0, "attack", enemy)
-- Enter hyperspace if possible.
elseif task == "hyperspace" then
ai.hyperspace() -- Try to hyperspace.
end
end
function attacked(attacker)
task = ai.taskname()
if task ~= "attack" and task ~= "runaway" then
-- Some taunting
taunt(attacker, false)
-- Now pilot fights back.
ai.pushtask(0, "attack", attacker)
elseif task == "attack" then
if ai.target() ~= attacker then
ai.pushtask(0, "attack", attacker)
end
elseif task == "runaway" then
if ai.target() ~= attacker then
ai.poptask()
ai.pushtask(0, "runaway", attacker)
end
end
end
-- Default task to run when idle.
function idle()
planet = ai.landplanet()
-- Planet must exist.
if planet == nil or land_planet == false then
ai.settimer(0, rnd.int(1000, 3000))
ai.pushtask(0, "enterdelay")
else
mem.land = planet
ai.pushtask(0, "hyperspace")
ai.pushtask(0, "land")
end
end
-- Delays the ship when entering systems so that it doesn't leave right away.
function enterdelay()
if ai.timeup(0) then
ai.pushtask(0, "hyperspace")
end
end
function create()
attack_choose()
end
function taunt(target, offensive)
-- Empty stub.
end