new entity protocol nearly complete, but still unused
[divverent/darkplaces.git] / protocol.c
1
2 #include "quakedef.h"
3
4 void ClearStateToDefault(entity_state_t *s)
5 {
6         s->time = 0;
7         VectorClear(s->origin);
8         VectorClear(s->angles);
9         s->effects = 0;
10         s->modelindex = 0;
11         s->frame = 0;
12         s->colormap = 0;
13         s->skin = 0;
14         s->alpha = 255;
15         s->scale = 16;
16         s->glowsize = 0;
17         s->glowcolor = 254;
18         s->flags = 0;
19         s->active = 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         d->ackframe = -1;
27 }
28
29 // (server and client) removes frames older than 'frame' from database
30 void EntityFrame_AckFrame(entity_database_t *d, int frame)
31 {
32         int i;
33         for (i = 0;i < d->numframes && d->frames[i].framenum >= frame;i++);
34         // ignore outdated frame acks (out of order packets)
35         if (i == 0)
36                 return;
37         d->ackframe = frame;
38         d->numframes -= i;
39         // if some queue is left, slide it down to beginning of array
40         if (d->numframes)
41                 memcpy(&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 void EntityFrame_FetchFrame(entity_database_t *d, int framenum, entity_frame_t *f)
64 {
65         int i, n;
66         memset(f, 0, sizeof(*f));
67         for (i = 0;i < d->numframes && d->frames[i].framenum < framenum;i++);
68         if (framenum == d->frames[i].framenum)
69         {
70                 f->framenum = framenum;
71                 f->numentities = d->frames[i].endentity - d->frames[i].firstentity;
72                 n = MAX_ENTITY_DATABASE - (d->frames[i].firstentity % MAX_ENTITY_DATABASE);
73                 if (n > f->numentities)
74                         n = f->numentities;
75                 memcpy(f->entitydata, d->entitydata + d->frames[i].firstentity % MAX_ENTITY_DATABASE, sizeof(*f->entitydata) * n);
76                 if (f->numentities > n)
77                         memcpy(f->entitydata + n, d->entitydata, sizeof(*f->entitydata) * (f->numentities - n));
78         }
79         else
80                 f->framenum = -1;
81 }
82
83 // (server and client) adds a entity_frame to the database, for future reference
84 void EntityFrame_AddFrame(entity_database_t *d, entity_frame_t *f)
85 {
86         int n, e;
87         entity_frameinfo_t *info;
88         // figure out how many entity slots are used already
89         if (d->numframes)
90         {
91                 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
92                 if (n + f->numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
93                 {
94                         // ran out of room, dump database
95                         EntityFrame_ClearDatabase(d);
96                 }
97         }
98
99         info = &d->frames[d->numframes];
100         info->framenum = f->framenum;
101         // if database still has frames after that...
102         if (d->numframes)
103                 info->firstentity = d->frames[d->numframes - 1].endentity;
104         else
105                 info->firstentity = 0;
106         info->endentity = info->firstentity + f->numentities;
107
108         n = info->firstentity % MAX_ENTITY_DATABASE;
109         e = MAX_ENTITY_DATABASE - n;
110         if (e > f->numentities)
111                 e = f->numentities;
112         memcpy(d->entitydata + n, f->entitydata, sizeof(entity_state_t) * e);
113         if (f->numentities > e)
114                 memcpy(d->entitydata, f->entitydata + e, sizeof(entity_state_t) * (f->numentities - e));
115 }
116
117 // (server) writes a frame to network stream
118 void EntityFrame_Write(entity_database_t *d, entity_frame_t *f, int newframe, sizebuf_t *msg)
119 {
120         int i, onum, bits, number;
121         entity_frame_t deltaframe, *o = &deltaframe;
122         entity_state_t *ent, *delta, baseline;
123
124         EntityFrame_AddFrame(d, f);
125
126         ClearStateToDefault(&baseline);
127         EntityFrame_FetchFrame(d, d->ackframe, o);
128         MSG_WriteByte (msg, svc_entities);
129         MSG_WriteLong (msg, o->framenum);
130         MSG_WriteLong (msg, newframe);
131         MSG_WriteFloat (msg, f->eye[0]);
132         MSG_WriteFloat (msg, f->eye[1]);
133         MSG_WriteFloat (msg, f->eye[2]);
134
135         onum = 0;
136         for (i = 0;i < f->numentities;i++)
137         {
138                 ent = f->entitydata + i;
139                 number = ent->number;
140                 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
141                 {
142                         // write remove message
143                         MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
144                 }
145                 if (onum < o->numentities && (o->entitydata[onum].number == number))
146                 {
147                         // delta from previous frame
148                         delta = o->entitydata + onum;
149                         // advance to next entity in delta frame
150                         onum++;
151                 }
152                 else
153                 {
154                         // delta from baseline
155                         delta = &baseline;
156                 }
157                 bits = 0;
158                 if (ent->origin[0] != delta->origin[0])
159                         bits |= E_ORIGIN1;
160                 if (ent->origin[1] != delta->origin[1])
161                         bits |= E_ORIGIN2;
162                 if (ent->origin[2] != delta->origin[2])
163                         bits |= E_ORIGIN3;
164                 if (ent->angles[0] != delta->angles[0])
165                         bits |= E_ANGLE1;
166                 if (ent->angles[1] != delta->angles[1])
167                         bits |= E_ANGLE2;
168                 if (ent->angles[2] != delta->angles[2])
169                         bits |= E_ANGLE3;
170                 if ((ent->modelindex ^ delta->modelindex) & 0x00FF)
171                         bits |= E_MODEL1;
172                 if ((ent->modelindex ^ delta->modelindex) & 0xFF00)
173                         bits |= E_MODEL2;
174                 if ((ent->frame ^ delta->frame) & 0x00FF)
175                         bits |= E_FRAME1;
176                 if ((ent->frame ^ delta->frame) & 0xFF00)
177                         bits |= E_FRAME2;
178                 if ((ent->effects ^ delta->effects) & 0x00FF)
179                         bits |= E_EFFECTS1;
180                 if ((ent->effects ^ delta->effects) & 0xFF00)
181                         bits |= E_EFFECTS2;
182                 if (ent->colormap != delta->colormap)
183                         bits |= E_COLORMAP;
184                 if (ent->skin != delta->skin)
185                         bits |= E_SKIN;
186                 if (ent->alpha != delta->alpha)
187                         bits |= E_ALPHA;
188                 if (ent->scale != delta->scale)
189                         bits |= E_SCALE;
190                 if (ent->glowsize != delta->glowsize)
191                         bits |= E_GLOWSIZE;
192                 if (ent->glowcolor != delta->glowcolor)
193                         bits |= E_GLOWCOLOR;
194                 if (ent->flags != delta->flags)
195                         bits |= E_FLAGS;
196
197                 if (bits) // don't send anything if it hasn't changed
198                 {
199                         if (bits & 0xFF000000)
200                                 bits |= E_EXTEND3;
201                         if (bits & 0x00FF0000)
202                                 bits |= E_EXTEND2;
203                         if (bits & 0x0000FF00)
204                                 bits |= E_EXTEND1;
205
206                         MSG_WriteShort(msg, number);
207                         MSG_WriteByte(msg, bits & 0xFF);
208                         if (bits & E_EXTEND1)
209                         {
210                                 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
211                                 if (bits & E_EXTEND2)
212                                 {
213                                         MSG_WriteByte(msg, (bits >> 16) & 0xFF);
214                                         if (bits & E_EXTEND3)
215                                                 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
216                                 }
217                         }
218                         if (bits & E_ORIGIN1)
219                                 MSG_WriteFloat(msg, ent->origin[0]);
220                         if (bits & E_ORIGIN2)
221                                 MSG_WriteFloat(msg, ent->origin[1]);
222                         if (bits & E_ORIGIN3)
223                                 MSG_WriteFloat(msg, ent->origin[2]);
224                         if (bits & E_ANGLE1)
225                                 MSG_WriteAngle(msg, ent->angles[0]);
226                         if (bits & E_ANGLE2)
227                                 MSG_WriteAngle(msg, ent->angles[1]);
228                         if (bits & E_ANGLE3)
229                                 MSG_WriteAngle(msg, ent->angles[2]);
230                         if (bits & E_MODEL1)
231                                 MSG_WriteByte(msg, ent->modelindex & 0x00FF);
232                         if (bits & E_MODEL2)
233                                 MSG_WriteByte(msg, ent->modelindex & 0xFF00);
234                         if (bits & E_FRAME1)
235                                 MSG_WriteByte(msg, ent->frame & 0x00FF);
236                         if (bits & E_FRAME2)
237                                 MSG_WriteByte(msg, ent->frame & 0xFF00);
238                         if (bits & E_EFFECTS1)
239                                 MSG_WriteByte(msg, ent->effects & 0x00FF);
240                         if (bits & E_EFFECTS2)
241                                 MSG_WriteByte(msg, ent->effects & 0xFF00);
242                         if (bits & E_COLORMAP)
243                                 MSG_WriteByte(msg, ent->colormap);
244                         if (bits & E_SKIN)
245                                 MSG_WriteByte(msg, ent->skin);
246                         if (bits & E_ALPHA)
247                                 MSG_WriteByte(msg, ent->alpha);
248                         if (bits & E_SCALE)
249                                 MSG_WriteByte(msg, ent->scale);
250                         if (bits & E_GLOWSIZE)
251                                 MSG_WriteByte(msg, ent->glowsize);
252                         if (bits & E_GLOWCOLOR)
253                                 MSG_WriteByte(msg, ent->glowcolor);
254                         if (bits & E_FLAGS)
255                                 MSG_WriteByte(msg, ent->flags);
256                 }
257         }
258         for (;onum < o->numentities;onum++)
259         {
260                 // write remove message
261                 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
262         }
263         MSG_WriteShort(msg, 0xFFFF);
264 }
265
266 // (client) reads a frame from network stream
267 void EntityFrame_Read(entity_database_t *d)
268 {
269         int newframenum, deltaframenum, onum, number, removed, bits;
270         entity_frame_t framedata, *f = &framedata, deltaframedata, *delta = &deltaframedata;
271         entity_state_t *e, baseline;
272
273         ClearStateToDefault(&baseline);
274         memset(f, 0, sizeof(*f));
275         // read the frame header info
276         f->time = cl.mtime[0];
277         deltaframenum = MSG_ReadLong();
278         newframenum = MSG_ReadLong();
279         f->eye[0] = MSG_ReadFloat();
280         f->eye[1] = MSG_ReadFloat();
281         f->eye[2] = MSG_ReadFloat();
282         EntityFrame_AckFrame(d, deltaframenum);
283         EntityFrame_FetchFrame(d, deltaframenum, delta);
284         f->framenum = newframenum;
285         onum = 0;
286         f->numentities = 0;
287         // read entities until we hit the magic 0xFFFF end tag
288         while ((number = MSG_ReadShort()) != 0xFFFF)
289         {
290                 if (msg_badread)
291                         Host_Error("EntityFrame_Read: read error\n");
292                 removed = number & 0x8000;
293                 number &= 0x7FFF;
294                 if (number >= MAX_EDICTS)
295                         Host_Error("EntityFrame_Read: number (%i) >= MAX_EDICTS (%i)\n", number, MAX_EDICTS);
296
297                 // seek to entity, while copying any skipped entities (assume unchanged)
298                 while (onum < delta->numentities && delta->entitydata[onum].number < number)
299                 {
300                         if (f->numentities >= MAX_ENTITY_DATABASE)
301                                 Host_Error("EntityFrame_Read: entity list too big\n");
302                         memcpy(f->entitydata + f->numentities, delta->entitydata + onum, sizeof(entity_state_t));
303                         onum++;
304                         f->numentities++;
305                 }
306                 if (removed)
307                 {
308                         if (onum < delta->numentities && delta->entitydata[onum].number == number)
309                                 onum++;
310                         else
311                                 Con_Printf("EntityFrame_Read: REMOVE on unused entity!\n");
312                 }
313                 else
314                 {
315                         if (f->numentities >= MAX_ENTITY_DATABASE)
316                                 Host_Error("EntityFrame_Read: entity list too big\n");
317                         if (onum < delta->numentities && delta->entitydata[onum].number == number)
318                         {
319                                 // delta from old entity
320                                 memcpy(f->entitydata + f->numentities, delta->entitydata + onum++, sizeof(*e));
321                         }
322                         else
323                         {
324                                 // delta from baseline
325                                 memcpy(f->entitydata + f->numentities, &baseline, sizeof(*e));
326                         }
327
328                         // reserve this slot
329                         e = f->entitydata + f->numentities++;
330
331                         e->active = true;
332                         e->time = cl.mtime[0];
333
334                         bits = MSG_ReadByte();
335                         if (bits & E_EXTEND1)
336                         {
337                                 bits |= MSG_ReadByte() << 8;
338                                 if (bits & E_EXTEND2)
339                                 {
340                                         bits |= MSG_ReadByte() << 16;
341                                         if (bits & E_EXTEND3)
342                                                 bits |= MSG_ReadByte() << 24;
343                                 }
344                         }
345
346                         if (bits & E_ORIGIN1)
347                                 e->origin[0] = MSG_ReadFloat();
348                         if (bits & E_ORIGIN2)
349                                 e->origin[1] = MSG_ReadFloat();
350                         if (bits & E_ORIGIN3)
351                                 e->origin[2] = MSG_ReadFloat();
352                         if (bits & E_ANGLE1)
353                                 e->angles[0] = MSG_ReadAngle();
354                         if (bits & E_ANGLE2)
355                                 e->angles[1] = MSG_ReadAngle();
356                         if (bits & E_ANGLE3)
357                                 e->angles[2] = MSG_ReadAngle();
358                         if (bits & E_MODEL1)
359                                 e->modelindex = (e->modelindex & 0xFF00) | MSG_ReadByte();
360                         if (bits & E_MODEL2)
361                                 e->modelindex = (e->modelindex & 0x00FF) | (MSG_ReadByte() << 8);
362                         if (bits & E_FRAME1)
363                                 e->frame = (e->frame & 0xFF00) | MSG_ReadByte();
364                         if (bits & E_FRAME2)
365                                 e->frame = (e->frame & 0x00FF) | (MSG_ReadByte() << 8);
366                         if (bits & E_EFFECTS1)
367                                 e->effects = (e->effects & 0xFF00) | MSG_ReadByte();
368                         if (bits & E_EFFECTS2)
369                                 e->effects = (e->effects & 0x00FF) | (MSG_ReadByte() << 8);
370                         if (bits & E_COLORMAP)
371                                 e->colormap = MSG_ReadByte();
372                         if (bits & E_SKIN)
373                                 e->skin = MSG_ReadByte();
374                         if (bits & E_ALPHA)
375                                 e->alpha = MSG_ReadByte();
376                         if (bits & E_SCALE)
377                                 e->scale = MSG_ReadByte();
378                         if (bits & E_GLOWSIZE)
379                                 e->glowsize = MSG_ReadByte();
380                         if (bits & E_GLOWCOLOR)
381                                 e->glowcolor = MSG_ReadByte();
382                         if (bits & E_FLAGS)
383                                 e->flags = MSG_ReadByte();
384                 }
385         }
386         while (onum < delta->numentities)
387         {
388                 if (f->numentities >= MAX_ENTITY_DATABASE)
389                         Host_Error("EntityFrame_Read: entity list too big\n");
390                 memcpy(f->entitydata + f->numentities, delta->entitydata + onum, sizeof(entity_state_t));
391                 onum++;
392                 f->numentities++;
393         }
394         EntityFrame_AddFrame(d, f);
395 }
396
397 void EntityFrame_FetchEye(entity_database_t *d, vec3_t eye, double time)
398 {
399         float frac;
400         if (d->numframes == 0)
401                 Host_Error("EntityFrame_FetchEye: no frames\n");
402         if (d->numframes > 1 && d->frames[d->numframes - 2].time != d->frames[d->numframes - 1].time)
403         {
404                 frac = (time - d->frames[d->numframes - 2].time) / (d->frames[d->numframes - 1].time - d->frames[d->numframes - 2].time);
405                 frac = bound(0, frac, 1);
406                 VectorSubtract(d->frames[d->numframes - 2].eye, d->frames[d->numframes - 1].eye, eye);
407                 VectorMA(d->frames[d->numframes - 2].eye, frac, eye, eye);
408         }
409         else
410                 VectorCopy(d->frames[0].eye, eye);
411 }
412
413 // (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
414 int EntityFrame_FetchEntityByIndex(entity_frame_t *f, entity_state_t *e, int index)
415 {
416         if (index < 0 || index >= f->numentities)
417                 return false;
418         memcpy(e, f->entitydata + index, sizeof(*e));
419         return true;
420 }
421
422 int EntityFrame_FetchEntityByNumber(entity_frame_t *f, entity_state_t *e, int number)
423 {
424         int i;
425         for (i = 0;i < f->numentities;i++)
426         {
427                 if (f->entitydata[i].number == number)
428                 {
429                         memcpy(e, f->entitydata + i, sizeof(*e));
430                         return true;
431                 }
432         }
433         ClearStateToDefault(e);
434         return false;
435 }