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++)
84 d->referenceentity[i] = defaultstate;
85 // if this is the server, remove commits
86 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
87 commit->numentities = 0;
91 else if (d->referenceframenum == framenum)
96 for (i = 0, commit = d->commit;i < MAX_ENTITY_HISTORY;i++, commit++)
98 if (commit->numentities && commit->framenum <= framenum)
100 if (commit->framenum == framenum)
103 d->referenceframenum = framenum;
104 if (developer_networkentities.integer >= 3)
106 for (j = 0;j < commit->numentities;j++)
108 entity_state_t *s = EntityFrame4_GetReferenceEntity(d, commit->entity[j].number);
109 if (commit->entity[j].active != s->active)
111 if (commit->entity[j].active == ACTIVE_NETWORK)
112 Con_Printf("commit entity %i has become active (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
114 Con_Printf("commit entity %i has become inactive (modelindex %i)\n", commit->entity[j].number, commit->entity[j].modelindex);
116 *s = commit->entity[j];
120 for (j = 0;j < commit->numentities;j++)
121 *EntityFrame4_GetReferenceEntity(d, commit->entity[j].number) = commit->entity[j];
123 commit->numentities = 0;
127 if (developer_networkentities.integer >= 1)
129 Con_Printf("ack ref:%i database updated to: ref:%i commits:", framenum, d->referenceframenum);
130 for (i = 0;i < MAX_ENTITY_HISTORY;i++)
131 if (d->commit[i].numentities)
132 Con_Printf(" %i", d->commit[i].framenum);