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