moved all type-specific model fields to respective structures (alias, sprite, brush)
[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         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 static entity_frame_t deltaframe; // FIXME?
148 void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, sizebuf_t *msg)
149 {
150         int i, onum, bits, number;
151         float org[3], deltaorg[3];
152         entity_frame_t *o = &deltaframe;
153         entity_state_t *ent, *delta, baseline;
154
155         EntityFrame_AddFrame(d, f);
156
157         ClearStateToDefault(&baseline);
158         EntityFrame_FetchFrame(d, d->ackframe > 0 ? d->ackframe : -1, o);
159         MSG_WriteByte (msg, svc_entities);
160         MSG_WriteLong (msg, o->framenum);
161         MSG_WriteLong (msg, f->framenum);
162         MSG_WriteFloat (msg, f->eye[0]);
163         MSG_WriteFloat (msg, f->eye[1]);
164         MSG_WriteFloat (msg, f->eye[2]);
165
166         onum = 0;
167         for (i = 0;i < f->numentities;i++)
168         {
169                 ent = f->entitydata + i;
170                 number = ent->number;
171                 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
172                 {
173                         // write remove message
174                         MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
175                 }
176                 if (onum < o->numentities && (o->entitydata[onum].number == number))
177                 {
178                         // delta from previous frame
179                         delta = o->entitydata + onum;
180                         // advance to next entity in delta frame
181                         onum++;
182                 }
183                 else
184                 {
185                         // delta from baseline
186                         delta = &baseline;
187                 }
188                 bits = 0;
189                 VectorCopy(ent->origin, org);
190                 VectorCopy(delta->origin, deltaorg);
191                 if (ent->flags & RENDER_LOWPRECISION)
192                 {
193                         if (org[0] > 0)
194                                 org[0] = (int) (org[0] + 0.5f);
195                         else
196                                 org[0] = (int) (org[0] - 0.5f);
197                         if (org[1] > 0)
198                                 org[1] = (int) (org[1] + 0.5f);
199                         else
200                                 org[1] = (int) (org[1] - 0.5f);
201                         if (org[2] > 0)
202                                 org[2] = (int) (org[2] + 0.5f);
203                         else
204                                 org[2] = (int) (org[2] - 0.5f);
205                 }
206                 if (delta->flags & RENDER_LOWPRECISION)
207                 {
208                         if (deltaorg[0] > 0)
209                                 deltaorg[0] = (int) (deltaorg[0] + 0.5f);
210                         else
211                                 deltaorg[0] = (int) (deltaorg[0] - 0.5f);
212                         if (deltaorg[1] > 0)
213                                 deltaorg[1] = (int) (deltaorg[1] + 0.5f);
214                         else
215                                 deltaorg[1] = (int) (deltaorg[1] - 0.5f);
216                         if (deltaorg[2] > 0)
217                                 deltaorg[2] = (int) (deltaorg[2] + 0.5f);
218                         else
219                                 deltaorg[2] = (int) (deltaorg[2] - 0.5f);
220                 }
221                 if (fabs(org[0] - deltaorg[0]) > 0.01f)
222                         bits |= E_ORIGIN1;
223                 if (fabs(org[1] - deltaorg[1]) > 0.01f)
224                         bits |= E_ORIGIN2;
225                 if (fabs(org[2] - deltaorg[2]) > 0.01f)
226                         bits |= E_ORIGIN3;
227                 if ((qbyte) (ent->angles[0] * (256.0f / 360.0f)) != (qbyte) (delta->angles[0] * (256.0f / 360.0f)))
228                         bits |= E_ANGLE1;
229                 if ((qbyte) (ent->angles[1] * (256.0f / 360.0f)) != (qbyte) (delta->angles[1] * (256.0f / 360.0f)))
230                         bits |= E_ANGLE2;
231                 if ((qbyte) (ent->angles[2] * (256.0f / 360.0f)) != (qbyte) (delta->angles[2] * (256.0f / 360.0f)))
232                         bits |= E_ANGLE3;
233                 if ((ent->modelindex ^ delta->modelindex) & 0x00FF)
234                         bits |= E_MODEL1;
235                 if ((ent->modelindex ^ delta->modelindex) & 0xFF00)
236                         bits |= E_MODEL2;
237                 if ((ent->frame ^ delta->frame) & 0x00FF)
238                         bits |= E_FRAME1;
239                 if ((ent->frame ^ delta->frame) & 0xFF00)
240                         bits |= E_FRAME2;
241                 if ((ent->effects ^ delta->effects) & 0x00FF)
242                         bits |= E_EFFECTS1;
243                 if ((ent->effects ^ delta->effects) & 0xFF00)
244                         bits |= E_EFFECTS2;
245                 if (ent->colormap != delta->colormap)
246                         bits |= E_COLORMAP;
247                 if (ent->skin != delta->skin)
248                         bits |= E_SKIN;
249                 if (ent->alpha != delta->alpha)
250                         bits |= E_ALPHA;
251                 if (ent->scale != delta->scale)
252                         bits |= E_SCALE;
253                 if (ent->glowsize != delta->glowsize)
254                         bits |= E_GLOWSIZE;
255                 if (ent->glowcolor != delta->glowcolor)
256                         bits |= E_GLOWCOLOR;
257                 if (ent->flags != delta->flags)
258                         bits |= E_FLAGS;
259
260                 if (bits) // don't send anything if it hasn't changed
261                 {
262                         if (bits & 0xFF000000)
263                                 bits |= E_EXTEND3;
264                         if (bits & 0x00FF0000)
265                                 bits |= E_EXTEND2;
266                         if (bits & 0x0000FF00)
267                                 bits |= E_EXTEND1;
268
269                         MSG_WriteShort(msg, number);
270                         MSG_WriteByte(msg, bits & 0xFF);
271                         if (bits & E_EXTEND1)
272                         {
273                                 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
274                                 if (bits & E_EXTEND2)
275                                 {
276                                         MSG_WriteByte(msg, (bits >> 16) & 0xFF);
277                                         if (bits & E_EXTEND3)
278                                                 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
279                                 }
280                         }
281                         // LordHavoc: have to write flags first, as they can modify protocol
282                         if (bits & E_FLAGS)
283                                 MSG_WriteByte(msg, ent->flags);
284                         if (ent->flags & RENDER_LOWPRECISION)
285                         {
286                                 if (bits & E_ORIGIN1)
287                                         MSG_WriteShort(msg, org[0]);
288                                 if (bits & E_ORIGIN2)
289                                         MSG_WriteShort(msg, org[1]);
290                                 if (bits & E_ORIGIN3)
291                                         MSG_WriteShort(msg, org[2]);
292                         }
293                         else
294                         {
295                                 if (bits & E_ORIGIN1)
296                                         MSG_WriteFloat(msg, org[0]);
297                                 if (bits & E_ORIGIN2)
298                                         MSG_WriteFloat(msg, org[1]);
299                                 if (bits & E_ORIGIN3)
300                                         MSG_WriteFloat(msg, org[2]);
301                         }
302                         if (bits & E_ANGLE1)
303                                 MSG_WriteAngle(msg, ent->angles[0]);
304                         if (bits & E_ANGLE2)
305                                 MSG_WriteAngle(msg, ent->angles[1]);
306                         if (bits & E_ANGLE3)
307                                 MSG_WriteAngle(msg, ent->angles[2]);
308                         if (bits & E_MODEL1)
309                                 MSG_WriteByte(msg, ent->modelindex & 0xFF);
310                         if (bits & E_MODEL2)
311                                 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
312                         if (bits & E_FRAME1)
313                                 MSG_WriteByte(msg, ent->frame & 0xFF);
314                         if (bits & E_FRAME2)
315                                 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
316                         if (bits & E_EFFECTS1)
317                                 MSG_WriteByte(msg, ent->effects & 0xFF);
318                         if (bits & E_EFFECTS2)
319                                 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
320                         if (bits & E_COLORMAP)
321                                 MSG_WriteByte(msg, ent->colormap);
322                         if (bits & E_SKIN)
323                                 MSG_WriteByte(msg, ent->skin);
324                         if (bits & E_ALPHA)
325                                 MSG_WriteByte(msg, ent->alpha);
326                         if (bits & E_SCALE)
327                                 MSG_WriteByte(msg, ent->scale);
328                         if (bits & E_GLOWSIZE)
329                                 MSG_WriteByte(msg, ent->glowsize);
330                         if (bits & E_GLOWCOLOR)
331                                 MSG_WriteByte(msg, ent->glowcolor);
332                 }
333         }
334         for (;onum < o->numentities;onum++)
335         {
336                 // write remove message
337                 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
338         }
339         MSG_WriteShort(msg, 0xFFFF);
340 }
341
342 // (client) reads a frame from network stream
343 static entity_frame_t framedata; // FIXME?
344 void EntityFrame_Read(entity_database_t *d)
345 {
346         int number, removed, bits;
347         entity_frame_t *f = &framedata, *delta = &deltaframe;
348         entity_state_t *e, baseline, *old, *oldend;
349
350         ClearStateToDefault(&baseline);
351
352         EntityFrame_Clear(f, NULL);
353
354         // read the frame header info
355         f->time = cl.mtime[0];
356         number = MSG_ReadLong();
357         f->framenum = MSG_ReadLong();
358         f->eye[0] = MSG_ReadFloat();
359         f->eye[1] = MSG_ReadFloat();
360         f->eye[2] = MSG_ReadFloat();
361         EntityFrame_AckFrame(d, number);
362         EntityFrame_FetchFrame(d, number, delta);
363         old = delta->entitydata;
364         oldend = old + delta->numentities;
365         // read entities until we hit the magic 0xFFFF end tag
366         while ((number = (unsigned short) MSG_ReadShort()) != 0xFFFF)
367         {
368                 if (msg_badread)
369                         Host_Error("EntityFrame_Read: read error\n");
370                 removed = number & 0x8000;
371                 number &= 0x7FFF;
372                 if (number >= MAX_EDICTS)
373                         Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
374
375                 // seek to entity, while copying any skipped entities (assume unchanged)
376                 while (old < oldend && old->number < number)
377                 {
378                         if (f->numentities >= MAX_ENTITY_DATABASE)
379                                 Host_Error("EntityFrame_Read: entity list too big\n");
380                         memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
381                         f->entitydata[f->numentities].time = cl.mtime[0];
382                         old++;
383                         f->numentities++;
384                 }
385                 if (removed)
386                 {
387                         if (old < oldend && old->number == number)
388                                 old++;
389                         else
390                                 Con_Printf("EntityFrame_Read: REMOVE on unused entity %i\n", number);
391                 }
392                 else
393                 {
394                         if (f->numentities >= MAX_ENTITY_DATABASE)
395                                 Host_Error("EntityFrame_Read: entity list too big\n");
396
397                         // reserve this slot
398                         e = f->entitydata + f->numentities++;
399
400                         if (old < oldend && old->number == number)
401                         {
402                                 // delta from old entity
403                                 memcpy(e, old++, sizeof(*e));
404                         }
405                         else
406                         {
407                                 // delta from baseline
408                                 memcpy(e, &baseline, sizeof(*e));
409                         }
410
411                         e->active = true;
412                         e->time = cl.mtime[0];
413                         e->number = number;
414
415                         bits = MSG_ReadByte();
416                         if (bits & E_EXTEND1)
417                         {
418                                 bits |= MSG_ReadByte() << 8;
419                                 if (bits & E_EXTEND2)
420                                 {
421                                         bits |= MSG_ReadByte() << 16;
422                                         if (bits & E_EXTEND3)
423                                                 bits |= MSG_ReadByte() << 24;
424                                 }
425                         }
426
427                         if (dpprotocol == DPPROTOCOL_VERSION2)
428                         {
429                                 if (bits & E_ORIGIN1)
430                                         e->origin[0] = (signed short) MSG_ReadShort();
431                                 if (bits & E_ORIGIN2)
432                                         e->origin[1] = (signed short) MSG_ReadShort();
433                                 if (bits & E_ORIGIN3)
434                                         e->origin[2] = (signed short) MSG_ReadShort();
435                         }
436                         else
437                         {
438                                 if (bits & E_FLAGS)
439                                         e->flags = MSG_ReadByte();
440                                 if (e->flags & RENDER_LOWPRECISION || dpprotocol == DPPROTOCOL_VERSION2)
441                                 {
442                                         if (bits & E_ORIGIN1)
443                                                 e->origin[0] = (signed short) MSG_ReadShort();
444                                         if (bits & E_ORIGIN2)
445                                                 e->origin[1] = (signed short) MSG_ReadShort();
446                                         if (bits & E_ORIGIN3)
447                                                 e->origin[2] = (signed short) MSG_ReadShort();
448                                 }
449                                 else
450                                 {
451                                         if (bits & E_ORIGIN1)
452                                                 e->origin[0] = MSG_ReadFloat();
453                                         if (bits & E_ORIGIN2)
454                                                 e->origin[1] = MSG_ReadFloat();
455                                         if (bits & E_ORIGIN3)
456                                                 e->origin[2] = MSG_ReadFloat();
457                                 }
458                         }
459                         if (bits & E_ANGLE1)
460                                 e->angles[0] = MSG_ReadAngle();
461                         if (bits & E_ANGLE2)
462                                 e->angles[1] = MSG_ReadAngle();
463                         if (bits & E_ANGLE3)
464                                 e->angles[2] = MSG_ReadAngle();
465                         if (bits & E_MODEL1)
466                                 e->modelindex = (e->modelindex & 0xFF00) | (unsigned int) MSG_ReadByte();
467                         if (bits & E_MODEL2)
468                                 e->modelindex = (e->modelindex & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
469                         if (bits & E_FRAME1)
470                                 e->frame = (e->frame & 0xFF00) | (unsigned int) MSG_ReadByte();
471                         if (bits & E_FRAME2)
472                                 e->frame = (e->frame & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
473                         if (bits & E_EFFECTS1)
474                                 e->effects = (e->effects & 0xFF00) | (unsigned int) MSG_ReadByte();
475                         if (bits & E_EFFECTS2)
476                                 e->effects = (e->effects & 0x00FF) | ((unsigned int) MSG_ReadByte() << 8);
477                         if (bits & E_COLORMAP)
478                                 e->colormap = MSG_ReadByte();
479                         if (bits & E_SKIN)
480                                 e->skin = MSG_ReadByte();
481                         if (bits & E_ALPHA)
482                                 e->alpha = MSG_ReadByte();
483                         if (bits & E_SCALE)
484                                 e->scale = MSG_ReadByte();
485                         if (bits & E_GLOWSIZE)
486                                 e->glowsize = MSG_ReadByte();
487                         if (bits & E_GLOWCOLOR)
488                                 e->glowcolor = MSG_ReadByte();
489                         if (dpprotocol == DPPROTOCOL_VERSION2)
490                                 if (bits & E_FLAGS)
491                                         e->flags = MSG_ReadByte();
492                 }
493         }
494         while (old < oldend)
495         {
496                 if (f->numentities >= MAX_ENTITY_DATABASE)
497                         Host_Error("EntityFrame_Read: entity list too big\n");
498                 memcpy(f->entitydata + f->numentities, old, sizeof(entity_state_t));
499                 f->entitydata[f->numentities].time = cl.mtime[0];
500                 old++;
501                 f->numentities++;
502         }
503         EntityFrame_AddFrame(d, f);
504 }
505
506
507 // (client) returns the frame number of the most recent frame recieved
508 int EntityFrame_MostRecentlyRecievedFrameNum(entity_database_t *d)
509 {
510         if (d->numframes)
511                 return d->frames[d->numframes - 1].framenum;
512         else
513                 return -1;
514 }
515