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