-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrenadePrediction.cpp
385 lines (313 loc) · 8.82 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
377
378
379
380
381
382
383
384
385
#include "GrenadePrediction.h"
void CGrenadePrediction::Tick(int buttons)
{
if (!vars.visuals.nadepred)
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 CGrenadePrediction::View(CViewSetup* setup)
{
if (!vars.visuals.nadepred)
return;
if (csgo->local && csgo->local->isAlive())
{
IBaseCombatWeapon* weapon = csgo->local->GetWeapon();
if (weapon && weapon->IsNade() && act != ACT_NONE)
{
type = weapon->GetItemDefinitionIndex();
Simulate(setup);
}
else
{
type = 0;
}
}
}
void CGrenadePrediction::ParseInfo() {
if (!csgo->is_local_alive)
return;
if (!vars.visuals.nadepred)
return;
csgo->mtx.lock();
render_info.path.clear();
if ((type) && !(path.empty())) {
Vector prev = path[0];
render_info.type = type;
render_info.path.push_back(prev);
for (auto it = path.begin(), end = path.end(); it != end; ++it)
{
render_info.path.push_back(*it);
prev = *it;
}
}
csgo->mtx.unlock();
}
void CGrenadePrediction::Paint()
{
if (!vars.visuals.nadepred) {
return;
}
csgo->mtx.lock();
auto flags_backup = g_Render->_drawList->Flags;
g_Render->_drawList->Flags |= ImDrawListFlags_AntiAliasedFill | ImDrawListFlags_AntiAliasedLines;
std::vector<ImVec2> pts = {};
std::vector<ImVec2> shadow_pts = {};
pts.clear();
shadow_pts.clear();
if ((render_info.type) && !(render_info.path.empty()))
{
Vector nadeStart, nadeEnd;
Vector prev = render_info.path[0];
for (const auto &it : render_info.path)
{
if (Math::WorldToScreen(prev, nadeStart) && Math::WorldToScreen(it, nadeEnd))
{
pts.push_back(ImVec2(nadeEnd.x, nadeEnd.y));
shadow_pts.push_back(ImVec2(nadeEnd.x + 1, nadeEnd.y + 1));
}
prev = it;
}
if (!shadow_pts.empty())
g_Render->_drawList->AddPolyline(shadow_pts.data(), shadow_pts.size(), vars.visuals.nadepred_color.u32() & IM_COL32_A_MASK, false, 1.5f);
if (!pts.empty())
g_Render->_drawList->AddPolyline(pts.data(), pts.size(), vars.visuals.nadepred_color.u32(), false, 1.5f);
g_Render->CircleFilled(nadeEnd.x, nadeEnd.y, 5, color_t(0, 0, 0, vars.visuals.nadepred_color.get_alpha()), 20);
g_Render->CircleFilled(nadeEnd.x, nadeEnd.y, 4, vars.visuals.nadepred_color, 20);
}
g_Render->_drawList->Flags = flags_backup;
csgo->mtx.unlock();
}
void CGrenadePrediction::Setup(Vector& vecSrc, Vector& vecThrow, Vector viewangles)
{
if (!vars.visuals.nadepred)
return;
Vector angThrow = viewangles;
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;
Math::AngleVector4(angThrow, vForward, vRight, vUp); //angThrow.ToVector(vForward, vRight, vUp);
vecSrc = csgo->unpred_eyepos;
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 = csgo->local->GetVelocity(); vecThrow *= 1.25f;
vecThrow += vForward * flVel; // vecThrow.MultAdd(vForward, flVel);
}
void CGrenadePrediction::Simulate(CViewSetup* setup)
{
if (!vars.visuals.nadepred)
return;
Vector vecSrc, vecThrow;
Vector angles; interfaces.engine->GetViewAngles(angles);
Setup(vecSrc, vecThrow, angles);
float interval = interfaces.global_vars->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 CGrenadePrediction::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 CGrenadePrediction::CheckDetonate(const Vector& vecThrow, const trace_t& tr, int tick, float interval)
{
switch (type)
{
case WEAPON_SMOKEGRENADE:
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_FLASHBANG:
case WEAPON_HEGRENADE:
// 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:
return false;
}
}
void CGrenadePrediction::TraceHull(Vector& src, Vector& end, trace_t& tr)
{
if (!vars.visuals.nadepred)
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;
interfaces.trace->TraceRay(ray, 0x200400B, &filter, &tr);
}
void CGrenadePrediction::AddGravityMove(Vector& move, Vector& vel, float frametime, bool onground)
{
if (!vars.visuals.nadepred)
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 CGrenadePrediction::PushEntity(Vector& src, const Vector& move, trace_t& tr)
{
if (!vars.visuals.nadepred)
return;
Vector vecAbsEnd = src;
vecAbsEnd += move;
// Trace through world
TraceHull(src, vecAbsEnd, tr);
}
void CGrenadePrediction::ResolveFlyCollisionCustom(trace_t& tr, Vector& vecVelocity, float interval)
{
if (!vars.visuals.nadepred)
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 CGrenadePrediction::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;
}