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