From 8d7975810323fb62e00a37e981a758978195545b Mon Sep 17 00:00:00 2001 From: havoc Date: Mon, 28 Oct 2002 14:26:49 +0000 Subject: [PATCH] faster number to edict lookup (sv.edictstable) and faster edict to number lookup (edict_t now has a number inside it) git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@2574 d7cf8633-e32d-0410-b094-e92efae38249 --- pr_edict.c | 15 +++------------ progs.h | 17 +++++++++-------- server.h | 2 ++ sv_main.c | 7 +++++++ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/pr_edict.c b/pr_edict.c index 8520ede0..54e25b50 100644 --- a/pr_edict.c +++ b/pr_edict.c @@ -1556,22 +1556,13 @@ edict_t *EDICT_NUM_ERROR(int n) 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; } diff --git a/progs.h b/progs.h index b69e0caa..a50b7af9 100644 --- a/progs.h +++ b/progs.h @@ -43,17 +43,18 @@ typedef struct link_s #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; @@ -140,7 +141,7 @@ void ED_ParseGlobals (const char *data); 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); diff --git a/server.h b/server.h index dd0bc421..4887aa13 100644 --- a/server.h +++ b/server.h @@ -69,6 +69,8 @@ typedef struct 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; diff --git a/sv_main.c b/sv_main.c index 1f3e4bb6..7f250b0d 100644 --- a/sv_main.c +++ b/sv_main.c @@ -1766,6 +1766,13 @@ void SV_SpawnServer (const char *server) // 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; -- 2.39.2