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