+#else
+static int numsendentities;
+static entity_state_t sendentities[MAX_EDICTS];
+static entity_state_t *sendentitiesindex[MAX_EDICTS];
+
+void SV_PrepareEntitiesForSending(void)
+{
+ int e, i;
+ float f;
+ edict_t *ent;
+ entity_state_t cs;
+ // send all entities that touch the pvs
+ numsendentities = 0;
+ sendentitiesindex[0] = NULL;
+ for (e = 1, ent = NEXT_EDICT(sv.edicts);e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
+ {
+ sendentitiesindex[e] = NULL;
+ if (ent->e->free)
+ continue;
+
+ ClearStateToDefault(&cs);
+ cs.active = true;
+ cs.number = e;
+ VectorCopy(ent->v->origin, cs.origin);
+ VectorCopy(ent->v->angles, cs.angles);
+ cs.flags = 0;
+ cs.effects = (int)ent->v->effects;
+ cs.colormap = (qbyte)ent->v->colormap;
+ cs.skin = (qbyte)ent->v->skin;
+ cs.frame = (qbyte)ent->v->frame;
+ cs.viewmodelforclient = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)->edict;
+ cs.exteriormodelforclient = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)->edict;
+ cs.nodrawtoclient = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)->edict;
+ cs.drawonlytoclient = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)->edict;
+ cs.tagentity = GETEDICTFIELDVALUE(ent, eval_tag_entity)->edict;
+ cs.tagindex = (qbyte)GETEDICTFIELDVALUE(ent, eval_tag_index)->_float;
+ i = (int)(GETEDICTFIELDVALUE(ent, eval_glow_size)->_float * 0.25f);
+ cs.glowsize = (qbyte)bound(0, i, 255);
+ if (GETEDICTFIELDVALUE(ent, eval_glow_trail)->_float)
+ cs.flags |= RENDER_GLOWTRAIL;
+
+ cs.modelindex = 0;
+ i = (int)ent->v->modelindex;
+ if (i >= 1 && i < MAX_MODELS && *PR_GetString(ent->v->model))
+ cs.modelindex = i;
+
+ cs.alpha = 255;
+ f = (GETEDICTFIELDVALUE(ent, eval_alpha)->_float * 255.0f);
+ if (f)
+ {
+ i = (int)f;
+ cs.alpha = (qbyte)bound(0, i, 255);
+ }
+ // halflife
+ f = (GETEDICTFIELDVALUE(ent, eval_renderamt)->_float);
+ if (f)
+ {
+ i = (int)f;
+ cs.alpha = (qbyte)bound(0, i, 255);
+ }
+
+ cs.scale = 16;
+ f = (GETEDICTFIELDVALUE(ent, eval_scale)->_float * 16.0f);
+ if (f)
+ {
+ i = (int)f;
+ cs.scale = (qbyte)bound(0, i, 255);
+ }
+
+ cs.glowcolor = 254;
+ f = (GETEDICTFIELDVALUE(ent, eval_glow_color)->_float);
+ if (f)
+ cs.glowcolor = (int)f;
+
+ if (GETEDICTFIELDVALUE(ent, eval_fullbright)->_float)
+ cs.effects |= EF_FULLBRIGHT;
+
+ if (ent->v->movetype == MOVETYPE_STEP)
+ cs.flags |= RENDER_STEP;
+ if ((cs.effects & EF_LOWPRECISION) && cs.origin[0] >= -32768 && cs.origin[1] >= -32768 && cs.origin[2] >= -32768 && cs.origin[0] <= 32767 && cs.origin[1] <= 32767 && cs.origin[2] <= 32767)
+ cs.flags |= RENDER_LOWPRECISION;
+ if (ent->v->colormap >= 1024)
+ cs.flags |= RENDER_COLORMAPPED;
+ if (cs.viewmodelforclient)
+ cs.flags |= RENDER_VIEWMODEL; // show relative to the view
+
+ cs.specialvisibilityradius = 0;
+ if (cs.glowsize)
+ cs.specialvisibilityradius = max(cs.specialvisibilityradius, cs.glowsize * 4);
+ if (cs.flags & RENDER_GLOWTRAIL)
+ cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
+ if (cs.effects & (EF_BRIGHTFIELD | EF_MUZZLEFLASH | EF_BRIGHTLIGHT | EF_DIMLIGHT | EF_RED | EF_BLUE | EF_FLAME | EF_STARDUST))
+ {
+ if (cs.effects & EF_BRIGHTFIELD)
+ cs.specialvisibilityradius = max(cs.specialvisibilityradius, 80);
+ if (cs.effects & EF_MUZZLEFLASH)
+ cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
+ if (cs.effects & EF_BRIGHTLIGHT)
+ cs.specialvisibilityradius = max(cs.specialvisibilityradius, 400);
+ if (cs.effects & EF_DIMLIGHT)
+ cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
+ if (cs.effects & EF_RED)
+ cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
+ if (cs.effects & EF_BLUE)
+ cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
+ if (cs.effects & EF_FLAME)
+ cs.specialvisibilityradius = max(cs.specialvisibilityradius, 250);
+ if (cs.effects & EF_STARDUST)
+ cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
+ }
+
+ if (numsendentities >= MAX_EDICTS)
+ continue;
+ // we can omit invisible entities with no effects that are not clients
+ // LordHavoc: this could kill tags attached to an invisible entity, I
+ // just hope we never have to support that case
+ if (cs.number > svs.maxclients && ((cs.effects & EF_NODRAW) || (!cs.modelindex && !cs.specialvisibilityradius)))
+ continue;
+ sendentitiesindex[e] = sendentities + numsendentities;
+ sendentities[numsendentities++] = cs;
+ }
+}
+
+static int sententitiesmark = 0;
+static int sententities[MAX_EDICTS];
+static int sententitiesconsideration[MAX_EDICTS];
+static int sv_writeentitiestoclient_culled_pvs;
+static int sv_writeentitiestoclient_culled_trace;
+static int sv_writeentitiestoclient_visibleentities;
+static int sv_writeentitiestoclient_totalentities;
+//static entity_frame_t sv_writeentitiestoclient_entityframe;
+static int sv_writeentitiestoclient_clentnum;
+static vec3_t sv_writeentitiestoclient_testeye;
+static client_t *sv_writeentitiestoclient_client;
+
+void SV_MarkWriteEntityStateToClient(entity_state_t *s)
+{
+ vec3_t entmins, entmaxs, lightmins, lightmaxs, testorigin;
+ model_t *model;
+ trace_t trace;
+ if (sententitiesconsideration[s->number] == sententitiesmark)
+ return;
+ sententitiesconsideration[s->number] = sententitiesmark;
+ // viewmodels don't have visibility checking
+ if (s->viewmodelforclient)
+ {
+ if (s->viewmodelforclient != sv_writeentitiestoclient_clentnum)
+ return;
+ }
+ // never reject player
+ else if (s->number != sv_writeentitiestoclient_clentnum)
+ {
+ // check various rejection conditions
+ if (s->nodrawtoclient == sv_writeentitiestoclient_clentnum)
+ return;
+ if (s->drawonlytoclient && s->drawonlytoclient != sv_writeentitiestoclient_clentnum)
+ return;
+ if (s->effects & EF_NODRAW)
+ return;
+ // LordHavoc: only send entities with a model or important effects
+ if (!s->modelindex && s->specialvisibilityradius == 0)
+ return;
+ if (s->tagentity)
+ {
+ // tag attached entities simply check their parent
+ if (!sendentitiesindex[s->tagentity])
+ return;
+ SV_MarkWriteEntityStateToClient(sendentitiesindex[s->tagentity]);
+ if (sententities[s->tagentity] != sententitiesmark)
+ return;
+ }
+ // always send world submodels, they don't generate much traffic
+ else if ((model = sv.models[s->modelindex]) == NULL || model->name[0] != '*')
+ {
+ Mod_CheckLoaded(model);
+ // entity has survived every check so far, check if visible
+ // enlarged box to account for prediction (not that there is
+ // any currently, but still helps the 'run into a room and
+ // watch items pop up' problem)
+ entmins[0] = s->origin[0] - 32.0f;
+ entmins[1] = s->origin[1] - 32.0f;
+ entmins[2] = s->origin[2] - 32.0f;
+ entmaxs[0] = s->origin[0] + 32.0f;
+ entmaxs[1] = s->origin[1] + 32.0f;
+ entmaxs[2] = s->origin[2] + 32.0f;
+ // using the model's bounding box to ensure things are visible regardless of their physics box
+ if (model)
+ {
+ if (s->angles[0] || s->angles[2]) // pitch and roll
+ {
+ VectorAdd(entmins, model->rotatedmins, entmins);
+ VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
+ }
+ else if (s->angles[1])
+ {
+ VectorAdd(entmins, model->yawmins, entmins);
+ VectorAdd(entmaxs, model->yawmaxs, entmaxs);
+ }
+ else
+ {
+ VectorAdd(entmins, model->normalmins, entmins);
+ VectorAdd(entmaxs, model->normalmaxs, entmaxs);
+ }
+ }
+ lightmins[0] = min(entmins[0], s->origin[0] - s->specialvisibilityradius);
+ lightmins[1] = min(entmins[1], s->origin[1] - s->specialvisibilityradius);
+ lightmins[2] = min(entmins[2], s->origin[2] - s->specialvisibilityradius);
+ lightmaxs[0] = min(entmaxs[0], s->origin[0] + s->specialvisibilityradius);
+ lightmaxs[1] = min(entmaxs[1], s->origin[1] + s->specialvisibilityradius);
+ lightmaxs[2] = min(entmaxs[2], s->origin[2] + s->specialvisibilityradius);
+ sv_writeentitiestoclient_totalentities++;
+ // if not touching a visible leaf
+ if (sv_cullentities_pvs.integer && sv_writeentitiestoclient_pvsbytes && sv.worldmodel && sv.worldmodel->brush.BoxTouchingPVS && !sv.worldmodel->brush.BoxTouchingPVS(sv.worldmodel, sv_writeentitiestoclient_pvs, lightmins, lightmaxs))
+ {
+ sv_writeentitiestoclient_culled_pvs++;
+ return;
+ }
+ // or not seen by random tracelines
+ if (sv_cullentities_trace.integer)
+ {
+ // LordHavoc: test center first
+ testorigin[0] = (entmins[0] + entmaxs[0]) * 0.5f;
+ testorigin[1] = (entmins[1] + entmaxs[1]) * 0.5f;
+ testorigin[2] = (entmins[2] + entmaxs[2]) * 0.5f;
+ sv.worldmodel->brush.TraceBox(sv.worldmodel, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
+ if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
+ sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
+ else
+ {
+ // LordHavoc: test random offsets, to maximize chance of detection
+ testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
+ testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
+ testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
+ sv.worldmodel->brush.TraceBox(sv.worldmodel, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
+ if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
+ sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
+ else
+ {
+ if (s->specialvisibilityradius)
+ {
+ // LordHavoc: test random offsets, to maximize chance of detection
+ testorigin[0] = lhrandom(lightmins[0], lightmaxs[0]);
+ testorigin[1] = lhrandom(lightmins[1], lightmaxs[1]);
+ testorigin[2] = lhrandom(lightmins[2], lightmaxs[2]);
+ sv.worldmodel->brush.TraceBox(sv.worldmodel, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
+ if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
+ sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
+ }
+ }
+ }
+ if (realtime > sv_writeentitiestoclient_client->visibletime[s->number])
+ {
+ sv_writeentitiestoclient_culled_trace++;
+ return;
+ }
+ }
+ sv_writeentitiestoclient_visibleentities++;
+ }
+ }
+ // this just marks it for sending
+ // FIXME: it would be more efficient to send here, but the entity
+ // compressor isn't that flexible
+ sententities[s->number] = sententitiesmark;
+}
+
+void SV_WriteEntitiesToClient(client_t *client, edict_t *clent, sizebuf_t *msg)
+{
+ int i;
+ vec3_t testorigin;
+ entity_state_t *s;
+ entity_database4_t *d;
+ int maxbytes, n, startnumber;
+ entity_state_t *e, inactiveentitystate;
+ sizebuf_t buf;
+ qbyte data[128];
+ // prepare the buffer
+ memset(&buf, 0, sizeof(buf));
+ buf.data = data;
+ buf.maxsize = sizeof(data);
+
+ d = client->entitydatabase4;
+
+ for (i = 0;i < MAX_ENTITY_HISTORY;i++)
+ if (!d->commit[i].numentities)
+ break;
+ // if commit buffer full, just don't bother writing an update this frame
+ if (i == MAX_ENTITY_HISTORY)
+ return;
+ d->currentcommit = d->commit + i;
+
+ // this state's number gets played around with later
+ ClearStateToDefault(&inactiveentitystate);
+ //inactiveentitystate = defaultstate;
+
+ sv_writeentitiestoclient_client = client;
+
+ sv_writeentitiestoclient_culled_pvs = 0;
+ sv_writeentitiestoclient_culled_trace = 0;
+ sv_writeentitiestoclient_visibleentities = 0;
+ sv_writeentitiestoclient_totalentities = 0;
+
+ Mod_CheckLoaded(sv.worldmodel);
+
+// find the client's PVS
+ // the real place being tested from
+ VectorAdd(clent->v->origin, clent->v->view_ofs, sv_writeentitiestoclient_testeye);
+ sv_writeentitiestoclient_pvsbytes = 0;
+ if (sv.worldmodel && sv.worldmodel->brush.FatPVS)
+ sv_writeentitiestoclient_pvsbytes = sv.worldmodel->brush.FatPVS(sv.worldmodel, sv_writeentitiestoclient_testeye, 8, sv_writeentitiestoclient_pvs, sizeof(sv_writeentitiestoclient_pvs));
+
+ sv_writeentitiestoclient_clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
+
+ sententitiesmark++;
+
+ // the place being reported (to consider the fact the client still
+ // applies the view_ofs[2], so we have to only send the fractional part
+ // of view_ofs[2], undoing what the client will redo)
+ VectorCopy(sv_writeentitiestoclient_testeye, testorigin);
+ i = (int) clent->v->view_ofs[2] & 255;
+ if (i >= 128)
+ i -= 256;
+ testorigin[2] -= (float) i;
+
+ for (i = 0;i < numsendentities;i++)
+ SV_MarkWriteEntityStateToClient(sendentities + i);
+
+ // calculate maximum bytes to allow in this packet
+ // deduct 4 to account for the end data
+ maxbytes = min(msg->maxsize, MAX_PACKETFRAGMENT) - 4;
+
+ d->currentcommit->numentities = 0;
+ d->currentcommit->framenum = ++client->entityframenumber;
+ MSG_WriteByte(msg, svc_entities);
+ MSG_WriteLong(msg, d->referenceframenum);
+ MSG_WriteLong(msg, d->currentcommit->framenum);
+ if (developer_networkentities.integer >= 1)
+ {
+ Con_Printf("send svc_entities ref:%i num:%i (database: ref:%i commits:", d->referenceframenum, d->currentcommit->framenum, d->referenceframenum);
+ for (i = 0;i < MAX_ENTITY_HISTORY;i++)
+ if (d->commit[i].numentities)
+ Con_Printf(" %i", d->commit[i].framenum);
+ Con_Printf(")\n");
+ }
+ if (d->currententitynumber >= sv.max_edicts)
+ startnumber = 1;
+ else
+ startnumber = bound(1, d->currententitynumber, sv.max_edicts - 1);
+ MSG_WriteShort(msg, startnumber);
+ // reset currententitynumber so if the loop does not break it we will
+ // start at beginning next frame (if it does break, it will set it)
+ d->currententitynumber = 1;
+ for (i = 0, n = startnumber;n < sv.max_edicts;n++)
+ {
+ // find the old state to delta from
+ e = EntityFrame4_GetReferenceEntity(d, n);
+ // prepare the buffer
+ SZ_Clear(&buf);
+ // make the message
+ if (sententities[n] == sententitiesmark)
+ {
+ // entity exists, build an update (if empty there is no change)
+ // find the state in the list
+ for (;i < numsendentities && sendentities[i].number < n;i++);
+ s = sendentities + i;
+ if (s->number != n)
+ Sys_Error("SV_WriteEntitiesToClient: s->number != n\n");
+ // build the update
+ if (s->exteriormodelforclient && s->exteriormodelforclient == sv_writeentitiestoclient_clentnum)
+ {
+ s->flags |= RENDER_EXTERIORMODEL;
+ EntityState_Write(s, &buf, e);
+ s->flags &= ~RENDER_EXTERIORMODEL;
+ }
+ else
+ EntityState_Write(s, &buf, e);
+ }
+ else
+ {
+ s = &inactiveentitystate;
+ s->number = n;
+ if (e->active)
+ {
+ // entity used to exist but doesn't anymore, send remove
+ MSG_WriteShort(&buf, n | 0x8000);
+ }
+ }
+ // if the commit is full, we're done this frame
+ if (msg->cursize + buf.cursize > maxbytes)
+ {
+ // next frame we will continue where we left off
+ break;
+ }
+ // add the entity to the commit
+ EntityFrame4_AddCommitEntity(d, s);
+ // if the message is empty, skip out now
+ if (buf.cursize)
+ {
+ // write the message to the packet
+ SZ_Write(msg, buf.data, buf.cursize);
+ }
+ }
+ d->currententitynumber = n;
+
+ // remove world message (invalid, and thus a good terminator)
+ MSG_WriteShort(msg, 0x8000);
+ // write the number of the end entity
+ MSG_WriteShort(msg, d->currententitynumber);
+ // just to be sure
+ d->currentcommit = NULL;
+
+ if (sv_cullentities_stats.integer)
+ Con_Printf("client \"%s\" entities: %d total, %d visible, %d culled by: %d pvs %d trace\n", client->name, sv_writeentitiestoclient_totalentities, sv_writeentitiestoclient_visibleentities, sv_writeentitiestoclient_culled_pvs + sv_writeentitiestoclient_culled_trace, sv_writeentitiestoclient_culled_pvs, sv_writeentitiestoclient_culled_trace);
+}
+#endif