forked from cutie-coders/1337
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIGrenadeWarning.cpp
More file actions
299 lines (231 loc) · 6.41 KB
/
Copy pathIGrenadeWarning.cpp
File metadata and controls
299 lines (231 loc) · 6.41 KB
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
#include "IGrenadeWarning.h"
void IGrenadeWarning::View(Vector& source, Vector& Vec, Vector& EyeAng,int Type)
{
if (csgo->local && csgo->local->isAlive())
{
Source = source;
Velocity = Vec;
Direction = EyeAng;
type = Type;
Simulate();
}
}
void IGrenadeWarning::Setup(Vector& vecSrc, Vector& vecThrow, Vector viewangles)
{
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
float b = 1.0f;
// 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 = Source;
float off = (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 = Velocity; vecThrow *= 1.25f;
vecThrow += vForward * flVel; // vecThrow.MultAdd(vForward, flVel);
}
void IGrenadeWarning::Simulate()
{
Vector vecSrc, vecThrow;
Vector angles = Direction;
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;
bounces.clear();
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);
EndPos = vecSrc;
}
int IGrenadeWarning::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);
bounces.push_back(tr.endpos);
}
// Set new position
vecSrc = tr.endpos;
return result;
}
bool IGrenadeWarning::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 IGrenadeWarning::TraceHull(Vector& src, Vector& end, trace_t& tr)
{
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 IGrenadeWarning::AddGravityMove(Vector& move, Vector& Velocity, float frametime, bool onground)
{
Vector basevel(0.0f, 0.0f, 0.0f);
move.x = (Velocity.x + basevel.x) * frametime;
move.y = (Velocity.y + basevel.y) * frametime;
if (onground)
{
move.z = (Velocity.z + basevel.z) * frametime;
}
else
{
// Game calls GetActualGravity( this );
float gravity = 800.0f * 0.4f;
float newZ = Velocity.z - (gravity * frametime);
move.z = ((Velocity.z + newZ) / 2.0f + basevel.z) * frametime;
Velocity.z = newZ;
}
}
void IGrenadeWarning::PushEntity(Vector& src, const Vector& move, trace_t& tr)
{
Vector vecAbsEnd = src;
vecAbsEnd += move;
// Trace through world
TraceHull(src, vecAbsEnd, tr);
}
void IGrenadeWarning::ResolveFlyCollisionCustom(trace_t& tr, Vector& vecVelocity, float interval)
{
// 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 IGrenadeWarning::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;
}