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