]> git.xonotic.org Git - xonotic/darkplaces.git/blob - cvar.h
Use double for cvar values. Fixes a number of bugs caused by atof precision loss
[xonotic/darkplaces.git] / cvar.h
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.h
21
22 struct cmd_state_s;
23 typedef struct cmd_state_s cmd_state_t;
24
25 /*
26
27 cvar_t variables are used to hold scalar or string variables that can be changed or displayed at the console or prog code as well as accessed directly
28 in C code.
29
30 it is sufficient to initialize a cvar_t with just the first two fields, or
31 you can add a ,true flag for variables that you want saved to the configuration
32 file when the game is quit:
33
34 cvar_t  r_draworder = {"r_draworder","1"};
35 cvar_t  scr_screensize = {"screensize","1",true};
36
37 Cvars must be registered before use, or they will have a 0 value instead of the float interpretation of the string.  Generally, all cvar_t declarations should be registered in the apropriate init function before any console commands are executed:
38 Cvar_RegisterVariable (&host_framerate);
39
40
41 C code usually just references a cvar in place:
42 if ( r_draworder.value )
43
44 It could optionally ask for the value to be looked up for a string name:
45 if (Cvar_VariableValue ("r_draworder"))
46
47 Interpreted prog code can access cvars with the cvar(name) or
48 cvar_set (name, value) internal functions:
49 teamplay = cvar("teamplay");
50 cvar_set ("registered", "1");
51
52 The user can access cvars from the console in two ways:
53 r_draworder                     prints the current value
54 r_draworder 0           sets the current value to 0
55 Cvars are restricted from having the same names as commands to keep this
56 interface from being ambiguous.
57 */
58
59 #ifndef CVAR_H
60 #define CVAR_H
61
62 // cvar flags
63
64 #define CVAR_SAVE 1
65 #define CVAR_NOTIFY 2
66 #define CVAR_READONLY 4
67 #define CVAR_SERVERINFO 8
68 #define CVAR_USERINFO 16
69 // CVAR_PRIVATE means do not $ expand or sendcvar this cvar under any circumstances (rcon_password uses this)
70 #define CVAR_PRIVATE 32
71 // for engine-owned cvars that must not be reset on gametype switch (e.g. scr_screenshot_name, which otherwise isn't set to the mod name properly)
72 #define CVAR_NORESETTODEFAULTS 64
73 // cvar is accessible in client
74 #define CVAR_CLIENT 128
75 // cvar is accessible in dedicated server
76 #define CVAR_SERVER 256
77 // used to determine if flags is valid
78 #define CVAR_MAXFLAGSVAL 511
79 // for internal use only!
80 #define CVAR_DEFAULTSET (1<<30)
81 #define CVAR_ALLOCATED (1<<31)
82
83 /*
84 // type of a cvar for menu purposes
85 #define CVARMENUTYPE_FLOAT 1
86 #define CVARMENUTYPE_INTEGER 2
87 #define CVARMENUTYPE_SLIDER 3
88 #define CVARMENUTYPE_BOOL 4
89 #define CVARMENUTYPE_STRING 5
90 #define CVARMENUTYPE_OPTION 6
91
92 // which menu to put a cvar in
93 #define CVARMENU_GRAPHICS 1
94 #define CVARMENU_SOUND 2
95 #define CVARMENU_INPUT 3
96 #define CVARMENU_NETWORK 4
97 #define CVARMENU_SERVER 5
98
99 #define MAX_CVAROPTIONS 16
100
101 typedef struct cvaroption_s
102 {
103         int value;
104         const char *name;
105 }
106 cvaroption_t;
107
108 typedef struct menucvar_s
109 {
110         int type;
111         float valuemin, valuemax, valuestep;
112         int numoptions;
113         cvaroption_t optionlist[MAX_CVAROPTIONS];
114 }
115 menucvar_t;
116 */
117
118 typedef struct cvar_s
119 {
120         int flags;
121
122         const char *name;
123
124         const char *string;
125         const char *description;
126         int integer;
127         double value;
128         double vector[3];
129
130         const char *defstring;
131
132         void (*callback)(char *value);
133         qboolean ignore_callback;
134
135         char **aliases;
136         int aliasindex;
137
138         // values at init (for Cvar_RestoreInitState)
139         qboolean initstate; // indicates this existed at init
140         int initflags;
141         const char *initstring;
142         const char *initdescription;
143         int initinteger;
144         double initvalue;
145         double initvector[3];
146         const char *initdefstring;
147
148         int globaldefindex[3];
149         int globaldefindex_stringno[3];
150
151         //menucvar_t menuinfo;
152         struct cvar_s *next;
153 } cvar_t;
154
155 typedef struct cvar_hash_s
156 {
157         cvar_t *cvar;
158         struct cvar_hash_s *next;
159 } cvar_hash_t;
160
161 typedef struct cvar_state_s
162 {
163         cvar_t *vars;
164         cvar_hash_t *hashtable[CVAR_HASHSIZE];
165 }
166 cvar_state_t;
167
168 extern cvar_state_t cvars_all;
169 extern cvar_state_t cvars_null; // used by cmd_serverfromclient which intentionally has no cvars available
170
171 /*
172 void Cvar_MenuSlider(cvar_t *variable, int menu, float slider_min, float slider_max, float slider_step);
173 void Cvar_MenuBool(cvar_t *variable, int menu, const char *name_false, const char *name_true);
174 void Cvar_MenuFloat(cvar_t *variable, int menu, float range_min, float range_max);
175 void Cvar_MenuInteger(cvar_t *variable, int menu, int range_min, int range_max);
176 void Cvar_MenuString(cvar_t *variable, int menu);
177 void Cvar_MenuOption(cvar_t *variable, int menu, int value[16], const char *name[16]);
178 */
179
180
181 void Cvar_RegisterAlias(cvar_t *variable, const char *alias );
182
183 void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(char *));
184
185 /// registers a cvar that already has the name, string, and optionally the
186 /// archive elements set.
187 void Cvar_RegisterVariable(cvar_t *variable);
188
189 qboolean Cvar_Readonly (cvar_t *var, const char *cmd_name);
190
191 /// equivelant to "<name> <variable>" typed at the console
192 void Cvar_Set (cvar_state_t *cvars, const char *var_name, const char *value);
193 void Cvar_Set_NoCallback (cvar_t *var, const char *value);
194
195 /// expands value to a string and calls Cvar_Set
196 void Cvar_SetValue (cvar_state_t *cvars, const char *var_name, double value);
197
198 void Cvar_SetQuick (cvar_t *var, const char *value);
199 void Cvar_SetValueQuick (cvar_t *var, double value);
200
201 float Cvar_VariableValueOr (cvar_state_t *cvars, const char *var_name, double def, int neededflags);
202 // returns def if not defined
203
204 float Cvar_VariableValue (cvar_state_t *cvars, const char *var_name, int neededflags);
205 // returns 0 if not defined or non numeric
206
207 const char *Cvar_VariableStringOr (cvar_state_t *cvars, const char *var_name, const char *def, int neededflags);
208 // returns def if not defined
209
210 const char *Cvar_VariableString (cvar_state_t *cvars, const char *var_name, int neededflags);
211 // returns an empty string if not defined
212
213 const char *Cvar_VariableDefString (cvar_state_t *cvars, const char *var_name, int neededflags);
214 // returns an empty string if not defined
215
216 const char *Cvar_VariableDescription (cvar_state_t *cvars, const char *var_name, int neededflags);
217 // returns an empty string if not defined
218
219 const char *Cvar_CompleteVariable (cvar_state_t *cvars, const char *partial, int neededflags);
220 // attempts to match a partial variable name for command line completion
221 // returns NULL if nothing fits
222
223 void Cvar_PrintHelp(cvar_t *cvar, const char *name, qboolean full);
224
225 void Cvar_CompleteCvarPrint (cvar_state_t *cvars, const char *partial, int neededflags);
226
227 qboolean Cvar_Command (cmd_state_t *cmd);
228 // called by Cmd_ExecuteString when Cmd_Argv(cmd, 0) doesn't match a known
229 // command.  Returns true if the command was a variable reference that
230 // was handled. (print or change)
231
232 void Cvar_SaveInitState(cvar_state_t *cvars);
233 void Cvar_RestoreInitState(cvar_state_t *cvars);
234
235 void Cvar_UnlockDefaults(cmd_state_t *cmd);
236 void Cvar_LockDefaults_f(cmd_state_t *cmd);
237 void Cvar_ResetToDefaults_All_f(cmd_state_t *cmd);
238 void Cvar_ResetToDefaults_NoSaveOnly_f(cmd_state_t *cmd);
239 void Cvar_ResetToDefaults_SaveOnly_f(cmd_state_t *cmd);
240
241 void Cvar_WriteVariables (cvar_state_t *cvars, qfile_t *f);
242 // Writes lines containing "set variable value" for all variables
243 // with the archive flag set to true.
244
245 cvar_t *Cvar_FindVar(cvar_state_t *cvars, const char *var_name, int neededflags);
246 cvar_t *Cvar_FindVarAfter(cvar_state_t *cvars, const char *prev_var_name, int neededflags);
247
248 int Cvar_CompleteCountPossible(cvar_state_t *cvars, const char *partial, int neededflags);
249 const char **Cvar_CompleteBuildList(cvar_state_t *cvars, const char *partial, int neededflags);
250 // Added by EvilTypeGuy - functions for tab completion system
251 // Thanks to Fett erich@heintz.com
252 // Thanks to taniwha
253
254 /// Prints a list of Cvars including a count of them to the user console
255 /// Referenced in cmd.c in Cmd_Init hence it's inclusion here.
256 /// Added by EvilTypeGuy eviltypeguy@qeradiant.com
257 /// Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
258 void Cvar_List_f(cmd_state_t *cmd);
259
260 void Cvar_Set_f(cmd_state_t *cmd);
261 void Cvar_SetA_f(cmd_state_t *cmd);
262 void Cvar_Del_f(cmd_state_t *cmd);
263 // commands to create new cvars (or set existing ones)
264 // seta creates an archived cvar (saved to config)
265
266 /// allocates a cvar by name and returns its address,
267 /// or merely sets its value if it already exists.
268 cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int flags, const char *newdescription);
269
270 extern const char *cvar_dummy_description; // ALWAYS the same pointer
271
272 void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars); // updates ALL autocvars of the active prog to the cvar values (savegame loading)
273
274 #ifdef FILLALLCVARSWITHRUBBISH
275 void Cvar_FillAll_f(cmd_state_t *cmd);
276 #endif /* FILLALLCVARSWITHRUBBISH */
277
278 #endif
279