Skip to content

Commit c0bbc78

Browse files
v2.5.2
1 parent f34b43b commit c0bbc78

File tree

1 file changed

+20
-43
lines changed

1 file changed

+20
-43
lines changed

Swing_Prediction.lua

+20-43
Original file line numberDiff line numberDiff line change
@@ -60,78 +60,76 @@ function GameData()
6060
return data
6161
end
6262

63-
63+
function GetClosestEnemy(pLocal, pLocalOrigin, players)
64+
local closestDistance = 1000
65+
local maxDistance = 1000
66+
-- find clsoest enemy
67+
for _, vPlayer in ipairs(players) do
68+
if vPlayer ~= nil and vPlayer:IsAlive() and vPlayer:GetTeamNumber() ~= pLocal:GetTeamNumber() then
69+
vPlayerOrigin = vPlayer:GetAbsOrigin()
70+
local distance = (vPlayerOrigin - pLocalOrigin):Length()
71+
if distance < closestDistance and distance <= maxDistance then
72+
closestPlayer = vPlayer
73+
closestDistance = distance
74+
end
75+
end
76+
end
77+
return closestPlayer
78+
end
6479
--[[ Global table of velocity vectors
6580
local velocitySamples = {}
6681
local maxSamples = 3 -- maximum number of samples
6782
local tickrate = 66 -- number of ticks per second
68-
6983
-- Returns the future position of a target based on its current position, last position, and a specified time ahead
7084
function getFuturePosition(targetPosition, targetLastPos, timeAhead)
7185
-- Calculate the current velocity
7286
local currentVelocity = (targetPosition - targetLastPos) / tickrate
7387
-- Add a new velocity sample to the table
7488
table.insert(velocitySamples, currentVelocity)
75-
7689
-- Remove the oldest sample if the number of samples exceeds the limit
7790
if #velocitySamples > maxSamples then
7891
table.remove(velocitySamples, 1)
7992
end
80-
8193
-- Calculate the average velocity from the latest samples
8294
local avgVelocity = nil
8395
for _, velocity in ipairs(velocitySamples) do
8496
avgVelocity = avgVelocity + velocity
8597
end
8698
avgVelocity = avgVelocity / #velocitySamples
87-
8899
-- Calculate the trajectory
89100
local trajectory = Vector3.new()
90101
for i = 2, #velocitySamples do
91102
local prevVelocity = velocitySamples[i - 1]
92103
local currVelocity = velocitySamples[i]
93-
94104
-- Calculate the time difference between the two samples
95105
local timeDiff = 1 / tickrate
96-
97106
-- Calculate the angle between the two velocity vectors
98107
local angle = math.acos(currVelocity:Dot(prevVelocity) / (currVelocity.Magnitude * prevVelocity.Magnitude))
99-
100108
-- Calculate the cross product of the two velocity vectors
101109
local cross = currVelocity:Cross(prevVelocity)
102-
103110
-- Flip the angle if the cross product is negative
104111
if cross.Z < 0 then
105112
angle = -angle
106113
end
107-
108114
-- Calculate the curvature of the trajectory
109115
local curvature = angle / timeDiff
110-
111116
-- Calculate the horizontal correction based on the curvature
112117
local correction = Vector3.new(0, 0, -curvature * 0.25)
113-
114118
-- Calculate the lateral vector, which is perpendicular to the forward vector
115119
local forwardVector = currVelocity.Unit
116120
local lateralVector = forwardVector:Cross(Vector3.new(0,0,1)).Unit
117-
118121
-- Calculate the turn angle between the two velocity vectors
119122
local turnAngle = math.atan2(forwardVector.Y, forwardVector.X) - math.atan2(prevVelocity.Y, prevVelocity.X)
120-
121123
-- Calculate the turn radius and center offset
122124
local turnRadius = currVelocity.Magnitude / turnAngle
123125
local turnCenterOffset = lateralVector * turnRadius
124-
125126
-- Calculate the horizontal correction based on the turn
126127
local turnCorrection = Vector3.new(turnCenterOffset.X, turnCenterOffset.Y, 0)
127-
128128
-- Add the corrections to the trajectory
129129
trajectory = trajectory + currVelocity + correction * timeDiff + turnCorrection
130130
end
131-
132131
-- Calculate the future position based on the trajectory and time ahead
133132
local futurePosition = targetPosition + trajectory * timeAhead
134-
135133
return futurePosition
136134
end]]
137135

@@ -270,24 +268,12 @@ local function OnCreateMove(pCmd, gameData)
270268

271269
-- Initialize closest distance and closest player
272270
isMelee = pWeapon:IsMeleeWeapon() -- check if using melee weapon
273-
local closestDistance = 1200
274-
local maxDistance = 1000
275271
local players = entities.FindByClass("CTFPlayer") -- Create a table of all players in the game
276272

277273
if not isMelee then return end
278274

279-
-- find clsoest enemy
280-
for _, vPlayer in ipairs(players) do
281-
if vPlayer ~= nil and vPlayer:IsAlive() and vPlayer:GetTeamNumber() ~= pLocal:GetTeamNumber() then
282-
vPlayerOrigin = vPlayer:GetAbsOrigin()
283-
local distance = (vPlayerOrigin - pLocalOrigin):Length()
284-
if distance < closestDistance and distance <= maxDistance then
285-
closestPlayer = vPlayer
286-
closestDistance = distance
287-
end
288-
end
289-
end
290-
275+
276+
closestPlayer = GetClosestEnemy(pLocal, pLocalOrigin, players)
291277
if closestPlayer == nil then goto continue end
292278
if closestDistance == 1200 then goto continue end
293279
vPlayerOrigin = closestPlayer:GetAbsOrigin()
@@ -303,11 +289,10 @@ if not isMelee then return end
303289
local stop = false
304290
if (pLocal:InCond(17)) and pLocalClass == 4 or pLocalClass == 8 then -- If we are charging (17 is TF_COND_SHIELD_CHARGE)
305291
stop = true
306-
dynamicstop = swingrange + 15
307-
if (pCmd.forwardmove == 0) then dynamicstop = swingrange + 10 end -- case if you dont hold w when charging
292+
dynamicstop = swingrange + 10
293+
if (pCmd.forwardmove == 0) then dynamicstop = swingrange end -- case if you dont hold w when charging
308294

309295
vdistance = (vPlayerFuture - pLocalOrigin):Length()
310-
print(vdistance)
311296
if pLocalClass == 4 and vdistance <= dynamicstop then
312297
pCmd:SetButtons(pCmd:GetButtons() | IN_ATTACK)
313298
end
@@ -414,37 +399,29 @@ end
414399
--[[ czapka
415400
-- ustawienie rozdzielczości koła
416401
local resolution = 36
417-
418402
-- promień koła
419403
local radius = 50
420-
421404
-- wektor środka koła
422405
local center = pLocalOrigin
423-
424406
-- wektor wysokości ostrosłupa
425407
local height = Vector3(0, 0, 20)
426-
427408
-- inicjalizacja tablicy wierzchołków koła
428409
local vertices = {}
429-
430410
-- wyznaczanie pozycji wierzchołków koła
431411
for i = 1, resolution do
432412
local angle = (2 * math.pi / resolution) * (i - 1)
433413
local x = radius * math.cos(angle)
434414
local y = radius * math.sin(angle)
435415
vertices[i] = Vector3(center.x + x, center.y + y)
436416
end
437-
438417
-- rysowanie linii z wierzchołków koła do punktu v2
439418
for i = 1, resolution do
440419
draw.line(vertices[i], height + vertices[i])
441420
end
442-
443421
-- rysowanie linii łączących kolejne wierzchołki koła
444422
for i = 1, resolution do
445423
draw.line(vertices[i], vertices[(i % resolution) + 1])
446424
end
447-
448425
-- rysowanie linii łączącej ostatni wierzchołek z pierwszym
449426
draw.line(vertices[resolution], vertices[1])
450427
]]

0 commit comments

Comments
 (0)