md3 tag attachments (implemented but untested), also the capability to attach any...
[divverent/darkplaces.git] / protocol.c
1
2 #include "quakedef.h"
3
4 void ClearStateToDefault(entity_state_t *s)
5 {
6         memset(s, 0, sizeof(*s));
7         s->alpha = 255;
8         s->scale = 16;
9         s->glowcolor = 254;
10 }
11
12 // (server) clears the database to contain no frames (thus delta compression compresses against nothing)
13 void EntityFrame_ClearDatabase(entity_database_t *d)
14 {
15         memset(d, 0, sizeof(*d));
16 }
17
18 // (server and client) removes frames older than 'frame' from database
19 void EntityFrame_AckFrame(entity_database_t *d, int frame)
20 {
21         int i;
22         if (d->ackframe < frame)
23                 d->ackframe = frame;
24         for (i = 0;i < d->numframes && d->frames[i].framenum < frame;i++);
25         // ignore outdated frame acks (out of order packets)
26         if (i == 0)
27                 return;
28         d->numframes -= i;
29         // if some queue is left, slide it down to beginning of array
30         if (d->numframes)
31                 memmove(&d->frames[0], &d->frames[i], sizeof(d->frames[0]) * d->numframes);
32 }
33
34 // (server) clears frame, to prepare for adding entities
35 void EntityFrame_Clear(entity_frame_t *f, vec3_t eye, int framenum)
36 {
37         f->time = 0;
38         f->framenum = framenum;
39         f->numentities = 0;
40         if (eye == NULL)
41         {
42                 VectorClear(f->eye);
43         }
44         else
45         {
46                 VectorCopy(eye, f->eye);
47         }
48 }
49
50 // (server) adds an entity to frame
51 void EntityFrame_AddEntity(entity_frame_t *f, entity_state_t *s)
52 {
53         if (f->numentities < MAX_ENTITY_DATABASE)
54         {
55                 f->entitydata[f->numentities] = *s;
56                 f->entitydata[f->numentities++].active = true;
57         }
58 }
59
60 // (server and client) reads a frame from the database
61 void EntityFrame_FetchFrame(entity_database_t *d, int framenum, entity_frame_t *f)
62 {
63         int i, n;
64         EntityFrame_Clear(f, NULL, -1);
65         for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
66         if (i < d->numframes && framenum == d->frames[i].framenum)
67         {
68                 f->framenum = framenum;
69                 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
70                 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
71                 if (n > f->numentities)
72                         n = f->numentities;
73                 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
74                 if (f->numentities > n)
75                         memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
76                 VectorCopy(d->eye, f->eye);
77         }
78 }
79
80 // (server and client) adds a entity_frame to the database, for future reference
81 void EntityFrame_AddFrame(entity_database_t *d, entity_frame_t *f)
82 {
83         int n, e;
84         entity_frameinfo_t *info;
85
86         VectorCopy(f->eye, d->eye);
87
88         // figure out how many entity slots are used already
89         if (d->numframes)
90         {
91                 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
92                 if (n + f->numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
93                 {
94                         // ran out of room, dump database
95                         EntityFrame_ClearDatabase(d);
96                 }
97         }
98
99         info = &d->frames[d->numframes];
100         info->framenum = f->framenum;
101         e = -1000;
102         // make sure we check the newly added frame as well, but we haven't incremented numframes yet
103         for (n = 0;n <= d->numframes;n++)
104         {
105                 if (e >= d->frames[n].framenum)
106                 {
107                         if (e == f->framenum)
108                                 Con_Printf("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
109                         else
110                                 Con_Printf("EntityFrame_AddFrame: out of sequence frames in database\n");
111                         return;
112                 }
113                 e = d->frames[n].framenum;
114         }
115         // if database still has frames after that...
116         if (d->numframes)
117                 info->firstentity = d->frames[d->numframes - 1].endentity;
118         else
119                 info->firstentity = 0;
120         info->endentity = info->firstentity + f->numentities;
121         d->numframes++;
122
123         n = info->firstentity % MAX_ENTITY_DATABASE;
124         e = MAX_ENTITY_DATABASE - n;
125         if (e > f->numentities)
126                 e = f->numentities;
127         memcpy(d->entitydata + n, f->entitydata, sizeof(entity_state_t) * e);
128         if (f->numentities > e)
129                 memcpy(d->entitydata, f->entitydata + e, sizeof(entity_state_t) * (f->numentities - e));
130 }
131
132 // (server) writes a frame to network stream
133 static entity_frame_t deltaframe; // FIXME?
134 void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, sizebuf_t *msg)
135 {
136         int i, onum, bits, number;
137         float org[3], deltaorg[3];
138         entity_frame_t *o = &deltaframe;
139         entity_state_t *ent, *delta, baseline;
140
141         EntityFrame_AddFrame(d, f);
142
143         ClearStateToDefault(&baseline);
144         EntityFrame_FetchFrame(d, d->ackframe > 0 ? d->ackframe : -1, o);
145         MSG_WriteByte (msg, svc_entities);
146         MSG_WriteLong (msg, o->framenum);
147         MSG_WriteLong (msg, f->framenum);
148         MSG_WriteFloat (msg, f->eye[0]);
149         MSG_WriteFloat (msg, f->eye[1]);
150         MSG_WriteFloat (msg, f->eye[2]);
151
152         onum = 0;
153         for (i = 0;i < f->numentities;i++)
154         {
155                 ent = f->entitydata + i;
156                 number = ent->number;
157                 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
158                 {
159                         // write remove message
160                         MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
161                 }
162                 if (onum < o->numentities && (o->entitydata[onum].number == number))
163                 {
164                         // delta from previous frame
165                         delta = o->entitydata + onum;
166                         // advance to next entity in delta frame
167                         onum++;
168                 }
169                 else
170                 {
171                         // delta from baseline
172                         delta = &baseline;
173                 }
174                 bits = 0;
175                 VectorCopy(ent->origin, org);
176                 VectorCopy(delta->origin, deltaorg);
177                 if (ent->flags & RENDER_LOWPRECISION)
178                 {
179                         if (org[0] > 0)
180                                 org[0] = (int) (org[0] + 0.5f);
181                         else
182                                 org[0] = (int) (org[0] - 0.5f);
183                         if (org[1] > 0)
184                                 org[1] = (int) (org[1] + 0.5f);
185                         else
186                                 org[1] = (int) (org[1] - 0.5f);
187                         if (org[2] > 0)
188                                 org[2] = (int) (org[2] + 0.5f);
189                         else
190                                 org[2] = (int) (org[2] - 0.5f);
191                 }
192                 if (delta->flags & RENDER_LOWPRECISION)
193                 {
194                         if (deltaorg[0] > 0)
195                                 deltaorg[0] = (int) (deltaorg[0] + 0.5f);
196                         else
197                                 deltaorg[0] = (int) (deltaorg[0] - 0.5f);
198                         if (deltaorg[1] > 0)
199                                 deltaorg[1] = (int) (deltaorg[1] + 0.5f);
200                         else
201                                 deltaorg[1] = (int) (deltaorg[1] - 0.5f);
202                         if (deltaorg[2] > 0)
203                                 deltaorg[2] = (int) (deltaorg[2] + 0.5f);
204                         else
205                                 deltaorg[2] = (int) (deltaorg[2] - 0.5f);
206                 }
207                 if (fabs(org[0] - deltaorg[0]) > 0.01f)
208                         bits |= E_ORIGIN1;
209                 if (fabs(org[1] - deltaorg[1]) > 0.01f)
210                         bits |= E_ORIGIN2;
211                 if (fabs(org[2] - deltaorg[2]) > 0.01f)
212                         bits |= E_ORIGIN3;
213                 if ((qbyte) (ent->angles[0] * (256.0f / 360.0f)) != (qbyte) (delta->angles[0] * (256.0f / 360.0f)))
214                         bits |= E_ANGLE1;
215                 if ((qbyte) (ent->angles[1] * (256.0f / 360.0f)) != (qbyte) (delta->angles[1] * (256.0f / 360.0f)))
216                         bits |= E_ANGLE2;
217                 if ((qbyte) (ent->angles[2] * (256.0f / 360.0f)) != (qbyte) (delta->angles[2] * (256.0f / 360.0f)))
218                         bits |= E_ANGLE3;
219                 if ((ent->modelindex ^ delta->modelindex) & 0x00FF)
220                         bits |= E_MODEL1;
221                 if ((ent->modelindex ^ delta->modelindex) & 0xFF00)
222                         bits |= E_MODEL2;
223                 if ((ent->frame ^ delta->frame) & 0x00FF)
224                         bits |= E_FRAME1;
225                 if ((ent->frame ^ delta->frame) & 0xFF00)
226                         bits |= E_FRAME2;
227                 if ((ent->effects ^ delta->effects) & 0x00FF)
228                         bits |= E_EFFECTS1;
229                 if ((ent->effects ^ delta->effects) & 0xFF00)
230                         bits |= E_EFFECTS2;
231                 if (ent->colormap != delta->colormap)
232                         bits |= E_COLORMAP;
233                 if (ent->skin != delta->skin)
234                         bits |= E_SKIN;
235                 if (ent->alpha != delta->alpha)
236                         bits |= E_ALPHA;
237                 if (ent->scale != delta->scale)
238                         bits |= E_SCALE;
239                 if (ent->glowsize != delta->glowsize)
240                         bits |= E_GLOWSIZE;
241                 if (ent->glowcolor != delta->glowcolor)
242                         bits |= E_GLOWCOLOR;
243                 if (ent->flags != delta->flags)
244                         bits |= E_FLAGS;
245                 if (ent->tagindex != delta->tagindex || ent->tagentity != delta->tagentity)
246                         bits |= E_TAGATTACHMENT;
247
248                 if (bits) // don't send anything if it hasn't changed
249                 {
250                         if (bits & 0xFF000000)
251                                 bits |= E_EXTEND3;
252                         if (bits & 0x00FF0000)
253                                 bits |= E_EXTEND2;
254                         if (bits & 0x0000FF00)
255                                 bits |= E_EXTEND1;
256
257                         MSG_WriteShort(msg, number);
258                         MSG_WriteByte(msg, bits & 0xFF);
259                         if (bits & E_EXTEND1)
260                         {
261                                 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
262                                 if (bits & E_EXTEND2)
263                                 {
264                                         MSG_WriteByte(msg, (bits >> 16) & 0xFF);
265                                         if (bits & E_EXTEND3)
266                                                 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
267                                 }
268                         }
269                         // LordHavoc: have to write flags first, as they can modify protocol
270                         if (bits & E_FLAGS)
271                                 MSG_WriteByte(msg, ent->flags);
272                         if (ent->flags & RENDER_LOWPRECISION)
273                         {
274                                 if (bits & E_ORIGIN1)
275                                         MSG_WriteShort(msg, org[0]);
276                                 if (bits & E_ORIGIN2)
277                                         MSG_WriteShort(msg, org[1]);
278                                 if (bits & E_ORIGIN3)
279                                         MSG_WriteShort(msg, org[2]);
280                         }
281                         else
282                         {
283                                 if (bits & E_ORIGIN1)
284                                         MSG_WriteFloat(msg, org[0]);
285                                 if (bits & E_ORIGIN2)
286                                         MSG_WriteFloat(msg, org[1]);
287                                 if (bits & E_ORIGIN3)
288                                         MSG_WriteFloat(msg, org[2]);
289                         }
290                         if (bits & E_ANGLE1)
291                                 MSG_WriteAngle(msg, ent->angles[0]);
292                         if (bits & E_ANGLE2)
293                                 MSG_WriteAngle(msg, ent->angles[1]);
294                         if (bits & E_ANGLE3)
295                                 MSG_WriteAngle(msg, ent->angles[2]);
296                         if (bits & E_MODEL1)
297                                 MSG_WriteByte(msg, ent->modelindex & 0xFF);
298                         if (bits & E_MODEL2)
299                                 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
300                         if (bits & E_FRAME1)
301                                 MSG_WriteByte(msg, ent->frame & 0xFF);
302                         if (bits & E_FRAME2)
303                                 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
304                         if (bits & E_EFFECTS1)
305                                 MSG_WriteByte(msg, ent->effects & 0xFF);
306                         if (bits & E_EFFECTS2)
307                                 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
308                         if (bits & E_COLORMAP)
309                                 MSG_WriteByte(msg, ent->colormap);
310                         if (bits & E_SKIN)
311                                 MSG_WriteByte(msg, ent->skin);
312                         if (bits & E_ALPHA)
313                                 MSG_WriteByte(msg, ent->alpha);
314                         if (bits & E_SCALE)
315                                 MSG_WriteByte(msg, ent->scale);
316                         if (bits & E_GLOWSIZE)
317                                 MSG_WriteByte(msg, ent->glowsize);
318                         if (bits & E_GLOWCOLOR)
319                                 MSG_WriteByte(msg, ent->glowcolor);
320                         if (bits & E_TAGATTACHMENT)
321                         {
322                                 MSG_WriteShort(msg, ent->tagentity);
323                                 MSG_WriteByte(msg, ent->tagindex);
324                         }
325                 }
326         }
327         for (;onum < o->numentities;onum++)
328         {
329                 // write remove message
330                 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
331         }
332         MSG_WriteShort(msg, 0xFFFF);
333 }
334
335 // (client) reads a frame from network stream
336 static entity_frame_t framedata; // FIXME?
337 void EntityFrame_Read(entity_database_t *d)
338 {
339         int number, removed, bits;
340         entity_frame_t *f = &framedata, *delta = &deltaframe;
341         entity_state_t *e, baseline, *old, *oldend;
342
343         ClearStateToDefault(&baseline);
344
345         EntityFrame_Clear(f, NULL, -1);
346
347         // read the frame header info
348         f->time = cl.mtime[0];
349         number = MSG_ReadLong();
350         f->framenum = MSG_ReadLong();
351         f->eye[0] = MSG_ReadFloat();
352         f->eye[1] = MSG_ReadFloat();
353         f->eye[2] = MSG_ReadFloat();
354         EntityFrame_AckFrame(d, number);
355         EntityFrame_FetchFrame(d, number, delta);
356         old = delta->entitydata;
357         oldend = old + delta->numentities;
358         // read entities until we hit the magic 0xFFFF end tag
359         while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF)
360         {
361                 if (msg_badread)
362                         Host_Error("EntityFrame_Read: read error\n");
363                 removed = number & 0x8000;
364                 number &= 0x7FFF;
365                 if (number >= MAX_EDICTS)
366                         Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
367
368                 // seek to entity, while copying any skipped entities (assume unchanged)
369                 while (old < oldend && old->number < number)
370                 {
371                         if (f->numentities >= MAX_ENTITY_DATABASE)
372                                 Host_Error("EntityFrame_Read: entity list too big\n");
373                         memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
374                         f->entitydata[f->numentities].time = cl.mtime[0];
375                         old++;
376                         f->numentities++;
377                 }
378                 if (removed)
379                 {
380                         if (old < oldend && old->number == number)
381                                 old++;
382                         else
383                                 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
384                 }
385                 else
386                 {
387                         if (f->numentities >= MAX_ENTITY_DATABASE)
388                                 Host_Error("EntityFrame_Read: entity list too big\n");
389
390                         // reserve this slot
391                         e = f->entitydata + f->numentities++;
392
393                         if (old < oldend && old->number == number)
394                         {
395                                 // delta from old entity
396                                 memcpy(e, old++, sizeof(*e));
397                         }
398                         else
399                         {
400                                 // delta from baseline
401                                 memcpy(e, &baseline, sizeof(*e));
402                         }
403
404                         e->active = true;
405                         e->time = cl.mtime[0];
406                         e->number = number;
407
408                         bits = MSG_ReadByte();
409                         if (bits & E_EXTEND1)
410                         {
411                                 bits |= MSG_ReadByte() << 8;
412                                 if (bits & E_EXTEND2)
413                                 {
414                                         bits |= MSG_ReadByte() << 16;
415                                         if (bits & E_EXTEND3)
416                                                 bits |= MSG_ReadByte() << 24;
417                                 }
418                         }
419
420                         if (dpprotocol == DPPROTOCOL_VERSION2)
421                         {
422                                 if (bits & E_ORIGIN1)
423                                         e->origin[0] = (signed short) MSG_ReadShort();
424                                 if (bits & E_ORIGIN2)
425                                         e->origin[1] = (signed short) MSG_ReadShort();
426                                 if (bits & E_ORIGIN3)
427                                         e->origin[2] = (signed short) MSG_ReadShort();
428                         }
429                         else
430                         {
431                                 if (bits & E_FLAGS)
432                                         e->flags = MSG_ReadByte();
433                                 if (e->flags & RENDER_LOWPRECISION || dpprotocol == DPPROTOCOL_VERSION2)
434                                 {
435                                         if (bits & E_ORIGIN1)
436                                                 e->origin[0] = (signed short) MSG_ReadShort();
437                                         if (bits & E_ORIGIN2)
438                                                 e->origin[1] = (signed short) MSG_ReadShort();
439                                         if (bits & E_ORIGIN3)
440                                                 e->origin[2] = (signed short) MSG_ReadShort();
441                                 }
442                                 else
443                                 {
444                                         if (bits & E_ORIGIN1)
445                                                 e->origin[0] = MSG_ReadFloat();
446                                         if (bits & E_ORIGIN2)
447                                                 e->origin[1] = MSG_ReadFloat();
448                                         if (bits & E_ORIGIN3)
449                                                 e->origin[2] = MSG_ReadFloat();
450                                 }
451                         }
452                         if (bits & E_ANGLE1)
453                                 e->angles[0] = MSG_ReadAngle();
454                         if (bits & E_ANGLE2)
455                                 e->angles[1] = MSG_ReadAngle();
456                         if (bits & E_ANGLE3)
457                                 e->angles[2] = MSG_ReadAngle();
458                         if (bits & E_MODEL1)
459                                 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
460                         if (bits & E_MODEL2)
461                                 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
462                         if (bits & E_FRAME1)
463                                 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
464                         if (bits & E_FRAME2)
465                                 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
466                         if (bits & E_EFFECTS1)
467                                 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
468                         if (bits & E_EFFECTS2)
469                                 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
470                         if (bits & E_COLORMAP)
471                                 e->colormap = MSG_ReadByte();
472                         if (bits & E_SKIN)
473                                 e->skin = MSG_ReadByte();
474                         if (bits & E_ALPHA)
475                                 e->alpha = MSG_ReadByte();
476                         if (bits & E_SCALE)
477                                 e->scale = MSG_ReadByte();
478                         if (bits & E_GLOWSIZE)
479                                 e->glowsize = MSG_ReadByte();
480                         if (bits & E_GLOWCOLOR)
481                                 e->glowcolor = MSG_ReadByte();
482                         if (dpprotocol == DPPROTOCOL_VERSION2)
483                                 if (bits & E_FLAGS)
484                                         e->flags = MSG_ReadByte();
485                         if (bits & E_TAGATTACHMENT)
486                         {
487                                 e->tagentity = MSG_ReadShort();
488                                 e->tagindex = MSG_ReadByte();
489                         }
490                 }
491         }
492         while (old < oldend)
493         {
494                 if (f->numentities >= MAX_ENTITY_DATABASE)
495                         Host_Error("EntityFrame_Read: entity list too big\n");
496                 memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
497                 f->entitydata[f->numentities].time = cl.mtime[0];
498                 old++;
499                 f->numentities++;
500         }
501         EntityFrame_AddFrame(d, f);
502 }
503
504
505 // (client) returns the frame number of the most recent frame recieved
506 int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
507 {
508         if (d->numframes)
509                 return d->frames[d->numframes - 1].framenum;
510         else
511                 return -1;
512 }
513