]> icculus.org git repositories - divverent/darkplaces.git/blob - protocol.c
added note not to use athlon optimizations in current gcc versions
[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 ((int) ent->origin[0] != (int) delta->origin[0])
179                         bits |= E_ORIGIN1;
180                 if ((int) ent->origin[1] != (int) delta->origin[1])
181                         bits |= E_ORIGIN2;
182                 if ((int) ent->origin[2] != (int) delta->origin[2])
183                         bits |= E_ORIGIN3;
184                 if ((qbyte) (ent->angles[0] * (256.0f / 360.0f)) != (qbyte) (delta->angles[0] * (256.0f / 360.0f)))
185                         bits |= E_ANGLE1;
186                 if ((qbyte) (ent->angles[1] * (256.0f / 360.0f)) != (qbyte) (delta->angles[1] * (256.0f / 360.0f)))
187                         bits |= E_ANGLE2;
188                 if ((qbyte) (ent->angles[2] * (256.0f / 360.0f)) != (qbyte) (delta->angles[2] * (256.0f / 360.0f)))
189                         bits |= E_ANGLE3;
190                 if ((ent->modelindex ^ delta->modelindex) & 0x00FF)
191                         bits |= E_MODEL1;
192                 if ((ent->modelindex ^ delta->modelindex) & 0xFF00)
193                         bits |= E_MODEL2;
194                 if ((ent->frame ^ delta->frame) & 0x00FF)
195                         bits |= E_FRAME1;
196                 if ((ent->frame ^ delta->frame) & 0xFF00)
197                         bits |= E_FRAME2;
198                 if ((ent->effects ^ delta->effects) & 0x00FF)
199                         bits |= E_EFFECTS1;
200                 if ((ent->effects ^ delta->effects) & 0xFF00)
201                         bits |= E_EFFECTS2;
202                 if (ent->colormap != delta->colormap)
203                         bits |= E_COLORMAP;
204                 if (ent->skin != delta->skin)
205                         bits |= E_SKIN;
206                 if (ent->alpha != delta->alpha)
207                         bits |= E_ALPHA;
208                 if (ent->scale != delta->scale)
209                         bits |= E_SCALE;
210                 if (ent->glowsize != delta->glowsize)
211                         bits |= E_GLOWSIZE;
212                 if (ent->glowcolor != delta->glowcolor)
213                         bits |= E_GLOWCOLOR;
214                 if (ent->flags != delta->flags)
215                         bits |= E_FLAGS;
216
217                 if (bits) // don't send anything if it hasn't changed
218                 {
219                         if (bits & 0xFF000000)
220                                 bits |= E_EXTEND3;
221                         if (bits & 0x00FF0000)
222                                 bits |= E_EXTEND2;
223                         if (bits & 0x0000FF00)
224                                 bits |= E_EXTEND1;
225
226                         MSG_WriteShort(msg, number);
227                         MSG_WriteByte(msg, bits & 0xFF);
228                         if (bits & E_EXTEND1)
229                         {
230                                 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
231                                 if (bits & E_EXTEND2)
232                                 {
233                                         MSG_WriteByte(msg, (bits >> 16) & 0xFF);
234                                         if (bits & E_EXTEND3)
235                                                 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
236                                 }
237                         }
238                         if (bits & E_ORIGIN1)
239                                 MSG_WriteShort(msg, ent->origin[0]);
240                         if (bits & E_ORIGIN2)
241                                 MSG_WriteShort(msg, ent->origin[1]);
242                         if (bits & E_ORIGIN3)
243                                 MSG_WriteShort(msg, ent->origin[2]);
244                         if (bits & E_ANGLE1)
245                                 MSG_WriteAngle(msg, ent->angles[0]);
246                         if (bits & E_ANGLE2)
247                                 MSG_WriteAngle(msg, ent->angles[1]);
248                         if (bits & E_ANGLE3)
249                                 MSG_WriteAngle(msg, ent->angles[2]);
250                         if (bits & E_MODEL1)
251                                 MSG_WriteByte(msg, ent->modelindex & 0x00FF);
252                         if (bits & E_MODEL2)
253                                 MSG_WriteByte(msg, ent->modelindex & 0xFF00);
254                         if (bits & E_FRAME1)
255                                 MSG_WriteByte(msg, ent->frame & 0x00FF);
256                         if (bits & E_FRAME2)
257                                 MSG_WriteByte(msg, ent->frame & 0xFF00);
258                         if (bits & E_EFFECTS1)
259                                 MSG_WriteByte(msg, ent->effects & 0x00FF);
260                         if (bits & E_EFFECTS2)
261                                 MSG_WriteByte(msg, ent->effects & 0xFF00);
262                         if (bits & E_COLORMAP)
263                                 MSG_WriteByte(msg, ent->colormap);
264                         if (bits & E_SKIN)
265                                 MSG_WriteByte(msg, ent->skin);
266                         if (bits & E_ALPHA)
267                                 MSG_WriteByte(msg, ent->alpha);
268                         if (bits & E_SCALE)
269                                 MSG_WriteByte(msg, ent->scale);
270                         if (bits & E_GLOWSIZE)
271                                 MSG_WriteByte(msg, ent->glowsize);
272                         if (bits & E_GLOWCOLOR)
273                                 MSG_WriteByte(msg, ent->glowcolor);
274                         if (bits & E_FLAGS)
275                                 MSG_WriteByte(msg, ent->flags);
276                 }
277         }
278         for (;onum < o->numentities;onum++)
279         {
280                 // write remove message
281                 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
282         }
283         MSG_WriteShort(msg, 0xFFFF);
284 }
285
286 // (client) reads a frame from network stream
287 void EntityFrame_Read(entity_database_t *d)
288 {
289         int number, removed, bits;
290         entity_frame_t framedata, *f = &framedata, deltaframedata, *delta = &deltaframedata;
291         entity_state_t *e, baseline, *old, *oldend;
292
293         ClearStateToDefault(&baseline);
294         memset(f, 0, sizeof(*f));
295         // read the frame header info
296         f->time = cl.mtime[0];
297         number = MSG_ReadLong();
298         f->framenum = MSG_ReadLong();
299         f->eye[0] = MSG_ReadFloat();
300         f->eye[1] = MSG_ReadFloat();
301         f->eye[2] = MSG_ReadFloat();
302         EntityFrame_AckFrame(d, number);
303         EntityFrame_FetchFrame(d, number, delta);
304         old = delta->entitydata;
305         oldend = old + delta->numentities;
306         // read entities until we hit the magic 0xFFFF end tag
307         while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF)
308         {
309                 if (msg_badread)
310                         Host_Error("EntityFrame_Read: read error\n");
311                 removed = number & 0x8000;
312                 number &= 0x7FFF;
313                 if (number >= MAX_EDICTS)
314                         Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
315
316                 // seek to entity, while copying any skipped entities (assume unchanged)
317                 while (old < oldend && old->number < number)
318                 {
319                         if (f->numentities >= MAX_ENTITY_DATABASE)
320                                 Host_Error("EntityFrame_Read: entity list too big\n");
321                         memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
322                         f->entitydata[f->numentities].time = cl.mtime[0];
323                         old++;
324                         f->numentities++;
325                 }
326                 if (removed)
327                 {
328                         if (old < oldend && old->number == number)
329                                 old++;
330                         else
331                                 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
332                 }
333                 else
334                 {
335                         if (f->numentities >= MAX_ENTITY_DATABASE)
336                                 Host_Error("EntityFrame_Read: entity list too big\n");
337
338                         // reserve this slot
339                         e = f->entitydata + f->numentities++;
340
341                         if (old < oldend && old->number == number)
342                         {
343                                 // delta from old entity
344                                 memcpy(e, old++, sizeof(*e));
345                         }
346                         else
347                         {
348                                 // delta from baseline
349                                 memcpy(e, &baseline, sizeof(*e));
350                         }
351
352                         e->active = true;
353                         e->time = cl.mtime[0];
354                         e->number = number;
355
356                         bits = MSG_ReadByte();
357                         if (bits & E_EXTEND1)
358                         {
359                                 bits |= MSG_ReadByte() << 8;
360                                 if (bits & E_EXTEND2)
361                                 {
362                                         bits |= MSG_ReadByte() << 16;
363                                         if (bits & E_EXTEND3)
364                                                 bits |= MSG_ReadByte() << 24;
365                                 }
366                         }
367
368                         if (bits & E_ORIGIN1)
369                                 e->origin[0] = (signed short) MSG_ReadShort();
370                         if (bits & E_ORIGIN2)
371                                 e->origin[1] = (signed short) MSG_ReadShort();
372                         if (bits & E_ORIGIN3)
373                                 e->origin[2] = (signed short) MSG_ReadShort();
374                         if (bits & E_ANGLE1)
375                                 e->angles[0] = MSG_ReadAngle();
376                         if (bits & E_ANGLE2)
377                                 e->angles[1] = MSG_ReadAngle();
378                         if (bits & E_ANGLE3)
379                                 e->angles[2] = MSG_ReadAngle();
380                         if (bits & E_MODEL1)
381                                 e->modelindex = (e->modelindex & 0xFF00) | MSG_ReadByte();
382                         if (bits & E_MODEL2)
383                                 e->modelindex = (e->modelindex & 0x00FF) | (MSG_ReadByte() << 8);
384                         if (bits & E_FRAME1)
385                                 e->frame = (e->frame & 0xFF00) | MSG_ReadByte();
386                         if (bits & E_FRAME2)
387                                 e->frame = (e->frame & 0x00FF) | (MSG_ReadByte() << 8);
388                         if (bits & E_EFFECTS1)
389                                 e->effects = (e->effects & 0xFF00) | MSG_ReadByte();
390                         if (bits & E_EFFECTS2)
391                                 e->effects = (e->effects & 0x00FF) | (MSG_ReadByte() << 8);
392                         if (bits & E_COLORMAP)
393                                 e->colormap = MSG_ReadByte();
394                         if (bits & E_SKIN)
395                                 e->skin = MSG_ReadByte();
396                         if (bits & E_ALPHA)
397                                 e->alpha = MSG_ReadByte();
398                         if (bits & E_SCALE)
399                                 e->scale = MSG_ReadByte();
400                         if (bits & E_GLOWSIZE)
401                                 e->glowsize = MSG_ReadByte();
402                         if (bits & E_GLOWCOLOR)
403                                 e->glowcolor = MSG_ReadByte();
404                         if (bits & E_FLAGS)
405                                 e->flags = MSG_ReadByte();
406                 }
407         }
408         while (old < oldend)
409         {
410                 if (f->numentities >= MAX_ENTITY_DATABASE)
411                         Host_Error("EntityFrame_Read: entity list too big\n");
412                 memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
413                 f->entitydata[f->numentities].time = cl.mtime[0];
414                 old++;
415                 f->numentities++;
416         }
417         EntityFrame_AddFrame(d, f);
418 }
419
420 /*
421 // (client) reads (and interpolates) the eye location from the database,
422 // given a current time
423 int EntityFrame_FetchEye(entity_database_t *d, vec3_t eye, double time)
424 {
425         float frac;
426         if (d->numframes == 0)
427                 return false;
428 //              Host_Error("EntityFrame_FetchEye: no frames\n");
429         if (d->numframes > 1 && d->frames[d->numframes - 2].time != d->frames[d->numframes - 1].time)
430         {
431                 frac = (time - d->frames[d->numframes - 2].time) / (d->frames[d->numframes - 1].time - d->frames[d->numframes - 2].time);
432                 frac = bound(0, frac, 1);
433                 VectorSubtract(d->frames[d->numframes - 2].eye, d->frames[d->numframes - 1].eye, eye);
434                 VectorMA(d->frames[d->numframes - 2].eye, frac, eye, eye);
435         }
436         else
437                 VectorCopy(d->frames[0].eye, eye);
438         return true;
439 }
440
441 // (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
442 int EntityFrame_FetchEntityByIndex(entity_frame_t *f, entity_state_t *e, int index)
443 {
444         if (index < 0 || index >= f->numentities)
445                 return false;
446         memcpy(e, f->entitydata + index, sizeof(*e));
447         return true;
448 }
449
450 int EntityFrame_FetchEntityByNumber(entity_frame_t *f, entity_state_t *e, int number)
451 {
452         int i;
453         for (i = 0;i < f->numentities;i++)
454         {
455                 if (f->entitydata[i].number == number)
456                 {
457                         memcpy(e, f->entitydata + i, sizeof(*e));
458                         return true;
459                 }
460         }
461         ClearStateToDefault(e);
462         return false;
463 }
464 */
465
466 // (client) returns the frame number of the most recent frame recieved
467 int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
468 {
469         if (d->numframes)
470                 return d->frames[d->numframes - 1].framenum;
471         else
472                 return -1;
473 }