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