1 #include "movetypes.qh"
2 #include "../player.qh"
5 #include <client/defs.qh>
6 #include <common/stats.qh>
7 #include <common/util.qh>
8 #include <lib/csqcmodel/common.qh>
9 #include <common/t_items.qh>
12 #include <server/autocvars.qh>
16 void set_movetype(entity this, int mt)
18 this.move_movetype = mt;
19 if (mt == MOVETYPE_PHYSICS || mt == MOVETYPE_PUSH || mt == MOVETYPE_FAKEPUSH) {
20 this.move_qcphysics = false;
22 if (!this.move_qcphysics) {
27 void set_movetype(entity this, int mt)
29 this.move_movetype = mt;
33 void _Movetype_WallFriction(entity this, vector stepnormal) // SV_WallFriction
37 makevectors(this.v_angle);
38 d = (stepnormal * v_forward) + 0.5;
42 i = (stepnormal * this.velocity);
43 into = i * stepnormal;
44 side = this.velocity - into;
45 this.velocity_x = side.x * (1 * d);
46 this.velocity_y = side.y * (1 * d);
50 vector planes[MAX_CLIP_PLANES];
51 int _Movetype_FlyMove(entity this, float dt, bool applygravity, vector stepnormal, float stepheight) // SV_FlyMove
53 int blocked = 0, bumpcount;
54 int i, j, numplanes = 0;
55 float time_left = dt, grav = 0;
57 vector primal_velocity, original_velocity, restore_velocity;
59 for(i = 0; i < MAX_CLIP_PLANES; ++i)
64 this.move_didgravity = 1;
65 grav = dt * (PHYS_ENTGRAVITY(this) ? PHYS_ENTGRAVITY(this) : 1) * PHYS_GRAVITY(this);
67 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
69 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
70 this.velocity_z -= grav * 0.5;
72 this.velocity_z -= grav;
76 original_velocity = primal_velocity = restore_velocity = this.velocity;
78 for(bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
80 if(this.velocity == '0 0 0')
83 push = this.velocity * time_left;
84 _Movetype_PushEntity(this, push, true);
87 // we got teleported by a touch function
88 // let's abort the move
93 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
94 // abort move if we're stuck in the world (and didn't make it out)
95 if(trace_startsolid && trace_allsolid)
97 this.velocity = restore_velocity;
101 if(trace_fraction == 1)
104 float my_trace_fraction = trace_fraction;
105 vector my_trace_plane_normal = trace_plane_normal;
107 if(trace_plane_normal.z)
109 if(trace_plane_normal.z > 0.7)
116 //dprint("_Movetype_FlyMove: !trace_ent\n");
121 this.groundentity = trace_ent;
126 // step - handle it immediately
127 vector org = this.origin;
128 vector steppush = '0 0 1' * stepheight;
130 _Movetype_PushEntity(this, steppush, true);
136 _Movetype_PushEntity(this, push, true);
142 float trace2_fraction = trace_fraction;
143 steppush = '0 0 1' * (org_z - this.origin_z);
144 _Movetype_PushEntity(this, steppush, true);
151 // accept the new position if it made some progress...
152 if(fabs(this.origin_x - org_x) >= 0.03125 || fabs(this.origin_y - org_y) >= 0.03125)
154 trace_endpos = this.origin;
155 time_left *= 1 - trace2_fraction;
164 // step - return it to caller
166 // save the trace for player extrafriction
168 stepnormal = trace_plane_normal;
171 if(my_trace_fraction >= 0.001)
173 // actually covered some distance
174 original_velocity = this.velocity;
178 time_left *= 1 - my_trace_fraction;
180 // clipped to another plane
181 if(numplanes >= MAX_CLIP_PLANES)
183 // this shouldn't really happen
184 this.velocity = '0 0 0';
189 planes[numplanes] = my_trace_plane_normal;
192 // modify original_velocity so it parallels all of the clip planes
193 vector new_velocity = '0 0 0';
194 for (i = 0;i < numplanes;i++)
196 new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
197 for (j = 0;j < numplanes;j++)
202 if((new_velocity * planes[j]) < 0)
212 // go along this plane
213 this.velocity = new_velocity;
217 // go along the crease
220 this.velocity = '0 0 0';
224 vector dir = cross(planes[0], planes[1]);
225 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
226 float ilength = sqrt((dir * dir));
228 ilength = 1.0 / ilength;
232 float d = (dir * this.velocity);
233 this.velocity = dir * d;
236 // if current velocity is against the original velocity,
237 // stop dead to avoid tiny occilations in sloping corners
238 if((this.velocity * primal_velocity) <= 0)
240 this.velocity = '0 0 0';
245 // LordHavoc: this came from QW and allows you to get out of water more easily
246 if(GAMEPLAYFIX_EASIERWATERJUMP(this) && (this.flags & FL_WATERJUMP) && !(blocked & 8))
247 this.velocity = primal_velocity;
251 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
253 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
254 this.velocity_z -= grav * 0.5f;
261 void _Movetype_CheckVelocity(entity this) // SV_CheckVelocity
263 // if(vlen(this.velocity) < 0.0001)
264 // this.velocity = '0 0 0';
267 bool _Movetype_CheckWater(entity this) // SV_CheckWater
269 vector point = this.origin;
270 point.z += this.mins.z + 1;
272 int nativecontents = pointcontents(point);
273 if(this.watertype && this.watertype != nativecontents)
275 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.watertype, nativecontents);
276 if(this.contentstransition)
277 this.contentstransition(this.watertype, nativecontents);
280 this.waterlevel = WATERLEVEL_NONE;
281 this.watertype = CONTENT_EMPTY;
283 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
284 if(supercontents & DPCONTENTS_LIQUIDSMASK)
286 this.watertype = nativecontents;
287 this.waterlevel = WATERLEVEL_WETFEET;
288 point.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
289 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
291 this.waterlevel = WATERLEVEL_SWIMMING;
292 point.z = this.origin.z + this.view_ofs.z;
293 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
294 this.waterlevel = WATERLEVEL_SUBMERGED;
298 return this.waterlevel > 1;
301 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
303 int contents = pointcontents(ent.origin);
308 if(!GAMEPLAYFIX_WATERTRANSITION(ent))
310 ent.watertype = contents;
315 else if(ent.watertype != contents)
317 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.origin), pointcontents(ent.origin), ent.watertype, contents);
318 if(ent.contentstransition)
319 ent.contentstransition(ent.watertype, contents);
322 if(contents <= CONTENT_WATER)
324 ent.watertype = contents;
329 ent.watertype = CONTENT_EMPTY;
330 ent.waterlevel = (GAMEPLAYFIX_WATERTRANSITION(ent) ? 0 : contents);
334 void _Movetype_Impact(entity this, entity oth) // SV_Impact
337 gettouch(this)(this, oth);
340 gettouch(oth)(oth, this);
343 void _Movetype_LinkEdict_TouchAreaGrid(entity this) // SV_LinkEdict_TouchAreaGrid
345 if(this.solid == SOLID_NOT)
348 FOREACH_ENTITY_RADIUS(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
349 if (it.solid == SOLID_TRIGGER && it != this)
350 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
351 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
353 trace_allsolid = false;
354 trace_startsolid = false;
356 trace_inwater = false;
358 trace_endpos = it.origin;
359 trace_plane_normal = '0 0 1';
360 trace_plane_dist = 0;
363 gettouch(it)(it, this);
368 void _Movetype_LinkEdict(entity this, bool touch_triggers) // SV_LinkEdict
371 if(this.solid == SOLID_BSP)
373 // TODO set the absolute bbox
385 if(this.flags & FL_ITEM)
408 _Movetype_LinkEdict_TouchAreaGrid(this);
411 entity _Movetype_TestEntityPosition_ent;
412 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
414 entity this = _Movetype_TestEntityPosition_ent;
415 // vector org = this.origin + ofs;
417 int cont = this.dphitcontentsmask;
418 this.dphitcontentsmask = DPCONTENTS_SOLID;
419 tracebox(this.origin, this.mins, this.maxs, this.origin, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
420 this.dphitcontentsmask = cont;
425 if(vdist(trace_endpos - this.origin, >, 0.0001))
426 this.origin = trace_endpos;
430 bool _Movetype_UnstickEntity(entity this) // SV_UnstickEntity
432 _Movetype_TestEntityPosition_ent = this;
433 if (!_Movetype_TestEntityPosition(' 0 0 0')) {
436 #define X(v) if (_Movetype_TestEntityPosition(v))
437 X('-1 0 0') X(' 1 0 0')
438 X(' 0 -1 0') X(' 0 1 0')
439 X('-1 -1 0') X(' 1 -1 0')
440 X('-1 1 0') X(' 1 1 0')
444 if (_Movetype_TestEntityPosition('0 0 -1' * i)) \
445 if (_Movetype_TestEntityPosition('0 0 1' * i))
446 X(01) X(02) X(03) X(04) X(05) X(06) X(07) X(08)
447 X(09) X(10) X(11) X(12) X(13) X(14) X(15) X(16)
451 LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)",
452 etof(this), this.classname, vtos(this.origin));
456 LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)",
457 etof(this), this.classname, vtos(this.origin));
458 _Movetype_LinkEdict(this, true);
462 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
464 vel -= ((vel * norm) * norm) * f;
466 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
467 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
468 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
473 void _Movetype_PushEntityTrace(entity this, vector push)
475 vector end = this.origin + push;
477 if(this.move_nomonsters)
478 type = max(0, this.move_nomonsters);
479 else if(this.move_movetype == MOVETYPE_FLYMISSILE)
481 else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
482 type = MOVE_WORLDONLY;
483 else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
484 type = MOVE_NOMONSTERS;
488 tracebox(this.origin, this.mins, this.maxs, end, type, this);
491 float _Movetype_PushEntity(entity this, vector push, bool failonstartsolid) // SV_PushEntity
493 _Movetype_PushEntityTrace(this, push);
495 if(trace_startsolid && failonstartsolid)
496 return trace_fraction;
498 this.origin = trace_endpos;
500 if(trace_fraction < 1)
501 if(this.solid >= SOLID_TRIGGER && (!IS_ONGROUND(this) || (this.groundentity != trace_ent)))
502 _Movetype_Impact(this, trace_ent);
504 return trace_fraction;
510 // matrix version of makevectors, sets v_forward, v_right and v_up
511 void makevectors_matrix(vector myangles) // AngleVectorsFLU
513 v_forward = v_right = v_up = '0 0 0';
515 float y = myangles.y * (M_PI * 2 / 360);
518 float p = myangles.x * (M_PI * 2 / 360);
523 v_forward.x = cp * cy;
524 v_forward.y = cp * sy;
531 float r = myangles.z * (M_PI * 2 / 360);
536 v_right.x = sr * sp * cy + cr * -sy;
537 v_right.y = sr * sp * sy + cr * cy;
542 v_up.x = cr * sp * cy + -sr * -sy;
543 v_up.y = cr * sp * sy + -sr * cy;
565 void _Movetype_Physics_Frame(entity this, float movedt)
567 this.move_didgravity = -1;
568 switch (this.move_movetype)
571 case MOVETYPE_FAKEPUSH:
572 LOG_DEBUGF("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
576 case MOVETYPE_FOLLOW:
577 _Movetype_Physics_Follow(this);
579 case MOVETYPE_NOCLIP:
580 _Movetype_CheckWater(this);
581 this.origin = this.origin + movedt * this.velocity;
582 this.angles = this.angles + movedt * this.avelocity;
583 _Movetype_LinkEdict(this, false);
586 _Movetype_Physics_Step(this, movedt);
589 _Movetype_Physics_Walk(this, movedt);
592 case MOVETYPE_BOUNCE:
593 case MOVETYPE_BOUNCEMISSILE:
594 case MOVETYPE_FLYMISSILE:
596 case MOVETYPE_FLY_WORLDONLY:
597 _Movetype_Physics_Toss(this, movedt);
598 _Movetype_LinkEdict(this, true);
600 case MOVETYPE_PHYSICS:
605 void _Movetype_Physics_ClientFrame(entity this, float movedt)
607 this.move_didgravity = -1;
608 switch (this.move_movetype)
611 case MOVETYPE_FAKEPUSH:
612 LOG_DEBUGF("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
616 case MOVETYPE_FOLLOW:
617 _Movetype_Physics_Follow(this);
619 case MOVETYPE_NOCLIP:
620 _Movetype_CheckWater(this);
621 this.origin = this.origin + movedt * this.velocity;
622 this.angles = this.angles + movedt * this.avelocity;
623 _Movetype_LinkEdict(this, false);
626 _Movetype_Physics_Step(this, movedt);
630 case MOVETYPE_FLY_WORLDONLY:
631 _Movetype_Physics_Walk(this, movedt);
634 case MOVETYPE_BOUNCE:
635 case MOVETYPE_BOUNCEMISSILE:
636 case MOVETYPE_FLYMISSILE:
637 _Movetype_Physics_Toss(this, movedt);
639 case MOVETYPE_PHYSICS:
644 void Movetype_Physics_NoMatchTicrate(entity this, float movedt, bool isclient) // to be run every move frame
646 this.move_time = time;
649 _Movetype_Physics_ClientFrame(this, movedt);
651 _Movetype_Physics_Frame(this, movedt);
655 setorigin(this, this.origin);
658 void Movetype_Physics_NoMatchServer(entity this) // optimized
660 float movedt = time - this.move_time;
661 this.move_time = time;
663 _Movetype_Physics_Frame(this, movedt);
667 setorigin(this, this.origin);
670 void Movetype_Physics_MatchServer(entity this, bool sloppy)
672 Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
676 .vector tic_velocity;
678 .vector tic_avelocity;
681 .vector tic_saved_origin;
682 .vector tic_saved_velocity;
683 .int tic_saved_flags;
684 .vector tic_saved_avelocity;
685 .vector tic_saved_angles;
686 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy) // SV_Physics_Entity
689 if(this.(s) != this.tic_saved_##s) \
690 this.tic_##s = this.(s)
701 this.flags = this.tic_flags;
702 this.velocity = this.tic_velocity;
703 this.origin = this.tic_origin;
704 this.avelocity = this.tic_avelocity;
705 this.angles = this.tic_angles;
706 Movetype_Physics_NoMatchServer(this);
707 this.tic_origin = this.origin;
708 this.tic_velocity = this.velocity;
709 this.tic_avelocity = this.avelocity;
710 this.tic_angles = this.angles;
711 this.tic_flags = this.flags;
713 this.tic_saved_flags = this.flags;
714 this.tic_saved_velocity = this.velocity;
715 this.tic_saved_origin = this.origin;
716 this.tic_saved_avelocity = this.avelocity;
717 this.tic_saved_angles = this.angles;
721 float dt = time - this.move_time;
723 int n = max(0, floor(dt / tr));
725 this.move_time += n * tr;
727 if(!this.move_didgravity)
728 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.tic_flags & FL_ONGROUND));
730 for (int i = 0; i < n; ++i)
732 this.flags = this.tic_flags;
733 this.velocity = this.tic_velocity;
734 setorigin(this, this.tic_origin);
735 this.avelocity = this.tic_avelocity;
736 this.angles = this.tic_angles;
737 _Movetype_Physics_Frame(this, tr);
738 this.tic_origin = this.origin;
739 this.tic_velocity = this.velocity;
740 this.tic_avelocity = this.avelocity;
741 this.tic_angles = this.angles;
742 this.tic_flags = this.flags;
747 this.avelocity = this.tic_avelocity;
749 if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.tic_flags & FL_ONGROUND))
751 // now continue the move from move_time to time
752 this.velocity = this.tic_velocity;
754 if(this.move_didgravity > 0)
756 this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
758 * (this.gravity ? this.gravity : 1)
759 * PHYS_GRAVITY(this);
762 this.angles = this.tic_angles + dt * this.avelocity;
764 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
766 setorigin(this, this.tic_origin + dt * this.velocity);
770 vector oldorg = this.origin;
771 this.origin = this.tic_origin;
772 _Movetype_PushEntityTrace(this, dt * this.velocity);
773 this.origin = oldorg;
774 if(!trace_startsolid)
775 setorigin(this, trace_endpos);
778 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
779 this.velocity_z -= 0.5 * dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
783 this.velocity = this.tic_velocity;
784 this.angles = this.tic_angles;
785 setorigin(this, this.tic_origin);
788 this.tic_saved_flags = this.flags;
789 this.tic_saved_velocity = this.velocity;
790 this.tic_saved_origin = this.origin;
791 this.tic_saved_avelocity = this.avelocity;
792 this.tic_saved_angles = this.angles;