+entity_database4_commit_t;
+
+typedef struct entity_database4_s
+{
+ // what mempool to use for allocations
+ struct mempool_s *mempool;
+ // reference frame
+ int referenceframenum;
+ // reference entities array is resized according to demand
+ int maxreferenceentities;
+ // array of states for entities, these are indexable by their entity number (yes there are gaps)
+ entity_state_t *referenceentity;
+ // commits waiting to be applied to the reference database when confirmed
+ // (commit[i]->numentities == 0 means it is empty)
+ entity_database4_commit_t commit[MAX_ENTITY_HISTORY];
+ // (server only) the current commit being worked on
+ entity_database4_commit_t *currentcommit;
+ // (server only) if a commit won't fit entirely, continue where it left
+ // off next frame
+ int currententitynumber;
+ // (server only)
+ int latestframenumber;
+}
+entityframe4_database_t;
+
+// should-be-private functions that aren't
+entity_state_t *EntityFrame4_GetReferenceEntity(entityframe4_database_t *d, int number);
+void EntityFrame4_AddCommitEntity(entityframe4_database_t *d, const entity_state_t *s);
+
+// allocate a database
+entityframe4_database_t *EntityFrame4_AllocDatabase(struct mempool_s *pool);
+// free a database
+void EntityFrame4_FreeDatabase(entityframe4_database_t *d);
+// reset a database (resets compression but does not reallocate anything)
+void EntityFrame4_ResetDatabase(entityframe4_database_t *d);
+// updates database to account for a frame-received acknowledgment
+int EntityFrame4_AckFrame(entityframe4_database_t *d, int framenum, int servermode);
+// writes a frame to the network stream
+qbool EntityFrame4_WriteFrame(struct sizebuf_s *msg, int maxsize, entityframe4_database_t *d, int numstates, const entity_state_t **states);
+// reads a frame from the network stream
+void EntityFrame4_CL_ReadFrame(void);
+
+// reset all entity fields (typically used if status changed)
+#define E5_FULLUPDATE (1<<0)
+// E5_ORIGIN32=0: short[3] = s->origin[0] * 8, s->origin[1] * 8, s->origin[2] * 8
+// E5_ORIGIN32=1: float[3] = s->origin[0], s->origin[1], s->origin[2]
+#define E5_ORIGIN (1<<1)
+// E5_ANGLES16=0: byte[3] = s->angle[0] * 256 / 360, s->angle[1] * 256 / 360, s->angle[2] * 256 / 360
+// E5_ANGLES16=1: short[3] = s->angle[0] * 65536 / 360, s->angle[1] * 65536 / 360, s->angle[2] * 65536 / 360
+#define E5_ANGLES (1<<2)
+// E5_MODEL16=0: byte = s->modelindex
+// E5_MODEL16=1: short = s->modelindex
+#define E5_MODEL (1<<3)
+// E5_FRAME16=0: byte = s->frame
+// E5_FRAME16=1: short = s->frame
+#define E5_FRAME (1<<4)
+// byte = s->skin
+#define E5_SKIN (1<<5)
+// E5_EFFECTS16=0 && E5_EFFECTS32=0: byte = s->effects
+// E5_EFFECTS16=1 && E5_EFFECTS32=0: short = s->effects
+// E5_EFFECTS16=0 && E5_EFFECTS32=1: int = s->effects
+// E5_EFFECTS16=1 && E5_EFFECTS32=1: int = s->effects
+#define E5_EFFECTS (1<<6)
+// bits >= (1<<8)
+#define E5_EXTEND1 (1<<7)
+
+// byte = s->renderflags
+#define E5_FLAGS (1<<8)
+// byte = bound(0, s->alpha * 255, 255)
+#define E5_ALPHA (1<<9)
+// byte = bound(0, s->scale * 16, 255)
+#define E5_SCALE (1<<10)
+// flag
+#define E5_ORIGIN32 (1<<11)
+// flag
+#define E5_ANGLES16 (1<<12)
+// flag
+#define E5_MODEL16 (1<<13)
+// byte = s->colormap
+#define E5_COLORMAP (1<<14)
+// bits >= (1<<16)
+#define E5_EXTEND2 (1<<15)
+
+// short = s->tagentity
+// byte = s->tagindex
+#define E5_ATTACHMENT (1<<16)
+// short[4] = s->light[0], s->light[1], s->light[2], s->light[3]
+// byte = s->lightstyle
+// byte = s->lightpflags
+#define E5_LIGHT (1<<17)
+// byte = s->glowsize
+// byte = s->glowcolor
+#define E5_GLOW (1<<18)
+// short = s->effects
+#define E5_EFFECTS16 (1<<19)
+// int = s->effects
+#define E5_EFFECTS32 (1<<20)
+// flag
+#define E5_FRAME16 (1<<21)
+// byte[3] = s->colormod[0], s->colormod[1], s->colormod[2]
+#define E5_COLORMOD (1<<22)
+// bits >= (1<<24)
+#define E5_EXTEND3 (1<<23)
+
+// byte[3] = s->glowmod[0], s->glowmod[1], s->glowmod[2]
+#define E5_GLOWMOD (1<<24)
+// byte type=0 short frames[1] short times[1]
+// byte type=1 short frames[2] short times[2] byte lerps[2]
+// byte type=2 short frames[3] short times[3] byte lerps[3]
+// byte type=3 short frames[4] short times[4] byte lerps[4]
+// byte type=4 short modelindex byte numbones {short pose7s[7]}
+// see also RENDER_COMPLEXANIMATION
+#define E5_COMPLEXANIMATION (1<<25)
+// ushort traileffectnum
+#define E5_TRAILEFFECTNUM (1<<26)
+// unused
+#define E5_UNUSED27 (1<<27)
+// unused
+#define E5_UNUSED28 (1<<28)
+// unused
+#define E5_UNUSED29 (1<<29)
+// unused
+#define E5_UNUSED30 (1<<30)
+// bits2 > 0
+#define E5_EXTEND4 (1<<31)
+
+#define ENTITYFRAME5_MAXPACKETLOGS 64
+#define ENTITYFRAME5_MAXSTATES 1024
+#define ENTITYFRAME5_PRIORITYLEVELS 32
+
+typedef struct entityframe5_changestate_s
+{
+ unsigned int number;
+ unsigned int bits;
+}
+entityframe5_changestate_t;
+
+typedef struct entityframe5_packetlog_s
+{
+ int packetnumber;
+ int numstates;
+ entityframe5_changestate_t states[ENTITYFRAME5_MAXSTATES];
+ unsigned char statsdeltabits[(MAX_CL_STATS+7)/8];
+}
+entityframe5_packetlog_t;
+
+typedef struct entityframe5_database_s
+{
+ // number of the latest message sent to client
+ int latestframenum;
+ // updated by WriteFrame for internal use
+ int viewentnum;
+
+ // logs of all recently sent messages (between acked and latest)
+ entityframe5_packetlog_t packetlog[ENTITYFRAME5_MAXPACKETLOGS];
+
+ // this goes up as needed and causes all the arrays to be reallocated
+ int maxedicts;
+
+ // which properties of each entity have changed since last send
+ int *deltabits; // [maxedicts]
+ // priorities of entities (updated whenever deltabits change)
+ // (derived from deltabits)
+ unsigned char *priorities; // [maxedicts]
+ // last frame this entity was sent on, for prioritzation
+ int *updateframenum; // [maxedicts]
+
+ // database of current status of all entities
+ entity_state_t *states; // [maxedicts]
+ // which entities are currently active
+ // (duplicate of the active bit of every state in states[])
+ // (derived from states)
+ unsigned char *visiblebits; // [(maxedicts+7)/8]
+
+ // old notes
+
+ // this is used to decide which changestates to set each frame
+ //int numvisiblestates;
+ //entity_state_t visiblestates[MAX_EDICTS];
+
+ // sorted changing states that need to be sent to the client
+ // kept sorted in lowest to highest priority order, because this allows
+ // the numchangestates to simply be decremented whenever an state is sent,
+ // rather than a memmove to remove them from the start.
+ //int numchangestates;
+ //entityframe5_changestate_t changestates[MAX_EDICTS];
+
+ // buffers for building priority info
+ int prioritychaincounts[ENTITYFRAME5_PRIORITYLEVELS];
+ unsigned short prioritychains[ENTITYFRAME5_PRIORITYLEVELS][ENTITYFRAME5_MAXSTATES];
+}
+entityframe5_database_t;
+
+entityframe5_database_t *EntityFrame5_AllocDatabase(struct mempool_s *pool);
+void EntityFrame5_FreeDatabase(entityframe5_database_t *d);
+void EntityState5_WriteUpdate(int number, const entity_state_t *s, int changedbits, struct sizebuf_s *msg);
+int EntityState5_DeltaBitsForState(entity_state_t *o, entity_state_t *n);
+void EntityFrame5_CL_ReadFrame(void);
+void EntityFrame5_LostFrame(entityframe5_database_t *d, int framenum);
+void EntityFrame5_AckFrame(entityframe5_database_t *d, int framenum);
+qbool EntityFrame5_WriteFrame(struct sizebuf_s *msg, int maxsize, entityframe5_database_t *d, int numstates, const entity_state_t **states, int viewentnum, unsigned int movesequence, qbool need_empty);
+
+extern struct cvar_s developer_networkentities;
+
+// QUAKEWORLD
+// server to client
+#define qw_svc_bad 0
+#define qw_svc_nop 1
+#define qw_svc_disconnect 2
+#define qw_svc_updatestat 3 // [byte] [byte]
+#define qw_svc_setview 5 // [short] entity number
+#define qw_svc_sound 6 // <see code>
+#define qw_svc_print 8 // [byte] id [string] null terminated string
+#define qw_svc_stufftext 9 // [string] stuffed into client's console buffer
+#define qw_svc_setangle 10 // [angle3] set the view angle to this absolute value
+#define qw_svc_serverdata 11 // [long] protocol ...
+#define qw_svc_lightstyle 12 // [byte] [string]
+#define qw_svc_updatefrags 14 // [byte] [short]
+#define qw_svc_stopsound 16 // <see code>
+#define qw_svc_damage 19
+#define qw_svc_spawnstatic 20
+#define qw_svc_spawnbaseline 22
+#define qw_svc_temp_entity 23 // variable
+#define qw_svc_setpause 24 // [byte] on / off
+#define qw_svc_centerprint 26 // [string] to put in center of the screen
+#define qw_svc_killedmonster 27
+#define qw_svc_foundsecret 28
+#define qw_svc_spawnstaticsound 29 // [coord3] [byte] samp [byte] vol [byte] aten
+#define qw_svc_intermission 30 // [vec3_t] origin [vec3_t] angle
+#define qw_svc_finale 31 // [string] text
+#define qw_svc_cdtrack 32 // [byte] track
+#define qw_svc_sellscreen 33
+#define qw_svc_smallkick 34 // set client punchangle to 2
+#define qw_svc_bigkick 35 // set client punchangle to 4
+#define qw_svc_updateping 36 // [byte] [short]
+#define qw_svc_updateentertime 37 // [byte] [float]
+#define qw_svc_updatestatlong 38 // [byte] [long]
+#define qw_svc_muzzleflash 39 // [short] entity
+#define qw_svc_updateuserinfo 40 // [byte] slot [long] uid
+#define qw_svc_download 41 // [short] size [size bytes]
+#define qw_svc_playerinfo 42 // variable
+#define qw_svc_nails 43 // [byte] num [48 bits] xyzpy 12 12 12 4 8
+#define qw_svc_chokecount 44 // [byte] packets choked
+#define qw_svc_modellist 45 // [strings]
+#define qw_svc_soundlist 46 // [strings]
+#define qw_svc_packetentities 47 // [...]
+#define qw_svc_deltapacketentities 48 // [...]
+#define qw_svc_maxspeed 49 // maxspeed change, for prediction
+#define qw_svc_entgravity 50 // gravity change, for prediction
+#define qw_svc_setinfo 51 // setinfo on a client
+#define qw_svc_serverinfo 52 // serverinfo
+#define qw_svc_updatepl 53 // [byte] [byte]
+// QUAKEWORLD
+// client to server
+#define qw_clc_bad 0
+#define qw_clc_nop 1
+#define qw_clc_move 3 // [[usercmd_t]
+#define qw_clc_stringcmd 4 // [string] message
+#define qw_clc_delta 5 // [byte] sequence number, requests delta compression of message
+#define qw_clc_tmove 6 // teleport request, spectator only
+#define qw_clc_upload 7 // teleport request, spectator only
+// QUAKEWORLD
+// playerinfo flags from server
+// playerinfo always sends: playernum, flags, origin[] and framenumber
+#define QW_PF_MSEC (1<<0)
+#define QW_PF_COMMAND (1<<1)
+#define QW_PF_VELOCITY1 (1<<2)
+#define QW_PF_VELOCITY2 (1<<3)
+#define QW_PF_VELOCITY3 (1<<4)
+#define QW_PF_MODEL (1<<5)
+#define QW_PF_SKINNUM (1<<6)
+#define QW_PF_EFFECTS (1<<7)
+#define QW_PF_WEAPONFRAME (1<<8) // only sent for view player
+#define QW_PF_DEAD (1<<9) // don't block movement any more
+#define QW_PF_GIB (1<<10) // offset the view height differently
+#define QW_PF_NOGRAV (1<<11) // don't apply gravity for prediction
+// QUAKEWORLD
+// if the high bit of the client to server byte is set, the low bits are
+// client move cmd bits
+// ms and angle2 are allways sent, the others are optional
+#define QW_CM_ANGLE1 (1<<0)
+#define QW_CM_ANGLE3 (1<<1)
+#define QW_CM_FORWARD (1<<2)
+#define QW_CM_SIDE (1<<3)
+#define QW_CM_UP (1<<4)
+#define QW_CM_BUTTONS (1<<5)
+#define QW_CM_IMPULSE (1<<6)
+#define QW_CM_ANGLE2 (1<<7)
+// QUAKEWORLD
+// the first 16 bits of a packetentities update holds 9 bits
+// of entity number and 7 bits of flags
+#define QW_U_ORIGIN1 (1<<9)
+#define QW_U_ORIGIN2 (1<<10)
+#define QW_U_ORIGIN3 (1<<11)
+#define QW_U_ANGLE2 (1<<12)
+#define QW_U_FRAME (1<<13)
+#define QW_U_REMOVE (1<<14) // REMOVE this entity, don't add it
+#define QW_U_MOREBITS (1<<15)
+// if MOREBITS is set, these additional flags are read in next
+#define QW_U_ANGLE1 (1<<0)
+#define QW_U_ANGLE3 (1<<1)
+#define QW_U_MODEL (1<<2)
+#define QW_U_COLORMAP (1<<3)
+#define QW_U_SKIN (1<<4)
+#define QW_U_EFFECTS (1<<5)
+#define QW_U_SOLID (1<<6) // the entity should be solid for prediction
+// QUAKEWORLD
+// temp entity events
+#define QW_TE_SPIKE 0
+#define QW_TE_SUPERSPIKE 1
+#define QW_TE_GUNSHOT 2
+#define QW_TE_EXPLOSION 3
+#define QW_TE_TAREXPLOSION 4
+#define QW_TE_LIGHTNING1 5
+#define QW_TE_LIGHTNING2 6
+#define QW_TE_WIZSPIKE 7
+#define QW_TE_KNIGHTSPIKE 8
+#define QW_TE_LIGHTNING3 9
+#define QW_TE_LAVASPLASH 10
+#define QW_TE_TELEPORT 11
+#define QW_TE_BLOOD 12
+#define QW_TE_LIGHTNINGBLOOD 13
+// QUAKEWORLD
+// effect flags
+#define QW_EF_BRIGHTFIELD 1
+#define QW_EF_MUZZLEFLASH 2
+#define QW_EF_BRIGHTLIGHT 4
+#define QW_EF_DIMLIGHT 8
+#define QW_EF_FLAG1 16
+#define QW_EF_FLAG2 32
+#define QW_EF_BLUE 64
+#define QW_EF_RED 128
+
+#define QW_UPDATE_BACKUP 64
+#define QW_UPDATE_MASK (QW_UPDATE_BACKUP - 1)
+#define QW_MAX_PACKET_ENTITIES 64
+
+// note: QW stats are directly compatible with NQ
+// (but FRAGS, WEAPONFRAME, and VIEWHEIGHT are unused)
+// so these defines are not actually used by darkplaces, but kept for reference
+#define QW_STAT_HEALTH 0
+//#define QW_STAT_FRAGS 1
+#define QW_STAT_WEAPON 2
+#define QW_STAT_AMMO 3
+#define QW_STAT_ARMOR 4
+//#define QW_STAT_WEAPONFRAME 5
+#define QW_STAT_SHELLS 6
+#define QW_STAT_NAILS 7
+#define QW_STAT_ROCKETS 8
+#define QW_STAT_CELLS 9
+#define QW_STAT_ACTIVEWEAPON 10
+#define QW_STAT_TOTALSECRETS 11
+#define QW_STAT_TOTALMONSTERS 12
+#define QW_STAT_SECRETS 13 // bumped on client side by svc_foundsecret
+#define QW_STAT_MONSTERS 14 // bumped by svc_killedmonster
+#define QW_STAT_ITEMS 15
+//#define QW_STAT_VIEWHEIGHT 16
+
+// build entity data in this, to pass to entity read/write functions
+typedef struct entityframeqw_snapshot_s
+{
+ double time;
+ qbool invalid;
+ int num_entities;
+ entity_state_t entities[QW_MAX_PACKET_ENTITIES];
+}
+entityframeqw_snapshot_t;
+
+typedef struct entityframeqw_database_s
+{
+ entityframeqw_snapshot_t snapshot[QW_UPDATE_BACKUP];
+}
+entityframeqw_database_t;
+
+entityframeqw_database_t *EntityFrameQW_AllocDatabase(struct mempool_s *pool);
+void EntityFrameQW_FreeDatabase(entityframeqw_database_t *d);
+void EntityStateQW_ReadPlayerUpdate(void);
+void EntityFrameQW_CL_ReadFrame(qbool delta);
+
+struct client_s;
+void EntityFrameCSQC_LostFrame(struct client_s *client, int framenum);
+qbool EntityFrameCSQC_WriteFrame (struct sizebuf_s *msg, int maxsize, int numnumbers, const unsigned short *numbers, int framenum);
+
+#endif