4 int EntityState_DeltaBits(const entity_state_t *o, const entity_state_t *n)
7 // if o is not active, delta from default
8 if (o->active != ACTIVE_NETWORK)
11 if (fabs(n->origin[0] - o->origin[0]) > (1.0f / 256.0f))
13 if (fabs(n->origin[1] - o->origin[1]) > (1.0f / 256.0f))
15 if (fabs(n->origin[2] - o->origin[2]) > (1.0f / 256.0f))
17 if ((unsigned char) (n->angles[0] * (256.0f / 360.0f)) != (unsigned char) (o->angles[0] * (256.0f / 360.0f)))
19 if ((unsigned char) (n->angles[1] * (256.0f / 360.0f)) != (unsigned char) (o->angles[1] * (256.0f / 360.0f)))
21 if ((unsigned char) (n->angles[2] * (256.0f / 360.0f)) != (unsigned char) (o->angles[2] * (256.0f / 360.0f)))
23 if ((n->modelindex ^ o->modelindex) & 0x00FF)
25 if ((n->modelindex ^ o->modelindex) & 0xFF00)
27 if ((n->frame ^ o->frame) & 0x00FF)
29 if ((n->frame ^ o->frame) & 0xFF00)
31 if ((n->effects ^ o->effects) & 0x00FF)
33 if ((n->effects ^ o->effects) & 0xFF00)
35 if (n->colormap != o->colormap)
37 if (n->skin != o->skin)
39 if (n->alpha != o->alpha)
41 if (n->scale != o->scale)
43 if (n->glowsize != o->glowsize)
45 if (n->glowcolor != o->glowcolor)
47 if (n->flags != o->flags)
49 if (n->tagindex != o->tagindex || n->tagentity != o->tagentity)
50 bits |= E_TAGATTACHMENT;
51 if (n->light[0] != o->light[0] || n->light[1] != o->light[1] || n->light[2] != o->light[2] || n->light[3] != o->light[3])
53 if (n->lightstyle != o->lightstyle)
55 if (n->lightpflags != o->lightpflags)
56 bits |= E_LIGHTPFLAGS;
60 if (bits & 0xFF000000)
62 if (bits & 0x00FF0000)
64 if (bits & 0x0000FF00)
70 void EntityState_WriteExtendBits(sizebuf_t *msg, unsigned int bits)
72 MSG_WriteByte(msg, bits & 0xFF);
73 if (bits & 0x00000080)
75 MSG_WriteByte(msg, (bits >> 8) & 0xFF);
76 if (bits & 0x00008000)
78 MSG_WriteByte(msg, (bits >> 16) & 0xFF);
79 if (bits & 0x00800000)
80 MSG_WriteByte(msg, (bits >> 24) & 0xFF);
85 void EntityState_WriteFields(const entity_state_t *ent, sizebuf_t *msg, unsigned int bits)
87 if (sv.protocol == PROTOCOL_DARKPLACES2)
90 MSG_WriteCoord16i(msg, ent->origin[0]);
92 MSG_WriteCoord16i(msg, ent->origin[1]);
94 MSG_WriteCoord16i(msg, ent->origin[2]);
98 // LadyHavoc: have to write flags first, as they can modify protocol
100 MSG_WriteByte(msg, ent->flags);
101 if (ent->flags & RENDER_LOWPRECISION)
103 if (bits & E_ORIGIN1)
104 MSG_WriteCoord16i(msg, ent->origin[0]);
105 if (bits & E_ORIGIN2)
106 MSG_WriteCoord16i(msg, ent->origin[1]);
107 if (bits & E_ORIGIN3)
108 MSG_WriteCoord16i(msg, ent->origin[2]);
112 if (bits & E_ORIGIN1)
113 MSG_WriteCoord32f(msg, ent->origin[0]);
114 if (bits & E_ORIGIN2)
115 MSG_WriteCoord32f(msg, ent->origin[1]);
116 if (bits & E_ORIGIN3)
117 MSG_WriteCoord32f(msg, ent->origin[2]);
120 if ((sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4) && (ent->flags & RENDER_LOWPRECISION))
123 MSG_WriteAngle8i(msg, ent->angles[0]);
125 MSG_WriteAngle8i(msg, ent->angles[1]);
127 MSG_WriteAngle8i(msg, ent->angles[2]);
132 MSG_WriteAngle16i(msg, ent->angles[0]);
134 MSG_WriteAngle16i(msg, ent->angles[1]);
136 MSG_WriteAngle16i(msg, ent->angles[2]);
139 MSG_WriteByte(msg, ent->modelindex & 0xFF);
141 MSG_WriteByte(msg, (ent->modelindex >> 8) & 0xFF);
143 MSG_WriteByte(msg, ent->frame & 0xFF);
145 MSG_WriteByte(msg, (ent->frame >> 8) & 0xFF);
146 if (bits & E_EFFECTS1)
147 MSG_WriteByte(msg, ent->effects & 0xFF);
148 if (bits & E_EFFECTS2)
149 MSG_WriteByte(msg, (ent->effects >> 8) & 0xFF);
150 if (bits & E_COLORMAP)
151 MSG_WriteByte(msg, ent->colormap);
153 MSG_WriteByte(msg, ent->skin);
155 MSG_WriteByte(msg, ent->alpha);
157 MSG_WriteByte(msg, ent->scale);
158 if (bits & E_GLOWSIZE)
159 MSG_WriteByte(msg, ent->glowsize);
160 if (bits & E_GLOWCOLOR)
161 MSG_WriteByte(msg, ent->glowcolor);
162 if (sv.protocol == PROTOCOL_DARKPLACES2)
164 MSG_WriteByte(msg, ent->flags);
165 if (bits & E_TAGATTACHMENT)
167 MSG_WriteShort(msg, ent->tagentity);
168 MSG_WriteByte(msg, ent->tagindex);
172 MSG_WriteShort(msg, ent->light[0]);
173 MSG_WriteShort(msg, ent->light[1]);
174 MSG_WriteShort(msg, ent->light[2]);
175 MSG_WriteShort(msg, ent->light[3]);
177 if (bits & E_LIGHTSTYLE)
178 MSG_WriteByte(msg, ent->lightstyle);
179 if (bits & E_LIGHTPFLAGS)
180 MSG_WriteByte(msg, ent->lightpflags);
183 void EntityState_WriteUpdate(const entity_state_t *ent, sizebuf_t *msg, const entity_state_t *delta)
185 prvm_prog_t *prog = SVVM_prog;
187 if (ent->active == ACTIVE_NETWORK)
189 // entity is active, check for changes from the delta
190 if ((bits = EntityState_DeltaBits(delta, ent)))
192 // write the update number, bits, and fields
193 ENTITYSIZEPROFILING_START(msg, ent->number, bits);
194 MSG_WriteShort(msg, ent->number);
195 EntityState_WriteExtendBits(msg, bits);
196 EntityState_WriteFields(ent, msg, bits);
197 ENTITYSIZEPROFILING_END(msg, ent->number, bits);
202 // entity is inactive, check if the delta was active
203 if (delta->active == ACTIVE_NETWORK)
205 // write the remove number
206 ENTITYSIZEPROFILING_START(msg, ent->number, 0);
207 MSG_WriteShort(msg, ent->number | 0x8000);
208 ENTITYSIZEPROFILING_END(msg, ent->number, 0);
213 // (server) adds a entity_frame to the database, for future reference
214 void EntityFrame_AddFrame_Server(entityframe_database_t *d, vec3_t eye, int framenum, int numentities, const entity_state_t **entitydata)
217 entity_frameinfo_t *info;
219 VectorCopy(eye, d->eye);
221 // figure out how many entity slots are used already
224 n = d->frames[d->numframes - 1].endentity - d->frames[0].firstentity;
225 if (n + numentities > MAX_ENTITY_DATABASE || d->numframes >= MAX_ENTITY_HISTORY)
227 // ran out of room, dump database
228 EntityFrame_ClearDatabase(d);
232 info = &d->frames[d->numframes];
233 info->framenum = framenum;
235 // make sure we check the newly added frame as well, but we haven't incremented numframes yet
236 for (n = 0;n <= d->numframes;n++)
238 if (e >= d->frames[n].framenum)
241 Con_Print("EntityFrame_AddFrame: tried to add out of sequence frame to database\n");
243 Con_Print("EntityFrame_AddFrame: out of sequence frames in database\n");
246 e = d->frames[n].framenum;
248 // if database still has frames after that...
250 info->firstentity = d->frames[d->numframes - 1].endentity;
252 info->firstentity = 0;
253 info->endentity = info->firstentity + numentities;
256 n = info->firstentity % MAX_ENTITY_DATABASE;
257 e = MAX_ENTITY_DATABASE - n;
260 memcpy(d->entitydata + n, entitydata, sizeof(entity_state_t) * e);
262 memcpy(d->entitydata, entitydata + e, sizeof(entity_state_t) * (numentities - e));
265 // (server) writes a frame to network stream
266 qboolean EntityFrame_WriteFrame(sizebuf_t *msg, int maxsize, entityframe_database_t *d, int numstates, const entity_state_t **states, int viewentnum)
268 prvm_prog_t *prog = SVVM_prog;
270 entity_frame_t *o = &d->deltaframe;
271 const entity_state_t *ent, *delta;
277 for (i = 0;i < numstates;i++)
280 if (ent->number == viewentnum)
282 VectorSet(eye, ent->origin[0], ent->origin[1], ent->origin[2] + 22);
287 EntityFrame_AddFrame_Server(d, eye, d->latestframenum, numstates, states);
289 EntityFrame_FetchFrame(d, d->ackframenum, o);
291 MSG_WriteByte (msg, svc_entities);
292 MSG_WriteLong (msg, o->framenum);
293 MSG_WriteLong (msg, d->latestframenum);
294 MSG_WriteFloat (msg, eye[0]);
295 MSG_WriteFloat (msg, eye[1]);
296 MSG_WriteFloat (msg, eye[2]);
299 for (i = 0;i < numstates;i++)
302 number = ent->number;
304 if (PRVM_serveredictfunction((&prog->edicts[number]), SendEntity))
306 for (;onum < o->numentities && o->entitydata[onum].number < number;onum++)
308 // write remove message
309 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
311 if (onum < o->numentities && (o->entitydata[onum].number == number))
313 // delta from previous frame
314 delta = o->entitydata + onum;
315 // advance to next entity in delta frame
320 // delta from defaults
321 delta = &defaultstate;
323 EntityState_WriteUpdate(ent, msg, delta);
325 for (;onum < o->numentities;onum++)
327 // write remove message
328 MSG_WriteShort(msg, o->entitydata[onum].number | 0x8000);
330 MSG_WriteShort(msg, 0xFFFF);