+ EntityState_WriteUpdate(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 > msg->maxsize - 4)
+ {
+ // 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);
+}
+*/
+
+void SV_WriteEntitiesToClient(client_t *client, edict_t *clent, sizebuf_t *msg)
+{
+ int i, numsendstates;
+ entity_state_t *s;
+
+ // if there isn't enough space to accomplish anything, skip it
+ if (msg->cursize + 25 > msg->maxsize)
+ return;
+
+ 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++;
+
+ for (i = 0;i < numsendentities;i++)
+ SV_MarkWriteEntityStateToClient(sendentities + i);
+
+ numsendstates = 0;
+ for (i = 0;i < numsendentities;i++)
+ {
+ if (sententities[sendentities[i].number] == sententitiesmark)
+ {
+ s = &sendstates[numsendstates++];
+ *s = sendentities[i];
+ if (s->exteriormodelforclient && s->exteriormodelforclient == sv_writeentitiestoclient_clentnum)
+ s->flags |= RENDER_EXTERIORMODEL;