Merge branch 'master' of git://github.com/BTAxis/naev into testmission
[naev.git] / docs / ai / examples / attacked.lua
blob540f18f5763ef7f60a762dc942172144d92019a1
1 --[[
2 -- Based on when pilot is hit by something, then will attempt to retaliate
3 --]]
5 -- triggered when pilot is hit by something
6 function attacked ( attacker )
7 task = ai.taskname()
8 if task ~= "attack" and task ~= "runaway" then
10 -- some taunting
11 taunt( attacker )
13 -- now pilot fights back
14 ai.pushtask(0, "attack", attacker)
16 elseif task == "attack" then
17 if ai.targetid() ~= attacker then
18 ai.pushtask(0, "attack", attacker)
19 end
20 end
21 end
23 -- taunts
24 function taunt ( target )
25 num = ai.rnd(0,4)
26 if num == 0 then msg = "You dare attack me!"
27 elseif num == 1 then msg = "You are no match for the Empire!"
28 elseif num == 2 then msg = "The Empire will have your head!"
29 elseif num == 3 then msg = "You'll regret this!"
30 end
31 if msg then ai.comm(attacker, msg) end
32 end
34 -- attacks
35 function attack ()
36 target = ai.targetid()
38 -- make sure pilot exists
39 if not ai.exists(target) then
40 ai.poptask()
41 return
42 end
44 dir = ai.face( target )
45 dist = ai.dist( ai.pos(target) )
46 second = ai.secondary()
48 if ai.secondary() == "Launcher" then
49 ai.settarget(target)
50 ai.shoot(2)
51 end
53 if dir < 10 and dist > 300 then
54 ai.accel()
55 elseif (dir < 10 or ai.hasturrets()) and dist < 300 then
56 ai.shoot()
57 end
58 end