]> icculus.org git repositories - divverent/darkplaces.git/blob - progsvm.h
r_showsurfaces: fix "ghost normals"
[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 #ifndef DP_SMALLMEMORY
37 #define PROFILING
38 #endif
39
40 // forward declaration of clgecko_t
41 struct clgecko_s;
42
43 typedef struct prvm_stack_s
44 {
45         int                             s;
46         mfunction_t             *f;
47         double                  tprofile_acc;
48         double                  profile_acc;
49         double                  builtinsprofile_acc;
50 } prvm_stack_t;
51
52
53 typedef union prvm_eval_s
54 {
55         string_t                string;
56         float                   _float;
57         float                   vector[3];
58         func_t                  function;
59         int                             ivector[3];
60         int                             _int;
61         int                             edict;
62 } prvm_eval_t;
63
64 typedef struct prvm_required_field_s
65 {
66         int type;
67         const char *name;
68 } prvm_required_field_t;
69
70
71 // AK: I dont call it engine private cause it doesnt really belongs to the engine
72 //     it belongs to prvm.
73 typedef struct prvm_edict_private_s
74 {
75         qboolean free;
76         float freetime;
77         int mark;
78         const char *allocation_origin;
79 } prvm_edict_private_t;
80
81 typedef struct prvm_edict_s
82 {
83         // engine-private fields (stored in dynamically resized array)
84         //edict_engineprivate_t *e;
85         union
86         {
87                 prvm_edict_private_t *required;
88                 vec_t *vp;
89                 // FIXME: this server pointer really means world, not server
90                 // (it is used by both server qc and client qc, but not menu qc)
91                 edict_engineprivate_t *server;
92                 // add other private structs as you desire
93                 // new structs have to start with the elements of prvm_edit_private_t
94                 // e.g. a new struct has to either look like this:
95                 //      typedef struct server_edict_private_s {
96                 //              prvm_edict_private_t base;
97                 //              vec3_t moved_from;
98                 //      vec3_t moved_fromangles;
99                 //              ... } server_edict_private_t;
100                 // or:
101                 //      typedef struct server_edict_private_s {
102                 //              qboolean free;
103                 //              float freetime;
104                 //              vec3_t moved_from;
105                 //      vec3_t moved_fromangles;
106                 //              ... } server_edict_private_t;
107                 // However, the first one should be preferred.
108         } priv;
109         // QuakeC fields (stored in dynamically resized array)
110         union
111         {
112                 vec_t *vp;
113                 entvars_t               *server;
114                 cl_entvars_t    *client;
115         } fields;
116 } prvm_edict_t;
117
118 #define PRVM_EDICTFIELDVALUE(ed, fieldoffset) (fieldoffset >= 0 ? (prvm_eval_t *)((int *)ed->fields.vp + fieldoffset) : NULL)
119 #define PRVM_GLOBALFIELDVALUE(fieldoffset) (fieldoffset >= 0 ? (prvm_eval_t *)((int *)prog->globals.generic + fieldoffset) : NULL)
120
121 //============================================================================
122 #define PRVM_OP_STATE           1
123
124 #ifdef DP_SMALLMEMORY
125 #define PRVM_MAX_STACK_DEPTH            128
126 #define PRVM_LOCALSTACK_SIZE            2048
127
128 #define PRVM_MAX_OPENFILES 16
129 #define PRVM_MAX_OPENSEARCHES 8
130 #define PRVM_MAX_GECKOINSTANCES 1
131 #else
132 #define PRVM_MAX_STACK_DEPTH            1024
133 #define PRVM_LOCALSTACK_SIZE            16384
134
135 #define PRVM_MAX_OPENFILES 256
136 #define PRVM_MAX_OPENSEARCHES 128
137 #define PRVM_MAX_GECKOINSTANCES 32
138 #endif
139
140 typedef void (*prvm_builtin_t) (void);
141
142 // NOTE: field offsets use -1 for NULL
143 typedef struct prvm_prog_fieldoffsets_s
144 {
145         // server and client use a lot of similar fields, so this is combined
146         int SendEntity; // ssqc
147         int SendFlags; // ssqc
148         int Version; // ssqc (legacy)
149         int alpha; // ssqc / csqc
150         int ammo_cells1; // ssqc - Dissolution of Eternity mission pack
151         int ammo_lava_nails; // ssqc - Dissolution of Eternity mission pack
152         int ammo_multi_rockets; // ssqc - Dissolution of Eternity mission pack
153         int ammo_nails1; // ssqc - Dissolution of Eternity mission pack
154         int ammo_plasma; // ssqc - Dissolution of Eternity mission pack
155         int ammo_rockets1; // ssqc - Dissolution of Eternity mission pack
156         int ammo_shells1; // ssqc - Dissolution of Eternity mission pack
157         int angles; // common - used by changeyaw/changepitch
158         int button3; // ssqc
159         int button4; // ssqc
160         int button5; // ssqc
161         int button6; // ssqc
162         int button7; // ssqc
163         int button8; // ssqc
164         int button9; // ssqc
165         int button10; // ssqc
166         int button11; // ssqc
167         int button12; // ssqc
168         int button13; // ssqc
169         int button14; // ssqc
170         int button15; // ssqc
171         int button16; // ssqc
172         int buttonchat; // ssqc
173         int buttonuse; // ssqc
174         int chain; // common - used by find builtins
175         int classname; // common
176         int clientcamera; // ssqc
177         int clientcolors; // ssqc
178         int clientstatus; // ssqc
179         int color; // ssqc
180         int colormod; // ssqc / csqc
181         int contentstransition; // ssqc
182         int cursor_active; // ssqc
183         int cursor_screen; // ssqc
184         int cursor_trace_endpos; // ssqc
185         int cursor_trace_ent; // ssqc
186         int cursor_trace_start; // ssqc
187         int customizeentityforclient; // ssqc
188         int dimension_hit; // ssqc / csqc
189         int dimension_solid; // ssqc / csqc
190         int disableclientprediction; // ssqc
191         int discardabledemo; // ssqc
192         int dphitcontentsmask; // ssqc / csqc
193         int drawonlytoclient; // ssqc
194         int exteriormodeltoclient; // ssqc
195         int fatness; // ssqc / csqc
196         int forceshader; // csqc
197         int frame1time; // csqc
198         int frame2; // csqc
199         int frame2time; // csqc
200         int frame3; // csqc
201         int frame3time; // csqc
202         int frame4; // csqc
203         int frame4time; // csqc
204         int frame; // common - used by OP_STATE
205         int fullbright; // ssqc - Nehahra support
206         int glow_color; // ssqc
207         int glow_size; // ssqc
208         int glow_trail; // ssqc
209         int glowmod; // ssqc / csqc
210         int gravity; // ssqc
211         int groundentity; // ssqc / csqc
212         int hull; // ssqc / csqc
213         int ideal_yaw; // ssqc / csqc
214         int idealpitch; // ssqc / csqc
215         int items2; // ssqc
216         int lerpfrac3; // csqc
217         int lerpfrac4; // csqc
218         int lerpfrac; // csqc
219         int light_lev; // ssqc
220         int message; // csqc
221         int modelflags; // ssqc
222         int movement; // ssqc
223         int movetypesteplandevent; // ssqc
224         int netaddress; // ssqc
225         int nextthink; // common - used by OP_STATE
226         int nodrawtoclient; // ssqc
227         int pflags; // ssqc
228         int ping; // ssqc
229         int packetloss; // ssqc
230         int movementloss; // ssqc
231         int pitch_speed; // ssqc / csqc
232         int playermodel; // ssqc
233         int playerskin; // ssqc
234         int pmodel; // ssqc
235         int punchvector; // ssqc
236         int renderamt; // ssqc - HalfLife support
237         int renderflags; // csqc
238         int rendermode; // ssqc - HalfLife support
239         int scale; // ssqc / csqc
240         int shadertime; // csqc
241         int skeletonindex; // csqc / ssqc FTE_CSQC_SKELETONOBJECTS / DP_SKELETONOBJECTS
242         int style; // ssqc
243         int tag_entity; // ssqc / csqc
244         int tag_index; // ssqc / csqc
245         int think; // common - used by OP_STATE
246         int viewmodelforclient; // ssqc
247         int viewzoom; // ssqc
248         int yaw_speed; // ssqc / csqc
249         int bouncefactor; // ssqc
250         int bouncestop; // ssqc
251
252         int solid; // ssqc / csqc (physics)
253         int movetype; // ssqc / csqc (physics)
254         int modelindex; // ssqc / csqc (physics)
255         int mins; // ssqc / csqc (physics)
256         int maxs; // ssqc / csqc (physics)
257         int mass; // ssqc / csqc (physics)
258         int origin; // ssqc / csqc (physics)
259         int velocity; // ssqc / csqc (physics)
260         //int axis_forward; // ssqc / csqc (physics)
261         //int axis_left; // ssqc / csqc (physics)
262         //int axis_up; // ssqc / csqc (physics)
263         //int spinvelocity; // ssqc / csqc (physics)
264         //int angles; // ssqc / csqc (physics)
265         int avelocity; // ssqc / csqc (physics)
266         int jointtype; // ssqc / csqc (physics)
267         int enemy; // ssqc / csqc (physics)
268         int aiment; // ssqc / csqc (physics)
269         int movedir; // ssqc / csqc (physics)
270
271         int camera_transform; // csqc (warpzones)
272
273         int userwavefunc_param0; // csqc (userwavefunc)
274         int userwavefunc_param1; // csqc (userwavefunc)
275         int userwavefunc_param2; // csqc (userwavefunc)
276         int userwavefunc_param3; // csqc (userwavefunc)
277
278         int crypto_keyfp; // svqc (crypto)
279         int crypto_mykeyfp; // svqc (crypto)
280         int crypto_idfp; // svqc (crypto)
281         int crypto_encryptmethod; // svqc (crypto)
282         int crypto_signmethod; // svqc (crypto)
283 }
284 prvm_prog_fieldoffsets_t;
285
286 // NOTE: global offsets use -1 for NULL
287 typedef struct prvm_prog_globaloffsets_s
288 {
289         // server and client use a lot of similar globals, so this is combined
290         int SV_InitCmd; // ssqc
291         int self; // common
292         int time; // ssqc / csqc
293         int v_forward; // ssqc / csqc
294         int v_right; // ssqc / csqc
295         int v_up; // ssqc / csqc
296         int view_angles; // csqc
297         int view_punchangle; // csqc
298         int view_punchvector; // csqc
299         int trace_allsolid; // ssqc / csqc
300         int trace_startsolid; // ssqc / csqc
301         int trace_fraction; // ssqc / csqc
302         int trace_inwater; // ssqc / csqc
303         int trace_inopen; // ssqc / csqc
304         int trace_endpos; // ssqc / csqc
305         int trace_plane_normal; // ssqc / csqc
306         int trace_plane_dist; // ssqc / csqc
307         int trace_ent; // ssqc / csqc
308         int trace_networkentity; // csqc
309         int trace_dphitcontents; // ssqc / csqc
310         int trace_dphitq3surfaceflags; // ssqc / csqc
311         int trace_dphittexturename; // ssqc / csqc
312         int trace_dpstartcontents; // ssqc / csqc
313         int intermission; // csqc
314         int coop; // csqc
315         int deathmatch; // csqc
316         int dmg_take; // csqc
317         int dmg_save; // csqc
318         int dmg_origin; // csqc
319         int sb_showscores; // csqc
320         int drawfont; // csqc / menu
321         int drawfontscale; // csqc / menu
322         int require_spawnfunc_prefix; // ssqc
323         int worldstatus; // ssqc
324         int servertime; // csqc
325         int serverprevtime; // csqc
326         int serverdeltatime; // csqc
327         int gettaginfo_name; // ssqc / csqc
328         int gettaginfo_parent; // ssqc / csqc
329         int gettaginfo_offset; // ssqc / csqc
330         int gettaginfo_forward; // ssqc / csqc
331         int gettaginfo_right; // ssqc / csqc
332         int gettaginfo_up; // ssqc / csqc
333         int transparent_offset; // csqc
334
335         int particles_alphamin; // csqc
336         int particles_alphamax; // csqc
337         int particles_colormin; // csqc
338         int particles_colormax; // csqc
339 }
340 prvm_prog_globaloffsets_t;
341
342 // these are initialized using PRVM_ED_FindFunction
343 // NOTE: function offsets use 0 for NULL
344 typedef struct prvm_prog_funcoffsets_s
345 {
346         func_t CSQC_ConsoleCommand; // csqc
347         func_t CSQC_Ent_Remove; // csqc
348         func_t CSQC_Ent_Spawn; // csqc DP_CSQC_ENT_SPAWN extension (BlackHC - TODO: needs to be added to dpextensions.qc)
349         func_t CSQC_Ent_Update; // csqc
350         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...
351         func_t CSQC_Event_Sound; // csqc : called by engine when an incoming sound packet arrives so CSQC can act on it
352         func_t CSQC_Init; // csqc
353         func_t CSQC_InputEvent; // csqc
354         func_t CSQC_Parse_CenterPrint; // csqc
355         func_t CSQC_Parse_Print; // csqc
356         func_t CSQC_Parse_StuffCmd; // csqc
357         func_t CSQC_Parse_TempEntity; // csqc [515]: very helpfull when you want to create your own particles/decals/etc for effects that already exist
358         func_t CSQC_Shutdown; // csqc
359         func_t CSQC_UpdateView; // csqc
360         func_t Gecko_Query; // csqc, mqc
361         func_t EndFrame; // ssqc
362         func_t RestoreGame; // ssqc
363         func_t SV_ChangeTeam; // ssqc
364         func_t SV_ParseClientCommand; // ssqc
365         func_t SV_PlayerPhysics; // ssqc
366         func_t SV_OnEntityPreSpawnFunction; // ssqc
367         func_t SV_OnEntityNoSpawnFunction; // ssqc
368         func_t SV_OnEntityPostSpawnFunction; // ssqc
369         func_t GameCommand; // any
370         func_t SV_Shutdown; // ssqc
371         func_t URI_Get_Callback; // any
372         func_t SV_PausedTic; //ssqc
373
374         // menu qc only uses some functions, nothing else
375         func_t m_draw; // mqc
376         func_t m_init; // mqc
377         func_t m_keydown; // mqc
378         func_t m_keyup; // mqc
379         func_t m_shutdown; // mqc
380         func_t m_toggle; // mqc
381         func_t m_newmap; // mqc
382 }
383 prvm_prog_funcoffsets_t;
384
385 // stringbuffer flags
386 #define STRINGBUFFER_SAVED      1 // saved in savegames
387
388 typedef struct prvm_stringbuffer_s
389 {
390         int max_strings;
391         int num_strings;
392         char **strings;
393         const char *origin;
394         unsigned char flags;
395 }
396 prvm_stringbuffer_t;
397
398 // [INIT] variables flagged with this token can be initialized by 'you'
399 // NOTE: external code has to create and free the mempools but everything else is done by prvm !
400 typedef struct prvm_prog_s
401 {
402         double              starttime;
403         unsigned int            id; // increasing unique id of progs instance
404         dprograms_t                     *progs;
405         mfunction_t                     *functions;
406         char                            *strings;
407         int                                     stringssize;
408         ddef_t                          *fielddefs;
409         ddef_t                          *globaldefs;
410         dstatement_t            *statements;
411         int                                     entityfields;                   // number of vec_t fields in progs (some variables are 3)
412         int                                     entityfieldsarea;               // LordHavoc: equal to max_edicts * entityfields (for bounds checking)
413
414         int                                     *statement_linenums; // NULL if not available
415
416         double                          *statement_profile; // only incremented if prvm_statementprofiling is on
417
418         union {
419                 vec_t *generic;
420                 globalvars_t *server;
421                 cl_globalvars_t *client;
422         } globals;
423
424         int                                     maxknownstrings;
425         int                                     numknownstrings;
426         // this is updated whenever a string is removed or added
427         // (simple optimization of the free string search)
428         int                                     firstfreeknownstring;
429         const char                      **knownstrings;
430         unsigned char           *knownstrings_freeable;
431         const char          **knownstrings_origin;
432         const char                      ***stringshash;
433
434         memexpandablearray_t    stringbuffersarray;
435
436         // all memory allocations related to this vm_prog (code, edicts, strings)
437         mempool_t                       *progs_mempool; // [INIT]
438
439         prvm_builtin_t          *builtins; // [INIT]
440         int                                     numbuiltins; // [INIT]
441
442         int                                     argc;
443
444         int                                     trace;
445         mfunction_t                     *xfunction;
446         int                                     xstatement;
447
448         // stacktrace writes into stack[MAX_STACK_DEPTH]
449         // thus increase the array, so depth wont be overwritten
450         prvm_stack_t            stack[PRVM_MAX_STACK_DEPTH+1];
451         int                                     depth;
452
453         int                                     localstack[PRVM_LOCALSTACK_SIZE];
454         int                                     localstack_used;
455
456         unsigned short          headercrc; // [INIT]
457         unsigned short          headercrc2; // [INIT] alternate CRC for tenebrae progs.dat
458
459         unsigned short          filecrc;
460
461         //============================================================================
462         // until this point everything also exists (with the pr_ prefix) in the old vm
463
464         qfile_t                         *openfiles[PRVM_MAX_OPENFILES];
465         const char *         openfiles_origin[PRVM_MAX_OPENFILES];
466         fssearch_t                      *opensearches[PRVM_MAX_OPENSEARCHES];
467         const char *         opensearches_origin[PRVM_MAX_OPENSEARCHES];
468         struct clgecko_s                *opengeckoinstances[PRVM_MAX_GECKOINSTANCES];
469         skeleton_t                      *skeletons[MAX_EDICTS];
470
471         // copies of some vars that were former read from sv
472         int                                     num_edicts;
473         // number of edicts for which space has been (should be) allocated
474         int                                     max_edicts; // [INIT]
475         // used instead of the constant MAX_EDICTS
476         int                                     limit_edicts; // [INIT]
477
478         // number of reserved edicts (allocated from 1)
479         int                                     reserved_edicts; // [INIT]
480
481         prvm_edict_t            *edicts;
482         vec_t                           *edictsfields;
483         void                                    *edictprivate;
484
485         // size of the engine private struct
486         int                                     edictprivate_size; // [INIT]
487
488         prvm_prog_fieldoffsets_t        fieldoffsets;
489         prvm_prog_globaloffsets_t       globaloffsets;
490         prvm_prog_funcoffsets_t         funcoffsets;
491
492         // allow writing to world entity fields, this is set by server init and
493         // cleared before first server frame
494         qboolean                        allowworldwrites;
495
496         // name of the prog, e.g. "Server", "Client" or "Menu" (used for text output)
497         const char                      *name; // [INIT]
498
499         // flag - used to store general flags like PRVM_GE_SELF, etc.
500         int                             flag;
501
502         const char                      *extensionstring; // [INIT]
503
504         qboolean                        loadintoworld; // [INIT]
505
506         // used to indicate whether a prog is loaded
507         qboolean                        loaded;
508         qboolean                        leaktest_active;
509
510         // translation buffer (only needs to be freed on unloading progs, type is private to prvm_edict.c)
511         void *po;
512
513         // printed together with backtraces
514         const char *statestring;
515
516 //      prvm_builtin_mem_t  *mem_list;
517
518 // now passed as parameter of PRVM_LoadProgs
519 //      char                            **required_func;
520 //      int                                     numrequiredfunc;
521
522         //============================================================================
523
524         ddef_t                          *self; // if self != 0 then there is a global self
525
526         //============================================================================
527         // function pointers
528
529         void                            (*begin_increase_edicts)(void); // [INIT] used by PRVM_MEM_Increase_Edicts
530         void                            (*end_increase_edicts)(void); // [INIT]
531
532         void                            (*init_edict)(prvm_edict_t *edict); // [INIT] used by PRVM_ED_ClearEdict
533         void                            (*free_edict)(prvm_edict_t *ed); // [INIT] used by PRVM_ED_Free
534
535         void                            (*count_edicts)(void); // [INIT] used by PRVM_ED_Count_f
536
537         qboolean                        (*load_edict)(prvm_edict_t *ent); // [INIT] used by PRVM_ED_LoadFromFile
538
539         void                            (*init_cmd)(void); // [INIT] used by PRVM_InitProg
540         void                            (*reset_cmd)(void); // [INIT] used by PRVM_ResetProg
541
542         void                            (*error_cmd)(const char *format, ...) DP_FUNC_PRINTF(1); // [INIT]
543
544         void                            (*ExecuteProgram)(func_t fnum, const char *errormessage); // pointer to one of the *VM_ExecuteProgram functions
545 } prvm_prog_t;
546
547 extern prvm_prog_t * prog;
548
549 #define PRVM_MAXPROGS 3
550 #define PRVM_SERVERPROG 0 // actually not used at the moment
551 #define PRVM_CLIENTPROG 1
552 #define PRVM_MENUPROG   2
553
554 extern prvm_prog_t prvm_prog_list[PRVM_MAXPROGS];
555
556 //============================================================================
557 // prvm_cmds part
558
559 extern prvm_builtin_t vm_sv_builtins[];
560 extern prvm_builtin_t vm_cl_builtins[];
561 extern prvm_builtin_t vm_m_builtins[];
562
563 extern const int vm_sv_numbuiltins;
564 extern const int vm_cl_numbuiltins;
565 extern const int vm_m_numbuiltins;
566
567 extern const char * vm_sv_extensions; // client also uses this
568 extern const char * vm_m_extensions;
569
570 void VM_SV_Cmd_Init(void);
571 void VM_SV_Cmd_Reset(void);
572
573 void VM_CL_Cmd_Init(void);
574 void VM_CL_Cmd_Reset(void);
575
576 void VM_M_Cmd_Init(void);
577 void VM_M_Cmd_Reset(void);
578
579 void VM_Cmd_Init(void);
580 void VM_Cmd_Reset(void);
581 //============================================================================
582
583 void PRVM_Init (void);
584
585 #ifdef PROFILING
586 void MVM_ExecuteProgram (func_t fnum, const char *errormessage);
587 void CLVM_ExecuteProgram (func_t fnum, const char *errormessage);
588 void SVVM_ExecuteProgram (func_t fnum, const char *errormessage);
589 #else
590 #define MVM_ExecuteProgram SVVM_ExecuteProgram
591 #define CLVM_ExecuteProgram SVVM_ExecuteProgram
592 void SVVM_ExecuteProgram (func_t fnum, const char *errormessage);
593 #endif
594 #define PRVM_ExecuteProgram prog->ExecuteProgram
595
596 #define PRVM_Alloc(buffersize) _PRVM_Alloc(buffersize, __FILE__, __LINE__)
597 #define PRVM_Free(buffer) _PRVM_Free(buffer, __FILE__, __LINE__)
598 #define PRVM_FreeAll() _PRVM_FreeAll(__FILE__, __LINE__)
599 void *_PRVM_Alloc (size_t buffersize, const char *filename, int fileline);
600 void _PRVM_Free (void *buffer, const char *filename, int fileline);
601 void _PRVM_FreeAll (const char *filename, int fileline);
602
603 void PRVM_Profile (int maxfunctions, double mintime, int sortby);
604 void PRVM_Profile_f (void);
605 void PRVM_ChildProfile_f (void);
606 void PRVM_CallProfile_f (void);
607 void PRVM_PrintFunction_f (void);
608
609 void PRVM_PrintState(void);
610 void PRVM_CrashAll (void);
611 void PRVM_Crash (void);
612 void PRVM_ShortStackTrace(char *buf, size_t bufsize);
613 const char *PRVM_AllocationOrigin(void);
614
615 ddef_t *PRVM_ED_FindField(const char *name);
616 ddef_t *PRVM_ED_FindGlobal(const char *name);
617 mfunction_t *PRVM_ED_FindFunction(const char *name);
618
619 int PRVM_ED_FindFieldOffset(const char *name);
620 int PRVM_ED_FindGlobalOffset(const char *name);
621 func_t PRVM_ED_FindFunctionOffset(const char *name);
622 #define PRVM_ED_FindFieldOffset_FromStruct(st, field) prog->fieldoffsets . field = ((int *)(&((st *)NULL)-> field ) - ((int *)NULL))
623 #define PRVM_ED_FindGlobalOffset_FromStruct(st, field) prog->globaloffsets . field = ((int *)(&((st *)NULL)-> field ) - ((int *)NULL))
624
625 void PRVM_MEM_IncreaseEdicts(void);
626
627 qboolean PRVM_ED_CanAlloc(prvm_edict_t *e);
628 prvm_edict_t *PRVM_ED_Alloc (void);
629 void PRVM_ED_Free (prvm_edict_t *ed);
630 void PRVM_ED_ClearEdict (prvm_edict_t *e);
631
632 void PRVM_PrintFunctionStatements (const char *name);
633 void PRVM_ED_Print(prvm_edict_t *ed, const char *wildcard_fieldname);
634 void PRVM_ED_Write (qfile_t *f, prvm_edict_t *ed);
635 const char *PRVM_ED_ParseEdict (const char *data, prvm_edict_t *ent);
636
637 void PRVM_ED_WriteGlobals (qfile_t *f);
638 void PRVM_ED_ParseGlobals (const char *data);
639
640 void PRVM_ED_LoadFromFile (const char *data);
641
642 unsigned int PRVM_EDICT_NUM_ERROR(unsigned int n, const char *filename, int fileline);
643 #define PRVM_EDICT(n) (((unsigned)(n) < (unsigned int)prog->max_edicts) ? (unsigned int)(n) : PRVM_EDICT_NUM_ERROR((unsigned int)(n), __FILE__, __LINE__))
644 #define PRVM_EDICT_NUM(n) (prog->edicts + PRVM_EDICT(n))
645
646 //int NUM_FOR_EDICT_ERROR(prvm_edict_t *e);
647 #define PRVM_NUM_FOR_EDICT(e) ((int)((prvm_edict_t *)(e) - prog->edicts))
648 //int PRVM_NUM_FOR_EDICT(prvm_edict_t *e);
649
650 #define PRVM_NEXT_EDICT(e) ((e) + 1)
651
652 #define PRVM_EDICT_TO_PROG(e) (PRVM_NUM_FOR_EDICT(e))
653 //int PRVM_EDICT_TO_PROG(prvm_edict_t *e);
654 #define PRVM_PROG_TO_EDICT(n) (PRVM_EDICT_NUM(n))
655 //prvm_edict_t *PRVM_PROG_TO_EDICT(int n);
656
657 //============================================================================
658
659 #define PRVM_G_FLOAT(o) (prog->globals.generic[o])
660 #define PRVM_G_INT(o) (*(int *)&prog->globals.generic[o])
661 #define PRVM_G_EDICT(o) (PRVM_PROG_TO_EDICT(*(int *)&prog->globals.generic[o]))
662 #define PRVM_G_EDICTNUM(o) PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(o))
663 #define PRVM_G_VECTOR(o) (&prog->globals.generic[o])
664 #define PRVM_G_STRING(o) (PRVM_GetString(*(string_t *)&prog->globals.generic[o]))
665 //#define       PRVM_G_FUNCTION(o) (*(func_t *)&prog->globals.generic[o])
666
667 // FIXME: make these go away?
668 #define PRVM_E_FLOAT(e,o) (((float*)e->fields.vp)[o])
669 #define PRVM_E_INT(e,o) (((int*)e->fields.vp)[o])
670 //#define       PRVM_E_VECTOR(e,o) (&((float*)e->fields.vp)[o])
671 #define PRVM_E_STRING(e,o) (PRVM_GetString(*(string_t *)&((float*)e->fields.vp)[o]))
672
673 extern  int             prvm_type_size[8]; // for consistency : I think a goal of this sub-project is to
674 // make the new vm mostly independent from the old one, thus if it's necessary, I copy everything
675
676 void PRVM_Init_Exec(void);
677
678 void PRVM_ED_PrintEdicts_f (void);
679 void PRVM_ED_PrintNum (int ent, const char *wildcard_fieldname);
680
681 const char *PRVM_GetString(int num);
682 int PRVM_SetEngineString(const char *s);
683 const char *PRVM_ChangeEngineString(int i, const char *s);
684 int PRVM_SetTempString(const char *s);
685 int PRVM_AllocString(size_t bufferlength, char **pointer);
686 void PRVM_FreeString(int num);
687
688 //============================================================================
689
690 // used as replacement for a prog stack
691 //#define PRVM_DEBUGPRSTACK
692
693 #ifdef PRVM_DEBUGPRSTACK
694 #define PRVM_Begin  if(prog != 0) Con_Printf("prog not 0(prog = %i) in file: %s line: %i!\n", PRVM_GetProgNr(), __FILE__, __LINE__)
695 #define PRVM_End        prog = 0
696 #else
697 #define PRVM_Begin
698 #define PRVM_End        prog = 0
699 #endif
700
701 //#define PRVM_SAFENAME
702 #ifndef PRVM_SAFENAME
703 #       define PRVM_NAME        (prog->name)
704 #else
705 #       define PRVM_NAME        (prog->name ? prog->name : "Unknown prog name")
706 #endif
707
708 // helper macro to make function pointer calls easier
709 #define PRVM_GCALL(func)        if(prog->func) prog->func
710
711 #define PRVM_ERROR              prog->error_cmd
712
713 // other prog handling functions
714 qboolean PRVM_SetProgFromString(const char *str);
715 void PRVM_SetProg(int prognr);
716
717 /*
718 Initializing a vm:
719 Call InitProg with the num
720 Set up the fields marked with [INIT] in the prog struct
721 Load a program with LoadProgs
722 */
723 void PRVM_InitProg(int prognr);
724 // LoadProgs expects to be called right after InitProg
725 void PRVM_LoadProgs (const char *filename, int numrequiredfunc, const char **required_func, int numrequiredfields, prvm_required_field_t *required_field, int numrequiredglobals, char **required_global);
726 void PRVM_ResetProg(void);
727
728 qboolean PRVM_ProgLoaded(int prognr);
729
730 int     PRVM_GetProgNr(void);
731
732 void VM_Warning(const char *fmt, ...) DP_FUNC_PRINTF(1);
733
734 // TODO: fill in the params
735 //void PRVM_Create();
736
737 void VM_GenerateFrameGroupBlend(framegroupblend_t *framegroupblend, const prvm_edict_t *ed);
738 void VM_FrameBlendFromFrameGroupBlend(frameblend_t *frameblend, const framegroupblend_t *framegroupblend, const dp_model_t *model);
739 void VM_UpdateEdictSkeleton(prvm_edict_t *ed, const dp_model_t *edmodel, const frameblend_t *frameblend);
740 void VM_RemoveEdictSkeleton(prvm_edict_t *ed);
741
742 #endif