-
Notifications
You must be signed in to change notification settings - Fork 4
/
cvar.pas
322 lines (278 loc) · 6.45 KB
/
cvar.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// ------------------------------------------------------------------------------
// DelphiQuake, Copyright (C) 2005-2011 by Jim Valavanis
// E-Mail: [email protected]
//
// Copyright (C) 1996-1997 Id Software, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// ------------------------------------------------------------------------------
{$I dquake.inc}
{$Z4}
unit cvar;
// cvar.c -- dynamic variable tracking
interface
uses
q_delphi;
type
Pcvar_t = ^cvar_t;
cvar_t = record
name: PChar;
text: PChar;
archive: qboolean; // set to true to cause it to be saved to vars.rc
server: qboolean; // notifies players when changed
value: single;
next: Pcvar_t;
end;
function Cvar_FindVar(var_name: PChar): Pcvar_t;
function Cvar_VariableValue(var_name: PChar): single;
function Cvar_VariableString(var_name: PChar): PChar;
function Cvar_CompleteVariable(partial: PChar): PChar;
procedure Cvar_Set(var_name: PChar; value: PChar);
procedure Cvar_SetValue(var_name: PChar; value: single); overload;
procedure Cvar_SetValue(var_name: PChar; value: qboolean); overload;
procedure Cvar_SetValue(var_name: string; value: single); overload;
procedure Cvar_SetValue(var_name: string; value: qboolean); overload;
procedure Cvar_RegisterVariable(variable: Pcvar_t);
function Cvar_Command: qboolean;
procedure Cvar_WriteVariables(var f: text);
var
cvar_vars: Pcvar_t = nil;
implementation
uses
common,
console,
zone,
sv_main,
host,
cmd;
var
cvar_null_string: PChar = '';
(*
============
Cvar_FindVar
============
*)
function Cvar_FindVar(var_name: PChar): Pcvar_t;
var
_var: Pcvar_t;
begin
_var := cvar_vars;
while _var <> nil do
begin
if Q_strcmp(var_name, _var.name) = 0 then
begin
result := _var;
exit;
end;
_var := _var.next;
end;
result := nil;
end;
(*
============
Cvar_VariableValue
============
*)
function Cvar_VariableValue(var_name: PChar): single;
var
_var: Pcvar_t;
begin
_var := Cvar_FindVar(var_name);
if _var = nil then
result := 0
else
result := Q_atof(_var.text);
end;
(*
============
Cvar_VariableString
============
*)
function Cvar_VariableString(var_name: PChar): PChar;
var
_var: Pcvar_t;
begin
_var := Cvar_FindVar(var_name);
if _var = nil then
result := cvar_null_string
else
result := _var.text;
end;
(*
============
Cvar_CompleteVariable
============
*)
function Cvar_CompleteVariable(partial: PChar): PChar;
var
_var: Pcvar_t;
len: integer;
begin
len := Q_strlen(partial);
if len = 0 then
begin
result := nil;
exit;
end;
// check functions
_var := cvar_vars;
while _var <> nil do
begin
if Q_strncmp(partial, _var.name, len) = 0 then
begin
result := _var.name;
exit;
end;
_var := _var.next;
end;
result := nil;
end;
(*
============
Cvar_Set
============
*)
procedure Cvar_Set(var_name: PChar; value: PChar);
var
_var: Pcvar_t;
changed: qboolean;
begin
_var := Cvar_FindVar(var_name);
if _var = nil then
begin // there is an error in C code if this happens
Con_Printf('Cvar_Set: variable %s not found'#10, [var_name]);
exit;
end;
changed := Q_strcmp(_var.text, value) = 0;
Z_Free(_var.text); // free the old value string
_var.text := Z_Malloc(Q_strlen(value) + 1);
Q_strcpy(_var.text, value);
_var.value := Q_atof(_var.text);
if _var.server and changed then
begin
if sv.active then
SV_BroadcastPrintf('"%s" changed to "%s"'#10, [_var.name, _var.text]);
end;
end;
(*
============
Cvar_SetValue
============
*)
procedure Cvar_SetValue(var_name: PChar; value: qboolean);
var
f: single;
begin
if value then
f := 1.0
else
f := 0.0;
Cvar_SetValue(var_name, f);
end;
procedure Cvar_SetValue(var_name: PChar; value: single);
var
val: array[0..31] of char;
begin
sprintf(val, '%f', [value]);
Cvar_Set(var_name, val);
end;
procedure Cvar_SetValue(var_name: string; value: single);
begin
Cvar_SetValue(PChar(var_name), value);
end;
procedure Cvar_SetValue(var_name: string; value: qboolean);
begin
Cvar_SetValue(PChar(var_name), value);
end;
(*
============
Cvar_RegisterVariable
Adds a freestanding variable to the variable list.
============
*)
procedure Cvar_RegisterVariable(variable: Pcvar_t);
var
oldstr: PChar;
begin
// first check to see if it has allready been defined
if Cvar_FindVar(variable.name) <> nil then
begin
Con_Printf('Can''t register variable %s, allready defined'#10, [variable.name]);
exit;
end;
// check for overlap with a command
if Cmd_Exists(variable.name) then
begin
Con_Printf('Cvar_RegisterVariable: %s is a command'#10, [variable.name]);
exit;
end;
// copy the value off, because future sets will Z_Free it
oldstr := variable.text;
variable.text := Z_Malloc(Q_strlen(variable.text) + 1);
Q_strcpy(variable.text, oldstr);
variable.value := Q_atof(variable.text);
// link the variable in
variable.next := cvar_vars;
cvar_vars := variable;
end;
(*
============
Cvar_Command
Handles variable inspection and changing from the console
============
*)
function Cvar_Command: qboolean;
var
v: Pcvar_t;
begin
// check variables
v := Cvar_FindVar(Cmd_Argv_f(0));
if v = nil then
begin
result := false;
exit;
end;
// perform a variable print or set
if Cmd_Argc_f = 1 then
begin
Con_Printf('"%s" is "%s"'#10, [v.name, v.text]);
result := true;
exit;
end;
Cvar_Set(v.name, Cmd_Argv_f(1));
result := true;
end;
(*
============
Cvar_WriteVariables
Writes lines containing "set variable value" for all variables
with the archive flag set to true.
============
*)
procedure Cvar_WriteVariables(var f: text);
var
_var: Pcvar_t;
begin
_var := cvar_vars;
while _var <> nil do
begin
if _var.archive then
fprintf(f, '%s "%s"'#10, [_var.name, _var.text]);
_var := _var.next;
end;
end;
end.