]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/extra/fteqcc-src/progslib.h
fteqcc source
[voretournament/voretournament.git] / misc / mediasource / extra / fteqcc-src / progslib.h
1
2 #ifndef PROGSLIB_H
3 #define PROGSLIB_H
4 /*#define true 1
5 #define false 0
6
7 #define PITCH   0
8 #define YAW             1
9 #define ROLL    2
10
11 typedef char bool;
12 //typedef float vec3_t[3];
13 typedef int progsnum_t;
14 typedef int     func_t;
15 #ifndef COMPILER
16 typedef char *string_t;
17 #endif
18 //typedef struct globalvars_s globalvars_t;
19 //typedef struct edict_s edict_t;
20 #define globalvars_t void
21 #define edict_t void
22 */
23
24 #ifdef _MSC_VER
25         #define VARGS __cdecl
26 #endif
27 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
28         #define LIKEPRINTF(x) __attribute__((format(printf,x,x+1)))
29 #endif
30 #ifndef LIKEPRINTF
31         #define LIKEPRINTF(x)
32 #endif
33 #ifndef VARGS
34         #define VARGS
35 #endif
36
37
38 struct edict_s;
39 struct entvars_s;
40 struct globalvars_s;
41 struct qcthread_s;
42 typedef struct progfuncs_s progfuncs_t;
43 typedef void (*builtin_t) (progfuncs_t *prinst, struct globalvars_s *gvars);
44
45 //used by progs engine. All nulls is reset.
46 typedef struct {
47         char *varname;
48         struct fdef_s *ofs32;
49
50         int spare[2];
51 } evalc_t;
52 #define sizeofevalc sizeof(evalc_t)
53 typedef enum {ev_void, ev_string, ev_float, ev_vector, ev_entity, ev_field, ev_function, ev_pointer, ev_integer, ev_variant, ev_struct, ev_union} etype_t;
54
55 //the number of pointers to variables (as opposed to functions - those are fine) in these structures is excessive.
56 //Many of the functions are also obsolete.
57 struct progfuncs_s {
58         int progsversion;       //PROGSTRUCT_VERSION
59
60
61         void    (*Configure)                            (progfuncs_t *prinst, int addressablesize, int max_progs);              //configure buffers and memory. Used to reset and must be called first. Flushes a running VM.
62         progsnum_t      (*LoadProgs)                    (progfuncs_t *prinst, char *s, int headercrc, builtin_t *builtins, int numbuiltins);    //load a progs
63         int             (*InitEnts)                                     (progfuncs_t *prinst, int max_ents);    //returns size of edicts for use with nextedict macro
64         void    (*ExecuteProgram)                       (progfuncs_t *prinst, func_t fnum);     //start execution
65         pbool   (*SwitchProgs)                          (progfuncs_t *prinst, progsnum_t num);  //switch to a different progs - this should be obsolete.
66         struct globalvars_s     *(*globals)             (progfuncs_t *prinst, progsnum_t num);  //get the globals of a progs
67         struct entvars_s        *(*entvars)             (progfuncs_t *prinst, struct edict_s *ent);     //return a pointer to the entvars of an ent. can be achieved via the edict_t structure instead, so obsolete.
68
69         void    (VARGS *RunError)                       (progfuncs_t *prinst, char *msg, ...) LIKEPRINTF(2);            //builtins call this to say there was a problem
70         void    (*PrintEdict)                           (progfuncs_t *prinst, struct edict_s *ed);      //get a listing of all vars on an edict (sent back via 'print')
71
72         struct edict_s  *(*EntAlloc)            (progfuncs_t *prinst);
73         void    (*EntFree)                                      (progfuncs_t *prinst, struct edict_s *ed);
74
75         struct edict_s  *(*EDICT_NUM)           (progfuncs_t *prinst, unsigned int n);          //get the nth edict
76         unsigned int            (*NUM_FOR_EDICT)                        (progfuncs_t *prinst, struct edict_s *e);       //so you can find out what that 'n' will be
77
78         void    (*SetGlobalEdict)                       (progfuncs_t *prinst, struct edict_s *ed, int ofs);     //set a global to an edict (partially obsolete)
79
80         char    *(*VarString)                           (progfuncs_t *prinst, int       first); //returns a string made up of multiple arguments
81
82         struct progstate_s **progstate; //internal to the library.
83
84         func_t  (*FindFunction)                         (progfuncs_t *prinst, char *funcname, progsnum_t num);
85
86         int             (*StartCompile)                         (progfuncs_t *prinst, int argv, char **argc);   //1 if can compile, 0 if failed to compile
87         int             (*ContinueCompile)                      (progfuncs_t *prinst);  //2 if finished, 1 if more to go, 0 if failed
88
89         char    *(*filefromprogs)                       (progfuncs_t *prinst, progsnum_t prnum, char *fname, int *size, char *buffer);  //reveals encoded/added files from already loaded progs
90         char    *(*filefromnewprogs)            (progfuncs_t *prinst, char *prname, char *fname, int *size, char *buffer);      //reveals encoded/added files from a progs on the disk somewhere
91
92         char    *(*save_ents)                           (progfuncs_t *prinst, char *buf, int *size, int mode);  //dump the entire progs info into one big self allocated string
93         int             (*load_ents)                            (progfuncs_t *prinst, char *s, float killonspawnflags); //restore the entire progs state (or just add some more ents) (returns edicts ize)
94
95         char    *(*saveent)                                     (progfuncs_t *prinst, char *buf, int *size, struct edict_s *ed);        //will save just one entities vars
96         struct edict_s  *(*restoreent)          (progfuncs_t *prinst, char *buf, int *size, struct edict_s *ed);        //will restore the entity that had it's values saved (can use NULL for ed)
97
98         union eval_s    *(*FindGlobal)          (progfuncs_t *prinst, char *name, progsnum_t num);      //find a pointer to the globals value
99         char    *(*AddString)                           (progfuncs_t *prinst, char *val, int minlength);        //dump a string into the progs memory (for setting globals and whatnot)
100         void    *(*Tempmem)                                     (progfuncs_t *prinst, int ammount, char *whatfor);      //grab some mem for as long as the progs stays loaded
101
102         union eval_s    *(*GetEdictFieldValue)  (progfuncs_t *prinst, struct edict_s *ent, char *name, evalc_t *s); //get an entityvar (cache it) and return the possible values
103         struct edict_s  *(*ProgsToEdict)        (progfuncs_t *prinst, int progs);       //edicts are stored as ints and need to be adjusted
104         int             (*EdictToProgs)                         (progfuncs_t *prinst, struct edict_s *ed);              //edicts are stored as ints and need to be adjusted
105
106         char    *(*EvaluateDebugString)         (progfuncs_t *prinst, char *key);       //evaluate a string and return it's value (according to current progs) (expands edict vars)
107
108         int             *pr_trace;      //start calling the editor for each line executed       
109
110         void    (*StackTrace)                           (progfuncs_t *prinst);
111         
112         int             (*ToggleBreak)                          (progfuncs_t *prinst, char *filename, int linenum, int mode);
113
114         int             numprogs;
115
116         struct  progexterns_s *parms;   //these are the initial parms, they may be changed
117
118         pbool   (*Decompile)                            (progfuncs_t *prinst, char *fname);
119
120
121         struct prinst_s *prinst;        //internal variables. Leave alone.
122
123         int             *callargc;      //number of args of built-in call
124         void    (*RegisterBuiltin)                      (progfuncs_t *prinst, char *, builtin_t);
125
126         char *stringtable;      //qc strings are all relative. add to a qc string. this is required for support of frikqcc progs that strip string immediates.
127         int fieldadjust;        //FrikQCC style arrays can cause problems due to field remapping. This causes us to leave gaps but offsets identical.
128
129         struct qcthread_s *(*Fork)                      (progfuncs_t *prinst);  //returns a pointer to a thread which can be resumed via RunThread.
130         void    (*RunThread)                            (progfuncs_t *prinst, struct qcthread_s *thread);
131         void    (*AbortStack)                           (progfuncs_t *prinst);  //annigilates the current stack, positioning on a return statement. It is expected that this is only used via a builtin!
132
133         int lastcalledbuiltinnumber;                    //useful with non-implemented opcodes.
134
135         int (*RegisterFieldVar)                         (progfuncs_t *prinst, unsigned int type, char *name, int requestedpos, int originalofs);
136
137         char    *tempstringbase;                                //for engine's use. Store your base tempstring pointer here.
138         int             tempstringnum;                  //for engine's use.
139
140         string_t (*TempString)                          (progfuncs_t *prinst, char *str);
141
142         string_t (*StringToProgs)                       (progfuncs_t *prinst, char *str);
143         char *(*StringToNative)                         (progfuncs_t *prinst, string_t str);
144         int stringtablesize;
145
146         int (*QueryField)                                       (progfuncs_t *prinst, unsigned int fieldoffset, etype_t *type, char **name, evalc_t *fieldcache);       //find info on a field definition at an offset
147
148         void (*EntClear)                                        (progfuncs_t *progfuncs, struct edict_s *e);
149 };
150
151 typedef struct progexterns_s {
152         int progsversion;       //PROGSTRUCT_VERSION
153
154         unsigned char *(*ReadFile) (char *fname, void *buffer, int len);
155         int (*FileSize) (char *fname);  //-1 if file does not exist
156         pbool (*WriteFile) (char *name, void *data, int len);
157         int (VARGS *printf) (const char *, ...) LIKEPRINTF(1);
158         void (VARGS *Sys_Error) (const char *, ...) LIKEPRINTF(1);
159         void (VARGS *Abort) (char *, ...) LIKEPRINTF(1);
160         int edictsize;  //size of edict_t
161
162         void (*entspawn) (struct edict_s *ent, int loading);    //ent has been spawned, but may not have all the extra variables (that may need to be set) set
163         pbool (*entcanfree) (struct edict_s *ent);      //return true to stop ent from being freed
164         void (*stateop) (progfuncs_t *prinst, float var, func_t func);  //what to do on qc's state opcode.
165         void (*cstateop) (progfuncs_t *prinst, float vara, float varb, func_t currentfunc);             //a hexen2 opcode.
166         void (*cwstateop) (progfuncs_t *prinst, float vara, float varb, func_t currentfunc);    //a hexen2 opcode.
167         void (*thinktimeop) (progfuncs_t *prinst, struct edict_s *ent, float varb);                     //a hexen2 opcode.
168
169
170         //used when loading a game
171         builtin_t *(*builtinsfor) (int num, int headercrc);     //must return a pointer to the builtins that were used before the state was saved.
172         void (*loadcompleate) (int edictsize);  //notification to reset any pointers.
173
174         void *(VARGS *memalloc) (int size);     //small string allocation       malloced and freed randomly by the executor. (use malloc if you want)
175         void (VARGS *memfree) (void * mem);
176
177
178         builtin_t *globalbuiltins;      //these are available to all progs
179         int numglobalbuiltins;
180
181         enum {PR_NOCOMPILE, PR_COMPILENEXIST, PR_COMPILEEXISTANDCHANGED, PR_COMPILECHANGED, PR_COMPILEALWAYS, PR_COMPILEIGNORE} autocompile;
182
183         double *gametime;       //used to prevent the vm from reusing an entity faster than 2 secs.
184
185         struct edict_s **sv_edicts;     //pointer to the engine's reference to world.
186         unsigned int *sv_num_edicts;            //pointer to the engine's edict count.
187
188         int (*useeditor) (progfuncs_t *prinst, char *filename, int line, int nump, char **parms);       //called on syntax errors or step-by-step debugging.
189 } progparms_t, progexterns_t;
190
191 //FIXMEs
192 void QC_AddSharedVar(progfuncs_t *progfuncs, int start, int size);
193 void QC_AddSharedFieldVar(progfuncs_t *progfuncs, int num, char *relstringtable);
194 void ED_Print(progfuncs_t *progfuncs, struct edict_s *ed);
195 char *PR_RemoveProgsString(progfuncs_t *progfuncs, string_t str);
196 int PR_GetFuncArgCount(progfuncs_t *progfuncs, func_t func);
197
198 #if defined(QCLIBDLL_EXPORTS)
199 __declspec(dllexport)
200 #endif
201 progfuncs_t * InitProgs(progparms_t *ext);
202 #if defined(QCLIBDLL_EXPORTS)
203 __declspec(dllexport)
204 #endif
205 void CloseProgs(progfuncs_t *inst);
206
207 #ifndef COMPILER
208 typedef union eval_s
209 {
210         string_t                string;
211         float                   _float;
212         float                   _vector[3];
213         func_t                  function;
214         int                             _int;
215         int                             edict;
216         progsnum_t              prog;   //so it can easily be changed
217 } eval_t;
218 #endif
219
220 #define PR_CURRENT      -1
221 #define PR_ANY  -2      //not always valid. Use for finding funcs
222 #define PR_ANYBACK -3
223 #define PROGSTRUCT_VERSION 2
224
225
226 #ifndef DLL_PROG
227 #define PR_Configure(pf, memsize, max_progs)                            (*pf->Configure)                        (pf, memsize, max_progs)
228 #define PR_LoadProgs(pf, s, headercrc, builtins, numb)          (*pf->LoadProgs)                        (pf, s, headercrc, builtins, numb)
229 #define PR_InitEnts(pf, maxents)                                                        (*pf->InitEnts)                         (pf, maxents)
230 #define PR_ExecuteProgram(pf, fnum)                                                     (*pf->ExecuteProgram)           (pf, fnum)
231 #define PR_SwitchProgs(pf, num)                                                         (*pf->SwitchProgs)                      (pf, num)
232 #define PR_globals(pf, num)                                                                     (*pf->globals)                          (pf, num)
233 #define PR_entvars(pf, ent)                                                                     (*pf->entvars)                          (pf, ent)
234
235 #define PR_RegisterFieldVar(pf,type,name,reqofs,qcofs)          (*pf->RegisterFieldVar)         (pf,type,name,reqofs,qcofs)
236
237 #define ED_Alloc(pf)                                                                            (*pf->EntAlloc)                         (pf)
238 #define ED_Free(pf, ed)                                                                         (*pf->EntFree)                          (pf, ed)
239 #define ED_Clear(pf, ed)                                                                        (*pf->EntClear)                         (pf, ed)
240
241 #define PR_LoadEnts(pf, s, kf)                                                          (*pf->load_ents)                        (pf, s, kf)
242 #define PR_SaveEnts(pf, buf, size, mode)                                        (*pf->save_ents)                        (pf, buf, size, mode)
243
244 #define EDICT_NUM(pf, num)                                                                      (*pf->EDICT_NUM)                        (pf, num)
245 #define NUM_FOR_EDICT(pf, e)                                                            (*pf->NUM_FOR_EDICT)            (pf, e)
246 #define SetGlobalEdict(pf, ed, ofs)                                                     (*pf->SetGlobalEdict)           (pf, ed, ofs)
247 #define PR_VarString(pf,first)                                                          (*pf->VarString)                        (pf,first)
248
249 #define PR_StartCompile(pf,argc,argv)                                           (*pf->StartCompile)                     (pf,argc,argv)
250 #define PR_ContinueCompile(pf)                                                          (*pf->ContinueCompile)          (pf)
251
252 #define PR_StackTrace(pf)                                                                       (*pf->StackTrace)                       (pf)
253 #define PR_AbortStack(pf)                                                                       (*pf->AbortStack)                       (pf)
254
255 #define PR_RunError(pf,str)                                                                     (*pf->RunError)                         (pf,str)
256
257 #define PR_PrintEdict(pf,ed)                                                            (*pf->PrintEdict)                       (pf, ed)
258
259 #define PR_FindFunction(pf, name, num)                                          (*pf->FindFunction)                     (pf, name, num)
260 #define PR_FindGlobal(pf, name, progs)                                          (*pf->FindGlobal)                       (pf, name, progs)
261 #define PR_AddString(pf, ed, len)                                                       (*pf->AddString)                        (pf, ed, len)
262 #define PR_Alloc(pf,size)                                                                       (*pf->Tempmem)                          (pf, size)
263
264 #define PROG_TO_EDICT(pf, ed)                                                           (*pf->ProgsToEdict)                     (pf, ed)
265 #define EDICT_TO_PROG(pf, ed)                                                           (*pf->EdictToProgs)                     (pf, (struct edict_s*)ed)
266
267 #define PR_RegisterBuiltin(pf, name, func)                                      (*pf->RegisterBuiltin)          (pf, name, func)
268
269 #define PR_GetString(pf,s)                                                                      (*pf->StringToNative)           (pf, s)
270 #define PR_GetStringOfs(pf,o)                                                           (*pf->StringToNative)           (pf, G_INT(o))
271 #define PR_SetString(pf, s)                                                                     (*pf->StringToProgs)            (pf, s)
272
273 #define NEXT_EDICT(pf,o)                EDICT_NUM(pf, NUM_FOR_EDICT(pf, o)+1)
274 #define RETURN_EDICT(pf, e) (((int *)pr_globals)[OFS_RETURN] = EDICT_TO_PROG(pf, e))
275
276
277 //builtin funcs (which operate on globals)
278 //To use these outside of builtins, you will likly have to use the 'globals' method.
279 #define G_FLOAT(o) (((float *)pr_globals)[o])
280 #define G_FLOAT2(o) (((float *)pr_globals)[OFS_PARM0 + o*3])
281 #define G_INT(o) (((int *)pr_globals)[o])
282 #define G_EDICT(pf, o) PROG_TO_EDICT(pf, G_INT(o)) //((edict_t *)((char *) sv.edicts+ *(int *)&((float *)pr_globals)[o]))
283 #define G_EDICTNUM(pf, o) NUM_FOR_EDICT(pf, G_EDICT(pf, o))
284 #define G_VECTOR(o) (&((float *)pr_globals)[o])
285 #define G_FUNCTION(o) (*(func_t *)&((float *)pr_globals)[o])
286
287 /*
288 #define PR_GetString(p,s) (s?s + p->stringtable:"")
289 #define PR_GetStringOfs(p,o) (G_INT(o)?G_INT(o) + p->stringtable:"")
290 #define PR_SetStringOfs(p,o,s) (G_INT(o) = s - p->stringtable)
291 */
292 //#define PR_SetString(p, s) ((s&&*s)?(s - p->stringtable):0)
293 #define PR_NewString(p, s, l) PR_SetString(p, PR_AddString(p, s, l))
294 /**/
295
296 #define ev_prog ev_integer
297
298 #define E_STRING(o) (char *)(((int *)((char *)ed) + progparms.edictsize)[o])
299
300 //#define pr_global_struct pr_globals
301
302 #endif
303
304
305 #define OFS_NULL                0
306 #define OFS_RETURN              1
307 #define OFS_PARM0               4               // leave 3 ofs for each parm to hold vectors
308 #define OFS_PARM1               7
309 #define OFS_PARM2               10
310 #define OFS_PARM3               13
311 #define OFS_PARM4               16
312 #define OFS_PARM5               19
313 #define OFS_PARM6               22
314 #define OFS_PARM7               25
315 #define RESERVED_OFS    28
316
317
318 #undef edict_t
319 #undef globalvars_t
320
321 #endif //PROGSLIB_H