]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/physics/movetypes/movetypes.qc
2634b8246bedc6e0eb165b772a11f341d6364924
[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 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(int j = 0; j < MAX_CLIP_PLANES; ++j)
139                 planes[j] = '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, 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 = vec3(0, 0, stepheight);
209                         push = this.velocity * time_left;
210
211                         if(!_Movetype_PushEntity(this, steppush, false))
212                         {
213                                 blockedflag |= 8;
214                                 break;
215                         }
216                         if(!_Movetype_PushEntity(this, push, 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, false))
224                         {
225                                 blockedflag |= 8;
226                                 break;
227                         }
228
229                         // accept the new position if it made some progress...
230                         // previously this checked if absolute distance >= 0.03125 which made stepping up unreliable
231                         if(this.origin_x - org.x || this.origin_y - org.y)
232                         {
233                                 trace_endpos = this.origin;
234                                 time_left *= 1 - trace2_fraction;
235                                 numplanes = 0;
236                                 continue;
237                         }
238                         else
239                                 this.origin = org;
240                 }
241                 else
242                 {
243                         // step - return it to caller
244                         blockedflag |= 2;
245                         // save the trace for player extrafriction
246                         if(applystepnormal)
247                                 move_stepnormal = trace_plane_normal;
248                 }
249
250                 if(my_trace_fraction >= 0.001)
251                 {
252                         // actually covered some distance
253                         original_velocity = this.velocity;
254                         numplanes = 0;
255                 }
256
257                 // clipped to another plane
258                 if(numplanes >= MAX_CLIP_PLANES)
259                 {
260                         // this shouldn't really happen
261                         this.velocity = '0 0 0';
262                         blockedflag = 3;
263                         break;
264                 }
265
266                 planes[numplanes] = my_trace_plane_normal;
267                 numplanes++;
268
269                 // modify original_velocity so it parallels all of the clip planes
270                 vector new_velocity = '0 0 0';
271                 int plane;
272                 for (plane = 0;plane < numplanes;plane++)
273                 {
274                         int newplane;
275                         new_velocity = _Movetype_ClipVelocity(original_velocity, planes[plane], 1);
276                         for (newplane = 0;newplane < numplanes;newplane++)
277                         {
278                                 if(newplane != plane)
279                                 {
280                                         // not ok
281                                         if((new_velocity * planes[newplane]) < 0)
282                                                 break;
283                                 }
284                         }
285                         if(newplane == numplanes)
286                                 break;
287                 }
288
289                 if(plane != numplanes)
290                 {
291                         // go along this plane
292                         this.velocity = new_velocity;
293                 }
294                 else
295                 {
296                         // go along the crease
297                         if(numplanes != 2)
298                         {
299                                 this.velocity = '0 0 0';
300                                 blockedflag = 7;
301                                 break;
302                         }
303                         vector dir = cross(planes[0], planes[1]);
304                         // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
305                         dir = normalize(dir);
306                         float d = (dir * this.velocity);
307                         this.velocity = dir * d;
308                 }
309
310                 // if current velocity is against the original velocity,
311                 // stop dead to avoid tiny occilations in sloping corners
312                 if((this.velocity * primal_velocity) <= 0)
313                 {
314                         this.velocity = '0 0 0';
315                         break;
316                 }
317         }
318
319         // LordHavoc: this came from QW and allows you to get out of water more easily
320         if(GAMEPLAYFIX_EASIERWATERJUMP(this) && (this.flags & FL_WATERJUMP) && !(blockedflag & 8))
321                 this.velocity = primal_velocity;
322
323         if(PHYS_WALLCLIP(this) && this.pm_time && !(this.flags & FL_WATERJUMP) && !(blockedflag & 8))
324                 this.velocity = primal_velocity;
325
326         if(applygravity)
327         {
328                 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
329                 {
330                         if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
331                                 this.velocity_z -= grav * 0.5f;
332                 }
333         }
334
335         return blockedflag;
336 }
337
338 void _Movetype_CheckVelocity(entity this)  // SV_CheckVelocity
339 {
340         // if(vlen(this.velocity) < 0.0001)
341         // this.velocity = '0 0 0';
342 }
343
344 bool _Movetype_CheckWater(entity this)  // SV_CheckWater
345 {
346         vector point = this.origin;
347         point.z += this.mins.z + 1;
348
349         int nativecontents = pointcontents(point);
350         if(this.watertype && this.watertype != nativecontents)
351         {
352                 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.watertype, nativecontents);
353                 if(this.contentstransition)
354                         this.contentstransition(this.watertype, nativecontents);
355         }
356
357         this.waterlevel = WATERLEVEL_NONE;
358         this.watertype = CONTENT_EMPTY;
359
360         int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
361         if(supercontents & DPCONTENTS_LIQUIDSMASK)
362         {
363                 this.watertype = nativecontents;
364                 this.waterlevel = WATERLEVEL_WETFEET;
365                 point.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
366                 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
367                 {
368                         this.waterlevel = WATERLEVEL_SWIMMING;
369                         point.z = this.origin.z + this.view_ofs.z;
370                         if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
371                                 this.waterlevel = WATERLEVEL_SUBMERGED;
372                 }
373         }
374
375         return this.waterlevel > 1;
376 }
377
378 void _Movetype_CheckWaterTransition(entity ent)  // SV_CheckWaterTransition
379 {
380         int contents = pointcontents(ent.origin);
381
382         if(!ent.watertype)
383         {
384                 // just spawned here
385                 if(!GAMEPLAYFIX_WATERTRANSITION(ent))
386                 {
387                         ent.watertype = contents;
388                         ent.waterlevel = 1;
389                         return;
390                 }
391         }
392         else if(ent.watertype != contents)
393         {
394                 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.origin), pointcontents(ent.origin), ent.watertype, contents);
395                 if(ent.contentstransition)
396                         ent.contentstransition(ent.watertype, contents);
397         }
398
399         if(contents <= CONTENT_WATER)
400         {
401                 ent.watertype = contents;
402                 ent.waterlevel = 1;
403         }
404         else
405         {
406                 ent.watertype = CONTENT_EMPTY;
407                 ent.waterlevel = (GAMEPLAYFIX_WATERTRANSITION(ent) ? 0 : contents);
408         }
409 }
410
411 void _Movetype_Impact(entity this, entity oth)  // SV_Impact
412 {
413         if(!this && !oth)
414                 return;
415
416         // due to a lack of pointers in QC, we must save the trace values and restore them for other functions
417         bool save_trace_allsolid = trace_allsolid;
418         bool save_trace_startsolid = trace_startsolid;
419         float save_trace_fraction = trace_fraction;
420         bool save_trace_inwater = trace_inwater;
421         bool save_trace_inopen = trace_inopen;
422         vector save_trace_endpos = trace_endpos;
423         vector save_trace_plane_normal = trace_plane_normal;
424         float save_trace_plane_dist = trace_plane_dist;
425         entity save_trace_ent = trace_ent;
426         int save_trace_dpstartcontents = trace_dpstartcontents;
427         int save_trace_dphitcontents = trace_dphitcontents;
428         int save_trace_dphitq3surfaceflags = trace_dphitq3surfaceflags;
429         string save_trace_dphittexturename = trace_dphittexturename;
430
431         if(this.solid != SOLID_NOT && gettouch(this))
432                 gettouch(this)(this, oth);
433
434         if(oth.solid != SOLID_NOT && gettouch(oth))
435                 gettouch(oth)(oth, this);
436
437         trace_allsolid = save_trace_allsolid;
438         trace_startsolid = save_trace_startsolid;
439         trace_fraction = save_trace_fraction;
440         trace_inwater = save_trace_inwater;
441         trace_inopen = save_trace_inopen;
442         trace_endpos = save_trace_endpos;
443         trace_plane_normal = save_trace_plane_normal;
444         trace_plane_dist = save_trace_plane_dist;
445         trace_ent = save_trace_ent;
446         trace_dpstartcontents = save_trace_dpstartcontents;
447         trace_dphitcontents = save_trace_dphitcontents;
448         trace_dphitq3surfaceflags = save_trace_dphitq3surfaceflags;
449         trace_dphittexturename = save_trace_dphittexturename;
450 }
451
452 void _Movetype_LinkEdict_TouchAreaGrid(entity this)  // SV_LinkEdict_TouchAreaGrid
453 {
454         if(this.solid == SOLID_NOT)
455                 return;
456
457         // due to a lack of pointers in QC, we must save the trace values and restore them for other functions
458         bool save_trace_allsolid = trace_allsolid;
459         bool save_trace_startsolid = trace_startsolid;
460         float save_trace_fraction = trace_fraction;
461         bool save_trace_inwater = trace_inwater;
462         bool save_trace_inopen = trace_inopen;
463         vector save_trace_endpos = trace_endpos;
464         vector save_trace_plane_normal = trace_plane_normal;
465         float save_trace_plane_dist = trace_plane_dist;
466         entity save_trace_ent = trace_ent;
467         int save_trace_dpstartcontents = trace_dpstartcontents;
468         int save_trace_dphitcontents = trace_dphitcontents;
469         int save_trace_dphitq3surfaceflags = trace_dphitq3surfaceflags;
470         string save_trace_dphittexturename = trace_dphittexturename;
471
472     FOREACH_ENTITY_RADIUS_ORDERED(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
473                 if (it.solid == SOLID_TRIGGER && it != this)
474                 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
475                 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
476                 {
477                         trace_allsolid = false;
478                         trace_startsolid = false;
479                         trace_fraction = 1;
480                         trace_inwater = false;
481                         trace_inopen = true;
482                         trace_endpos = it.origin;
483                         trace_plane_normal = '0 0 1';
484                         trace_plane_dist = 0;
485                         trace_ent = this;
486                         trace_dpstartcontents = 0;
487                         trace_dphitcontents = 0;
488                         trace_dphitq3surfaceflags = 0;
489                         trace_dphittexturename = string_null;
490
491                         gettouch(it)(it, this);
492                 }
493     });
494
495         trace_allsolid = save_trace_allsolid;
496         trace_startsolid = save_trace_startsolid;
497         trace_fraction = save_trace_fraction;
498         trace_inwater = save_trace_inwater;
499         trace_inopen = save_trace_inopen;
500         trace_endpos = save_trace_endpos;
501         trace_plane_normal = save_trace_plane_normal;
502         trace_plane_dist = save_trace_plane_dist;
503         trace_ent = save_trace_ent;
504         trace_dpstartcontents = save_trace_dpstartcontents;
505         trace_dphitcontents = save_trace_dphitcontents;
506         trace_dphitq3surfaceflags = save_trace_dphitq3surfaceflags;
507         trace_dphittexturename = save_trace_dphittexturename;
508 }
509
510 bool autocvar__movetype_debug = false;
511 void _Movetype_LinkEdict(entity this, bool touch_triggers)  // SV_LinkEdict
512 {
513         if(autocvar__movetype_debug)
514         {
515                 vector mi, ma;
516                 if(this.solid == SOLID_BSP)
517                 {
518                         // TODO set the absolute bbox
519                         mi = this.mins;
520                         ma = this.maxs;
521                 }
522                 else
523                 {
524                         mi = this.mins;
525                         ma = this.maxs;
526                 }
527                 mi += this.origin;
528                 ma += this.origin;
529
530                 if(this.flags & FL_ITEM)
531                 {
532                         mi -= '15 15 1';
533                         ma += '15 15 1';
534                 }
535                 else
536                 {
537                         mi -= '1 1 1';
538                         ma += '1 1 1';
539                 }
540
541                 this.absmin = mi;
542                 this.absmax = ma;
543         }
544         else
545         {
546                 setorigin(this, this.origin); // calls SV_LinkEdict
547         #ifdef CSQC
548                 // NOTE: CSQC's version of setorigin doesn't expand
549                 this.absmin -= '1 1 1';
550                 this.absmax += '1 1 1';
551         #endif
552         }
553
554         if(touch_triggers)
555                 _Movetype_LinkEdict_TouchAreaGrid(this);
556 }
557
558 int _Movetype_ContentsMask(entity this)  // SV_GenericHitSuperContentsMask
559 {
560         if(this)
561         {
562                 if(this.dphitcontentsmask)
563                         return this.dphitcontentsmask;
564                 else if(this.solid == SOLID_SLIDEBOX)
565                 {
566                         if(this.flags & FL_MONSTER)
567                                 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_MONSTERCLIP;
568                         else
569                                 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
570                 }
571                 else if(this.solid == SOLID_CORPSE)
572                         return DPCONTENTS_SOLID | DPCONTENTS_BODY;
573                 else if(this.solid == SOLID_TRIGGER)
574                         return DPCONTENTS_SOLID | DPCONTENTS_BODY;
575                 else
576                         return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
577         }
578         else
579                 return DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
580 }
581
582 entity _Movetype_TestEntityPosition_ent;
583 bool _Movetype_TestEntityPosition(vector ofs)  // SV_TestEntityPosition
584 {
585     entity this = _Movetype_TestEntityPosition_ent;
586         vector org = this.origin + ofs;
587
588         //int cont = this.dphitcontentsmask;
589         //this.dphitcontentsmask = DPCONTENTS_SOLID;
590         tracebox(org, this.mins, this.maxs, this.origin, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
591         //this.dphitcontentsmask = cont;
592         if(trace_dpstartcontents & _Movetype_ContentsMask(this))
593                 return true;
594
595         if(vlen2(trace_endpos - this.origin) >= 0.0001)
596         {
597                 tracebox(trace_endpos, this.mins, this.maxs, trace_endpos, MOVE_NOMONSTERS, this);
598                 if(!trace_startsolid)
599                         this.origin = trace_endpos;
600         }
601         return false;
602 }
603
604 bool _Movetype_TestEntityPosition_Offset(int offset)
605 {
606         // NOTE: expects _Movetype_TestEntityPosition_ent to be set to the correct entity
607         // returns true if stuck
608
609     // start at 2, since the first position has already been checked
610     for(int j = 2; j <= offset; ++j)
611     {
612         if(!_Movetype_TestEntityPosition('0 0 -1' * j))
613                 return false;
614         if(!_Movetype_TestEntityPosition('0 0 1' * j))
615                 return false;
616     }
617
618         return true;
619 }
620
621 int _Movetype_UnstickEntity(entity this)  // SV_UnstickEntity
622 {
623     _Movetype_TestEntityPosition_ent = this;
624         if (!_Movetype_TestEntityPosition(' 0  0  0')) {
625             return UNSTICK_FINE;
626         }
627         #define X(v) if (_Movetype_TestEntityPosition(v))
628         X('0  0  -1') X(' 0  0  1')
629         X('-1  0  0') X(' 1  0  0')
630         X(' 0 -1  0') X(' 0  1  0')
631         X('-1 -1  0') X(' 1 -1  0')
632         X('-1  1  0') X(' 1  1  0')
633         #undef X
634         {
635         if(_Movetype_TestEntityPosition_Offset(rint((this.maxs.z - this.mins.z) * 0.36)))
636         {
637             LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)",
638                 etof(this), this.classname, vtos(this.origin));
639             return UNSTICK_STUCK;
640         }
641         }
642         LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)",
643                 etof(this), this.classname, vtos(this.origin));
644         _Movetype_LinkEdict(this, false);
645         return UNSTICK_FIXED;
646 }
647
648 void _Movetype_CheckStuck(entity this)  // SV_CheckStuck
649 {
650         int unstick = _Movetype_UnstickEntity(this); // sets test position entity
651         switch(unstick)
652         {
653                 case UNSTICK_FINE:
654                         this.oldorigin = this.origin;
655                         break;
656                 case UNSTICK_FIXED:
657                         break; // already sorted
658                 case UNSTICK_STUCK:
659                         vector offset = this.oldorigin - this.origin;
660                         if(!_Movetype_TestEntityPosition(offset))
661                                 _Movetype_LinkEdict(this, false);
662                         // couldn't unstick, should we warn about this?
663                         break;
664         }
665 }
666
667 vector _Movetype_ClipVelocity(vector vel, vector norm, float f)  // ClipVelocity
668 {
669         vel -= ((vel * norm) * norm) * f;
670
671         if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
672         if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
673         if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
674
675         return vel;
676 }
677
678 void _Movetype_PushEntityTrace(entity this, vector push)
679 {
680         vector end = this.origin + push;
681         int type;
682         if(this.move_nomonsters)
683                 type = max(0, this.move_nomonsters);
684         else if(this.move_movetype == MOVETYPE_FLYMISSILE)
685                 type = MOVE_MISSILE;
686         else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
687                 type = MOVE_WORLDONLY;
688         else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
689                 type = MOVE_NOMONSTERS;
690         else
691                 type = MOVE_NORMAL;
692
693         tracebox(this.origin, this.mins, this.maxs, end, type, this);
694 }
695
696 bool _Movetype_PushEntity(entity this, vector push, bool dolink)  // SV_PushEntity
697 {
698         _Movetype_PushEntityTrace(this, push);
699
700         // NOTE: this is a workaround for the QC's lack of a worldstartsolid trace parameter
701         if(trace_startsolid)
702         {
703                 int oldtype = this.move_nomonsters;
704                 this.move_nomonsters = MOVE_WORLDONLY;
705                 _Movetype_PushEntityTrace(this, push);
706                 this.move_nomonsters = oldtype;
707                 if(trace_startsolid)
708                         return true;
709         }
710
711         this.origin = trace_endpos;
712
713         vector last_origin = this.origin;
714
715         _Movetype_LinkEdict(this, dolink);
716
717         if((this.solid >= SOLID_TRIGGER && trace_fraction < 1 && (!IS_ONGROUND(this) || this.groundentity != trace_ent)))
718                 _Movetype_Impact(this, trace_ent);
719
720         return (this.origin == last_origin); // false if teleported by touch
721 }
722
723 void _Movetype_Physics_Frame(entity this, float movedt)
724 {
725         this.move_didgravity = -1;
726         switch (this.move_movetype)
727         {
728                 case MOVETYPE_PUSH:
729                 case MOVETYPE_FAKEPUSH:
730                         _Movetype_Physics_Push(this, movedt);
731                         break;
732                 case MOVETYPE_NONE:
733                         break;
734                 case MOVETYPE_FOLLOW:
735                         _Movetype_Physics_Follow(this);
736                         break;
737                 case MOVETYPE_NOCLIP:
738                         _Movetype_CheckWater(this);
739                         this.origin = this.origin + movedt * this.velocity;
740                         this.angles = this.angles + movedt * this.avelocity;
741                         _Movetype_LinkEdict(this, false);
742                         break;
743                 case MOVETYPE_STEP:
744                         _Movetype_Physics_Step(this, movedt);
745                         break;
746                 case MOVETYPE_WALK:
747                         _Movetype_Physics_Walk(this, movedt);
748                         break;
749                 case MOVETYPE_TOSS:
750                 case MOVETYPE_BOUNCE:
751                 case MOVETYPE_BOUNCEMISSILE:
752                 case MOVETYPE_FLYMISSILE:
753                 case MOVETYPE_FLY:
754                 case MOVETYPE_FLY_WORLDONLY:
755                         _Movetype_Physics_Toss(this, movedt);
756                         break;
757                 case MOVETYPE_PHYSICS:
758                         break;
759         }
760 }
761
762 void _Movetype_Physics_ClientFrame(entity this, float movedt)
763 {
764         this.move_didgravity = -1;
765         switch (this.move_movetype)
766         {
767                 case MOVETYPE_PUSH:
768                 case MOVETYPE_FAKEPUSH:
769                         LOG_DEBUG("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
770                         break;
771                 case MOVETYPE_NONE:
772                         break;
773                 case MOVETYPE_FOLLOW:
774                         _Movetype_Physics_Follow(this);
775                         break;
776                 case MOVETYPE_NOCLIP:
777                         _Movetype_CheckWater(this);
778                         this.origin = this.origin + movedt * this.velocity;
779                         this.angles = this.angles + movedt * this.avelocity;
780                         break;
781                 case MOVETYPE_STEP:
782                         _Movetype_Physics_Step(this, movedt);
783                         break;
784                 case MOVETYPE_WALK:
785                 case MOVETYPE_FLY:
786                 case MOVETYPE_FLY_WORLDONLY:
787                         _Movetype_Physics_Walk(this, movedt);
788                         break;
789                 case MOVETYPE_TOSS:
790                 case MOVETYPE_BOUNCE:
791                 case MOVETYPE_BOUNCEMISSILE:
792                 case MOVETYPE_FLYMISSILE:
793                         if (GAMEPLAYFIX_UNSTICKPLAYERS(this) == 2)
794                                 _Movetype_CheckStuck(this);
795                         _Movetype_Physics_Toss(this, movedt);
796                         break;
797                 case MOVETYPE_PHYSICS:
798                         break;
799         }
800
801         //_Movetype_CheckVelocity(this);
802
803         _Movetype_LinkEdict(this, true);
804
805         //_Movetype_CheckVelocity(this);
806 }
807
808 void Movetype_Physics_NoMatchTicrate(entity this, float movedt, bool isclient)  // to be run every move frame
809 {
810         bool didmove = (this.move_time != 0);
811         this.move_time = time;
812
813         if(isclient)
814                 _Movetype_Physics_ClientFrame(this, movedt);
815         else
816         {
817                 // this doesn't apply to clients, and only applies to unmatched entities
818                 // don't run think/move on newly spawned projectiles as it messes up
819                 // movement interpolation and rocket trails, and is inconsistent with
820                 // respect to entities spawned in the same frame
821                 // (if an ent spawns a higher numbered ent, it moves in the same frame,
822                 //  but if it spawns a lower numbered ent, it doesn't - this never moves
823                 //  ents in the first frame regardless)
824                 if(!didmove && GAMEPLAYFIX_DELAYPROJECTILES(this) > 0)
825                         return;
826                 _Movetype_Physics_Frame(this, movedt);
827         }
828         if(wasfreed(this))
829                 return;
830
831         setorigin(this, this.origin);
832 }
833
834 void Movetype_Physics_NoMatchServer(entity this)  // optimized
835 {
836         float movedt = time - this.move_time;
837         this.move_time = time;
838
839         _Movetype_Physics_Frame(this, movedt);
840         if(wasfreed(this))
841                 return;
842
843         setorigin(this, this.origin);
844 }
845
846 void Movetype_Physics_MatchServer(entity this, bool sloppy)
847 {
848         Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
849 }
850
851 // saved .move_*
852 .vector tic_origin;
853 .vector tic_velocity;
854 .int tic_flags;
855 .vector tic_avelocity;
856 .vector tic_angles;
857
858 // saved .*
859 .vector tic_saved_origin;
860 .vector tic_saved_velocity;
861 .int tic_saved_flags;
862 .vector tic_saved_avelocity;
863 .vector tic_saved_angles;
864 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy)  // SV_Physics_Entity
865 {
866         // this hack exists to contain the physics feature
867         // (so entities can place themselves in the world and not need to update .tic_* themselves)
868 #define X(s) \
869         if(this.(s) != this.tic_saved_##s) \
870                 this.tic_##s = this.(s)
871
872         X(origin);
873         X(velocity);
874         X(flags);
875         X(avelocity);
876         X(angles);
877 #undef X
878
879         this.flags = this.tic_flags;
880         this.velocity = this.tic_velocity;
881         setorigin(this, this.tic_origin);
882         this.avelocity = this.tic_avelocity;
883         this.angles = this.tic_angles;
884
885         if(tr <= 0)
886         {
887                 Movetype_Physics_NoMatchServer(this);
888
889                 this.tic_saved_flags = this.tic_flags = this.flags;
890                 this.tic_saved_velocity = this.tic_velocity = this.velocity;
891                 this.tic_saved_origin = this.tic_origin = this.origin;
892                 this.tic_saved_avelocity = this.tic_avelocity = this.avelocity;
893                 this.tic_saved_angles = this.tic_angles = this.angles;
894                 return;
895         }
896
897         float dt = time - this.move_time;
898
899         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)
900         dt -= n * tr;
901         this.move_time += n * tr;
902
903         if(!this.move_didgravity)
904                 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.tic_flags & FL_ONGROUND));
905
906         for (int j = 0; j < n; ++j)
907         {
908                 _Movetype_Physics_Frame(this, tr);
909                 if(wasfreed(this))
910                         return;
911         }
912
913         // update the physics fields
914         this.tic_origin = this.origin;
915         this.tic_velocity = this.velocity;
916         this.tic_avelocity = this.avelocity;
917         this.tic_angles = this.angles;
918         this.tic_flags = this.flags;
919
920         // restore their actual values
921         this.flags = this.tic_saved_flags;
922         this.velocity = this.tic_saved_velocity;
923         setorigin(this, this.tic_saved_origin);
924         //this.avelocity = this.tic_saved_avelocity;
925         this.angles = this.tic_saved_angles;
926
927         this.avelocity = this.tic_avelocity;
928
929         if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.tic_flags & FL_ONGROUND))
930         {
931                 // now continue the move from move_time to time
932                 this.velocity = this.tic_velocity;
933
934                 if(this.move_didgravity > 0)
935                 {
936                         this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
937                             * dt
938                             * ((this.gravity) ? this.gravity : 1)
939                             * PHYS_GRAVITY(this);
940                 }
941
942                 this.angles = this.tic_angles + dt * this.avelocity;
943
944                 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
945                 {
946                         setorigin(this, this.tic_origin + dt * this.velocity);
947                 }
948                 else
949                 {
950                         setorigin(this, this.tic_origin);
951                         _Movetype_PushEntityTrace(this, dt * this.velocity);
952                         if(!trace_startsolid)
953                                 setorigin(this, trace_endpos);
954                         else
955                                 setorigin(this, this.tic_saved_origin);
956                 }
957
958                 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
959                         this.velocity_z -= 0.5 * dt * ((this.gravity) ? this.gravity : 1) * PHYS_GRAVITY(this);
960         }
961         else
962         {
963                 this.velocity = this.tic_velocity;
964                 this.angles = this.tic_angles;
965                 setorigin(this, this.tic_origin);
966         }
967
968         this.flags = this.tic_flags;
969
970         this.tic_saved_flags = this.flags;
971         this.tic_saved_velocity = this.velocity;
972         this.tic_saved_origin = this.origin;
973         this.tic_saved_avelocity = this.avelocity;
974         this.tic_saved_angles = this.angles;
975 }