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.
21 This is a try to make the vm more generic, it is mainly based on the progs.h file.
22 For the license refer to progs.h.
24 Generic means, less as possible hard-coded links with the other parts of the engine.
25 This means no edict_engineprivate struct usage, etc.
26 The code uses void pointers instead.
32 #include "pr_comp.h" // defs shared with qcc
33 #include "progdefs.h" // generated by program cdefs
34 #include "clprogdefs.h" // generated by program cdefs
36 typedef struct prvm_stack_s
43 typedef union prvm_eval_s
54 typedef struct prvm_required_field_s
58 } prvm_required_field_t;
61 // AK: I dont call it engine private cause it doesnt really belongs to the engine
62 // it belongs to prvm.
63 typedef struct prvm_edict_private_s
67 } prvm_edict_private_t;
69 typedef struct prvm_edict_s
71 // engine-private fields (stored in dynamically resized array)
72 //edict_engineprivate_t *e;
75 prvm_edict_private_t *required;
77 // FIXME: this server pointer really means world, not server
78 // (it is used by both server qc and client qc, but not menu qc)
79 edict_engineprivate_t *server;
80 // add other private structs as you desire
81 // new structs have to start with the elements of prvm_edit_private_t
82 // e.g. a new struct has to either look like this:
83 // typedef struct server_edict_private_s {
84 // prvm_edict_private_t base;
86 // vec3_t moved_fromangles;
87 // ... } server_edict_private_t;
89 // typedef struct server_edict_private_s {
93 // vec3_t moved_fromangles;
94 // ... } server_edict_private_t;
95 // However, the first one should be preferred.
97 // QuakeC fields (stored in dynamically resized array)
102 cl_entvars_t *client;
106 #define PRVM_EDICTFIELDVALUE(ed, fieldoffset) (fieldoffset >= 0 ? (prvm_eval_t *)((int *)ed->fields.vp + fieldoffset) : NULL)
107 #define PRVM_GLOBALFIELDVALUE(fieldoffset) (fieldoffset >= 0 ? (prvm_eval_t *)((int *)prog->globals.generic + fieldoffset) : NULL)
109 //============================================================================
110 #define PRVM_OP_STATE 1
112 #define PRVM_MAX_STACK_DEPTH 1024
113 #define PRVM_LOCALSTACK_SIZE 16384
115 #define PRVM_MAX_OPENFILES 256
116 #define PRVM_MAX_OPENSEARCHES 128
118 typedef void (*prvm_builtin_t) (void);
120 // NOTE: field offsets use -1 for NULL
121 typedef struct prvm_prog_fieldoffsets_s
123 // server and client use a lot of similar fields, so this is combined
124 int SendEntity; // ssqc
126 int alpha; // ssqc / csqc
127 int ammo_cells1; // ssqc - Dissolution of Eternity mission pack
128 int ammo_lava_nails; // ssqc - Dissolution of Eternity mission pack
129 int ammo_multi_rockets; // ssqc - Dissolution of Eternity mission pack
130 int ammo_nails1; // ssqc - Dissolution of Eternity mission pack
131 int ammo_plasma; // ssqc - Dissolution of Eternity mission pack
132 int ammo_rockets1; // ssqc - Dissolution of Eternity mission pack
133 int ammo_shells1; // ssqc - Dissolution of Eternity mission pack
134 int angles; // common - used by changeyaw/changepitch
142 int button10; // ssqc
143 int button11; // ssqc
144 int button12; // ssqc
145 int button13; // ssqc
146 int button14; // ssqc
147 int button15; // ssqc
148 int button16; // ssqc
149 int buttonchat; // ssqc
150 int buttonuse; // ssqc
151 int chain; // common - used by find builtins
152 int classname; // common
153 int clientcolors; // ssqc
155 int colormod; // ssqc / csqc
156 int contentstransition; // ssqc
157 int cursor_active; // ssqc
158 int cursor_screen; // ssqc
159 int cursor_trace_endpos; // ssqc
160 int cursor_trace_ent; // ssqc
161 int cursor_trace_start; // ssqc
162 int customizeentityforclient; // ssqc
163 int dimension_hit; // ssqc / csqc
164 int dimension_solid; // ssqc / csqc
165 int disableclientprediction; // ssqc
166 int dphitcontentsmask; // ssqc / csqc
167 int drawonlytoclient; // ssqc
168 int effects; // ssqc / csqc
169 int exteriormodeltoclient; // ssqc
170 int fatness; // ssqc / csqc
171 int forceshader; // csqc
172 int frame1time; // csqc
174 int frame2time; // csqc
175 int frame; // common - used by OP_STATE
176 int fullbright; // ssqc - Nehahra support
177 int glow_color; // ssqc
178 int glow_size; // ssqc
179 int glow_trail; // ssqc
181 int groundentity; // ssqc / csqc
182 int hull; // ssqc / csqc
183 int ideal_yaw; // ssqc / csqc
184 int idealpitch; // ssqc / csqc
186 int lerpfrac; // csqc
187 int light_lev; // ssqc
189 int modelflags; // ssqc
190 int movement; // ssqc
191 int nextthink; // common - used by OP_STATE
192 int netaddress; // ssqc
193 int nodrawtoclient; // ssqc
196 int pitch_speed; // ssqc / csqc
197 int playermodel; // ssqc
198 int playerskin; // ssqc
200 int punchvector; // ssqc
201 int renderamt; // ssqc - HalfLife support
202 int renderflags; // csqc
203 int rendermode; // ssqc - HalfLife support
204 int scale; // ssqc / csqc
206 int tag_entity; // ssqc / csqc
207 int tag_index; // ssqc / csqc
208 int think; // common - used by OP_STATE
209 int viewmodelforclient; // ssqc
210 int viewzoom; // ssqc
211 int yaw_speed; // ssqc / csqc
213 prvm_prog_fieldoffsets_t;
215 // NOTE: global offsets use -1 for NULL
216 typedef struct prvm_prog_globaloffsets_s
218 // server and client use a lot of similar globals, so this is combined
219 int SV_InitCmd; // ssqc
221 int time; // ssqc / csqc
222 int v_forward; // ssqc / csqc
223 int v_right; // ssqc / csqc
224 int v_up; // ssqc / csqc
225 int view_angles; // csqc
226 int trace_allsolid; // ssqc / csqc
227 int trace_startsolid; // ssqc / csqc
228 int trace_fraction; // ssqc / csqc
229 int trace_inwater; // ssqc / csqc
230 int trace_inopen; // ssqc / csqc
231 int trace_endpos; // ssqc / csqc
232 int trace_plane_normal; // ssqc / csqc
233 int trace_plane_dist; // ssqc / csqc
234 int trace_ent; // ssqc / csqc
235 int trace_dphitcontents; // ssqc / csqc
236 int trace_dphitq3surfaceflags; // ssqc / csqc
237 int trace_dphittexturename; // ssqc / csqc
238 int trace_dpstartcontents; // ssqc / csqc
239 int intermission; // csqc
241 int deathmatch; // csqc
242 int dmg_take; // csqc
243 int dmg_save; // csqc
244 int dmg_origin; // csqc
245 int sb_showscores; // csqc
248 prvm_prog_globaloffsets_t;
250 // these are initialized using PRVM_ED_FindFunction
251 // NOTE: function offsets use 0 for NULL
252 typedef struct prvm_prog_funcoffsets_s
254 func_t CSQC_ConsoleCommand; // csqc
255 func_t CSQC_Ent_Remove; // csqc
256 func_t CSQC_Ent_Update; // csqc
257 func_t CSQC_Event; // csqc [515]: engine call this for its own needs so csqc can do some things according to what engine it's running on. example: to say about edicts increase, whatever...
258 func_t CSQC_Event_Sound; // csqc : called by engine when an incoming sound packet arrives so CSQC can act on it
259 func_t CSQC_Init; // csqc
260 func_t CSQC_InputEvent; // csqc
261 func_t CSQC_Parse_CenterPrint; // csqc
262 func_t CSQC_Parse_Print; // csqc
263 func_t CSQC_Parse_StuffCmd; // csqc
264 func_t CSQC_Parse_TempEntity; // csqc [515]: very helpfull when you want to create your own particles/decals/etc for effects that already exist
265 func_t CSQC_Shutdown; // csqc
266 func_t CSQC_UpdateView; // csqc
267 func_t EndFrame; // ssqc
268 func_t RestoreGame; // ssqc
269 func_t SV_ChangeTeam; // ssqc
270 func_t SV_ParseClientCommand; // ssqc
271 func_t SV_PlayerPhysics; // ssqc
272 func_t SV_OnEntityNoSpawnFunction; // ssqc
273 func_t GameCommand; // any
275 // menu qc only uses some functions, nothing else
276 func_t m_display; // mqc
277 func_t m_draw; // mqc
278 func_t m_hide; // mqc
279 func_t m_init; // mqc
280 func_t m_keydown; // mqc
281 func_t m_keyup; // mqc
282 func_t m_shutdown; // mqc
283 func_t m_toggle; // mqc
285 prvm_prog_funcoffsets_t;
287 // [INIT] variables flagged with this token can be initialized by 'you'
288 // NOTE: external code has to create and free the mempools but everything else is done by prvm !
289 typedef struct prvm_prog_s
293 mfunction_t *functions;
298 dstatement_t *statements;
299 int edict_size; // in bytes
300 int edictareasize; // LordHavoc: in bytes (for bound checking)
302 int *statement_linenums; // NULL if not available
304 double *statement_profile; // only incremented if prvm_statementprofiling is on
308 globalvars_t *server;
309 cl_globalvars_t *client;
314 // this is updated whenever a string is removed or added
315 // (simple optimization of the free string search)
316 int firstfreeknownstring;
317 const char **knownstrings;
318 unsigned char *knownstrings_freeable;
319 const char ***stringshash;
321 // all memory allocations related to this vm_prog (code, edicts, strings)
322 mempool_t *progs_mempool; // [INIT]
324 prvm_builtin_t *builtins; // [INIT]
325 int numbuiltins; // [INIT]
330 mfunction_t *xfunction;
333 // stacktrace writes into stack[MAX_STACK_DEPTH]
334 // thus increase the array, so depth wont be overwritten
335 prvm_stack_t stack[PRVM_MAX_STACK_DEPTH+1];
338 int localstack[PRVM_LOCALSTACK_SIZE];
341 unsigned short headercrc; // [INIT]
343 unsigned short filecrc;
345 //============================================================================
346 // until this point everything also exists (with the pr_ prefix) in the old vm
348 qfile_t *openfiles[PRVM_MAX_OPENFILES];
349 fssearch_t *opensearches[PRVM_MAX_OPENSEARCHES];
351 // copies of some vars that were former read from sv
353 // number of edicts for which space has been (should be) allocated
354 int max_edicts; // [INIT]
355 // used instead of the constant MAX_EDICTS
356 int limit_edicts; // [INIT]
358 // number of reserved edicts (allocated from 1)
359 int reserved_edicts; // [INIT]
361 prvm_edict_t *edicts;
365 // size of the engine private struct
366 int edictprivate_size; // [INIT]
368 prvm_prog_fieldoffsets_t fieldoffsets;
369 prvm_prog_globaloffsets_t globaloffsets;
370 prvm_prog_funcoffsets_t funcoffsets;
372 // allow writing to world entity fields, this is set by server init and
373 // cleared before first server frame
374 qboolean allowworldwrites;
376 // name of the prog, e.g. "Server", "Client" or "Menu" (used for text output)
377 char *name; // [INIT]
379 // flag - used to store general flags like PRVM_GE_SELF, etc.
382 char *extensionstring; // [INIT]
384 qboolean loadintoworld; // [INIT]
386 // used to indicate whether a prog is loaded
389 // prvm_builtin_mem_t *mem_list;
391 // now passed as parameter of PRVM_LoadProgs
392 // char **required_func;
393 // int numrequiredfunc;
395 //============================================================================
397 ddef_t *self; // if self != 0 then there is a global self
399 //============================================================================
402 void (*begin_increase_edicts)(void); // [INIT] used by PRVM_MEM_Increase_Edicts
403 void (*end_increase_edicts)(void); // [INIT]
405 void (*init_edict)(prvm_edict_t *edict); // [INIT] used by PRVM_ED_ClearEdict
406 void (*free_edict)(prvm_edict_t *ed); // [INIT] used by PRVM_ED_Free
408 void (*count_edicts)(void); // [INIT] used by PRVM_ED_Count_f
410 qboolean (*load_edict)(prvm_edict_t *ent); // [INIT] used by PRVM_ED_LoadFromFile
412 void (*init_cmd)(void); // [INIT] used by PRVM_InitProg
413 void (*reset_cmd)(void); // [INIT] used by PRVM_ResetProg
415 void (*error_cmd)(const char *format, ...) DP_FUNC_PRINTF(1); // [INIT]
419 extern prvm_prog_t * prog;
421 #define PRVM_MAXPROGS 3
422 #define PRVM_SERVERPROG 0 // actually not used at the moment
423 #define PRVM_CLIENTPROG 1
424 #define PRVM_MENUPROG 2
426 extern prvm_prog_t prvm_prog_list[PRVM_MAXPROGS];
428 //============================================================================
431 extern prvm_builtin_t vm_sv_builtins[];
432 extern prvm_builtin_t vm_cl_builtins[];
433 extern prvm_builtin_t vm_m_builtins[];
435 extern const int vm_sv_numbuiltins;
436 extern const int vm_cl_numbuiltins;
437 extern const int vm_m_numbuiltins;
439 extern char * vm_sv_extensions; // client also uses this
440 extern char * vm_m_extensions;
442 void VM_SV_Cmd_Init(void);
443 void VM_SV_Cmd_Reset(void);
445 void VM_CL_Cmd_Init(void);
446 void VM_CL_Cmd_Reset(void);
448 void VM_M_Cmd_Init(void);
449 void VM_M_Cmd_Reset(void);
451 void VM_Cmd_Init(void);
452 void VM_Cmd_Reset(void);
453 //============================================================================
455 void PRVM_Init (void);
457 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage);
459 #define PRVM_Alloc(buffersize) _PRVM_Alloc(buffersize, __FILE__, __LINE__)
460 #define PRVM_Free(buffer) _PRVM_Free(buffer, __FILE__, __LINE__)
461 #define PRVM_FreeAll() _PRVM_FreeAll(__FILE__, __LINE__)
462 void *_PRVM_Alloc (size_t buffersize, const char *filename, int fileline);
463 void _PRVM_Free (void *buffer, const char *filename, int fileline);
464 void _PRVM_FreeAll (const char *filename, int fileline);
466 void PRVM_Profile (int maxfunctions, int mininstructions);
467 void PRVM_Profile_f (void);
468 void PRVM_CallProfile_f (void);
469 void PRVM_PrintFunction_f (void);
471 void PRVM_PrintState(void);
472 void PRVM_CrashAll (void);
473 void PRVM_Crash (void);
475 ddef_t *PRVM_ED_FindField(const char *name);
476 ddef_t *PRVM_ED_FindGlobal(const char *name);
477 mfunction_t *PRVM_ED_FindFunction(const char *name);
479 int PRVM_ED_FindFieldOffset(const char *name);
480 int PRVM_ED_FindGlobalOffset(const char *name);
481 func_t PRVM_ED_FindFunctionOffset(const char *name);
482 #define PRVM_ED_FindFieldOffset_FromStruct(st, field) prog->fieldoffsets . field = ((int *)(&((st *)NULL)-> field ) - ((int *)NULL))
483 #define PRVM_ED_FindGlobalOffset_FromStruct(st, field) prog->globaloffsets . field = ((int *)(&((st *)NULL)-> field ) - ((int *)NULL))
485 void PRVM_MEM_IncreaseEdicts(void);
487 prvm_edict_t *PRVM_ED_Alloc (void);
488 void PRVM_ED_Free (prvm_edict_t *ed);
489 void PRVM_ED_ClearEdict (prvm_edict_t *e);
491 void PRVM_PrintFunctionStatements (const char *name);
492 void PRVM_ED_Print(prvm_edict_t *ed, const char *wildcard_fieldname);
493 void PRVM_ED_Write (qfile_t *f, prvm_edict_t *ed);
494 const char *PRVM_ED_ParseEdict (const char *data, prvm_edict_t *ent);
496 void PRVM_ED_WriteGlobals (qfile_t *f);
497 void PRVM_ED_ParseGlobals (const char *data);
499 void PRVM_ED_LoadFromFile (const char *data);
501 prvm_edict_t *PRVM_EDICT_NUM_ERROR(int n, char *filename, int fileline);
502 #define PRVM_EDICT_NUM(n) (((unsigned)(n) < (unsigned int)prog->max_edicts) ? prog->edicts + (n) : PRVM_EDICT_NUM_ERROR(n, __FILE__, __LINE__))
504 //int NUM_FOR_EDICT_ERROR(prvm_edict_t *e);
505 #define PRVM_NUM_FOR_EDICT(e) ((int)((prvm_edict_t *)(e) - prog->edicts))
506 //int PRVM_NUM_FOR_EDICT(prvm_edict_t *e);
508 #define PRVM_NEXT_EDICT(e) ((e) + 1)
510 #define PRVM_EDICT_TO_PROG(e) (PRVM_NUM_FOR_EDICT(e))
511 //int PRVM_EDICT_TO_PROG(prvm_edict_t *e);
512 #define PRVM_PROG_TO_EDICT(n) (PRVM_EDICT_NUM(n))
513 //prvm_edict_t *PRVM_PROG_TO_EDICT(int n);
515 //============================================================================
517 #define PRVM_G_FLOAT(o) (prog->globals.generic[o])
518 #define PRVM_G_INT(o) (*(int *)&prog->globals.generic[o])
519 #define PRVM_G_EDICT(o) (PRVM_PROG_TO_EDICT(*(int *)&prog->globals.generic[o]))
520 #define PRVM_G_EDICTNUM(o) PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(o))
521 #define PRVM_G_VECTOR(o) (&prog->globals.generic[o])
522 #define PRVM_G_STRING(o) (PRVM_GetString(*(string_t *)&prog->globals.generic[o]))
523 //#define PRVM_G_FUNCTION(o) (*(func_t *)&prog->globals.generic[o])
525 // FIXME: make these go away?
526 #define PRVM_E_FLOAT(e,o) (((float*)e->fields.vp)[o])
527 #define PRVM_E_INT(e,o) (((int*)e->fields.vp)[o])
528 //#define PRVM_E_VECTOR(e,o) (&((float*)e->fields.vp)[o])
529 #define PRVM_E_STRING(e,o) (PRVM_GetString(*(string_t *)&((float*)e->fields.vp)[o]))
531 extern int prvm_type_size[8]; // for consistency : I think a goal of this sub-project is to
532 // make the new vm mostly independent from the old one, thus if it's necessary, I copy everything
534 void PRVM_Init_Exec(void);
536 void PRVM_ED_PrintEdicts_f (void);
537 void PRVM_ED_PrintNum (int ent, const char *wildcard_fieldname);
539 const char *PRVM_GetString(int num);
540 int PRVM_SetEngineString(const char *s);
541 int PRVM_SetTempString(const char *s);
542 int PRVM_AllocString(size_t bufferlength, char **pointer);
543 void PRVM_FreeString(int num);
545 //============================================================================
547 // used as replacement for a prog stack
548 //#define PRVM_DEBUGPRSTACK
550 #ifdef PRVM_DEBUGPRSTACK
551 #define PRVM_Begin if(prog != 0) Con_Printf("prog not 0(prog = %i) in file: %s line: %i!\n", PRVM_GetProgNr(), __FILE__, __LINE__)
552 #define PRVM_End prog = 0
555 #define PRVM_End prog = 0
558 //#define PRVM_SAFENAME
559 #ifndef PRVM_SAFENAME
560 # define PRVM_NAME (prog->name)
562 # define PRVM_NAME (prog->name ? prog->name : "Unknown prog name")
565 // helper macro to make function pointer calls easier
566 #define PRVM_GCALL(func) if(prog->func) prog->func
568 #define PRVM_ERROR prog->error_cmd
570 // other prog handling functions
571 qboolean PRVM_SetProgFromString(const char *str);
572 void PRVM_SetProg(int prognr);
576 Call InitProg with the num
577 Set up the fields marked with [INIT] in the prog struct
578 Load a program with LoadProgs
580 void PRVM_InitProg(int prognr);
581 // LoadProgs expects to be called right after InitProg
582 void PRVM_LoadProgs (const char *filename, int numrequiredfunc, char **required_func, int numrequiredfields, prvm_required_field_t *required_field, int numrequiredglobals, char **required_global);
583 void PRVM_ResetProg(void);
585 qboolean PRVM_ProgLoaded(int prognr);
587 int PRVM_GetProgNr(void);
589 void VM_Warning(const char *fmt, ...) DP_FUNC_PRINTF(1);
591 // TODO: fill in the params
592 //void PRVM_Create();