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