+/*
+==================
+SV_TraceLine
+==================
+*/
+trace_t SV_TraceLine(const vec3_t start, const vec3_t end, int type, prvm_edict_t *passedict, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask, float extend)
+{
+ prvm_prog_t *prog = SVVM_prog;
+ int i, bodysupercontents;
+ int passedictprog;
+ float pitchsign = 1;
+ prvm_edict_t *traceowner, *touch;
+ trace_t trace;
+ // temporary storage because prvm_vec_t may differ from vec_t
+ vec3_t touchmins, touchmaxs;
+ // bounding box of entire move area
+ vec3_t clipboxmins, clipboxmaxs;
+ // size when clipping against monsters
+ vec3_t clipmins2, clipmaxs2;
+ // start and end origin of move
+ vec3_t clipstart, clipend;
+ // trace results
+ trace_t cliptrace;
+ // matrices to transform into/out of other entity's space
+ matrix4x4_t matrix, imatrix;
+ // model of other entity
+ model_t *model;
+ // list of entities to test for collisions
+ int numtouchedicts;
+ static prvm_edict_t *touchedicts[MAX_EDICTS];
+ int clipgroup;
+ if (VectorCompare(start, end))
+ return SV_TracePoint(start, type, passedict, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask);
+
+ //return SV_TraceBox(start, vec3_origin, vec3_origin, end, type, passedict, hitsupercontentsmask);
+
+ VectorCopy(start, clipstart);
+ VectorCopy(end, clipend);
+ VectorClear(clipmins2);
+ VectorClear(clipmaxs2);
+#if COLLISIONPARANOID >= 3
+ Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
+#endif
+
+ // clip to world
+ Collision_ClipLineToWorld(&cliptrace, sv.worldmodel, clipstart, clipend, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask, extend, false);
+ cliptrace.worldstartsolid = cliptrace.bmodelstartsolid = cliptrace.startsolid;
+ if (cliptrace.startsolid || cliptrace.fraction < 1)
+ cliptrace.ent = prog->edicts;
+ if (type == MOVE_WORLDONLY)
+ goto finished;
+
+ if (type == MOVE_MISSILE)
+ {
+ // LadyHavoc: modified this, was = -15, now -= 15
+ for (i = 0;i < 3;i++)
+ {
+ clipmins2[i] -= 15;
+ clipmaxs2[i] += 15;
+ }
+ }
+
+ // create the bounding box of the entire move
+ for (i = 0;i < 3;i++)
+ {
+ clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + clipmins2[i] - 1;
+ clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + clipmaxs2[i] + 1;
+ }
+
+ // debug override to test against everything
+ if (sv_debugmove.integer)
+ {
+ clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = (vec_t)-999999999;
+ clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] = (vec_t)999999999;
+ }
+
+ // if the passedict is world, make it NULL (to avoid two checks each time)
+ if (passedict == prog->edicts)
+ passedict = NULL;
+ // precalculate prog value for passedict for comparisons
+ passedictprog = PRVM_EDICT_TO_PROG(passedict);
+ // precalculate passedict's owner edict pointer for comparisons
+ traceowner = passedict ? PRVM_PROG_TO_EDICT(PRVM_serveredictedict(passedict, owner)) : 0;
+
+ clipgroup = passedict ? (int)PRVM_serveredictfloat(passedict, clipgroup) : 0;
+
+ // clip to entities
+ // because this uses World_EntitiestoBox, we know all entity boxes overlap
+ // the clip region, so we can skip culling checks in the loop below
+ numtouchedicts = SV_EntitiesInBox(clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
+ if (numtouchedicts > MAX_EDICTS)
+ {
+ // this never happens
+ Con_Printf("SV_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
+ numtouchedicts = MAX_EDICTS;
+ }
+ for (i = 0;i < numtouchedicts;i++)
+ {
+ touch = touchedicts[i];
+
+ if (PRVM_serveredictfloat(touch, solid) < SOLID_BBOX)
+ continue;
+ if (type == MOVE_NOMONSTERS && PRVM_serveredictfloat(touch, solid) != SOLID_BSP)
+ continue;
+
+ if (passedict)
+ {
+ // don't clip against self
+ if (passedict == touch)
+ continue;
+ // don't clip owned entities against owner
+ if (traceowner == touch)
+ continue;
+ // don't clip owner against owned entities
+ if (passedictprog == PRVM_serveredictedict(touch, owner))
+ continue;
+ // don't clip against any entities in the same clipgroup (DP_RM_CLIPGROUP)
+ if (clipgroup && clipgroup == (int)PRVM_serveredictfloat(touch, clipgroup))
+ continue;
+ // don't clip points against points (they can't collide)
+ if (VectorCompare(PRVM_serveredictvector(touch, mins), PRVM_serveredictvector(touch, maxs)) && (type != MOVE_MISSILE || !((int)PRVM_serveredictfloat(touch, flags) & FL_MONSTER)))
+ continue;
+ }
+
+ bodysupercontents = PRVM_serveredictfloat(touch, solid) == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
+
+ // might interact, so do an exact clip
+ model = NULL;
+ if ((int) PRVM_serveredictfloat(touch, solid) == SOLID_BSP || type == MOVE_HITMODEL)
+ {
+ model = SV_GetModelFromEdict(touch);
+ pitchsign = SV_GetPitchSign(prog, touch);
+ }
+ if (model)
+ Matrix4x4_CreateFromQuakeEntity(&matrix, PRVM_serveredictvector(touch, origin)[0], PRVM_serveredictvector(touch, origin)[1], PRVM_serveredictvector(touch, origin)[2], pitchsign * PRVM_serveredictvector(touch, angles)[0], PRVM_serveredictvector(touch, angles)[1], PRVM_serveredictvector(touch, angles)[2], 1);
+ else
+ Matrix4x4_CreateTranslate(&matrix, PRVM_serveredictvector(touch, origin)[0], PRVM_serveredictvector(touch, origin)[1], PRVM_serveredictvector(touch, origin)[2]);
+ Matrix4x4_Invert_Simple(&imatrix, &matrix);
+ VM_GenerateFrameGroupBlend(prog, touch->priv.server->framegroupblend, touch);
+ VM_FrameBlendFromFrameGroupBlend(touch->priv.server->frameblend, touch->priv.server->framegroupblend, model, sv.time);
+ VM_UpdateEdictSkeleton(prog, touch, model, touch->priv.server->frameblend);
+ VectorCopy(PRVM_serveredictvector(touch, mins), touchmins);
+ VectorCopy(PRVM_serveredictvector(touch, maxs), touchmaxs);
+ if (type == MOVE_MISSILE && (int)PRVM_serveredictfloat(touch, flags) & FL_MONSTER)
+ Collision_ClipToGenericEntity(&trace, model, touch->priv.server->frameblend, &touch->priv.server->skeleton, touchmins, touchmaxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins2, clipmaxs2, clipend, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask, extend);
+ else
+ Collision_ClipLineToGenericEntity(&trace, model, touch->priv.server->frameblend, &touch->priv.server->skeleton, touchmins, touchmaxs, bodysupercontents, &matrix, &imatrix, clipstart, clipend, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask, extend, false);
+
+ Collision_CombineTraces(&cliptrace, &trace, (void *)touch, PRVM_serveredictfloat(touch, solid) == SOLID_BSP);
+ }
+
+finished:
+ return cliptrace;
+}
+
+/*
+==================
+SV_Move
+==================
+*/
+#if COLLISIONPARANOID >= 1
+trace_t SV_TraceBox_(const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int type, prvm_edict_t *passedict, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask, float extend)
+#else
+trace_t SV_TraceBox(const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int type, prvm_edict_t *passedict, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask, float extend)
+#endif
+{
+ prvm_prog_t *prog = SVVM_prog;
+ vec3_t hullmins, hullmaxs;
+ int i, bodysupercontents;
+ int passedictprog;
+ float pitchsign = 1;
+ qbool pointtrace;
+ prvm_edict_t *traceowner, *touch;
+ trace_t trace;
+ // temporary storage because prvm_vec_t may differ from vec_t
+ vec3_t touchmins, touchmaxs;
+ // bounding box of entire move area
+ vec3_t clipboxmins, clipboxmaxs;
+ // size of the moving object
+ vec3_t clipmins, clipmaxs;
+ // size when clipping against monsters
+ vec3_t clipmins2, clipmaxs2;
+ // start and end origin of move
+ vec3_t clipstart, clipend;
+ // trace results
+ trace_t cliptrace;
+ // matrices to transform into/out of other entity's space
+ matrix4x4_t matrix, imatrix;
+ // model of other entity
+ model_t *model;
+ // list of entities to test for collisions
+ int numtouchedicts;
+ static prvm_edict_t *touchedicts[MAX_EDICTS];
+ int clipgroup;
+ if (VectorCompare(mins, maxs))
+ {
+ vec3_t shiftstart, shiftend;
+ VectorAdd(start, mins, shiftstart);
+ VectorAdd(end, mins, shiftend);
+ if (VectorCompare(start, end))
+ trace = SV_TracePoint(shiftstart, type, passedict, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask);
+ else
+ trace = SV_TraceLine(shiftstart, shiftend, type, passedict, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask, extend);
+ VectorSubtract(trace.endpos, mins, trace.endpos);
+ return trace;
+ }
+
+ VectorCopy(start, clipstart);
+ VectorCopy(end, clipend);
+ VectorCopy(mins, clipmins);
+ VectorCopy(maxs, clipmaxs);
+ VectorCopy(mins, clipmins2);
+ VectorCopy(maxs, clipmaxs2);
+#if COLLISIONPARANOID >= 3
+ Con_Printf("move(%f %f %f,%f %f %f)", clipstart[0], clipstart[1], clipstart[2], clipend[0], clipend[1], clipend[2]);
+#endif
+
+ // clip to world
+ Collision_ClipToWorld(&cliptrace, sv.worldmodel, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask, extend);
+ cliptrace.worldstartsolid = cliptrace.bmodelstartsolid = cliptrace.startsolid;
+ if (cliptrace.startsolid || cliptrace.fraction < 1)
+ cliptrace.ent = prog->edicts;
+ if (type == MOVE_WORLDONLY)
+ goto finished;
+
+ if (type == MOVE_MISSILE)
+ {
+ // LadyHavoc: modified this, was = -15, now -= 15
+ for (i = 0;i < 3;i++)
+ {
+ clipmins2[i] -= 15;
+ clipmaxs2[i] += 15;
+ }
+ }
+
+ // get adjusted box for bmodel collisions if the world is q1bsp or hlbsp
+ if (sv.worldmodel && sv.worldmodel->brush.RoundUpToHullSize)
+ sv.worldmodel->brush.RoundUpToHullSize(sv.worldmodel, clipmins, clipmaxs, hullmins, hullmaxs);
+ else
+ {
+ VectorCopy(clipmins, hullmins);
+ VectorCopy(clipmaxs, hullmaxs);
+ }
+
+ // create the bounding box of the entire move
+ for (i = 0;i < 3;i++)
+ {
+ clipboxmins[i] = min(clipstart[i], cliptrace.endpos[i]) + min(hullmins[i], clipmins2[i]) - 1;
+ clipboxmaxs[i] = max(clipstart[i], cliptrace.endpos[i]) + max(hullmaxs[i], clipmaxs2[i]) + 1;
+ }
+
+ // debug override to test against everything
+ if (sv_debugmove.integer)
+ {
+ clipboxmins[0] = clipboxmins[1] = clipboxmins[2] = (vec_t)-999999999;
+ clipboxmaxs[0] = clipboxmaxs[1] = clipboxmaxs[2] = (vec_t)999999999;
+ }
+
+ // if the passedict is world, make it NULL (to avoid two checks each time)
+ if (passedict == prog->edicts)
+ passedict = NULL;
+ // precalculate prog value for passedict for comparisons
+ passedictprog = PRVM_EDICT_TO_PROG(passedict);
+ // figure out whether this is a point trace for comparisons
+ pointtrace = VectorCompare(clipmins, clipmaxs);
+ // precalculate passedict's owner edict pointer for comparisons
+ traceowner = passedict ? PRVM_PROG_TO_EDICT(PRVM_serveredictedict(passedict, owner)) : 0;
+
+ clipgroup = passedict ? (int)PRVM_serveredictfloat(passedict, clipgroup) : 0;
+
+ // clip to entities
+ // because this uses World_EntitiestoBox, we know all entity boxes overlap
+ // the clip region, so we can skip culling checks in the loop below
+ numtouchedicts = SV_EntitiesInBox(clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
+ if (numtouchedicts > MAX_EDICTS)
+ {
+ // this never happens
+ Con_Printf("SV_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
+ numtouchedicts = MAX_EDICTS;
+ }
+ for (i = 0;i < numtouchedicts;i++)
+ {
+ touch = touchedicts[i];
+
+ if (PRVM_serveredictfloat(touch, solid) < SOLID_BBOX)
+ continue;
+ if (type == MOVE_NOMONSTERS && PRVM_serveredictfloat(touch, solid) != SOLID_BSP)
+ continue;
+
+ if (passedict)
+ {
+ // don't clip against self
+ if (passedict == touch)
+ continue;
+ // don't clip owned entities against owner
+ if (traceowner == touch)
+ continue;
+ // don't clip owner against owned entities
+ if (passedictprog == PRVM_serveredictedict(touch, owner))
+ continue;
+ // don't clip against any entities in the same clipgroup (DP_RM_CLIPGROUP)
+ if (clipgroup && clipgroup == (int)PRVM_serveredictfloat(touch, clipgroup))
+ continue;
+ // don't clip points against points (they can't collide)
+ if (pointtrace && VectorCompare(PRVM_serveredictvector(touch, mins), PRVM_serveredictvector(touch, maxs)) && (type != MOVE_MISSILE || !((int)PRVM_serveredictfloat(touch, flags) & FL_MONSTER)))
+ continue;
+ }
+
+ bodysupercontents = PRVM_serveredictfloat(touch, solid) == SOLID_CORPSE ? SUPERCONTENTS_CORPSE : SUPERCONTENTS_BODY;
+
+ // might interact, so do an exact clip
+ model = NULL;
+ if ((int) PRVM_serveredictfloat(touch, solid) == SOLID_BSP || type == MOVE_HITMODEL)
+ {
+ model = SV_GetModelFromEdict(touch);
+ pitchsign = SV_GetPitchSign(prog, touch);
+ }
+ if (model)
+ Matrix4x4_CreateFromQuakeEntity(&matrix, PRVM_serveredictvector(touch, origin)[0], PRVM_serveredictvector(touch, origin)[1], PRVM_serveredictvector(touch, origin)[2], pitchsign * PRVM_serveredictvector(touch, angles)[0], PRVM_serveredictvector(touch, angles)[1], PRVM_serveredictvector(touch, angles)[2], 1);
+ else
+ Matrix4x4_CreateTranslate(&matrix, PRVM_serveredictvector(touch, origin)[0], PRVM_serveredictvector(touch, origin)[1], PRVM_serveredictvector(touch, origin)[2]);
+ Matrix4x4_Invert_Simple(&imatrix, &matrix);
+ VM_GenerateFrameGroupBlend(prog, touch->priv.server->framegroupblend, touch);
+ VM_FrameBlendFromFrameGroupBlend(touch->priv.server->frameblend, touch->priv.server->framegroupblend, model, sv.time);
+ VM_UpdateEdictSkeleton(prog, touch, model, touch->priv.server->frameblend);
+ VectorCopy(PRVM_serveredictvector(touch, mins), touchmins);
+ VectorCopy(PRVM_serveredictvector(touch, maxs), touchmaxs);
+ if (type == MOVE_MISSILE && (int)PRVM_serveredictfloat(touch, flags) & FL_MONSTER)
+ Collision_ClipToGenericEntity(&trace, model, touch->priv.server->frameblend, &touch->priv.server->skeleton, touchmins, touchmaxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins2, clipmaxs2, clipend, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask, extend);
+ else
+ Collision_ClipToGenericEntity(&trace, model, touch->priv.server->frameblend, &touch->priv.server->skeleton, touchmins, touchmaxs, bodysupercontents, &matrix, &imatrix, clipstart, clipmins, clipmaxs, clipend, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask, extend);
+
+ Collision_CombineTraces(&cliptrace, &trace, (void *)touch, PRVM_serveredictfloat(touch, solid) == SOLID_BSP);
+ }
+
+finished:
+ return cliptrace;
+}
+
+#if COLLISIONPARANOID >= 1
+trace_t SV_TraceBox(const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int type, prvm_edict_t *passedict, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask)
+{
+ prvm_prog_t *prog = SVVM_prog;
+ int endstuck;
+ trace_t trace;
+ vec3_t temp;
+ trace = SV_TraceBox_(start, mins, maxs, end, type, passedict, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask);
+ if (passedict)
+ {
+ VectorCopy(trace.endpos, temp);
+ endstuck = SV_TraceBox_(temp, mins, maxs, temp, type, passedict, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask).startsolid;
+#if COLLISIONPARANOID < 3
+ if (trace.startsolid || endstuck)
+#endif
+ Con_Printf("%s{e%i:%f %f %f:%f %f %f:%f:%f %f %f%s%s}\n", (trace.startsolid || endstuck) ? "^3" : "", passedict ? (int)(passedict - prog->edicts) : -1, PRVM_serveredictvector(passedict, origin)[0], PRVM_serveredictvector(passedict, origin)[1], PRVM_serveredictvector(passedict, origin)[2], end[0] - PRVM_serveredictvector(passedict, origin)[0], end[1] - PRVM_serveredictvector(passedict, origin)[1], end[2] - PRVM_serveredictvector(passedict, origin)[2], trace.fraction, trace.endpos[0] - PRVM_serveredictvector(passedict, origin)[0], trace.endpos[1] - PRVM_serveredictvector(passedict, origin)[1], trace.endpos[2] - PRVM_serveredictvector(passedict, origin)[2], trace.startsolid ? " startstuck" : "", endstuck ? " endstuck" : "");
+ }
+ return trace;
+}
+#endif
+
+int SV_PointSuperContents(const vec3_t point)
+{
+ prvm_prog_t *prog = SVVM_prog;
+ int supercontents = 0;
+ int i;
+ prvm_edict_t *touch;
+ vec3_t transformed;
+ // matrices to transform into/out of other entity's space
+ matrix4x4_t matrix, imatrix;
+ // model of other entity
+ model_t *model;
+ int frame;
+ // list of entities to test for collisions
+ int numtouchedicts;
+ static prvm_edict_t *touchedicts[MAX_EDICTS];
+
+ // get world supercontents at this point
+ if (sv.worldmodel && sv.worldmodel->PointSuperContents)
+ supercontents = sv.worldmodel->PointSuperContents(sv.worldmodel, 0, point);
+
+ // if sv_gameplayfix_swiminbmodels is off we're done
+ if (!sv_gameplayfix_swiminbmodels.integer)
+ return supercontents;
+
+ // get list of entities at this point
+ numtouchedicts = SV_EntitiesInBox(point, point, MAX_EDICTS, touchedicts);
+ if (numtouchedicts > MAX_EDICTS)
+ {
+ // this never happens
+ Con_Printf("SV_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
+ numtouchedicts = MAX_EDICTS;
+ }
+ for (i = 0;i < numtouchedicts;i++)
+ {
+ touch = touchedicts[i];
+
+ // we only care about SOLID_BSP for pointcontents
+ if (PRVM_serveredictfloat(touch, solid) != SOLID_BSP)
+ continue;
+
+ // might interact, so do an exact clip
+ model = SV_GetModelFromEdict(touch);
+ if (!model || !model->PointSuperContents)
+ continue;
+ Matrix4x4_CreateFromQuakeEntity(&matrix, PRVM_serveredictvector(touch, origin)[0], PRVM_serveredictvector(touch, origin)[1], PRVM_serveredictvector(touch, origin)[2], PRVM_serveredictvector(touch, angles)[0], PRVM_serveredictvector(touch, angles)[1], PRVM_serveredictvector(touch, angles)[2], 1);
+ Matrix4x4_Invert_Simple(&imatrix, &matrix);
+ Matrix4x4_Transform(&imatrix, point, transformed);
+ frame = (int)PRVM_serveredictfloat(touch, frame);
+ supercontents |= model->PointSuperContents(model, bound(0, frame, (model->numframes - 1)), transformed);
+ }
+
+ return supercontents;
+}
+
+/*
+===============================================================================
+
+Linking entities into the world culling system
+
+===============================================================================
+*/
+
+int SV_EntitiesInBox(const vec3_t mins, const vec3_t maxs, int maxedicts, prvm_edict_t **resultedicts)
+{
+ prvm_prog_t *prog = SVVM_prog;
+ vec3_t paddedmins, paddedmaxs;
+ if (maxedicts < 1 || resultedicts == NULL)
+ return 0;
+ // LadyHavoc: discovered this actually causes its own bugs (dm6 teleporters being too close to info_teleport_destination)
+ //VectorSet(paddedmins, mins[0] - 10, mins[1] - 10, mins[2] - 1);
+ //VectorSet(paddedmaxs, maxs[0] + 10, maxs[1] + 10, maxs[2] + 1);
+ VectorCopy(mins, paddedmins);
+ VectorCopy(maxs, paddedmaxs);
+ if (sv_areadebug.integer)
+ {
+ int numresultedicts = 0;
+ int edictindex;
+ prvm_edict_t *ed;
+ for (edictindex = 1;edictindex < prog->num_edicts;edictindex++)
+ {
+ ed = PRVM_EDICT_NUM(edictindex);
+ if (!ed->free && BoxesOverlap(PRVM_serveredictvector(ed, absmin), PRVM_serveredictvector(ed, absmax), paddedmins, paddedmaxs))
+ {
+ resultedicts[numresultedicts++] = ed;
+ if (numresultedicts == maxedicts)
+ break;
+ }
+ }
+ return numresultedicts;
+ }
+ else
+ return World_EntitiesInBox(&sv.world, paddedmins, paddedmaxs, maxedicts, resultedicts);
+}
+
+void SV_LinkEdict_TouchAreaGrid_Call(prvm_edict_t *touch, prvm_edict_t *ent)
+{
+ prvm_prog_t *prog = SVVM_prog;
+ PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(touch);
+ PRVM_serverglobaledict(other) = PRVM_EDICT_TO_PROG(ent);
+ PRVM_serverglobalfloat(time) = sv.time;
+ PRVM_serverglobalfloat(trace_allsolid) = false;
+ PRVM_serverglobalfloat(trace_startsolid) = false;
+ PRVM_serverglobalfloat(trace_fraction) = 1;
+ PRVM_serverglobalfloat(trace_inwater) = false;
+ PRVM_serverglobalfloat(trace_inopen) = true;
+ VectorCopy (PRVM_serveredictvector(touch, origin), PRVM_serverglobalvector(trace_endpos));
+ VectorSet (PRVM_serverglobalvector(trace_plane_normal), 0, 0, 1);
+ PRVM_serverglobalfloat(trace_plane_dist) = 0;
+ PRVM_serverglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(ent);
+ PRVM_serverglobalfloat(trace_dpstartcontents) = 0;
+ PRVM_serverglobalfloat(trace_dphitcontents) = 0;
+ PRVM_serverglobalfloat(trace_dphitq3surfaceflags) = 0;
+ PRVM_serverglobalstring(trace_dphittexturename) = 0;
+ prog->ExecuteProgram(prog, PRVM_serveredictfunction(touch, touch), "QC function self.touch is missing");
+}
+
+void SV_LinkEdict_TouchAreaGrid(prvm_edict_t *ent)
+{
+ prvm_prog_t *prog = SVVM_prog;
+ int i, numtouchedicts, old_self, old_other;
+ prvm_edict_t *touch;
+ static prvm_edict_t *touchedicts[MAX_EDICTS];
+
+ if (ent == prog->edicts)
+ return; // don't add the world
+
+ if (ent->free)
+ return;
+
+ if (PRVM_serveredictfloat(ent, solid) == SOLID_NOT)
+ return;
+
+ // build a list of edicts to touch, because the link loop can be corrupted
+ // by IncreaseEdicts called during touch functions
+ numtouchedicts = SV_EntitiesInBox(ent->priv.server->areamins, ent->priv.server->areamaxs, MAX_EDICTS, touchedicts);
+ if (numtouchedicts > MAX_EDICTS)
+ {
+ // this never happens
+ Con_Printf("SV_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
+ numtouchedicts = MAX_EDICTS;
+ }
+
+ old_self = PRVM_serverglobaledict(self);
+ old_other = PRVM_serverglobaledict(other);
+ for (i = 0;i < numtouchedicts;i++)
+ {
+ touch = touchedicts[i];
+ if (touch != ent && (int)PRVM_serveredictfloat(touch, solid) == SOLID_TRIGGER && PRVM_serveredictfunction(touch, touch))
+ {
+ SV_LinkEdict_TouchAreaGrid_Call(touch, ent);
+ }
+ }
+ PRVM_serverglobaledict(self) = old_self;
+ PRVM_serverglobaledict(other) = old_other;
+}
+
+static void RotateBBox(const vec3_t mins, const vec3_t maxs, const vec3_t angles, vec3_t rotatedmins, vec3_t rotatedmaxs)
+{
+ vec3_t v, u;
+ matrix4x4_t m;
+ Matrix4x4_CreateFromQuakeEntity(&m, 0, 0, 0, angles[PITCH], angles[YAW], angles[ROLL], 1.0);
+
+ v[0] = mins[0]; v[1] = mins[1]; v[2] = mins[2]; Matrix4x4_Transform(&m, v, u);
+ VectorCopy(u, rotatedmins); VectorCopy(u, rotatedmaxs);
+ v[0] = maxs[0]; v[1] = mins[1]; v[2] = mins[2]; Matrix4x4_Transform(&m, v, u);
+ if(rotatedmins[0] > u[0]) { rotatedmins[0] = u[0]; } if(rotatedmins[1] > u[1]) { rotatedmins[1] = u[1]; } if(rotatedmins[2] > u[2]) { rotatedmins[2] = u[2]; }
+ if(rotatedmaxs[0] < u[0]) { rotatedmaxs[0] = u[0]; } if(rotatedmaxs[1] < u[1]) { rotatedmaxs[1] = u[1]; } if(rotatedmaxs[2] < u[2]) { rotatedmaxs[2] = u[2]; }
+ v[0] = mins[0]; v[1] = maxs[1]; v[2] = mins[2]; Matrix4x4_Transform(&m, v, u);
+ if(rotatedmins[0] > u[0]) { rotatedmins[0] = u[0]; } if(rotatedmins[1] > u[1]) { rotatedmins[1] = u[1]; } if(rotatedmins[2] > u[2]) { rotatedmins[2] = u[2]; }
+ if(rotatedmaxs[0] < u[0]) { rotatedmaxs[0] = u[0]; } if(rotatedmaxs[1] < u[1]) { rotatedmaxs[1] = u[1]; } if(rotatedmaxs[2] < u[2]) { rotatedmaxs[2] = u[2]; }
+ v[0] = maxs[0]; v[1] = maxs[1]; v[2] = mins[2]; Matrix4x4_Transform(&m, v, u);
+ if(rotatedmins[0] > u[0]) { rotatedmins[0] = u[0]; } if(rotatedmins[1] > u[1]) { rotatedmins[1] = u[1]; } if(rotatedmins[2] > u[2]) { rotatedmins[2] = u[2]; }
+ if(rotatedmaxs[0] < u[0]) { rotatedmaxs[0] = u[0]; } if(rotatedmaxs[1] < u[1]) { rotatedmaxs[1] = u[1]; } if(rotatedmaxs[2] < u[2]) { rotatedmaxs[2] = u[2]; }
+ v[0] = mins[0]; v[1] = mins[1]; v[2] = maxs[2]; Matrix4x4_Transform(&m, v, u);
+ if(rotatedmins[0] > u[0]) { rotatedmins[0] = u[0]; } if(rotatedmins[1] > u[1]) { rotatedmins[1] = u[1]; } if(rotatedmins[2] > u[2]) { rotatedmins[2] = u[2]; }
+ if(rotatedmaxs[0] < u[0]) { rotatedmaxs[0] = u[0]; } if(rotatedmaxs[1] < u[1]) { rotatedmaxs[1] = u[1]; } if(rotatedmaxs[2] < u[2]) { rotatedmaxs[2] = u[2]; }
+ v[0] = maxs[0]; v[1] = mins[1]; v[2] = maxs[2]; Matrix4x4_Transform(&m, v, u);
+ if(rotatedmins[0] > u[0]) { rotatedmins[0] = u[0]; } if(rotatedmins[1] > u[1]) { rotatedmins[1] = u[1]; } if(rotatedmins[2] > u[2]) { rotatedmins[2] = u[2]; }
+ if(rotatedmaxs[0] < u[0]) { rotatedmaxs[0] = u[0]; } if(rotatedmaxs[1] < u[1]) { rotatedmaxs[1] = u[1]; } if(rotatedmaxs[2] < u[2]) { rotatedmaxs[2] = u[2]; }
+ v[0] = mins[0]; v[1] = maxs[1]; v[2] = maxs[2]; Matrix4x4_Transform(&m, v, u);
+ if(rotatedmins[0] > u[0]) { rotatedmins[0] = u[0]; } if(rotatedmins[1] > u[1]) { rotatedmins[1] = u[1]; } if(rotatedmins[2] > u[2]) { rotatedmins[2] = u[2]; }
+ if(rotatedmaxs[0] < u[0]) { rotatedmaxs[0] = u[0]; } if(rotatedmaxs[1] < u[1]) { rotatedmaxs[1] = u[1]; } if(rotatedmaxs[2] < u[2]) { rotatedmaxs[2] = u[2]; }
+ v[0] = maxs[0]; v[1] = maxs[1]; v[2] = maxs[2]; Matrix4x4_Transform(&m, v, u);
+ if(rotatedmins[0] > u[0]) { rotatedmins[0] = u[0]; } if(rotatedmins[1] > u[1]) { rotatedmins[1] = u[1]; } if(rotatedmins[2] > u[2]) { rotatedmins[2] = u[2]; }
+ if(rotatedmaxs[0] < u[0]) { rotatedmaxs[0] = u[0]; } if(rotatedmaxs[1] < u[1]) { rotatedmaxs[1] = u[1]; } if(rotatedmaxs[2] < u[2]) { rotatedmaxs[2] = u[2]; }
+}
+
+/*
+===============
+SV_LinkEdict
+
+===============
+*/
+void SV_LinkEdict (prvm_edict_t *ent)
+{
+ prvm_prog_t *prog = SVVM_prog;
+ model_t *model;
+ vec3_t mins, maxs, entmins, entmaxs, entangles;
+ int modelindex;
+
+ if (ent == prog->edicts)
+ return; // don't add the world
+
+ if (ent->free)
+ return;
+
+ modelindex = (int)PRVM_serveredictfloat(ent, modelindex);
+ if (modelindex < 0 || modelindex >= MAX_MODELS)
+ {
+ Con_Printf("edict %i: SOLID_BSP with invalid modelindex!\n", PRVM_NUM_FOR_EDICT(ent));
+ modelindex = 0;
+ }
+ model = SV_GetModelByIndex(modelindex);
+
+ VM_GenerateFrameGroupBlend(prog, ent->priv.server->framegroupblend, ent);
+ VM_FrameBlendFromFrameGroupBlend(ent->priv.server->frameblend, ent->priv.server->framegroupblend, model, sv.time);
+ VM_UpdateEdictSkeleton(prog, ent, model, ent->priv.server->frameblend);
+
+// set the abs box
+
+ if (PRVM_serveredictfloat(ent, movetype) == MOVETYPE_PHYSICS)
+ {
+ // TODO maybe should do this for rotating SOLID_BSP too? Would behave better with rotating doors
+ // TODO special handling for spheres?
+ VectorCopy(PRVM_serveredictvector(ent, mins), entmins);
+ VectorCopy(PRVM_serveredictvector(ent, maxs), entmaxs);
+ VectorCopy(PRVM_serveredictvector(ent, angles), entangles);
+ RotateBBox(entmins, entmaxs, entangles, mins, maxs);
+ VectorAdd(PRVM_serveredictvector(ent, origin), mins, mins);
+ VectorAdd(PRVM_serveredictvector(ent, origin), maxs, maxs);
+ }
+ else if (PRVM_serveredictfloat(ent, solid) == SOLID_BSP)
+ {
+ if (model != NULL)
+ {
+ if (!model->TraceBox)
+ Con_DPrintf("edict %i: SOLID_BSP with non-collidable model\n", PRVM_NUM_FOR_EDICT(ent));
+
+ if (PRVM_serveredictvector(ent, angles)[0] || PRVM_serveredictvector(ent, angles)[2] || PRVM_serveredictvector(ent, avelocity)[0] || PRVM_serveredictvector(ent, avelocity)[2])
+ {
+ VectorAdd(PRVM_serveredictvector(ent, origin), model->rotatedmins, mins);
+ VectorAdd(PRVM_serveredictvector(ent, origin), model->rotatedmaxs, maxs);
+ }
+ else if (PRVM_serveredictvector(ent, angles)[1] || PRVM_serveredictvector(ent, avelocity)[1])
+ {
+ VectorAdd(PRVM_serveredictvector(ent, origin), model->yawmins, mins);
+ VectorAdd(PRVM_serveredictvector(ent, origin), model->yawmaxs, maxs);
+ }
+ else
+ {
+ VectorAdd(PRVM_serveredictvector(ent, origin), model->normalmins, mins);
+ VectorAdd(PRVM_serveredictvector(ent, origin), model->normalmaxs, maxs);
+ }
+ }
+ else
+ {
+ // SOLID_BSP with no model is valid, mainly because some QC setup code does so temporarily
+ VectorAdd(PRVM_serveredictvector(ent, origin), PRVM_serveredictvector(ent, mins), mins);
+ VectorAdd(PRVM_serveredictvector(ent, origin), PRVM_serveredictvector(ent, maxs), maxs);
+ }
+ }
+ else
+ {
+ VectorAdd(PRVM_serveredictvector(ent, origin), PRVM_serveredictvector(ent, mins), mins);
+ VectorAdd(PRVM_serveredictvector(ent, origin), PRVM_serveredictvector(ent, maxs), maxs);
+ }
+
+//
+// to make items easier to pick up and allow them to be grabbed off
+// of shelves, the abs sizes are expanded
+//
+ if ((int)PRVM_serveredictfloat(ent, flags) & FL_ITEM)
+ {
+ mins[0] -= 15;
+ mins[1] -= 15;
+ mins[2] -= 1;
+ maxs[0] += 15;
+ maxs[1] += 15;
+ maxs[2] += 1;
+ }
+ else
+ {
+ // because movement is clipped an epsilon away from an actual edge,
+ // we must fully check even when bounding boxes don't quite touch
+ mins[0] -= 1;
+ mins[1] -= 1;
+ mins[2] -= 1;
+ maxs[0] += 1;
+ maxs[1] += 1;
+ maxs[2] += 1;
+ }
+
+ VectorCopy(mins, PRVM_serveredictvector(ent, absmin));
+ VectorCopy(maxs, PRVM_serveredictvector(ent, absmax));
+
+ World_LinkEdict(&sv.world, ent, mins, maxs);
+}
+
+/*
+===============================================================================
+
+Utility functions
+
+===============================================================================
+*/
+
+/*
+============
+SV_TestEntityPosition
+
+returns true if the entity is in solid currently
+============
+*/
+static int SV_TestEntityPosition (prvm_edict_t *ent, vec3_t offset)
+{
+ prvm_prog_t *prog = SVVM_prog;
+ int hitsupercontentsmask = SV_GenericHitSuperContentsMask(ent);
+ int skipsupercontentsmask = 0;
+ int skipmaterialflagsmask = 0;
+ vec3_t org, entorigin, entmins, entmaxs;
+ trace_t trace;
+ VectorAdd(PRVM_serveredictvector(ent, origin), offset, org);
+ VectorCopy(PRVM_serveredictvector(ent, origin), entorigin);
+ VectorCopy(PRVM_serveredictvector(ent, mins), entmins);
+ VectorCopy(PRVM_serveredictvector(ent, maxs), entmaxs);
+ trace = SV_TraceBox(org, entmins, entmaxs, entorigin, ((PRVM_serveredictfloat(ent, movetype) == MOVETYPE_FLY_WORLDONLY) ? MOVE_WORLDONLY : MOVE_NOMONSTERS), ent, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask, collision_extendmovelength.value);
+ if (trace.startsupercontents & hitsupercontentsmask)
+ return true;
+ else
+ {
+ if (sv.worldmodel->brushq1.numclipnodes && !VectorCompare(PRVM_serveredictvector(ent, mins), PRVM_serveredictvector(ent, maxs)))
+ {
+ // q1bsp/hlbsp use hulls and if the entity does not exactly match
+ // a hull size it is incorrectly tested, so this code tries to
+ // 'fix' it slightly...
+ // FIXME: this breaks entities larger than the hull size
+ int i;
+ vec3_t v, m1, m2, s;
+ VectorAdd(org, entmins, m1);
+ VectorAdd(org, entmaxs, m2);
+ VectorSubtract(m2, m1, s);
+#define EPSILON (1.0f / 32.0f)
+ if (s[0] >= EPSILON*2) {m1[0] += EPSILON;m2[0] -= EPSILON;}
+ if (s[1] >= EPSILON*2) {m1[1] += EPSILON;m2[1] -= EPSILON;}
+ if (s[2] >= EPSILON*2) {m1[2] += EPSILON;m2[2] -= EPSILON;}
+ for (i = 0;i < 8;i++)
+ {
+ v[0] = (i & 1) ? m2[0] : m1[0];
+ v[1] = (i & 2) ? m2[1] : m1[1];
+ v[2] = (i & 4) ? m2[2] : m1[2];
+ if (SV_PointSuperContents(v) & hitsupercontentsmask)
+ return true;
+ }
+ }
+ }
+ // if the trace found a better position for the entity, move it there
+ if (VectorDistance2(trace.endpos, PRVM_serveredictvector(ent, origin)) >= 0.0001)
+ {
+#if 0
+ // please switch back to this code when trace.endpos sometimes being in solid bug is fixed
+ VectorCopy(trace.endpos, PRVM_serveredictvector(ent, origin));
+#else
+ // verify if the endpos is REALLY outside solid
+ VectorCopy(trace.endpos, org);
+ trace = SV_TraceBox(org, entmins, entmaxs, org, MOVE_NOMONSTERS, ent, hitsupercontentsmask, skipsupercontentsmask, skipmaterialflagsmask, collision_extendmovelength.value);
+ if(trace.startsolid)
+ Con_Printf("SV_TestEntityPosition: trace.endpos detected to be in solid. NOT using it.\n");
+ else
+ VectorCopy(org, PRVM_serveredictvector(ent, origin));
+#endif
+ }
+ return false;
+}
+
+// DRESK - Support for Entity Contents Transition Event
+/*
+================
+SV_CheckContentsTransition
+
+returns true if entity had a valid contentstransition function call
+================
+*/
+static int SV_CheckContentsTransition(prvm_edict_t *ent, const int nContents)
+{
+ prvm_prog_t *prog = SVVM_prog;
+ int bValidFunctionCall;
+
+ // Default Valid Function Call to False
+ bValidFunctionCall = false;
+
+ if(PRVM_serveredictfloat(ent, watertype) != nContents)
+ { // Changed Contents
+ // Acquire Contents Transition Function from QC
+ if(PRVM_serveredictfunction(ent, contentstransition))
+ { // Valid Function; Execute
+ // Assign Valid Function
+ bValidFunctionCall = true;
+ // Prepare Parameters (Original Contents, New Contents)
+ // Original Contents
+ PRVM_G_FLOAT(OFS_PARM0) = PRVM_serveredictfloat(ent, watertype);
+ // New Contents
+ PRVM_G_FLOAT(OFS_PARM1) = nContents;
+ // Assign Self
+ PRVM_serverglobaledict(self) = PRVM_EDICT_TO_PROG(ent);
+ // Set Time
+ PRVM_serverglobalfloat(time) = sv.time;
+ // Execute VM Function
+ prog->ExecuteProgram(prog, PRVM_serveredictfunction(ent, contentstransition), "contentstransition: NULL function");
+ }
+ }
+
+ // Return if Function Call was Valid
+ return bValidFunctionCall;
+}
+
+