-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_strategy.lua
347 lines (285 loc) · 8.57 KB
/
test_strategy.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
function print_table(table)
for key, value in ipairs(table) do
print(key, value)
end
end
-- vec CLASS
vec = {}
function vec:new(x, y, type)
local obj = {
_x = x,
_y = y
}
setmetatable(obj, self)
self.__index = self
return obj
end
function vec:add(v)
return vec:new(v._x + self._x, v._y + self._y)
end
function vec:sub(v)
return vec:new(self._x - v._x, self._y - v._y)
end
function vec:x()
return self._x
end
function vec:y()
return self._y
end
function vec:neg()
return vec:new(-self._x, -self._y)
end
function vec:distance(pos1, pos2)
local resta = pos1:sub(pos2)
return math.sqrt(resta:x()^2 + resta:y()^2)
end
-- entity CLASS
entity = {}
function entity:new(x, y, type, health)
local obj = {
_x = x,
_y = y,
_type = type,
_health = health
}
setmetatable(obj, self)
self.__index = self
-- Return the instance
return obj
end
function entity:pos()
return vec:new(self._x, self._y)
end
function entity:move(vec)
local norm = math.sqrt(vec.x()^2 + vec.y()^2)
local unit_vec = {vec.x()/norm, vec.y()/norm}
self._x = unit_vec[1]
self._y = unit_vec[2]
end
function entity:health()
return self._health
end
function entity:type()
return self._type
end
function entity:repr()
print(
'x:', self._x,
'y:', self._y ,
'type:', self._type,
'health:', self._health
)
end
player1 = entity:new(2, 2, 'player', 10)
player2 = entity:new(3, 5, 'player', 10)
player3 = entity:new(1, 5, 'player', 10)
player4 = entity:new(0, 0, 'player', 10)
local global_players = {
player1, player2, player3, player4
}
-- hay que definirlo despues porque es imbecil
function entity:visible()
return global_players
end
-- STRATEGY !!!!!!!!!
-- Global variables
local target = nil
local cooldowns = {0, 0, 0}
--Allies
local allies = {}
local bullets = {}
function rotationMatrix(angle)
local cos = math.cos(angle)
local sin = math.sin(angle)
return {
{cos, -sin},
{sin, cos}
}
end
Vec2 = {}
function Vec2:new(position, trajectory)
local obj = {}
obj.position = position
obj._trajectory = trajectory
setmetatable(obj, self)
self.__index = self
return obj
end
-- hacer que Vec2 se comporte como entity
function Vec2:pos()
return self.position
end
function Vec2:trajectory()
return self._trajectory
end
-- Define a method to calculate the perpendicular line passing through a point
function Vec2:perpendicular(point)
local dx = self:trajectory():y()
local dy = -self:trajectory():x()
local directionVector = vec:new(dx, dy)
return Vec2:new(point, directionVector)
end
-- Calculate the distance between two points
function pointDistance(p1, p2)
local dx = p2:x() - p1:x()
local dy = p2:y() - p1:y()
return math.sqrt(dx^2 + dy^2)
end
-- Define a method to calculate the intersection point of two lines
function intersection(line1, line2)
local x1, y1 = line1:pos():x(), line1:pos():y()
local x2, y2 = line2:pos():x(), line2:pos():y()
local dx1, dy1 = line1:trajectory():x(), line1:trajectory():y()
local dx2, dy2 = line2:trajectory():x(), line2:trajectory():y()
local det = dx1 * dy2 - dx2 * dy1
if det == 0 then
return nil -- The lines are parallel
end
local t1 = (dy2 * (x1 - x2) - dx2 * (y1 - y2)) / det
local t2 = (dy1 * (x1 - x2) - dx1 * (y1 - y2)) / det
local x = x1 + dx1 * t1
local y = y1 + dy1 * t1
return vec:new(x, y)
end
function furtherAlongLine(bullet, intersectionPoint) -- returns true if the bullet is further along the line than the intersection point
local aux_vec = bullet:pos():sub(intersectionPoint)
local dx = bullet:trajectory():x() / aux_vec.x()
local dy = bullet:trajectory():y() / aux_vec.y()
if dx == dy and dx > 0 then
return true
else
return false
end
end
-- Function to check for intersection between a circle and a line
-- Returns true if there is an intersection, false otherwise
function intersectionCircle(line, circleCenter)
-- Check the closest point to the circle center
local closestPoint = intersection(line, line:perpendicular(circleCenter))
if pointDistance(closestPoint, circleCenter) <= 0.5 then
return true
else
return false
end
end
function bulletTooFar(me, bulletPosition, bulletTrajectory)
local mePosition = me:pos()
local perpendicular = bulletTrajectory:perpendicular(mePosition)
local intersectionPoint = intersection(bulletTrajectory, perpendicular)
if pointDistance(bulletPosition, intersectionPoint) < 4 * pointDistance(mePosition, intersectionPoint) then
return true
else
return false
end
end
function bulletPast(me, bulletPosition, bulletTrajectory)
local mePosition = me:pos()
local perpendicular = bulletTrajectory:perpendicular(mePosition)
local intersectionPoint = intersection(bulletTrajectory, perpendicular)
if fartherAlongLine(bulletPosition, intersectionPoint) then
return true
else
return false
end
end
function purgeBullets(me)
for i = 1, #bullets do
if bulletTooFar(me, bullets[i].position, bullets[i].trajectory) or bulletPast(me, bullets[i].position, bullets[i].trajectory) then
table.remove(bullets, i)
end
end
end
function checkViablePosition(position) -- returns true if no intersect with bullet path, false otherwise
purgeBullets()
local changePosition = false
for i = 1, #bullets do
if intersectionCircle(bullets[i].trajectory, position) then
return false
end
end
return true
end
function normalize_vector(vector)
local norm = math.sqrt(vector:x()^2 + vector:y()^2)
return vec:new(vector:x()/norm, vector:y()/norm)
end
step = 5
function tryMove(me, vector_dir) --given the objective position, goes there if possible, else the nearest place
local norm_dir_vec = normalize_vector(vector_dir)
local objectivePosition = vec:new(me:pos():x() + norm_dir_vec.x()*step, me:pos():y() + norm_dir_vec.y()*step)
if checkViablePosition() then
me:move(objectivePosition)
else
for i = 1, 180, 5 do
for j = -1, 1, 2 do
-- make the rotation matrix
local angle = math.rad(j*i) -- rotate one degree to the right
local rotation = rotationMatrix(angle)
local rotatedVector = {
rotation[1][1] * norm_dir_vec[1] + rotation[1][2] * norm_dir_vec[2],
rotation[2][1] * norm_dir_vec[1] + rotation[2][2] * norm_dir_vec[2]
}
if checkViablePosition(rotatedVector) then
me:move(rotatedVector:sub(me:pos()))
break
end
end
end
end
end
-- Initialize bot
function bot_init(me)
local position
end
-- Main bot function
function bot_main(me)
local me_pos = me:pos()
-- Update cooldowns
for i = 1, 3 do
if cooldowns[i] > 0 then
cooldowns[i] = cooldowns[i] - 1
end
end
-- Attack logic
local closest_enemy = nil
local min_distance = math.huge
for _, player in ipairs(me:visible()) do
local dist = vec:distance(me_pos, player:pos())
if dist < min_distance then
min_distance = dist
local attack = true
for i, ally in ipairs(allies) do
if player:id() == ally:id() then
attack = false
end
end
if attack then
closest_enemy = player
end
end
end
-- If enemy is within range, melee, otherwise, projectile
-- Set target to closest visible enemy
-- local target = closest_enemy
-- if target then
-- local direction = {target:pos()}
-- -- If target is within melee range and melee attack is not on cooldown, use melee attack
-- if min_distance <= 2 and cooldowns[3] == 0 then
-- me:cast(2, direction)
-- cooldowns[3] = 50
-- -- If target is not within melee range and projectile is not on cooldown, use projectile
-- elseif cooldowns[1] == 0 then
-- me:cast(0, direction)
-- cooldowns[1] = 1
-- end
-- -- Move towards the center
-- local direction = vec:new(250, 250)
-- me:move(direction)
-- end
if checkViablePosition(me_pos) then
local diraction = vec:new(0,0)
else
local direction = vec:new(1,1)
end
end
bot_main(player1)