-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.lua
367 lines (326 loc) · 14.3 KB
/
script.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
dofile("scripts/forts.lua")
dofile("scripts/core.lua")
--[[
TODO
make missiles choose untargeted projectiles to spread the damage
]]
SMARTMISSILE_SAVENAMES = {
--"rocketemp",
--"missile2",
--"rocket",
--"rocketmi24",
"HighFlight_antiair1"
}
SMARTMISSILE_CONFIG = {}
SMARTMISSILE_CONFIG["rocketemp"] = {
ignoreNeutrals = true,
ignoreProjectiles = false,
ignoreStructures = false,
predictionType = 1,
sectorRadius = 2500,
sectorWidth = 30,
}
SMARTMISSILE_CONFIG["rocketmi24"] = {
ignoreNeutrals = true,
ignoreProjectiles = false,
ignoreStructures = true,
predictionType = 1,
sectorRadius = 4000,
sectorWidth = 45,
}
SMARTMISSILE_CONFIG["missile2"] = {
ignoreNeutrals = true,
ignoreProjectiles = true,
ignoreStructures = false,
predictionType = 1,
sectorRadius = 2500,
sectorWidth = 30,
}
SMARTMISSILE_CONFIG["HighFlight_antiair1"] = {
ignoreNeutrals = false,
ignoreProjectiles = false,
ignoreStructures = false,
predictionType = 2,
sectorRadius = 7000,
sectorWidth = 30,
courseHoldDelay = 1,
}
function Load(gameStart)
data.smartMissiles = {}
--local t = GetEnemyTeams(1, false)
--for index, v in ipairs(t) do
-- Print(v)
--end
end
function OnRestart()
data.smartMissiles = {}
end
function Update(frame)
UpdateMissilesVision(frame)
end
function OnWeaponFired(teamId, saveName, weaponId, projectileNodeId, projectileNodeIdFrom)
--Print(saveName)
if AnyOf(SMARTMISSILE_SAVENAMES, GetNodeProjectileSaveName(projectileNodeId)) --[[GetNodeProjectileType(projectileNodeId) == 2]] then
local missile = {}
missile.projectileNodeId = projectileNodeId
missile.teamId = teamId
missile.saveName = GetNodeProjectileSaveName(projectileNodeId)
table.insert(data.smartMissiles, #data.smartMissiles + 1, missile)
--Print(SMARTMISSILE_CONFIG[missile.saveName].courseHoldDelay .. " : " .. #data.smartMissiles + 1)
ScheduleCall(SMARTMISSILE_CONFIG[missile.saveName].courseHoldDelay, MissileCourseHoldDelay, projectileNodeId)
end
end
function OnProjectileDestroyed(nodeId, teamId, saveName, structureIdHit, destroyType)
CleanSmartMissiles(nodeId)
end
function CleanSmartMissiles(nodeId)
for i, v in pairs(data.smartMissiles) do
if v.projectileNodeId == nodeId then
table.remove(data.smartMissiles, i)
end
end
end
function MissileCourseHoldDelay(id)
for i, v in pairs(data.smartMissiles) do
if v.projectileNodeId == id then
data.smartMissiles[i].courseHoldDelayExpired = true
end
end
end
function UpdateMissilesVision(frame)
for i, v in pairs(data.smartMissiles) do
local selfvel = NodeVelocity(v.projectileNodeId)
local anglev = Vec3Nor(selfvel)
local position = NodePosition(v.projectileNodeId)
local conf = SMARTMISSILE_CONFIG[GetNodeProjectileSaveName(v.projectileNodeId)]
local nodes = GetNodesInSector(position, anglev, conf.sectorWidth, v.teamId, conf.sectorRadius, conf.ignoreNeutrals, SMARTMISSILE_CONFIG[GetNodeProjectileSaveName(v.projectileNodeId)].ignoreProjectiles, conf.ignoreStructures)
local closestnode = ClosestOf(nodes, position)
local angle = GetVectorAngle(anglev)
local aimat
if closestnode then
aimat = nodes[closestnode]
SpawnCircle(nodes[closestnode], 50, Colour(255, 255, 100, 255), 0.1)
if conf.predictionType == 1 then
aimat = nodes[closestnode] + Vec3MultiplyScalar(NodeVelocity(closestnode), Vec3Length((nodes[closestnode] - position)) / Vec3Length(NodeVelocity(v.projectileNodeId)))
elseif conf.predictionType == 2 then
aimat = nodes[closestnode] + Vec3MultiplyScalar(NodeVelocity(closestnode), Vec3Length((nodes[closestnode] - position)) / Vec3Length(NodeVelocity(closestnode) - NodeVelocity(v.projectileNodeId)))
elseif conf.predictionType == 3 then
local acc = Vec3(0, 0)
local targetvel = NodeVelocity(closestnode)
local targetpos = nodes[closestnode]
if v.lastTargetVelocity then
acc = (targetvel - v.lastTargetVelocity) - (selfvel - v.lastSelfVelocity)
aimat = targetpos + Vec3MultiplyScalar(targetvel, Vec3Length((targetpos - position)) / Vec3Length(targetvel - selfvel - acc))
else
aimat = targetpos + Vec3MultiplyScalar(targetvel, Vec3Length((targetpos - position)) / Vec3Length(targetvel - selfvel))
end
v.lastTargetVelocity = targetvel
end
else
v.lastTargetVelocity = nil
end
for i, v in pairs(nodes) do
SpawnCircle(v, 20, Colour(255, 100, 100, 255), 0.1)
end
if aimat then
SetMissileTarget(v.projectileNodeId, aimat)
else
if v.courseHoldDelayExpired then
SetMissileTarget(v.projectileNodeId, position + Vec3MultiplyScalar(anglev, 1000000))
end
end
--SpawnCircle(position + AngleToVector(angle, Vec3Length(NodeVelocity(v.projectileNodeId))), 20,
-- Colour(255, 255, 255, 255), 0.1)
v.lastSelfVelocity = selfvel
end
end
function GetNodesInSector(position, anglev, degrees, teamOwner, radius, ignoreNeutrals, ignoreProjectiles, ignoreStructures)
--Print(GetVectorAngle(angle))
--Print(AngleToVector(angle, 5).x .. " " .. AngleToVector(angle, 5).y)
--Print(angle.x .. " " .. angle.y)
local enemyteams = GetEnemyTeams(teamOwner, ignoreNeutrals)
local angle = GetVectorAngle(anglev)
local nodes = {}
DrawCircleSector(position, angle, degrees, radius, Red(), 0.05, false)
if not CheckType(enemyteams, "number") then --dont ignore neutrals
for k, enemyteam in pairs(enemyteams) do
if not ignoreStructures then
for i = 0, NodeCount(enemyteam), 1 do
local nodeid = GetNodeId(enemyteam, i)
local nodepos = NodePosition(nodeid)
local dirNonNor = nodepos - position
--local nodeangle = GetVectorAngle(Vec3Nor(dirNonNor))
local anglebetween = AngleBetweenTwoVectors(Vec3Nor(dirNonNor), anglev)
if anglebetween < degrees and Vec3Length(dirNonNor) < radius and NodeExists(nodeid) then
nodes[nodeid] = nodepos
end
end
end
if not ignoreProjectiles then
for i = 0, ProjectileCount(enemyteam), 1 do
local nodeid = GetProjectileId(enemyteam, i)
local nodepos = NodePosition(nodeid)
local dirNonNor = nodepos - position
--local nodeangle = GetVectorAngle(Vec3Nor(dirNonNor))
local anglebetween = AngleBetweenTwoVectors(Vec3Nor(dirNonNor), anglev)
if anglebetween < degrees and Vec3Length(dirNonNor) < radius and NodeExists(nodeid) and (GetNodeProjectileType(nodeid) == 2 or GetNodeProjectileType(nodeid) == 3) then
nodes[nodeid] = nodepos
end
end
end
end
else
local enemyteam = enemyteams
if not ignoreStructures then
for i = 0, NodeCount(enemyteam), 1 do
local nodeid = GetNodeId(enemyteam, i)
local nodepos = NodePosition(nodeid)
local dirNonNor = nodepos - position
--local nodeangle = GetVectorAngle(Vec3Nor(dirNonNor))
local anglebetween = AngleBetweenTwoVectors(Vec3Nor(dirNonNor), anglev)
if anglebetween < degrees and Vec3Length(dirNonNor) < radius and NodeExists(nodeid) then
nodes[nodeid] = nodepos
end
end
end
if not ignoreProjectiles then
for i = 0, ProjectileCount(enemyteam), 1 do
local nodeid = GetProjectileId(enemyteam, i)
local nodepos = NodePosition(nodeid)
local dirNonNor = nodepos - position
--local nodeangle = GetVectorAngle(Vec3Nor(dirNonNor))
local anglebetween = AngleBetweenTwoVectors(Vec3Nor(dirNonNor), anglev)
if anglebetween < degrees and Vec3Length(dirNonNor) < radius and NodeExists(nodeid) and (GetNodeProjectileType(nodeid) == 2 or GetNodeProjectileType(nodeid) == 3) then
nodes[nodeid] = nodepos
end
end
end
end
return nodes
end
function GetEnemyTeams(teamId, ignoreNeutrals)
local teamstable = {}
if not ignoreNeutrals then
--Print("Testing: " .. teamId)
--Print("Enemy table:")
for i = 1, GetTeamCount(), 1 do
if teamId % 100 ~= GetTeamId(i) % 100 then
table.insert(teamstable, GetTeamId(i))
--Print(GetTeamId(i))
end
end
return teamstable
else
if teamId % 100 == 1 then
return 2
elseif teamId % 100 == 2 then
return 1
end
end
end
function Vec3Nor(vector)
local l = Vec3Length(vector)
local il = 1 / l
local x = vector.x * il
local y = vector.y * il
--local z = vector.z * il
return Vec3(x, y)
end
function Vec3MultiplyScalar(vector, scalar)
local x = vector.x * scalar
local y = vector.y * scalar
return Vec3(x, y)
end
--function Vec3DivideScalar(vector, scalar)
-- local x = vector.x / scalar
-- local y = vector.y / scalar
-- return Vec3(x, y)
--end
function AnyOf(table, element)
for i, v in pairs(table) do
if v == element then
return true
end
end
return false
end
function FindIn(table, element)
for i, v in pairs(table) do
if v == element then
return i
end
end
end
function ClosestOf(table, position)
local closestid
local smallestLength = math.huge
for i, v in pairs(table) do
local len = Vec3Length(position - v)
--if not smallestLength then
-- smallestLength = len
-- closestid = i
--else
if smallestLength > len then
smallestLength = len
closestid = i
end
--end
end
return closestid
end
function AngleBetweenTwoVectors(a, b)
if not b then
local b = Vec3(1, 0)
end
local ans = math.acos(Vec3Dot(a, b) / (Vec3Length(a) * Vec3Length(b)))
return math.deg(ans)
end
function GetVectorAngle(vector)
--CHECK IF X = 0
local ans = math.deg(math.atan(vector.y / vector.x))
if vector.x < 0 then
ans = ans + 180
end
if vector.y < 0 and vector.x > 0 then
ans = ans + 360
end
return ans
end
function AngleToVector(angle, radius)
if radius then
return Vec3MultiplyScalar(Vec3(math.cos(math.rad(angle)), math.sin(math.rad(angle))), radius)
else
return Vec3(math.cos(math.rad(angle)), math.sin(math.rad(angle)))
end
end
function DrawCircleSector(position, angle, degrees, radius, color, fadetime, middleLine)
local lowerangle = angle - (degrees)
local upperrangle = angle + (degrees)
SpawnLine(position, position + AngleToVector(lowerangle, radius), color, fadetime)
SpawnLine(position, position + AngleToVector(upperrangle, radius), color, fadetime)
if middleLine then
SpawnLine(position, position + AngleToVector(angle, radius), color, fadetime)
end
local vertexes = math.ceil(radius / 100)
local currentangle = lowerangle
local step = 2 * degrees / vertexes
for i = 1, vertexes, 1 do
local nextangle = currentangle + step
SpawnLine(position + AngleToVector(currentangle, radius), position + AngleToVector(nextangle, radius),
color, fadetime)
currentangle = nextangle
end
end
function FixVelocityV(vector) -- NOT NEEDED
return Vec3(vector.x, -vector.y)
end
function CheckType(object, typ)
if type(object) == typ then
return true
else
return false
end
end
function Print(text)
Log(tostring(text))
end