]> icculus.org git repositories - divverent/darkplaces.git/blob - progsvm.h
allow non-integer framerates for q3 shader animmap commands
[divverent/darkplaces.git] / progsvm.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 /*
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.
23
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.
27 */
28
29 #ifndef PROGSVM_H
30 #define PROGSVM_H
31
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
35
36 typedef struct prvm_stack_s
37 {
38         int                             s;
39         mfunction_t             *f;
40 } prvm_stack_t;
41
42
43 typedef union prvm_eval_s
44 {
45         string_t                string;
46         float                   _float;
47         float                   vector[3];
48         func_t                  function;
49         int                             ivector[3];
50         int                             _int;
51         int                             edict;
52 } prvm_eval_t;
53
54 typedef struct prvm_required_field_s
55 {
56         int type;
57         const char *name;
58 } prvm_required_field_t;
59
60
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
64 {
65         qboolean free;
66         float freetime;
67 } prvm_edict_private_t;
68
69 typedef struct prvm_edict_s
70 {
71         // engine-private fields (stored in dynamically resized array)
72         //edict_engineprivate_t *e;
73         union
74         {
75                 prvm_edict_private_t *required;
76                 void *vp;
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;
85                 //              vec3_t moved_from;
86                 //      vec3_t moved_fromangles;
87                 //              ... } server_edict_private_t;
88                 // or:
89                 //      typedef struct server_edict_private_s {
90                 //              qboolean free;
91                 //              float freetime;
92                 //              vec3_t moved_from;
93                 //      vec3_t moved_fromangles;
94                 //              ... } server_edict_private_t;
95                 // However, the first one should be preferred.
96         } priv;
97         // QuakeC fields (stored in dynamically resized array)
98         union
99         {
100                 void *vp;
101                 entvars_t               *server;
102                 cl_entvars_t    *client;
103         } fields;
104 } prvm_edict_t;
105
106 #define PRVM_GETEDICTFIELDVALUE(ed, fieldoffset) (fieldoffset >= 0 ? (prvm_eval_t *)((unsigned char *)ed->fields.vp + fieldoffset) : NULL)
107 #define PRVM_GETGLOBALFIELDVALUE(fieldoffset) (fieldoffset >= 0 ? (prvm_eval_t *)((unsigned char *)prog->globals.generic + fieldoffset) : NULL)
108
109 //============================================================================
110 #define PRVM_OP_STATE           1
111
112 #define PRVM_MAX_STACK_DEPTH            1024
113 #define PRVM_LOCALSTACK_SIZE            16384
114
115 #define PRVM_MAX_OPENFILES 256
116 #define PRVM_MAX_OPENSEARCHES 128
117
118 typedef void (*prvm_builtin_t) (void);
119
120 // NOTE: field offsets use -1 for NULL
121 typedef struct prvm_prog_fieldoffsets_s
122 {
123         int classname; // common
124         int chain; // common - used by find builtins
125         int think; // common - used by OP_STATE
126         int nextthink; // common - used by OP_STATE
127         int frame; // common - used by OP_STATE
128         int angles; // common - used by changeyaw/changepitch
129
130         int gravity; // ssqc
131         int button3; // ssqc
132         int button4; // ssqc
133         int button5; // ssqc
134         int button6; // ssqc
135         int button7; // ssqc
136         int button8; // ssqc
137         int button9; // ssqc
138         int button10; // ssqc
139         int button11; // ssqc
140         int button12; // ssqc
141         int button13; // ssqc
142         int button14; // ssqc
143         int button15; // ssqc
144         int button16; // ssqc
145         int buttonuse; // ssqc
146         int buttonchat; // ssqc
147         int glow_size; // ssqc
148         int glow_trail; // ssqc
149         int glow_color; // ssqc
150         int items2; // ssqc
151         int scale; // ssqc / csqc
152         int alpha; // ssqc / csqc
153         int renderamt; // ssqc - HalfLife support
154         int rendermode; // ssqc - HalfLife support
155         int fullbright; // ssqc - Nehahra support
156         int ammo_shells1; // ssqc - Dissolution of Eternity mission pack
157         int ammo_nails1; // ssqc - Dissolution of Eternity mission pack
158         int ammo_lava_nails; // ssqc - Dissolution of Eternity mission pack
159         int ammo_rockets1; // ssqc - Dissolution of Eternity mission pack
160         int ammo_multi_rockets; // ssqc - Dissolution of Eternity mission pack
161         int ammo_cells1; // ssqc - Dissolution of Eternity mission pack
162         int ammo_plasma; // ssqc - Dissolution of Eternity mission pack
163         int ideal_yaw; // ssqc / csqc
164         int yaw_speed; // ssqc / csqc
165         int idealpitch; // ssqc / csqc
166         int pitch_speed; // ssqc / csqc
167         int viewmodelforclient; // ssqc
168         int nodrawtoclient; // ssqc
169         int exteriormodeltoclient; // ssqc
170         int drawonlytoclient; // ssqc
171         int ping; // ssqc
172         int movement; // ssqc
173         int pmodel; // ssqc
174         int punchvector; // ssqc
175         int viewzoom; // ssqc
176         int clientcolors; // ssqc
177         int tag_entity; // ssqc
178         int tag_index; // ssqc
179         int light_lev; // ssqc
180         int color; // ssqc
181         int style; // ssqc
182         int pflags; // ssqc
183         int cursor_active; // ssqc
184         int cursor_screen; // ssqc
185         int cursor_trace_start; // ssqc
186         int cursor_trace_endpos; // ssqc
187         int cursor_trace_ent; // ssqc
188         int colormod; // ssqc / csqc
189         int playermodel; // ssqc
190         int playerskin; // ssqc
191         int SendEntity; // ssqc
192         int Version; // ssqc
193         int customizeentityforclient; // ssqc
194         int dphitcontentsmask; // ssqc
195         int contentstransition; // ssqc
196         //int fatness; // ssqc / csqc
197         //int dimension_hit; // ssqc / csqc
198         //int dimension_solid; // ssqc / csqc
199
200         // csqc
201         int frame2;
202         int frame1time;
203         int frame2time;
204         int lerpfrac;
205         int renderflags;
206         //int forceshader;
207         int groundentity;
208         //int hull;
209         int effects;
210         //int tag_entity;
211         //int tag_index;
212         //int dphitcontentsmask;
213
214         // mqc
215 }
216 prvm_prog_fieldoffsets_t;
217
218 // NOTE: global offsets use -1 for NULL
219 typedef struct prvm_prog_globaloffsets_s
220 {
221         // common
222         int self;
223         int time;
224
225         // ssqc
226         int trace_dpstartcontents;
227         int trace_dphitcontents;
228         int trace_dphitq3surfaceflags;
229         int trace_dphittexturename;
230         int SV_InitCmd;
231
232         // csqc
233
234         // mqc
235 }
236 prvm_prog_globaloffsets_t;
237
238 // these are initialized using PRVM_ED_FindFunction
239 // NOTE: function offsets use 0 for NULL
240 typedef struct prvm_prog_funcoffsets_s
241 {
242         // common
243
244         // ssqc
245         func_t SV_PlayerPhysics;
246         func_t SV_ParseClientCommand;
247         func_t SV_ChangeTeam;
248         func_t EndFrame;
249         func_t RestoreGame;
250
251         // csqc
252         func_t CSQC_Init;
253         func_t CSQC_InputEvent;
254         func_t CSQC_UpdateView;
255         func_t CSQC_ConsoleCommand;
256         func_t CSQC_Shutdown;
257         func_t CSQC_Parse_TempEntity; //[515]: very helpfull when you want to create your own particles/decals/etc for effects that already exist
258         func_t CSQC_Parse_StuffCmd;
259         func_t CSQC_Parse_Print;
260         func_t CSQC_Parse_CenterPrint;
261         func_t CSQC_Ent_Update;
262         func_t CSQC_Ent_Remove;
263         func_t CSQC_Event; //[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...
264
265         // mqc
266         func_t m_init;
267 #ifdef NG_MENU
268         func_t m_display;
269         func_t m_hide;
270 #endif
271         func_t m_keydown;
272         func_t m_keyup;
273         func_t m_draw;
274         func_t m_toggle;
275         func_t m_shutdown;
276 }
277 prvm_prog_funcoffsets_t;
278
279 // [INIT] variables flagged with this token can be initialized by 'you'
280 // NOTE: external code has to create and free the mempools but everything else is done by prvm !
281 typedef struct prvm_prog_s
282 {
283         dprograms_t                     *progs;
284         mfunction_t                     *functions;
285         char                            *strings;
286         int                                     stringssize;
287         ddef_t                          *fielddefs;
288         ddef_t                          *globaldefs;
289         dstatement_t            *statements;
290         int                                     edict_size;                     // in bytes
291         int                                     edictareasize;          // LordHavoc: in bytes (for bound checking)
292
293         int                                     *statement_linenums; // NULL if not available
294
295         double                          *statement_profile; // only incremented if prvm_statementprofiling is on
296
297         union {
298                 float *generic;
299                 globalvars_t *server;
300                 cl_globalvars_t *client;
301         } globals;
302
303         int                                     maxknownstrings;
304         int                                     numknownstrings;
305         // this is updated whenever a string is removed or added
306         // (simple optimization of the free string search)
307         int                                     firstfreeknownstring;
308         const char                      **knownstrings;
309         unsigned char                           *knownstrings_freeable;
310         const char                      ***stringshash;
311
312         // all memory allocations related to this vm_prog (code, edicts, strings)
313         mempool_t                       *progs_mempool; // [INIT]
314
315         prvm_builtin_t          *builtins; // [INIT]
316         int                                     numbuiltins; // [INIT]
317
318         int                                     argc;
319
320         int                                     trace;
321         mfunction_t                     *xfunction;
322         int                                     xstatement;
323
324         // stacktrace writes into stack[MAX_STACK_DEPTH]
325         // thus increase the array, so depth wont be overwritten
326         prvm_stack_t            stack[PRVM_MAX_STACK_DEPTH+1];
327         int                                     depth;
328
329         int                                     localstack[PRVM_LOCALSTACK_SIZE];
330         int                                     localstack_used;
331
332         unsigned short          headercrc; // [INIT]
333
334         unsigned short          filecrc;
335
336         //============================================================================
337         // until this point everything also exists (with the pr_ prefix) in the old vm
338
339         qfile_t                         *openfiles[PRVM_MAX_OPENFILES];
340         fssearch_t                      *opensearches[PRVM_MAX_OPENSEARCHES];
341
342         // copies of some vars that were former read from sv
343         int                                     num_edicts;
344         // number of edicts for which space has been (should be) allocated
345         int                                     max_edicts; // [INIT]
346         // used instead of the constant MAX_EDICTS
347         int                                     limit_edicts; // [INIT]
348
349         // number of reserved edicts (allocated from 1)
350         int                                     reserved_edicts; // [INIT]
351
352         prvm_edict_t            *edicts;
353         void                            *edictsfields;
354         void                            *edictprivate;
355
356         // size of the engine private struct
357         int                                     edictprivate_size; // [INIT]
358
359         prvm_prog_fieldoffsets_t        fieldoffsets;
360         prvm_prog_globaloffsets_t       globaloffsets;
361         prvm_prog_funcoffsets_t         funcoffsets;
362
363         // allow writing to world entity fields, this is set by server init and
364         // cleared before first server frame
365         qboolean                        allowworldwrites;
366
367         // name of the prog, e.g. "Server", "Client" or "Menu" (used for text output)
368         char                            *name; // [INIT]
369
370         // flag - used to store general flags like PRVM_GE_SELF, etc.
371         int                                     flag;
372
373         char                            *extensionstring; // [INIT]
374
375         qboolean                        loadintoworld; // [INIT]
376
377         // used to indicate whether a prog is loaded
378         qboolean                        loaded;
379
380 //      prvm_builtin_mem_t  *mem_list;
381
382 // now passed as parameter of PRVM_LoadProgs
383 //      char                            **required_func;
384 //      int                                     numrequiredfunc;
385
386         //============================================================================
387
388         ddef_t                          *self; // if self != 0 then there is a global self
389
390         //============================================================================
391         // function pointers
392
393         void                            (*begin_increase_edicts)(void); // [INIT] used by PRVM_MEM_Increase_Edicts
394         void                            (*end_increase_edicts)(void); // [INIT]
395
396         void                            (*init_edict)(prvm_edict_t *edict); // [INIT] used by PRVM_ED_ClearEdict
397         void                            (*free_edict)(prvm_edict_t *ed); // [INIT] used by PRVM_ED_Free
398
399         void                            (*count_edicts)(void); // [INIT] used by PRVM_ED_Count_f
400
401         qboolean                        (*load_edict)(prvm_edict_t *ent); // [INIT] used by PRVM_ED_LoadFromFile
402
403         void                            (*init_cmd)(void); // [INIT] used by PRVM_InitProg
404         void                            (*reset_cmd)(void); // [INIT] used by PRVM_ResetProg
405
406         void                            (*error_cmd)(const char *format, ...) DP_FUNC_PRINTF(1); // [INIT]
407
408 } prvm_prog_t;
409
410 extern prvm_prog_t * prog;
411
412 #define PRVM_MAXPROGS 3
413 #define PRVM_SERVERPROG 0 // actually not used at the moment
414 #define PRVM_CLIENTPROG 1
415 #define PRVM_MENUPROG   2
416
417 extern prvm_prog_t prvm_prog_list[PRVM_MAXPROGS];
418
419 //============================================================================
420 // prvm_cmds part
421
422 extern prvm_builtin_t vm_sv_builtins[];
423 extern prvm_builtin_t vm_cl_builtins[];
424 extern prvm_builtin_t vm_m_builtins[];
425
426 extern const int vm_sv_numbuiltins;
427 extern const int vm_cl_numbuiltins;
428 extern const int vm_m_numbuiltins;
429
430 extern char * vm_sv_extensions;
431 extern char * vm_cl_extensions;
432 extern char * vm_m_extensions;
433
434 void VM_SV_Cmd_Init(void);
435 void VM_SV_Cmd_Reset(void);
436
437 void VM_CL_Cmd_Init(void);
438 void VM_CL_Cmd_Reset(void);
439
440 void VM_M_Cmd_Init(void);
441 void VM_M_Cmd_Reset(void);
442
443 void VM_Cmd_Init(void);
444 void VM_Cmd_Reset(void);
445 //============================================================================
446
447 void PRVM_Init (void);
448
449 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage);
450
451 #define PRVM_Alloc(buffersize) _PRVM_Alloc(buffersize, __FILE__, __LINE__)
452 #define PRVM_Free(buffer) _PRVM_Free(buffer, __FILE__, __LINE__)
453 #define PRVM_FreeAll() _PRVM_FreeAll(__FILE__, __LINE__)
454 void *_PRVM_Alloc (size_t buffersize, const char *filename, int fileline);
455 void _PRVM_Free (void *buffer, const char *filename, int fileline);
456 void _PRVM_FreeAll (const char *filename, int fileline);
457
458 void PRVM_Profile (int maxfunctions, int mininstructions);
459 void PRVM_Profile_f (void);
460 void PRVM_PrintFunction_f (void);
461
462 void PRVM_PrintState(void);
463 void PRVM_CrashAll (void);
464 void PRVM_Crash (void);
465
466 ddef_t *PRVM_ED_FindField(const char *name);
467 ddef_t *PRVM_ED_FindGlobal(const char *name);
468 mfunction_t *PRVM_ED_FindFunction(const char *name);
469
470 int PRVM_ED_FindFieldOffset(const char *name);
471 int PRVM_ED_FindGlobalOffset(const char *name);
472 func_t PRVM_ED_FindFunctionOffset(const char *name);
473
474 void PRVM_MEM_IncreaseEdicts(void);
475
476 prvm_edict_t *PRVM_ED_Alloc (void);
477 void PRVM_ED_Free (prvm_edict_t *ed);
478 void PRVM_ED_ClearEdict (prvm_edict_t *e);
479
480 void PRVM_PrintFunctionStatements (const char *name);
481 void PRVM_ED_Print(prvm_edict_t *ed);
482 void PRVM_ED_Write (qfile_t *f, prvm_edict_t *ed);
483 const char *PRVM_ED_ParseEdict (const char *data, prvm_edict_t *ent);
484
485 void PRVM_ED_WriteGlobals (qfile_t *f);
486 void PRVM_ED_ParseGlobals (const char *data);
487
488 void PRVM_ED_LoadFromFile (const char *data);
489
490 prvm_edict_t *PRVM_EDICT_NUM_ERROR(int n, char *filename, int fileline);
491 #define PRVM_EDICT_NUM(n) (((n) >= 0 && (n) < prog->max_edicts) ? prog->edicts + (n) : PRVM_EDICT_NUM_ERROR(n, __FILE__, __LINE__))
492 #define PRVM_EDICT_NUM_UNSIGNED(n) (((n) < prog->max_edicts) ? prog->edicts + (n) : PRVM_EDICT_NUM_ERROR(n, __FILE__, __LINE__))
493
494 //int NUM_FOR_EDICT_ERROR(prvm_edict_t *e);
495 #define PRVM_NUM_FOR_EDICT(e) ((int)((prvm_edict_t *)(e) - prog->edicts))
496 //int PRVM_NUM_FOR_EDICT(prvm_edict_t *e);
497
498 #define PRVM_NEXT_EDICT(e) ((e) + 1)
499
500 #define PRVM_EDICT_TO_PROG(e) (PRVM_NUM_FOR_EDICT(e))
501 //int PRVM_EDICT_TO_PROG(prvm_edict_t *e);
502 #define PRVM_PROG_TO_EDICT(n) (PRVM_EDICT_NUM(n))
503 //prvm_edict_t *PRVM_PROG_TO_EDICT(int n);
504
505 //============================================================================
506
507 #define PRVM_G_FLOAT(o) (prog->globals.generic[o])
508 #define PRVM_G_INT(o) (*(int *)&prog->globals.generic[o])
509 #define PRVM_G_EDICT(o) (PRVM_PROG_TO_EDICT(*(int *)&prog->globals.generic[o]))
510 #define PRVM_G_EDICTNUM(o) PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(o))
511 #define PRVM_G_VECTOR(o) (&prog->globals.generic[o])
512 #define PRVM_G_STRING(o) (PRVM_GetString(*(string_t *)&prog->globals.generic[o]))
513 //#define       PRVM_G_FUNCTION(o) (*(func_t *)&prog->globals.generic[o])
514
515 // FIXME: make these go away?
516 #define PRVM_E_FLOAT(e,o) (((float*)e->fields.vp)[o])
517 #define PRVM_E_INT(e,o) (((int*)e->fields.vp)[o])
518 //#define       PRVM_E_VECTOR(e,o) (&((float*)e->fields.vp)[o])
519 #define PRVM_E_STRING(e,o) (PRVM_GetString(*(string_t *)&((float*)e->fields.vp)[o]))
520
521 extern  int             prvm_type_size[8]; // for consistency : I think a goal of this sub-project is to
522 // make the new vm mostly independent from the old one, thus if it's necessary, I copy everything
523
524 void PRVM_Init_Exec(void);
525
526 void PRVM_ED_PrintEdicts_f (void);
527 void PRVM_ED_PrintNum (int ent);
528
529 const char *PRVM_GetString(int num);
530 int PRVM_SetEngineString(const char *s);
531 int PRVM_SetTempString(const char *s);
532 int PRVM_AllocString(size_t bufferlength, char **pointer);
533 void PRVM_FreeString(int num);
534
535 //============================================================================
536
537 // used as replacement for a prog stack
538 //#define PRVM_DEBUGPRSTACK
539
540 #ifdef PRVM_DEBUGPRSTACK
541 #define PRVM_Begin  if(prog != 0) Con_Printf("prog not 0(prog = %i) in file: %s line: %i!\n", PRVM_GetProgNr(), __FILE__, __LINE__)
542 #define PRVM_End        prog = 0
543 #else
544 #define PRVM_Begin
545 #define PRVM_End        prog = 0
546 #endif
547
548 //#define PRVM_SAFENAME
549 #ifndef PRVM_SAFENAME
550 #       define PRVM_NAME        (prog->name)
551 #else
552 #       define PRVM_NAME        (prog->name ? prog->name : "Unknown prog name")
553 #endif
554
555 // helper macro to make function pointer calls easier
556 #define PRVM_GCALL(func)        if(prog->func) prog->func
557
558 #define PRVM_ERROR              prog->error_cmd
559
560 // other prog handling functions
561 qboolean PRVM_SetProgFromString(const char *str);
562 void PRVM_SetProg(int prognr);
563
564 /*
565 Initializing a vm:
566 Call InitProg with the num
567 Set up the fields marked with [INIT] in the prog struct
568 Load a program with LoadProgs
569 */
570 void PRVM_InitProg(int prognr);
571 // LoadProgs expects to be called right after InitProg
572 void PRVM_LoadProgs (const char *filename, int numrequiredfunc, char **required_func, int numrequiredfields, prvm_required_field_t *required_field, int numrequiredglobals, char **required_global);
573 void PRVM_ResetProg(void);
574
575 qboolean PRVM_ProgLoaded(int prognr);
576
577 int     PRVM_GetProgNr(void);
578
579 void VM_Warning(const char *fmt, ...) DP_FUNC_PRINTF(1);
580
581 // TODO: fill in the params
582 //void PRVM_Create();
583
584 #endif