-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.lua
95 lines (69 loc) · 2.94 KB
/
ai.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
--[[Interspace AI routines
Copyright (C) 2012 Stephen Ward (Aukondk) aukondk.com bluedrava.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>--]]
ai = {}
--base AI for flying ship, could be used for player autopilot
function ai:newPilot(ship)
self = {}
self.ship = ship
self.target = {}
return self
end
function ai:newEnemy(ship)
self = ai:newPilot(ship)
self.update = function (dt) ai.attack(self, objects.ships.player) end
return self
end
function ai:newNeutral(ship)
self = ai:newPilot(ship)
--Initial target is object start position. Later if target is in range of position then next sequence is triggered.
self.target.x = self.ship.body:getX()
self.target.y = self.ship.body:getY()
self.update = function (dt) ai.roam(self) end
return self
end
function ai.roam(current)
-- When in range of target area, pic new one at random.
if ((current.target.x >= current.ship.body:getX()-100) and (current.target.x < current.ship.body:getX()+100)) and ((current.target.y >= current.ship.body:getY()-100) and (current.target.y < current.ship.body:getY()+100)) then
current.target.x = math.random(100, worldloop - 100)
current.target.y = math.random(100, worldloop - 100)
print(current.target.x, current.target.y)
end
ai.moveto(current.ship, current.target)
end
function ai.attack(current, target)
current.target.x = target.body:getX()
current.target.y = target.body:getY()
local distance = vector(current.target.x,current.target.y) - vector(current.ship.body:getX(),current.ship.body:getY())
if (distance:len() < 100) then
--current.shoot()
end
ai.moveto(current.ship, current.target)
end
function ai.moveto(ship, target)
local vector1 = vector(ship.body:getX(),ship.body:getY())
local vector2 = vector(target.x,target.y)
local distance = vector2 - vector1
local force = 1
local normforce = force*distance
local velx, vely = ship.body:getLinearVelocity()
local vel = math.sqrt(velx^2 + vely^2)
if ((distance:len() > 10) and (vel < 500)) then
ship.body:setLinearDamping(0) --handbrake off
ship.body:applyForce(normforce.x,normforce.y)
elseif ((distance:len() > 10) and (vel > 500)) then
ship.body:setLinearDamping(1)
--ship.body:applyForce(-normforce.x,-normforce.y)
elseif ((distance:len() <= 100)) then
ship.body:setLinearDamping(2) --handbrake on
end
end