Libprimis
Imprimis' 3D destroyable world engine
Loading...
Searching...
No Matches
command.h
Go to the documentation of this file.
1
8
9#ifndef COMMAND_H_
10#define COMMAND_H_
11
12enum
13{
14 Value_Null = 0,
15 Value_Integer,
16 Value_Float,
17 Value_String,
18 Value_Any,
19 Value_Code,
20 Value_Macro,
21 Value_Ident,
22 Value_CString,
23 Value_CAny,
24 Value_Word,
25 Value_Pop,
26 Value_Cond,
27};
28
29enum
30{
31 Id_Var, //0
32 Id_FloatVar,
33 Id_StringVar,
34 Id_Command,
35 Id_Alias,
36 Id_Local, //5
37 Id_Do,
38 Id_DoArgs,
39 Id_If,
40 Id_Result,
41 Id_Not, //10
42 Id_And,
43 Id_Or, //12
44};
45
46enum
47{
48 Idf_Persist = 1<<0,
49 Idf_Override = 1<<1,
50 Idf_Hex = 1<<2,
51 Idf_ReadOnly = 1<<3,
52 Idf_Overridden = 1<<4,
53 Idf_Unknown = 1<<5,
54 Idf_Arg = 1<<6,
55};
56
57struct ident;
58
60{
61 union
62 {
63 int i; // Id_Var, ValueInteger
64 float f; // Id_FloatVar, ValueFloat
65 char *s; // Id_StringVar, ValueString
66 const uint *code; // ValueCode
67 ident *id; // ValueIdent
68 const char *cstr; // ValueCString
69 };
70};
71
73{
74 int type;
75
76 void setint(int val);
77 void setfloat(float val);
78 void setnumber(double val);
79 void setstr(char *val);
80 void setnull();
81 void setcode(const uint *val);
82 void setmacro(const uint *val);
83 void setcstr(const char *val);
84 void setident(ident *val);
85
86 const char *getstr() const;
87 int getint() const;
88 float getfloat() const;
89 double getnumber() const;
90 bool getbool() const;
91 void getval(tagval &r) const;
92
93 void cleanup();
94};
95
97{
98 identval val;
99 int valtype;
100 identstack *next;
101};
102
103typedef void (__cdecl *identfun)(ident *id);
104
113struct ident
114{
115 //this pointer will point to different types depending upon the type of variable
117 {
118 void *p;
119 int *i;
120 float *f;
121 char **s;
122 };
123
124 uchar type;
125 union
126 {
127 uchar valtype;
128 uchar numargs;
129 };
130 ushort flags;
131 int index;
132 const char *name;
133 union
134 {
135 struct // Id_Var, Id_FloatVar, Id_StringVar
136 {
137 union //number var range union type
138 {
139 struct
140 {
141 int min,
143 } i; // Id_Var
144 struct
145 {
146 float min,
148 } f; // Id_FloatVar
149 };
150 identvalptr storage;
151 identval overrideval;
152 } val;
153 struct // Id_Alias
154 {
155 uint *code;
156 identval val;
157 identstack *stack;
158 } alias;
159 struct // Id_Command
160 {
161 const char *args;
162 uint argmask;
163 } cmd;
164 };
165 identfun fun;
166
167 ident() {}
171 ident(int t, const char *n, int m, int x, int *s, void *f = nullptr, int flags = 0)
172 : type(t), flags(flags | (m > x ? Idf_ReadOnly : 0)), name(n), fun((identfun)f)
173 {
174 val.storage.i = s;
175 val.i.min = m;
176 val.i.max = x;
177 }
178
182 ident(int t, const char *n, float m, float x, float *s, void *f = nullptr, int flags = 0)
183 : type(t), flags(flags | (m > x ? Idf_ReadOnly : 0)), name(n), fun((identfun)f)
184 {
185 val.storage.f = s;
186 val.f.min = m;
187 val.f.max = x;
188 }
189
193 ident(int t, const char *n, char **s, void *f = nullptr, int flags = 0)
194 : type(t), flags(flags), name(n), fun((identfun)f)
195 {
196 val.storage.s = s;
197 }
198
199 // Id_Alias
200 ident(int t, const char *n, char *a, int flags)
201 : type(t), valtype(Value_String), flags(flags), name(n)
202 {
203 alias.code = nullptr;
204 alias.stack = nullptr;
205 alias.val.s = a;
206 }
207
208 ident(int t, const char *n, int a, int flags)
209 : type(t), valtype(Value_Integer), flags(flags), name(n)
210 {
211 alias.code = nullptr;
212 alias.stack = nullptr;
213 alias.val.i = a;
214 }
215
216 ident(int t, const char *n, float a, int flags)
217 : type(t), valtype(Value_Float), flags(flags), name(n)
218 {
219 alias.code = nullptr;
220 alias.stack = nullptr;
221 alias.val.f = a;
222 }
223
224 ident(int t, const char *n, int flags)
225 : type(t), valtype(Value_Null), flags(flags), name(n)
226 {
227 alias.code = nullptr;
228 alias.stack = nullptr;
229 }
230
231 ident(int t, const char *n, const tagval &v, int flags)
232 : type(t), valtype(v.type), flags(flags), name(n)
233 {
234 alias.code = nullptr;
235 alias.stack = nullptr;
236 alias.val = v;
237 }
238
242 ident(int t, const char *n, const char *args, uint argmask, int numargs, void *f = nullptr, int flags = 0)
243 : type(t), numargs(numargs), flags(flags), name(n), fun((identfun)f)
244 {
245 cmd.args = args;
246 cmd.argmask = argmask;
247 }
248
255 void changed()
256 {
257 if(fun)
258 {
259 fun(this);
260 }
261 }
262
271 void setval(const tagval &v)
272 {
273 valtype = v.type;
274 alias.val = v;
275 }
276
285 void setval(const identstack &v)
286 {
287 valtype = v.valtype;
288 alias.val = v.val;
289 }
290
297 {
298 if(valtype==Value_String)
299 {
300 delete[] alias.val.s;
301 }
302 valtype = Value_Null;
303 }
304
312 float getfloat() const;
313
319 int getint() const;
320
328 double getnumber() const;
329 const char *getstr() const;
330 void getval(tagval &r) const;
331 void getcstr(tagval &v) const;
332 void getcval(tagval &v) const;
333};
334
343extern void intret(int v);
344
345extern const char *floatstr(float v);
346
355extern void floatret(float v);
356
365extern void stringret(char *s);
366
375extern void result(const char *s);
376
387inline int parseint(const char *s)
388{
389 return static_cast<int>(std::strtoul(s, nullptr, 0));
390}
391
405extern int variable(const char *name, int min, int cur, int max, int *storage, identfun fun, int flags);
406
423extern float fvariable(const char *name, float min, float cur, float max, float *storage, identfun fun, int flags);
424
436extern char *svariable(const char *name, const char *cur, char **storage, identfun fun, int flags);
437
446extern void setvar(const char *name, int i, bool dofunc = true, bool doclamp = true);
447
456extern void setfvar(const char *name, float f, bool dofunc = true, bool doclamp = true);
457
465extern void setsvar(const char *name, const char *str, bool dofunc = true);
466
478extern bool addcommand(const char *name, identfun fun, const char *narg = "", int type = Id_Command);
479
480extern std::queue<ident *> triggerqueue;
481
489extern ident *getident(const char *name);
490extern int execute(const uint *code);
491
501extern int execute(const char *p);
502
513extern int execident(const char *name, int noid = 0, bool lookup = false);
514extern bool executebool(const uint *code);
515
525extern bool execfile(const char *cfgfile, bool msg = true);
526
536extern const char *escapestring(const char *s);
537
546extern void printvar(const ident *id, int i);
547
563extern int clampvar(bool hex, std::string name, int i, int minval, int maxval);
564extern void loopiter(ident *id, identstack &stack, int i);
565extern void loopend(ident *id, identstack &stack);
566
577const char *escapeid(const char *s);
578
587extern void writecfg(const char *savedconfig, const char *autoexec = nullptr, const char *defaultconfig = nullptr, const char *name = nullptr);
588
598extern void checksleep(int millis);
599
609extern bool initidents();
610
611extern int identflags;
612
620extern void clear_command();
621
622// nasty macros for registering script functions, abuses globals to avoid excessive infrastructure
623
624//* note: many of the VARF comments are very repetitive, because the code itself is nearly duplicated too *//
625
626/* how command registration works:
627 * In C++, all global variables must be initiated before main() is called
628 * Each of these macro kludges (which are placed at the global scope level) initializes some kind of global variable
629 * Because the macro kludges are the definition of global variables, at program initializiation, they must be run first
630 * The values of the variables themselves don't matter because they only exist to cheat and run before main()
631 * The macro kludges themselves register commands or values within some vector<>-s which keeps track of all cmds/vars
632 */
633
634/*
635 * Parameter Flags (exhaustive list, not exhaustive descriptions)
636 * i an integer parameter
637 * b a boolean parameter
638 * f a float parameter (sets to 0 if over parameter limit)
639 * F a float parameter (sets to value of previous parameter if over parameter limit)
640 * s a string parameter (sets to new allocated string "" if over parameter limit)
641 * S a string paremter (sets current string to "" if over parameter limit)
642 * t sets to null if over parameter limit
643 * T same as "t"
644 * e a code block parameter
645 * E a code block paremter (null)
646 * r an ident parameter
647 * $ an ident parameter
648 * N a number parameter
649 * D a bind parameter (e.g. player movement), adds a release action for the command
650 * C an ident parameter, ComC
651 * V a series of variadic parameter(s) (open ended series of values)
652 * 1 repeat one arg
653 * 2 repeat two args
654 * 3 repeat three args
655 * 4 repeat four args
656 */
657
658//integer var macros
659//VAR_, _VARF, VARM_ are templates for "normal" macros that do not execute an inline function
660#define VAR_(name, global, min, cur, max, persist) int global = variable(#name, min, cur, max, &global, nullptr, persist)
661#define VARN(name, global, min, cur, max) VAR_(name, global, min, cur, max, 0)
662#define VARNP(name, global, min, cur, max) VAR_(name, global, min, cur, max, Idf_Persist)
663#define VARNR(name, global, min, cur, max) VAR_(name, global, min, cur, max, Idf_Override)
664#define VAR(name, min, cur, max) VAR_(name, name, min, cur, max, 0)
665#define VARP(name, min, cur, max) VAR_(name, name, min, cur, max, Idf_Persist)
666#define VARR(name, min, cur, max) VAR_(name, name, min, cur, max, Idf_Override)
667
668//variable with inline function to be executed on change (VARiable with Function = VARF)
669#define VARF_(name, global, min, cur, max, body, persist) \
670 int global = variable(#name, min, cur, max, &global, [] (ident *) { body; }, persist); /* assign variable, needs fxn prototype declared above */
671
672#define VARFN(name, global, min, cur, max, body) VARF_(name, global, min, cur, max, body, 0)
673#define VARF(name, min, cur, max, body) VARF_(name, name, min, cur, max, body, 0)
674#define VARFP(name, min, cur, max, body) VARF_(name, name, min, cur, max, body, Idf_Persist)
675#define VARFR(name, min, cur, max, body) VARF_(name, name, min, cur, max, body, Idf_Override)
676#define VARFNP(name, global, min, cur, max, body) VARF_(name, global, min, cur, max, body, Idf_Persist)
677
678//hexadecimal var macros
679#define HVAR_(name, global, min, cur, max, persist) int global = variable(#name, min, cur, max, &global, nullptr, persist | Idf_Hex)
680#define HVARP(name, min, cur, max) HVAR_(name, name, min, cur, max, Idf_Persist)
681
682//hex variable with function to be executed on change (Hexadecimal VARiable with Function = HVARF)
683//the CVAR series of macros borrows the HVARF macro as it deals with hex colors
684#define HVARF_(name, global, min, cur, max, body, persist) \
685 int global = variable(#name, min, cur, max, &global, [] (ident *) { body; }, persist | Idf_Hex); /* assign variable, needs fxn prototype declared above */
686
687//color var macros
688#define CVAR_(name, cur, init, body, persist) bvec name = bvec::hexcolor(cur); HVARF_(name, _##name, 0, cur, 0xFFFFFF, { init; name = bvec::hexcolor(_##name); body; }, persist)
689#define CVARP(name, cur) CVAR_(name, cur, , , Idf_Persist)
690#define CVARR(name, cur) CVAR_(name, cur, , , Idf_Override)
691#define CVARFP(name, cur, body) CVAR_(name, cur, , body, Idf_Persist)
692#define CVAR0_(name, cur, body, persist) CVAR_(name, cur, { if(!_##name) _##name = cur; }, body, persist)
693#define CVAR0R(name, cur) CVAR0_(name, cur, , Idf_Override)
694#define CVAR1_(name, cur, body, persist) CVAR_(name, cur, { if(_##name <= 255) _##name |= (_##name<<8) | (_##name<<16); }, body, persist)
695#define CVAR1R(name, cur) CVAR1_(name, cur, , Idf_Override)
696#define CVAR1FR(name, cur, body) CVAR1_(name, cur, body, Idf_Override)
697
698//float var macros
699#define FVAR_(name, global, min, cur, max, persist) float global = fvariable(#name, min, cur, max, &global, nullptr, persist)
700#define FVARNP(name, global, min, cur, max) FVAR_(name, global, min, cur, max, Idf_Persist)
701#define FVAR(name, min, cur, max) FVAR_(name, name, min, cur, max, 0)
702#define FVARP(name, min, cur, max) FVAR_(name, name, min, cur, max, Idf_Persist)
703#define FVARR(name, min, cur, max) FVAR_(name, name, min, cur, max, Idf_Override)
704
705//float variable with function to be executed on change (Float VARiable with Function = FVARF)
706#define FVARF_(name, global, min, cur, max, body, persist) \
707 float global = fvariable(#name, min, cur, max, &global, [] (ident *) { body; }, persist); /* assign variable, needs fxn prototype declared above */ \
708
709#define FVARF(name, min, cur, max, body) FVARF_(name, name, min, cur, max, body, 0)
710#define FVARFP(name, min, cur, max, body) FVARF_(name, name, min, cur, max, body, Idf_Persist)
711#define FVARFR(name, min, cur, max, body) FVARF_(name, name, min, cur, max, body, Idf_Override)
712
713//string var macros
714#define SVAR_(name, global, cur, persist) char *global = svariable(#name, cur, &global, nullptr, persist)
715#define SVAR(name, cur) SVAR_(name, name, cur, 0)
716#define SVARP(name, cur) SVAR_(name, name, cur, Idf_Persist)
717#define SVARR(name, cur) SVAR_(name, name, cur, Idf_Override)
718
719//string variable with function to be executed on change (Float VARiable with Function = FVARF)
720#define SVARF_(name, global, cur, body, persist) \
721 char *global = svariable(#name, cur, &global, [] (ident *) { body; }, persist); /* assign variable, needs fxn prototype declared above */
722
723#define SVARF(name, cur, body) SVARF_(name, name, cur, body, 0)
724#define SVARFR(name, cur, body) SVARF_(name, name, cur, body, Idf_Override)
725
726#endif /* COMMAND_H_ */
void stringret(char *s)
Returns a string value from a Cubescript command.
void result(const char *s)
Returns an alias' name from a Cubescript command.
const char * escapestring(const char *s)
Replaces C style excape characters with Cubescript ones.
void intret(int v)
Returns an integer value from a Cubescript command.
int variable(const char *name, int min, int cur, int max, int *storage, identfun fun, int flags)
Registers an int variable in the Cubescript ident table.
const char * escapeid(const char *s)
Escapes a string unless it is null.
void floatret(float v)
Returns a float value from a Cubescript command.
void setfvar(const char *name, float f, bool dofunc=true, bool doclamp=true)
Sets a Cubescript float value to the given value.
void setsvar(const char *name, const char *str, bool dofunc=true)
Sets a Cubescript string value to the given value.
void setvar(const char *name, int i, bool dofunc=true, bool doclamp=true)
Sets a Cubescript integer value to the given value.
bool execfile(const char *cfgfile, bool msg=true)
Executes the contents of the referenced file.
int parseint(const char *s)
Returns the string passed as an integer.
Definition command.h:387
bool initidents()
Initializes the CubeScript preset argument idents.
bool addcommand(const char *name, identfun fun, const char *narg="", int type=Id_Command)
Registers a command in the Cubescript ident table.
int clampvar(bool hex, std::string name, int i, int minval, int maxval)
Modifies the value passed to fall within the boundaries passed.
int execident(const char *name, int noid=0, bool lookup=false)
Executes the contents of an ident, searched by name.
void printvar(const ident *id, int i)
Prints out the formatted variable.
void writecfg(const char *savedconfig, const char *autoexec=nullptr, const char *defaultconfig=nullptr, const char *name=nullptr)
Writes out the state of the CubeScript idents to a file.
void clear_command()
Clears all aliases from the ident map.
char * svariable(const char *name, const char *cur, char **storage, identfun fun, int flags)
Registers a C string variable in the Cubescript ident table.
int identflags
float fvariable(const char *name, float min, float cur, float max, float *storage, identfun fun, int flags)
Registers a float variable in the Cubescript ident table.
ident * getident(const char *name)
Returns the pointer to the ident object with the given CS alias.
void checksleep(int millis)
Processes the cubescript sleep queue.
std::queue< ident * > triggerqueue
An object representing all Cubescript objects.
Definition command.h:114
double getnumber() const
Returns the saved value of the ident as a double.
int min
Definition command.h:141
ident(int t, const char *n, char **s, void *f=nullptr, int flags=0)
Constructor for an ident for a string variable.
Definition command.h:193
void forcenull()
Sets the value type of this ident to null.
Definition command.h:296
void changed()
Calls a change effect for this ident, if one exists.
Definition command.h:255
int max
Definition command.h:142
void setval(const identstack &v)
Sets the value and type of value of this ident given an identstack.
Definition command.h:285
float getfloat() const
Returns the saved value of the ident as a float.
uchar valtype
Definition command.h:127
uchar numargs
Definition command.h:128
ident(int t, const char *n, int m, int x, int *s, void *f=nullptr, int flags=0)
Constructor for an ident for an int variable.
Definition command.h:171
int getint() const
Returns the saved value of the ident as an integer.
uchar type
Definition command.h:124
ident(int t, const char *n, const char *args, uint argmask, int numargs, void *f=nullptr, int flags=0)
Constructor for an ident for a C++ bound command.
Definition command.h:242
identfun fun
Definition command.h:165
void setval(const tagval &v)
Sets the value and type of value of this ident given a tagval.
Definition command.h:271
ident(int t, const char *n, float m, float x, float *s, void *f=nullptr, int flags=0)
Constructor for an ident for a float variable.
Definition command.h:182
Definition command.h:97
Definition command.h:60
Definition command.h:73
Definition command.h:117
char ** s
Definition command.h:121
int * i
Definition command.h:119
float * f
Definition command.h:120
void * p
Definition command.h:118