]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - sv_phys.c
bound fractions after trace code
[xonotic/darkplaces.git] / sv_phys.c
index b25bc527af729dfd6a4fc22e9e4285eb81a032d3..9a9fc359898faef0e5c33ebe7e65fd30750beab7 100644 (file)
--- a/sv_phys.c
+++ b/sv_phys.c
@@ -128,37 +128,6 @@ void SV_CheckVelocity (edict_t *ent)
        }
 }
 
-/*
-=============
-SV_RunThink
-
-Runs thinking code if time.  There is some play in the exact time the think
-function will be called, because it is called before any movement is done
-in a frame.  Not used for pushmove objects, because they must be exact.
-Returns false if the entity removed itself.
-=============
-*/
-qboolean SV_RunThink (edict_t *ent)
-{
-       float thinktime;
-
-       thinktime = ent->v->nextthink;
-       if (thinktime <= 0 || thinktime > sv.time + sv.frametime)
-               return true;
-
-       // don't let things stay in the past.
-       // it is possible to start that way by a trigger with a local time.
-       if (thinktime < sv.time)
-               thinktime = sv.time;
-
-       ent->v->nextthink = 0;
-       pr_global_struct->time = thinktime;
-       pr_global_struct->self = EDICT_TO_PROG(ent);
-       pr_global_struct->other = EDICT_TO_PROG(sv.edicts);
-       PR_ExecuteProgram (ent->v->think, "NULL think function");
-       return !ent->e->free;
-}
-
 /*
 ==================
 SV_Impact
@@ -178,14 +147,14 @@ void SV_Impact (edict_t *e1, edict_t *e2)
        {
                pr_global_struct->self = EDICT_TO_PROG(e1);
                pr_global_struct->other = EDICT_TO_PROG(e2);
-               PR_ExecuteProgram (e1->v->touch, "");
+               PR_ExecuteProgram (e1->v->touch, "QC function self.touch is missing");
        }
 
        if (e2->v->touch && e2->v->solid != SOLID_NOT)
        {
                pr_global_struct->self = EDICT_TO_PROG(e2);
                pr_global_struct->other = EDICT_TO_PROG(e1);
-               PR_ExecuteProgram (e2->v->touch, "");
+               PR_ExecuteProgram (e2->v->touch, "QC function self.touch is missing");
        }
 
        pr_global_struct->self = old_self;
@@ -563,6 +532,8 @@ void SV_PushMove (edict_t *pusher, float movetime)
        float savesolid, movetime2, pushltime;
        vec3_t mins, maxs, move, move1, moveangle, pushorig, pushang, a, forward, left, up, org, org2;
        int num_moved;
+       int numcheckentities;
+       static edict_t *checkentities[MAX_EDICTS];
        model_t *pushermodel;
        trace_t trace;
 
@@ -592,13 +563,13 @@ void SV_PushMove (edict_t *pusher, float movetime)
                SV_LinkEdict (pusher, false);
                return;
        default:
-               Con_DPrintf("SV_PushMove: unrecognized solid type %f\n", pusher->v->solid);
+               Con_Printf("SV_PushMove: unrecognized solid type %f\n", pusher->v->solid);
                return;
        }
        index = (int) pusher->v->modelindex;
        if (index < 1 || index >= MAX_MODELS)
        {
-               Con_DPrintf("SV_PushMove: invalid modelindex %f\n", pusher->v->modelindex);
+               Con_Printf("SV_PushMove: invalid modelindex %f\n", pusher->v->modelindex);
                return;
        }
        pushermodel = sv.models[index];
@@ -673,11 +644,11 @@ void SV_PushMove (edict_t *pusher, float movetime)
 
 // see if any solid entities are inside the final position
        num_moved = 0;
-       check = NEXT_EDICT(sv.edicts);
-       for (e = 1;e < sv.num_edicts;e++, check = NEXT_EDICT(check))
+
+       numcheckentities = SV_EntitiesInBox(mins, maxs, MAX_EDICTS, checkentities);
+       for (e = 1;e < numcheckentities;e++)
        {
-               if (check->e->free)
-                       continue;
+               check = checkentities[e];
                if (check->v->movetype == MOVETYPE_PUSH
                 || check->v->movetype == MOVETYPE_NONE
                 || check->v->movetype == MOVETYPE_FOLLOW
@@ -687,18 +658,8 @@ void SV_PushMove (edict_t *pusher, float movetime)
 
                // if the entity is standing on the pusher, it will definitely be moved
                if (!(((int)check->v->flags & FL_ONGROUND) && PROG_TO_EDICT(check->v->groundentity) == pusher))
-               {
-                       if (check->v->absmin[0] >= maxs[0]
-                        || check->v->absmax[0] <= mins[0]
-                        || check->v->absmin[1] >= maxs[1]
-                        || check->v->absmax[1] <= mins[1]
-                        || check->v->absmin[2] >= maxs[2]
-                        || check->v->absmax[2] <= mins[2])
-                               continue;
-
                        if (!SV_ClipMoveToEntity(pusher, check->v->origin, check->v->mins, check->v->maxs, check->v->origin).startsolid)
                                continue;
-               }
 
                if (forward[0] != 1) // quick way to check if any rotation is used
                {
@@ -769,7 +730,7 @@ void SV_PushMove (edict_t *pusher, float movetime)
                                {
                                        pr_global_struct->self = EDICT_TO_PROG(pusher);
                                        pr_global_struct->other = EDICT_TO_PROG(check);
-                                       PR_ExecuteProgram (pusher->v->blocked, "");
+                                       PR_ExecuteProgram (pusher->v->blocked, "QC function self.blocked is missing");
                                }
                                break;
                        }
@@ -806,14 +767,16 @@ void SV_Physics_Pusher (edict_t *ent)
                // advances ent->v->ltime if not blocked
                SV_PushMove (ent, movetime);
 
+       /*
        if (thinktime > oldltime && thinktime <= ent->v->ltime)
        {
                ent->v->nextthink = 0;
                pr_global_struct->time = sv.time;
                pr_global_struct->self = EDICT_TO_PROG(ent);
                pr_global_struct->other = EDICT_TO_PROG(sv.edicts);
-               PR_ExecuteProgram (ent->v->think, "NULL think function");
+               PR_ExecuteProgram (ent->v->think, "QC function self.think is missing");
        }
+       */
 }
 
 
@@ -1025,6 +988,12 @@ void SV_WalkMove (edict_t *ent)
        originalmove_flags = (int)ent->v->flags;
        originalmove_groundentity = ent->v->groundentity;
 
+       if ((int)ent->v->flags & FL_WATERJUMP)
+               return;
+
+       if (sv_nostep.integer)
+               return;
+       
        // if move didn't block on a step, return
        if (clip & 2)
        {
@@ -1032,12 +1001,6 @@ void SV_WalkMove (edict_t *ent)
                if (fabs(start_velocity[0]) < 0.03125 && fabs(start_velocity[1]) < 0.03125)
                        return;
        
-               if (sv_nostep.integer)
-                       return;
-               
-               if ((int)ent->v->flags & FL_WATERJUMP)
-                       return;
-       
                if (ent->v->movetype != MOVETYPE_FLY)
                {
                        // return if gibbed by a trigger
@@ -1092,7 +1055,8 @@ void SV_WalkMove (edict_t *ent)
                if (clip & 2 && sv_wallfriction.integer)
                        SV_WallFriction (ent, stepnormal);
        }
-       else if (!sv_gameplayfix_stepdown.integer || !oldonground || ((int)ent->v->flags & FL_ONGROUND))
+       // skip out if stepdown is enabled, moving downward, not in water, and the move started onground and ended offground
+       else if (!(sv_gameplayfix_stepdown.integer && ent->v->waterlevel < 2 && start_velocity[2] < (1.0 / 32.0) && oldonground && !((int)ent->v->flags & FL_ONGROUND)))
                return;
 
        // move down
@@ -1143,9 +1107,6 @@ void SV_Physics_Follow (edict_t *ent)
        edict_t *e;
 
        // regular thinking
-       if (!SV_RunThink (ent))
-               return;
-
        // LordHavoc: implemented rotation on MOVETYPE_FOLLOW objects
        e = PROG_TO_EDICT(ent->v->aiment);
        if (e->v->angles[0] == ent->v->punchangle[0] && e->v->angles[1] == ent->v->punchangle[1] && e->v->angles[2] == ent->v->punchangle[2])
@@ -1229,9 +1190,9 @@ void SV_Physics_Toss (edict_t *ent)
        vec3_t move;
        edict_t *groundentity;
 
-       // regular thinking
-       if (!SV_RunThink (ent))
-               return;
+       // don't stick to ground if onground and moving upward
+       if (ent->v->velocity[2] >= (1.0 / 32.0) && ((int)ent->v->flags & FL_ONGROUND))
+               ent->v->flags = (int)ent->v->flags & ~FL_ONGROUND;
 
 // if onground, return without moving
        if ((int)ent->v->flags & FL_ONGROUND)
@@ -1351,6 +1312,10 @@ will fall if the floor is pulled out from under them.
 */
 void SV_Physics_Step (edict_t *ent)
 {
+       // don't stick to ground if onground and moving upward
+       if (ent->v->velocity[2] >= (1.0 / 32.0) && ((int)ent->v->flags & FL_ONGROUND))
+               ent->v->flags = (int)ent->v->flags & ~FL_ONGROUND;
+
        // freefall if not onground/fly/swim
        if (!((int)ent->v->flags & (FL_ONGROUND | FL_FLY | FL_SWIM)))
        {
@@ -1366,9 +1331,6 @@ void SV_Physics_Step (edict_t *ent)
                        SV_StartSound(ent, 0, "demon/dland2.wav", 255, 1);
        }
 
-// regular thinking
-       SV_RunThink(ent);
-
        SV_CheckWaterTransition(ent);
 }
 
@@ -1382,7 +1344,9 @@ SV_Physics
 */
 void SV_Physics (void)
 {
-       int i;
+       int i, end, retouch;
+       float nexttime;
+       vec3_t oldorigin;
        edict_t *ent;
 
 // let the progs know that a new frame has started
@@ -1391,37 +1355,16 @@ void SV_Physics (void)
        pr_global_struct->time = sv.time;
        PR_ExecuteProgram (pr_global_struct->StartFrame, "QC function StartFrame is missing");
 
-//
-// treat each object in turn
-//
-       ent = sv.edicts;
-       for (i=0 ; i<sv.num_edicts ; i++, ent = NEXT_EDICT(ent))
+       retouch = pr_global_struct->force_retouch > 0;
+       end = i = sv_freezenonclients.integer ? svs.maxclients + 1 : sv.num_edicts;
+       for (i = 0, ent = sv.edicts;i < end;i++, ent = NEXT_EDICT(ent))
        {
-               if (ent->e->free)
-                       continue;
-
-               if (pr_global_struct->force_retouch)
-                       SV_LinkEdict (ent, true);       // force retouch even for stationary
-
-               if (i <= svs.maxclients)
-               {
-                       if (i > 0)
-                       {
-                               if (!svs.clients[i-1].spawned)
-                                       continue;
-                               // connected slot
-                               // call standard client pre-think
-                               SV_CheckVelocity (ent);
-                               pr_global_struct->time = sv.time;
-                               pr_global_struct->self = EDICT_TO_PROG(ent);
-                               PR_ExecuteProgram (pr_global_struct->PlayerPreThink, "QC function PlayerPreThink is missing");
-                               SV_CheckVelocity (ent);
-                       }
-               }
-               else if (sv_freezenonclients.integer)
+               if (ent->e->free || ent->v->movetype == MOVETYPE_NONE)
                        continue;
 
                // LordHavoc: merged client and normal entity physics
+               VectorCopy(ent->v->origin, oldorigin);
+
                switch ((int) ent->v->movetype)
                {
                case MOVETYPE_PUSH:
@@ -1429,79 +1372,136 @@ void SV_Physics (void)
                        SV_Physics_Pusher (ent);
                        break;
                case MOVETYPE_NONE:
-                       // LordHavoc: manually inlined the thinktime check here because MOVETYPE_NONE is used on so many objects
-                       if (ent->v->nextthink > 0 && ent->v->nextthink <= sv.time + sv.frametime)
-                               SV_RunThink (ent);
                        break;
                case MOVETYPE_FOLLOW:
                        SV_Physics_Follow (ent);
                        break;
                case MOVETYPE_NOCLIP:
-                       if (SV_RunThink(ent))
-                       {
-                               SV_CheckWater(ent);
-                               VectorMA(ent->v->origin, sv.frametime, ent->v->velocity, ent->v->origin);
-                               VectorMA(ent->v->angles, sv.frametime, ent->v->avelocity, ent->v->angles);
-                       }
-                       // relink normal entities here, players always get relinked so don't relink twice
-                       if (!(i > 0 && i <= svs.maxclients))
-                               SV_LinkEdict(ent, false);
+                       SV_CheckWater(ent);
+                       VectorMA(ent->v->origin, sv.frametime, ent->v->velocity, ent->v->origin);
+                       VectorMA(ent->v->angles, sv.frametime, ent->v->avelocity, ent->v->angles);
                        break;
                case MOVETYPE_STEP:
                        SV_Physics_Step (ent);
                        break;
                case MOVETYPE_WALK:
-                       if (SV_RunThink (ent))
-                       {
-                               if (!SV_CheckWater (ent) && ! ((int)ent->v->flags & FL_WATERJUMP) )
-                                       SV_AddGravity (ent);
-                               SV_CheckStuck (ent);
-                               SV_WalkMove (ent);
-                               // relink normal entities here, players always get relinked so don't relink twice
-                               if (!(i > 0 && i <= svs.maxclients))
-                                       SV_LinkEdict (ent, true);
-                       }
+                       if (!SV_CheckWater(ent) && ! ((int)ent->v->flags & FL_WATERJUMP) )
+                               SV_AddGravity(ent);
+                       SV_CheckStuck(ent);
+                       SV_WalkMove(ent);
                        break;
                case MOVETYPE_TOSS:
                case MOVETYPE_BOUNCE:
                case MOVETYPE_BOUNCEMISSILE:
                case MOVETYPE_FLYMISSILE:
-                       SV_Physics_Toss (ent);
+                       SV_Physics_Toss(ent);
                        break;
                case MOVETYPE_FLY:
-                       if (i > 0 && i <= svs.maxclients)
+                       if (i >= 1 && i <= svs.maxclients && svs.clients[i-1].spawned)
                        {
-                               if (SV_RunThink (ent))
-                               {
-                                       SV_CheckWater (ent);
-                                       SV_WalkMove (ent);
-                               }
+                               SV_CheckWater(ent);
+                               SV_WalkMove(ent);
                        }
                        else
-                               SV_Physics_Toss (ent);
+                               SV_Physics_Toss(ent);
                        break;
                default:
                        Host_Error ("SV_Physics: bad movetype %i", (int)ent->v->movetype);
                        break;
                }
 
-               if (i <= svs.maxclients && i > 0 && !ent->e->free)
-               {
-                       SV_CheckVelocity (ent);
+               if (!VectorCompare(ent->v->origin, oldorigin) || retouch)
+                       SV_LinkEdict(ent, true);
+       }
 
-                       // call standard player post-think
-                       SV_LinkEdict (ent, true);
+       for (i = 1, ent = NEXT_EDICT(sv.edicts);i <= svs.maxclients;i++, ent = NEXT_EDICT(ent))
+       {
+               if (ent->e->free || !svs.clients[i-1].spawned)
+                       continue;
 
-                       SV_CheckVelocity (ent);
+               // call standard client pre-think
+               SV_CheckVelocity (ent);
+               pr_global_struct->time = sv.time;
+               pr_global_struct->self = EDICT_TO_PROG(ent);
+               PR_ExecuteProgram (pr_global_struct->PlayerPreThink, "QC function PlayerPreThink is missing");
+               SV_CheckVelocity (ent);
+       }
 
-                       pr_global_struct->time = sv.time;
-                       pr_global_struct->self = EDICT_TO_PROG(ent);
-                       PR_ExecuteProgram (pr_global_struct->PlayerPostThink, "QC function PlayerPostThink is missing");
+       nexttime = sv.time + sv.frametime;
+       end = i = sv_freezenonclients.integer ? svs.maxclients + 1 : sv.num_edicts;
+       for (i = 0, ent = sv.edicts;i < end;i++, ent = NEXT_EDICT(ent))
+       {
+               if (ent->e->free)
+                       continue;
+
+               // LordHavoc: merged client and normal entity physics
+               switch ((int) ent->v->movetype)
+               {
+               case MOVETYPE_PUSH:
+               case MOVETYPE_FAKEPUSH:
+                       if (ent->v->nextthink && ent->v->ltime > ent->v->nextthink)
+                       {
+                               ent->v->nextthink = 0;
+                               pr_global_struct->time = sv.time;
+                               pr_global_struct->self = EDICT_TO_PROG(ent);
+                               pr_global_struct->other = EDICT_TO_PROG(sv.edicts);
+                               PR_ExecuteProgram (ent->v->think, "QC function self.think is missing");
+                       }
+                       break;
+               case MOVETYPE_NONE:
+               case MOVETYPE_FOLLOW:
+               case MOVETYPE_NOCLIP:
+               case MOVETYPE_STEP:
+               case MOVETYPE_WALK:
+               case MOVETYPE_TOSS:
+               case MOVETYPE_BOUNCE:
+               case MOVETYPE_BOUNCEMISSILE:
+               case MOVETYPE_FLY:
+               case MOVETYPE_FLYMISSILE:
+                       // LordHavoc: manually inlined SV_RunThink here
+                       if (ent->v->nextthink && ent->v->nextthink <= nexttime)
+                       {
+                               /*
+                               SV_RunThink
+                               Runs thinking code if time.  There is some play in the exact time the think
+                               function will be called, because it is called before any movement is done
+                               in a frame.  Not used for pushmove objects, because they must be exact.
+                               Returns false if the entity removed itself.
+                               */
+                               float thinktime = ent->v->nextthink;
+                               if (thinktime && thinktime < sv.time + sv.frametime)
+                               {
+                                       ent->v->nextthink = 0;
+                                       // don't let things stay in the past.
+                                       // it is possible to start that way by a trigger with a local time.
+                                       pr_global_struct->time = max(thinktime, sv.time);
+                                       pr_global_struct->self = EDICT_TO_PROG(ent);
+                                       pr_global_struct->other = EDICT_TO_PROG(sv.edicts);
+                                       PR_ExecuteProgram (ent->v->think, "QC function self.think is missing");
+                               }
+                       }
+                       break;
+               default:
+                       Host_Error ("SV_Physics: bad movetype %i", (int)ent->v->movetype);
+                       break;
                }
        }
 
-       if (pr_global_struct->force_retouch)
-               pr_global_struct->force_retouch--;
+       for (i = 1, ent = NEXT_EDICT(sv.edicts);i <= svs.maxclients;i++, ent = NEXT_EDICT(ent))
+       {
+               if (ent->e->free || !svs.clients[i-1].spawned)
+                       continue;
+
+               // call standard player post-think
+               SV_LinkEdict (ent, true);
+               SV_CheckVelocity (ent);
+               pr_global_struct->time = sv.time;
+               pr_global_struct->self = EDICT_TO_PROG(ent);
+               PR_ExecuteProgram (pr_global_struct->PlayerPostThink, "QC function PlayerPostThink is missing");
+       }
+
+       if (pr_global_struct->force_retouch > 0)
+               pr_global_struct->force_retouch = max(0, pr_global_struct->force_retouch - 1);
 
        // LordHavoc: endframe support
        if (EndFrameQC)
@@ -1509,7 +1509,7 @@ void SV_Physics (void)
                pr_global_struct->self = EDICT_TO_PROG(sv.edicts);
                pr_global_struct->other = EDICT_TO_PROG(sv.edicts);
                pr_global_struct->time = sv.time;
-               PR_ExecuteProgram ((func_t)(EndFrameQC - pr_functions), "");
+               PR_ExecuteProgram ((func_t)(EndFrameQC - pr_functions), "QC function EndFrame is missing");
        }
 
        if (!sv_freezenonclients.integer)