From 876eaa467cf7c13b7047cc0554738914ee5fb105 Mon Sep 17 00:00:00 2001 From: Brian Koropoff Date: Sat, 24 Aug 2002 05:14:46 +0000 Subject: [PATCH] Added a few useful builtins and began adding GIB hooks into quakeworld. The player global branch variable is now updated with useful information and the location.get builtin is available to return the current location of the player based on loc files. Fixed a bug with zooming out in zoom.gib. --- doc/config/zoom.gib | 2 +- include/QF/gib_builtin.h | 5 +++++ include/QF/gib_vars.h | 9 +++++---- libs/util/gib_builtin.c | 32 ++++++++++++++++++++++++++++---- libs/util/gib_function.c | 4 ++-- libs/util/gib_parse.c | 6 +++--- libs/util/gib_process.c | 7 +++---- libs/util/gib_vars.c | 25 +++++++++++++++++++++++-- qw/source/cl_parse.c | 35 ++++++++++++++++++++++++++++++++++- qw/source/teamplay.c | 18 ++++++++++++++++++ 10 files changed, 122 insertions(+), 21 deletions(-) diff --git a/doc/config/zoom.gib b/doc/config/zoom.gib index 095f43b5d..3da7b9422 100644 --- a/doc/config/zoom.gib +++ b/doc/config/zoom.gib @@ -84,7 +84,7 @@ function "zoom.increase" { function "zoom.decrease" { ifnot ${zoom.zoomed} return - zoom.mult = (${zoom.mult}/(1 + ${zoom.mult.step})) + zoom.mult = (${zoom.mult}/${zoom.mult.step}) if (${zoom.mult} < ${zoom.mult.lower}) { zoom.mult = ${zoom.mult.lower} } diff --git a/include/QF/gib_builtin.h b/include/QF/gib_builtin.h index 7d819ace5..52df375b3 100644 --- a/include/QF/gib_builtin.h +++ b/include/QF/gib_builtin.h @@ -39,6 +39,11 @@ typedef struct gib_builtin_s { } type; } gib_builtin_t; +unsigned int GIB_Argc (void); +const char *GIB_Argv (unsigned int arg); +const char *GIB_Args (unsigned int arg); +void GIB_Arg_Strip_Delim (unsigned int arg); +void GIB_Return (const char *str); void GIB_Builtin_Add (const char *name, void (*func) (void), enum gib_builtin_type_e type); gib_builtin_t *GIB_Builtin_Find (const char *name); void GIB_Builtin_Init (void); diff --git a/include/QF/gib_vars.h b/include/QF/gib_vars.h index cceac3aef..7e90f9be5 100644 --- a/include/QF/gib_vars.h +++ b/include/QF/gib_vars.h @@ -39,9 +39,10 @@ typedef struct gib_var_s { struct hashtab_s *subvars; } gib_var_t; -void GIB_Var_Set (cbuf_t *cbuf, const char *key, const char *value); -const char *GIB_Var_Get (cbuf_t *cbuf, const char *key); +void GIB_Var_Set_Local (cbuf_t *cbuf, const char *key, const char *value); +void GIB_Var_Set_Global (const char *key, const char *value); +const char *GIB_Var_Get_Local (cbuf_t *cbuf, const char *key); +const char *GIB_Var_Get_Global (const char *key); const char *GIB_Var_Get_Key (void *ele, void *ptr); void GIB_Var_Free (void *ele, void *ptr); -void GIB_Var_Set_R (hashtab_t *vars, char *name, const char *value); -gib_var_t *GIB_Var_Get_R (hashtab_t *vars, char *name); + diff --git a/libs/util/gib_builtin.c b/libs/util/gib_builtin.c index d543bf8e1..d5d2b9967 100644 --- a/libs/util/gib_builtin.c +++ b/libs/util/gib_builtin.c @@ -212,7 +212,7 @@ GIB_Local_f (void) "lset: invalid syntax\n" "usage: local variable"); else - GIB_Var_Set (cbuf_active, GIB_Argv(1), ""); + GIB_Var_Set_Local (cbuf_active, GIB_Argv(1), ""); } void @@ -224,9 +224,7 @@ GIB_Global_f (void) "usage: global variable"); else { char *a = strdup (GIB_Argv(1)); - if (!gib_globals) - gib_globals = Hash_NewTable (256, GIB_Var_Get_Key, GIB_Var_Free, 0); - GIB_Var_Set_R (gib_globals, a, ""); + GIB_Var_Set_Global (a, ""); free(a); } } @@ -357,9 +355,33 @@ GIB_Export_f (void) } } +void +GIB_String_Length_f (void) +{ + if (GIB_Argc() != 2) + Cbuf_Error ("syntax", + "string.length: invalid syntax\n" + "usage: string.length string"); + else + GIB_Return (va("%i", strlen(GIB_Argv(1)))); +} + +void +GIB_String_Equal_f (void) +{ + if (GIB_Argc() != 3) + Cbuf_Error ("syntax", + "string.length: invalid syntax\n" + "usage: string.equal string1 string2"); + else + GIB_Return (va("%i", !strcmp(GIB_Argv(1), GIB_Argv(2)))); +} + void GIB_Builtin_Init (void) { + gib_globals = Hash_NewTable (512, GIB_Var_Get_Key, GIB_Var_Free, 0); + GIB_Builtin_Add ("function", GIB_Function_f, GIB_BUILTIN_NORMAL); GIB_Builtin_Add ("function.get", GIB_FunctionDotGet_f, GIB_BUILTIN_NORMAL); GIB_Builtin_Add ("export", GIB_Export_f, GIB_BUILTIN_NORMAL); @@ -370,4 +392,6 @@ GIB_Builtin_Init (void) GIB_Builtin_Add ("ifnot", GIB_If_f, GIB_BUILTIN_FIRSTONLY); GIB_Builtin_Add ("while", GIB_While_f, GIB_BUILTIN_NOPROCESS); GIB_Builtin_Add ("break", GIB_Break_f, GIB_BUILTIN_NORMAL); + GIB_Builtin_Add ("string.length", GIB_String_Length_f, GIB_BUILTIN_NORMAL); + GIB_Builtin_Add ("string.equal", GIB_String_Equal_f, GIB_BUILTIN_NORMAL); } diff --git a/libs/util/gib_function.c b/libs/util/gib_function.c index e1d1d6c82..addcf7caf 100644 --- a/libs/util/gib_function.c +++ b/libs/util/gib_function.c @@ -143,6 +143,6 @@ GIB_Function_Execute (gib_function_t *func) cbuf_active->state = CBUF_STATE_STACK; for (i = 0; i < cbuf_active->args->argc; i++) - GIB_Var_Set (sub, va("%i", i), cbuf_active->args->argv[i]->str); - GIB_Var_Set (sub, "argc", va("%i", cbuf_active->args->argc)); + GIB_Var_Set_Local (sub, va("%i", i), cbuf_active->args->argv[i]->str); + GIB_Var_Set_Local (sub, "argc", va("%i", cbuf_active->args->argc)); } diff --git a/libs/util/gib_parse.c b/libs/util/gib_parse.c index c7b97e1e9..22c6f96ea 100644 --- a/libs/util/gib_parse.c +++ b/libs/util/gib_parse.c @@ -474,13 +474,13 @@ void GIB_Parse_Execute_Line (cbuf_t *cbuf) char *c = 0; if ((c = strchr (args->argv[0]->str, '.'))) // Only check stem *c = 0; - glob = (!GIB_Var_Get (cbuf, args->argv[0]->str) && GIB_Var_Get_R (gib_globals, args->argv[0]->str)); + glob = (!GIB_Var_Get_Local (cbuf, args->argv[0]->str) && GIB_Var_Get_Global (args->argv[0]->str)); if (c) *c = '.'; if (glob) - GIB_Var_Set_R (gib_globals, args->argv[0]->str, args->argv[2]->str); // Set the global + GIB_Var_Set_Global (args->argv[0]->str, args->argv[2]->str); // Set the global else - GIB_Var_Set (cbuf, args->argv[0]->str, args->argv[2]->str); // Set the local + GIB_Var_Set_Local (cbuf, args->argv[0]->str, args->argv[2]->str); // Set the local } else Cmd_Command (cbuf->args); dstring_clearstr (cbuf->line); diff --git a/libs/util/gib_process.c b/libs/util/gib_process.c index 3be682155..4bc973367 100644 --- a/libs/util/gib_process.c +++ b/libs/util/gib_process.c @@ -83,17 +83,16 @@ unsigned int GIB_Process_Variable (struct dstring_s *dstr, unsigned int pos, qboolean tolerant) { cvar_t *cvar; - gib_var_t *gvar; const char *str; char *p, c; for (p = dstr->str+pos+1; tolerant ? *p : isalnum ((byte)*p) || *p == '_'; p++); c = *p; *p = 0; - if ((str = GIB_Var_Get (cbuf_active, dstr->str+pos+1))) + if ((str = GIB_Var_Get_Local (cbuf_active, dstr->str+pos+1))) ; // yay for us - else if ((gvar = GIB_Var_Get_R (gib_globals, dstr->str+pos+1))) - str = gvar->value->str; + else if ((str = GIB_Var_Get_Global (dstr->str+pos+1))) + ; // yay again else if ((cvar = Cvar_FindVar (dstr->str+pos+1))) str = cvar->string; else diff --git a/libs/util/gib_vars.c b/libs/util/gib_vars.c index cab6765a6..d0a7bb980 100644 --- a/libs/util/gib_vars.c +++ b/libs/util/gib_vars.c @@ -116,7 +116,7 @@ GIB_Var_Set_R (hashtab_t *vars, char *name, const char *value) } void -GIB_Var_Set (cbuf_t *cbuf, const char *key, const char *value) +GIB_Var_Set_Local (cbuf_t *cbuf, const char *key, const char *value) { char *k = strdup (key); if (!GIB_DATA(cbuf)->locals) @@ -125,8 +125,16 @@ GIB_Var_Set (cbuf_t *cbuf, const char *key, const char *value) free(k); } +void +GIB_Var_Set_Global (const char *key, const char *value) +{ + char *k = strdup (key); + GIB_Var_Set_R (gib_globals, k, value); + free (k); +} + const char * -GIB_Var_Get (cbuf_t *cbuf, const char *key) +GIB_Var_Get_Local (cbuf_t *cbuf, const char *key) { gib_var_t *l; char *k; @@ -140,3 +148,16 @@ GIB_Var_Get (cbuf_t *cbuf, const char *key) else return 0; } + +const char * +GIB_Var_Get_Global (const char *key) +{ + gib_var_t *l; + char *k = strdup (key); + l = GIB_Var_Get_R (gib_globals, k); + free (k); + if (l) + return l->value->str; + else + return 0; +} diff --git a/qw/source/cl_parse.c b/qw/source/cl_parse.c index 38f262d83..10a9aaec3 100644 --- a/qw/source/cl_parse.c +++ b/qw/source/cl_parse.c @@ -56,6 +56,7 @@ static const char rcsid[] = #include "QF/va.h" #include "QF/vfile.h" #include "QF/dstring.h" +#include "QF/gib_vars.h" #include "bothdefs.h" #include "cl_ents.h" @@ -1054,6 +1055,7 @@ void CL_SetStat (int stat, int value) { int j; + const char *arm; if (stat < 0 || stat >= MAX_CL_STATS) Host_Error ("CL_SetStat: %i is invalid", stat); @@ -1061,16 +1063,47 @@ CL_SetStat (int stat, int value) Sbar_Changed (); switch (stat) { - case STAT_ITEMS: // set flash times + case STAT_ITEMS: Sbar_Changed (); + GIB_Var_Set_Global ("player.key.1", value & IT_KEY1 ? "1" : "0"); + GIB_Var_Set_Global ("player.key.2", value & IT_KEY2 ? "1" : "0"); + if (value & IT_ARMOR1) + arm = "green"; + else if (value & IT_ARMOR2) + arm = "yellow"; + else if (value & IT_ARMOR3) + arm = "red"; + else + arm = "blue"; + GIB_Var_Set_Global ("player.armor.type", arm); + GIB_Var_Set_Global ("player.weapon.1", value & IT_AXE ? "1" : "0"); + for (j = 0; j < 7; j++) + GIB_Var_Set_Global (va("player.weapon.%i", j+2), + value & (1 << j) ? "1" : "0"); for (j = 0; j < 32; j++) if ((value & (1 << j)) && !(cl.stats[stat] & (1 << j))) cl.item_gettime[j] = cl.time; break; case STAT_HEALTH: + GIB_Var_Set_Global ("player.health", va("%i", value)); if (value <= 0) Team_Dead (); break; + case STAT_ARMOR: + GIB_Var_Set_Global ("player.armor", va("%i", value)); + break; + case STAT_SHELLS: + GIB_Var_Set_Global ("player.ammo.shells", va("%i", value)); + break; + case STAT_NAILS: + GIB_Var_Set_Global ("player.ammo.nails", va("%i", value)); + break; + case STAT_ROCKETS: + GIB_Var_Set_Global ("player.ammo.rockets", va("%i", value)); + break; + case STAT_CELLS: + GIB_Var_Set_Global ("player.ammo.cells", va("%i", value)); + break; } cl.stats[stat] = value; diff --git a/qw/source/teamplay.c b/qw/source/teamplay.c index 9016fa507..69f238438 100644 --- a/qw/source/teamplay.c +++ b/qw/source/teamplay.c @@ -51,6 +51,7 @@ static const char rcsid[] = #include "QF/teamplay.h" #include "QF/va.h" #include "QF/skin.h" +#include "QF/gib_builtin.h" #include "bothdefs.h" #include "cl_input.h" @@ -399,11 +400,28 @@ locs_loc (void) } } +void +Locs_Location_Get (void) +{ + location_t *location; + if (GIB_Argc() != 1) + Cbuf_Error ( + "syntax", + "location.get: invalid syntax\n" + "usage: location.get" + ); + else { + location = locs_find (cl.simorg); + GIB_Return (location ? location->name : "unknown"); + } +} + void Locs_Init (void) { Cmd_AddCommand ("loc", locs_loc, "Location marker editing commands: 'loc " "help' for more"); + GIB_Builtin_Add ("location.get", Locs_Location_Get, GIB_BUILTIN_NORMAL); } char *