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 this.movetype = (this.move_qcphysics) ? MOVETYPE_NONE : mt;
25 void set_movetype(entity this, int mt)
27 this.move_movetype = mt;
31 void _Movetype_WallFriction(entity this, vector stepnormal) // SV_WallFriction
35 makevectors(this.v_angle);
36 d = (stepnormal * v_forward) + 0.5;
40 i = (stepnormal * this.velocity);
41 into = i * stepnormal;
42 side = this.velocity - into;
43 this.velocity_x = side.x * (1 * d);
44 this.velocity_y = side.y * (1 * d);
48 vector planes[MAX_CLIP_PLANES];
49 int _Movetype_FlyMove(entity this, float dt, bool applygravity, vector stepnormal, float stepheight) // SV_FlyMove
51 int blocked = 0, bumpcount;
52 int i, j, numplanes = 0;
53 float time_left = dt, grav = 0;
55 vector primal_velocity, original_velocity, restore_velocity;
57 for(i = 0; i < MAX_CLIP_PLANES; ++i)
62 this.move_didgravity = 1;
63 grav = dt * (PHYS_ENTGRAVITY(this) ? PHYS_ENTGRAVITY(this) : 1) * PHYS_GRAVITY(this);
65 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
67 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
68 this.velocity_z -= grav * 0.5;
70 this.velocity_z -= grav;
74 original_velocity = primal_velocity = restore_velocity = this.velocity;
76 for(bumpcount = 0;bumpcount < MAX_CLIP_PLANES;bumpcount++)
78 if(this.velocity == '0 0 0')
81 push = this.velocity * time_left;
82 _Movetype_PushEntity(this, push, true);
85 // we got teleported by a touch function
86 // let's abort the move
91 // this code is used by MOVETYPE_WALK and MOVETYPE_STEP and SV_UnstickEntity
92 // abort move if we're stuck in the world (and didn't make it out)
93 if(trace_startsolid && trace_allsolid)
95 this.velocity = restore_velocity;
99 if(trace_fraction == 1)
102 float my_trace_fraction = trace_fraction;
103 vector my_trace_plane_normal = trace_plane_normal;
105 if(trace_plane_normal.z)
107 if(trace_plane_normal.z > 0.7)
114 //dprint("_Movetype_FlyMove: !trace_ent\n");
119 this.groundentity = trace_ent;
124 // step - handle it immediately
125 vector org = this.origin;
126 vector steppush = '0 0 1' * stepheight;
128 _Movetype_PushEntity(this, steppush, true);
134 _Movetype_PushEntity(this, push, true);
140 float trace2_fraction = trace_fraction;
141 steppush = '0 0 1' * (org_z - this.origin_z);
142 _Movetype_PushEntity(this, steppush, true);
149 // accept the new position if it made some progress...
150 if(fabs(this.origin_x - org_x) >= 0.03125 || fabs(this.origin_y - org_y) >= 0.03125)
152 trace_endpos = this.origin;
153 time_left *= 1 - trace2_fraction;
162 // step - return it to caller
164 // save the trace for player extrafriction
166 stepnormal = trace_plane_normal;
169 if(my_trace_fraction >= 0.001)
171 // actually covered some distance
172 original_velocity = this.velocity;
176 time_left *= 1 - my_trace_fraction;
178 // clipped to another plane
179 if(numplanes >= MAX_CLIP_PLANES)
181 // this shouldn't really happen
182 this.velocity = '0 0 0';
187 planes[numplanes] = my_trace_plane_normal;
190 // modify original_velocity so it parallels all of the clip planes
191 vector new_velocity = '0 0 0';
192 for (i = 0;i < numplanes;i++)
194 new_velocity = _Movetype_ClipVelocity(original_velocity, planes[i], 1);
195 for (j = 0;j < numplanes;j++)
200 if((new_velocity * planes[j]) < 0)
210 // go along this plane
211 this.velocity = new_velocity;
215 // go along the crease
218 this.velocity = '0 0 0';
222 vector dir = cross(planes[0], planes[1]);
223 // LordHavoc: thanks to taniwha of QuakeForge for pointing out this fix for slowed falling in corners
224 float ilength = sqrt((dir * dir));
226 ilength = 1.0 / ilength;
230 float d = (dir * this.velocity);
231 this.velocity = dir * d;
234 // if current velocity is against the original velocity,
235 // stop dead to avoid tiny occilations in sloping corners
236 if((this.velocity * primal_velocity) <= 0)
238 this.velocity = '0 0 0';
243 // LordHavoc: this came from QW and allows you to get out of water more easily
244 if(GAMEPLAYFIX_EASIERWATERJUMP(this) && (this.flags & FL_WATERJUMP) && !(blocked & 8))
245 this.velocity = primal_velocity;
249 if(!GAMEPLAYFIX_NOGRAVITYONGROUND || !IS_ONGROUND(this))
251 if(GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
252 this.velocity_z -= grav * 0.5f;
259 void _Movetype_CheckVelocity(entity this) // SV_CheckVelocity
261 // if(vlen(this.velocity) < 0.0001)
262 // this.velocity = '0 0 0';
265 bool _Movetype_CheckWater(entity this) // SV_CheckWater
267 vector point = this.origin;
268 point.z += this.mins.z + 1;
270 int nativecontents = pointcontents(point);
271 if(this.watertype && this.watertype != nativecontents)
273 // dprintf("_Movetype_CheckWater(): Original: '%d', New: '%d'\n", this.watertype, nativecontents);
274 if(this.contentstransition)
275 this.contentstransition(this.watertype, nativecontents);
278 this.waterlevel = WATERLEVEL_NONE;
279 this.watertype = CONTENT_EMPTY;
281 int supercontents = Mod_Q1BSP_SuperContentsFromNativeContents(nativecontents);
282 if(supercontents & DPCONTENTS_LIQUIDSMASK)
284 this.watertype = nativecontents;
285 this.waterlevel = WATERLEVEL_WETFEET;
286 point.z = this.origin.z + (this.mins.z + this.maxs.z) * 0.5;
287 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
289 this.waterlevel = WATERLEVEL_SWIMMING;
290 point.z = this.origin.z + this.view_ofs.z;
291 if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(point)) & DPCONTENTS_LIQUIDSMASK)
292 this.waterlevel = WATERLEVEL_SUBMERGED;
296 return this.waterlevel > 1;
299 void _Movetype_CheckWaterTransition(entity ent) // SV_CheckWaterTransition
301 int contents = pointcontents(ent.origin);
306 if(!GAMEPLAYFIX_WATERTRANSITION(ent))
308 ent.watertype = contents;
313 else if(ent.watertype != contents)
315 // dprintf("_Movetype_CheckWaterTransition(): Origin: %s, Direct: '%d', Original: '%d', New: '%d'\n", vtos(ent.origin), pointcontents(ent.origin), ent.watertype, contents);
316 if(ent.contentstransition)
317 ent.contentstransition(ent.watertype, contents);
320 if(contents <= CONTENT_WATER)
322 ent.watertype = contents;
327 ent.watertype = CONTENT_EMPTY;
328 ent.waterlevel = (GAMEPLAYFIX_WATERTRANSITION(ent) ? 0 : contents);
332 void _Movetype_Impact(entity this, entity oth) // SV_Impact
335 gettouch(this)(this, oth);
338 gettouch(oth)(oth, this);
341 void _Movetype_LinkEdict_TouchAreaGrid(entity this) // SV_LinkEdict_TouchAreaGrid
343 if(this.solid == SOLID_NOT)
346 FOREACH_ENTITY_RADIUS(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
347 if (it.solid == SOLID_TRIGGER && it != this)
348 if (it.move_nomonsters != MOVE_NOMONSTERS && it.move_nomonsters != MOVE_WORLDONLY)
349 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax))
351 trace_allsolid = false;
352 trace_startsolid = false;
354 trace_inwater = false;
356 trace_endpos = it.origin;
357 trace_plane_normal = '0 0 1';
358 trace_plane_dist = 0;
361 gettouch(it)(it, this);
366 void _Movetype_LinkEdict(entity this, bool touch_triggers) // SV_LinkEdict
369 if(this.solid == SOLID_BSP)
371 // TODO set the absolute bbox
383 if(this.flags & FL_ITEM)
406 _Movetype_LinkEdict_TouchAreaGrid(this);
409 entity _Movetype_TestEntityPosition_ent;
410 bool _Movetype_TestEntityPosition(vector ofs) // SV_TestEntityPosition
412 entity this = _Movetype_TestEntityPosition_ent;
413 // vector org = this.origin + ofs;
415 int cont = this.dphitcontentsmask;
416 this.dphitcontentsmask = DPCONTENTS_SOLID;
417 tracebox(this.origin, this.mins, this.maxs, this.origin, ((this.move_movetype == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), this);
418 this.dphitcontentsmask = cont;
423 if(vdist(trace_endpos - this.origin, >, 0.0001))
424 this.origin = trace_endpos;
428 bool _Movetype_UnstickEntity(entity this) // SV_UnstickEntity
430 _Movetype_TestEntityPosition_ent = this;
431 if (!_Movetype_TestEntityPosition(' 0 0 0')) {
434 #define X(v) if (_Movetype_TestEntityPosition(v))
435 X('-1 0 0') X(' 1 0 0')
436 X(' 0 -1 0') X(' 0 1 0')
437 X('-1 -1 0') X(' 1 -1 0')
438 X('-1 1 0') X(' 1 1 0')
442 if (_Movetype_TestEntityPosition('0 0 -1' * i)) \
443 if (_Movetype_TestEntityPosition('0 0 1' * i))
444 X(01) X(02) X(03) X(04) X(05) X(06) X(07) X(08)
445 X(09) X(10) X(11) X(12) X(13) X(14) X(15) X(16)
449 LOG_DEBUGF("Can't unstick an entity (edict: %d, classname: %s, origin: %s)",
450 etof(this), this.classname, vtos(this.origin));
454 LOG_DEBUGF("Sucessfully unstuck an entity (edict: %d, classname: %s, origin: %s)",
455 etof(this), this.classname, vtos(this.origin));
456 _Movetype_LinkEdict(this, true);
460 vector _Movetype_ClipVelocity(vector vel, vector norm, float f) // SV_ClipVelocity
462 vel -= ((vel * norm) * norm) * f;
464 if(vel.x > -0.1 && vel.x < 0.1) vel.x = 0;
465 if(vel.y > -0.1 && vel.y < 0.1) vel.y = 0;
466 if(vel.z > -0.1 && vel.z < 0.1) vel.z = 0;
471 void _Movetype_PushEntityTrace(entity this, vector push)
473 vector end = this.origin + push;
475 if(this.move_nomonsters)
476 type = max(0, this.move_nomonsters);
477 else if(this.move_movetype == MOVETYPE_FLYMISSILE)
479 else if(this.move_movetype == MOVETYPE_FLY_WORLDONLY)
480 type = MOVE_WORLDONLY;
481 else if(this.solid == SOLID_TRIGGER || this.solid == SOLID_NOT)
482 type = MOVE_NOMONSTERS;
486 tracebox(this.origin, this.mins, this.maxs, end, type, this);
489 float _Movetype_PushEntity(entity this, vector push, bool failonstartsolid) // SV_PushEntity
491 _Movetype_PushEntityTrace(this, push);
493 if(trace_startsolid && failonstartsolid)
494 return trace_fraction;
496 this.origin = trace_endpos;
498 if(trace_fraction < 1)
499 if(this.solid >= SOLID_TRIGGER && (!IS_ONGROUND(this) || (this.groundentity != trace_ent)))
500 _Movetype_Impact(this, trace_ent);
502 return trace_fraction;
508 // matrix version of makevectors, sets v_forward, v_right and v_up
509 void makevectors_matrix(vector myangles) // AngleVectorsFLU
511 v_forward = v_right = v_up = '0 0 0';
513 float yy = myangles.y * (M_PI * 2 / 360);
516 float p = myangles.x * (M_PI * 2 / 360);
521 v_forward.x = cp * cy;
522 v_forward.y = cp * sy;
529 float r = myangles.z * (M_PI * 2 / 360);
534 v_right.x = sr * sp * cy + cr * -sy;
535 v_right.y = sr * sp * sy + cr * cy;
540 v_up.x = cr * sp * cy + -sr * -sy;
541 v_up.y = cr * sp * sy + -sr * cy;
563 void _Movetype_Physics_Frame(entity this, float movedt)
565 this.move_didgravity = -1;
566 switch (this.move_movetype)
569 case MOVETYPE_FAKEPUSH:
570 LOG_DEBUGF("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
574 case MOVETYPE_FOLLOW:
575 _Movetype_Physics_Follow(this);
577 case MOVETYPE_NOCLIP:
578 _Movetype_CheckWater(this);
579 this.origin = this.origin + movedt * this.velocity;
580 this.angles = this.angles + movedt * this.avelocity;
581 _Movetype_LinkEdict(this, false);
584 _Movetype_Physics_Step(this, movedt);
587 _Movetype_Physics_Walk(this, movedt);
590 case MOVETYPE_BOUNCE:
591 case MOVETYPE_BOUNCEMISSILE:
592 case MOVETYPE_FLYMISSILE:
594 case MOVETYPE_FLY_WORLDONLY:
595 _Movetype_Physics_Toss(this, movedt);
596 _Movetype_LinkEdict(this, true);
598 case MOVETYPE_PHYSICS:
603 void _Movetype_Physics_ClientFrame(entity this, float movedt)
605 this.move_didgravity = -1;
606 switch (this.move_movetype)
609 case MOVETYPE_FAKEPUSH:
610 LOG_DEBUGF("Physics: Lacking QuakeC support for Push movetype, FIX ME by using engine physics!");
614 case MOVETYPE_FOLLOW:
615 _Movetype_Physics_Follow(this);
617 case MOVETYPE_NOCLIP:
618 _Movetype_CheckWater(this);
619 this.origin = this.origin + movedt * this.velocity;
620 this.angles = this.angles + movedt * this.avelocity;
621 _Movetype_LinkEdict(this, false);
624 _Movetype_Physics_Step(this, movedt);
628 case MOVETYPE_FLY_WORLDONLY:
629 _Movetype_Physics_Walk(this, movedt);
632 case MOVETYPE_BOUNCE:
633 case MOVETYPE_BOUNCEMISSILE:
634 case MOVETYPE_FLYMISSILE:
635 _Movetype_Physics_Toss(this, movedt);
637 case MOVETYPE_PHYSICS:
642 void Movetype_Physics_NoMatchTicrate(entity this, float movedt, bool isclient) // to be run every move frame
644 this.move_time = time;
647 _Movetype_Physics_ClientFrame(this, movedt);
649 _Movetype_Physics_Frame(this, movedt);
653 setorigin(this, this.origin);
656 void Movetype_Physics_NoMatchServer(entity this) // optimized
658 float movedt = time - this.move_time;
659 this.move_time = time;
661 _Movetype_Physics_Frame(this, movedt);
665 setorigin(this, this.origin);
668 void Movetype_Physics_MatchServer(entity this, bool sloppy)
670 Movetype_Physics_MatchTicrate(this, TICRATE, sloppy);
674 .vector tic_velocity;
676 .vector tic_avelocity;
679 .vector tic_saved_origin;
680 .vector tic_saved_velocity;
681 .int tic_saved_flags;
682 .vector tic_saved_avelocity;
683 .vector tic_saved_angles;
684 void Movetype_Physics_MatchTicrate(entity this, float tr, bool sloppy) // SV_Physics_Entity
687 if(this.(s) != this.tic_saved_##s) \
688 this.tic_##s = this.(s)
699 this.flags = this.tic_flags;
700 this.velocity = this.tic_velocity;
701 this.origin = this.tic_origin;
702 this.avelocity = this.tic_avelocity;
703 this.angles = this.tic_angles;
704 Movetype_Physics_NoMatchServer(this);
705 this.tic_origin = this.origin;
706 this.tic_velocity = this.velocity;
707 this.tic_avelocity = this.avelocity;
708 this.tic_angles = this.angles;
709 this.tic_flags = this.flags;
711 this.tic_saved_flags = this.flags;
712 this.tic_saved_velocity = this.velocity;
713 this.tic_saved_origin = this.origin;
714 this.tic_saved_avelocity = this.avelocity;
715 this.tic_saved_angles = this.angles;
719 float dt = time - this.move_time;
721 int n = max(0, floor(dt / tr));
723 this.move_time += n * tr;
725 if(!this.move_didgravity)
726 this.move_didgravity = ((this.move_movetype == MOVETYPE_BOUNCE || this.move_movetype == MOVETYPE_TOSS) && !(this.tic_flags & FL_ONGROUND));
728 for (int i = 0; i < n; ++i)
730 this.flags = this.tic_flags;
731 this.velocity = this.tic_velocity;
732 setorigin(this, this.tic_origin);
733 this.avelocity = this.tic_avelocity;
734 this.angles = this.tic_angles;
735 _Movetype_Physics_Frame(this, tr);
736 this.tic_origin = this.origin;
737 this.tic_velocity = this.velocity;
738 this.tic_avelocity = this.avelocity;
739 this.tic_angles = this.angles;
740 this.tic_flags = this.flags;
745 this.avelocity = this.tic_avelocity;
747 if(dt > 0 && this.move_movetype != MOVETYPE_NONE && !(this.tic_flags & FL_ONGROUND))
749 // now continue the move from move_time to time
750 this.velocity = this.tic_velocity;
752 if(this.move_didgravity > 0)
754 this.velocity_z -= (GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE ? 0.5 : 1)
756 * (this.gravity ? this.gravity : 1)
757 * PHYS_GRAVITY(this);
760 this.angles = this.tic_angles + dt * this.avelocity;
762 if(sloppy || this.move_movetype == MOVETYPE_NOCLIP)
764 setorigin(this, this.tic_origin + dt * this.velocity);
768 vector oldorg = this.origin;
769 this.origin = this.tic_origin;
770 _Movetype_PushEntityTrace(this, dt * this.velocity);
771 this.origin = oldorg;
772 if(!trace_startsolid)
773 setorigin(this, trace_endpos);
776 if(this.move_didgravity > 0 && GAMEPLAYFIX_GRAVITYUNAFFECTEDBYTICRATE)
777 this.velocity_z -= 0.5 * dt * (this.gravity ? this.gravity : 1) * PHYS_GRAVITY(this);
781 this.velocity = this.tic_velocity;
782 this.angles = this.tic_angles;
783 setorigin(this, this.tic_origin);
786 this.tic_saved_flags = this.flags;
787 this.tic_saved_velocity = this.velocity;
788 this.tic_saved_origin = this.origin;
789 this.tic_saved_avelocity = this.avelocity;
790 this.tic_saved_angles = this.angles;