2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
pr_edict.c
|
|
|
|
|
|
|
|
entity dictionary
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/progs.h"
|
2001-09-21 04:22:46 +00:00
|
|
|
#include "QF/sys.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-12-08 08:19:48 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
ED_ClearEdict
|
|
|
|
|
|
|
|
Sets everything to NULL
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-10-08 19:46:42 +00:00
|
|
|
ED_ClearEdict (progs_t *pr, edict_t *e, int val)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2007-04-06 00:47:41 +00:00
|
|
|
pr_uint_t i;
|
2001-04-19 22:42:01 +00:00
|
|
|
|
2020-02-25 05:57:36 +00:00
|
|
|
if (pr->reserved_edicts && NUM_FOR_EDICT (pr, e) < *pr->reserved_edicts)
|
2007-04-09 08:55:24 +00:00
|
|
|
Sys_Printf ("clearing reserved edict %d\n", NUM_FOR_EDICT (pr, e));
|
2001-04-19 22:42:01 +00:00
|
|
|
for (i=0; i < pr->progs->entityfields; i++)
|
2013-01-17 05:11:54 +00:00
|
|
|
E_INT (e, i) = val;
|
2001-02-19 21:15:25 +00:00
|
|
|
e->free = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Either finds a free edict, or allocates a new one.
|
|
|
|
Try to avoid reusing an entity that was recently freed, because it
|
|
|
|
can cause the client to think the entity morphed into something else
|
|
|
|
instead of being removed and recreated, which can cause interpolated
|
|
|
|
angles and bad trails.
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE edict_t *
|
2002-10-08 19:46:42 +00:00
|
|
|
ED_Alloc (progs_t *pr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2022-01-16 13:15:18 +00:00
|
|
|
pr_uint_t i;
|
2001-02-19 21:15:25 +00:00
|
|
|
edict_t *e;
|
2001-02-28 09:12:01 +00:00
|
|
|
int start = pr->reserved_edicts ? *pr->reserved_edicts : 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2020-02-25 05:57:36 +00:00
|
|
|
if (!pr->num_edicts) {
|
|
|
|
PR_RunError (pr, "Edicts not supported in this VM\n");
|
|
|
|
}
|
2004-11-09 22:32:00 +00:00
|
|
|
for (i = start + 1; i < *pr->num_edicts; i++) {
|
2001-02-19 21:15:25 +00:00
|
|
|
e = EDICT_NUM (pr, i);
|
|
|
|
// the first couple seconds of server time can involve a lot of
|
|
|
|
// freeing and allocating, so relax the replacement policy
|
2022-01-16 10:32:47 +00:00
|
|
|
if (e->free && (!pr->globals.ftime//FIXME double time
|
2004-11-09 22:32:00 +00:00
|
|
|
|| e->freetime < 2
|
2022-01-16 10:32:47 +00:00
|
|
|
|| *pr->globals.ftime - e->freetime > 0.5)) {
|
2001-04-19 22:42:01 +00:00
|
|
|
ED_ClearEdict (pr, e, 0);
|
2001-02-19 21:15:25 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-09 06:06:42 +00:00
|
|
|
if (i == pr->max_edicts) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("WARNING: ED_Alloc: no free edicts\n");
|
2012-07-18 01:30:58 +00:00
|
|
|
PR_DumpState (pr);
|
2001-02-19 21:15:25 +00:00
|
|
|
i--; // step on whatever is the last edict
|
|
|
|
e = EDICT_NUM (pr, i);
|
|
|
|
if (pr->unlink)
|
|
|
|
pr->unlink (e);
|
|
|
|
} else
|
|
|
|
(*(pr)->num_edicts)++;
|
|
|
|
e = EDICT_NUM (pr, i);
|
2001-04-19 22:42:01 +00:00
|
|
|
ED_ClearEdict (pr, e, 0);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
ED_Free
|
|
|
|
|
|
|
|
Marks the edict as free
|
|
|
|
FIXME: walk all entities and NULL out references to this entity
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-10-08 19:46:42 +00:00
|
|
|
ED_Free (progs_t *pr, edict_t *ed)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (pr->unlink)
|
|
|
|
pr->unlink (ed); // unlink from world bsp
|
|
|
|
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (pr_deadbeef_ents) {
|
2001-04-22 04:58:33 +00:00
|
|
|
ED_ClearEdict (pr, ed, 0xdeadbeef);
|
|
|
|
} else {
|
2001-08-12 04:14:25 +00:00
|
|
|
if (pr->free_edict)
|
|
|
|
pr->free_edict (pr, ed);
|
|
|
|
else
|
|
|
|
ED_ClearEdict (pr, ed, 0);
|
2001-04-22 04:58:33 +00:00
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
ed->free = true;
|
2022-01-16 10:32:47 +00:00
|
|
|
if (pr->globals.ftime)//FIXME double time
|
|
|
|
ed->freetime = *pr->globals.ftime;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2021-07-25 00:54:08 +00:00
|
|
|
ED_PrintNum (progs_t *pr, pr_int_t ent, const char *fieldname)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2020-02-25 05:57:36 +00:00
|
|
|
if (!pr->num_edicts) {
|
|
|
|
Sys_Printf ("Edicts not supported in this VM\n");
|
|
|
|
return;
|
|
|
|
}
|
2021-07-25 00:54:08 +00:00
|
|
|
ED_Print (pr, EDICT_NUM (pr, ent), fieldname);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
ED_PrintEdicts
|
|
|
|
|
|
|
|
For debugging, prints all the entities in the current server
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-07-19 03:18:11 +00:00
|
|
|
ED_PrintEdicts (progs_t *pr, const char *fieldval)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2022-01-16 13:15:18 +00:00
|
|
|
pr_uint_t i;
|
2007-04-06 00:47:41 +00:00
|
|
|
int count;
|
2020-02-22 13:33:44 +00:00
|
|
|
pr_def_t *def;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2004-11-05 11:49:00 +00:00
|
|
|
def = PR_FindField(pr, "classname");
|
2001-07-19 03:18:11 +00:00
|
|
|
|
2020-02-25 05:57:36 +00:00
|
|
|
if (!pr->num_edicts) {
|
|
|
|
Sys_Printf ("Edicts not supported in this VM\n");
|
|
|
|
return;
|
|
|
|
}
|
2001-07-19 03:18:11 +00:00
|
|
|
if (fieldval && fieldval[0] && def) {
|
|
|
|
count = 0;
|
2020-02-25 05:57:36 +00:00
|
|
|
for (i = 0; i < *pr->num_edicts; i++)
|
2003-04-22 18:20:15 +00:00
|
|
|
if (strequal(fieldval,
|
|
|
|
E_GSTRING (pr, EDICT_NUM(pr, i), def->ofs))) {
|
2021-07-25 00:54:08 +00:00
|
|
|
ED_PrintNum (pr, i, 0);
|
2001-07-19 03:18:11 +00:00
|
|
|
count++;
|
|
|
|
}
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("%i entities\n", count);
|
2001-07-19 03:18:11 +00:00
|
|
|
} else {
|
2020-02-25 05:57:36 +00:00
|
|
|
for (i = 0; i < *pr->num_edicts; i++)
|
2021-07-25 00:54:08 +00:00
|
|
|
ED_PrintNum (pr, i, 0);
|
2020-02-25 05:57:36 +00:00
|
|
|
Sys_Printf ("%i entities\n", *pr->num_edicts);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
ED_Count
|
|
|
|
|
|
|
|
For debugging
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-10-08 19:46:42 +00:00
|
|
|
ED_Count (progs_t *pr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2007-04-06 00:47:41 +00:00
|
|
|
int active, models, solid, step, zombie;
|
2020-02-22 13:33:44 +00:00
|
|
|
pr_def_t *solid_def;
|
|
|
|
pr_def_t *model_def;
|
2007-04-06 00:47:41 +00:00
|
|
|
edict_t *ent;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2020-02-25 05:57:36 +00:00
|
|
|
if (!pr->num_edicts) {
|
|
|
|
Sys_Printf ("Edicts not supported in this VM\n");
|
|
|
|
return;
|
|
|
|
}
|
2004-11-05 11:49:00 +00:00
|
|
|
solid_def = PR_FindField (pr, "solid");
|
|
|
|
model_def = PR_FindField (pr, "model");
|
2001-09-20 19:34:02 +00:00
|
|
|
active = models = solid = step = zombie = 0;
|
2022-01-16 13:15:18 +00:00
|
|
|
for (pr_uint_t i = 0; i < *pr->num_edicts; i++) {
|
2001-02-19 21:15:25 +00:00
|
|
|
ent = EDICT_NUM (pr, i);
|
2001-09-20 19:34:02 +00:00
|
|
|
if (ent->free) {
|
2022-01-16 10:32:47 +00:00
|
|
|
if (pr->globals.ftime && *pr->globals.ftime - ent->freetime <= 0.5)//FIXME double time
|
2001-09-20 19:34:02 +00:00
|
|
|
zombie++;
|
2001-02-19 21:15:25 +00:00
|
|
|
continue;
|
2001-09-20 19:34:02 +00:00
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
active++;
|
2013-01-17 05:11:54 +00:00
|
|
|
if (solid_def && E_FLOAT (ent, solid_def->ofs))
|
2001-02-19 21:15:25 +00:00
|
|
|
solid++;
|
2013-01-17 05:11:54 +00:00
|
|
|
if (model_def && E_FLOAT (ent, model_def->ofs))
|
2001-02-19 21:15:25 +00:00
|
|
|
models++;
|
|
|
|
}
|
|
|
|
|
2020-02-25 05:57:36 +00:00
|
|
|
Sys_Printf ("num_edicts:%3i\n", *pr->num_edicts);
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("active :%3i\n", active);
|
|
|
|
Sys_Printf ("view :%3i\n", models);
|
|
|
|
Sys_Printf ("touch :%3i\n", solid);
|
|
|
|
Sys_Printf ("zombie :%3i\n", zombie);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2002-10-08 19:46:42 +00:00
|
|
|
edict_t *
|
2022-01-16 13:15:18 +00:00
|
|
|
ED_EdictNum (progs_t *pr, pr_uint_t n)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2022-01-16 13:15:18 +00:00
|
|
|
if (n >= *pr->num_edicts)
|
2007-04-06 00:47:41 +00:00
|
|
|
PR_RunError (pr, "EDICT_NUM: bad number %d", n);
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2013-01-17 05:11:54 +00:00
|
|
|
return PR_edicts(pr) + n;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 13:15:18 +00:00
|
|
|
pr_uint_t
|
2004-11-05 11:49:00 +00:00
|
|
|
ED_NumForEdict (progs_t *pr, edict_t *e)
|
2001-09-23 01:29:52 +00:00
|
|
|
{
|
2022-01-16 13:15:18 +00:00
|
|
|
pr_uint_t b;
|
2001-09-23 01:29:52 +00:00
|
|
|
|
|
|
|
b = NUM_FOR_BAD_EDICT (pr, e);
|
|
|
|
|
2022-01-16 13:15:18 +00:00
|
|
|
if (b && b >= *pr->num_edicts)
|
2004-01-31 08:34:01 +00:00
|
|
|
PR_RunError (pr, "NUM_FOR_EDICT: bad pointer %d %p %p", b, e,
|
2013-01-17 05:11:54 +00:00
|
|
|
pr->pr_edicts);
|
2001-09-23 01:29:52 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
return b;
|
|
|
|
}
|
2002-10-29 05:07:10 +00:00
|
|
|
|
2023-06-13 09:06:11 +00:00
|
|
|
bool
|
2022-01-02 11:46:32 +00:00
|
|
|
PR_EdictValid (progs_t *pr, pr_uint_t e)
|
2002-10-29 05:07:10 +00:00
|
|
|
{
|
2020-02-25 05:57:36 +00:00
|
|
|
if (!pr->num_edicts) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-02 11:46:32 +00:00
|
|
|
if (e >= pr->pr_edict_area_size)
|
2002-10-29 05:07:10 +00:00
|
|
|
return false;
|
|
|
|
if (e % pr->pr_edict_size)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|