-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommonActions.lua
More file actions
257 lines (203 loc) · 6.6 KB
/
commonActions.lua
File metadata and controls
257 lines (203 loc) · 6.6 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
-- Common actions that an agent might perform
commonActions = {}
-- DODGING
function commonActions.isAboutToGetHit(self)
if self.player.invin then return false end
for _, bullet in pairs(helper.getAllBullets()) do
if helper.isFacingObject(bullet, self.player) then
return true
end
end
return false
end
function commonActions.dodge(self, inputs)
local bulletsFacingMe = {}
for _, bullet in ipairs(helper.getAllBullets()) do
if helper.isFacingObject(bullet, self.player) then
table.insert(bulletsFacingMe, bullet)
end
end
-- TODO - consider all of them rather than just considering closest one
local closestBullet
local minDist = 9999999
for _, bullet in ipairs(bulletsFacingMe) do
local dist = helper.getObjectDist(bullet, self.player)
if dist < minDist then
minDist = dist
closestBullet = bullet
end
end
local _, facingDiff = helper.isFacingObject(closestBullet, self.player)
local offsetAng = -math.pi / 2
if facingDiff > 0 then
offsetAng = -offsetAng
end
-- Look at the angle of the bullet and move in a perpendicular direction to avoid it
local dirDiff = closestBullet.dir - self.player.dir
if dirDiff < 0 then dirDiff = dirDiff + (2 * math.pi) end
local dirButtons = helper.dirButtons(dirDiff + offsetAng)
for k, v in pairs(dirButtons) do inputs[k] = v end
end
-- these ones are for "smart" dodging
function commonActions.closeToWall(self)
local px = self.player.x
local py = self.player.y
return ((px < 100 + 100) or (px > 1280 - 100 - 100) or (py < 100 + 100) or (py > 720 - 100 - 100))
end
function commonActions.closeToInactiveEnemy(self)
if self.player.invin then return false end
local inactivePlayers = {}
for _, plr in ipairs(state.players) do
if ((plr.dead and plr.lives > 0) or plr.invin) and plr ~= self.player then
table.insert(inactivePlayers, plr)
end
end
local minDist = 999999
local closest
for _, plr in ipairs(inactivePlayers) do
local dist = helper.getObjectDist(self.player, plr)
if dist < minDist then
minDist = dist
closest = plr
end
end
if minDist < 150 then
return true, closest
else
return false
end
end
function commonActions.shouldDodgeSmart(self)
return commonActions.isAboutToGetHit(self)
or commonActions.closeToWall(self)
or commonActions.closeToInactiveEnemy(self)
end
function commonActions.dodgeSmart(self, inputs)
if commonActions.closeToWall(self) then
-- Move away from wall
local dir
local closeToTop = self.player.y < (100 + 100)
local closeToBottom = self.player.y > (720 - 100 - 100)
if self.player.x < (100 + 100) then
if closeToTop then
dir = math.pi / 4
elseif closeToBottom then
dir = 7 * math.pi / 4
else
dir = 0
end
elseif self.player.x > (1280 - 100 - 100) then
if closeToTop then
dir = 3 * math.pi / 4
elseif closeToBottom then
dir = 5 * math.pi / 4
else
dir = math.pi
end
elseif closeToTop then
dir = math.pi / 2
elseif closeToBottom then
dir = 3 * math.pi / 2
end
local dirDiff = dir - self.player.dir
for k, v in pairs(helper.dirButtons(dirDiff)) do
inputs[k] = v
end
else
if commonActions.isAboutToGetHit(self) then
commonActions.dodge(self, inputs)
else
local isClose, plr = commonActions.closeToInactiveEnemy(self, inputs)
if isClose then
local angleToPlayer = math.atan2(plr.y - self.player.y, plr.x - self.player.x)
if angleToPlayer < 0 then angleToPlayer = angleToPlayer + (2 * math.pi) end
local diff = angleToPlayer - self.player.dir
while diff < (-math.pi) do diff = diff + (2 * math.pi) end
while diff >= (math.pi) do diff = diff - (2 * math.pi) end
if math.abs(diff) > 0.1 then
inputs[diff > 0 and "rightturn" or "leftturn"] = true
end
for k, v in pairs(helper.dirButtons(diff + math.pi)) do
inputs[k] = v
end
end
end
end
end
-- AMMO
function commonActions.goTowardsAmmo(self, inputs)
local ammo = agentHelper.getClosestDistAmmoPack(self)
if ammo == nil then return end
local angleToAmmo = math.atan2(ammo.y - self.player.y, ammo.x - self.player.x)
if angleToAmmo < 0 then angleToAmmo = angleToAmmo + (2 * math.pi) end
local diff = angleToAmmo - self.player.dir
while diff < (-math.pi) do diff = diff + (2 * math.pi) end
while diff >= (math.pi) do diff = diff - (2 * math.pi) end
if math.abs(diff) > 0.1 then
inputs[diff > 0 and "rightturn" or "leftturn"] = true
end
for k, v in pairs(helper.dirButtons(diff)) do
inputs[k] = v
end
end
-- SHOOTING
function commonActions.shootAtPlayer(self, inputs)
local player = agentHelper.getClosestDistPlayer(self)
if player == nil then return end
local angleToPlayer = math.atan2(player.y - self.player.y, player.x - self.player.x)
if angleToPlayer < 0 then angleToPlayer = angleToPlayer + (2 * math.pi) end
local distToPlayer = helper.getObjectDist(self.player, player)
local diff = angleToPlayer - self.player.dir
while diff < (-math.pi) do diff = diff + (2 * math.pi) end
while diff >= (math.pi) do diff = diff - (2 * math.pi) end
if math.abs(diff) > 0.06 then
inputs[diff > 0 and "rightturn" or "leftturn"] = true
end
if distToPlayer > 400 then
for k, v in pairs(helper.dirButtons(diff)) do
inputs[k] = v
end
elseif distToPlayer < 300 then
for k, v in pairs(helper.dirButtons(diff + math.pi)) do
inputs[k] = v
end
end
if helper.isFacingObject(self.player, player) then
inputs.shoot = true
end
end
function commonActions.shootAtPlayerLeading(self, inputs, iterations)
local player = agentHelper.getClosestDistPlayer(self)
if player == nil then return end
iterations = iterations or 2
local dist
local px, py = player.x, player.y
local dist = helper.getMagnitude(self.player.x, self.player.y, px, py)
for i = 1, iterations do
-- this is how long it will take to reach the bullet
local t = dist / objects.bullet.MOVE_SPEED
px = player.x + (player.xspeed * t)
py = player.y + (player.yspeed * t)
dist = helper.getMagnitude(self.player.x, self.player.y, px, py)
end
local angle = math.atan2(py - self.player.y, px - self.player.x)
if angle < 0 then angle = angle + (2 * math.pi) end
local diff = angle - self.player.dir
while diff < (-math.pi) do diff = diff + (2 * math.pi) end
while diff >= (math.pi) do diff = diff - (2 * math.pi) end
if math.abs(diff) > 0.06 then
inputs[diff > 0 and "rightturn" or "leftturn"] = true
end
if dist > 400 then
for k, v in pairs(helper.dirButtons(diff)) do
inputs[k] = v
end
elseif dist < 300 then
for k, v in pairs(helper.dirButtons(diff + math.pi)) do
inputs[k] = v
end
end
if helper.isFacing(self.player.x, self.player.y, self.player.dir, px, py) then
inputs.shoot = true
end
end