diff --git a/scripts/ai/dlf.lua b/scripts/ai/dlf.lua index 1971996..b5fbfa1 100644 --- a/scripts/ai/dlf.lua +++ b/scripts/ai/dlf.lua @@ -9,6 +9,7 @@ land_planet = false function create() ai.setcredits(ai.shipprice()/1000, ai.shipprice()/100) + attack_choose() end function taunt(target, offence) diff --git a/scripts/ai/draktharr.lua b/scripts/ai/draktharr.lua index fc92d4f..6e9a201 100644 --- a/scripts/ai/draktharr.lua +++ b/scripts/ai/draktharr.lua @@ -6,6 +6,7 @@ aggressive = true function create() ai.setcredits(rnd.int(1000, ai.shipprice()/200)) + attack_choose() end function taunt(target, offense) diff --git a/scripts/ai/empire.lua b/scripts/ai/empire.lua index ecbf107..805eba3 100644 --- a/scripts/ai/empire.lua +++ b/scripts/ai/empire.lua @@ -10,6 +10,7 @@ function create() if rnd.int(0,2)==0 then ai.broadcast("The Empire is watching") end + attack_choose() end function taunt(target, offence) diff --git a/scripts/ai/goddard.lua b/scripts/ai/goddard.lua index 0350f76..72c3c06 100644 --- a/scripts/ai/goddard.lua +++ b/scripts/ai/goddard.lua @@ -5,6 +5,7 @@ aggressive = true function create() ai.setcredits(rnd.int(300, ai.shipprice()/70)) + attack_choose() end function taunt(target, offense) diff --git a/scripts/ai/include/attack.lua b/scripts/ai/include/attack.lua index fb9f99f..19e5114 100644 --- a/scripts/ai/include/attack.lua +++ b/scripts/ai/include/attack.lua @@ -1,68 +1,39 @@ --[[ --- Generic attack functions. +-- Attack wrappers for calling the correct attack functions. --]] -function attack_think() +include("../scripts/ai/include/attack_generic.lua") +include("../scripts/ai/include/attack_bomber.lua") +--[[ +-- Wrapper for the think functions. +--]] +function attack_think() + if mem.atk_think ~= nil then + mem.atk_think() + else + atk_g_think() end --[[ --- Generic "brute force" attack. Doesn't really do anything interesting. +-- Wrapper for the attack functions. --]] function attack() - target = ai.targetid() - ai.hostile(target) -- Mark as hostile. - - -- Make sure pilot exists. - if not ai.exists(target) then - ai.poptask() - return - end - ai.settarget(target) - - -- Get stats about enemy. - dist = ai.dist(ai.pos(target)) -- Get distance. - range = ai.getweaprange() - - if dist > range then - dir = ai.face(target) -- Normal face the target. - - secondary, special, ammo = ai.secondary("Launcher") - - -- Shoot missiles if in range. - if secondary == "Launcher" and - dist < ai.getweaprange(1) then - -- More lenient with aiming. - if special == "Smart" and dir < 30 then - ai.shoot(2) - - -- Non-smart miss more. - elseif dir < 10 then - ai.shoot(2) - end - end - - -- Approach for melee. - if dir < 10 then - ai.accel() - end - - -- Close enough to melee. + if mem.atk ~= nil then + mem.atk() else - secondary, special = ai.secondary("Beam Weapon") - dir = ai.aim(target) -- We aim instead of face. - - -- Fire non-smart secondary weapons. - if(secondary == "Launcher" and special ~= "Smart") or - secondary == "Beam Weapon" then - if dir < 10 or special == "Turret" then -- Need good accuracy. - ai.shoot(2) - end - end - - if dir < 10 or ai.hasturrets() then - ai.shoot() - end + atk_g() end end +--[[ +-- Generic function to choose what attack functions match the ship best. +--]] +function attack_choose() + class = ai.shipclass() + + if class == "Bomber" then + mem.atk_think = atk_b_think + mem.atk = atk_b +end + diff --git a/scripts/ai/include/attack_bomber.lua b/scripts/ai/include/attack_bomber.lua index 224e03e..25f16b7 100644 --- a/scripts/ai/include/attack_bomber.lua +++ b/scripts/ai/include/attack_bomber.lua @@ -5,7 +5,7 @@ --[[ -- Bombers don't really thing, they lock on until target is dead. --]] -function attack_think() +function atk_b_think() -- No thinking atm. end @@ -15,7 +15,7 @@ end -- Specialized for bomber type craft. AI will try to shoot missiles and such -- until out and then they will melee. --]] -function attack() +function atk_b() target = ai.targetid() ai.hostile(target) -- Mark as hostile. diff --git a/scripts/ai/include/attack_generic.lua b/scripts/ai/include/attack_generic.lua new file mode 100644 index 0000000..fad34db --- /dev/null +++ b/scripts/ai/include/attack_generic.lua @@ -0,0 +1,36 @@ +--[[ +-- Generic attack functions. +--]] + +--[[ +-- Mainly manages targetting nearest enemy. +--]] +function atk_g_think() + if mem.atk_think ~= nil then + mem.atk_think() + else + atk_g_think() +end + +--[[ +-- Generic "brute force" attack. Doesn't really do anything interesting. +--]] +function atk_g() + if mem.atk ~= nil then + mem.atk() + else + atk_g() + end +end + +--[[ +-- Generic function to choose what attack functions match the ship best. +--]] +function attack_choose() + class = ai.shipclass() + + if class == "Bomber" then + mem.atk_think = atk_b_think + mem.atk = atk_b +end + diff --git a/scripts/ai/pirate.lua b/scripts/ai/pirate.lua index 38a30de..ee21e45 100644 --- a/scripts/ai/pirate.lua +++ b/scripts/ai/pirate.lua @@ -8,6 +8,7 @@ armour_return = 100 function create() ai.setcredits(ai.shipprice()/1000, ai.shipprice()/100) + attack_choose() end function taunt(target, offense) diff --git a/scripts/ai/tpl/generic.lua b/scripts/ai/tpl/generic.lua index 42344f6..d85a88e 100644 --- a/scripts/ai/tpl/generic.lua +++ b/scripts/ai/tpl/generic.lua @@ -91,7 +91,7 @@ function idle() end function create() - -- Empty stub. + attack_choose() end function taunt(target, offensive)