1 #include "movetypes.qh"
4 void set_movetype(entity this, int mt)
6 this.move_movetype = mt;
7 if (mt == MOVETYPE_PHYSICS || mt == MOVETYPE_PUSH || mt == MOVETYPE_FAKEPUSH) {
8 this.move_qcphysics = false;
10 this.movetype = (this.move_qcphysics) ? MOVETYPE_NONE : mt;
13 void set_movetype(entity this, int mt)
15 this.move_movetype = mt;
19 void _Movetype_WallFriction(entity this, vector stepnormal) // SV_WallFriction
23 makevectors(this.v_angle);
24 d = (stepnormal * v_forward) + 0.5;
28 i = (stepnormal * this.velocity);
29 into = i * stepnormal;
30 side = this.velocity - into;
31 this.velocity_x = side.x * (1 * d);
32 this.velocity_y = side.y * (1 * d);
36 vector planes[MAX_CLIP_PLANES];
37 int _Movetype_FlyMove(entity this, float dt, bool applygravity, vector stepnormal, float stepheight) // SV_FlyMove
40 int i, j, numplanes = 0;
41 float time_left = dt, grav = 0;
43 vector primal_velocity, original_velocity, restore_velocity;
45 for(i = 0; i < MAX_CLIP_PLANES; ++i)
50 this.move_didgravity = 1;
51 grav = dt * (PHYS_ENTGRAVITY(this) ? PHYS_ENTGRAVITY(this) : 1) * PHYS_GRAVITY(this);
53 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
55 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
56 this.velocity_z -= grav * 0.5;
58 this.velocity_z -= grav;
62 original_velocity = primal_velocity = restore_velocity = this.velocity;
64 for(int bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
66 if(this.velocity == '0 0 0')
69 push = this.velocity * time_left;
70 _Movetype_PushEntity(this, push, true);
73 // we got teleported by a touch function
74 // let's abort the move
79 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
80 // abort move if we're stuck in the world (and didn't make it out)
81 if(trace_startsolid && trace_allsolid)
83 this.velocity = restore_velocity;
87 if(trace_fraction == 1)
90 float my_trace_fraction = trace_fraction;
91 vector my_trace_plane_normal = trace_plane_normal;
93 if(trace_plane_normal.z)
95 if(trace_plane_normal.z > 0.7)
102 //dprint("_Movetype_FlyMove: !trace_ent\n");
107 this.groundentity = trace_ent;
112 // step - handle it immediately
113 vector org = this.origin;
114 vector steppush = '0 0 1' * stepheight;
116 _Movetype_PushEntity(this, steppush, true);
122 _Movetype_PushEntity(this, push, true);
128 float trace2_fraction = trace_fraction;
129 steppush = '0 0 1' * (org.z - this.origin_z);
130 _Movetype_PushEntity(this, steppush, true);
137 // accept the new position if it made some progress...
138 if(fabs(this.origin_x - org.x) >= 0.03125 || fabs(this.origin_y - org.y) >= 0.03125)
140 trace_endpos = this.origin;
141 time_left *= 1 - trace2_fraction;
150 // step - return it to caller
152 // save the trace for player extrafriction
154 stepnormal = trace_plane_normal;
157 if(my_trace_fraction >= 0.001)
159 // actually covered some distance
160 original_velocity = this.velocity;
164 time_left *= 1 - my_trace_fraction;
166 // clipped to another plane
167 if(numplanes >= MAX_CLIP_PLANES)
169 // this shouldn't really happen
170 this.velocity = '0 0 0';
175 planes[numplanes] = my_trace_plane_normal;
178 // modify original_velocity so it parallels all of the clip planes
179 vector new_velocity = '0 0 0';
180 for (i = 0;i < numplanes;i++)
182 new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
183 for (j = 0;j < numplanes;j++)
188 if((new_velocity * planes[j]) < 0)
198 // go along this plane
199 this.velocity = new_velocity;
203 // go along the crease
206 this.velocity = '0 0 0';
210 vector dir = cross(planes[0], planes[1]);
211 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
212 float ilength = sqrt((dir * dir));
214 ilength = 1.0 / ilength;
218 float d = (dir * this.velocity);
219 this.velocity = dir * d;
222 // if current velocity is against the original velocity,
223 // stop dead to avoid tiny occilations in sloping corners
224 if((this.velocity * primal_velocity) <= 0)
226 this.velocity = '0 0 0';
231 // LordHavoc: this came from QW and allows you to get out of water more easily
232 if(GAMEPLAYFIX_EASIERWATERJUMP(this) && (this.flags & FL_WATERJUMP) && !(blocked & 8))
233 this.velocity = primal_velocity;
237 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
239 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
240 this.velocity_z -= grav * 0.5f;
247 void _Movetype_CheckVelocity(entity this) // SV_CheckVelocity
249 // if(vlen(this.velocity) < 0.0001)
250 // this.velocity = '0 0 0';
253 bool _Movetype_CheckWater(entity this) // SV_CheckWater
255 vector point = this.origin;
256 point.z += this.mins.z + 1;
258 int nativecontents = pointcontents(point);
259 if(this.watertype && this.watertype != nativecontents)
261 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.watertype, nativecontents);
262 if(this.contentstransition)
263 this.contentstransition(this.watertype, nativecontents);
266 this.waterlevel = WATERLEVEL_NONE;
267 this.watertype = CONTENT_EMPTY;
269 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
270 if(supercontents & DPCONTENTS_LIQUIDSMASK)
272 this.watertype = nativecontents;
273 this.waterlevel = WATERLEVEL_WETFEET;
274 point.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
275 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
277 this.waterlevel = WATERLEVEL_SWIMMING;
278 point.z = this.origin.z + this.view_ofs.z;
279 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
280 this.waterlevel = WATERLEVEL_SUBMERGED;
284 return this.waterlevel > 1;
287 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
289 int contents = pointcontents(ent.origin);
294 if(!GAMEPLAYFIX_WATERTRANSITION(ent))
296 ent.watertype = contents;
301 else if(ent.watertype != contents)
303 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.origin), pointcontents(ent.origin), ent.watertype, contents);
304 if(ent.contentstransition)
305 ent.contentstransition(ent.watertype, contents);
308 if(contents <= CONTENT_WATER)
310 ent.watertype = contents;
315 ent.watertype = CONTENT_EMPTY;
316 ent.waterlevel = (GAMEPLAYFIX_WATERTRANSITION(ent) ? 0 : contents);
320 void _Movetype_Impact(entity this, entity oth) // SV_Impact
323 gettouch(this)(this, oth);
326 gettouch(oth)(oth, this);
329 void _Movetype_LinkEdict_TouchAreaGrid(entity this) // SV_LinkEdict_TouchAreaGrid
331 if(this.solid == SOLID_NOT)
334 FOREACH_ENTITY_RADIUS(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
335 if (it.solid == SOLID_TRIGGER && it != this)
336 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
337 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
339 trace_allsolid = false;
340 trace_startsolid = false;
342 trace_inwater = false;
344 trace_endpos = it.origin;
345 trace_plane_normal = '0 0 1';
346 trace_plane_dist = 0;
349 gettouch(it)(it, this);
354 void _Movetype_LinkEdict(entity this, bool touch_triggers) // SV_LinkEdict
357 if(this.solid == SOLID_BSP)
359 // TODO set the absolute bbox
371 if(this.flags & FL_ITEM)
386 _Movetype_LinkEdict_TouchAreaGrid(this);
389 entity _Movetype_TestEntityPosition_ent;
390 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
392 entity this = _Movetype_TestEntityPosition_ent;
393 // vector org = this.origin + ofs;
395 int cont = this.dphitcontentsmask;
396 this.dphitcontentsmask = DPCONTENTS_SOLID;
397 tracebox(this.origin, this.mins, this.maxs, this.origin, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
398 this.dphitcontentsmask = cont;
403 if(vdist(trace_endpos - this.origin, >, 0.0001))
404 this.origin = trace_endpos;
408 bool _Movetype_UnstickEntity(entity this) // SV_UnstickEntity
410 _Movetype_TestEntityPosition_ent = this;
411 if (!_Movetype_TestEntityPosition(' 0 0 0')) {
414 #define X(v) if (_Movetype_TestEntityPosition(v))
415 X('-1 0 0') X(' 1 0 0')
416 X(' 0 -1 0') X(' 0 1 0')
417 X('-1 -1 0') X(' 1 -1 0')
418 X('-1 1 0') X(' 1 1 0')
422 if (_Movetype_TestEntityPosition('0 0 -1' * i)) \
423 if (_Movetype_TestEntityPosition('0 0 1' * i))
424 X(01) X(02) X(03) X(04) X(05) X(06) X(07) X(08)
425 X(09) X(10) X(11) X(12) X(13) X(14) X(15) X(16)
429 LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)",
430 etof(this), this.classname, vtos(this.origin));
434 LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)",
435 etof(this), this.classname, vtos(this.origin));
436 _Movetype_LinkEdict(this, true);
440 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
442 vel -= ((vel * norm) * norm) * f;
444 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
445 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
446 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
451 void _Movetype_PushEntityTrace(entity this, vector push)
453 vector end = this.origin + push;
455 if(this.move_nomonsters)
456 type = max(0, this.move_nomonsters);
457 else if(this.move_movetype == MOVETYPE_FLYMISSILE)
459 else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
460 type = MOVE_WORLDONLY;
461 else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
462 type = MOVE_NOMONSTERS;
466 tracebox(this.origin, this.mins, this.maxs, end, type, this);
469 float _Movetype_PushEntity(entity this, vector push, bool failonstartsolid) // SV_PushEntity
471 _Movetype_PushEntityTrace(this, push);
473 if(trace_startsolid && failonstartsolid)
474 return trace_fraction;
476 this.origin = trace_endpos;
478 if(trace_fraction < 1)
479 if(this.solid >= SOLID_TRIGGER && (!IS_ONGROUND(this) || (this.groundentity != trace_ent)))
480 _Movetype_Impact(this, trace_ent);
482 return trace_fraction;
489 void _Movetype_Physics_Frame(entity this, float movedt)
491 this.move_didgravity = -1;
492 switch (this.move_movetype)
495 case MOVETYPE_FAKEPUSH:
496 LOG_DEBUGF("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
500 case MOVETYPE_FOLLOW:
501 _Movetype_Physics_Follow(this);
503 case MOVETYPE_NOCLIP:
504 _Movetype_CheckWater(this);
505 this.origin = this.origin + movedt * this.velocity;
506 this.angles = this.angles + movedt * this.avelocity;
507 _Movetype_LinkEdict(this, false);
510 _Movetype_Physics_Step(this, movedt);
513 _Movetype_Physics_Walk(this, movedt);
516 case MOVETYPE_BOUNCE:
517 case MOVETYPE_BOUNCEMISSILE:
518 case MOVETYPE_FLYMISSILE:
520 case MOVETYPE_FLY_WORLDONLY:
521 _Movetype_Physics_Toss(this, movedt);
522 _Movetype_LinkEdict(this, true);
524 case MOVETYPE_PHYSICS:
529 void _Movetype_Physics_ClientFrame(entity this, float movedt)
531 this.move_didgravity = -1;
532 switch (this.move_movetype)
535 case MOVETYPE_FAKEPUSH:
536 LOG_DEBUGF("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
540 case MOVETYPE_FOLLOW:
541 _Movetype_Physics_Follow(this);
543 case MOVETYPE_NOCLIP:
544 _Movetype_CheckWater(this);
545 this.origin = this.origin + movedt * this.velocity;
546 this.angles = this.angles + movedt * this.avelocity;
547 _Movetype_LinkEdict(this, false);
550 _Movetype_Physics_Step(this, movedt);
554 case MOVETYPE_FLY_WORLDONLY:
555 _Movetype_Physics_Walk(this, movedt);
558 case MOVETYPE_BOUNCE:
559 case MOVETYPE_BOUNCEMISSILE:
560 case MOVETYPE_FLYMISSILE:
561 _Movetype_Physics_Toss(this, movedt);
563 case MOVETYPE_PHYSICS:
568 void Movetype_Physics_NoMatchTicrate(entity this, float movedt, bool isclient) // to be run every move frame
570 this.move_time = time;
573 _Movetype_Physics_ClientFrame(this, movedt);
575 _Movetype_Physics_Frame(this, movedt);
579 setorigin(this, this.origin);
582 void Movetype_Physics_NoMatchServer(entity this) // optimized
584 float movedt = time - this.move_time;
585 this.move_time = time;
587 _Movetype_Physics_Frame(this, movedt);
591 setorigin(this, this.origin);
594 void Movetype_Physics_MatchServer(entity this, bool sloppy)
596 Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
600 .vector tic_velocity;
602 .vector tic_avelocity;
605 .vector tic_saved_origin;
606 .vector tic_saved_velocity;
607 .int tic_saved_flags;
608 .vector tic_saved_avelocity;
609 .vector tic_saved_angles;
610 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy) // SV_Physics_Entity
613 if(this.(s) != this.tic_saved_##s) \
614 this.tic_##s = this.(s)
625 this.flags = this.tic_flags;
626 this.velocity = this.tic_velocity;
627 this.origin = this.tic_origin;
628 this.avelocity = this.tic_avelocity;
629 this.angles = this.tic_angles;
630 Movetype_Physics_NoMatchServer(this);
631 this.tic_origin = this.origin;
632 this.tic_velocity = this.velocity;
633 this.tic_avelocity = this.avelocity;
634 this.tic_angles = this.angles;
635 this.tic_flags = this.flags;
637 this.tic_saved_flags = this.flags;
638 this.tic_saved_velocity = this.velocity;
639 this.tic_saved_origin = this.origin;
640 this.tic_saved_avelocity = this.avelocity;
641 this.tic_saved_angles = this.angles;
645 float dt = time - this.move_time;
647 int n = max(0, floor(dt / tr));
649 this.move_time += n * tr;
651 if(!this.move_didgravity)
652 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.tic_flags & FL_ONGROUND));
654 for (int i = 0; i < n; ++i)
656 this.flags = this.tic_flags;
657 this.velocity = this.tic_velocity;
658 setorigin(this, this.tic_origin);
659 this.avelocity = this.tic_avelocity;
660 this.angles = this.tic_angles;
661 _Movetype_Physics_Frame(this, tr);
662 this.tic_origin = this.origin;
663 this.tic_velocity = this.velocity;
664 this.tic_avelocity = this.avelocity;
665 this.tic_angles = this.angles;
666 this.tic_flags = this.flags;
671 this.avelocity = this.tic_avelocity;
673 if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.tic_flags & FL_ONGROUND))
675 // now continue the move from move_time to time
676 this.velocity = this.tic_velocity;
678 if(this.move_didgravity > 0)
680 this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
682 * (this.gravity ? this.gravity : 1)
683 * PHYS_GRAVITY(this);
686 this.angles = this.tic_angles + dt * this.avelocity;
688 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
690 setorigin(this, this.tic_origin + dt * this.velocity);
694 vector oldorg = this.origin;
695 this.origin = this.tic_origin;
696 _Movetype_PushEntityTrace(this, dt * this.velocity);
697 this.origin = oldorg;
698 if(!trace_startsolid)
699 setorigin(this, trace_endpos);
702 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
703 this.velocity_z -= 0.5 * dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
707 this.velocity = this.tic_velocity;
708 this.angles = this.tic_angles;
709 setorigin(this, this.tic_origin);
712 this.tic_saved_flags = this.flags;
713 this.tic_saved_velocity = this.velocity;
714 this.tic_saved_origin = this.origin;
715 this.tic_saved_avelocity = this.avelocity;
716 this.tic_saved_angles = this.angles;