]> icculus.org git repositories - divverent/darkplaces.git/blob - protocol.c
more Nexuiz hud stuff from BlackHC, minor style edits to fit in with surrounding...
[divverent/darkplaces.git] / protocol.c
1
2 #include "quakedef.h"
3
4 extern cvar_t developer_networkentities;
5
6 entity_state_t defaultstate =
7 {
8         0,//double time; // time this state was built
9         {0,0,0},//vec3_t origin;
10         {0,0,0},//vec3_t angles;
11         0,//int number; // entity number this state is for
12         0,//unsigned short active; // true if a valid state
13         0,//unsigned short modelindex;
14         0,//unsigned short frame;
15         0,//unsigned short effects;
16         0,//unsigned short tagentity;
17         0,//unsigned short specialvisibilityradius;
18         0,//unsigned short viewmodelforclient;
19         0,//unsigned short exteriormodelforclient;
20         0,//unsigned short nodrawtoclient;
21         0,//unsigned short drawonlytoclient;
22         0,//qbyte colormap;
23         0,//qbyte skin;
24         255,//qbyte alpha;
25         16,//qbyte scale;
26         0,//qbyte glowsize;
27         254,//qbyte glowcolor;
28         0,//qbyte flags;
29         0,//qbyte tagindex;
30 };
31
32 void ClearStateToDefault(entity_state_t *s)
33 {
34         *s = defaultstate;
35         /*
36         memset(s, 0, sizeof(*s));
37         s->alpha = 255;
38         s->scale = 16;
39         s->glowcolor = 254;
40         */
41 }
42
43 void EntityState_Write(entity_state_t *ent, sizebuf_t *msg, entity_state_t *delta)
44 {
45         int bits;
46         vec3_t org, deltaorg;
47         if (ent->active)
48         {
49                 // if not active last frame, delta from defaults
50                 if (!delta->active)
51                         delta = &defaultstate;
52                 bits = 0;
53                 VectorCopy(ent->origin, org);
54                 VectorCopy(delta->origin, deltaorg);
55                 if (ent->flags & RENDER_LOWPRECISION)
56                 {
57                         if (org[0] > 0)
58                                 org[0] = (int) (org[0] + 0.5f);
59                         else
60                                 org[0] = (int) (org[0] - 0.5f);
61                         if (org[1] > 0)
62                                 org[1] = (int) (org[1] + 0.5f);
63                         else
64                                 org[1] = (int) (org[1] - 0.5f);
65                         if (org[2] > 0)
66                                 org[2] = (int) (org[2] + 0.5f);
67                         else
68                                 org[2] = (int) (org[2] - 0.5f);
69                 }
70                 if (delta->flags & RENDER_LOWPRECISION)
71                 {
72                         if (deltaorg[0] > 0)
73                                 deltaorg[0] = (int) (deltaorg[0] + 0.5f);
74                         else
75                                 deltaorg[0] = (int) (deltaorg[0] - 0.5f);
76                         if (deltaorg[1] > 0)
77                                 deltaorg[1] = (int) (deltaorg[1] + 0.5f);
78                         else
79                                 deltaorg[1] = (int) (deltaorg[1] - 0.5f);
80                         if (deltaorg[2] > 0)
81                                 deltaorg[2] = (int) (deltaorg[2] + 0.5f);
82                         else
83                                 deltaorg[2] = (int) (deltaorg[2] - 0.5f);
84                 }
85                 if (fabs(org[0] - deltaorg[0]) > 0.01f)
86                         bits |= E_ORIGIN1;
87                 if (fabs(org[1] - deltaorg[1]) > 0.01f)
88                         bits |= E_ORIGIN2;
89                 if (fabs(org[2] - deltaorg[2]) > 0.01f)
90                         bits |= E_ORIGIN3;
91                 if ((qbyte) (ent->angles[0] * (256.0f / 360.0f)) != (qbyte) (delta->angles[0] * (256.0f / 360.0f)))
92                         bits |= E_ANGLE1;
93                 if ((qbyte) (ent->angles[1] * (256.0f / 360.0f)) != (qbyte) (delta->angles[1] * (256.0f / 360.0f)))
94                         bits |= E_ANGLE2;
95                 if ((qbyte) (ent->angles[2] * (256.0f / 360.0f)) != (qbyte) (delta->angles[2] * (256.0f / 360.0f)))
96                         bits |= E_ANGLE3;
97                 if ((ent->modelindex ^ delta->modelindex) & 0x00FF)
98                         bits |= E_MODEL1;
99                 if ((ent->modelindex ^ delta->modelindex) & 0xFF00)
100                         bits |= E_MODEL2;
101                 if ((ent->frame ^ delta->frame) & 0x00FF)
102                         bits |= E_FRAME1;
103                 if ((ent->frame ^ delta->frame) & 0xFF00)
104                         bits |= E_FRAME2;
105                 if ((ent->effects ^ delta->effects) & 0x00FF)
106                         bits |= E_EFFECTS1;
107                 if ((ent->effects ^ delta->effects) & 0xFF00)
108                         bits |= E_EFFECTS2;
109                 if (ent->colormap != delta->colormap)
110                         bits |= E_COLORMAP;
111                 if (ent->skin != delta->skin)
112                         bits |= E_SKIN;
113                 if (ent->alpha != delta->alpha)
114                         bits |= E_ALPHA;
115                 if (ent->scale != delta->scale)
116                         bits |= E_SCALE;
117                 if (ent->glowsize != delta->glowsize)
118                         bits |= E_GLOWSIZE;
119                 if (ent->glowcolor != delta->glowcolor)
120                         bits |= E_GLOWCOLOR;
121                 if (ent->flags != delta->flags)
122                         bits |= E_FLAGS;
123                 if (ent->tagindex != delta->tagindex || ent->tagentity != delta->tagentity)
124                         bits |= E_TAGATTACHMENT;
125
126                 if (bits) // don't send anything if it hasn't changed
127                 {
128                         if (bits & 0xFF000000)
129                                 bits |= E_EXTEND3;
130                         if (bits & 0x00FF0000)
131                                 bits |= E_EXTEND2;
132                         if (bits & 0x0000FF00)
133                                 bits |= E_EXTEND1;
134
135                         MSG_WriteShort(msg, ent->number);
136                         MSG_WriteByte(msg, bits & 0xFF);
137                         if (bits & E_EXTEND1)
138                         {
139                                 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
140                                 if (bits & E_EXTEND2)
141                                 {
142                                         MSG_WriteByte(msg, (bits >> 16) & 0xFF);
143                                         if (bits & E_EXTEND3)
144                                                 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
145                                 }
146                         }
147                         // LordHavoc: have to write flags first, as they can modify protocol
148                         if (bits & E_FLAGS)
149                                 MSG_WriteByte(msg, ent->flags);
150                         if (ent->flags & RENDER_LOWPRECISION)
151                         {
152                                 if (bits & E_ORIGIN1)
153                                         MSG_WriteShort(msg, org[0]);
154                                 if (bits & E_ORIGIN2)
155                                         MSG_WriteShort(msg, org[1]);
156                                 if (bits & E_ORIGIN3)
157                                         MSG_WriteShort(msg, org[2]);
158                         }
159                         else
160                         {
161                                 if (bits & E_ORIGIN1)
162                                         MSG_WriteFloat(msg, org[0]);
163                                 if (bits & E_ORIGIN2)
164                                         MSG_WriteFloat(msg, org[1]);
165                                 if (bits & E_ORIGIN3)
166                                         MSG_WriteFloat(msg, org[2]);
167                         }
168                         if (bits & E_ANGLE1)
169                                 MSG_WriteAngle(msg, ent->angles[0]);
170                         if (bits & E_ANGLE2)
171                                 MSG_WriteAngle(msg, ent->angles[1]);
172                         if (bits & E_ANGLE3)
173                                 MSG_WriteAngle(msg, ent->angles[2]);
174                         if (bits & E_MODEL1)
175                                 MSG_WriteByte(msg, ent->modelindex & 0xFF);
176                         if (bits & E_MODEL2)
177                                 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
178                         if (bits & E_FRAME1)
179                                 MSG_WriteByte(msg, ent->frame & 0xFF);
180                         if (bits & E_FRAME2)
181                                 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
182                         if (bits & E_EFFECTS1)
183                                 MSG_WriteByte(msg, ent->effects & 0xFF);
184                         if (bits & E_EFFECTS2)
185                                 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
186                         if (bits & E_COLORMAP)
187                                 MSG_WriteByte(msg, ent->colormap);
188                         if (bits & E_SKIN)
189                                 MSG_WriteByte(msg, ent->skin);
190                         if (bits & E_ALPHA)
191                                 MSG_WriteByte(msg, ent->alpha);
192                         if (bits & E_SCALE)
193                                 MSG_WriteByte(msg, ent->scale);
194                         if (bits & E_GLOWSIZE)
195                                 MSG_WriteByte(msg, ent->glowsize);
196                         if (bits & E_GLOWCOLOR)
197                                 MSG_WriteByte(msg, ent->glowcolor);
198                         if (bits & E_TAGATTACHMENT)
199                         {
200                                 MSG_WriteShort(msg, ent->tagentity);
201                                 MSG_WriteByte(msg, ent->tagindex);
202                         }
203                 }
204         }
205         else if (delta->active)
206                 MSG_WriteShort(msg, ent->number | 0x8000);
207 }
208
209 void EntityState_ReadUpdate(entity_state_t *e, int number)
210 {
211         int bits;
212         cl_entities_active[number] = true;
213         e->active = true;
214         e->time = cl.mtime[0];
215         e->number = number;
216
217         bits = MSG_ReadByte();
218         if (bits & E_EXTEND1)
219         {
220                 bits |= MSG_ReadByte() << 8;
221                 if (bits & E_EXTEND2)
222                 {
223                         bits |= MSG_ReadByte() << 16;
224                         if (bits & E_EXTEND3)
225                                 bits |= MSG_ReadByte() << 24;
226                 }
227         }
228
229         if (cl.protocol == PROTOCOL_DARKPLACES2)
230         {
231                 if (bits & E_ORIGIN1)
232                         e->origin[0] = (signed short) MSG_ReadShort();
233                 if (bits & E_ORIGIN2)
234                         e->origin[1] = (signed short) MSG_ReadShort();
235                 if (bits & E_ORIGIN3)
236                         e->origin[2] = (signed short) MSG_ReadShort();
237         }
238         else
239         {
240                 if (bits & E_FLAGS)
241                         e->flags = MSG_ReadByte();
242                 if (e->flags & RENDER_LOWPRECISION || cl.protocol == PROTOCOL_DARKPLACES2)
243                 {
244                         if (bits & E_ORIGIN1)
245                                 e->origin[0] = (signed short) MSG_ReadShort();
246                         if (bits & E_ORIGIN2)
247                                 e->origin[1] = (signed short) MSG_ReadShort();
248                         if (bits & E_ORIGIN3)
249                                 e->origin[2] = (signed short) MSG_ReadShort();
250                 }
251                 else
252                 {
253                         if (bits & E_ORIGIN1)
254                                 e->origin[0] = MSG_ReadFloat();
255                         if (bits & E_ORIGIN2)
256                                 e->origin[1] = MSG_ReadFloat();
257                         if (bits & E_ORIGIN3)
258                                 e->origin[2] = MSG_ReadFloat();
259                 }
260         }
261         if (bits & E_ANGLE1)
262                 e->angles[0] = MSG_ReadAngle();
263         if (bits & E_ANGLE2)
264                 e->angles[1] = MSG_ReadAngle();
265         if (bits & E_ANGLE3)
266                 e->angles[2] = MSG_ReadAngle();
267         if (bits & E_MODEL1)
268                 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
269         if (bits & E_MODEL2)
270                 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
271         if (bits & E_FRAME1)
272                 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
273         if (bits & E_FRAME2)
274                 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
275         if (bits & E_EFFECTS1)
276                 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
277         if (bits & E_EFFECTS2)
278                 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
279         if (bits & E_COLORMAP)
280                 e->colormap = MSG_ReadByte();
281         if (bits & E_SKIN)
282                 e->skin = MSG_ReadByte();
283         if (bits & E_ALPHA)
284                 e->alpha = MSG_ReadByte();
285         if (bits & E_SCALE)
286                 e->scale = MSG_ReadByte();
287         if (bits & E_GLOWSIZE)
288                 e->glowsize = MSG_ReadByte();
289         if (bits & E_GLOWCOLOR)
290                 e->glowcolor = MSG_ReadByte();
291         if (cl.protocol == PROTOCOL_DARKPLACES2)
292                 if (bits & E_FLAGS)
293                         e->flags = MSG_ReadByte();
294         if (bits & E_TAGATTACHMENT)
295         {
296                 e->tagentity = MSG_ReadShort();
297                 e->tagindex = MSG_ReadByte();
298         }
299
300         if (developer_networkentities.integer)
301         {
302                 Con_Printf("ReadUpdate e%i", number);
303
304                 if (bits & E_ORIGIN1)
305                         Con_Printf(" E_ORIGIN1 %f", e->origin[0]);
306                 if (bits & E_ORIGIN2)
307                         Con_Printf(" E_ORIGIN2 %f", e->origin[1]);
308                 if (bits & E_ORIGIN3)
309                         Con_Printf(" E_ORIGIN3 %f", e->origin[2]);
310                 if (bits & E_ANGLE1)
311                         Con_Printf(" E_ANGLE1 %f", e->angles[0]);
312                 if (bits & E_ANGLE2)
313                         Con_Printf(" E_ANGLE2 %f", e->angles[1]);
314                 if (bits & E_ANGLE3)
315                         Con_Printf(" E_ANGLE3 %f", e->angles[2]);
316                 if (bits & (E_MODEL1 | E_MODEL2))
317                         Con_Printf(" E_MODEL %i", e->modelindex);
318
319                 if (bits & (E_FRAME1 | E_FRAME2))
320                         Con_Printf(" E_FRAME %i", e->frame);
321                 if (bits & (E_EFFECTS1 | E_EFFECTS2))
322                         Con_Printf(" E_EFFECTS %i", e->effects);
323                 if (bits & E_ALPHA)
324                         Con_Printf(" E_ALPHA %f", e->alpha / 255.0f);
325                 if (bits & E_SCALE)
326                         Con_Printf(" E_SCALE %f", e->scale / 16.0f);
327                 if (bits & E_COLORMAP)
328                         Con_Printf(" E_COLORMAP %i", e->colormap);
329                 if (bits & E_SKIN)
330                         Con_Printf(" E_SKIN %i", e->skin);
331
332                 if (bits & E_GLOWSIZE)
333                         Con_Printf(" E_GLOWSIZE %i", e->glowsize * 8);
334                 if (bits & E_GLOWCOLOR)
335                         Con_Printf(" E_GLOWCOLOR %i", e->glowcolor);
336
337                 if (bits & E_TAGATTACHMENT)
338                         Con_Printf(" E_TAGATTACHMENT e%i:%i", e->tagentity, e->tagindex);
339                 Con_Printf("\n");
340         }
341 }
342
343 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
344 void EntityFrame_ClearDatabase(entity_database_t *d)
345 {
346         memset(d, 0, sizeof(*d));
347 }
348
349 // (server and client) removes frames older than 'frame' from database
350 void EntityFrame_AckFrame(entity_database_t *d, int frame)
351 {
352         int i;
353         if (d->ackframe < frame)
354                 d->ackframe = frame;
355         for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
356         // ignore outdated frame acks (out of order packets)
357         if (i == 0)
358                 return;
359         d->numframes -= i;
360         // if some queue is left, slide it down to beginning of array
361         if (d->numframes)
362                 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
363 }
364
365 // (server) clears frame, to prepare for adding entities
366 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
367 {
368         f->time = 0;
369         f->framenum = framenum;
370         f->numentities = 0;
371         if (eye == NULL)
372         {
373                 VectorClear(f->eye);
374         }
375         else
376         {
377                 VectorCopy(eye, f->eye);
378         }
379 }
380
381 // (server) adds an entity to frame
382 void EntityFrame_AddEntity(entity_frame_t *f, entity_state_t *s)
383 {
384         if (f->numentities < MAX_ENTITY_DATABASE)
385         {
386                 f->entitydata[f->numentities] = *s;
387                 f->entitydata[f->numentities++].active = true;
388         }
389 }
390
391 // (server and client) reads a frame from the database
392 void EntityFrame_FetchFrame(entity_database_t *d, int framenum, entity_frame_t *f)
393 {
394         int i, n;
395         EntityFrame_Clear(f, NULL, -1);
396         for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
397         if (i < d->numframes && framenum == d->frames[i].framenum)
398         {
399                 f->framenum = framenum;
400                 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
401                 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
402                 if (n > f->numentities)
403                         n = f->numentities;
404                 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
405                 if (f->numentities > n)
406                         memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
407                 VectorCopy(d->eye, f->eye);
408         }
409 }
410
411 // (server and client) adds a entity_frame to the database, for future reference
412 void EntityFrame_AddFrame(entity_database_t *d, entity_frame_t *f)
413 {
414         int n, e;
415         entity_frameinfo_t *info;
416
417         VectorCopy(f->eye, d->eye);
418
419         // figure out how many entity slots are used already
420         if (d->numframes)
421         {
422                 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
423                 if (n + f->numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
424                 {
425                         // ran out of room, dump database
426                         EntityFrame_ClearDatabase(d);
427                 }
428         }
429
430         info = &d->frames[d->numframes];
431         info->framenum = f->framenum;
432         e = -1000;
433         // make sure we check the newly added frame as well, but we haven't incremented numframes yet
434         for (n = 0;n <= d->numframes;n++)
435         {
436                 if (e >= d->frames[n].framenum)
437                 {
438                         if (e == f->framenum)
439                                 Con_Printf("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
440                         else
441                                 Con_Printf("EntityFrame_AddFrame: out of sequence frames in database\n");
442                         return;
443                 }
444                 e = d->frames[n].framenum;
445         }
446         // if database still has frames after that...
447         if (d->numframes)
448                 info->firstentity = d->frames[d->numframes - 1].endentity;
449         else
450                 info->firstentity = 0;
451         info->endentity = info->firstentity + f->numentities;
452         d->numframes++;
453
454         n = info->firstentity % MAX_ENTITY_DATABASE;
455         e = MAX_ENTITY_DATABASE - n;
456         if (e > f->numentities)
457                 e = f->numentities;
458         memcpy(d->entitydata + n, f->entitydata, sizeof(entity_state_t) * e);
459         if (f->numentities > e)
460                 memcpy(d->entitydata, f->entitydata + e, sizeof(entity_state_t) * (f->numentities - e));
461 }
462
463 // (server) writes a frame to network stream
464 static entity_frame_t deltaframe; // FIXME?
465 void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, sizebuf_t *msg)
466 {
467         int i, onum, number;
468         entity_frame_t *o = &deltaframe;
469         entity_state_t *ent, *delta;
470
471         EntityFrame_AddFrame(d, f);
472
473         EntityFrame_FetchFrame(d, d->ackframe > 0 ? d->ackframe : -1, o);
474         MSG_WriteByte (msg, svc_entities);
475         MSG_WriteLong (msg, o->framenum);
476         MSG_WriteLong (msg, f->framenum);
477         MSG_WriteFloat (msg, f->eye[0]);
478         MSG_WriteFloat (msg, f->eye[1]);
479         MSG_WriteFloat (msg, f->eye[2]);
480
481         onum = 0;
482         for (i = 0;i < f->numentities;i++)
483         {
484                 ent = f->entitydata + i;
485                 number = ent->number;
486                 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
487                 {
488                         // write remove message
489                         MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
490                 }
491                 if (onum < o->numentities && (o->entitydata[onum].number == number))
492                 {
493                         // delta from previous frame
494                         delta = o->entitydata + onum;
495                         // advance to next entity in delta frame
496                         onum++;
497                 }
498                 else
499                 {
500                         // delta from defaults
501                         delta = &defaultstate;
502                 }
503                 EntityState_Write(ent, msg, delta);
504         }
505         for (;onum < o->numentities;onum++)
506         {
507                 // write remove message
508                 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
509         }
510         MSG_WriteShort(msg, 0xFFFF);
511 }
512
513 // (client) reads a frame from network stream
514 static entity_frame_t framedata; // FIXME?
515 void EntityFrame_Read(entity_database_t *d)
516 {
517         int number, removed;
518         entity_frame_t *f = &framedata, *delta = &deltaframe;
519         entity_state_t *e, *old, *oldend;
520
521         EntityFrame_Clear(f, NULL, -1);
522
523         // read the frame header info
524         f->time = cl.mtime[0];
525         number = MSG_ReadLong();
526         f->framenum = MSG_ReadLong();
527         f->eye[0] = MSG_ReadFloat();
528         f->eye[1] = MSG_ReadFloat();
529         f->eye[2] = MSG_ReadFloat();
530         EntityFrame_AckFrame(d, number);
531         EntityFrame_FetchFrame(d, number, delta);
532         old = delta->entitydata;
533         oldend = old + delta->numentities;
534         // read entities until we hit the magic 0xFFFF end tag
535         while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF)
536         {
537                 if (msg_badread)
538                         Host_Error("EntityFrame_Read: read error\n");
539                 removed = number & 0x8000;
540                 number &= 0x7FFF;
541                 if (number >= MAX_EDICTS)
542                         Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
543
544                 // seek to entity, while copying any skipped entities (assume unchanged)
545                 while (old < oldend && old->number < number)
546                 {
547                         if (f->numentities >= MAX_ENTITY_DATABASE)
548                                 Host_Error("EntityFrame_Read: entity list too big\n");
549                         f->entitydata[f->numentities] = *old++;
550                         f->entitydata[f->numentities++].time = cl.mtime[0];
551                 }
552                 if (removed)
553                 {
554                         if (old < oldend && old->number == number)
555                                 old++;
556                         else
557                                 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
558                 }
559                 else
560                 {
561                         if (f->numentities >= MAX_ENTITY_DATABASE)
562                                 Host_Error("EntityFrame_Read: entity list too big\n");
563
564                         // reserve this slot
565                         e = f->entitydata + f->numentities++;
566
567                         if (old < oldend && old->number == number)
568                         {
569                                 // delta from old entity
570                                 *e = *old++;
571                         }
572                         else
573                         {
574                                 // delta from defaults
575                                 *e = defaultstate;
576                         }
577
578                         EntityState_ReadUpdate(e, number);
579                 }
580         }
581         while (old < oldend)
582         {
583                 if (f->numentities >= MAX_ENTITY_DATABASE)
584                         Host_Error("EntityFrame_Read: entity list too big\n");
585                 f->entitydata[f->numentities] = *old++;
586                 f->entitydata[f->numentities++].time = cl.mtime[0];
587         }
588         EntityFrame_AddFrame(d, f);
589 }
590
591
592 // (client) returns the frame number of the most recent frame recieved
593 int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
594 {
595         if (d->numframes)
596                 return d->frames[d->numframes - 1].framenum;
597         else
598                 return -1;
599 }
600
601
602
603
604
605
606 int EntityFrame4_SV_ChooseCommitToReplace(entity_database4_t *d)
607 {
608         int i, best, bestframenum;
609         best = 0;
610         bestframenum = d->commit[0].framenum;
611         for (i = 0;i < MAX_ENTITY_HISTORY;i++)
612         {
613                 if (!d->commit[i].numentities)
614                         return i;
615                 if (bestframenum > d->commit[i].framenum)
616                 {
617                         bestframenum = d->commit[i].framenum;
618                         best = i;
619                 }
620         }
621         return best;
622 }
623
624 entity_state_t *EntityFrame4_GetReferenceEntity(entity_database4_t *d, int number)
625 {
626         if (d->maxreferenceentities <= number)
627         {
628                 int oldmax = d->maxreferenceentities;
629                 entity_state_t *oldentity = d->referenceentity;
630                 d->maxreferenceentities = (number + 15) & ~7;
631                 d->referenceentity = Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
632                 if (oldentity)
633                 {
634                         memcpy(d->referenceentity, oldentity, oldmax * sizeof(*d->referenceentity));
635                         Mem_Free(oldentity);
636                 }
637                 // clear the newly created entities
638                 for (;oldmax < d->maxreferenceentities;oldmax++)
639                 {
640                         d->referenceentity[oldmax] = defaultstate;
641                         d->referenceentity[oldmax].number = oldmax;
642                 }
643         }
644         return d->referenceentity + number;
645 }
646
647 void EntityFrame4_AddCommitEntity(entity_database4_t *d, entity_state_t *s)
648 {
649         // resize commit's entity list if full
650         if (d->currentcommit->maxentities <= d->currentcommit->numentities)
651         {
652                 entity_state_t *oldentity = d->currentcommit->entity;
653                 d->currentcommit->maxentities += 8;
654                 d->currentcommit->entity = Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
655                 if (oldentity)
656                 {
657                         memcpy(d->currentcommit->entity, oldentity, d->currentcommit->numentities * sizeof(*d->currentcommit->entity));
658                         Mem_Free(oldentity);
659                 }
660         }
661         d->currentcommit->entity[d->currentcommit->numentities++] = *s;
662 }
663
664 entity_database4_t *EntityFrame4_AllocDatabase(mempool_t *pool)
665 {
666         entity_database4_t *d;
667         d = Mem_Alloc(pool, sizeof(*d));
668         d->mempool = pool;
669         EntityFrame4_ResetDatabase(d);
670         d->ackframenum = -1;
671         return d;
672 }
673
674 void EntityFrame4_FreeDatabase(entity_database4_t *d)
675 {
676         int i;
677         for (i = 0;i < MAX_ENTITY_HISTORY;i++)
678                 if (d->commit[i].entity)
679                         Mem_Free(d->commit[i].entity);
680         if (d->referenceentity)
681                 Mem_Free(d->referenceentity);
682         Mem_Free(d);
683 }
684
685 void EntityFrame4_ResetDatabase(entity_database4_t *d)
686 {
687         int i;
688         d->referenceframenum = -1;
689         for (i = 0;i < MAX_ENTITY_HISTORY;i++)
690                 d->commit[i].numentities = 0;
691         for (i = 0;i < d->maxreferenceentities;i++)
692                 d->referenceentity[i] = defaultstate;
693 }
694
695 void EntityFrame4_AckFrame(entity_database4_t *d, int framenum)
696 {
697         int i, j;
698         entity_database4_commit_t *commit;
699         // check if the peer is requesting no delta compression
700         if (framenum == -1)
701         {
702                 EntityFrame4_ResetDatabase(d);
703                 return;
704         }
705         // return early if this is already the reference frame
706         if (d->referenceframenum == framenum)
707                 return;
708         // find the frame in the database
709         for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
710                 if (commit->numentities && commit->framenum == framenum)
711                         break;
712         // check if the frame was found
713         if (i == MAX_ENTITY_HISTORY)
714         {
715                 Con_Printf("EntityFrame4_AckFrame: frame %i not found in database, expect glitches!  (VERY BAD ERROR)\n", framenum);
716                 d->ackframenum = -1;
717                 EntityFrame4_ResetDatabase(d);
718                 return;
719         }
720         // apply commit to database
721         d->referenceframenum = framenum;
722         for (j = 0;j < commit->numentities;j++)
723         {
724                 //*EntityFrame4_GetReferenceEntity(d, commit->entity[j].number) = commit->entity[j];
725                 entity_state_t *s = EntityFrame4_GetReferenceEntity(d, commit->entity[j].number);
726                 if (commit->entity[j].active != s->active)
727                 {
728                         if (commit->entity[j].active)
729                                 Con_Printf("commit entity %i has become active (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
730                         else
731                                 Con_Printf("commit entity %i has become inactive (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
732                 }
733                 *s = commit->entity[j];
734         }
735         // purge the now-obsolete updates
736         for (i = 0;i < MAX_ENTITY_HISTORY;i++)
737                 if (d->commit[i].framenum <= framenum)
738                         d->commit[i].numentities = 0;
739 }
740
741 void EntityFrame4_SV_WriteFrame_Begin(entity_database4_t *d, sizebuf_t *msg, int framenum)
742 {
743         d->currentcommit = d->commit + EntityFrame4_SV_ChooseCommitToReplace(d);
744         d->currentcommit->numentities = 0;
745         d->currentcommit->framenum = framenum;
746         MSG_WriteByte(msg, svc_entities);
747         MSG_WriteLong(msg, d->referenceframenum);
748         MSG_WriteLong(msg, d->currentcommit->framenum);
749 }
750
751 int EntityFrame4_SV_WriteFrame_Entity(entity_database4_t *d, sizebuf_t *msg, int maxbytes, entity_state_t *s)
752 {
753         qbyte data[128];
754         sizebuf_t buf;
755         entity_state_t *e;
756         // prepare the buffer
757         memset(&buf, 0, sizeof(buf));
758         buf.data = data;
759         buf.maxsize = sizeof(data);
760         // make the message
761         e = EntityFrame4_GetReferenceEntity(d, s->number);
762         // send an update (may update or remove the entity)
763         EntityState_Write(s, &buf, e);
764         // if the message is empty, skip out now
765         if (!buf.cursize)
766                 return true;
767         // if the commit is full, we're done
768         if (msg->cursize + buf.cursize + 2 >= min(msg->maxsize, maxbytes))
769                 return false;
770         // add the entity to the commit
771         EntityFrame4_AddCommitEntity(d, s);
772         // write the message to the packet
773         SZ_Write(msg, buf.data, buf.cursize);
774         // carry on
775         return true;
776 }
777
778 void EntityFrame4_SV_WriteFrame_End(entity_database4_t *d, sizebuf_t *msg)
779 {
780         // remove world message (invalid, and thus a good terminator)
781         MSG_WriteShort(msg, 0x8000);
782         // just to be sure
783         d->currentcommit = NULL;
784 }
785
786 extern void CL_MoveLerpEntityStates(entity_t *ent);
787 void EntityFrame4_CL_ReadFrame(entity_database4_t *d)
788 {
789         int i, n, cnumber, referenceframenum, framenum, enumber, done, stopnumber, skip = false;
790         // read the number of the frame this refers to
791         referenceframenum = MSG_ReadLong();
792         // read the number of this frame
793         framenum = MSG_ReadLong();
794         // read the start number
795         enumber = MSG_ReadShort();
796         for (i = 0;i < MAX_ENTITY_HISTORY;i++)
797                 if (!d->commit[i].numentities)
798                         break;
799         if (i < MAX_ENTITY_HISTORY)
800         {
801                 d->currentcommit = d->commit + i;
802                 d->currentcommit->framenum = d->ackframenum = framenum;
803                 d->currentcommit->numentities = 0;
804                 EntityFrame4_AckFrame(d, referenceframenum);
805                 if (d->ackframenum == -1)
806                 {
807                         Con_Printf("EntityFrame4_CL_ReadFrame: reference frame invalid, this update will be skipped\n");
808                         skip = true;
809                 }
810         }
811         else
812         {
813                 Con_Printf("EntityFrame4_CL_ReadFrame: error while decoding frame %i: database full, reading but not storing this update\n", framenum);
814                 skip = true;
815         }
816         done = false;
817         while (!done && !msg_badread)
818         {
819                 // read the number of the modified entity
820                 // (gaps will be copied unmodified)
821                 n = (unsigned short)MSG_ReadShort();
822                 if (n == 0x8000)
823                 {
824                         // no more entities in this update, but we still need to copy the
825                         // rest of the reference entities (final gap)
826                         done = true;
827                         // read end of range number, then process normally
828                         n = (unsigned short)MSG_ReadShort();
829                 }
830                 // high bit means it's a remove message
831                 cnumber = n & 0x7FFF;
832                 // add one (the changed one) if not done
833                 stopnumber = cnumber + !done;
834                 // process entities in range from the last one to the changed one
835                 for (;enumber < stopnumber;enumber++)
836                 {
837                         if (skip)
838                         {
839                                 if (enumber == cnumber && (n & 0x8000) == 0)
840                                 {
841                                         entity_state_t tempstate;
842                                         EntityState_ReadUpdate(&tempstate, enumber);
843                                 }
844                                 continue;
845                         }
846                         // slide the current into the previous slot
847                         cl_entities[enumber].state_previous = cl_entities[enumber].state_current;
848                         // copy a new current from reference database
849                         cl_entities[enumber].state_current = *EntityFrame4_GetReferenceEntity(d, enumber);
850                         // if this is the one to modify, read more data...
851                         if (enumber == cnumber)
852                         {
853                                 if (n & 0x8000)
854                                 {
855                                         // simply removed
856                                         if (developer_networkentities.integer)
857                                                 Con_Printf("entity %i: remove\n", enumber);
858                                         cl_entities[enumber].state_current = defaultstate;
859                                 }
860                                 else
861                                 {
862                                         // read the changes
863                                         if (developer_networkentities.integer)
864                                                 Con_Printf("entity %i: update\n", enumber);
865                                         EntityState_ReadUpdate(&cl_entities[enumber].state_current, enumber);
866                                 }
867                         }
868                         //else if (developer_networkentities.integer)
869                         //      Con_Printf("entity %i: copy\n", enumber);
870                         // fix the number (it gets wiped occasionally by copying from defaultstate)
871                         cl_entities[enumber].state_current.number = enumber;
872                         // check if we need to update the lerp stuff
873                         if (cl_entities[enumber].state_current.active)
874                         {
875                                 CL_MoveLerpEntityStates(&cl_entities[enumber]);
876                                 cl_entities_active[enumber] = true;
877                         }
878                         // add this to the commit entry whether it is modified or not
879                         if (d->currentcommit)
880                                 EntityFrame4_AddCommitEntity(d, &cl_entities[enumber].state_current);
881                         // print extra messages if desired
882                         if (developer_networkentities.integer && cl_entities[enumber].state_current.active != cl_entities[enumber].state_previous.active)
883                         {
884                                 if (cl_entities[enumber].state_current.active)
885                                         Con_Printf("entity #%i has become active\n", enumber);
886                                 else if (cl_entities[enumber].state_previous.active)
887                                         Con_Printf("entity #%i has become inactive\n", enumber);
888                         }
889                 }
890         }
891         d->currentcommit = NULL;
892 }
893
894