LCOV - code coverage report
Current view: top level - libprimis-headers - command.h (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 98.1 % 52 51
Test Date: 2026-02-16 07:02:41 Functions: 100.0 % 12 12

            Line data    Source code
       1              : /**
       2              :  * @file command.h
       3              :  * @brief Script binding functionality.
       4              :  *
       5              :  * This file describes the CubeScript API for binding and executing code from within
       6              :  * the engine's CubeScript context.
       7              :  */
       8              : 
       9              : #ifndef COMMAND_H_
      10              : #define COMMAND_H_
      11              : 
      12              : enum
      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              : 
      29              : enum
      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              : 
      46              : enum
      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              : 
      57              : struct ident;
      58              : 
      59              : struct identval
      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              : 
      72              : struct tagval : identval
      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              : 
      96              : struct identstack
      97              : {
      98              :     identval val;
      99              :     int valtype;
     100              :     identstack *next;
     101              : };
     102              : 
     103              : typedef void (__cdecl *identfun)(ident *id);
     104              : 
     105              : /**
     106              :  * @brief An object representing all Cubescript objects.
     107              :  *
     108              :  * This object defines all possible state for any object in Cubescript. Different
     109              :  * types will have different members due to the union types, but the ident object
     110              :  * is common to them regardless. This allows storage of CS objects in a uniform
     111              :  * map consisting of one type.
     112              :  */
     113              : struct ident
     114              : {
     115              :     //this pointer will point to different types depending upon the type of variable
     116              :     union identvalptr /**< points to an ident's value */
     117              :     {
     118              :         void  *p; /**< can point to any Id_*Var */
     119              :         int   *i; /**< points to an Id_Var (int) */
     120              :         float *f; /**< points to an Id_FloatVar */
     121              :         char **s; /**< points to a pointer to a Id_StringVar (C string) */
     122              :     };
     123              : 
     124              :     uchar type; /**< one of Id_* in Id_ enum */
     125              :     union
     126              :     {
     127              :         uchar valtype; /**< if alias, points to Id_Alias's type */
     128              :         uchar numargs; /**< if command, number of commands the Id_Command has */
     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, /**< if an int variable, its min allowed value*/
     142              :                         max; /**< if an int variable, its max allowed value*/
     143              :                 } i;     // Id_Var
     144              :                 struct
     145              :                 {
     146              :                     float min, /**< if a float variable, its min allowed value*/
     147              :                           max; /**< if a float variable, its max allowed value*/
     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; /**< the pointer a command or variable points to (the on-change command for a var)*/
     166              : 
     167         1113 :     ident() {}
     168              :     /**
     169              :      * @brief Constructor for an ident for an int variable.
     170              :      */
     171          387 :     ident(int t, const char *n, int m, int x, int *s, void *f = nullptr, int flags = 0)
     172          387 :         : type(t), flags(flags | (m > x ? Idf_ReadOnly : 0)), name(n), fun(reinterpret_cast<identfun>(f))
     173              :     {
     174          387 :         val.storage.i = s;
     175          387 :         val.i.min = m;
     176          387 :         val.i.max = x;
     177          387 :     }
     178              : 
     179              :     /**
     180              :      * @brief Constructor for an ident for a float variable.
     181              :      */
     182          112 :     ident(int t, const char *n, float m, float x, float *s, void *f = nullptr, int flags = 0)
     183          112 :         : type(t), flags(flags | (m > x ? Idf_ReadOnly : 0)), name(n), fun(reinterpret_cast<identfun>(f))
     184              :     {
     185          112 :         val.storage.f = s;
     186          112 :         val.f.min = m;
     187          112 :         val.f.max = x;
     188          112 :     }
     189              : 
     190              :     /**
     191              :      * @brief Constructor for an ident for a string variable.
     192              :      */
     193            5 :     ident(int t, const char *n, char **s, void *f = nullptr, int flags = 0)
     194            5 :         : type(t), flags(flags), name(n), fun(reinterpret_cast<identfun>(f))
     195              :     {
     196            5 :         val.storage.s = s;
     197            5 :     }
     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           32 :     ident(int t, const char *n, int flags)
     225           32 :         : type(t), valtype(Value_Null), flags(flags), name(n)
     226              :     {
     227           32 :         alias.code = nullptr;
     228           32 :         alias.stack = nullptr;
     229           32 :     }
     230              : 
     231            2 :     ident(int t, const char *n, const tagval &v, int flags)
     232            2 :         : type(t), valtype(v.type), flags(flags), name(n)
     233              :     {
     234            2 :         alias.code = nullptr;
     235            2 :         alias.stack = nullptr;
     236            2 :         alias.val = v;
     237            2 :     }
     238              : 
     239              :     /**
     240              :      * @brief Constructor for an ident for a C++ bound command.
     241              :      */
     242          595 :     ident(int t, const char *n, const char *args, uint argmask, int numargs, void *f = nullptr, int flags = 0)
     243          595 :         : type(t), numargs(numargs), flags(flags), name(n), fun(reinterpret_cast<identfun>(f))
     244              :     {
     245          595 :         cmd.args = args;
     246          595 :         cmd.argmask = argmask;
     247          595 :     }
     248              : 
     249              :     /**
     250              :      * @brief Calls a change effect for this ident, if one exists.
     251              :      *
     252              :      * If there is no function pointed to by `fun` (it is null), then nothing
     253              :      * will occur.
     254              :      */
     255            1 :     void changed()
     256              :     {
     257            1 :         if(fun)
     258              :         {
     259            0 :             fun(this);
     260              :         }
     261            1 :     }
     262              : 
     263              :     /**
     264              :      * @brief Sets the value and type of value of this ident given a tagval
     265              :      *
     266              :      * Sets this ident's value type and value to the corresponding values from
     267              :      * the passed tagval object.
     268              :      *
     269              :      * @param v the tagval to set values from
     270              :      */
     271          285 :     void setval(const tagval &v)
     272              :     {
     273          285 :         valtype = v.type;
     274          285 :         alias.val = v;
     275          285 :     }
     276              : 
     277              :     /**
     278              :      * @brief Sets the value and type of value of this ident given an identstack
     279              :      *
     280              :      * Sets this ident's value type and value to the corresponding values from
     281              :      * the passed identstack object.
     282              :      *
     283              :      * @param v the identstack to set values from
     284              :      */
     285           95 :     void setval(const identstack &v)
     286              :     {
     287           95 :         valtype = v.valtype;
     288           95 :         alias.val = v.val;
     289           95 :     }
     290              : 
     291              :     /**
     292              :      * @brief Sets the value type of this ident to null.
     293              :      *
     294              :      * If a string is being stored inside this ident, it is freed.
     295              :      */
     296           28 :     void forcenull()
     297              :     {
     298           28 :         if(valtype==Value_String)
     299              :         {
     300            2 :             delete[] alias.val.s;
     301              :         }
     302           28 :         valtype = Value_Null;
     303           28 :     }
     304              : 
     305              :     /**
     306              :      * @brief Returns the saved value of the ident as a float.
     307              :      *
     308              :      * Returns a float even if the ident is of another type, e.g. integer
     309              :      *
     310              :      * @return the float value of the ident
     311              :      */
     312              :     float getfloat() const;
     313              : 
     314              :     /**
     315              :      * @brief Returns the saved value of the ident as an integer.
     316              :      *
     317              :      * Returns an int even if the ident is of another type, e.g. float
     318              :      */
     319              :     int getint() const;
     320              : 
     321              :     /**
     322              :      * @brief Returns the saved value of the ident as a double.
     323              :      *
     324              :      * Returns a double even if the ident is of another type, e.g. integer
     325              :      *
     326              :      * @return the double value of the ident
     327              :      */
     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              : 
     335              : /**
     336              :  * @brief Returns an integer value from a Cubescript command.
     337              :  *
     338              :  * When writing a CS command, this function returns an integer value to the inheriting
     339              :  * CS environment.
     340              :  *
     341              :  * @param v the value to return
     342              :  */
     343              : extern void intret(int v);
     344              : 
     345              : /**
     346              :  * @brief Writes the passed float to a formatted string
     347              :  *
     348              :  * Increments the return buffer retbuf by one (looping if it has reached the end)
     349              :  * and saves the formatted form of the float passed to that array element.
     350              :  *
     351              :  * @param v the float to convert to a string
     352              :  *
     353              :  * @return pointer to a string which is stored in retbuf
     354              :  */
     355              : extern const char *floatstr(float v);
     356              : 
     357              : /**
     358              :  * @brief Returns a float value from a Cubescript command.
     359              :  *
     360              :  * When writing a CS command, this function returns a float value to the inheriting
     361              :  * CS environment.
     362              :  *
     363              :  * @param v the value to return
     364              :  */
     365              : extern void floatret(float v);
     366              : 
     367              : /**
     368              :  * @brief Returns a string value from a Cubescript command.
     369              :  *
     370              :  * When writing a CS command, this function returns a string value to the inheriting
     371              :  * CS environment.
     372              :  *
     373              :  * @param v the value to return
     374              :  */
     375              : extern void stringret(char *s);
     376              : 
     377              : /**
     378              :  * @brief Returns an alias' name from a Cubescript command.
     379              :  *
     380              :  * When writing a CS command, this functions a string value representing the name
     381              :  * of a Cubescript object to the inheriting CS environment.
     382              :  *
     383              :  * @param s the name of the ident to return
     384              :  */
     385              : extern void result(const char *s);
     386              : 
     387              : /**
     388              :  * @brief Returns the string passed as an integer.
     389              :  *
     390              :  * Parses the entire string, returning the value of the passed value as an integer.
     391              :  * The value of this value will always be greater than zero unless there is an
     392              :  * overflow beyond the size of `int`. The output may be converted by a trailing
     393              :  * radix digit as described in `strtoul`.
     394              :  *
     395              :  * @param s the string to turn into an integer
     396              :  */
     397          596 : inline int parseint(const char *s)
     398              : {
     399          596 :     return static_cast<int>(std::strtoul(s, nullptr, 0));
     400              : }
     401              : 
     402              : /**
     403              :  * @brief Registers an int variable in the Cubescript ident table.
     404              :  *
     405              :  * @param name the name of the aliased variable in Cubescript
     406              :  * @param min the minimum value the variable can be set at
     407              :  * @param cur the starting value of the variable
     408              :  * @param max the maximum value the variable can be set at
     409              :  * @param storage the pointer to the variable to be aliased to Cubescript
     410              :  * @param fun a function pointer to be called upon modification of the variable
     411              :  * @param flags the handling flags for the variable
     412              :  *
     413              :  * @return the value cur passed
     414              :  */
     415              : extern int variable(const char *name, int min, int cur, int max, int *storage, identfun fun, int flags);
     416              : 
     417              : /**
     418              :  * @brief Registers a float variable in the Cubescript ident table.
     419              :  *
     420              :  * Adds a float variable to the Cubescript ident table. The name of the Cubescript
     421              :  * variable does not necessarily need to correspond to the C++ variable's name.
     422              :  *
     423              :  * @param name the name of the aliased variable in Cubescript
     424              :  * @param min the minimum value the variable can be set at
     425              :  * @param cur the starting value of the variable
     426              :  * @param max the maximum value the variable can be set at
     427              :  * @param storage the pointer to the variable to be aliased to Cubescript
     428              :  * @param fun a function pointer to be called upon modification of the variable
     429              :  * @param flags the handling flags for the variable
     430              :  *
     431              :  * @return the value cur passed
     432              :  */
     433              : extern float fvariable(const char *name, float min, float cur, float max, float *storage, identfun fun, int flags);
     434              : 
     435              : /**
     436              :  * @brief Registers a C string variable in the Cubescript ident table.
     437              :  *
     438              :  * @param name the name of the aliased variable in Cubescript
     439              :  * @param cur the starting value of the variable
     440              :  * @param storage the pointer to the pointer to the variable to be aliased to Cubescript
     441              :  * @param fun a function pointer to be called upon modification of the variable
     442              :  * @param flags the handling flags for the variable
     443              :  *
     444              :  * @return the value cur passed
     445              :  */
     446              : extern char *svariable(const char *name, const char *cur, char **storage, identfun fun, int flags);
     447              : 
     448              : /**
     449              :  * @brief Sets a Cubescript integer value to the given value.
     450              :  *
     451              :  * @param name the name of the cubescript alias to change
     452              :  * @param i the value to set
     453              :  * @param dofunc whether to run the onchange function
     454              :  * @param doclamp whether to clamp the value to the specified limits
     455              :  */
     456              : extern void setvar(const char *name, int i, bool dofunc = true, bool doclamp = true);
     457              : 
     458              : /**
     459              :  * @brief Sets a Cubescript float value to the given value.
     460              :  *
     461              :  * @param name the name of the cubescript alias to change
     462              :  * @param i the value to set
     463              :  * @param dofunc whether to run the onchange function
     464              :  * @param doclamp whether to clamp the value to the specified limits
     465              :  */
     466              : extern void setfvar(const char *name, float f, bool dofunc = true, bool doclamp = true);
     467              : 
     468              : /**
     469              :  * @brief Sets a Cubescript string value to the given value.
     470              :  *
     471              :  * @param name the name of the cubescript alias to change
     472              :  * @param i the value to set
     473              :  * @param dofunc whether to run the onchange function
     474              :  */
     475              : extern void setsvar(const char *name, const char *str, bool dofunc = true);
     476              : 
     477              : /**
     478              :  * @brief Registers a command in the Cubescript ident table.
     479              :  *
     480              :  * The arguments of the function passed are cast away, so they are reconstructed using
     481              :  * the char * string passed to `narg`.
     482              :  *
     483              :  * @param name of the command in Cubescript
     484              :  * @param fun a function pointer to be called when the command is executed
     485              :  * @param narg string containing the arguments of the function
     486              :  * @param type the type of the command to create
     487              :  */
     488              : extern bool addcommand(const char *name, identfun fun, const char *narg = "", int type = Id_Command);
     489              : 
     490              : extern std::queue<ident *> triggerqueue; /**< A queue of game events for the engine to process */
     491              : 
     492              : /**
     493              :  * @brief Returns the pointer to the ident object with the given CS alias.
     494              :  *
     495              :  * @param the name of the cubescript object to get
     496              :  *
     497              :  * @return The address of the ident object.
     498              :  */
     499              : extern ident *getident(const char *name);
     500              : extern int execute(const uint *code);
     501              : 
     502              : /**
     503              :  * @brief Executes the contents of the string passed.
     504              :  *
     505              :  * Parses and executes the line of Cubescript given.
     506              :  *
     507              :  * @param p the C string containing the code to execute
     508              :  *
     509              :  * @return an int from the results of the execution
     510              :  */
     511              : extern int execute(const char *p);
     512              : 
     513              : /**
     514              :  * @brief Executes the contents of an ident, searched by name.
     515              :  *
     516              :  * Attempts to find in the ident table an ident with the given name, and
     517              :  * if found executes it.
     518              :  *
     519              :  * @param name the name of the ident to look for
     520              :  * @param noid the value to return if no ident is found
     521              :  * @param lookup if a command, and parameters are of format 'N', sets to -1
     522              :  */
     523              : extern int execident(const char *name, int noid = 0, bool lookup = false);
     524              : extern bool executebool(const uint *code);
     525              : 
     526              : /**
     527              :  * @brief Executes the contents of the referenced file.
     528              :  *
     529              :  * @param cfgfile the relative director of the file to execute
     530              :  * @param msg whether to print to console a failure message
     531              :  *
     532              :  * @return true if the file was successfully loaded
     533              :  * @return false if the file was not found
     534              :  */
     535              : extern bool execfile(const char *cfgfile, bool msg = true);
     536              : 
     537              : /**
     538              :  * @brief Replaces C style excape characters with Cubescript ones
     539              :  *
     540              :  * The resulting string is also wrapped in quotes ("").
     541              :  *
     542              :  * @param s the string to convert
     543              :  *
     544              :  * @return a string containing the escaped changes
     545              :  */
     546              : extern const char *escapestring(const char *s);
     547              : 
     548              : /**
     549              :  * @brief Prints out the formatted variable
     550              :  *
     551              :  * The param is intended to represent the value the ident object represents.
     552              :  *
     553              :  * @param id the ident object to print out
     554              :  * @param i the value to print out the variable equalling.
     555              :  */
     556              : extern void printvar(const ident *id, int i);
     557              : 
     558              : /**
     559              :  * @brief Modifies the value passed to fall within the boundaries passed.
     560              :  *
     561              :  * Clamps the value i to within minval and maxval. If hex is passed, warns in hex
     562              :  * mode, otherwise decimal. If the values are not within bounds, issued aformentioned
     563              :  * warning, refering to the passed name in the console message.
     564              :  *
     565              :  * @param hex toggles whether to display hex warning instead of decimal
     566              :  * @param name the name
     567              :  * @param i the value to clamp
     568              :  * @param minval the lowest value to clamp to
     569              :  * @param maxval the largest value to clamp to
     570              :  *
     571              :  * @return the clamped value
     572              :  */
     573              : extern int clampvar(bool hex, std::string name, int i, int minval, int maxval);
     574              : extern void loopiter(ident *id, identstack &stack, int i);
     575              : extern void loopend(ident *id, identstack &stack);
     576              : 
     577              : /**
     578              :  * @brief Escapes a string unless it is null.
     579              :  *
     580              :  * If an empty string is passed, escapestring() is not called. Otherwise, the same
     581              :  * behavior as in escapestring() is executed.
     582              :  *
     583              :  * @param s the string to convert
     584              :  *
     585              :  * @return a string containing the potentially applied escape changes
     586              :  */
     587              : const char *escapeid(const char *s);
     588              : 
     589              : /**
     590              :  * @brief Writes out the state of the CubeScript idents to a file.
     591              :  *
     592              :  * @param savedconfig the path to write to if name is nullptr
     593              :  * @param autoexec the path for the autoexec file for user modification, for display in top of file comment
     594              :  * @param defaultconfig the path for the master copy of the configuration file, for display in top of file comment
     595              :  * @param name the path to write to, if nullptr savedconfig is used instead
     596              :  */
     597              : extern void writecfg(const char *savedconfig, const char *autoexec = nullptr, const char *defaultconfig = nullptr, const char *name = nullptr);
     598              : 
     599              : /**
     600              :  * @brief Processes the cubescript sleep queue.
     601              :  *
     602              :  * At most, one sleep command is removed from the queue per cycle. Removes
     603              :  * queued sleep commands as they expire, which stops CS commands from executing
     604              :  * for a certain period of time.
     605              :  *
     606              :  * @param millis the current timestamp
     607              :  */
     608              : extern void checksleep(int millis);
     609              : 
     610              : /**
     611              :  * @brief Initializes the CubeScript preset argument idents.
     612              :  *
     613              :  * Intitializes the argument parameters (arg1, arg2, ..., arg<Max_Args>) as CS
     614              :  * ident objects. Required to use of $argN variables.
     615              :  * Also initializes the dummy ident, `//dummy`.
     616              :  *
     617              :  * @return always returns true
     618              :  */
     619              : extern bool initidents();
     620              : 
     621              : extern int identflags; /**< The flags to automatically set on ident objects */
     622              : 
     623              : /**
     624              :  * @brief Clears all aliases from the ident map.
     625              :  *
     626              :  * All aliases, aka objects created from within CubeScript, have their contents
     627              :  * (the names and code values associated with them) freed. Does not remove the
     628              :  * ident object itself from the global ident map.
     629              :  */
     630              : extern void clear_command();
     631              : 
     632              : // nasty macros for registering script functions, abuses globals to avoid excessive infrastructure
     633              : 
     634              : //* note: many of the VARF comments are very repetitive, because the code itself is nearly duplicated too *//
     635              : 
     636              : /* how command registration works:
     637              :  *  In C++, all global variables must be initiated before main() is called
     638              :  *  Each of these macro kludges (which are placed at the global scope level) initializes some kind of global variable
     639              :  *  Because the macro kludges are the definition of global variables, at program initializiation, they must be run first
     640              :  *  The values of the variables themselves don't matter because they only exist to cheat and run before main()
     641              :  *  The macro kludges themselves register commands or values within some vector<>-s which keeps track of all cmds/vars
     642              :  */
     643              : 
     644              : /*
     645              :  * Parameter Flags (exhaustive list, not exhaustive descriptions)
     646              :  * i an integer parameter
     647              :  * b a boolean parameter
     648              :  * f a float parameter (sets to 0 if over parameter limit)
     649              :  * F a float parameter (sets to value of previous parameter if over parameter limit)
     650              :  * s a string parameter (sets to new allocated string "" if over parameter limit)
     651              :  * S a string paremter (sets current string to "" if over parameter limit)
     652              :  * t sets to null if over parameter limit
     653              :  * T same as "t"
     654              :  * e a code block parameter
     655              :  * E a code block paremter (null)
     656              :  * r an ident parameter
     657              :  * $ an ident parameter
     658              :  * N a number parameter
     659              :  * D a bind parameter (e.g. player movement), adds a release action for the command
     660              :  * C an ident parameter, ComC
     661              :  * V a series of variadic parameter(s) (open ended series of values)
     662              :  * 1 repeat one arg
     663              :  * 2 repeat two args
     664              :  * 3 repeat three args
     665              :  * 4 repeat four args
     666              :  */
     667              : 
     668              : //integer var macros
     669              : //VAR_, _VARF, VARM_ are templates for "normal" macros that do not execute an inline function
     670              : #define VAR_(name, global, min, cur, max, persist)  int global = variable(#name, min, cur, max, &global, nullptr, persist)
     671              : #define VARN(name, global, min, cur, max) VAR_(name, global, min, cur, max, 0)
     672              : #define VARNP(name, global, min, cur, max) VAR_(name, global, min, cur, max, Idf_Persist)
     673              : #define VARNR(name, global, min, cur, max) VAR_(name, global, min, cur, max, Idf_Override)
     674              : #define VAR(name, min, cur, max) VAR_(name, name, min, cur, max, 0)
     675              : #define VARP(name, min, cur, max) VAR_(name, name, min, cur, max, Idf_Persist)
     676              : #define VARR(name, min, cur, max) VAR_(name, name, min, cur, max, Idf_Override)
     677              : 
     678              : //variable with inline function to be executed on change (VARiable with Function = VARF)
     679              : #define VARF_(name, global, min, cur, max, body, persist) \
     680              :     int global = variable(#name, min, cur, max, &global, [] (ident *) { body; }, persist); /* assign variable, needs fxn prototype declared above */
     681              : 
     682              : #define VARFN(name, global, min, cur, max, body) VARF_(name, global, min, cur, max, body, 0)
     683              : #define VARF(name, min, cur, max, body) VARF_(name, name, min, cur, max, body, 0)
     684              : #define VARFP(name, min, cur, max, body) VARF_(name, name, min, cur, max, body, Idf_Persist)
     685              : #define VARFR(name, min, cur, max, body) VARF_(name, name, min, cur, max, body, Idf_Override)
     686              : #define VARFNP(name, global, min, cur, max, body) VARF_(name, global, min, cur, max, body, Idf_Persist)
     687              : 
     688              : //hexadecimal var macros
     689              : #define HVAR_(name, global, min, cur, max, persist)  int global = variable(#name, min, cur, max, &global, nullptr, persist | Idf_Hex)
     690              : #define HVARP(name, min, cur, max) HVAR_(name, name, min, cur, max, Idf_Persist)
     691              : 
     692              : //hex variable with function to be executed on change (Hexadecimal VARiable with Function = HVARF)
     693              : //the CVAR series of macros borrows the HVARF macro as it deals with hex colors
     694              : #define HVARF_(name, global, min, cur, max, body, persist) \
     695              :     int global = variable(#name, min, cur, max, &global, [] (ident *) { body; }, persist | Idf_Hex); /* assign variable, needs fxn prototype declared above */
     696              : 
     697              : //color var macros
     698              : #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)
     699              : #define CVARP(name, cur) CVAR_(name, cur, , , Idf_Persist)
     700              : #define CVARR(name, cur) CVAR_(name, cur, , , Idf_Override)
     701              : #define CVARFP(name, cur, body) CVAR_(name, cur, , body, Idf_Persist)
     702              : #define CVAR0_(name, cur, body, persist) CVAR_(name, cur, { if(!_##name) _##name = cur; }, body, persist)
     703              : #define CVAR0R(name, cur) CVAR0_(name, cur, , Idf_Override)
     704              : #define CVAR1_(name, cur, body, persist) CVAR_(name, cur, { if(_##name <= 255) _##name |= (_##name<<8) | (_##name<<16); }, body, persist)
     705              : #define CVAR1R(name, cur) CVAR1_(name, cur, , Idf_Override)
     706              : #define CVAR1FR(name, cur, body) CVAR1_(name, cur, body, Idf_Override)
     707              : 
     708              : //float var macros
     709              : #define FVAR_(name, global, min, cur, max, persist) float global = fvariable(#name, min, cur, max, &global, nullptr, persist)
     710              : #define FVARNP(name, global, min, cur, max) FVAR_(name, global, min, cur, max, Idf_Persist)
     711              : #define FVAR(name, min, cur, max) FVAR_(name, name, min, cur, max, 0)
     712              : #define FVARP(name, min, cur, max) FVAR_(name, name, min, cur, max, Idf_Persist)
     713              : #define FVARR(name, min, cur, max) FVAR_(name, name, min, cur, max, Idf_Override)
     714              : 
     715              : //float variable with function to be executed on change (Float VARiable with Function = FVARF)
     716              : #define FVARF_(name, global, min, cur, max, body, persist) \
     717              :     float global = fvariable(#name, min, cur, max, &global, [] (ident *) { body; }, persist); /* assign variable, needs fxn prototype declared above */ \
     718              : 
     719              : #define FVARF(name, min, cur, max, body) FVARF_(name, name, min, cur, max, body, 0)
     720              : #define FVARFP(name, min, cur, max, body) FVARF_(name, name, min, cur, max, body, Idf_Persist)
     721              : #define FVARFR(name, min, cur, max, body) FVARF_(name, name, min, cur, max, body, Idf_Override)
     722              : 
     723              : //string var macros
     724              : #define SVAR_(name, global, cur, persist) char *global = svariable(#name, cur, &global, nullptr, persist)
     725              : #define SVAR(name, cur) SVAR_(name, name, cur, 0)
     726              : #define SVARP(name, cur) SVAR_(name, name, cur, Idf_Persist)
     727              : #define SVARR(name, cur) SVAR_(name, name, cur, Idf_Override)
     728              : 
     729              : //string variable with function to be executed on change (Float VARiable with Function = FVARF)
     730              : #define SVARF_(name, global, cur, body, persist) \
     731              :     char *global = svariable(#name, cur, &global, [] (ident *) { body; }, persist); /* assign variable, needs fxn prototype declared above */
     732              : 
     733              : #define SVARF(name, cur, body) SVARF_(name, name, cur, body, 0)
     734              : #define SVARFR(name, cur, body) SVARF_(name, name, cur, body, Idf_Override)
     735              : 
     736              : #endif /* COMMAND_H_ */
        

Generated by: LCOV version 2.0-1