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