+// FIXME FIXME FIXME: at this time the CSQC entity writing does not store
+// packet logs and thus if an update is lost it is never repeated, this makes
+// csqc entities useless at the moment.
+
+void EntityFrameCSQC_WriteState (sizebuf_t *msg, int number, qboolean doupdate, qboolean *sectionstarted)
+{
+ int version;
+ prvm_eval_t *val, *val2;
+ version = 0;
+ if (doupdate)
+ {
+ if (msg->cursize + !*sectionstarted + 2 + 1 + 2 > msg->maxsize)
+ return;
+ val2 = PRVM_EDICTFIELDVALUE((&prog->edicts[number]), prog->fieldoffsets.Version);
+ version = (int)val2->_float;
+ // LordHavoc: do some validity checks on self.Version
+ // if Version reaches 8388608, it has already exhausted half of the
+ // reliable integer space in a 32bit float, and should be reset to 1
+ // FIXME: we should set all client versions of this entity to -1
+ // so that it will not be skipped accidentally in some cases after
+ // the reset
+ if (version < 0)
+ val2->_float = 0;
+ if (version >= 8388608)
+ {
+ int i;
+ val2->_float = 1;
+ // since we just reset the Version field to 1, it may accidentally
+ // end up being equal to an existing client version now or in the
+ // future, so to fix this situation we have to loop over all
+ // clients and change their versions for this entity to be -1
+ // which never matches, thus causing them to receive the update
+ // soon, as they should
+ for (i = 0;i < svs.maxclients;i++)
+ if (sv2csqcents_version[i] && sv2csqcents_version[i][number])
+ sv2csqcents_version[i][number] = -1;
+ }
+ }
+ // if the version already matches, we don't need to do anything as the
+ // latest version has already been sent.
+ if (sv2csqcents_version[csqc_clientnum][number] == version)
+ return;
+ if (version)
+ {
+ // if there's no SendEntity function, treat it as a remove
+ val = PRVM_EDICTFIELDVALUE((&prog->edicts[number]), prog->fieldoffsets.SendEntity);
+ if (val->function)
+ {
+ // there is a function to call, save the cursize value incase we
+ // have to do a rollback due to overflow
+ int oldcursize = msg->cursize;
+ if(!*sectionstarted)
+ MSG_WriteByte(msg, svc_csqcentities);
+ MSG_WriteShort(msg, number);
+ ((int *)prog->globals.generic)[OFS_PARM0] = csqc_clientnum+1;
+ prog->globals.server->self = number;
+ PRVM_ExecuteProgram(val->function, "Null SendEntity\n");
+ if(prog->globals.generic[OFS_RETURN])
+ {
+ if (msg->cursize + 2 > msg->maxsize)
+ {
+ // if the packet no longer has enough room to write the
+ // final index code that ends the message, rollback to the
+ // state before we tried to write anything and then return
+ msg->cursize = oldcursize;
+ msg->overflowed = false;
+ return;
+ }
+ // an update has been successfully written, update the version
+ sv2csqcents_version[csqc_clientnum][number] = version;
+ // and take note that we have begun the svc_csqcentities
+ // section of the packet
+ *sectionstarted = 1;
+ return;
+ }
+ else
+ {
+ // rollback the buffer to its state before the writes
+ msg->cursize = oldcursize;
+ // if the function returned FALSE, simply write a remove
+ // this is done by falling through to the remove code below
+ version = 0;
+ }
+ }
+ }
+ // write a remove message if needed
+ // if already removed, do nothing
+ if (!sv2csqcents_version[csqc_clientnum][number])
+ return;
+ // if there isn't enough room to write the remove message, just return, as
+ // it will be handled in a later packet
+ if (msg->cursize + !*sectionstarted + 2 + 2 > msg->maxsize)
+ return;
+ // first write the message identifier if needed
+ if(!*sectionstarted)
+ {
+ *sectionstarted = 1;
+ MSG_WriteByte(msg, svc_csqcentities);
+ }
+ // write the remove message
+ MSG_WriteShort(msg, (unsigned short)number | 0x8000);
+ sv2csqcents_version[csqc_clientnum][number] = 0;
+}
+