4 entityframe4_database_t *EntityFrame4_AllocDatabase(mempool_t *pool)
6 entityframe4_database_t *d;
7 d = (entityframe4_database_t *)Mem_Alloc(pool, sizeof(*d));
9 EntityFrame4_ResetDatabase(d);
13 void EntityFrame4_FreeDatabase(entityframe4_database_t *d)
16 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
17 if (d->commit[i].entity)
18 Mem_Free(d->commit[i].entity);
19 if (d->referenceentity)
20 Mem_Free(d->referenceentity);
24 void EntityFrame4_ResetDatabase(entityframe4_database_t *d)
27 d->referenceframenum = -1;
28 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
29 d->commit[i].numentities = 0;
30 for (i = 0;i < d->maxreferenceentities;i++)
31 d->referenceentity[i] = defaultstate;
34 entity_state_t *EntityFrame4_GetReferenceEntity(entityframe4_database_t *d, int number)
36 if (d->maxreferenceentities <= number)
38 int oldmax = d->maxreferenceentities;
39 entity_state_t *oldentity = d->referenceentity;
40 d->maxreferenceentities = (number + 15) & ~7;
41 d->referenceentity = (entity_state_t *)Mem_Alloc(d->mempool, d->maxreferenceentities * sizeof(*d->referenceentity));
44 memcpy(d->referenceentity, oldentity, oldmax * sizeof(*d->referenceentity));
47 // clear the newly created entities
48 for (;oldmax < d->maxreferenceentities;oldmax++)
50 d->referenceentity[oldmax] = defaultstate;
51 d->referenceentity[oldmax].number = oldmax;
54 return d->referenceentity + number;
57 void EntityFrame4_AddCommitEntity(entityframe4_database_t *d, const entity_state_t *s)
59 // resize commit's entity list if full
60 if (d->currentcommit->maxentities <= d->currentcommit->numentities)
62 entity_state_t *oldentity = d->currentcommit->entity;
63 d->currentcommit->maxentities += 8;
64 d->currentcommit->entity = (entity_state_t *)Mem_Alloc(d->mempool, d->currentcommit->maxentities * sizeof(*d->currentcommit->entity));
67 memcpy(d->currentcommit->entity, oldentity, d->currentcommit->numentities * sizeof(*d->currentcommit->entity));
71 d->currentcommit->entity[d->currentcommit->numentities++] = *s;
74 int EntityFrame4_AckFrame(entityframe4_database_t *d, int framenum, int servermode)
77 entity_database4_commit_t *commit;
80 // reset reference, but leave commits alone
81 d->referenceframenum = -1;
82 for (i = 0;i < d->maxreferenceentities;i++)
83 d->referenceentity[i] = defaultstate;
84 // if this is the server, remove commits
85 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
86 commit->numentities = 0;
89 else if (d->referenceframenum == framenum)
94 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
96 if (commit->numentities && commit->framenum <= framenum)
98 if (commit->framenum == framenum)
101 d->referenceframenum = framenum;
102 if (developer_networkentities.integer >= 3)
104 for (j = 0;j < commit->numentities;j++)
106 entity_state_t *s = EntityFrame4_GetReferenceEntity(d, commit->entity[j].number);
107 if (commit->entity[j].active != s->active)
109 if (commit->entity[j].active == ACTIVE_NETWORK)
110 Con_Printf("commit entity %i has become active (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
112 Con_Printf("commit entity %i has become inactive (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
114 *s = commit->entity[j];
118 for (j = 0;j < commit->numentities;j++)
119 *EntityFrame4_GetReferenceEntity(d, commit->entity[j].number) = commit->entity[j];
121 commit->numentities = 0;
125 if (developer_networkentities.integer >= 1)
127 Con_Printf("ack ref:%i database updated to: ref:%i commits:", framenum, d->referenceframenum);
128 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
129 if (d->commit[i].numentities)
130 Con_Printf(" %i", d->commit[i].framenum);