From d1981e7edfcbefa5e307471395726f9cbf20da9d Mon Sep 17 00:00:00 2001 From: Yamagi Burmeister Date: Mon, 2 Sep 2019 17:29:00 +0200 Subject: [PATCH] Add 'toggle' cvar operation. The 'toggle' operation allows toggling a cvar between `0` and `1` or two custom values. This code was taken from q2pro. This is part of issue #414. --- doc/04_cvarlist.md | 11 ++++++--- src/common/cvar.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/doc/04_cvarlist.md b/doc/04_cvarlist.md index 16141adc..e81e5a6a 100644 --- a/doc/04_cvarlist.md +++ b/doc/04_cvarlist.md @@ -257,9 +257,14 @@ it's `+set busywait 0` (setting the `busywait` cvar) and `-portable` ## cvar operations -cvar operations are special commands that allow the manipulation of cvar -values. They can be used for simple scripts and the like. +cvar operations are special commands that allow the programmatic +manipulation of cvar values. They can be used for scripting and the +like. -* **reset **: Reset the cvar to it's default value. +* **reset **: Reset the given cvar to it's default value. * **resetall**: Reset all known cvar to their default values. + +* **toggle [val0] [val1]**: Toggle the given cvar between `0` and + `1`. If the optional arguments `val0` and `val1` are given the given + cvar is toggled between them. diff --git a/src/common/cvar.c b/src/common/cvar.c index bfbb5d68..134e5ce9 100644 --- a/src/common/cvar.c +++ b/src/common/cvar.c @@ -675,6 +675,10 @@ void Cvar_Reset_f(void) Cvar_Set(var->name, var->default_string); } +/* + * Resets all known cvar (with the exception of `game') to + * their default values. + */ void Cvar_ResetAll_f(void) { cvar_t *var; @@ -694,6 +698,60 @@ void Cvar_ResetAll_f(void) } } +/* + * Toggles a cvar between 0 and 1 or the given values. + */ +void Cvar_Toggle_f(void) +{ + cvar_t *var; + int i, argc = Cmd_Argc(); + + if (argc < 2) + { + Com_Printf("Usage: %s [values]\n", Cmd_Argv(0)); + return; + } + + var = Cvar_FindVar(Cmd_Argv(1)); + + if (!var) + { + Com_Printf("%s is not a cvar\n", Cmd_Argv(1)); + return; + } + + if (argc < 3) + { + if (!strcmp(var->string, "0")) + { + Cvar_Set(var->name, "1"); + } + else if (!strcmp(var->string, "1")) + { + Cvar_Set(var->name, "0"); + } + else + { + Com_Printf("\"%s\" is \"%s\", can't toggle\n", var->name, var->string); + } + + return; + } + + for (i = 0; i < argc - 2; i++) + { + if (!Q_stricmp(var->string, Cmd_Argv(2 + i))) + { + i = (i + 1) % (argc - 2); + Cvar_Set(var->name, Cmd_Argv(2 + i)); + + return; + } + } + + Com_Printf("\"%s\" is \"%s\", can't cycle\n", var->name, var->string); +} + /* * Reads in all archived cvars */ @@ -704,6 +762,7 @@ Cvar_Init(void) Cmd_AddCommand("reset", Cvar_Reset_f); Cmd_AddCommand("resetall", Cvar_ResetAll_f); Cmd_AddCommand("set", Cvar_Set_f); + Cmd_AddCommand("toggle", Cvar_Toggle_f); } /* @@ -727,5 +786,6 @@ Cvar_Fini(void) Cmd_RemoveCommand("reset"); Cmd_RemoveCommand("resetall"); Cmd_RemoveCommand("set"); + Cmd_RemoveCommand("toggle"); }