DEADSOFTWARE

91131d1d91e75bba067c9927f5d86ea118de5ef7
[d2df-sdl.git] / src / game / g_console.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_console;
18 interface
20 uses
21 utils; // for SSArray
23 const
24 ACTION_JUMP = 0;
25 ACTION_MOVELEFT = 1;
26 ACTION_MOVERIGHT = 2;
27 ACTION_LOOKDOWN = 3;
28 ACTION_LOOKUP = 4;
29 ACTION_ATTACK = 5;
30 ACTION_SCORES = 6;
31 ACTION_ACTIVATE = 7;
32 ACTION_STRAFE = 8;
33 ACTION_WEAPNEXT = 9;
34 ACTION_WEAPPREV = 10;
36 FIRST_ACTION = ACTION_JUMP;
37 LAST_ACTION = ACTION_WEAPPREV;
39 var (* private state *)
40 Line: AnsiString;
41 CPos: Word;
42 conSkipLines: Integer;
43 MsgArray: Array [0..4] of record
44 Msg: AnsiString;
45 Time: Word;
46 end;
48 procedure g_Console_Init;
49 procedure g_Console_Initialize;
50 procedure g_Console_Finalize;
51 procedure g_Console_Update;
52 procedure g_Console_Char (C: AnsiChar);
53 procedure g_Console_Control (K: Word);
54 procedure g_Console_Process (L: AnsiString; quiet: Boolean=false);
55 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
56 procedure g_Console_Clear;
57 function g_Console_CommandBlacklisted (C: AnsiString): Boolean;
58 procedure g_Console_ReadConfig (filename: String);
59 procedure g_Console_WriteConfig (filename: String);
60 procedure g_Console_WriteGameConfig;
62 function g_Console_Interactive: Boolean;
63 function g_Console_Action (action: Integer): Boolean;
64 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
65 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
66 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = '');
67 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
68 procedure g_Console_ProcessBindRepeat (key: Integer);
69 procedure g_Console_ResetBinds;
71 procedure conwriteln (const s: AnsiString; show: Boolean=false);
72 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
74 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
75 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
76 procedure conRegVar (const conname: AnsiString; pvar: PInteger; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
77 procedure conRegVar (const conname: AnsiString; pvar: PWord; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
78 procedure conRegVar (const conname: AnsiString; pvar: PCardinal; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
79 procedure conRegVar (const conname: AnsiString; pvar: PAnsiString; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
81 // <0: no arg; 0/1: true/false
82 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
84 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
85 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
87 const
88 defaultConfigScript = 'dfconfig.cfg';
90 var
91 gConsoleShow: Boolean = false; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
92 gChatShow: Boolean = false;
93 gChatTeam: Boolean = false;
94 gAllowConsoleMessages: Boolean = true;
95 gJustChatted: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
96 gParsingBinds: Boolean = true; // íå ïåðåñîõðàíÿòü êîíôèã âî âðåìÿ ïàðñèíãà
97 gPlayerAction: Array [0..1, 0..LAST_ACTION] of Boolean; // [player, action]
98 gConfigScript: string = defaultConfigScript;
100 implementation
102 uses
103 {$IFNDEF HEADLESS}
104 g_gui, g_menu, g_touch,
105 {$ENDIF}
106 g_textures, e_input, g_game, g_gfx, g_player, g_items,
107 SysUtils, g_basic, g_options, Math, e_res,
108 g_language, g_net, g_netmsg, e_log, conbuf;
110 const
111 configComment = 'generated by doom2d, do not modify';
113 type
114 PCommand = ^TCommand;
116 TCmdProc = procedure (p: SSArray);
117 TCmdProcEx = procedure (me: PCommand; p: SSArray);
119 TCommand = record
120 cmd: AnsiString;
121 proc: TCmdProc;
122 procEx: TCmdProcEx;
123 help: AnsiString;
124 hidden: Boolean;
125 ptr: Pointer; // various data
126 msg: AnsiString; // message for var changes
127 cheat: Boolean;
128 action: Integer; // >= 0 for action commands
129 player: Integer; // used for action commands
130 end;
132 TAlias = record
133 name: AnsiString;
134 commands: SSArray;
135 end;
138 const
139 MsgTime = 144;
140 MaxScriptRecursion = 16;
142 var
143 RecursionDepth: Word = 0;
144 RecursionLimitHit: Boolean = False;
145 InputReady: Boolean; // allow text input in console/chat
146 //ConsoleHistory: SSArray;
147 CommandHistory: SSArray;
148 Whitelist: SSArray;
149 commands: Array of TCommand = nil;
150 Aliases: Array of TAlias = nil;
151 CmdIndex: Word;
153 gInputBinds: Array [0..e_MaxInputKeys - 1] of record
154 rep: Boolean;
155 down, up: SSArray;
156 end;
157 menu_toggled: BOOLEAN; (* hack for menu controls *)
159 procedure g_Console_Switch;
160 begin
161 gChatShow := False;
162 gConsoleShow := not gConsoleShow;
163 InputReady := False;
164 {$IFNDEF HEADLESS}
165 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
166 {$ENDIF}
167 end;
169 procedure g_Console_Chat_Switch (Team: Boolean = False);
170 begin
171 if not g_Game_IsNet then Exit;
172 gConsoleShow := False;
173 gChatShow := not gChatShow;
174 gChatTeam := Team;
175 InputReady := False;
176 Line := '';
177 CPos := 1;
178 {$IFNDEF HEADLESS}
179 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
180 {$ENDIF}
181 end;
183 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
184 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
185 var
186 pos: Integer = 1;
187 frac: Single = 1;
188 slen: Integer;
189 begin
190 result := false;
191 res := 0;
192 slen := Length(s);
193 while (slen > 0) and (s[slen] <= ' ') do Dec(slen);
194 while (pos <= slen) and (s[pos] <= ' ') do Inc(pos);
195 if (pos > slen) then exit;
196 if (slen-pos = 1) and (s[pos] = '.') then exit; // single dot
197 // integral part
198 while (pos <= slen) do
199 begin
200 if (s[pos] < '0') or (s[pos] > '9') then break;
201 res := res*10+Byte(s[pos])-48;
202 Inc(pos);
203 end;
204 if (pos <= slen) then
205 begin
206 // must be a dot
207 if (s[pos] <> '.') then exit;
208 Inc(pos);
209 while (pos <= slen) do
210 begin
211 if (s[pos] < '0') or (s[pos] > '9') then break;
212 frac := frac/10;
213 res += frac*(Byte(s[pos])-48);
214 Inc(pos);
215 end;
216 end;
217 if (pos <= slen) then exit; // oops
218 result := true;
219 end;
222 // ////////////////////////////////////////////////////////////////////////// //
223 // <0: no arg; 0/1: true/false; 666: toggle
224 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
225 begin
226 if (idx < 0) or (idx > High(p)) then begin result := -1; exit; end;
227 result := 0;
228 if (p[idx] = '1') or (CompareText(p[idx], 'on') = 0) or (CompareText(p[idx], 'true') = 0) or
229 (CompareText(p[idx], 'tan') = 0) or (CompareText(p[idx], 'yes') = 0) then result := 1
230 else if (CompareText(p[idx], 'toggle') = 0) or (CompareText(p[idx], 'switch') = 0) or
231 (CompareText(p[idx], 't') = 0) then result := 666;
232 end;
235 procedure boolVarHandler (me: PCommand; p: SSArray);
236 procedure binaryFlag (var flag: Boolean; msg: AnsiString);
237 var
238 old: Boolean;
239 begin
240 if (Length(p) > 2) then
241 begin
242 conwritefln('too many arguments to ''%s''', [p[0]]);
243 end
244 else
245 begin
246 old := flag;
247 case conGetBoolArg(p, 1) of
248 -1: begin end;
249 0: if not me.cheat or conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
250 1: if not me.cheat or conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
251 666: if not me.cheat or conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
252 end;
253 if flag <> old then
254 g_Console_WriteGameConfig();
255 if (Length(msg) = 0) then msg := p[0] else msg += ':';
256 if flag then conwritefln('%s tan', [msg]) else conwritefln('%s ona', [msg]);
257 end;
258 end;
259 begin
260 binaryFlag(PBoolean(me.ptr)^, me.msg);
261 end;
264 procedure intVarHandler (me: PCommand; p: SSArray);
265 var
266 old: Integer;
267 begin
268 if (Length(p) <> 2) then
269 begin
270 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
271 end
272 else
273 begin
274 try
275 old := PInteger(me.ptr)^;
276 PInteger(me.ptr)^ := StrToInt(p[1]);
277 if PInteger(me.ptr)^ <> old then
278 g_Console_WriteGameConfig();
279 except
280 conwritefln('invalid integer value: "%s"', [p[1]]);
281 end;
282 end;
283 end;
286 procedure wordVarHandler (me: PCommand; p: SSArray);
287 var
288 old: Integer;
289 begin
290 if (Length(p) <> 2) then
291 begin
292 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
293 end
294 else
295 begin
296 try
297 old := PWord(me.ptr)^;
298 PWord(me.ptr)^ := min($FFFF, StrToDWord(p[1]));
299 if PWord(me.ptr)^ <> old then
300 g_Console_WriteGameConfig();
301 except
302 conwritefln('invalid word value: "%s"', [p[1]]);
303 end;
304 end;
305 end;
308 procedure dwordVarHandler (me: PCommand; p: SSArray);
309 var
310 old: Integer;
311 begin
312 if (Length(p) <> 2) then
313 begin
314 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
315 end
316 else
317 begin
318 try
319 old := PCardinal(me.ptr)^;
320 PCardinal(me.ptr)^ := StrToDWord(p[1]);
321 if PCardinal(me.ptr)^ <> old then
322 g_Console_WriteGameConfig();
323 except
324 conwritefln('invalid dword value: "%s"', [p[1]]);
325 end;
326 end;
327 end;
330 procedure strVarHandler (me: PCommand; p: SSArray);
331 var
332 old: AnsiString;
333 begin
334 if (Length(p) <> 2) then
335 begin
336 conwritefln('%s %s', [me.cmd, QuoteStr(PAnsiString(me.ptr)^)]);
337 end
338 else
339 begin
340 old := PAnsiString(me.ptr)^;
341 PAnsiString(me.ptr)^ := p[1];
342 if PAnsiString(me.ptr)^ <> old then
343 g_Console_WriteGameConfig();
344 end;
345 end;
348 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
349 var
350 f: Integer;
351 cp: PCommand;
352 begin
353 f := Length(commands);
354 SetLength(commands, f+1);
355 cp := @commands[f];
356 cp.cmd := LowerCase(conname);
357 cp.proc := nil;
358 cp.procEx := boolVarHandler;
359 cp.help := ahelp;
360 cp.hidden := ahidden;
361 cp.ptr := pvar;
362 cp.msg := amsg;
363 cp.cheat := acheat;
364 cp.action := -1;
365 cp.player := -1;
366 end;
369 procedure conRegVar (const conname: AnsiString; pvar: PInteger; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
370 var
371 f: Integer;
372 cp: PCommand;
373 begin
374 f := Length(commands);
375 SetLength(commands, f+1);
376 cp := @commands[f];
377 cp.cmd := LowerCase(conname);
378 cp.proc := nil;
379 cp.procEx := intVarHandler;
380 cp.help := ahelp;
381 cp.hidden := ahidden;
382 cp.ptr := pvar;
383 cp.msg := amsg;
384 cp.cheat := acheat;
385 cp.action := -1;
386 cp.player := -1;
387 end;
390 procedure conRegVar (const conname: AnsiString; pvar: PWord; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
391 var
392 f: Integer;
393 cp: PCommand;
394 begin
395 f := Length(commands);
396 SetLength(commands, f+1);
397 cp := @commands[f];
398 cp.cmd := LowerCase(conname);
399 cp.proc := nil;
400 cp.procEx := wordVarHandler;
401 cp.help := ahelp;
402 cp.hidden := ahidden;
403 cp.ptr := pvar;
404 cp.msg := amsg;
405 cp.cheat := acheat;
406 cp.action := -1;
407 cp.player := -1;
408 end;
411 procedure conRegVar (const conname: AnsiString; pvar: PCardinal; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
412 var
413 f: Integer;
414 cp: PCommand;
415 begin
416 f := Length(commands);
417 SetLength(commands, f+1);
418 cp := @commands[f];
419 cp.cmd := LowerCase(conname);
420 cp.proc := nil;
421 cp.procEx := dwordVarHandler;
422 cp.help := ahelp;
423 cp.hidden := ahidden;
424 cp.ptr := pvar;
425 cp.msg := amsg;
426 cp.cheat := acheat;
427 cp.action := -1;
428 cp.player := -1;
429 end;
432 procedure conRegVar (const conname: AnsiString; pvar: PAnsiString; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
433 var
434 f: Integer;
435 cp: PCommand;
436 begin
437 f := Length(commands);
438 SetLength(commands, f+1);
439 cp := @commands[f];
440 cp.cmd := LowerCase(conname);
441 cp.proc := nil;
442 cp.procEx := strVarHandler;
443 cp.help := ahelp;
444 cp.hidden := ahidden;
445 cp.ptr := pvar;
446 cp.msg := amsg;
447 cp.cheat := acheat;
448 cp.action := -1;
449 cp.player := -1;
450 end;
452 // ////////////////////////////////////////////////////////////////////////// //
453 type
454 PVarSingle = ^TVarSingle;
455 TVarSingle = record
456 val: PSingle;
457 min, max, def: Single; // default will be starting value
458 end;
461 procedure singleVarHandler (me: PCommand; p: SSArray);
462 var
463 pv: PVarSingle;
464 nv, old: Single;
465 msg: AnsiString;
466 begin
467 if (Length(p) > 2) then
468 begin
469 conwritefln('too many arguments to ''%s''', [me.cmd]);
470 exit;
471 end;
472 pv := PVarSingle(me.ptr);
473 old := pv.val^;
474 if (Length(p) = 2) then
475 begin
476 if me.cheat and (not conIsCheatsEnabled) then begin conwriteln('not available'); exit; end;
477 if (CompareText(p[1], 'default') = 0) or (CompareText(p[1], 'def') = 0) or
478 (CompareText(p[1], 'd') = 0) or (CompareText(p[1], 'off') = 0) or
479 (CompareText(p[1], 'ona') = 0) then
480 begin
481 pv.val^ := pv.def;
482 end
483 else
484 begin
485 if not conParseFloat(nv, p[1]) then
486 begin
487 conwritefln('%s: ''%s'' doesn''t look like a floating number', [me.cmd, p[1]]);
488 exit;
489 end;
490 if (nv < pv.min) then nv := pv.min;
491 if (nv > pv.max) then nv := pv.max;
492 pv.val^ := nv;
493 end;
494 end;
495 if pv.val^ <> old then
496 g_Console_WriteGameConfig();
497 msg := me.msg;
498 if (Length(msg) = 0) then msg := me.cmd else msg += ':';
499 conwritefln('%s %s', [msg, pv.val^]);
500 end;
503 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
504 var
505 f: Integer;
506 cp: PCommand;
507 pv: PVarSingle;
508 begin
509 GetMem(pv, sizeof(TVarSingle));
510 pv.val := pvar;
511 pv.min := amin;
512 pv.max := amax;
513 pv.def := pvar^;
514 f := Length(commands);
515 SetLength(commands, f+1);
516 cp := @commands[f];
517 cp.cmd := LowerCase(conname);
518 cp.proc := nil;
519 cp.procEx := singleVarHandler;
520 cp.help := ahelp;
521 cp.hidden := ahidden;
522 cp.ptr := pv;
523 cp.msg := amsg;
524 cp.cheat := acheat;
525 cp.action := -1;
526 cp.player := -1;
527 end;
530 // ////////////////////////////////////////////////////////////////////////// //
531 function GetStrACmd(var Str: AnsiString): AnsiString;
532 var
533 a: Integer;
534 begin
535 Result := '';
536 for a := 1 to Length(Str) do
537 if (a = Length(Str)) or (Str[a+1] = ';') then
538 begin
539 Result := Copy(Str, 1, a);
540 Delete(Str, 1, a+1);
541 Str := Trim(Str);
542 Exit;
543 end;
544 end;
546 function ParseAlias(Str: AnsiString): SSArray;
547 begin
548 Result := nil;
550 Str := Trim(Str);
552 if Str = '' then
553 Exit;
555 while Str <> '' do
556 begin
557 SetLength(Result, Length(Result)+1);
558 Result[High(Result)] := GetStrACmd(Str);
559 end;
560 end;
562 procedure ConsoleCommands(p: SSArray);
563 var
564 cmd, s: AnsiString;
565 a, b: Integer;
566 (* F: TextFile; *)
567 begin
568 cmd := LowerCase(p[0]);
569 s := '';
571 if cmd = 'clear' then
572 begin
573 //ConsoleHistory := nil;
574 cbufClear();
575 conSkipLines := 0;
577 for a := 0 to High(MsgArray) do
578 with MsgArray[a] do
579 begin
580 Msg := '';
581 Time := 0;
582 end;
583 end;
585 if cmd = 'clearhistory' then
586 CommandHistory := nil;
588 if cmd = 'showhistory' then
589 if CommandHistory <> nil then
590 begin
591 g_Console_Add('');
592 for a := 0 to High(CommandHistory) do
593 g_Console_Add(' '+CommandHistory[a]);
594 end;
596 if cmd = 'commands' then
597 begin
598 g_Console_Add('');
599 g_Console_Add('commands list:');
600 for a := High(commands) downto 0 do
601 begin
602 if (Length(commands[a].help) > 0) then
603 begin
604 g_Console_Add(' '+commands[a].cmd+' -- '+commands[a].help);
605 end
606 else
607 begin
608 g_Console_Add(' '+commands[a].cmd);
609 end;
610 end;
611 end;
613 if cmd = 'time' then
614 g_Console_Add(TimeToStr(Now), True);
616 if cmd = 'date' then
617 g_Console_Add(DateToStr(Now), True);
619 if cmd = 'echo' then
620 if Length(p) > 1 then
621 begin
622 if p[1] = 'ololo' then
623 gCheats := True
624 else
625 begin
626 s := '';
627 for a := 1 to High(p) do
628 s := s + p[a] + ' ';
629 g_Console_Add(b_Text_Format(s), True);
630 end;
631 end
632 else
633 g_Console_Add('');
635 if cmd = 'dump' then
636 begin
637 (*
638 if ConsoleHistory <> nil then
639 begin
640 if Length(P) > 1 then
641 s := P[1]
642 else
643 s := GameDir+'/console.txt';
645 {$I-}
646 AssignFile(F, s);
647 Rewrite(F);
648 if IOResult <> 0 then
649 begin
650 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
651 CloseFile(F);
652 Exit;
653 end;
655 for a := 0 to High(ConsoleHistory) do
656 WriteLn(F, ConsoleHistory[a]);
658 CloseFile(F);
659 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
660 {$I+}
661 end;
662 *)
663 end;
665 if cmd = 'exec' then
666 begin
667 // exec <filename>
668 if Length(p) = 2 then
669 g_Console_ReadConfig(p[1])
670 else
671 g_Console_Add('exec <script file>');
672 end;
674 if cmd = 'writeconfig' then
675 begin
676 // writeconfig <filename>
677 if Length(p) = 2 then
678 begin
679 s := e_GetWriteableDir(ConfigDirs);
680 g_Console_WriteConfig(e_CatPath(s, p[1]))
681 end
682 else
683 begin
684 g_Console_Add('writeconfig <file>')
685 end
686 end;
688 if (cmd = 'ver') or (cmd = 'version') then
689 begin
690 conwriteln('Doom 2D: Forever v. ' + GAME_VERSION);
691 conwritefln('Net protocol v. %d', [NET_PROTOCOL_VER]);
692 conwritefln('Build date: %s at %s', [GAME_BUILDDATE, GAME_BUILDTIME]);
693 end;
695 if cmd = 'alias' then
696 begin
697 // alias [alias_name] [commands]
698 if Length(p) > 1 then
699 begin
700 for a := 0 to High(Aliases) do
701 if Aliases[a].name = p[1] then
702 begin
703 if Length(p) > 2 then
704 Aliases[a].commands := ParseAlias(p[2])
705 else
706 for b := 0 to High(Aliases[a].commands) do
707 g_Console_Add(Aliases[a].commands[b]);
708 Exit;
709 end;
710 SetLength(Aliases, Length(Aliases)+1);
711 a := High(Aliases);
712 Aliases[a].name := p[1];
713 if Length(p) > 2 then
714 Aliases[a].commands := ParseAlias(p[2])
715 else
716 for b := 0 to High(Aliases[a].commands) do
717 g_Console_Add(Aliases[a].commands[b]);
718 end else
719 for a := 0 to High(Aliases) do
720 if Aliases[a].commands <> nil then
721 g_Console_Add(Aliases[a].name);
722 end;
724 if cmd = 'call' then
725 begin
726 // call <alias_name>
727 if Length(p) > 1 then
728 begin
729 if Aliases = nil then
730 Exit;
731 for a := 0 to High(Aliases) do
732 if Aliases[a].name = p[1] then
733 begin
734 if Aliases[a].commands <> nil then
735 begin
736 // with this system proper endless loop detection seems either impossible
737 // or very dirty to implement, so let's have this instead
738 // prevents endless loops
739 for b := 0 to High(Aliases[a].commands) do
740 begin
741 Inc(RecursionDepth);
742 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
743 if not RecursionLimitHit then
744 g_Console_Process(Aliases[a].commands[b], True);
745 Dec(RecursionDepth);
746 end;
747 if (RecursionDepth = 0) and RecursionLimitHit then
748 begin
749 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
750 RecursionLimitHit := False;
751 end;
752 end;
753 Exit;
754 end;
755 end
756 else
757 g_Console_Add('call <alias name>');
758 end;
759 end;
761 procedure WhitelistCommand(cmd: AnsiString);
762 var
763 a: Integer;
764 begin
765 SetLength(Whitelist, Length(Whitelist)+1);
766 a := High(Whitelist);
767 Whitelist[a] := LowerCase(cmd);
768 end;
770 procedure segfault (p: SSArray);
771 var
772 pp: PByte = nil;
773 begin
774 pp^ := 0;
775 end;
777 function GetCommandString (p: SSArray): AnsiString;
778 var i: Integer;
779 begin
780 result := '';
781 if Length(p) >= 1 then
782 begin
783 result := p[0];
784 for i := 1 to High(p) do
785 result := result + '; ' + p[i]
786 end
787 end;
789 function QuoteStr(str: String): String;
790 begin
791 if Pos(' ', str) > 0 then
792 Result := '"' + str + '"'
793 else
794 Result := str;
795 end;
797 procedure BindCommands (p: SSArray);
798 var cmd, key: AnsiString; i: Integer;
799 begin
800 cmd := LowerCase(p[0]);
801 case cmd of
802 'bind':
803 // bind <key> [down [up]]
804 if (Length(p) >= 2) and (Length(p) <= 4) then
805 begin
806 i := 0;
807 key := LowerCase(p[1]);
808 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
809 if i < e_MaxInputKeys then
810 begin
811 if Length(p) = 2 then
812 g_Console_Add(QuoteStr(e_KeyNames[i]) + ' = ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)))
813 else if Length(p) = 3 then
814 g_Console_BindKey(i, p[2], '')
815 else (* len = 4 *)
816 g_Console_BindKey(i, p[2], p[3])
817 end
818 else
819 g_Console_Add('bind: "' + p[1] + '" is not a key')
820 end
821 else
822 begin
823 g_Console_Add('bind <key> <down action> [up action]')
824 end;
825 'bindrep':
826 // bindrep <key>
827 if Length(p) = 2 then
828 begin
829 key := LowerCase(p[1]);
830 i := 0;
831 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
832 if i < e_MaxInputKeys then
833 gInputBinds[i].rep := True
834 else
835 g_Console_Add('bindrep: "' + p[1] + '" is not a key')
836 end
837 else
838 g_Console_Add('bindrep <key>');
839 'bindunrep':
840 // bindunrep <key>
841 if Length(p) = 2 then
842 begin
843 key := LowerCase(p[1]);
844 i := 0;
845 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
846 if i < e_MaxInputKeys then
847 gInputBinds[i].rep := False
848 else
849 g_Console_Add('bindunrep: "' + p[1] + '" is not a key')
850 end
851 else
852 g_Console_Add('bindunrep <key>');
853 'bindlist':
854 for i := 0 to e_MaxInputKeys - 1 do
855 if (gInputBinds[i].down <> nil) or (gInputBinds[i].up <> nil) then
856 g_Console_Add(e_KeyNames[i] + ' ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)));
857 'unbind':
858 // unbind <key>
859 if Length(p) = 2 then
860 begin
861 key := LowerCase(p[1]);
862 i := 0;
863 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
864 if i < e_MaxInputKeys then
865 g_Console_BindKey(i, '')
866 else
867 g_Console_Add('unbind: "' + p[1] + '" is not a key')
868 end
869 else
870 g_Console_Add('unbind <key>');
871 'unbindall':
872 for i := 0 to e_MaxInputKeys - 1 do
873 g_Console_BindKey(i, '');
874 {$IFNDEF HEADLESS}
875 'showkeyboard':
876 g_Touch_ShowKeyboard(True);
877 'hidekeyboard':
878 g_Touch_ShowKeyboard(False);
879 {$ENDIF}
880 'togglemenu':
881 begin
882 if gConsoleShow then
883 g_Console_Switch
884 else if gChatShow then
885 g_Console_Chat_Switch
886 else
887 KeyPress(VK_ESCAPE);
888 menu_toggled := True
889 end;
890 'toggleconsole':
891 g_Console_Switch;
892 'togglechat':
893 g_Console_Chat_Switch;
894 'toggleteamchat':
895 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
896 g_Console_Chat_Switch(True);
897 end
898 end;
900 procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
901 var
902 a: Integer;
903 cp: PCommand;
904 begin
905 SetLength(commands, Length(commands)+1);
906 a := High(commands);
907 cp := @commands[a];
908 cp.cmd := LowerCase(cmd);
909 cp.proc := proc;
910 cp.procEx := nil;
911 cp.help := ahelp;
912 cp.hidden := ahidden;
913 cp.ptr := nil;
914 cp.msg := '';
915 cp.cheat := acheat;
916 cp.action := -1;
917 cp.player := -1;
918 end;
920 procedure AddAction (cmd: AnsiString; action: Integer; help: AnsiString = ''; hidden: Boolean = False; cheat: Boolean = False);
921 const
922 PrefixList: array [0..1] of AnsiString = ('+', '-');
923 PlayerList: array [0..1] of Integer = (1, 2);
924 var
925 s: AnsiString;
926 i: Integer;
928 procedure NewAction (cmd: AnsiString; player: Integer);
929 var cp: PCommand;
930 begin
931 SetLength(commands, Length(commands) + 1);
932 cp := @commands[High(commands)];
933 cp.cmd := LowerCase(cmd);
934 cp.proc := nil;
935 cp.procEx := nil;
936 cp.help := help;
937 cp.hidden := hidden;
938 cp.ptr := nil;
939 cp.msg := '';
940 cp.cheat := cheat;
941 cp.action := action;
942 cp.player := player;
943 end;
945 begin
946 ASSERT(action >= FIRST_ACTION);
947 ASSERT(action <= LAST_ACTION);
948 for s in PrefixList do
949 begin
950 NewAction(s + cmd, 0);
951 for i in PlayerList do
952 NewAction(s + 'p' + IntToStr(i) + '_' + cmd, i - 1)
953 end
954 end;
956 procedure g_Console_Initialize;
957 var a: Integer;
958 begin
959 gConsoleShow := False;
960 gChatShow := False;
961 InputReady := False;
962 CPos := 1;
964 for a := 0 to High(MsgArray) do
965 with MsgArray[a] do
966 begin
967 Msg := '';
968 Time := 0;
969 end;
971 AddCommand('segfault', segfault, 'make segfault');
973 AddCommand('quit', SystemCommands);
974 AddCommand('exit', SystemCommands);
975 AddCommand('r_reset', SystemCommands);
976 AddCommand('r_maxfps', SystemCommands);
977 AddCommand('g_language', SystemCommands);
979 AddCommand('bind', BindCommands);
980 AddCommand('bindrep', BindCommands);
981 AddCommand('bindunrep', BindCommands);
982 AddCommand('bindlist', BindCommands);
983 AddCommand('unbind', BindCommands);
984 AddCommand('unbindall', BindCommands);
985 AddCommand('showkeyboard', BindCommands);
986 AddCommand('hidekeyboard', BindCommands);
987 AddCommand('togglemenu', BindCommands);
988 AddCommand('toggleconsole', BindCommands);
989 AddCommand('togglechat', BindCommands);
990 AddCommand('toggleteamchat', BindCommands);
992 AddCommand('clear', ConsoleCommands, 'clear console');
993 AddCommand('clearhistory', ConsoleCommands);
994 AddCommand('showhistory', ConsoleCommands);
995 AddCommand('commands', ConsoleCommands);
996 AddCommand('time', ConsoleCommands);
997 AddCommand('date', ConsoleCommands);
998 AddCommand('echo', ConsoleCommands);
999 AddCommand('dump', ConsoleCommands);
1000 AddCommand('exec', ConsoleCommands);
1001 AddCommand('writeconfig', ConsoleCommands);
1002 AddCommand('alias', ConsoleCommands);
1003 AddCommand('call', ConsoleCommands);
1004 AddCommand('ver', ConsoleCommands);
1005 AddCommand('version', ConsoleCommands);
1007 AddCommand('d_window', DebugCommands);
1008 AddCommand('d_sounds', DebugCommands);
1009 AddCommand('d_frames', DebugCommands);
1010 AddCommand('d_winmsg', DebugCommands);
1011 AddCommand('d_monoff', DebugCommands);
1012 AddCommand('d_botoff', DebugCommands);
1013 AddCommand('d_monster', DebugCommands);
1014 AddCommand('d_health', DebugCommands);
1015 AddCommand('d_player', DebugCommands);
1016 AddCommand('d_joy', DebugCommands);
1017 AddCommand('d_mem', DebugCommands);
1019 AddCommand('p1_name', PlayerSettingsCVars);
1020 AddCommand('p2_name', PlayerSettingsCVars);
1021 AddCommand('p1_color', PlayerSettingsCVars);
1022 AddCommand('p2_color', PlayerSettingsCVars);
1023 AddCommand('p1_model', PlayerSettingsCVars);
1024 AddCommand('p2_model', PlayerSettingsCVars);
1025 AddCommand('p1_team', PlayerSettingsCVars);
1026 AddCommand('p2_team', PlayerSettingsCVars);
1028 AddCommand('g_max_particles', GameCVars);
1029 AddCommand('g_max_shells', GameCVars);
1030 AddCommand('g_max_gibs', GameCVars);
1031 AddCommand('g_max_corpses', GameCVars);
1032 AddCommand('g_gamemode', GameCVars);
1033 AddCommand('g_friendlyfire', GameCVars);
1034 AddCommand('g_friendly_hit_trace', GameCVars);
1035 AddCommand('g_friendly_hit_projectile', GameCVars);
1036 AddCommand('g_friendly_absorb_damage', GameCVars);
1037 AddCommand('g_weaponstay', GameCVars);
1038 AddCommand('g_allow_exit', GameCVars);
1039 AddCommand('g_dm_keys', GameCVars);
1040 AddCommand('g_allow_monsters', GameCVars);
1041 AddCommand('g_bot_vsmonsters', GameCVars);
1042 AddCommand('g_bot_vsplayers', GameCVars);
1043 AddCommand('g_scorelimit', GameCVars);
1044 AddCommand('g_timelimit', GameCVars);
1045 AddCommand('g_maxlives', GameCVars);
1046 AddCommand('g_warmup_time', GameCVars);
1047 AddCommand('g_spawn_invul', GameCVars);
1048 AddCommand('g_item_respawn_time', GameCVars);
1049 AddCommand('sv_intertime', GameCVars);
1051 AddCommand('sv_name', NetServerCVars);
1052 AddCommand('sv_passwd', NetServerCVars);
1053 AddCommand('sv_maxplrs', NetServerCVars);
1054 AddCommand('sv_public', NetServerCVars);
1055 AddCommand('sv_port', NetServerCVars);
1057 AddCommand('pause', GameCommands);
1058 AddCommand('endgame', GameCommands);
1059 AddCommand('restart', GameCommands);
1060 AddCommand('addbot', GameCommands);
1061 AddCommand('bot_add', GameCommands);
1062 AddCommand('bot_addlist', GameCommands);
1063 AddCommand('bot_addred', GameCommands);
1064 AddCommand('bot_addblue', GameCommands);
1065 AddCommand('bot_removeall', GameCommands);
1066 AddCommand('chat', GameCommands);
1067 AddCommand('teamchat', GameCommands);
1068 AddCommand('game', GameCommands);
1069 AddCommand('host', GameCommands);
1070 AddCommand('map', GameCommands);
1071 AddCommand('nextmap', GameCommands);
1072 AddCommand('endmap', GameCommands);
1073 AddCommand('goodbye', GameCommands);
1074 AddCommand('suicide', GameCommands);
1075 AddCommand('spectate', GameCommands);
1076 AddCommand('ready', GameCommands);
1077 AddCommand('kick', GameCommands);
1078 AddCommand('kick_id', GameCommands);
1079 AddCommand('kick_pid', GameCommands);
1080 AddCommand('ban', GameCommands);
1081 AddCommand('ban_id', GameCommands);
1082 AddCommand('ban_pid', GameCommands);
1083 AddCommand('permban', GameCommands);
1084 AddCommand('permban_id', GameCommands);
1085 AddCommand('permban_pid', GameCommands);
1086 AddCommand('permban_ip', GameCommands);
1087 AddCommand('unban', GameCommands);
1088 AddCommand('connect', GameCommands);
1089 AddCommand('disconnect', GameCommands);
1090 AddCommand('reconnect', GameCommands);
1091 AddCommand('say', GameCommands);
1092 AddCommand('tell', GameCommands);
1093 AddCommand('centerprint', GameCommands);
1094 AddCommand('overtime', GameCommands);
1095 AddCommand('rcon_password', GameCommands);
1096 AddCommand('rcon', GameCommands);
1097 AddCommand('callvote', GameCommands);
1098 AddCommand('vote', GameCommands);
1099 AddCommand('clientlist', GameCommands);
1100 AddCommand('event', GameCommands);
1101 AddCommand('screenshot', GameCommands);
1102 AddCommand('weapon', GameCommands);
1103 AddCommand('p1_weapon', GameCommands);
1104 AddCommand('p2_weapon', GameCommands);
1106 AddCommand('god', GameCheats);
1107 AddCommand('notarget', GameCheats);
1108 AddCommand('give', GameCheats); // "exit" too ;-)
1109 AddCommand('open', GameCheats);
1110 AddCommand('fly', GameCheats);
1111 AddCommand('noclip', GameCheats);
1112 AddCommand('speedy', GameCheats);
1113 AddCommand('jumpy', GameCheats);
1114 AddCommand('noreload', GameCheats);
1115 AddCommand('aimline', GameCheats);
1116 AddCommand('automap', GameCheats);
1118 AddAction('jump', ACTION_JUMP);
1119 AddAction('moveleft', ACTION_MOVELEFT);
1120 AddAction('moveright', ACTION_MOVERIGHT);
1121 AddAction('lookup', ACTION_LOOKUP);
1122 AddAction('lookdown', ACTION_LOOKDOWN);
1123 AddAction('attack', ACTION_ATTACK);
1124 AddAction('scores', ACTION_SCORES);
1125 AddAction('activate', ACTION_ACTIVATE);
1126 AddAction('strafe', ACTION_STRAFE);
1127 AddAction('weapnext', ACTION_WEAPNEXT);
1128 AddAction('weapprev', ACTION_WEAPPREV);
1130 WhitelistCommand('say');
1131 WhitelistCommand('tell');
1132 WhitelistCommand('overtime');
1133 WhitelistCommand('ready');
1134 WhitelistCommand('map');
1135 WhitelistCommand('nextmap');
1136 WhitelistCommand('endmap');
1137 WhitelistCommand('restart');
1138 WhitelistCommand('kick');
1139 WhitelistCommand('kick_pid');
1140 WhitelistCommand('ban');
1141 WhitelistCommand('ban_pid');
1142 WhitelistCommand('centerprint');
1144 WhitelistCommand('addbot');
1145 WhitelistCommand('bot_add');
1146 WhitelistCommand('bot_addred');
1147 WhitelistCommand('bot_addblue');
1148 WhitelistCommand('bot_removeall');
1150 WhitelistCommand('g_gamemode');
1151 WhitelistCommand('g_friendlyfire');
1152 WhitelistCommand('g_friendly_hit_trace');
1153 WhitelistCommand('g_friendly_hit_projectile');
1154 WhitelistCommand('g_friendly_absorb_damage');
1155 WhitelistCommand('g_weaponstay');
1156 WhitelistCommand('g_allow_exit');
1157 WhitelistCommand('g_dm_keys');
1158 WhitelistCommand('g_allow_monsters');
1159 WhitelistCommand('g_bot_vsmonsters');
1160 WhitelistCommand('g_bot_vsplayers');
1161 WhitelistCommand('g_scorelimit');
1162 WhitelistCommand('g_timelimit');
1163 WhitelistCommand('g_maxlives');
1164 WhitelistCommand('g_warmup_time');
1165 WhitelistCommand('g_spawn_invul');
1166 WhitelistCommand('g_item_respawn_time');
1168 g_Console_ResetBinds;
1169 g_Console_ReadConfig(gConfigScript);
1170 // g_Console_ReadConfig(autoexecScript);
1171 g_Console_ReadConfig('autoexec.cfg');
1173 gParsingBinds := False;
1174 end;
1176 procedure g_Console_Finalize;
1177 begin
1179 end;
1181 procedure g_Console_Init;
1182 begin
1183 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
1184 g_Console_Add('');
1185 end;
1187 procedure g_Console_Update;
1188 var
1189 a, b: Integer;
1190 begin
1191 InputReady := gConsoleShow or gChatShow;
1193 a := 0;
1194 while a <= High(MsgArray) do
1195 begin
1196 if MsgArray[a].Time > 0 then
1197 begin
1198 if MsgArray[a].Time = 1 then
1199 begin
1200 if a < High(MsgArray) then
1201 begin
1202 for b := a to High(MsgArray)-1 do
1203 MsgArray[b] := MsgArray[b+1];
1205 MsgArray[High(MsgArray)].Time := 0;
1207 a := a - 1;
1208 end;
1209 end
1210 else
1211 Dec(MsgArray[a].Time);
1212 end;
1214 a := a + 1;
1215 end;
1216 end;
1218 procedure g_Console_Char(C: AnsiChar);
1219 begin
1220 if InputReady and (gConsoleShow or gChatShow) then
1221 begin
1222 Insert(C, Line, CPos);
1223 CPos := CPos + 1;
1224 end
1225 end;
1228 var
1229 tcomplist: array of AnsiString = nil;
1230 tcompidx: array of Integer = nil;
1232 procedure Complete ();
1233 var
1234 i, c: Integer;
1235 tused: Integer;
1236 ll, lpfx, cmd: AnsiString;
1237 begin
1238 if (Length(Line) = 0) then
1239 begin
1240 g_Console_Add('');
1241 for i := 0 to High(commands) do
1242 begin
1243 // hidden commands are hidden when cheats aren't enabled
1244 if commands[i].hidden and not conIsCheatsEnabled then continue;
1245 if (Length(commands[i].help) > 0) then
1246 begin
1247 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1248 end
1249 else
1250 begin
1251 g_Console_Add(' '+commands[i].cmd);
1252 end;
1253 end;
1254 exit;
1255 end;
1257 ll := LowerCase(Line);
1258 lpfx := '';
1260 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
1261 begin
1262 ll := Copy(ll, 0, Length(ll)-1);
1263 for i := 0 to High(commands) do
1264 begin
1265 // hidden commands are hidden when cheats aren't enabled
1266 if commands[i].hidden and not conIsCheatsEnabled then continue;
1267 if (commands[i].cmd = ll) then
1268 begin
1269 if (Length(commands[i].help) > 0) then
1270 begin
1271 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1272 end;
1273 end;
1274 end;
1275 exit;
1276 end;
1278 // build completion list
1279 tused := 0;
1280 for i := 0 to High(commands) do
1281 begin
1282 // hidden commands are hidden when cheats aren't enabled
1283 if commands[i].hidden and not conIsCheatsEnabled then continue;
1284 cmd := commands[i].cmd;
1285 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
1286 begin
1287 if (tused = Length(tcomplist)) then
1288 begin
1289 SetLength(tcomplist, Length(tcomplist)+128);
1290 SetLength(tcompidx, Length(tcompidx)+128);
1291 end;
1292 tcomplist[tused] := cmd;
1293 tcompidx[tused] := i;
1294 Inc(tused);
1295 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
1296 end;
1297 end;
1299 // get longest prefix
1300 for i := 0 to tused-1 do
1301 begin
1302 cmd := tcomplist[i];
1303 for c := 1 to Length(lpfx) do
1304 begin
1305 if (c > Length(cmd)) then break;
1306 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
1307 end;
1308 end;
1310 if (tused = 0) then exit;
1312 if (tused = 1) then
1313 begin
1314 Line := tcomplist[0]+' ';
1315 CPos := Length(Line)+1;
1316 end
1317 else
1318 begin
1319 // has longest prefix?
1320 if (Length(lpfx) > Length(ll)) then
1321 begin
1322 Line := lpfx;
1323 CPos:= Length(Line)+1;
1324 end
1325 else
1326 begin
1327 g_Console_Add('');
1328 for i := 0 to tused-1 do
1329 begin
1330 if (Length(commands[tcompidx[i]].help) > 0) then
1331 begin
1332 g_Console_Add(' '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
1333 end
1334 else
1335 begin
1336 g_Console_Add(' '+tcomplist[i]);
1337 end;
1338 end;
1339 end;
1340 end;
1341 end;
1344 procedure g_Console_Control(K: Word);
1345 begin
1346 case K of
1347 IK_BACKSPACE:
1348 if (Length(Line) > 0) and (CPos > 1) then
1349 begin
1350 Delete(Line, CPos-1, 1);
1351 CPos := CPos-1;
1352 end;
1353 IK_DELETE:
1354 if (Length(Line) > 0) and (CPos <= Length(Line)) then
1355 Delete(Line, CPos, 1);
1356 IK_LEFT, IK_KPLEFT, VK_LEFT, JOY0_LEFT, JOY1_LEFT, JOY2_LEFT, JOY3_LEFT:
1357 if CPos > 1 then
1358 CPos := CPos - 1;
1359 IK_RIGHT, IK_KPRIGHT, VK_RIGHT, JOY0_RIGHT, JOY1_RIGHT, JOY2_RIGHT, JOY3_RIGHT:
1360 if CPos <= Length(Line) then
1361 CPos := CPos + 1;
1362 IK_RETURN, IK_KPRETURN, VK_OPEN, VK_FIRE, JOY0_ATTACK, JOY1_ATTACK, JOY2_ATTACK, JOY3_ATTACK:
1363 begin
1364 if gConsoleShow then
1365 g_Console_Process(Line)
1366 else
1367 if gChatShow then
1368 begin
1369 if (Length(Line) > 0) and g_Game_IsNet then
1370 begin
1371 if gChatTeam then
1372 begin
1373 if g_Game_IsClient then
1374 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
1375 else
1376 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1377 NET_CHAT_TEAM, gPlayer1Settings.Team);
1378 end
1379 else
1380 begin
1381 if g_Game_IsClient then
1382 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
1383 else
1384 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1385 NET_CHAT_PLAYER);
1386 end;
1387 end;
1389 Line := '';
1390 CPos := 1;
1391 gJustChatted := True;
1392 g_Console_Chat_Switch;
1393 InputReady := False;
1394 end;
1395 end;
1396 IK_TAB:
1397 if not gChatShow then
1398 Complete();
1399 IK_DOWN, IK_KPDOWN, VK_DOWN, JOY0_DOWN, JOY1_DOWN, JOY2_DOWN, JOY3_DOWN:
1400 if not gChatShow then
1401 if (CommandHistory <> nil) and
1402 (CmdIndex < Length(CommandHistory)) then
1403 begin
1404 if CmdIndex < Length(CommandHistory)-1 then
1405 CmdIndex := CmdIndex + 1;
1406 Line := CommandHistory[CmdIndex];
1407 CPos := Length(Line) + 1;
1408 end;
1409 IK_UP, IK_KPUP, VK_UP, JOY0_UP, JOY1_UP, JOY2_UP, JOY3_UP:
1410 if not gChatShow then
1411 if (CommandHistory <> nil) and
1412 (CmdIndex <= Length(CommandHistory)) then
1413 begin
1414 if CmdIndex > 0 then
1415 CmdIndex := CmdIndex - 1;
1416 Line := CommandHistory[CmdIndex];
1417 Cpos := Length(Line) + 1;
1418 end;
1419 IK_PAGEUP, IK_KPPAGEUP, VK_PREV, JOY0_PREV, JOY1_PREV, JOY2_PREV, JOY3_PREV: // PgUp
1420 if not gChatShow then Inc(conSkipLines);
1421 IK_PAGEDN, IK_KPPAGEDN, VK_NEXT, JOY0_NEXT, JOY1_NEXT, JOY2_NEXT, JOY3_NEXT: // PgDown
1422 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1423 IK_HOME, IK_KPHOME:
1424 CPos := 1;
1425 IK_END, IK_KPEND:
1426 CPos := Length(Line) + 1;
1427 IK_A..IK_Z, IK_SPACE, IK_SHIFT, IK_RSHIFT, IK_CAPSLOCK, IK_LBRACKET, IK_RBRACKET,
1428 IK_SEMICOLON, IK_QUOTE, IK_BACKSLASH, IK_SLASH, IK_COMMA, IK_DOT, (*IK_EQUALS,*)
1429 IK_0, IK_1, IK_2, IK_3, IK_4, IK_5, IK_6, IK_7, IK_8, IK_9, IK_MINUS, IK_EQUALS:
1430 (* see TEXTINPUT event *)
1431 end
1432 end;
1434 function GetStr(var Str: AnsiString): AnsiString;
1435 var
1436 a, b: Integer;
1437 begin
1438 Result := '';
1439 if Str[1] = '"' then
1440 begin
1441 for b := 1 to Length(Str) do
1442 if (b = Length(Str)) or (Str[b+1] = '"') then
1443 begin
1444 Result := Copy(Str, 2, b-1);
1445 Delete(Str, 1, b+1);
1446 Str := Trim(Str);
1447 Exit;
1448 end;
1449 end;
1451 for a := 1 to Length(Str) do
1452 if (a = Length(Str)) or (Str[a+1] = ' ') then
1453 begin
1454 Result := Copy(Str, 1, a);
1455 Delete(Str, 1, a+1);
1456 Str := Trim(Str);
1457 Exit;
1458 end;
1459 end;
1461 function ParseString(Str: AnsiString): SSArray;
1462 begin
1463 Result := nil;
1465 Str := Trim(Str);
1467 if Str = '' then
1468 Exit;
1470 while Str <> '' do
1471 begin
1472 SetLength(Result, Length(Result)+1);
1473 Result[High(Result)] := GetStr(Str);
1474 end;
1475 end;
1477 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
1479 procedure conmsg (s: AnsiString);
1480 var
1481 a: Integer;
1482 begin
1483 if length(s) = 0 then exit;
1484 for a := 0 to High(MsgArray) do
1485 begin
1486 with MsgArray[a] do
1487 begin
1488 if Time = 0 then
1489 begin
1490 Msg := s;
1491 Time := MsgTime;
1492 exit;
1493 end;
1494 end;
1495 end;
1496 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1497 with MsgArray[High(MsgArray)] do
1498 begin
1499 Msg := L;
1500 Time := MsgTime;
1501 end;
1502 end;
1504 var
1505 f: Integer;
1506 begin
1507 // put it to console
1508 cbufPut(L);
1509 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1511 // now show 'em out of console too
1512 show := show and gAllowConsoleMessages;
1513 if show and gShowMessages then
1514 begin
1515 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1516 while length(L) > 0 do
1517 begin
1518 f := Pos(#10, L);
1519 if f <= 0 then f := length(L)+1;
1520 conmsg(Copy(L, 1, f-1));
1521 Delete(L, 1, f);
1522 end;
1523 end;
1525 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1526 //ConsoleHistory[High(ConsoleHistory)] := L;
1528 (*
1529 {$IFDEF HEADLESS}
1530 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1531 {$ENDIF}
1532 *)
1533 end;
1536 var
1537 consolewriterLastWasEOL: Boolean = false;
1539 procedure consolewriter (constref buf; len: SizeUInt);
1540 var
1541 b: PByte;
1542 begin
1543 if (len < 1) then exit;
1544 b := PByte(@buf);
1545 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1546 while (len > 0) do
1547 begin
1548 if (b[0] <> 13) and (b[0] <> 10) then
1549 begin
1550 cbufPut(AnsiChar(b[0]));
1551 end
1552 else
1553 begin
1554 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1555 cbufPut(#10);
1556 end;
1557 len -= 1;
1558 b += 1;
1559 end;
1560 end;
1563 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1564 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1565 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1566 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1567 begin
1568 g_Console_Add(s, show);
1569 end;
1572 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1573 begin
1574 if show then
1575 begin
1576 g_Console_Add(formatstrf(s, args), true);
1577 end
1578 else
1579 begin
1580 consolewriterLastWasEOL := false;
1581 formatstrf(s, args, consolewriter);
1582 if not consolewriterLastWasEOL then cbufPut(#10);
1583 end;
1584 end;
1587 procedure g_Console_Clear();
1588 begin
1589 //ConsoleHistory := nil;
1590 cbufClear();
1591 conSkipLines := 0;
1592 end;
1594 procedure AddToHistory(L: AnsiString);
1595 var
1596 len: Integer;
1597 begin
1598 len := Length(CommandHistory);
1600 if (len = 0) or
1601 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1602 begin
1603 SetLength(CommandHistory, len+1);
1604 CommandHistory[len] := L;
1605 end;
1607 CmdIndex := Length(CommandHistory);
1608 end;
1610 function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
1611 var
1612 Arr: SSArray;
1613 i: Integer;
1614 begin
1615 Result := True;
1617 Arr := nil;
1619 if Trim(C) = '' then
1620 Exit;
1622 Arr := ParseString(C);
1623 if Arr = nil then
1624 Exit;
1626 for i := 0 to High(Whitelist) do
1627 if Whitelist[i] = LowerCase(Arr[0]) then
1628 Result := False;
1629 end;
1631 procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
1632 var
1633 Arr: SSArray;
1634 i: Integer;
1635 begin
1636 Arr := nil;
1638 if Trim(L) = '' then
1639 Exit;
1641 conSkipLines := 0; // "unscroll"
1643 if L = 'goobers' then
1644 begin
1645 Line := '';
1646 CPos := 1;
1647 gCheats := true;
1648 g_Console_Add('Your memory serves you well.');
1649 exit;
1650 end;
1652 if not quiet then
1653 begin
1654 g_Console_Add('> '+L);
1655 Line := '';
1656 CPos := 1;
1657 end;
1659 Arr := ParseString(L);
1660 if Arr = nil then
1661 Exit;
1663 if commands = nil then
1664 Exit;
1666 if not quiet then
1667 AddToHistory(L);
1669 for i := 0 to High(commands) do
1670 begin
1671 if commands[i].cmd = LowerCase(Arr[0]) then
1672 begin
1673 if commands[i].action >= 0 then
1674 begin
1675 gPlayerAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+';
1676 exit
1677 end;
1678 if assigned(commands[i].procEx) then
1679 begin
1680 commands[i].procEx(@commands[i], Arr);
1681 exit
1682 end;
1683 if assigned(commands[i].proc) then
1684 begin
1685 commands[i].proc(Arr);
1686 exit
1687 end
1688 end
1689 end;
1691 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1692 end;
1695 function g_Console_Interactive: Boolean;
1696 begin
1697 Result := gConsoleShow
1698 end;
1700 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = '');
1701 begin
1702 // e_LogWritefln('bind "%s" "%s" "%s" <%s>', [LowerCase(e_KeyNames[key]), down, up, key]);
1703 ASSERT(key >= 0);
1704 ASSERT(key < e_MaxInputKeys);
1705 if key > 0 then
1706 begin
1707 gInputBinds[key].rep := False;
1708 gInputBinds[key].down := ParseAlias(down);
1709 gInputBinds[key].up := ParseAlias(up);
1710 end;
1711 g_Console_WriteGameConfig();
1712 end;
1714 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
1716 function EqualsCommandLists (a, b: SSArray): Boolean;
1717 var i, len: Integer;
1718 begin
1719 result := False;
1720 len := Length(a);
1721 if len = Length(b) then
1722 begin
1723 i := 0;
1724 while (i < len) and (a[i] = b[i]) do inc(i);
1725 if i >= len then
1726 result := True
1727 end
1728 end;
1730 begin
1731 ASSERT(key >= 0);
1732 ASSERT(key < e_MaxInputKeys);
1733 result := EqualsCommandLists(ParseAlias(down), gInputBinds[key].down) and EqualsCommandLists(ParseAlias(up), gInputBinds[key].up)
1734 end;
1736 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
1737 var i: Integer;
1738 begin
1739 ASSERT(n >= 1);
1740 result := 0;
1741 if commands = nil then Exit;
1742 i := 0;
1743 while (n >= 1) and (i < e_MaxInputKeys) do
1744 begin
1745 if g_Console_MatchBind(i, down, up) then
1746 begin
1747 result := i;
1748 dec(n)
1749 end;
1750 inc(i)
1751 end;
1752 if n >= 1 then
1753 result := 0
1754 end;
1756 function g_Console_Action (action: Integer): Boolean;
1757 var i, len: Integer;
1758 begin
1759 ASSERT(action >= FIRST_ACTION);
1760 ASSERT(action <= LAST_ACTION);
1761 i := 0;
1762 len := Length(gPlayerAction);
1763 while (i < len) and (not gPlayerAction[i, action]) do inc(i);
1764 Result := i < len
1765 end;
1767 function BindsAllowed (key: Integer): Boolean;
1768 var grab, active: Boolean;
1769 begin
1770 Result := False;
1771 {$IFDEF HEADLESS}
1772 grab := False;
1773 active := False;
1774 {$ELSE}
1775 grab := g_GUIGrabInput;
1776 active := g_ActiveWindow <> nil;
1777 {$ENDIF}
1778 if (not grab) and (key >= 0) and (key < e_MaxInputKeys) and ((gInputBinds[key].down <> nil) or (gInputBinds[key].up <> nil)) then
1779 begin
1780 if gChatShow then
1781 Result := g_Console_MatchBind(key, 'togglemenu') or
1782 g_Console_MatchBind(key, 'showkeyboard') or
1783 g_Console_MatchBind(key, 'hidekeyboard')
1784 else if gConsoleShow or active or (gGameSettings.GameType = GT_NONE) then
1785 Result := g_Console_MatchBind(key, 'togglemenu') or
1786 g_Console_MatchBind(key, 'toggleconsole') or
1787 g_Console_MatchBind(key, 'showkeyboard') or
1788 g_Console_MatchBind(key, 'hidekeyboard')
1789 else (* in game *)
1790 Result := True
1791 end
1792 end;
1794 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
1795 var i: Integer;
1796 begin
1797 if BindsAllowed(key) then
1798 begin
1799 if down then
1800 for i := 0 to High(gInputBinds[key].down) do
1801 g_Console_Process(gInputBinds[key].down[i], True)
1802 else
1803 for i := 0 to High(gInputBinds[key].up) do
1804 g_Console_Process(gInputBinds[key].up[i], True)
1805 end;
1806 if down and not menu_toggled then
1807 KeyPress(key);
1808 menu_toggled := False
1809 end;
1811 procedure g_Console_ProcessBindRepeat (key: Integer);
1812 var i: Integer; active: Boolean;
1813 begin
1814 {$IFDEF HEADLESS}
1815 active := False;
1816 {$ELSE}
1817 active := g_ActiveWindow <> nil;
1818 {$ENDIF}
1819 if gConsoleShow or gChatShow or active then
1820 begin
1821 KeyPress(key); // key repeat in menus and shit
1822 Exit;
1823 end;
1824 if BindsAllowed(key) and gInputBinds[key].rep then
1825 begin
1826 for i := 0 to High(gInputBinds[key].down) do
1827 g_Console_Process(gInputBinds[key].down[i], True);
1828 end;
1829 end;
1831 procedure g_Console_ResetBinds;
1832 var i: Integer;
1833 begin
1834 for i := 0 to e_MaxInputKeys - 1 do
1835 g_Console_BindKey(i, '', '');
1837 g_Console_BindKey(IK_GRAVE, 'toggleconsole');
1838 g_Console_BindKey(IK_ESCAPE, 'togglemenu');
1839 g_Console_BindKey(IK_A, '+p1_moveleft', '-p1_moveleft');
1840 g_Console_BindKey(IK_D, '+p1_moveright', '-p1_moveright');
1841 g_Console_BindKey(IK_W, '+p1_lookup', '-p1_lookup');
1842 g_Console_BindKey(IK_S, '+p1_lookdown', '-p1_lookdown');
1843 g_Console_BindKey(IK_SPACE, '+p1_jump', '-p1_jump');
1844 g_Console_BindKey(IK_H, '+p1_attack', '-p1_attack');
1845 g_Console_BindKey(IK_J, '+p1_activate', '-p1_activate');
1846 g_Console_BindKey(IK_E, '+p1_weapnext', '-p1_weapnext');
1847 g_Console_BindKey(IK_Q, '+p1_weapprev', '-p1_weapprev');
1848 g_Console_BindKey(IK_ALT, '+p1_strafe', '-p1_strafe');
1849 g_Console_BindKey(IK_1, 'p1_weapon 1');
1850 g_Console_BindKey(IK_2, 'p1_weapon 2');
1851 g_Console_BindKey(IK_3, 'p1_weapon 3');
1852 g_Console_BindKey(IK_4, 'p1_weapon 4');
1853 g_Console_BindKey(IK_5, 'p1_weapon 5');
1854 g_Console_BindKey(IK_6, 'p1_weapon 6');
1855 g_Console_BindKey(IK_7, 'p1_weapon 7');
1856 g_Console_BindKey(IK_8, 'p1_weapon 8');
1857 g_Console_BindKey(IK_9, 'p1_weapon 9');
1858 g_Console_BindKey(IK_0, 'p1_weapon 10');
1859 g_Console_BindKey(IK_MINUS, 'p1_weapon 11');
1860 g_Console_BindKey(IK_T, 'togglechat');
1861 g_Console_BindKey(IK_Y, 'toggleteamchat');
1862 g_Console_BindKey(IK_F11, 'screenshot');
1863 g_Console_BindKey(IK_TAB, '+p1_scores', '-p1_scores');
1864 g_Console_BindKey(IK_PAUSE, 'pause');
1865 g_Console_BindKey(IK_F1, 'vote');
1867 (* for i := 0 to e_MaxJoys - 1 do *)
1868 for i := 0 to 1 do
1869 begin
1870 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_LEFT), '+p' + IntToStr(i mod 2 + 1) + '_moveleft', '-p' + IntToStr(i mod 2 + 1) + '_moveleft');
1871 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_RIGHT), '+p' + IntToStr(i mod 2 + 1) + '_moveright', '-p' + IntToStr(i mod 2 + 1) + '_moveright');
1872 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_UP), '+p' + IntToStr(i mod 2 + 1) + '_lookup', '-p' + IntToStr(i mod 2 + 1) + '_lookup');
1873 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_DOWN), '+p' + IntToStr(i mod 2 + 1) + '_lookdown', '-p' + IntToStr(i mod 2 + 1) + '_lookdown');
1874 g_Console_BindKey(e_JoyButtonToKey(i, 2), '+p' + IntToStr(i mod 2 + 1) + '_jump', '-p' + IntToStr(i mod 2 + 1) + '_jump');
1875 g_Console_BindKey(e_JoyButtonToKey(i, 0), '+p' + IntToStr(i mod 2 + 1) + '_attack', '-p' + IntToStr(i mod 2 + 1) + '_attack');
1876 g_Console_BindKey(e_JoyButtonToKey(i, 3), '+p' + IntToStr(i mod 2 + 1) + '_activate', '-p' + IntToStr(i mod 2 + 1) + '_activate');
1877 g_Console_BindKey(e_JoyButtonToKey(i, 1), '+p' + IntToStr(i mod 2 + 1) + '_weapnext', '-p' + IntToStr(i mod 2 + 1) + '_weapnext');
1878 g_Console_BindKey(e_JoyButtonToKey(i, 4), '+p' + IntToStr(i mod 2 + 1) + '_weapprev', '-p' + IntToStr(i mod 2 + 1) + '_weapprev');
1879 g_Console_BindKey(e_JoyButtonToKey(i, 7), '+p' + IntToStr(i mod 2 + 1) + '_strafe', '-p' + IntToStr(i mod 2 + 1) + '_strafe');
1880 g_Console_BindKey(e_JoyButtonToKey(i, 10), 'togglemenu');
1881 end;
1883 g_Console_BindKey(VK_ESCAPE, 'togglemenu');
1884 g_Console_BindKey(VK_LSTRAFE, '+moveleft; +strafe', '-moveleft; -strafe');
1885 g_Console_BindKey(VK_RSTRAFE, '+moveright; +strafe', '-moveright; -strafe');
1886 g_Console_BindKey(VK_LEFT, '+moveleft', '-moveleft');
1887 g_Console_BindKey(VK_RIGHT, '+moveright', '-moveright');
1888 g_Console_BindKey(VK_UP, '+lookup', '-lookup');
1889 g_Console_BindKey(VK_DOWN, '+lookdown', '-lookdown');
1890 g_Console_BindKey(VK_JUMP, '+jump', '-jump');
1891 g_Console_BindKey(VK_FIRE, '+attack', '-attack');
1892 g_Console_BindKey(VK_OPEN, '+activate', '-activate');
1893 g_Console_BindKey(VK_NEXT, '+weapnext', '-weapnext');
1894 g_Console_BindKey(VK_PREV, '+weapprev', '-weapprev');
1895 g_Console_BindKey(VK_STRAFE, '+strafe', '-strafe');
1896 g_Console_BindKey(VK_0, 'weapon 1');
1897 g_Console_BindKey(VK_1, 'weapon 2');
1898 g_Console_BindKey(VK_2, 'weapon 3');
1899 g_Console_BindKey(VK_3, 'weapon 4');
1900 g_Console_BindKey(VK_4, 'weapon 5');
1901 g_Console_BindKey(VK_5, 'weapon 6');
1902 g_Console_BindKey(VK_6, 'weapon 7');
1903 g_Console_BindKey(VK_7, 'weapon 8');
1904 g_Console_BindKey(VK_8, 'weapon 9');
1905 g_Console_BindKey(VK_9, 'weapon 10');
1906 g_Console_BindKey(VK_A, 'weapon 11');
1907 g_Console_BindKey(VK_CHAT, 'togglechat');
1908 g_Console_BindKey(VK_TEAM, 'toggleteamchat');
1909 g_Console_BindKey(VK_CONSOLE, 'toggleconsole');
1910 g_Console_BindKey(VK_PRINTSCR, 'screenshot');
1911 g_Console_BindKey(VK_STATUS, '+scores', '-scores');
1912 g_Console_BindKey(VK_SHOWKBD, 'showkeyboard');
1913 g_Console_BindKey(VK_HIDEKBD, 'hidekeyboard');
1914 end;
1916 procedure g_Console_ReadConfig (filename: String);
1917 var f: TextFile; s: AnsiString; i, len: Integer;
1918 begin
1919 e_LogWritefln('g_Console_ReadConfig (1) "%s"', [filename]);
1920 if e_FindResource(ConfigDirs, filename, false) = true then
1921 begin
1922 e_LogWritefln('g_Console_ReadConfig (2) "%s"', [filename]);
1923 AssignFile(f, filename);
1924 Reset(f);
1925 while not EOF(f) do
1926 begin
1927 ReadLn(f, s);
1928 len := Length(s);
1929 if len > 0 then
1930 begin
1931 i := 1;
1932 (* skip spaces *)
1933 while (i <= len) and (s[i] <= ' ') do inc(i);
1934 (* skip comments *)
1935 if (i <= len) and ((s[i] <> '#') and ((i + 1 > len) or (s[i] <> '/') or (s[i + 1] <> '/'))) then
1936 g_Console_Process(s, True);
1937 end
1938 end;
1939 CloseFile(f);
1940 end
1941 end;
1943 procedure g_Console_WriteConfig (filename: String);
1944 var f: TextFile; i, j: Integer;
1946 procedure WriteFlag(name: string; flag: LongWord);
1947 begin
1948 WriteLn(f, name, IfThen(LongBool(gsGameFlags and flag), 1, 0));
1949 end;
1951 function FormatTeam(team: Byte): string;
1952 begin
1953 if team = TEAM_BLUE then
1954 result := 'blue'
1955 else
1956 result := 'red';
1957 end;
1959 begin
1960 // e_LogWritefln('g_Console_WriteConfig: %s', [filename]);
1961 AssignFile(f, filename);
1962 Rewrite(f);
1963 WriteLn(f, '// ' + configComment);
1965 // binds
1966 WriteLn(f, 'unbindall');
1967 for i := 0 to e_MaxInputKeys - 1 do
1968 if (Length(gInputBinds[i].down) > 0) or (Length(gInputBinds[i].up) > 0) then
1969 begin
1970 Write(f, 'bind ', e_KeyNames[i], ' ', QuoteStr(GetCommandString(gInputBinds[i].down)));
1971 if Length(gInputBinds[i].down) = 0 then
1972 Write(f, '""');
1973 if Length(gInputBinds[i].up) > 0 then
1974 Write(f, ' ', QuoteStr(GetCommandString(gInputBinds[i].up)));
1975 WriteLn(f, '');
1976 if gInputBinds[i].rep then
1977 WriteLn(f, 'bindrep ', e_KeyNames[i]);
1978 end;
1980 // lang
1981 if gAskLanguage then
1982 WriteLn(f, 'g_language ask')
1983 else
1984 WriteLn(f, 'g_language ', gLanguage);
1986 // net server
1987 WriteLn(f, 'sv_name ', QuoteStr(NetServerName));
1988 WriteLn(f, 'sv_passwd ', QuoteStr(NetPassword));
1989 WriteLn(f, 'sv_maxplrs ', NetMaxClients);
1990 WriteLn(f, 'sv_port ', NetPort);
1991 WriteLn(f, 'sv_public ', IfThen(NetUseMaster, 1, 0));
1993 // game settings
1994 WriteLn(f, 'g_max_particles ', g_GFX_GetMax());
1995 WriteLn(f, 'g_max_shells ', g_Shells_GetMax());
1996 WriteLn(f, 'g_max_gibs ', g_Gibs_GetMax());
1997 WriteLn(f, 'g_max_corpses ', g_Corpses_GetMax());
1998 WriteLn(f, 'sv_intertime ', gDefInterTime);
2000 // gameplay settings
2001 WriteLn(f, 'g_gamemode ', gsGameMode);
2002 WriteLn(f, 'g_scorelimit ', gsGoalLimit);
2003 WriteLn(f, 'g_timelimit ', gsTimeLimit);
2004 WriteLn(f, 'g_maxlives ', gsMaxLives);
2005 WriteLn(f, 'g_item_respawn_time ', gsItemRespawnTime);
2006 WriteLn(f, 'g_spawn_invul ', gsSpawnInvul);
2007 WriteLn(f, 'g_warmup_time ', gsWarmupTime);
2009 WriteFlag('g_friendlyfire ', GAME_OPTION_TEAMDAMAGE);
2010 WriteFlag('g_friendly_hit_trace ', GAME_OPTION_TEAMHITTRACE);
2011 WriteFlag('g_friendly_hit_projectile ', GAME_OPTION_TEAMHITPROJECTILE);
2012 WriteFlag('g_allow_exit ', GAME_OPTION_ALLOWEXIT);
2013 WriteFlag('g_allow_monsters ', GAME_OPTION_MONSTERS);
2014 WriteFlag('g_dm_keys ', GAME_OPTION_DMKEYS);
2015 WriteFlag('g_weaponstay ', GAME_OPTION_WEAPONSTAY);
2016 WriteFlag('g_bot_vsmonsters ', GAME_OPTION_BOTVSMONSTER);
2017 WriteFlag('g_bot_vsplayers ', GAME_OPTION_BOTVSPLAYER);
2019 // players
2020 with gPlayer1Settings do
2021 begin
2022 WriteLn(f, 'p1_name ', QuoteStr(Name));
2023 WriteLn(f, 'p1_color ', Color.R, ' ', Color.G, ' ', Color.B);
2024 WriteLn(f, 'p1_model ', QuoteStr(Model));
2025 WriteLn(f, 'p1_team ', FormatTeam(Team));
2026 end;
2027 with gPlayer2Settings do
2028 begin
2029 WriteLn(f, 'p2_name ', QuoteStr(Name));
2030 WriteLn(f, 'p2_color ', Color.R, ' ', Color.G, ' ', Color.B);
2031 WriteLn(f, 'p2_model ', QuoteStr(Model));
2032 WriteLn(f, 'p2_team ', FormatTeam(Team));
2033 end;
2035 // all cvars
2036 for i := 0 to High(commands) do
2037 begin
2038 if not commands[i].cheat then
2039 begin
2040 if @commands[i].procEx = @boolVarHandler then
2041 begin
2042 if PBoolean(commands[i].ptr)^ then j := 1 else j := 0;
2043 WriteLn(f, commands[i].cmd, ' ', j)
2044 end
2045 else if @commands[i].procEx = @intVarHandler then
2046 begin
2047 WriteLn(f, commands[i].cmd, ' ', PInteger(commands[i].ptr)^)
2048 end
2049 else if @commands[i].procEx = @wordVarHandler then
2050 begin
2051 WriteLn(f, commands[i].cmd, ' ', PWord(commands[i].ptr)^)
2052 end
2053 else if @commands[i].procEx = @dwordVarHandler then
2054 begin
2055 WriteLn(f, commands[i].cmd, ' ', PCardinal(commands[i].ptr)^)
2056 end
2057 else if @commands[i].procEx = @singleVarHandler then
2058 begin
2059 WriteLn(f, commands[i].cmd, ' ', PVarSingle(commands[i].ptr).val^:0:6)
2060 end
2061 else if @commands[i].procEx = @strVarHandler then
2062 begin
2063 if Length(PAnsiString(commands[i].ptr)^) = 0 then
2064 WriteLn(f, commands[i].cmd, ' ""')
2065 else
2066 WriteLn(f, commands[i].cmd, ' ', QuoteStr(PAnsiString(commands[i].ptr)^))
2067 end
2068 end
2069 end;
2071 WriteLn(f, 'r_maxfps ', gMaxFPS);
2072 WriteLn(f, 'r_reset');
2073 CloseFile(f)
2074 end;
2076 procedure g_Console_WriteGameConfig;
2077 var s: AnsiString;
2078 begin
2079 if gParsingBinds = false then
2080 begin
2081 s := e_GetWriteableDir(ConfigDirs);
2082 g_Console_WriteConfig(e_CatPath(s, gConfigScript))
2083 end
2084 end;
2086 procedure Init;
2087 var i: Integer;
2088 begin
2089 conRegVar('d_eres', @debug_e_res, '', '');
2090 for i := 1 to e_MaxJoys do
2091 conRegVar('joy' + IntToStr(i) + '_deadzone', @e_JoystickDeadzones[i - 1], '', '')
2092 end;
2094 initialization
2095 Init
2096 end.