2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // sv_edict.c -- entity dictionary
25 dfunction_t
*pr_functions
;
28 ddef_t
*pr_globaldefs
;
29 dstatement_t
*pr_statements
;
30 globalvars_t
*pr_global_struct
;
31 float *pr_globals
; // same as pr_global_struct
32 int pr_edict_size
; // in bytes
34 unsigned short pr_crc
;
36 int type_size
[8] = {1,sizeof(string_t
)/4,1,3,1,1,sizeof(func_t
)/4,sizeof(void *)/4};
38 ddef_t
*ED_FieldAtOfs (int ofs
);
39 qboolean
ED_ParseEpair (void *base
, ddef_t
*key
, char *s
);
41 cvar_t nomonsters
= {"nomonsters", "0"};
42 cvar_t gamecfg
= {"gamecfg", "0"};
43 cvar_t scratch1
= {"scratch1", "0"};
44 cvar_t scratch2
= {"scratch2", "0"};
45 cvar_t scratch3
= {"scratch3", "0"};
46 cvar_t scratch4
= {"scratch4", "0"};
47 cvar_t savedgamecfg
= {"savedgamecfg", "0", true};
48 cvar_t saved1
= {"saved1", "0", true};
49 cvar_t saved2
= {"saved2", "0", true};
50 cvar_t saved3
= {"saved3", "0", true};
51 cvar_t saved4
= {"saved4", "0", true};
53 #define MAX_FIELD_LEN 64
54 #define GEFV_CACHESIZE 2
58 char field
[MAX_FIELD_LEN
];
61 static gefv_cache gefvCache
[GEFV_CACHESIZE
] = {{NULL
, ""}, {NULL
, ""}};
67 Sets everything to NULL
70 void ED_ClearEdict (edict_t
*e
)
72 memset (&e
->v
, 0, progs
->entityfields
* 4);
80 Either finds a free edict, or allocates a new one.
81 Try to avoid reusing an entity that was recently freed, because it
82 can cause the client to think the entity morphed into something else
83 instead of being removed and recreated, which can cause interpolated
84 angles and bad trails.
87 edict_t
*ED_Alloc (void)
92 for ( i
=svs
.maxclients
+1 ; i
<sv
.num_edicts
; i
++)
95 // the first couple seconds of server time can involve a lot of
96 // freeing and allocating, so relax the replacement policy
97 if (e
->free
&& ( e
->freetime
< 2 || sv
.time
- e
->freetime
> 0.5 ) )
105 Sys_Error ("ED_Alloc: no free edicts");
118 Marks the edict as free
119 FIXME: walk all entities and NULL out references to this entity
122 void ED_Free (edict_t
*ed
)
124 SV_UnlinkEdict (ed
); // unlink from world bsp
128 ed
->v
.takedamage
= 0;
129 ed
->v
.modelindex
= 0;
133 VectorCopy (vec3_origin
, ed
->v
.origin
);
134 VectorCopy (vec3_origin
, ed
->v
.angles
);
135 ed
->v
.nextthink
= -1;
138 ed
->freetime
= sv
.time
;
141 //===========================================================================
148 ddef_t
*ED_GlobalAtOfs (int ofs
)
153 for (i
=0 ; i
<progs
->numglobaldefs
; i
++)
155 def
= &pr_globaldefs
[i
];
167 ddef_t
*ED_FieldAtOfs (int ofs
)
172 for (i
=0 ; i
<progs
->numfielddefs
; i
++)
174 def
= &pr_fielddefs
[i
];
186 ddef_t
*ED_FindField (char *name
)
191 for (i
=0 ; i
<progs
->numfielddefs
; i
++)
193 def
= &pr_fielddefs
[i
];
194 if (!strcmp(pr_strings
+ def
->s_name
,name
) )
206 ddef_t
*ED_FindGlobal (char *name
)
211 for (i
=0 ; i
<progs
->numglobaldefs
; i
++)
213 def
= &pr_globaldefs
[i
];
214 if (!strcmp(pr_strings
+ def
->s_name
,name
) )
226 dfunction_t
*ED_FindFunction (char *name
)
231 for (i
=0 ; i
<progs
->numfunctions
; i
++)
233 func
= &pr_functions
[i
];
234 if (!strcmp(pr_strings
+ func
->s_name
,name
) )
241 eval_t
*GetEdictFieldValue(edict_t
*ed
, char *field
)
247 for (i
=0 ; i
<GEFV_CACHESIZE
; i
++)
249 if (!strcmp(field
, gefvCache
[i
].field
))
251 def
= gefvCache
[i
].pcache
;
256 def
= ED_FindField (field
);
258 if (strlen(field
) < MAX_FIELD_LEN
)
260 gefvCache
[rep
].pcache
= def
;
261 strcpy (gefvCache
[rep
].field
, field
);
269 return (eval_t
*)((char *)&ed
->v
+ def
->ofs
*4);
277 Returns a string describing *data in a type specific manner
280 char *PR_ValueString (etype_t type
, eval_t
*val
)
282 static char line
[256];
286 type
&= ~DEF_SAVEGLOBAL
;
291 sprintf (line
, "%s", pr_strings
+ val
->string
);
294 sprintf (line
, "entity %i", NUM_FOR_EDICT(PROG_TO_EDICT(val
->edict
)) );
297 f
= pr_functions
+ val
->function
;
298 sprintf (line
, "%s()", pr_strings
+ f
->s_name
);
301 def
= ED_FieldAtOfs ( val
->_int
);
302 sprintf (line
, ".%s", pr_strings
+ def
->s_name
);
305 sprintf (line
, "void");
308 sprintf (line
, "%5.1f", val
->_float
);
311 sprintf (line
, "'%5.1f %5.1f %5.1f'", val
->vector
[0], val
->vector
[1], val
->vector
[2]);
314 sprintf (line
, "pointer");
317 sprintf (line
, "bad type %i", type
);
328 Returns a string describing *data in a type specific manner
329 Easier to parse than PR_ValueString
332 char *PR_UglyValueString (etype_t type
, eval_t
*val
)
334 static char line
[256];
338 type
&= ~DEF_SAVEGLOBAL
;
343 sprintf (line
, "%s", pr_strings
+ val
->string
);
346 sprintf (line
, "%i", NUM_FOR_EDICT(PROG_TO_EDICT(val
->edict
)));
349 f
= pr_functions
+ val
->function
;
350 sprintf (line
, "%s", pr_strings
+ f
->s_name
);
353 def
= ED_FieldAtOfs ( val
->_int
);
354 sprintf (line
, "%s", pr_strings
+ def
->s_name
);
357 sprintf (line
, "void");
360 sprintf (line
, "%f", val
->_float
);
363 sprintf (line
, "%f %f %f", val
->vector
[0], val
->vector
[1], val
->vector
[2]);
366 sprintf (line
, "bad type %i", type
);
377 Returns a string with a description and the contents of a global,
378 padded to 20 field width
381 char *PR_GlobalString (int ofs
)
387 static char line
[128];
389 val
= (void *)&pr_globals
[ofs
];
390 def
= ED_GlobalAtOfs(ofs
);
392 sprintf (line
,"%i(\?\?\?)", ofs
);
395 s
= PR_ValueString (def
->type
, val
);
396 sprintf (line
,"%i(%s)%s", ofs
, pr_strings
+ def
->s_name
, s
);
407 char *PR_GlobalStringNoContents (int ofs
)
411 static char line
[128];
413 def
= ED_GlobalAtOfs(ofs
);
415 sprintf (line
,"%i(\?\?\?)", ofs
);
417 sprintf (line
,"%i(%s)", ofs
, pr_strings
+ def
->s_name
);
435 void ED_Print (edict_t
*ed
)
446 Con_Printf ("FREE\n");
450 Con_Printf("\nEDICT %i:\n", NUM_FOR_EDICT(ed
));
451 for (i
=1 ; i
<progs
->numfielddefs
; i
++)
453 d
= &pr_fielddefs
[i
];
454 name
= pr_strings
+ d
->s_name
;
455 if (name
[strlen(name
)-2] == '_')
456 continue; // skip _x, _y, _z vars
458 v
= (int *)((char *)&ed
->v
+ d
->ofs
*4);
460 // if the value is still all 0, skip the field
461 type
= d
->type
& ~DEF_SAVEGLOBAL
;
463 for (j
=0 ; j
<type_size
[type
] ; j
++)
466 if (j
== type_size
[type
])
469 Con_Printf ("%s",name
);
474 Con_Printf ("%s\n", PR_ValueString(d
->type
, (eval_t
*)v
));
485 void ED_Write (FILE *f
, edict_t
*ed
)
501 for (i
=1 ; i
<progs
->numfielddefs
; i
++)
503 d
= &pr_fielddefs
[i
];
504 name
= pr_strings
+ d
->s_name
;
505 if (name
[strlen(name
)-2] == '_')
506 continue; // skip _x, _y, _z vars
508 v
= (int *)((char *)&ed
->v
+ d
->ofs
*4);
510 // if the value is still all 0, skip the field
511 type
= d
->type
& ~DEF_SAVEGLOBAL
;
512 for (j
=0 ; j
<type_size
[type
] ; j
++)
515 if (j
== type_size
[type
])
518 fprintf (f
,"\"%s\" ",name
);
519 fprintf (f
,"\"%s\"\n", PR_UglyValueString(d
->type
, (eval_t
*)v
));
525 void ED_PrintNum (int ent
)
527 ED_Print (EDICT_NUM(ent
));
534 For debugging, prints all the entities in the current server
537 void ED_PrintEdicts (void)
541 Con_Printf ("%i entities\n", sv
.num_edicts
);
542 for (i
=0 ; i
<sv
.num_edicts
; i
++)
550 For debugging, prints a single edicy
553 void ED_PrintEdict_f (void)
557 i
= Q_atoi (Cmd_Argv(1));
558 if (i
>= sv
.num_edicts
)
560 Con_Printf("Bad edict number\n");
577 int active
, models
, solid
, step
;
579 active
= models
= solid
= step
= 0;
580 for (i
=0 ; i
<sv
.num_edicts
; i
++)
590 if (ent
->v
.movetype
== MOVETYPE_STEP
)
594 Con_Printf ("num_edicts:%3i\n", sv
.num_edicts
);
595 Con_Printf ("active :%3i\n", active
);
596 Con_Printf ("view :%3i\n", models
);
597 Con_Printf ("touch :%3i\n", solid
);
598 Con_Printf ("step :%3i\n", step
);
603 ==============================================================================
607 FIXME: need to tag constants, doesn't really work
608 ==============================================================================
616 void ED_WriteGlobals (FILE *f
)
624 for (i
=0 ; i
<progs
->numglobaldefs
; i
++)
626 def
= &pr_globaldefs
[i
];
628 if ( !(def
->type
& DEF_SAVEGLOBAL
) )
630 type
&= ~DEF_SAVEGLOBAL
;
632 if (type
!= ev_string
634 && type
!= ev_entity
)
637 name
= pr_strings
+ def
->s_name
;
638 fprintf (f
,"\"%s\" ", name
);
639 fprintf (f
,"\"%s\"\n", PR_UglyValueString(type
, (eval_t
*)&pr_globals
[def
->ofs
]));
649 void ED_ParseGlobals (char *data
)
657 data
= COM_Parse (data
);
658 if (com_token
[0] == '}')
661 Sys_Error ("ED_ParseEntity: EOF without closing brace");
663 strcpy (keyname
, com_token
);
666 data
= COM_Parse (data
);
668 Sys_Error ("ED_ParseEntity: EOF without closing brace");
670 if (com_token
[0] == '}')
671 Sys_Error ("ED_ParseEntity: closing brace without data");
673 key
= ED_FindGlobal (keyname
);
676 Con_Printf ("'%s' is not a global\n", keyname
);
680 if (!ED_ParseEpair ((void *)pr_globals
, key
, com_token
))
681 Host_Error ("ED_ParseGlobals: parse error");
685 //============================================================================
693 char *ED_NewString (char *string
)
698 l
= strlen(string
) + 1;
699 new = Hunk_Alloc (l
);
702 for (i
=0 ; i
< l
; i
++)
704 if (string
[i
] == '\\' && i
< l
-1)
707 if (string
[i
] == 'n')
713 *new_p
++ = string
[i
];
724 Can parse either fields or globals
725 returns false if error
728 qboolean
ED_ParseEpair (void *base
, ddef_t
*key
, char *s
)
737 d
= (void *)((int *)base
+ key
->ofs
);
739 switch (key
->type
& ~DEF_SAVEGLOBAL
)
742 *(string_t
*)d
= ED_NewString (s
) - pr_strings
;
746 *(float *)d
= atof (s
);
753 for (i
=0 ; i
<3 ; i
++)
755 while (*v
&& *v
!= ' ')
758 ((float *)d
)[i
] = atof (w
);
764 *(int *)d
= EDICT_TO_PROG(EDICT_NUM(atoi (s
)));
768 def
= ED_FindField (s
);
771 Con_Printf ("Can't find field %s\n", s
);
774 *(int *)d
= G_INT(def
->ofs
);
778 func
= ED_FindFunction (s
);
781 Con_Printf ("Can't find function %s\n", s
);
784 *(func_t
*)d
= func
- pr_functions
;
797 Parses an edict out of the given string, returning the new position
798 ed should be a properly initialized empty edict.
799 Used for initial level load and for savegames.
802 char *ED_ParseEdict (char *data
, edict_t
*ent
)
813 if (ent
!= sv
.edicts
) // hack
814 memset (&ent
->v
, 0, progs
->entityfields
* 4);
816 // go through all the dictionary pairs
820 data
= COM_Parse (data
);
821 if (com_token
[0] == '}')
824 Sys_Error ("ED_ParseEntity: EOF without closing brace");
826 // anglehack is to allow QuakeEd to write single scalar angles
827 // and allow them to be turned into vectors. (FIXME...)
828 if (!strcmp(com_token
, "angle"))
830 strcpy (com_token
, "angles");
836 // FIXME: change light to _light to get rid of this hack
837 if (!strcmp(com_token
, "light"))
838 strcpy (com_token
, "light_lev"); // hack for single light def
840 strcpy (keyname
, com_token
);
842 // another hack to fix heynames with trailing spaces
844 while (n
&& keyname
[n
-1] == ' ')
851 data
= COM_Parse (data
);
853 Sys_Error ("ED_ParseEntity: EOF without closing brace");
855 if (com_token
[0] == '}')
856 Sys_Error ("ED_ParseEntity: closing brace without data");
860 // keynames with a leading underscore are used for utility comments,
861 // and are immediately discarded by quake
862 if (keyname
[0] == '_')
865 key
= ED_FindField (keyname
);
868 Con_Printf ("'%s' is not a field\n", keyname
);
875 strcpy (temp
, com_token
);
876 sprintf (com_token
, "0 %s 0", temp
);
879 if (!ED_ParseEpair ((void *)&ent
->v
, key
, com_token
))
880 Host_Error ("ED_ParseEdict: parse error");
894 The entities are directly placed in the array, rather than allocated with
895 ED_Alloc, because otherwise an error loading the map would have entity
896 number references out of order.
898 Creates a server's entity / program execution context by
899 parsing textual entity definitions out of an ent file.
901 Used for both fresh maps and savegame loads. A fresh map would also need
902 to call ED_CallSpawnFunctions () to let the objects initialize themselves.
905 void ED_LoadFromFile (char *data
)
913 pr_global_struct
->time
= sv
.time
;
918 // parse the opening brace
919 data
= COM_Parse (data
);
922 if (com_token
[0] != '{')
923 Sys_Error ("ED_LoadFromFile: found %s when expecting {",com_token
);
929 data
= ED_ParseEdict (data
, ent
);
931 // remove things from different skill levels or deathmatch
932 if (deathmatch
.value
)
934 if (((int)ent
->v
.spawnflags
& SPAWNFLAG_NOT_DEATHMATCH
))
941 else if ((current_skill
== 0 && ((int)ent
->v
.spawnflags
& SPAWNFLAG_NOT_EASY
))
942 || (current_skill
== 1 && ((int)ent
->v
.spawnflags
& SPAWNFLAG_NOT_MEDIUM
))
943 || (current_skill
>= 2 && ((int)ent
->v
.spawnflags
& SPAWNFLAG_NOT_HARD
)) )
951 // immediately call spawn function
953 if (!ent
->v
.classname
)
955 Con_Printf ("No classname for:\n");
961 // look for the spawn function
962 func
= ED_FindFunction ( pr_strings
+ ent
->v
.classname
);
966 Con_Printf ("No spawn function for:\n");
972 pr_global_struct
->self
= EDICT_TO_PROG(ent
);
973 PR_ExecuteProgram (func
- pr_functions
);
976 Con_DPrintf ("%i entities inhibited\n", inhibit
);
985 void PR_LoadProgs (void)
989 // flush the non-C variable lookup cache
990 for (i
=0 ; i
<GEFV_CACHESIZE
; i
++)
991 gefvCache
[i
].field
[0] = 0;
995 progs
= (dprograms_t
*)COM_LoadHunkFile ("progs.dat");
997 Sys_Error ("PR_LoadProgs: couldn't load progs.dat");
998 Con_DPrintf ("Programs occupy %iK.\n", com_filesize
/1024);
1000 for (i
=0 ; i
<com_filesize
; i
++)
1001 CRC_ProcessByte (&pr_crc
, ((byte
*)progs
)[i
]);
1003 // byte swap the header
1004 for (i
=0 ; i
<sizeof(*progs
)/4 ; i
++)
1005 ((int *)progs
)[i
] = LittleLong ( ((int *)progs
)[i
] );
1007 if (progs
->version
!= PROG_VERSION
)
1008 Sys_Error ("progs.dat has wrong version number (%i should be %i)", progs
->version
, PROG_VERSION
);
1009 if (progs
->crc
!= PROGHEADER_CRC
)
1010 Sys_Error ("progs.dat system vars have been modified, progdefs.h is out of date");
1012 pr_functions
= (dfunction_t
*)((byte
*)progs
+ progs
->ofs_functions
);
1013 pr_strings
= (char *)progs
+ progs
->ofs_strings
;
1014 pr_globaldefs
= (ddef_t
*)((byte
*)progs
+ progs
->ofs_globaldefs
);
1015 pr_fielddefs
= (ddef_t
*)((byte
*)progs
+ progs
->ofs_fielddefs
);
1016 pr_statements
= (dstatement_t
*)((byte
*)progs
+ progs
->ofs_statements
);
1018 pr_global_struct
= (globalvars_t
*)((byte
*)progs
+ progs
->ofs_globals
);
1019 pr_globals
= (float *)pr_global_struct
;
1021 pr_edict_size
= progs
->entityfields
* 4 + sizeof (edict_t
) - sizeof(entvars_t
);
1023 // byte swap the lumps
1024 for (i
=0 ; i
<progs
->numstatements
; i
++)
1026 pr_statements
[i
].op
= LittleShort(pr_statements
[i
].op
);
1027 pr_statements
[i
].a
= LittleShort(pr_statements
[i
].a
);
1028 pr_statements
[i
].b
= LittleShort(pr_statements
[i
].b
);
1029 pr_statements
[i
].c
= LittleShort(pr_statements
[i
].c
);
1032 for (i
=0 ; i
<progs
->numfunctions
; i
++)
1034 pr_functions
[i
].first_statement
= LittleLong (pr_functions
[i
].first_statement
);
1035 pr_functions
[i
].parm_start
= LittleLong (pr_functions
[i
].parm_start
);
1036 pr_functions
[i
].s_name
= LittleLong (pr_functions
[i
].s_name
);
1037 pr_functions
[i
].s_file
= LittleLong (pr_functions
[i
].s_file
);
1038 pr_functions
[i
].numparms
= LittleLong (pr_functions
[i
].numparms
);
1039 pr_functions
[i
].locals
= LittleLong (pr_functions
[i
].locals
);
1042 for (i
=0 ; i
<progs
->numglobaldefs
; i
++)
1044 pr_globaldefs
[i
].type
= LittleShort (pr_globaldefs
[i
].type
);
1045 pr_globaldefs
[i
].ofs
= LittleShort (pr_globaldefs
[i
].ofs
);
1046 pr_globaldefs
[i
].s_name
= LittleLong (pr_globaldefs
[i
].s_name
);
1049 for (i
=0 ; i
<progs
->numfielddefs
; i
++)
1051 pr_fielddefs
[i
].type
= LittleShort (pr_fielddefs
[i
].type
);
1052 if (pr_fielddefs
[i
].type
& DEF_SAVEGLOBAL
)
1053 Sys_Error ("PR_LoadProgs: pr_fielddefs[i].type & DEF_SAVEGLOBAL");
1054 pr_fielddefs
[i
].ofs
= LittleShort (pr_fielddefs
[i
].ofs
);
1055 pr_fielddefs
[i
].s_name
= LittleLong (pr_fielddefs
[i
].s_name
);
1058 for (i
=0 ; i
<progs
->numglobals
; i
++)
1059 ((int *)pr_globals
)[i
] = LittleLong (((int *)pr_globals
)[i
]);
1070 Cmd_AddCommand ("edict", ED_PrintEdict_f
);
1071 Cmd_AddCommand ("edicts", ED_PrintEdicts
);
1072 Cmd_AddCommand ("edictcount", ED_Count
);
1073 Cmd_AddCommand ("profile", PR_Profile_f
);
1074 Cvar_RegisterVariable (&nomonsters
);
1075 Cvar_RegisterVariable (&gamecfg
);
1076 Cvar_RegisterVariable (&scratch1
);
1077 Cvar_RegisterVariable (&scratch2
);
1078 Cvar_RegisterVariable (&scratch3
);
1079 Cvar_RegisterVariable (&scratch4
);
1080 Cvar_RegisterVariable (&savedgamecfg
);
1081 Cvar_RegisterVariable (&saved1
);
1082 Cvar_RegisterVariable (&saved2
);
1083 Cvar_RegisterVariable (&saved3
);
1084 Cvar_RegisterVariable (&saved4
);
1089 edict_t
*EDICT_NUM(int n
)
1091 if (n
< 0 || n
>= sv
.max_edicts
)
1092 Sys_Error ("EDICT_NUM: bad number %i", n
);
1093 return (edict_t
*)((byte
*)sv
.edicts
+ (n
)*pr_edict_size
);
1096 int NUM_FOR_EDICT(edict_t
*e
)
1100 b
= (byte
*)e
- (byte
*)sv
.edicts
;
1101 b
= b
/ pr_edict_size
;
1103 if (b
< 0 || b
>= sv
.num_edicts
)
1104 Sys_Error ("NUM_FOR_EDICT: bad pointer");