]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics/movetypes/movetypes.qc
Prevent players moving too far/fast when stepping up a staircase
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / physics / movetypes / movetypes.qc
1 #include "movetypes.qh"
2
3 #ifdef SVQC
4 void set_movetype(entity this, int mt)
5 {
6         this.move_movetype = mt;
7         if (mt == MOVETYPE_PHYSICS) {
8                 this.move_qcphysics = false;
9         } else if (autocvar_sv_qcphysics == 2) {
10                 this.move_qcphysics = true;
11         }
12         if(!IL_CONTAINS(g_moveables, this))
13                 IL_PUSH(g_moveables, this); // add it to the moveable entities list (even if it doesn't move!) logic: if an object never sets its movetype, we assume it never does anything notable
14         this.movetype = (this.move_qcphysics) ? MOVETYPE_QCENTITY : mt;
15 }
16 #elif defined(CSQC)
17 void set_movetype(entity this, int mt)
18 {
19         this.move_movetype = mt;
20 }
21 #endif
22
23 bool _Movetype_NudgeOutOfSolid_PivotIsKnownGood(entity this, vector pivot) // SV_NudgeOutOfSolid_PivotIsKnownGood
24 {
25         vector stuckorigin = this.origin;
26         vector goodmins = pivot, goodmaxs = pivot;
27         for(int bump = 0; bump < 6; bump++)
28         {
29                 int coord = 2 - (bump >> 1);
30                 int dir = (bump & 1);
31
32                 for(int subbump = 0; ; ++subbump)
33                 {
34                         vector testorigin = stuckorigin;
35                         if(dir)
36                         {
37                                 // pushing maxs
38                                 switch(coord)
39                                 {
40                                         case 0: testorigin.x += this.maxs_x - goodmaxs.x; break;
41                                         case 1: testorigin.y += this.maxs_y - goodmaxs.y; break;
42                                         case 2: testorigin.z += this.maxs_z - goodmaxs.z; break;
43                                 }
44                         }
45                         else
46                         {
47                                 // pushing mins
48                                 switch(coord)
49                                 {
50                                         case 0: testorigin.x += this.mins_x - goodmins.x; break;
51                                         case 1: testorigin.y += this.mins_y - goodmins.y; break;
52                                         case 2: testorigin.z += this.mins_z - goodmins.z; break;
53                                 }
54                         }
55
56                         tracebox(stuckorigin, goodmins, goodmaxs, testorigin, MOVE_NOMONSTERS, this);
57                         if(trace_startsolid && trace_ent.solid == SOLID_BSP) // NOTE: this checks for bmodelstartsolid in the engine
58                         {
59                                 // BAD BAD, can't fix that
60                                 return false;
61                         }
62
63                         if(trace_fraction >= 1)
64                                 break; // it WORKS!
65
66                         if(subbump >= 10)
67                         {
68                                 // BAD BAD, can't fix that
69                                 return false;
70                         }
71
72                         // we hit something... let's move out of it
73                         vector move = trace_endpos - testorigin;
74                         float nudge = (trace_plane_normal * move) + 0.03125; // FIXME cvar this constant
75                         stuckorigin = stuckorigin + nudge * trace_plane_normal;
76                 }
77
78                 if(dir)
79                 {
80                         // pushing maxs
81                         switch(coord)
82                         {
83                                 case 0: goodmaxs.x = this.maxs_x; break;
84                                 case 1: goodmaxs.y = this.maxs_y; break;
85                                 case 2: goodmaxs.z = this.maxs_z; break;
86                         }
87                 }
88                 else
89                 {
90                         // pushing mins
91                         switch(coord)
92                         {
93                                 case 0: goodmins.x = this.mins_x; break;
94                                 case 1: goodmins.y = this.mins_y; break;
95                                 case 2: goodmins.z = this.mins_z; break;
96                         }
97                 }
98         }
99
100         // WE WIN
101         this.origin = stuckorigin;
102
103         return true;
104 }
105
106 void _Movetype_WallFriction(entity this, vector stepnormal)  // SV_WallFriction
107 {
108         /*float d, i;
109         vector into, side;
110         makevectors(this.v_angle);
111         d = (stepnormal * v_forward) + 0.5;
112
113         if(d < 0)
114         {
115             i = (stepnormal * this.velocity);
116             into = i * stepnormal;
117             side = this.velocity - into;
118             this.velocity_x = side.x * (1 * d);
119             this.velocity_y = side.y * (1 * d);
120         }*/
121 }
122
123 vector planes[MAX_CLIP_PLANES];
124 int _Movetype_FlyMove(entity this, float dt, bool applygravity, bool applystepnormal, float stepheight) // SV_FlyMove
125 {
126         move_stepnormal = '0 0 0';
127
128         if(dt <= 0)
129                 return 0;
130
131         int blockedflag = 0;
132         int i, j, numplanes = 0;
133         float time_left = dt, grav = 0;
134         vector push;
135         vector primal_velocity, original_velocity;
136         vector restore_velocity = this.velocity;
137
138         for(i = 0; i < MAX_CLIP_PLANES; ++i)
139                 planes[i] = '0 0 0';
140
141         if(applygravity)
142         {
143                 this.move_didgravity = 1;
144                 grav = dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
145
146                 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
147                 {
148                         if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
149                                 this.velocity_z -= grav * 0.5;
150                         else
151                                 this.velocity_z -= grav;
152                 }
153         }
154
155         original_velocity = primal_velocity = this.velocity;
156
157         for(int bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
158         {
159                 if(this.velocity == '0 0 0')
160                         break;
161
162                 push = this.velocity * time_left;
163                 if(!_Movetype_PushEntity(this, push, true, false))
164                 {
165                         // we got teleported by a touch function
166                         // let's abort the move
167                         blockedflag |= 8;
168                         break;
169                 }
170
171                 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
172                 // abort move if we're stuck in the world (and didn't make it out)
173                 if(trace_startsolid && trace_allsolid)
174                 {
175                         this.velocity = restore_velocity;
176                         return 3;
177                 }
178
179                 if(trace_fraction == 1)
180                         break;
181
182                 time_left *= 1 - trace_fraction;
183
184                 float my_trace_fraction = trace_fraction;
185                 vector my_trace_plane_normal = trace_plane_normal;
186
187                 if(trace_plane_normal.z)
188                 {
189                         if(trace_plane_normal.z > 0.7)
190                         {
191                                 // floor
192                                 blockedflag |= 1;
193
194                                 if(!trace_ent)
195                                 {
196                                         //dprint("_Movetype_FlyMove: !trace_ent\n");
197                                         trace_ent = NULL;
198                                 }
199
200                                 SET_ONGROUND(this);
201                                 this.groundentity = trace_ent;
202                         }
203                 }
204                 else if(stepheight)
205                 {
206                         // step - handle it immediately
207                         vector org = this.origin;
208                         vector steppush = '0 0 1' * stepheight;
209                         push = this.velocity * time_left;
210
211                         if(!_Movetype_PushEntity(this, steppush, true, false))
212                         {
213                                 blockedflag |= 8;
214                                 break;
215                         }
216                         if(!_Movetype_PushEntity(this, push, true, false))
217                         {
218                                 blockedflag |= 8;
219                                 break;
220                         }
221                         float trace2_fraction = trace_fraction;
222                         steppush = vec3(0, 0, org.z - this.origin_z);
223                         if(!_Movetype_PushEntity(this, steppush, true, false))
224                         {
225                                 blockedflag |= 8;
226                                 break;
227                         }
228
229                         // accept the new position if it made some progress...
230                         if(fabs(this.origin_x - org.x) >= 0.03125 || fabs(this.origin_y - org.y) >= 0.03125)
231                         {
232                                 trace_endpos = this.origin;
233                                 time_left *= 1 - trace2_fraction;
234                                 numplanes = 0;
235                                 continue;
236                         }
237                         else
238                                 this.origin = org;
239                 }
240                 else
241                 {
242                         // step - return it to caller
243                         blockedflag |= 2;
244                         // save the trace for player extrafriction
245                         if(applystepnormal)
246                                 move_stepnormal = trace_plane_normal;
247                 }
248
249                 if(my_trace_fraction >= 0.001)
250                 {
251                         // actually covered some distance
252                         original_velocity = this.velocity;
253                         numplanes = 0;
254                 }
255
256                 // clipped to another plane
257                 if(numplanes >= MAX_CLIP_PLANES)
258                 {
259                         // this shouldn't really happen
260                         this.velocity = '0 0 0';
261                         blockedflag = 3;
262                         break;
263                 }
264
265                 planes[numplanes] = my_trace_plane_normal;
266                 numplanes++;
267
268                 // modify original_velocity so it parallels all of the clip planes
269                 vector new_velocity = '0 0 0';
270                 for (i = 0;i < numplanes;i++)
271                 {
272                         new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
273                         for (j = 0;j < numplanes;j++)
274                         {
275                                 if(j != i)
276                                 {
277                                         // not ok
278                                         if((new_velocity * planes[j]) < 0)
279                                                 break;
280                                 }
281                         }
282                         if(j == numplanes)
283                                 break;
284                 }
285
286                 if(i != numplanes)
287                 {
288                         // go along this plane
289                         this.velocity = new_velocity;
290                 }
291                 else
292                 {
293                         // go along the crease
294                         if(numplanes != 2)
295                         {
296                                 this.velocity = '0 0 0';
297                                 blockedflag = 7;
298                                 break;
299                         }
300                         vector dir = cross(planes[0], planes[1]);
301                         // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
302                         float ilength = sqrt((dir * dir));
303                         if(ilength)
304                                 ilength = 1.0 / ilength;
305                         dir.x *= ilength;
306                         dir.y *= ilength;
307                         dir.z *= ilength;
308                         float d = (dir * this.velocity);
309                         this.velocity = dir * d;
310                 }
311
312                 // if current velocity is against the original velocity,
313                 // stop dead to avoid tiny occilations in sloping corners
314                 if((this.velocity * primal_velocity) <= 0)
315                 {
316                         this.velocity = '0 0 0';
317                         break;
318                 }
319         }
320
321         // LordHavoc: this came from QW and allows you to get out of water more easily
322         if(GAMEPLAYFIX_EASIERWATERJUMP(this) && (this.flags & FL_WATERJUMP) && !(blockedflag & 8))
323                 this.velocity = primal_velocity;
324
325         if(PHYS_WALLCLIP(this) && this.pm_time && !(this.flags & FL_WATERJUMP) && !(blockedflag & 8))
326                 this.velocity = primal_velocity;
327
328         if(applygravity)
329         {
330                 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
331                 {
332                         if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
333                                 this.velocity_z -= grav * 0.5f;
334                 }
335         }
336
337         return blockedflag;
338 }
339
340 void _Movetype_CheckVelocity(entity this)  // SV_CheckVelocity
341 {
342         // if(vlen(this.velocity) < 0.0001)
343         // this.velocity = '0 0 0';
344 }
345
346 bool _Movetype_CheckWater(entity this)  // SV_CheckWater
347 {
348         vector point = this.origin;
349         point.z += this.mins.z + 1;
350
351         int nativecontents = pointcontents(point);
352         if(this.watertype && this.watertype != nativecontents)
353         {
354                 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.watertype, nativecontents);
355                 if(this.contentstransition)
356                         this.contentstransition(this.watertype, nativecontents);
357         }
358
359         this.waterlevel = WATERLEVEL_NONE;
360         this.watertype = CONTENT_EMPTY;
361
362         int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
363         if(supercontents & DPCONTENTS_LIQUIDSMASK)
364         {
365                 this.watertype = nativecontents;
366                 this.waterlevel = WATERLEVEL_WETFEET;
367                 point.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
368                 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
369                 {
370                         this.waterlevel = WATERLEVEL_SWIMMING;
371                         point.z = this.origin.z + this.view_ofs.z;
372                         if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
373                                 this.waterlevel = WATERLEVEL_SUBMERGED;
374                 }
375         }
376
377         return this.waterlevel > 1;
378 }
379
380 void _Movetype_CheckWaterTransition(entity ent)  // SV_CheckWaterTransition
381 {
382         int contents = pointcontents(ent.origin);
383
384         if(!ent.watertype)
385         {
386                 // just spawned here
387                 if(!GAMEPLAYFIX_WATERTRANSITION(ent))
388                 {
389                         ent.watertype = contents;
390                         ent.waterlevel = 1;
391                         return;
392                 }
393         }
394         else if(ent.watertype != contents)
395         {
396                 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.origin), pointcontents(ent.origin), ent.watertype, contents);
397                 if(ent.contentstransition)
398                         ent.contentstransition(ent.watertype, contents);
399         }
400
401         if(contents <= CONTENT_WATER)
402         {
403                 ent.watertype = contents;
404                 ent.waterlevel = 1;
405         }
406         else
407         {
408                 ent.watertype = CONTENT_EMPTY;
409                 ent.waterlevel = (GAMEPLAYFIX_WATERTRANSITION(ent) ? 0 : contents);
410         }
411 }
412
413 void _Movetype_Impact(entity this, entity oth)  // SV_Impact
414 {
415         if(!this && !oth)
416                 return;
417
418         // due to a lack of pointers in QC, we must save the trace values and restore them for other functions
419         bool save_trace_allsolid = trace_allsolid;
420         bool save_trace_startsolid = trace_startsolid;
421         float save_trace_fraction = trace_fraction;
422         bool save_trace_inwater = trace_inwater;
423         bool save_trace_inopen = trace_inopen;
424         vector save_trace_endpos = trace_endpos;
425         vector save_trace_plane_normal = trace_plane_normal;
426         float save_trace_plane_dist = trace_plane_dist;
427         entity save_trace_ent = trace_ent;
428         int save_trace_dpstartcontents = trace_dpstartcontents;
429         int save_trace_dphitcontents = trace_dphitcontents;
430         int save_trace_dphitq3surfaceflags = trace_dphitq3surfaceflags;
431         string save_trace_dphittexturename = trace_dphittexturename;
432
433         if(this.solid != SOLID_NOT && gettouch(this))
434                 gettouch(this)(this, oth);
435
436         if(oth.solid != SOLID_NOT && gettouch(oth))
437                 gettouch(oth)(oth, this);
438
439         trace_allsolid = save_trace_allsolid;
440         trace_startsolid = save_trace_startsolid;
441         trace_fraction = save_trace_fraction;
442         trace_inwater = save_trace_inwater;
443         trace_inopen = save_trace_inopen;
444         trace_endpos = save_trace_endpos;
445         trace_plane_normal = save_trace_plane_normal;
446         trace_plane_dist = save_trace_plane_dist;
447         trace_ent = save_trace_ent;
448         trace_dpstartcontents = save_trace_dpstartcontents;
449         trace_dphitcontents = save_trace_dphitcontents;
450         trace_dphitq3surfaceflags = save_trace_dphitq3surfaceflags;
451         trace_dphittexturename = save_trace_dphittexturename;
452 }
453
454 void _Movetype_LinkEdict_TouchAreaGrid(entity this)  // SV_LinkEdict_TouchAreaGrid
455 {
456         if(this.solid == SOLID_NOT)
457                 return;
458
459         // due to a lack of pointers in QC, we must save the trace values and restore them for other functions
460         bool save_trace_allsolid = trace_allsolid;
461         bool save_trace_startsolid = trace_startsolid;
462         float save_trace_fraction = trace_fraction;
463         bool save_trace_inwater = trace_inwater;
464         bool save_trace_inopen = trace_inopen;
465         vector save_trace_endpos = trace_endpos;
466         vector save_trace_plane_normal = trace_plane_normal;
467         float save_trace_plane_dist = trace_plane_dist;
468         entity save_trace_ent = trace_ent;
469         int save_trace_dpstartcontents = trace_dpstartcontents;
470         int save_trace_dphitcontents = trace_dphitcontents;
471         int save_trace_dphitq3surfaceflags = trace_dphitq3surfaceflags;
472         string save_trace_dphittexturename = trace_dphittexturename;
473
474     FOREACH_ENTITY_RADIUS_ORDERED(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
475                 if (it.solid == SOLID_TRIGGER && it != this)
476                 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
477                 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
478                 {
479                         trace_allsolid = false;
480                         trace_startsolid = false;
481                         trace_fraction = 1;
482                         trace_inwater = false;
483                         trace_inopen = true;
484                         trace_endpos = it.origin;
485                         trace_plane_normal = '0 0 1';
486                         trace_plane_dist = 0;
487                         trace_ent = this;
488                         trace_dpstartcontents = 0;
489                         trace_dphitcontents = 0;
490                         trace_dphitq3surfaceflags = 0;
491                         trace_dphittexturename = string_null;
492
493                         gettouch(it)(it, this);
494                 }
495     });
496
497         trace_allsolid = save_trace_allsolid;
498         trace_startsolid = save_trace_startsolid;
499         trace_fraction = save_trace_fraction;
500         trace_inwater = save_trace_inwater;
501         trace_inopen = save_trace_inopen;
502         trace_endpos = save_trace_endpos;
503         trace_plane_normal = save_trace_plane_normal;
504         trace_plane_dist = save_trace_plane_dist;
505         trace_ent = save_trace_ent;
506         trace_dpstartcontents = save_trace_dpstartcontents;
507         trace_dphitcontents = save_trace_dphitcontents;
508         trace_dphitq3surfaceflags = save_trace_dphitq3surfaceflags;
509         trace_dphittexturename = save_trace_dphittexturename;
510 }
511
512 bool autocvar__movetype_debug = false;
513 void _Movetype_LinkEdict(entity this, bool touch_triggers)  // SV_LinkEdict
514 {
515         if(autocvar__movetype_debug)
516         {
517                 vector mi, ma;
518                 if(this.solid == SOLID_BSP)
519                 {
520                         // TODO set the absolute bbox
521                         mi = this.mins;
522                         ma = this.maxs;
523                 }
524                 else
525                 {
526                         mi = this.mins;
527                         ma = this.maxs;
528                 }
529                 mi += this.origin;
530                 ma += this.origin;
531
532                 if(this.flags & FL_ITEM)
533                 {
534                         mi -= '15 15 1';
535                         ma += '15 15 1';
536                 }
537                 else
538                 {
539                         mi -= '1 1 1';
540                         ma += '1 1 1';
541                 }
542
543                 this.absmin = mi;
544                 this.absmax = ma;
545         }
546         else
547         {
548                 setorigin(this, this.origin); // calls SV_LinkEdict
549         #ifdef CSQC
550                 // NOTE: CSQC's version of setorigin doesn't expand
551                 this.absmin -= '1 1 1';
552                 this.absmax += '1 1 1';
553         #endif
554         }
555
556         if(touch_triggers)
557                 _Movetype_LinkEdict_TouchAreaGrid(this);
558 }
559
560 int _Movetype_ContentsMask(entity this)  // SV_GenericHitSuperContentsMask
561 {
562         if(this)
563         {
564                 if(this.dphitcontentsmask)
565                         return this.dphitcontentsmask;
566                 else if(this.solid == SOLID_SLIDEBOX)
567                 {
568                         if(this.flags & FL_MONSTER)
569                                 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_MONSTERCLIP;
570                         else
571                                 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
572                 }
573                 else if(this.solid == SOLID_CORPSE)
574                         return DPCONTENTS_SOLID | DPCONTENTS_BODY;
575                 else if(this.solid == SOLID_TRIGGER)
576                         return DPCONTENTS_SOLID | DPCONTENTS_BODY;
577                 else
578                         return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
579         }
580         else
581                 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
582 }
583
584 entity _Movetype_TestEntityPosition_ent;
585 bool _Movetype_TestEntityPosition(vector ofs)  // SV_TestEntityPosition
586 {
587     entity this = _Movetype_TestEntityPosition_ent;
588         vector org = this.origin + ofs;
589
590         //int cont = this.dphitcontentsmask;
591         //this.dphitcontentsmask = DPCONTENTS_SOLID;
592         tracebox(org, this.mins, this.maxs, this.origin, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
593         //this.dphitcontentsmask = cont;
594         if(trace_dpstartcontents & _Movetype_ContentsMask(this))
595                 return true;
596
597         if(vlen2(trace_endpos - this.origin) >= 0.0001)
598         {
599                 tracebox(trace_endpos, this.mins, this.maxs, trace_endpos, MOVE_NOMONSTERS, this);
600                 if(!trace_startsolid)
601                         this.origin = trace_endpos;
602         }
603         return false;
604 }
605
606 bool _Movetype_TestEntityPosition_Offset(int offset)
607 {
608         // NOTE: expects _Movetype_TestEntityPosition_ent to be set to the correct entity
609         // returns true if stuck
610
611     // start at 2, since the first position has already been checked
612     for(int j = 2; j <= offset; ++j)
613     {
614         if(!_Movetype_TestEntityPosition('0 0 -1' * j))
615                 return false;
616         if(!_Movetype_TestEntityPosition('0 0 1' * j))
617                 return false;
618     }
619
620         return true;
621 }
622
623 int _Movetype_UnstickEntity(entity this)  // SV_UnstickEntity
624 {
625     _Movetype_TestEntityPosition_ent = this;
626         if (!_Movetype_TestEntityPosition(' 0  0  0')) {
627             return UNSTICK_FINE;
628         }
629         #define X(v) if (_Movetype_TestEntityPosition(v))
630         X('0  0  -1') X(' 0  0  1')
631         X('-1  0  0') X(' 1  0  0')
632         X(' 0 -1  0') X(' 0  1  0')
633         X('-1 -1  0') X(' 1 -1  0')
634         X('-1  1  0') X(' 1  1  0')
635         #undef X
636         {
637         if(_Movetype_TestEntityPosition_Offset(rint((this.maxs.z - this.mins.z) * 0.36)))
638         {
639             LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)",
640                 etof(this), this.classname, vtos(this.origin));
641             return UNSTICK_STUCK;
642         }
643         }
644         LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)",
645                 etof(this), this.classname, vtos(this.origin));
646         _Movetype_LinkEdict(this, false);
647         return UNSTICK_FIXED;
648 }
649
650 void _Movetype_CheckStuck(entity this)  // SV_CheckStuck
651 {
652         int unstick = _Movetype_UnstickEntity(this); // sets test position entity
653         switch(unstick)
654         {
655                 case UNSTICK_FINE:
656                         this.oldorigin = this.origin;
657                         break;
658                 case UNSTICK_FIXED:
659                         break; // already sorted
660                 case UNSTICK_STUCK:
661                         vector offset = this.oldorigin - this.origin;
662                         if(!_Movetype_TestEntityPosition(offset))
663                                 _Movetype_LinkEdict(this, false);
664                         // couldn't unstick, should we warn about this?
665                         break;
666         }
667 }
668
669 vector _Movetype_ClipVelocity(vector vel, vector norm, float f)  // ClipVelocity
670 {
671         vel -= ((vel * norm) * norm) * f;
672
673         if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
674         if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
675         if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
676
677         return vel;
678 }
679
680 void _Movetype_PushEntityTrace(entity this, vector push)
681 {
682         vector end = this.origin + push;
683         int type;
684         if(this.move_nomonsters)
685                 type = max(0, this.move_nomonsters);
686         else if(this.move_movetype == MOVETYPE_FLYMISSILE)
687                 type = MOVE_MISSILE;
688         else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
689                 type = MOVE_WORLDONLY;
690         else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
691                 type = MOVE_NOMONSTERS;
692         else
693                 type = MOVE_NORMAL;
694
695         tracebox(this.origin, this.mins, this.maxs, end, type, this);
696 }
697
698 bool _Movetype_PushEntity(entity this, vector push, bool failonstartsolid, bool dolink)  // SV_PushEntity
699 {
700         _Movetype_PushEntityTrace(this, push);
701
702         // NOTE: this is a workaround for the QC's lack of a worldstartsolid trace parameter
703         if(trace_startsolid && failonstartsolid)
704         {
705                 int oldtype = this.move_nomonsters;
706                 this.move_nomonsters = MOVE_WORLDONLY;
707                 _Movetype_PushEntityTrace(this, push);
708                 this.move_nomonsters = oldtype;
709                 if(trace_startsolid)
710                         return true;
711         }
712
713         this.origin = trace_endpos;
714
715         vector last_origin = this.origin;
716
717         _Movetype_LinkEdict(this, dolink);
718
719         if((this.solid >= SOLID_TRIGGER && trace_fraction < 1 && (!IS_ONGROUND(this) || this.groundentity != trace_ent)))
720                 _Movetype_Impact(this, trace_ent);
721
722         return (this.origin == last_origin); // false if teleported by touch
723 }
724
725 void _Movetype_Physics_Frame(entity this, float movedt)
726 {
727         this.move_didgravity = -1;
728         switch (this.move_movetype)
729         {
730                 case MOVETYPE_PUSH:
731                 case MOVETYPE_FAKEPUSH:
732                         _Movetype_Physics_Push(this, movedt);
733                         break;
734                 case MOVETYPE_NONE:
735                         break;
736                 case MOVETYPE_FOLLOW:
737                         _Movetype_Physics_Follow(this);
738                         break;
739                 case MOVETYPE_NOCLIP:
740                         _Movetype_CheckWater(this);
741                         this.origin = this.origin + movedt * this.velocity;
742                         this.angles = this.angles + movedt * this.avelocity;
743                         _Movetype_LinkEdict(this, false);
744                         break;
745                 case MOVETYPE_STEP:
746                         _Movetype_Physics_Step(this, movedt);
747                         break;
748                 case MOVETYPE_WALK:
749                         _Movetype_Physics_Walk(this, movedt);
750                         break;
751                 case MOVETYPE_TOSS:
752                 case MOVETYPE_BOUNCE:
753                 case MOVETYPE_BOUNCEMISSILE:
754                 case MOVETYPE_FLYMISSILE:
755                 case MOVETYPE_FLY:
756                 case MOVETYPE_FLY_WORLDONLY:
757                         _Movetype_Physics_Toss(this, movedt);
758                         break;
759                 case MOVETYPE_PHYSICS:
760                         break;
761         }
762 }
763
764 void _Movetype_Physics_ClientFrame(entity this, float movedt)
765 {
766         this.move_didgravity = -1;
767         switch (this.move_movetype)
768         {
769                 case MOVETYPE_PUSH:
770                 case MOVETYPE_FAKEPUSH:
771                         LOG_DEBUG("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
772                         break;
773                 case MOVETYPE_NONE:
774                         break;
775                 case MOVETYPE_FOLLOW:
776                         _Movetype_Physics_Follow(this);
777                         break;
778                 case MOVETYPE_NOCLIP:
779                         _Movetype_CheckWater(this);
780                         this.origin = this.origin + movedt * this.velocity;
781                         this.angles = this.angles + movedt * this.avelocity;
782                         break;
783                 case MOVETYPE_STEP:
784                         if (GAMEPLAYFIX_UNSTICKPLAYERS(this) == 2)
785                                 _Movetype_CheckStuck(this);
786                         _Movetype_Physics_Step(this, movedt);
787                         break;
788                 case MOVETYPE_WALK:
789                 case MOVETYPE_FLY:
790                 case MOVETYPE_FLY_WORLDONLY:
791                         if (movedt > 0 && GAMEPLAYFIX_UNSTICKPLAYERS(this) == 2)
792                                 _Movetype_CheckStuck(this);
793                         _Movetype_Physics_Walk(this, movedt);
794                         break;
795                 case MOVETYPE_TOSS:
796                 case MOVETYPE_BOUNCE:
797                 case MOVETYPE_BOUNCEMISSILE:
798                 case MOVETYPE_FLYMISSILE:
799                         if (GAMEPLAYFIX_UNSTICKPLAYERS(this) == 2)
800                                 _Movetype_CheckStuck(this);
801                         _Movetype_Physics_Toss(this, movedt);
802                         break;
803                 case MOVETYPE_PHYSICS:
804                         break;
805         }
806
807         //_Movetype_CheckVelocity(this);
808
809         _Movetype_LinkEdict(this, true);
810
811         //_Movetype_CheckVelocity(this);
812 }
813
814 void Movetype_Physics_NoMatchTicrate(entity this, float movedt, bool isclient)  // to be run every move frame
815 {
816         bool didmove = (this.move_time != 0);
817         this.move_time = time;
818
819         if(isclient)
820                 _Movetype_Physics_ClientFrame(this, movedt);
821         else
822         {
823                 // this doesn't apply to clients, and only applies to unmatched entities
824                 // don't run think/move on newly spawned projectiles as it messes up
825                 // movement interpolation and rocket trails, and is inconsistent with
826                 // respect to entities spawned in the same frame
827                 // (if an ent spawns a higher numbered ent, it moves in the same frame,
828                 //  but if it spawns a lower numbered ent, it doesn't - this never moves
829                 //  ents in the first frame regardless)
830                 if(!didmove && GAMEPLAYFIX_DELAYPROJECTILES(this) > 0)
831                         return;
832                 _Movetype_Physics_Frame(this, movedt);
833         }
834         if(wasfreed(this))
835                 return;
836
837         setorigin(this, this.origin);
838 }
839
840 void Movetype_Physics_NoMatchServer(entity this)  // optimized
841 {
842         float movedt = time - this.move_time;
843         this.move_time = time;
844
845         _Movetype_Physics_Frame(this, movedt);
846         if(wasfreed(this))
847                 return;
848
849         setorigin(this, this.origin);
850 }
851
852 void Movetype_Physics_MatchServer(entity this, bool sloppy)
853 {
854         Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
855 }
856
857 // saved .move_*
858 .vector tic_origin;
859 .vector tic_velocity;
860 .int tic_flags;
861 .vector tic_avelocity;
862 .vector tic_angles;
863
864 // saved .*
865 .vector tic_saved_origin;
866 .vector tic_saved_velocity;
867 .int tic_saved_flags;
868 .vector tic_saved_avelocity;
869 .vector tic_saved_angles;
870 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy)  // SV_Physics_Entity
871 {
872         // this hack exists to contain the physics feature
873         // (so entities can place themselves in the world and not need to update .tic_* themselves)
874 #define X(s) \
875         if(this.(s) != this.tic_saved_##s) \
876                 this.tic_##s = this.(s)
877
878         X(origin);
879         X(velocity);
880         X(flags);
881         X(avelocity);
882         X(angles);
883 #undef X
884
885         this.flags = this.tic_flags;
886         this.velocity = this.tic_velocity;
887         setorigin(this, this.tic_origin);
888         this.avelocity = this.tic_avelocity;
889         this.angles = this.tic_angles;
890
891         if(tr <= 0)
892         {
893                 Movetype_Physics_NoMatchServer(this);
894
895                 this.tic_saved_flags = this.tic_flags = this.flags;
896                 this.tic_saved_velocity = this.tic_velocity = this.velocity;
897                 this.tic_saved_origin = this.tic_origin = this.origin;
898                 this.tic_saved_avelocity = this.tic_avelocity = this.avelocity;
899                 this.tic_saved_angles = this.tic_angles = this.angles;
900                 return;
901         }
902
903         float dt = time - this.move_time;
904
905         int n = bound(0, floor(dt / tr), 32); // limit the number of frames to 32 (CL_MAX_USERCMDS, using DP_SMALLMEMORY value for consideration of QC's limitations)
906         dt -= n * tr;
907         this.move_time += n * tr;
908
909         if(!this.move_didgravity)
910                 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.tic_flags & FL_ONGROUND));
911
912         for (int j = 0; j < n; ++j)
913         {
914                 _Movetype_Physics_Frame(this, tr);
915                 if(wasfreed(this))
916                         return;
917         }
918
919         // update the physics fields
920         this.tic_origin = this.origin;
921         this.tic_velocity = this.velocity;
922         this.tic_avelocity = this.avelocity;
923         this.tic_angles = this.angles;
924         this.tic_flags = this.flags;
925
926         // restore their actual values
927         this.flags = this.tic_saved_flags;
928         this.velocity = this.tic_saved_velocity;
929         setorigin(this, this.tic_saved_origin);
930         //this.avelocity = this.tic_saved_avelocity;
931         this.angles = this.tic_saved_angles;
932
933         this.avelocity = this.tic_avelocity;
934
935         if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.tic_flags & FL_ONGROUND))
936         {
937                 // now continue the move from move_time to time
938                 this.velocity = this.tic_velocity;
939
940                 if(this.move_didgravity > 0)
941                 {
942                         this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
943                             * dt
944                             * ((this.gravity) ? this.gravity : 1)
945                             * PHYS_GRAVITY(this);
946                 }
947
948                 this.angles = this.tic_angles + dt * this.avelocity;
949
950                 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
951                 {
952                         setorigin(this, this.tic_origin + dt * this.velocity);
953                 }
954                 else
955                 {
956                         setorigin(this, this.tic_origin);
957                         _Movetype_PushEntityTrace(this, dt * this.velocity);
958                         if(!trace_startsolid)
959                                 setorigin(this, trace_endpos);
960                         else
961                                 setorigin(this, this.tic_saved_origin);
962                 }
963
964                 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
965                         this.velocity_z -= 0.5 * dt * ((this.gravity) ? this.gravity : 1) * PHYS_GRAVITY(this);
966         }
967         else
968         {
969                 this.velocity = this.tic_velocity;
970                 this.angles = this.tic_angles;
971                 setorigin(this, this.tic_origin);
972         }
973
974         this.flags = this.tic_flags;
975
976         this.tic_saved_flags = this.flags;
977         this.tic_saved_velocity = this.velocity;
978         this.tic_saved_origin = this.origin;
979         this.tic_saved_avelocity = this.avelocity;
980         this.tic_saved_angles = this.angles;
981 }