]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - world.c
"edict" command no longer crashes if given -1 as edict number
[xonotic/darkplaces.git] / world.c
diff --git a/world.c b/world.c
index b337fe2e0185afc408a83cdb148b932f849280e1..51b2457ea5710ac0bcc1cbd7b6269d430ff06c4f 100644 (file)
--- a/world.c
+++ b/world.c
@@ -190,10 +190,12 @@ void SV_UnlinkEdict (edict_t *ent)
 
 void SV_TouchAreaGrid(edict_t *ent)
 {
-       link_t *l, *next;
+       link_t *l;
        edict_t *touch;
        areagrid_t *grid;
        int old_self, old_other, igrid[3], igridmins[3], igridmaxs[3];
+       int i, numtouchedicts;
+       unsigned short touchedictnumbers[MAX_EDICTS];
 
        sv_areagrid_marknumber++;
        igridmins[0] = (int) ((ent->v->absmin[0] + sv_areagrid_bias[0]) * sv_areagrid_scale[0]);
@@ -209,10 +211,25 @@ void SV_TouchAreaGrid(edict_t *ent)
        igridmaxs[1] = min(AREA_GRID, igridmaxs[1]);
        //igridmaxs[2] = min(AREA_GRID, igridmaxs[2]);
 
-       for (l = sv_areagrid_outside.trigger_edicts.next;l != &sv_areagrid_outside.trigger_edicts;l = next)
+       // build a list of edicts to touch, because the link loop can be corrupted
+       // by SV_IncreaseEdicts called during touch functions
+       numtouchedicts = 0;
+       for (l = sv_areagrid_outside.trigger_edicts.next;l != &sv_areagrid_outside.trigger_edicts;l = l->next)
+               touchedictnumbers[numtouchedicts++] = l->entitynumber;
+
+       for (igrid[1] = igridmins[1];igrid[1] < igridmaxs[1];igrid[1]++)
        {
-               next = l->next;
-               touch = EDICT_NUM(l->entitynumber);
+               grid = sv_areagrid + igrid[1] * AREA_GRID + igridmins[0];
+               for (igrid[0] = igridmins[0];igrid[0] < igridmaxs[0];igrid[0]++, grid++)
+                       for (l = grid->trigger_edicts.next;l != &grid->trigger_edicts;l = l->next)
+                               touchedictnumbers[numtouchedicts++] = l->entitynumber;
+       }
+
+       old_self = pr_global_struct->self;
+       old_other = pr_global_struct->other;
+       for (i = 0;i < numtouchedicts && !ent->e->free;i++)
+       {
+               touch = EDICT_NUM_UNSIGNED(touchedictnumbers[i]);
                if (ent->v->absmin[0] > touch->v->absmax[0]
                 || ent->v->absmax[0] < touch->v->absmin[0]
                 || ent->v->absmin[1] > touch->v->absmax[1]
@@ -224,55 +241,13 @@ void SV_TouchAreaGrid(edict_t *ent)
                        continue;
                if (!touch->v->touch || touch->v->solid != SOLID_TRIGGER)
                        continue;
-               old_self = pr_global_struct->self;
-               old_other = pr_global_struct->other;
-
                pr_global_struct->self = EDICT_TO_PROG(touch);
                pr_global_struct->other = EDICT_TO_PROG(ent);
                pr_global_struct->time = sv.time;
                PR_ExecuteProgram (touch->v->touch, "");
-
-               pr_global_struct->self = old_self;
-               pr_global_struct->other = old_other;
-       }
-
-       for (igrid[1] = igridmins[1];igrid[1] < igridmaxs[1];igrid[1]++)
-       {
-               grid = sv_areagrid + igrid[1] * AREA_GRID + igridmins[0];
-               for (igrid[0] = igridmins[0];igrid[0] < igridmaxs[0];igrid[0]++, grid++)
-               {
-                       for (l = grid->trigger_edicts.next;l != &grid->trigger_edicts;l = next)
-                       {
-                               next = l->next;
-                               touch = EDICT_NUM(l->entitynumber);
-                               if (touch->e->areagridmarknumber == sv_areagrid_marknumber)
-                                       continue;
-                               touch->e->areagridmarknumber = sv_areagrid_marknumber;
-                               if (ent->v->absmin[0] > touch->v->absmax[0]
-                                || ent->v->absmax[0] < touch->v->absmin[0]
-                                || ent->v->absmin[1] > touch->v->absmax[1]
-                                || ent->v->absmax[1] < touch->v->absmin[1]
-                                || ent->v->absmin[2] > touch->v->absmax[2]
-                                || ent->v->absmax[2] < touch->v->absmin[2])
-                                       continue;
-                               // LordHavoc: id bug that won't be fixed: triggers do not ignore their owner like solid objects do
-                               if (touch == ent)
-                                       continue;
-                               if (!touch->v->touch || touch->v->solid != SOLID_TRIGGER)
-                                       continue;
-                               old_self = pr_global_struct->self;
-                               old_other = pr_global_struct->other;
-
-                               pr_global_struct->self = EDICT_TO_PROG(touch);
-                               pr_global_struct->other = EDICT_TO_PROG(ent);
-                               pr_global_struct->time = sv.time;
-                               PR_ExecuteProgram (touch->v->touch, "");
-
-                               pr_global_struct->self = old_self;
-                               pr_global_struct->other = old_other;
-                       }
-               }
        }
+       pr_global_struct->self = old_self;
+       pr_global_struct->other = old_other;
 }
 
 void SV_LinkEdict_AreaGrid(edict_t *ent)
@@ -460,23 +435,42 @@ trace_t SV_ClipMoveToEntity(edict_t *ent, const vec3_t start, const vec3_t mins,
        if ((int) ent->v->solid == SOLID_BSP || movetype == MOVE_HITMODEL)
        {
                i = ent->v->modelindex;
+               // if the modelindex is 0, it shouldn't be SOLID_BSP!
+               if (i == 0)
+               {
+                       Con_Printf("SV_ClipMoveToEntity: edict %i: SOLID_BSP with no model\n", NUM_FOR_EDICT(ent));
+                       memset(&trace, 0, sizeof(trace));
+                       return trace;
+               }
                if ((unsigned int) i >= MAX_MODELS)
-                       Host_Error("SV_ClipMoveToEntity: invalid modelindex\n");
+               {
+                       Con_Printf("SV_ClipMoveToEntity: edict %i: SOLID_BSP with invalid modelindex\n", NUM_FOR_EDICT(ent));
+                       memset(&trace, 0, sizeof(trace));
+                       return trace;
+               }
                model = sv.models[i];
                if (i != 0 && model == NULL)
-                       Host_Error("SV_ClipMoveToEntity: invalid modelindex\n");
+               {
+                       Con_Printf("SV_ClipMoveToEntity: edict %i: SOLID_BSP with invalid modelindex\n", NUM_FOR_EDICT(ent));
+                       memset(&trace, 0, sizeof(trace));
+                       return trace;
+               }
 
                Mod_CheckLoaded(model);
                if ((int) ent->v->solid == SOLID_BSP)
                {
                        if (!model->TraceBox)
                        {
-                               Con_Printf("SV_ClipMoveToEntity: SOLID_BSP with a non-collidable model, entity dump:\n");
-                               ED_Print(ent);
-                               Host_Error("SV_ClipMoveToEntity: SOLID_BSP with a non-collidable model\n");
+                               Con_Printf("SV_ClipMoveToEntity: edict %i: SOLID_BSP with a non-collidable model\n", NUM_FOR_EDICT(ent));
+                               memset(&trace, 0, sizeof(trace));
+                               return trace;
                        }
                        if (ent->v->movetype != MOVETYPE_PUSH)
-                               Host_Error("SV_ClipMoveToEntity: SOLID_BSP without MOVETYPE_PUSH");
+                       {
+                               Con_Printf("SV_ClipMoveToEntity: edict %i: SOLID_BSP without MOVETYPE_PUSH\n", NUM_FOR_EDICT(ent));
+                               memset(&trace, 0, sizeof(trace));
+                               return trace;
+                       }
                }
                Matrix4x4_CreateFromQuakeEntity(&matrix, ent->v->origin[0], ent->v->origin[1], ent->v->origin[2], ent->v->angles[0], ent->v->angles[1], ent->v->angles[2], 1);
        }
@@ -586,16 +580,20 @@ void SV_ClipToNode(moveclip_t *clip, link_t *list)
                if (trace.startsolid)
                {
                        clip->trace.startsolid = true;
-                       if (clip->trace.fraction == 1)
+                       if (clip->trace.realfraction == 1)
                                clip->trace.ent = touch;
                }
-               if (trace.inopen)
-                       clip->trace.inopen = true;
+               // don't set this except on the world, because it can easily confuse
+               // monsters underwater if there's a bmodel involved in the trace
+               // (inopen && inwater is how they check water visibility)
+               //if (trace.inopen)
+               //      clip->trace.inopen = true;
                if (trace.inwater)
                        clip->trace.inwater = true;
-               if (trace.fraction < clip->trace.fraction)
+               if (trace.realfraction < clip->trace.realfraction)
                {
                        clip->trace.fraction = trace.fraction;
+                       clip->trace.realfraction = trace.realfraction;
                        VectorCopy(trace.endpos, clip->trace.endpos);
                        clip->trace.plane = trace.plane;
                        clip->trace.ent = touch;
@@ -620,6 +618,10 @@ trace_t SV_Move(const vec3_t start, const vec3_t mins, const vec3_t maxs, const
        areagrid_t *grid;
        int i, igrid[3], igridmins[3], igridmaxs[3];
 
+       // if the passedict is world, make it NULL (to avoid two checks each time)
+       if (passedict == sv.edicts)
+               passedict = NULL;
+
        memset(&clip, 0, sizeof(moveclip_t));
 
        VectorCopy(start, clip.start);