]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cvar.c
fixed combine overbright behavior
[xonotic/darkplaces.git] / cvar.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // cvar.c -- dynamic variable tracking
21
22 #include "quakedef.h"
23
24 cvar_t  *cvar_vars = NULL;
25 char    *cvar_null_string = "";
26
27 /*
28 ============
29 Cvar_FindVar
30 ============
31 */
32 cvar_t *Cvar_FindVar (char *var_name)
33 {
34         cvar_t *var;
35
36         for (var = cvar_vars;var;var = var->next)
37                 if (!strcmp (var_name, var->name))
38                         return var;
39
40         return NULL;
41 }
42
43 cvar_t *Cvar_FindVarAfter (char *prev_var_name, int neededflags)
44 {
45         cvar_t *var;
46
47         if (*prev_var_name)
48         {
49                 var = Cvar_FindVar (prev_var_name);
50                 if (!var)
51                         return NULL;
52                 var = var->next;
53         }
54         else
55                 var = cvar_vars;
56
57         // search for the next cvar matching the needed flags
58         while (var)
59         {
60                 if ((var->flags & neededflags) || !neededflags)
61                         break;
62                 var = var->next;
63         }
64         return var;
65 }
66
67 /*
68 ============
69 Cvar_VariableValue
70 ============
71 */
72 float   Cvar_VariableValue (char *var_name)
73 {
74         cvar_t  *var;
75
76         var = Cvar_FindVar (var_name);
77         if (!var)
78                 return 0;
79         return atof (var->string);
80 }
81
82
83 /*
84 ============
85 Cvar_VariableString
86 ============
87 */
88 char *Cvar_VariableString (char *var_name)
89 {
90         cvar_t *var;
91
92         var = Cvar_FindVar (var_name);
93         if (!var)
94                 return cvar_null_string;
95         return var->string;
96 }
97
98
99 /*
100 ============
101 Cvar_CompleteVariable
102 ============
103 */
104 char *Cvar_CompleteVariable (char *partial)
105 {
106         cvar_t          *cvar;
107         int                     len;
108
109         len = strlen(partial);
110
111         if (!len)
112                 return NULL;
113
114 // check functions
115         for (cvar=cvar_vars ; cvar ; cvar=cvar->next)
116                 if (!strncmp (partial,cvar->name, len))
117                         return cvar->name;
118
119         return NULL;
120 }
121
122
123 /*
124         CVar_CompleteCountPossible
125
126         New function for tab-completion system
127         Added by EvilTypeGuy
128         Thanks to Fett erich@heintz.com
129
130 */
131 int
132 Cvar_CompleteCountPossible (char *partial)
133 {
134         cvar_t  *cvar;
135         int             len;
136         int             h;
137
138         h = 0;
139         len = strlen(partial);
140
141         if (!len)
142                 return  0;
143
144         // Loop through the cvars and count all possible matches
145         for (cvar = cvar_vars; cvar; cvar = cvar->next)
146                 if (!strncasecmp(partial, cvar->name, len))
147                         h++;
148
149         return h;
150 }
151
152 /*
153         CVar_CompleteBuildList
154
155         New function for tab-completion system
156         Added by EvilTypeGuy
157         Thanks to Fett erich@heintz.com
158         Thanks to taniwha
159
160 */
161 char    **
162 Cvar_CompleteBuildList (char *partial)
163 {
164         cvar_t  *cvar;
165         int             len = 0;
166         int             bpos = 0;
167         int             sizeofbuf = (Cvar_CompleteCountPossible (partial) + 1) * sizeof (char *);
168         char    **buf;
169
170         len = strlen(partial);
171         buf = Mem_Alloc(tempmempool, sizeofbuf + sizeof (char *));
172         // Loop through the alias list and print all matches
173         for (cvar = cvar_vars; cvar; cvar = cvar->next)
174                 if (!strncasecmp(partial, cvar->name, len))
175                         buf[bpos++] = cvar->name;
176
177         buf[bpos] = NULL;
178         return buf;
179 }
180
181 /*
182 ============
183 Cvar_Set
184 ============
185 */
186 void Cvar_SetQuick (cvar_t *var, char *value)
187 {
188         qboolean changed;
189
190         if (var == NULL)
191         {
192                 Con_Printf("Cvar_SetQuick: var == NULL\n");
193                 return;
194         }
195
196         changed = strcmp(var->string, value);
197         // LordHavoc: don't reallocate when there is no change
198         if (!changed)
199                 return;
200
201         // LordHavoc: don't reallocate when the buffer is the same size
202         if (!var->string || strlen(var->string) != strlen(value))
203         {
204                 Z_Free (var->string);   // free the old value string
205
206                 var->string = Z_Malloc (strlen(value)+1);
207         }
208         strcpy (var->string, value);
209         var->value = atof (var->string);
210         var->integer = (int) var->value;
211         if ((var->flags & CVAR_NOTIFY) && changed)
212         {
213                 if (sv.active)
214                         SV_BroadcastPrintf ("\"%s\" changed to \"%s\"\n", var->name, var->string);
215         }
216 }
217
218 void Cvar_Set (char *var_name, char *value)
219 {
220         cvar_t *var;
221         var = Cvar_FindVar (var_name);
222         if (var == NULL)
223         {
224                 // there is an error in C code if this happens
225                 Con_Printf ("Cvar_Set: variable %s not found\n", var_name);
226                 return;
227         }
228
229         Cvar_SetQuick(var, value);
230 }
231
232 /*
233 ============
234 Cvar_SetValue
235 ============
236 */
237 void Cvar_SetValueQuick (cvar_t *var, float value)
238 {
239         char    val[32];
240
241         // LordHavoc: changed from %f to %g to use shortest representation
242         sprintf (val, "%g",value);
243         Cvar_SetQuick (var, val);
244 }
245
246 void Cvar_SetValue (char *var_name, float value)
247 {
248         char    val[32];
249
250         // LordHavoc: changed from %f to %g to use shortest representation
251         sprintf (val, "%g",value);
252         Cvar_Set (var_name, val);
253 }
254
255 /*
256 ============
257 Cvar_RegisterVariable
258
259 Adds a freestanding variable to the variable list.
260 ============
261 */
262 void Cvar_RegisterVariable (cvar_t *variable)
263 {
264         char    *oldstr;
265
266 // first check to see if it has already been defined
267         if (Cvar_FindVar (variable->name))
268         {
269                 Con_Printf ("Can't register variable %s, already defined\n", variable->name);
270                 return;
271         }
272
273 // check for overlap with a command
274         if (Cmd_Exists (variable->name))
275         {
276                 Con_Printf ("Cvar_RegisterVariable: %s is a command\n", variable->name);
277                 return;
278         }
279
280 // copy the value off, because future sets will Z_Free it
281         oldstr = variable->string;
282         variable->string = Z_Malloc (strlen(variable->string)+1);
283         strcpy (variable->string, oldstr);
284         variable->value = atof (variable->string);
285         variable->integer = (int) variable->value;
286
287 // link the variable in
288         variable->next = cvar_vars;
289         cvar_vars = variable;
290 }
291
292 /*
293 ============
294 Cvar_Command
295
296 Handles variable inspection and changing from the console
297 ============
298 */
299 qboolean        Cvar_Command (void)
300 {
301         cvar_t                  *v;
302
303 // check variables
304         v = Cvar_FindVar (Cmd_Argv(0));
305         if (!v)
306                 return false;
307
308 // perform a variable print or set
309         if (Cmd_Argc() == 1)
310         {
311                 Con_Printf ("\"%s\" is \"%s\"\n", v->name, v->string);
312                 return true;
313         }
314
315         Cvar_Set (v->name, Cmd_Argv(1));
316         return true;
317 }
318
319
320 /*
321 ============
322 Cvar_WriteVariables
323
324 Writes lines containing "set variable value" for all variables
325 with the archive flag set to true.
326 ============
327 */
328 void Cvar_WriteVariables (QFile *f)
329 {
330         cvar_t  *var;
331
332         for (var = cvar_vars ; var ; var = var->next)
333                 if (var->flags & CVAR_SAVE)
334                         Qprintf (f, "%s \"%s\"\n", var->name, var->string);
335 }
336
337
338 // Added by EvilTypeGuy eviltypeguy@qeradiant.com
339 // 2000-01-09 CvarList command By Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
340 /*
341 =========
342 Cvar_List
343 =========
344 */
345 void Cvar_List_f (void)
346 {
347         cvar_t  *cvar;
348         char    *partial;
349         int             len;
350         int             count;
351
352         if (Cmd_Argc() > 1) {
353                 partial = Cmd_Argv (1);
354                 len = strlen(partial);
355         } else {
356                 partial = NULL;
357                 len = 0;
358         }
359
360         count = 0;
361         for (cvar = cvar_vars; cvar; cvar = cvar->next) {
362                 if (partial && strncmp (partial,cvar->name,len))
363                         continue;
364
365                 Con_Printf ("%s is \"%s\"\n", cvar->name, cvar->string);
366                 count++;
367         }
368
369         Con_Printf ("%i cvar(s)", count);
370         if (partial)
371                 Con_Printf (" beginning with \"%s\"", partial);
372         Con_Printf ("\n");
373 }
374 // 2000-01-09 CvarList command by Maddes
375