+ recoversendflags[ent] = 0; // no need to update, we got a more recent remove (and will fix it THEN)
+ break; // no flags left to remove...
+ }
+ else if(sf > 0)
+ recoversendflags[ent] &= ~sf; // no need to update these bits, we already got them later
+ }
+ }
+ }
+
+ for(i = 0; i < client->csqcnumedicts; ++i)
+ {
+ if(recoversendflags[i] < 0)
+ {
+ // a remove got lost, then either send a remove or - if it was
+ // recreated later - a FULL update to make totally sure
+ client->csqcentityscope[i] = 1;
+ client->csqcentitysendflags[i] = 0xFFFFFF;
+ }
+ else
+ client->csqcentitysendflags[i] |= recoversendflags[i];
+ }
+}
+static int EntityFrameCSQC_AllocFrame(client_t *client, int framenum)
+{
+ int ringfirst = client->csqcentityframehistory_next; // oldest entry
+ client->csqcentityframehistory_next += 1;
+ client->csqcentityframehistory_next %= NUM_CSQCENTITYDB_FRAMES;
+ client->csqcentityframehistory[ringfirst].framenum = framenum;
+ client->csqcentityframehistory[ringfirst].num = 0;
+ return ringfirst;
+}
+static void EntityFrameCSQC_DeallocFrame(client_t *client, int framenum)
+{
+ int ringfirst = client->csqcentityframehistory_next; // oldest entry
+ int ringlast = (ringfirst + NUM_CSQCENTITYDB_FRAMES - 1) % NUM_CSQCENTITYDB_FRAMES; // most recently added entry
+ if(framenum == client->csqcentityframehistory[ringlast].framenum)
+ {
+ client->csqcentityframehistory[ringlast].framenum = -1;
+ client->csqcentityframehistory[ringlast].num = 0;
+ client->csqcentityframehistory_next = ringlast;
+ }
+ else
+ Con_Printf("Trying to dealloc the wrong entity frame\n");
+}
+
+//[515]: we use only one array per-client for SendEntity feature
+// TODO: add some handling for entity send priorities, to better deal with huge
+// amounts of csqc networked entities
+qboolean EntityFrameCSQC_WriteFrame (sizebuf_t *msg, int maxsize, int numnumbers, const unsigned short *numbers, int framenum)
+{
+ int num, number, end, sendflags;
+ qboolean sectionstarted = false;
+ const unsigned short *n;
+ prvm_edict_t *ed;
+ client_t *client = svs.clients + sv.writeentitiestoclient_clientnumber;
+ int dbframe = EntityFrameCSQC_AllocFrame(client, framenum);
+ csqcentityframedb_t *db = &client->csqcentityframehistory[dbframe];
+
+ if(client->csqcentityframe_lastreset < 0)
+ client->csqcentityframe_lastreset = framenum;
+
+ maxsize -= 24; // always fit in an empty svc_entities message (for packet loss detection!)
+
+ // make sure there is enough room to store the svc_csqcentities byte,
+ // the terminator (0x0000) and at least one entity update
+ if (msg->cursize + 32 >= maxsize)
+ return false;
+
+ if (client->csqcnumedicts < prog->num_edicts)
+ client->csqcnumedicts = prog->num_edicts;
+
+ number = 1;
+ for (num = 0, n = numbers;num < numnumbers;num++, n++)
+ {
+ end = *n;
+ for (;number < end;number++)
+ {
+ if (client->csqcentityscope[number])
+ {
+ client->csqcentityscope[number] = 1;
+ client->csqcentitysendflags[number] = 0xFFFFFF;
+ }
+ }
+ ed = prog->edicts + number;
+ if (PRVM_serveredictfunction(ed, SendEntity))
+ client->csqcentityscope[number] = 2;
+ else if (client->csqcentityscope[number])
+ {
+ client->csqcentityscope[number] = 1;
+ client->csqcentitysendflags[number] = 0xFFFFFF;
+ }
+ number++;
+ }
+ end = client->csqcnumedicts;
+ for (;number < end;number++)
+ {
+ if (client->csqcentityscope[number])
+ {
+ client->csqcentityscope[number] = 1;
+ client->csqcentitysendflags[number] = 0xFFFFFF;
+ }
+ }
+
+ /*
+ // mark all scope entities as remove
+ for (number = 1;number < client->csqcnumedicts;number++)
+ if (client->csqcentityscope[number])
+ client->csqcentityscope[number] = 1;
+ // keep visible entities
+ for (i = 0, n = numbers;i < numnumbers;i++, n++)
+ {
+ number = *n;
+ ed = prog->edicts + number;
+ if (PRVM_serveredictfunction(ed, SendEntity))
+ client->csqcentityscope[number] = 2;
+ }
+ */
+
+ // now try to emit the entity updates
+ // (FIXME: prioritize by distance?)
+ end = client->csqcnumedicts;
+ for (number = 1;number < end;number++)
+ {
+ if (!client->csqcentityscope[number])
+ continue;
+ sendflags = client->csqcentitysendflags[number];
+ if (!sendflags)
+ continue;
+ if(db->num >= NUM_CSQCENTITIES_PER_FRAME)
+ break;
+ ed = prog->edicts + number;
+ // entity scope is either update (2) or remove (1)
+ if (client->csqcentityscope[number] == 1)
+ {
+ // write a remove message
+ // first write the message identifier if needed
+ if(!sectionstarted)
+ {
+ sectionstarted = 1;
+ MSG_WriteByte(msg, svc_csqcentities);
+ }
+ // write the remove message
+ {
+ ENTITYSIZEPROFILING_START(msg, number);
+ MSG_WriteShort(msg, (unsigned short)number | 0x8000);
+ client->csqcentityscope[number] = 0;
+ client->csqcentitysendflags[number] = 0xFFFFFF; // resend completely if it becomes active again
+ db->entno[db->num] = number;
+ db->sendflags[db->num] = -1;
+ db->num += 1;
+ client->csqcentityglobalhistory[number] = 1;
+ ENTITYSIZEPROFILING_END(msg, number);
+ }
+ if (msg->cursize + 17 >= maxsize)
+ break;
+ }
+ else
+ {
+ // write an update
+ // save the cursize value in case we overflow and have to rollback
+ int oldcursize = msg->cursize;
+ client->csqcentityscope[number] = 1;
+ if (PRVM_serveredictfunction(ed, SendEntity))
+ {
+ if(!sectionstarted)
+ MSG_WriteByte(msg, svc_csqcentities);
+ {
+ ENTITYSIZEPROFILING_START(msg, number);
+ MSG_WriteShort(msg, number);
+ msg->allowoverflow = true;
+ PRVM_G_INT(OFS_PARM0) = sv.writeentitiestoclient_cliententitynumber;
+ PRVM_G_FLOAT(OFS_PARM1) = sendflags;
+ PRVM_serverglobaledict(self) = number;
+ PRVM_ExecuteProgram(PRVM_serveredictfunction(ed, SendEntity), "Null SendEntity\n");
+ msg->allowoverflow = false;
+ if(PRVM_G_FLOAT(OFS_RETURN) && msg->cursize + 2 <= maxsize)
+ {
+ // an update has been successfully written
+ client->csqcentitysendflags[number] = 0;
+ db->entno[db->num] = number;
+ db->sendflags[db->num] = sendflags;
+ db->num += 1;
+ client->csqcentityglobalhistory[number] = 1;
+ // and take note that we have begun the svc_csqcentities
+ // section of the packet
+ sectionstarted = 1;
+ ENTITYSIZEPROFILING_END(msg, number);
+ if (msg->cursize + 17 >= maxsize)
+ break;
+ continue;
+ }
+ }
+ }
+ // self.SendEntity returned false (or does not exist) or the
+ // update was too big for this packet - rollback the buffer to its
+ // state before the writes occurred, we'll try again next frame
+ msg->cursize = oldcursize;
+ msg->overflowed = false;
+ }
+ }
+ if (sectionstarted)
+ {
+ // write index 0 to end the update (0 is never used by real entities)
+ MSG_WriteShort(msg, 0);
+ }
+
+ if(db->num == 0)
+ // if no single ent got added, remove the frame from the DB again, to allow
+ // for a larger history
+ EntityFrameCSQC_DeallocFrame(client, framenum);
+
+ return sectionstarted;
+}
+
+void Protocol_UpdateClientStats(const int *stats)
+{
+ int i;
+ // update the stats array and set deltabits for any changed stats
+ for (i = 0;i < MAX_CL_STATS;i++)
+ {
+ if (host_client->stats[i] != stats[i])
+ {
+ host_client->statsdeltabits[i >> 3] |= 1 << (i & 7);
+ host_client->stats[i] = stats[i];
+ }
+ }
+}
+
+// only a few stats are within the 32 stat limit of Quake, and most of them
+// are sent every frame in svc_clientdata messages, so we only send the
+// remaining ones here
+static const int sendquakestats[] =
+{
+// quake did not send these secrets/monsters stats in this way, but doing so
+// allows a mod to increase STAT_TOTALMONSTERS during the game, and ensures
+// that STAT_SECRETS and STAT_MONSTERS are always correct (even if a client
+// didn't receive an svc_foundsecret or svc_killedmonster), which may be most
+// valuable if randomly seeking around in a demo
+STAT_TOTALSECRETS, // never changes during game
+STAT_TOTALMONSTERS, // changes in some mods
+STAT_SECRETS, // this makes svc_foundsecret unnecessary
+STAT_MONSTERS, // this makes svc_killedmonster unnecessary
+STAT_VIEWHEIGHT, // sent just for FTEQW clients
+STAT_VIEWZOOM, // this rarely changes
+-1,
+};
+
+void Protocol_WriteStatsReliable(void)
+{
+ int i, j;
+ if (!host_client->netconnection)
+ return;
+ // detect changes in stats and write reliable messages
+ // this only deals with 32 stats because the older protocols which use
+ // this function can only cope with 32 stats,
+ // they also do not support svc_updatestatubyte which was introduced in
+ // DP6 protocol (except for QW)
+ for (j = 0;sendquakestats[j] >= 0;j++)
+ {
+ i = sendquakestats[j];
+ // check if this bit is set
+ if (host_client->statsdeltabits[i >> 3] & (1 << (i & 7)))
+ {
+ host_client->statsdeltabits[i >> 3] -= (1 << (i & 7));
+ // send the stat as a byte if possible
+ if (sv.protocol == PROTOCOL_QUAKEWORLD)
+ {
+ if (host_client->stats[i] >= 0 && host_client->stats[i] < 256)
+ {
+ MSG_WriteByte(&host_client->netconnection->message, qw_svc_updatestat);
+ MSG_WriteByte(&host_client->netconnection->message, i);
+ MSG_WriteByte(&host_client->netconnection->message, host_client->stats[i]);