int NUM_FOR_EDICT(edict_t *e)
{
- int b;
-
- b = (qbyte *)e - (qbyte *)sv.edicts;
- b = b / pr_edict_size;
-
- if (b < 0 || b >= sv.num_edicts)
+ if ((qbyte *)e < (qbyte *)sv.edicts || (qbyte *)e > (qbyte *)sv.edicts + pr_edictareasize)
Host_Error ("NUM_FOR_EDICT: bad pointer");
- return b;
+ return e->number;
}
int NoCrash_NUM_FOR_EDICT(edict_t *e)
{
- int b;
-
- b = (qbyte *)e - (qbyte *)sv.edicts;
- b = b / pr_edict_size;
- return b;
+ return e->number;
}
#define MAX_ENT_LEAFS 256
typedef struct edict_s
{
- qboolean free;
- link_t area;
+ qboolean free; // true if this edict is unused
+ link_t area; // physics area this edict is linked into
+ int number; // number of this edict
#ifdef QUAKEENTITIES
- entity_state_t baseline;
- entity_state_t deltabaseline; // LordHavoc: previous frame
+ entity_state_t baseline; // baseline values
+ entity_state_t deltabaseline; // LordHavoc: previous frame
#endif
- int suspendedinairflag; // LordHavoc: gross hack to make floating items still work
- float freetime; // sv.time when the object was freed
- entvars_t v; // C exported fields from progs
+ int suspendedinairflag; // LordHavoc: gross hack to make floating items still work
+ float freetime; // sv.time when the object was freed
+ entvars_t v; // C exported fields from progs
// other fields from progs come immediately after
} edict_t;
void ED_LoadFromFile (const char *data);
edict_t *EDICT_NUM_ERROR(int n);
-#define EDICT_NUM(n) (n >= 0 ? (n < sv.max_edicts ? (edict_t *)((qbyte *)sv.edicts + (n) * pr_edict_size) : EDICT_NUM_ERROR(n)) : EDICT_NUM_ERROR(n))
+#define EDICT_NUM(n) ((n >= 0 && n < sv.max_edicts) ? sv.edictstable[(n)] : EDICT_NUM_ERROR(n))
int NUM_FOR_EDICT(edict_t *e);
int max_edicts;
// can NOT be array indexed, because edict_t is variable sized, but can be used to reference the world ent
edict_t *edicts;
+ // can be array indexed
+ edict_t **edictstable;
// some actions are only valid during load
server_state_t state;
// clear the edict memory pool
Mem_EmptyPool(sv_edicts_mempool);
sv.edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
+ sv.edictstable = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
+ for (i = 0;i < MAX_EDICTS;i++)
+ {
+ ent = (edict_t *)((qbyte *)sv.edicts + (i * pr_edict_size));
+ ent->number = i;
+ sv.edictstable[i] = ent;
+ }
sv.datagram.maxsize = sizeof(sv.datagram_buf);
sv.datagram.cursize = 0;