-
Notifications
You must be signed in to change notification settings - Fork 0
/
jump.cpp
355 lines (302 loc) · 6.99 KB
/
jump.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
#include "jump.hpp"
#if COMPILE_JUMP == 1
cvar_t *jump_height;
cvar_t *jump_stepSize;
cvar_t *jump_slowdownEnable;
cvar_t *jump_ladderPushVel;
cvar_t *jump_spreadAdd;
#define JUMP_LAND_SLOWDOWN_TIME 1800
bool player_jump_height_enabled[MAX_CLIENTS] = {0};
float player_jump_height[MAX_CLIENTS] = {0};
bool player_jump_slowdownenable_enabled[MAX_CLIENTS] = {0};
bool player_jump_slowdownenable[MAX_CLIENTS] = {0};
int playerstateToClientNum(playerState_t* ps)
{
return (int)(((byte *)ps - (byte *)sv.gameClients) / sv.gameClientSize);
}
float getJumpHeight(playerState_t* ps)
{
int clientid = playerstateToClientNum(ps);
if(player_jump_height_enabled[clientid])
return player_jump_height[clientid];
return jump_height->floatval;
}
bool getJumpSlowdownEnable(playerState_t* ps)
{
int clientid = playerstateToClientNum(ps);
if(player_jump_slowdownenable_enabled[clientid])
return player_jump_slowdownenable[clientid];
return jump_slowdownEnable->floatval;
}
float Jump_CalcHeight(playerState_t* ps )
{
float val;
float newdiv;
float jumpHeight = getJumpHeight(ps);//jump_height->floatval;
val = jumpHeight;
val = (val + val) * ps->gravity;
if(ps->pm_flags & PMF_JUMPING && ps->pm_time <= JUMP_LAND_SLOWDOWN_TIME )
{
if(getJumpSlowdownEnable(ps))//jump_slowdownEnable->boolean)
{
if(ps->pm_time > 1699)
{
newdiv = 2.5;
}
else
{
newdiv = ps->pm_time * (float)1.5 * (float)0.00058823527 + (float)1.0;
}
}
else
{
newdiv = 1.0;
}
val = val / newdiv;
}
return val;
}
double Jump_GetLandFactor(playerState_s *ps)
{
if ( !getJumpSlowdownEnable(ps))//jump_slowdownEnable->boolean )
{
return 1.0;
}
if ( ps->pm_time < 1700 )
{
return (double)ps->pm_time * 1.5 * 0.00058823527 + 1.0;
}
return 2.5;
}
void Jump_AddSurfaceEvent(playerState_s *ps, pml_t *pml)
{
int surfType;
if ( ps->pm_flags & PMF_LADDER )
{
BG_AddPredictableEventToPlayerstate(76, 0x15u, ps);
}
else
{
surfType = PM_GroundSurfaceType(pml);
if ( surfType )
{
BG_AddPredictableEventToPlayerstate(76, surfType, ps);
}
}
}
void Jump_PushOffLadder(playerState_s *ps, pml_t *pml)
{
vec3_t flatForward;
vec3_t pushOffDir;
float dot;
ps->velocity[2] = ps->velocity[2] * 0.75;
flatForward[0] = pml->forward[0];
flatForward[1] = pml->forward[1];
flatForward[2] = 0.0;
Vec3Normalize(flatForward);
dot = DotProduct(ps->vLadderVec, pml->forward);
if ( dot >= 0.0 )
{
VectorCopy(flatForward, pushOffDir);
}
else
{
dot = DotProduct(ps->vLadderVec, flatForward);
VectorMA(flatForward, -2.0 * dot, ps->vLadderVec, pushOffDir);
Vec3Normalize(pushOffDir);
}
ps->velocity[0] = jump_ladderPushVel->floatval * pushOffDir[0];
ps->velocity[1] = jump_ladderPushVel->floatval * pushOffDir[1];
ps->pm_flags &= 0xFFFFFFDF;
}
void Jump_Start(pmove_t *pm, pml_t *pml, float height)
{
float factor;
float velocitySqrd;
playerState_s *ps;
ps = pm->ps;
velocitySqrd = (float)(height * 2.0) * (float)ps->gravity;
if ( ps->pm_flags & PMF_JUMPING )
{
if ( ps->pm_time <= JUMP_LAND_SLOWDOWN_TIME )
{
factor = Jump_GetLandFactor(ps);
velocitySqrd = velocitySqrd / factor;
}
}
pml->groundPlane = 0;
pml->almostGroundPlane = 0;
pml->walking = 0;
ps->groundEntityNum = 1023;
ps->jumpTime = pm->cmd.serverTime;
ps->jumpOriginZ = ps->origin[2];
ps->velocity[2] = sqrtf(velocitySqrd);
ps->pm_flags &= 0xFFFFFE7F;
ps->pm_flags |= PMF_JUMPING;
ps->pm_time = 0;
ps->aimSpreadScale = ps->aimSpreadScale + jump_spreadAdd->floatval;
if ( ps->aimSpreadScale > 255.0 )
{
ps->aimSpreadScale = 255.0;
}
}
void Jump_ClearState(playerState_s *ps)
{
ps->pm_flags &= ~PMF_JUMPING;
ps->jumpOriginZ = 0.0;
}
double Jump_ReduceFriction(playerState_s *ps)
{
float control;
if ( ps->pm_time > JUMP_LAND_SLOWDOWN_TIME )
{
Jump_ClearState(ps);
control = 1.0;
}
else
{
control = Jump_GetLandFactor(ps);
}
return control;
}
void Jump_ClampVelocity(playerState_t* ps, vec3_t vec)
{
float comp;
float newZVelocity;
if(ps->origin[2] - vec[2] > 0)
{
float jumpHeight = getJumpHeight(ps);//jump_height->floatval;
comp = ps->jumpOriginZ + jumpHeight - ps->origin[2];
if(comp >= (float)0.1)
{
newZVelocity = sqrtf( (comp + comp) * (float)ps->gravity);
if(ps->velocity[2] > newZVelocity )
{
ps->velocity[2] = newZVelocity;
}
}
else
{
ps->velocity[2] = 0;
return;
}
}
}
qboolean Jump_IsPlayerAboveMax(playerState_t* ps)
{
float jumpHeight = getJumpHeight(ps);//jump_height->floatval;
if(ps->origin[2] >= ps->jumpOriginZ + jumpHeight )
return qtrue;
else
return qfalse;
}
qboolean Jump_GetStepHeight(playerState_t* ps, const vec3_t vec1, float* val2)
{
float jumpHeight = getJumpHeight(ps);//jump_height->floatval;
if(vec1[2] > ps->jumpOriginZ + jumpHeight)
return qfalse;
*val2 = jump_stepSize->floatval;
if(vec1[2] + jump_stepSize->floatval > ps->jumpOriginZ + jumpHeight)
*val2 = ps->jumpOriginZ + jumpHeight - vec1[2];
return qtrue;
}
qboolean Jump_Check(pmove_t *pm, pml_t *pml)
{
playerState_s *ps;
ps = pm->ps;
if ( ps->pm_flags & 0x1000 )
{
return qfalse;
}
if ( pm->cmd.serverTime - ps->jumpTime < 500 )
{
return qfalse;
}
if ( ps->pm_flags & 0x400 )
{
return qfalse;
}
if ( ps->pm_flags & 4 )
{
return qfalse;
}
if ( ps->pm_type > 5 )
{
return qfalse;
}
if ( PM_GetEffectiveStance(ps) && ps->groundEntityNum != 1023 )
{
return qfalse;
}
if ( !(pm->cmd.buttons & 0x400) )
{
return qfalse;
}
if ( pm->oldcmd.buttons & 0x400 )
{
pm->cmd.buttons &= 0xFFFFFBFF;
return qfalse;
}
Jump_Start(pm, pml, getJumpHeight(ps));//jump_height->floatval);
Jump_AddSurfaceEvent(ps, pml);
if ( ps->pm_flags & PMF_LADDER )
{
Jump_PushOffLadder(ps, pml);
}
if ( pm->cmd.forwardmove < 0 )
{
BG_AnimScriptEvent(ps, ANIM_ET_JUMPBK, 0, 1);
}
else
{
BG_AnimScriptEvent(ps, ANIM_ET_JUMP, 0, 1);
}
return qtrue;
}
void Jump_ApplySlowdown(playerState_s *ps)
{
float scale;
scale = 1.0;
if ( ps->pm_time <= JUMP_LAND_SLOWDOWN_TIME )
{
if ( !ps->pm_time )
{
if ( (float)(ps->jumpOriginZ + 18.0) <= ps->origin[2] )
{
ps->pm_time = 1200;
scale = 0.5;
}
else
{
ps->pm_time = JUMP_LAND_SLOWDOWN_TIME;
scale = 0.64999998;
}
}
}
else
{
Jump_ClearState(ps);
scale = 0.64999998;
}
if ( getJumpSlowdownEnable(ps))//jump_slowdownEnable->boolean )
{
VectorScale(ps->velocity, scale, ps->velocity);
}
}
void Jump_ActivateSlowdown(playerState_s *ps)
{
if ( !ps->pm_time )
{
ps->pm_flags |= PMF_JUMPING;
ps->pm_time = JUMP_LAND_SLOWDOWN_TIME;
}
}
void Jump_RegisterDvars()
{
jump_height = Cvar_RegisterFloat("jump_height", 39.0, 0.0, 128.0, CVAR_CHEAT | CVAR_SYSTEMINFO);
jump_stepSize = Cvar_RegisterFloat("jump_stepSize", 18.0, 0.0, 64.0, CVAR_CHEAT | CVAR_SYSTEMINFO);
jump_slowdownEnable = Cvar_RegisterBool("jump_slowdownEnable", qtrue, CVAR_CHEAT | CVAR_SYSTEMINFO);
jump_ladderPushVel = Cvar_RegisterFloat("jump_ladderPushVel", 128.0, 0.0, 1024.0, CVAR_CHEAT | CVAR_SYSTEMINFO);
jump_spreadAdd = Cvar_RegisterFloat("jump_spreadAdd", 64.0, 0.0, 512.0, CVAR_CHEAT | CVAR_SYSTEMINFO);
}
#endif