]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - progsvm.h
-Added sv_playerphysicsqc to control whether the qc physics function is called (if...
[xonotic/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
35 /*
36 typedef union vm_eval_s
37 {
38         string_t                string;
39         float                   _float;
40         float                   vector[3];
41         func_t                  function;
42         int                             ivector[3];
43         int                             _int;
44         int                             edict;
45 } vm_eval_t;
46
47 typedef struct vm_link_s
48 {
49         int entitynumber;
50         struct link_s   *prev, *next;
51 } vm_link_t;
52
53 #define ENTITYGRIDAREAS 16
54
55 typedef struct vm_edict_engineprivate_s
56 {
57         // true if this edict is unused
58         qboolean free;
59         // sv.time when the object was freed (to prevent early reuse which could
60         // mess up client interpolation or obscure severe QuakeC bugs)
61         float freetime;
62
63         // physics grid areas this edict is linked into
64         link_t areagrid[ENTITYGRIDAREAS];
65         // since the areagrid can have multiple references to one entity,
66         // we should avoid extensive checking on entities already encountered
67         int areagridmarknumber;
68
69         // old entity protocol, not used
70 #ifdef QUAKEENTITIES
71         // baseline values
72         entity_state_t baseline;
73         // LordHavoc: previous frame
74         entity_state_t deltabaseline;
75 #endif
76
77         // LordHavoc: gross hack to make floating items still work
78         int suspendedinairflag;
79         // used by PushMove to keep track of where objects were before they were
80         // moved, in case they need to be moved back
81         vec3_t moved_from;
82         vec3_t moved_fromangles;
83 }
84 vm_edict_engineprivate_t;
85
86 // the entire server entity structure
87 // NOTE: keep this small!  priv and v are dynamic but this struct is not!
88 typedef struct vm_edict_s
89 {
90         // engine-private fields (stored in dynamically resized array)
91         edict_engineprivate_t *e;
92         // QuakeC fields (stored in dynamically resized array)
93         entvars_t *v;
94 }
95 vm_edict_t;
96 */
97
98 /*// LordHavoc: in an effort to eliminate time wasted on GetEdictFieldValue...  see pr_edict.c for the functions which use these.
99 extern int eval_gravity;
100 extern int eval_button3;
101 extern int eval_button4;
102 extern int eval_button5;
103 extern int eval_button6;
104 extern int eval_button7;
105 extern int eval_button8;
106 extern int eval_glow_size;
107 extern int eval_glow_trail;
108 extern int eval_glow_color;
109 extern int eval_items2;
110 extern int eval_scale;
111 extern int eval_alpha;
112 extern int eval_renderamt; // HalfLife support
113 extern int eval_rendermode; // HalfLife support
114 extern int eval_fullbright;
115 extern int eval_ammo_shells1;
116 extern int eval_ammo_nails1;
117 extern int eval_ammo_lava_nails;
118 extern int eval_ammo_rockets1;
119 extern int eval_ammo_multi_rockets;
120 extern int eval_ammo_cells1;
121 extern int eval_ammo_plasma;
122 extern int eval_idealpitch;
123 extern int eval_pitch_speed;
124 extern int eval_viewmodelforclient;
125 extern int eval_nodrawtoclient;
126 extern int eval_exteriormodeltoclient;
127 extern int eval_drawonlytoclient;
128 extern int eval_ping;
129 extern int eval_movement;
130 extern int eval_pmodel;
131 extern int eval_punchvector;
132 extern int eval_viewzoom;
133 extern int eval_clientcolors;
134 extern int eval_tag_entity;
135 extern int eval_tag_index;*/
136
137 typedef struct prvm_stack_s
138 {
139         int                             s;
140         mfunction_t             *f;
141 } prvm_stack_t;
142
143
144 typedef union prvm_eval_s
145 {
146         string_t                string;
147         float                   _float;
148         float                   vector[3];
149         func_t                  function;
150         int                             ivector[3];
151         int                             _int;
152         int                             edict;
153 } prvm_eval_t;
154
155 typedef struct prvm_required_field_s
156 {
157         int type;
158         const char *name;
159 } prvm_required_field_t;
160
161
162 /*typedef struct prvm_link_s
163 {
164         int entitynumber;
165         struct link_s   *prev, *next;
166 } prvm_link_t;*/
167
168 // AK: I dont call it engine private cause it doesnt really belongs to the engine
169 //     it belongs to prvm.
170 typedef struct prvm_edict_private_s
171 {
172         qboolean free;
173         float freetime;
174 } prvm_edict_private_t;
175
176 typedef struct prvm_edict_s
177 {
178         // engine-private fields (stored in dynamically resized array)
179         //edict_engineprivate_t *e;
180         union
181         {
182                 prvm_edict_private_t *required;
183                 void *vp;
184                 edict_engineprivate_t *server;
185                 // add other private structs as you desire
186                 // new structs have to start with the elements of prvm_edit_private_t
187                 // e.g. a new struct has to either look like this:
188                 //      typedef struct server_edict_private_s {
189                 //              prvm_edict_private_t base;
190                 //              vec3_t moved_from;
191                 //      vec3_t moved_fromangles;
192                 //              ... } server_edict_private_t;
193                 // or:
194                 //      typedef struct server_edict_private_s {
195                 //              qboolean free;
196                 //              float freetime;
197                 //              vec3_t moved_from;
198                 //      vec3_t moved_fromangles;
199                 //              ... } server_edict_private_t;
200                 // However, the first one should be preferred.
201         } priv;
202         // QuakeC fields (stored in dynamically resized array)
203         union
204         {
205                 void *vp;
206                 entvars_t *server;
207         } fields;
208 } prvm_edict_t;
209
210 #define PRVM_GETEDICTFIELDVALUE(ed, fieldoffset) (fieldoffset ? (prvm_eval_t *)((qbyte *)ed->fields.vp + fieldoffset) : NULL)
211
212 /*// this struct is the basic requirement for a qc prog
213 typedef struct prvm_pr_globalvars_s
214 {
215         int pad[28];
216 } prvm_pr_globalvars_t;
217 */
218 /*
219 extern mfunction_t *SV_PlayerPhysicsQC;
220 extern mfunction_t *EndFrameQC;
221 //KrimZon - SERVER COMMANDS IN QUAKEC
222 extern mfunction_t *SV_ParseClientCommandQC;
223 */
224 //============================================================================
225 /*
226 typedef struct prvm_builtin_mem_s
227 {
228         void (*init)(void);
229         void (*deinit)(void);
230
231         void *mem;
232 } prvm_builtin_mem_t;
233 */
234
235 //============================================================================
236 /*
237 #define PRVM_FE_NEXTHINK        2
238 #define PRVM_FE_THINK           4
239 #define PRVM_FE_FRAME           8
240 */
241 #define PRVM_FE_CLASSNAME   8
242 #define PRVM_FE_CHAIN           4
243 #define PRVM_OP_STATE           1
244
245 #define PRVM_MAX_STACK_DEPTH            256
246 #define PRVM_LOCALSTACK_SIZE            2048
247
248 typedef void (*prvm_builtin_t) (void);
249
250 // [INIT] variables flagged with this token can be initialized by 'you'
251 // NOTE: external code has to create and free the mempools but everything else is done by prvm !
252 typedef struct prvm_prog_s
253 {
254         dprograms_t                     *progs;
255         mfunction_t                     *functions;
256         char                            *strings;
257         int                                     stringssize;
258         ddef_t                          *fielddefs;
259         ddef_t                          *globaldefs;
260         dstatement_t            *statements;
261         int                                     edict_size;                     // in bytes
262         int                                     edictareasize;          // LordHavoc: in bytes (for bound checking)
263
264         int                                     *statement_linenums; // NULL if not available
265
266         union {
267                 float *generic;
268                 globalvars_t *server;
269         } globals;
270
271         int                                     maxknownstrings;
272         int                                     numknownstrings;
273         // this is updated whenever a string is removed or added
274         // (simple optimization of the free string search)
275         int                                     firstfreeknownstring;
276         const char                      **knownstrings;
277         const char                      ***stringshash;
278
279         // all memory allocations related to this vm_prog (code, edicts, strings)
280         mempool_t                       *progs_mempool; // [INIT]
281
282         prvm_builtin_t          *builtins; // [INIT]
283         int                                     numbuiltins; // [INIT]
284
285         int                                     argc;
286
287         int                                     trace;
288         mfunction_t                     *xfunction;
289         int                                     xstatement;
290
291         // stacktrace writes into stack[MAX_STACK_DEPTH]
292         // thus increase the array, so depth wont be overwritten
293         prvm_stack_t            stack[PRVM_MAX_STACK_DEPTH+1];
294         int                                     depth;
295
296         int                                     localstack[PRVM_LOCALSTACK_SIZE];
297         int                                     localstack_used;
298
299         unsigned short          headercrc; // [INIT]
300
301         unsigned short          filecrc;
302
303         //============================================================================
304         // until this point everything also exists (with the pr_ prefix) in the old vm
305
306         // copies of some vars that were former read from sv
307         int                                     num_edicts;
308         // number of edicts for which space has been (should be) allocated
309         int                                     max_edicts; // [INIT]
310         // used instead of the constant MAX_EDICTS
311         int                                     limit_edicts; // [INIT]
312
313         // number of reserved edicts (allocated from 1)
314         int                                     reserved_edicts; // [INIT]
315
316
317         prvm_edict_t            *edicts;
318         void                            *edictsfields;
319         void                            *edictprivate;
320
321         // size of the engine private struct
322         int                                     edictprivate_size; // [INIT]
323
324         // has to be updated every frame - so the vm time is up-to-date
325         // AK changed so time will point to the time field (if there is one) else it points to _time
326         // actually should be double, but qc doesnt support it
327         float                           *time;
328         float                           _time;
329
330         // name of the prog, e.g. "Server", "Client" or "Menu" (used for text output)
331         char                            *name; // [INIT]
332
333         // flag - used to store general flags like PRVM_GE_SELF, etc.
334         int                                     flag;
335
336         char                            *extensionstring; // [INIT]
337
338         qboolean                        loadintoworld; // [INIT]
339
340         // used to indicate whether a prog is loaded
341         qboolean                        loaded;
342
343 //      prvm_builtin_mem_t  *mem_list;
344
345 // now passes as parameter of PRVM_LoadProgs
346 //      char                            **required_func;
347 //      int                                     numrequiredfunc;
348
349         //============================================================================
350
351         ddef_t                          *self; // if self != 0 then there is a global self
352
353         //============================================================================
354         // function pointers
355
356         void                            (*begin_increase_edicts)(void); // [INIT] used by PRVM_MEM_Increase_Edicts
357         void                            (*end_increase_edicts)(void); // [INIT]
358
359         void                            (*init_edict)(prvm_edict_t *edict); // [INIT] used by PRVM_ED_ClearEdict
360         void                            (*free_edict)(prvm_edict_t *ed); // [INIT] used by PRVM_ED_Free
361
362         void                            (*count_edicts)(void); // [INIT] used by PRVM_ED_Count_f
363
364         qboolean                        (*load_edict)(prvm_edict_t *ent); // [INIT] used by PRVM_ED_LoadFromFile
365
366         void                            (*init_cmd)(void); // [INIT] used by PRVM_InitProg
367         void                            (*reset_cmd)(void); // [INIT] used by PRVM_ResetProg
368
369         void                            (*error_cmd)(const char *format, ...); // [INIT]
370
371 } prvm_prog_t;
372
373 extern prvm_prog_t * prog;
374
375 #define PRVM_MAXPROGS 3
376 #define PRVM_SERVERPROG 0 // actually not used at the moment
377 #define PRVM_CLIENTPROG 1
378 #define PRVM_MENUPROG   2
379
380 extern prvm_prog_t prvm_prog_list[PRVM_MAXPROGS];
381
382 //============================================================================
383 // prvm_cmds part
384
385 extern prvm_builtin_t vm_sv_builtins[];
386 extern prvm_builtin_t vm_cl_builtins[];
387 extern prvm_builtin_t vm_m_builtins[];
388
389 extern const int vm_sv_numbuiltins;
390 extern const int vm_cl_numbuiltins;
391 extern const int vm_m_numbuiltins;
392
393 extern char * vm_sv_extensions;
394 extern char * vm_cl_extensions;
395 extern char * vm_m_extensions;
396
397 void VM_SV_Cmd_Init(void);
398 void VM_SV_Cmd_Reset(void);
399
400 void VM_CL_Cmd_Init(void);
401 void VM_CL_Cmd_Reset(void);
402
403 void VM_M_Cmd_Init(void);
404 void VM_M_Cmd_Reset(void);
405
406 void VM_Cmd_Init(void);
407 void VM_Cmd_Reset(void);
408 //============================================================================
409
410 void PRVM_Init (void);
411
412 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage);
413
414 #define PRVM_Alloc(buffersize) _PRVM_Alloc(buffersize, __FILE__, __LINE__)
415 #define PRVM_Free(buffer) _PRVM_Free(buffer, __FILE__, __LINE__)
416 #define PRVM_FreeAll() _PRVM_FreeAll(__FILE__, __LINE__)
417 void *_PRVM_Alloc (size_t buffersize, const char *filename, int fileline);
418 void _PRVM_Free (void *buffer, const char *filename, int fileline);
419 void _PRVM_FreeAll (const char *filename, int fileline);
420
421 void PRVM_Profile_f (void);
422
423 void PRVM_PrintState(void);
424 void PRVM_CrashAll (void);
425 void PRVM_Crash (void);
426
427 int PRVM_ED_FindFieldOffset(const char *field);
428 ddef_t *PRVM_ED_FindField (const char *name);
429 mfunction_t *PRVM_ED_FindFunction (const char *name);
430
431 void PRVM_MEM_IncreaseEdicts(void);
432
433 prvm_edict_t *PRVM_ED_Alloc (void);
434 void PRVM_ED_Free (prvm_edict_t *ed);
435 void PRVM_ED_ClearEdict (prvm_edict_t *e);
436
437 void PRVM_ED_Print(prvm_edict_t *ed);
438 void PRVM_ED_Write (qfile_t *f, prvm_edict_t *ed);
439 const char *PRVM_ED_ParseEdict (const char *data, prvm_edict_t *ent);
440
441 void PRVM_ED_WriteGlobals (qfile_t *f);
442 void PRVM_ED_ParseGlobals (const char *data);
443
444 void PRVM_ED_LoadFromFile (const char *data);
445
446 prvm_edict_t *PRVM_EDICT_NUM_ERROR(int n, char *filename, int fileline);
447 #define PRVM_EDICT_NUM(n) (((n) >= 0 && (n) < prog->max_edicts) ? prog->edicts + (n) : PRVM_EDICT_NUM_ERROR(n, __FILE__, __LINE__))
448 #define PRVM_EDICT_NUM_UNSIGNED(n) (((n) < prog->max_edicts) ? prog->edicts + (n) : PRVM_EDICT_NUM_ERROR(n, __FILE__, __LINE__))
449
450 //int NUM_FOR_EDICT_ERROR(prvm_edict_t *e);
451 #define PRVM_NUM_FOR_EDICT(e) ((int)((prvm_edict_t *)(e) - prog->edicts))
452 //int PRVM_NUM_FOR_EDICT(prvm_edict_t *e);
453
454 #define PRVM_NEXT_EDICT(e) ((e) + 1)
455
456 #define PRVM_EDICT_TO_PROG(e) (PRVM_NUM_FOR_EDICT(e))
457 //int PRVM_EDICT_TO_PROG(prvm_edict_t *e);
458 #define PRVM_PROG_TO_EDICT(n) (PRVM_EDICT_NUM(n))
459 //prvm_edict_t *PRVM_PROG_TO_EDICT(int n);
460
461 //============================================================================
462
463 #define PRVM_G_FLOAT(o) (prog->globals.generic[o])
464 #define PRVM_G_INT(o) (*(int *)&prog->globals.generic[o])
465 #define PRVM_G_EDICT(o) (PRVM_PROG_TO_EDICT(*(int *)&prog->globals.generic[o]))
466 #define PRVM_G_EDICTNUM(o) PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(o))
467 #define PRVM_G_VECTOR(o) (&prog->globals.generic[o])
468 #define PRVM_G_STRING(o) (PRVM_GetString(*(string_t *)&prog->globals.generic[o]))
469 //#define       PRVM_G_FUNCTION(o) (*(func_t *)&prog->globals.generic[o])
470
471 // FIXME: make these go away?
472 #define PRVM_E_FLOAT(e,o) (((float*)e->fields.vp)[o])
473 #define PRVM_E_INT(e,o) (((int*)e->fields.vp)[o])
474 //#define       PRVM_E_VECTOR(e,o) (&((float*)e->fields.vp)[o])
475 #define PRVM_E_STRING(e,o) (PRVM_GetString(*(string_t *)&((float*)e->fields.vp)[o]))
476
477 extern  int             prvm_type_size[8]; // for consistency : I think a goal of this sub-project is to
478 // make the new vm mostly independent from the old one, thus if it's necessary, I copy everything
479
480 void PRVM_Init_Exec(void);
481
482 void PRVM_ED_PrintEdicts_f (void);
483 void PRVM_ED_PrintNum (int ent);
484
485 const char *PRVM_GetString(int num);
486 int PRVM_SetEngineString(const char *s);
487 int PRVM_AllocString(int bufferlength, char **pointer);
488 void PRVM_FreeString(int num);
489
490 //============================================================================
491
492 // used as replacement for a prog stack
493 //#define PRVM_DEBUGPRSTACK
494
495 #ifdef PRVM_DEBUGPRSTACK
496 #define PRVM_Begin  if(prog != 0) Con_Printf("prog not 0(prog = %i) in file: %s line: %i!\n", PRVM_GetProgNr(), __FILE__, __LINE__)
497 #define PRVM_End        prog = 0
498 #else
499 #define PRVM_Begin
500 #define PRVM_End        prog = 0
501 #endif
502
503
504 //#define PRVM_SAFENAME
505 #ifndef PRVM_SAFENAME
506         #define PRVM_NAME       (prog->name)
507 #else
508         #define PRVM_NAME       (prog->name ? prog->name : "Unknown prog name")
509 #endif
510
511 // helper macro to make function pointer calls easier
512 #define PRVM_GCALL(func)        if(prog->func) prog->func
513
514 #define PRVM_ERROR              prog->error_cmd
515
516 // other prog handling functions
517 qboolean PRVM_SetProgFromString(const char *str);
518 void PRVM_SetProg(int prognr);
519
520 /*
521 Initializing a vm:
522 Call InitProg with the num
523 Set up the fields marked with [INIT] in the prog struct
524 Load a program with LoadProgs
525 */
526 void PRVM_InitProg(int prognr);
527 // LoadProgs expects to be called right after InitProg
528 void PRVM_LoadProgs (const char *filename, int numrequiredfunc, char **required_func, int numrequiredfields, prvm_required_field_t *required_field);
529 void PRVM_ResetProg(void);
530
531 qboolean PRVM_ProgLoaded(int prognr);
532
533 int     PRVM_GetProgNr(void);
534
535
536 // TODO: fill in the params
537 //void PRVM_Create();
538
539 #endif