-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrenadePrediction.cpp
376 lines (317 loc) · 8.93 KB
/
GrenadePrediction.cpp
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
368
369
370
371
372
373
374
375
376
#include "GrenadePrediction.h"
#include "Render.h"
void grenade_prediction::Tick(int buttons)
{
if (!g_Options.Visuals.GrenadePrediction)
return;
bool in_attack = buttons & IN_ATTACK;
bool in_attack2 = buttons & IN_ATTACK2;
act = (in_attack && in_attack2) ? ACT_LOB :
(in_attack2) ? ACT_DROP :
(in_attack) ? ACT_THROW :
ACT_NONE;
}
void grenade_prediction::View(CViewSetup* setup)
{
auto local = g_EntityList->GetClientEntity(g_Engine->GetLocalPlayer());
if (!g_Options.Visuals.GrenadePrediction)
return;
if (local && local->IsAlive())
{
CBaseCombatWeapon* weapon = (CBaseCombatWeapon*)g_EntityList->GetClientEntityFromHandle(local->GetActiveWeaponHandle());
if (weapon && MiscFunctions::IsGrenade(weapon) && act != ACT_NONE)
{
type = weapon->m_AttributeManager()->m_Item()->GetItemDefinitionIndex();
Simulate(setup);
}
else
{
type = 0;
}
}
}
void grenade_prediction::Paint()
{
if (!g_Options.Visuals.GrenadePrediction)
return;
if ((type) && path.size()>1)
{
Vector nadeStart, nadeEnd;
Color lineColor(52, 128, 249, 255);
Vector prev = path[0];
for (auto it = path.begin(), end = path.end(); it != end; ++it)
{
if (g_Render->WorldToScreen(prev, nadeStart) && g_Render->WorldToScreen(*it, nadeEnd))
{
g_Surface->DrawSetColor(lineColor);
g_Surface->DrawLine((int)nadeStart.x, (int)nadeStart.y, (int)nadeEnd.x, (int)nadeEnd.y);
}
prev = *it;
}
if (g_Render->WorldToScreen(prev, nadeEnd))
{
g_Surface->DrawSetColor(Color(0, 255, 0, 255));
g_Surface->DrawOutlinedCircle((int)nadeEnd.x, (int)nadeEnd.y, 10, 48);
}
}
}
static const constexpr auto PIRAD = 0.01745329251f;
void angle_vectors2(const Vector &angles, Vector *forward, Vector *right, Vector *up)
{
float sr, sp, sy, cr, cp, cy;
sp = static_cast<float>(sin(double(angles.x) * PIRAD));
cp = static_cast<float>(cos(double(angles.x) * PIRAD));
sy = static_cast<float>(sin(double(angles.y) * PIRAD));
cy = static_cast<float>(cos(double(angles.y) * PIRAD));
sr = static_cast<float>(sin(double(angles.z) * PIRAD));
cr = static_cast<float>(cos(double(angles.z) * PIRAD));
if (forward)
{
forward->x = cp*cy;
forward->y = cp*sy;
forward->z = -sp;
}
if (right)
{
right->x = (-1 * sr*sp*cy + -1 * cr*-sy);
right->y = (-1 * sr*sp*sy + -1 * cr*cy);
right->z = -1 * sr*cp;
}
if (up)
{
up->x = (cr*sp*cy + -sr*-sy);
up->y = (cr*sp*sy + -sr*cy);
up->z = cr*cp;
}
}
void grenade_prediction::Setup(Vector& vecSrc, Vector& vecThrow, Vector viewangles)
{
if (!g_Options.Visuals.GrenadePrediction)
return;
Vector angThrow = viewangles;
auto local = g_EntityList->GetClientEntity(g_Engine->GetLocalPlayer());
float pitch = angThrow.x;
if (pitch <= 90.0f)
{
if (pitch<-90.0f)
{
pitch += 360.0f;
}
}
else
{
pitch -= 360.0f;
}
float a = pitch - (90.0f - fabs(pitch)) * 10.0f / 90.0f;
angThrow.x = a;
// Gets ThrowVelocity from weapon files
// Clamped to [15,750]
float flVel = 750.0f * 0.9f;
// Do magic on member of grenade object [esi+9E4h]
// m1=1 m1+m2=0.5 m2=0
static const float power[] = { 1.0f, 1.0f, 0.5f, 0.0f };
float b = power[act];
// Clamped to [0,1]
b = b * 0.7f;
b = b + 0.3f;
flVel *= b;
Vector vForward, vRight, vUp;
angle_vectors2(angThrow, &vForward, &vRight, &vUp); //angThrow.ToVector(vForward, vRight, vUp);
vecSrc = local->GetEyePosition();
float off = (power[act] * 12.0f) - 12.0f;
vecSrc.z += off;
// Game calls UTIL_TraceHull here with hull and assigns vecSrc tr.endpos
trace_t tr;
Vector vecDest = vecSrc;
vecDest += vForward * 22.0f; //vecDest.MultAdd(vForward, 22.0f);
TraceHull(vecSrc, vecDest, tr);
// After the hull trace it moves 6 units back along vForward
// vecSrc = tr.endpos - vForward * 6
Vector vecBack = vForward; vecBack *= 6.0f;
vecSrc = tr.endpos;
vecSrc -= vecBack;
// Finally calculate velocity
vecThrow = local->GetVelocity(); vecThrow *= 1.25f;
vecThrow += vForward * flVel; // vecThrow.MultAdd(vForward, flVel);
}
void grenade_prediction::Simulate(CViewSetup* setup)
{
if (!g_Options.Visuals.GrenadePrediction)
return;
Vector vecSrc, vecThrow;
Vector angles; g_Engine->GetViewAngles(angles);
Setup(vecSrc, vecThrow, angles);
float interval = g_Globals->interval_per_tick;
// Log positions 20 times per sec
int logstep = static_cast<int>(0.05f / interval);
int logtimer = 0;
path.clear();
for (unsigned int i = 0; i<path.max_size() - 1; ++i)
{
if (!logtimer)
path.push_back(vecSrc);
int s = Step(vecSrc, vecThrow, i, interval);
if ((s & 1)) break;
// Reset the log timer every logstep OR we bounced
if ((s & 2) || logtimer >= logstep) logtimer = 0;
else ++logtimer;
}
path.push_back(vecSrc);
}
int grenade_prediction::Step(Vector& vecSrc, Vector& vecThrow, int tick, float interval)
{
// Apply gravity
Vector move;
AddGravityMove(move, vecThrow, interval, false);
// Push entity
trace_t tr;
PushEntity(vecSrc, move, tr);
int result = 0;
// Check ending conditions
if (CheckDetonate(vecThrow, tr, tick, interval))
{
result |= 1;
}
// Resolve collisions
if (tr.fraction != 1.0f)
{
result |= 2; // Collision!
ResolveFlyCollisionCustom(tr, vecThrow, interval);
}
// Set new position
vecSrc = tr.endpos;
return result;
}
bool grenade_prediction::CheckDetonate(const Vector& vecThrow, const trace_t& tr, int tick, float interval)
{
switch (type)
{
case WEAPON_SMOKE:
case WEAPON_DECOY:
// Velocity must be <0.1, this is only checked every 0.2s
if (vecThrow.Length2D()<0.1f)
{
int det_tick_mod = static_cast<int>(0.2f / interval);
return !(tick%det_tick_mod);
}
return false;
case WEAPON_MOLOTOV:
case WEAPON_INC:
// Detonate when hitting the floor
if (tr.fraction != 1.0f && tr.plane.normal.z>0.7f)
return true;
// OR we've been flying for too long
case WEAPON_FLASH:
case WEAPON_HE:
// Pure timer based, detonate at 1.5s, checked every 0.2s
return static_cast<float>(tick)*interval>1.5f && !(tick%static_cast<int>(0.2f / interval));
default:
assert(false);
return false;
}
}
void grenade_prediction::TraceHull(Vector& src, Vector& end, trace_t& tr)
{
if (!g_Options.Visuals.GrenadePrediction)
return;
Ray_t ray;
ray.Init(src, end, Vector(-2.0f, -2.0f, -2.0f), Vector(2.0f, 2.0f, 2.0f));
CTraceFilterWorldAndPropsOnly filter;
//filter.SetIgnoreClass("BaseCSGrenadeProjectile");
//filter.bShouldHitPlayers = false;
g_EngineTrace->TraceRay(ray, 0x200400B, &filter, &tr);
}
void grenade_prediction::AddGravityMove(Vector& move, Vector& vel, float frametime, bool onground)
{
if (!g_Options.Visuals.GrenadePrediction)
return;
Vector basevel(0.0f, 0.0f, 0.0f);
move.x = (vel.x + basevel.x) * frametime;
move.y = (vel.y + basevel.y) * frametime;
if (onground)
{
move.z = (vel.z + basevel.z) * frametime;
}
else
{
// Game calls GetActualGravity( this );
float gravity = 800.0f * 0.4f;
float newZ = vel.z - (gravity * frametime);
move.z = ((vel.z + newZ) / 2.0f + basevel.z) * frametime;
vel.z = newZ;
}
}
void grenade_prediction::PushEntity(Vector& src, const Vector& move, trace_t& tr)
{
if (!g_Options.Visuals.GrenadePrediction)
return;
Vector vecAbsEnd = src;
vecAbsEnd += move;
// Trace through world
TraceHull(src, vecAbsEnd, tr);
}
void grenade_prediction::ResolveFlyCollisionCustom(trace_t& tr, Vector& vecVelocity, float interval)
{
if (!g_Options.Visuals.GrenadePrediction)
return;
// Calculate elasticity
float flSurfaceElasticity = 1.0; // Assume all surfaces have the same elasticity
float flGrenadeElasticity = 0.45f; // GetGrenadeElasticity()
float flTotalElasticity = flGrenadeElasticity * flSurfaceElasticity;
if (flTotalElasticity>0.9f) flTotalElasticity = 0.9f;
if (flTotalElasticity<0.0f) flTotalElasticity = 0.0f;
// Calculate bounce
Vector vecAbsVelocity;
PhysicsClipVelocity(vecVelocity, tr.plane.normal, vecAbsVelocity, 2.0f);
vecAbsVelocity *= flTotalElasticity;
// Stop completely once we move too slow
float flSpeedSqr = vecAbsVelocity.LengthSqr();
static const float flMinSpeedSqr = 20.0f * 20.0f; // 30.0f * 30.0f in CSS
if (flSpeedSqr<flMinSpeedSqr)
{
//vecAbsVelocity.Zero();
vecAbsVelocity.x = 0.0f;
vecAbsVelocity.y = 0.0f;
vecAbsVelocity.z = 0.0f;
}
// Stop if on ground
if (tr.plane.normal.z>0.7f)
{
vecVelocity = vecAbsVelocity;
vecAbsVelocity *= ((1.0f - tr.fraction) * interval); //vecAbsVelocity.Mult((1.0f - tr.fraction) * interval);
PushEntity(tr.endpos, vecAbsVelocity, tr);
}
else
{
vecVelocity = vecAbsVelocity;
}
}
int grenade_prediction::PhysicsClipVelocity(const Vector& in, const Vector& normal, Vector& out, float overbounce)
{
static const float STOP_EPSILON = 0.1f;
float backoff;
float change;
float angle;
int i, blocked;
blocked = 0;
angle = normal[2];
if (angle > 0)
{
blocked |= 1; // floor
}
if (!angle)
{
blocked |= 2; // step
}
backoff = in.Dot(normal) * overbounce;
for (i = 0; i<3; i++)
{
change = normal[i] * backoff;
out[i] = in[i] - change;
if (out[i] > -STOP_EPSILON && out[i] < STOP_EPSILON)
{
out[i] = 0;
}
}
return blocked;
}