]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - progsvm.h
consolidated many mempools to make memlist more readable (and very slightly reduce...
[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_link_s
156 {
157         int entitynumber;
158         struct link_s   *prev, *next;
159 } prvm_link_t;*/
160
161 // AK: I dont call it engine private cause it doesnt really belongs to the engine
162 //     it belongs to prvm.
163 typedef struct prvm_edict_private_s
164 {
165         qboolean free;
166         float freetime;
167 } prvm_edict_private_t;
168
169 typedef struct prvm_edict_s
170 {
171         // engine-private fields (stored in dynamically resized array)
172         //edict_engineprivate_t *e;
173         union
174         {
175                 prvm_edict_private_t *e;
176                 void                             *vp;
177                 // add other private structs as you desire
178                 // new structs have to start with the elements of prvm_edit_private_t
179                 // e.g. a new struct has to either look like this:
180                 //      typedef struct server_edict_private_s {
181                 //              prvm_edict_private_t base;
182                 //              vec3_t moved_from;
183                 //      vec3_t moved_fromangles;
184                 //              ... } server_edict_private_t;
185                 // or:
186                 //      typedef struct server_edict_private_s {
187                 //              qboolean free;
188                 //              float freetime;
189                 //              vec3_t moved_from;
190                 //      vec3_t moved_fromangles;
191                 //              ... } server_edict_private_t;
192                 // However, the first one should be preferred.
193         } p;
194         // QuakeC fields (stored in dynamically resized array)
195         //entvars_t *v;
196         void *v;
197 } prvm_edict_t;
198
199 #define PRVM_GETEDICTFIELDVALUE(ed, fieldoffset) (fieldoffset ? (prvm_eval_t *)((qbyte *)ed->v + fieldoffset) : NULL)
200
201 /*// this struct is the basic requirement for a qc prog
202 typedef struct prvm_pr_globalvars_s
203 {
204         int pad[28];
205 } prvm_pr_globalvars_t;
206 */
207 /*
208 extern mfunction_t *SV_PlayerPhysicsQC;
209 extern mfunction_t *EndFrameQC;
210 //KrimZon - SERVER COMMANDS IN QUAKEC
211 extern mfunction_t *SV_ParseClientCommandQC;
212 */
213 //============================================================================
214 /*
215 typedef struct prvm_builtin_mem_s
216 {
217         void (*init)(void);
218         void (*deinit)(void);
219
220         void *mem;
221 } prvm_builtin_mem_t;
222 */
223
224 //============================================================================
225 /*
226 #define PRVM_FE_NEXTHINK        2
227 #define PRVM_FE_THINK           4
228 #define PRVM_FE_FRAME           8
229 */
230 #define PRVM_FE_CLASSNAME   8
231 #define PRVM_FE_CHAIN           4
232 #define PRVM_OP_STATE           1
233
234 #define PRVM_MAX_STACK_DEPTH            256
235 #define PRVM_LOCALSTACK_SIZE            2048
236
237 typedef void (*prvm_builtin_t) (void);
238
239 // NOTE: external code has to create and free the mempools but everything else is done by prvm !
240 typedef struct vm_prog_s
241 {
242         dprograms_t                     *progs;
243         mfunction_t                     *functions;
244         char                            *strings;
245         ddef_t                          *fielddefs;
246         ddef_t                          *globaldefs;
247         dstatement_t            *statements;
248         //prvm_pr_globalvars_t*pr_global_struct;
249         float                           *globals;                       // same as pr_global_struct
250         int                                     edict_size;                     // in bytes
251         int                                     edictareasize;          // LordHavoc: in bytes (for bound checking)
252
253         // all memory allocations related to this vm_prog (code, edicts, strings)
254         mempool_t                       *progs_mempool;
255
256         prvm_builtin_t          *builtins;
257         int                                     numbuiltins;
258
259         int                                     argc;
260
261         int                                     trace;
262         mfunction_t                     *xfunction;
263         int                                     xstatement;
264
265         // stacktrace writes into stack[MAX_STACK_DEPTH]
266         // thus increase the array, so depth wont be overwritten
267         prvm_stack_t            stack[PRVM_MAX_STACK_DEPTH+1];
268         int                                     depth;
269
270         int                                     localstack[PRVM_LOCALSTACK_SIZE];
271         int                                     localstack_used;
272
273         unsigned short          crc;
274
275         //============================================================================
276         // until this point everything also exists (with the pr_ prefix) in the old vm
277
278         // copies of some vars that were former read from sv
279         int                                     num_edicts;
280         int                                     max_edicts;
281
282         prvm_edict_t            *edicts;
283         void                            *edictsfields;
284         void                            *edictprivate;
285
286         // size of the engine private struct
287         int                                     edictprivate_size;
288
289         // has to be updated every frame - so the vm time is up-to-date
290         // AK changed so time will point to the time field (if there is one) else it points to _time
291         // actually should be double, but qc doesnt support it
292         float                           *time;
293         float                           _time;
294
295         // name of the prog, e.g. "Server", "Client" or "Menu" (used in for text output)
296         char                            *name;
297
298         // flag - used to store general flags like PRVM_GE_SELF, etc.
299         int                                     flag;
300
301         char                            *extensionstring;
302
303         // used to indicate whether a prog is loaded
304         qboolean                        loaded;
305
306         // used instead of the constant MAX_EDICTS
307         int                                     limit_edicts;
308
309 //      prvm_builtin_mem_t  *mem_list;
310
311 // now passes as parameter of PRVM_LoadProgs
312 //      char                            **required_func;
313 //      int                                     numrequiredfunc;
314
315         //============================================================================
316
317         ddef_t                          *self; // if self != 0 then there is a global self
318
319         //============================================================================
320         // function pointers
321
322         void                            (*begin_increase_edicts)(void);  // used by PRVM_MEM_Increase_Edicts
323         void                            (*end_increase_edicts)(void);
324
325         void                            (*init_edict)(int num);         // used by PRVM_ED_ClearEdict
326         void                            (*free_edict)(prvm_edict_t *ed); // used by PRVM_ED_Free
327
328         void                            (*count_edicts)(void);  // used by PRVM_ED_Count_f
329
330         qboolean                        (*load_edict)(prvm_edict_t *ent); // used by PRVM_ED_LoadFromFile
331
332         void                            (*init_cmd)(void);      // used by PRVM_InitProg
333         void                            (*reset_cmd)(void); // used by PRVM_ResetProg
334
335         void                            (*error_cmd)(void);
336
337 } prvm_prog_t;
338
339
340 extern prvm_prog_t * prog;
341
342 #define PRVM_MAXPROGS 3
343 #define PRVM_SERVERPROG 0 // actually not used at the moment
344 #define PRVM_CLIENTPROG 1
345 #define PRVM_MENUPROG   2
346
347 extern prvm_prog_t prvm_prog_list[PRVM_MAXPROGS];
348
349 //============================================================================
350 // prvm_cmds part
351
352 extern prvm_builtin_t vm_sv_builtins[];
353 extern prvm_builtin_t vm_cl_builtins[];
354 extern prvm_builtin_t vm_m_builtins[];
355
356 extern const int vm_sv_numbuiltins;
357 extern const int vm_cl_numbuiltins;
358 extern const int vm_m_numbuiltins;
359
360 extern char * vm_sv_extensions;
361 extern char * vm_cl_extensions;
362 extern char * vm_m_extensions;
363
364 void VM_SV_Cmd_Init(void);
365 void VM_SV_Cmd_Reset(void);
366
367 void VM_CL_Cmd_Init(void);
368 void VM_CL_Cmd_Reset(void);
369
370 void VM_M_Cmd_Init(void);
371 void VM_M_Cmd_Reset(void);
372
373 void VM_Cmd_Init(void);
374 void VM_Cmd_Reset(void);
375 //============================================================================
376
377 void PRVM_Init (void);
378
379 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage);
380 void PRVM_LoadProgs (const char *filename, int numrequiredfunc, char **required_func);
381
382 void PRVM_Profile_f (void);
383
384 void PRVM_PrintState(void);
385 void PRVM_CrashAll (void);
386 void PRVM_Crash (void);
387
388 prvm_edict_t *PRVM_ED_Alloc (void);
389 void PRVM_ED_Free (prvm_edict_t *ed);
390 void PRVM_ED_ClearEdict (prvm_edict_t *e);
391
392 char *PRVM_ED_NewString (const char *string);
393 // returns a copy of the string allocated from the server's string heap
394
395 void PRVM_ED_Print(prvm_edict_t *ed);
396 void PRVM_ED_Write (qfile_t *f, prvm_edict_t *ed);
397 const char *PRVM_ED_ParseEdict (const char *data, prvm_edict_t *ent);
398
399 void PRVM_ED_WriteGlobals (qfile_t *f);
400 void PRVM_ED_ParseGlobals (const char *data);
401
402 void PRVM_ED_LoadFromFile (const char *data);
403
404 prvm_edict_t *PRVM_EDICT_NUM_ERROR(int n, char *filename, int fileline);
405 #define PRVM_EDICT_NUM(n) (((n) >= 0 && (n) < prog->max_edicts) ? prog->edicts + (n) : PRVM_EDICT_NUM_ERROR(n, __FILE__, __LINE__))
406
407 //int NUM_FOR_EDICT_ERROR(edict_t *e);
408 #define PRVM_NUM_FOR_EDICT(e) ((int)((prvm_edict_t *)(e) - prog->edicts))
409 //int NUM_FOR_EDICT(edict_t *e);
410
411 #define PRVM_NEXT_EDICT(e) ((e) + 1)
412
413 #define PRVM_EDICT_TO_PROG(e) (PRVM_NUM_FOR_EDICT(e))
414 //int PRVM_EDICT_TO_PROG(edict_t *e);
415 #define PRVM_PROG_TO_EDICT(n) (PRVM_EDICT_NUM(n))
416 //edict_t *PRVM_PROG_TO_EDICT(int n);
417
418 //============================================================================
419
420 #define PRVM_G_FLOAT(o) (prog->globals[o])
421 #define PRVM_G_INT(o) (*(int *)&prog->globals[o])
422 #define PRVM_G_EDICT(o) (PRVM_PROG_TO_EDICT(*(int *)&prog->globals[o]))
423 #define PRVM_G_EDICTNUM(o) PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(o))
424 #define PRVM_G_VECTOR(o) (&prog->globals[o])
425 #define PRVM_G_STRING(o) (PRVM_GetString(*(string_t *)&prog->globals[o]))
426 //#define       PRVM_G_FUNCTION(o) (*(func_t *)&prog->globals[o])
427
428 // FIXME: make these go away?
429 #define PRVM_E_FLOAT(e,o) (((float*)e->v)[o])
430 #define PRVM_E_INT(e,o) (((int*)e->v)[o])
431 //#define       PRVM_E_VECTOR(e,o) (&((float*)e->v)[o])
432 #define PRVM_E_STRING(e,o) (PRVM_GetString(*(string_t *)&((float*)e->v)[o]))
433
434 extern  int             prvm_type_size[8]; // for consistency : I think a goal of this sub-project is to
435 // make the new vm mostly independent from the old one, thus if it's necessary, I copy everything
436
437 void PRVM_Init_Exec(void);
438
439 void PRVM_ED_PrintEdicts_f (void);
440 void PRVM_ED_PrintNum (int ent);
441
442 #define PRVM_GetString(num) (prog->strings + num)
443 #define PRVM_SetString(s)   ((s) != NULL ? (int) (s - prog->strings) : 0)
444
445 //============================================================================
446
447 // used as replacement for a prog stack
448 //#define PRVM_DEBUGPRSTACK
449
450 #ifdef PRVM_DEBUGPRSTACK
451 #define PRVM_Begin  if(prog != 0) Con_Printf("prog not 0(prog = %i) in file: %s line: %i!\n", PRVM_GetProgNr(), __FILE__, __LINE__)
452 #define PRVM_End        prog = 0
453 #else
454 #define PRVM_Begin
455 #define PRVM_End        prog = 0
456 #endif
457
458
459 //#define PRVM_SAFENAME
460 #ifndef PRVM_SAFENAME
461         #define PRVM_NAME       (prog->name)
462 #else
463         #define PRVM_NAME       (prog->name ? prog->name : "Unknown prog name")
464 #endif
465
466 // helper macro to make function pointer calls easier
467 #define PRVM_GCALL(func)        if(prog->func) prog->func
468
469 #define PRVM_ERROR              Host_Error
470
471 // other prog handling functions
472 qboolean PRVM_SetProgFromString(const char *str);
473 void     PRVM_SetProg(int prognr);
474
475 void     PRVM_InitProg(int prognr);
476 void     PRVM_ResetProg(void);
477
478 qboolean PRVM_ProgLoaded(int prognr);
479
480 int              PRVM_GetProgNr(void);
481
482
483 // TODO: fill in the params
484 //void PRVM_Create();
485
486 #endif