DEADSOFTWARE

13bf81f55870bc559f01d47e663b715cb83874ec
[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;
34 FIRST_ACTION = ACTION_JUMP;
35 LAST_ACTION = ACTION_STRAFE;
37 var (* private state *)
38 Line: AnsiString;
39 CPos: Word;
40 conSkipLines: Integer;
41 MsgArray: Array [0..4] of record
42 Msg: AnsiString;
43 Time: Word;
44 end;
46 procedure g_Console_Init;
47 procedure g_Console_Initialize;
48 procedure g_Console_Finalize;
49 procedure g_Console_Update;
50 procedure g_Console_Char (C: AnsiChar);
51 procedure g_Console_Control (K: Word);
52 procedure g_Console_Process (L: AnsiString; quiet: Boolean=false);
53 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
54 procedure g_Console_Clear;
55 function g_Console_CommandBlacklisted (C: AnsiString): Boolean;
56 procedure g_Console_ReadConfig (filename: String);
57 procedure g_Console_WriteConfig (filename: String);
58 procedure g_Console_WriteGameConfig;
60 function g_Console_Interactive: Boolean;
61 function g_Console_Action (action: Integer): Boolean;
62 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
63 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
64 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = ''; rep: Boolean = False);
65 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
66 procedure g_Console_ProcessBindRepeat (key: Integer);
67 procedure g_Console_ResetBinds;
69 procedure conwriteln (const s: AnsiString; show: Boolean=false);
70 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
72 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
73 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
74 procedure conRegVar (const conname: AnsiString; pvar: PInteger; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
75 procedure conRegVar (const conname: AnsiString; pvar: PWord; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
76 procedure conRegVar (const conname: AnsiString; pvar: PCardinal; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
77 procedure conRegVar (const conname: AnsiString; pvar: PAnsiString; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
79 // <0: no arg; 0/1: true/false
80 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
82 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
83 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
85 const
86 {$IFDEF HEADLESS}
87 defaultConfigScript = 'dfserver.cfg';
88 {$ELSE}
89 defaultConfigScript = 'dfconfig.cfg';
90 {$ENDIF}
92 var
93 gConsoleShow: Boolean = false; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
94 gChatShow: Boolean = false;
95 gChatTeam: Boolean = false;
96 gAllowConsoleMessages: Boolean = true;
97 gJustChatted: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
98 gParsingBinds: Boolean = true; // íå ïåðåñîõðàíÿòü êîíôèã âî âðåìÿ ïàðñèíãà
99 gPlayerAction: Array [0..1, 0..LAST_ACTION] of Boolean; // [player, action]
100 gConfigScript: string = defaultConfigScript;
102 implementation
104 uses
105 {$IFDEF ENABLE_MENU}
106 g_gui, g_menu,
107 {$ENDIF}
108 {$IFNDEF HEADLESS}
109 g_touch,
110 {$ENDIF}
111 g_textures, e_input, g_game, g_gfx, g_player, g_items,
112 SysUtils, g_basic, g_options, Math, e_res,
113 g_language, g_net, g_netmsg, e_log, conbuf, g_weapons,
114 Keyboard;
116 const
117 configComment = 'generated by doom2d, do not modify';
119 type
120 PCommand = ^TCommand;
122 TCmdProc = procedure (p: SSArray);
123 TCmdProcEx = procedure (me: PCommand; p: SSArray);
125 TCommand = record
126 cmd: AnsiString;
127 proc: TCmdProc;
128 procEx: TCmdProcEx;
129 help: AnsiString;
130 hidden: Boolean;
131 ptr: Pointer; // various data
132 msg: AnsiString; // message for var changes
133 cheat: Boolean;
134 action: Integer; // >= 0 for action commands
135 player: Integer; // used for action commands
136 end;
138 TAlias = record
139 name: AnsiString;
140 commands: SSArray;
141 end;
144 const
145 MsgTime = 144;
146 MaxScriptRecursion = 16;
148 var
149 RecursionDepth: Word = 0;
150 RecursionLimitHit: Boolean = False;
151 InputReady: Boolean; // allow text input in console/chat
152 //ConsoleHistory: SSArray;
153 CommandHistory: SSArray;
154 Whitelist: SSArray;
155 commands: Array of TCommand = nil;
156 Aliases: Array of TAlias = nil;
157 CmdIndex: Word;
159 gInputBinds: Array [0..e_MaxInputKeys - 1] of record
160 rep: Boolean;
161 down, up: SSArray;
162 end;
164 menu_toggled: BOOLEAN; (* hack for menu controls *)
165 ConsoleStdIn: Boolean;
167 procedure g_Console_Switch;
168 begin
169 gChatShow := False;
170 gConsoleShow := not gConsoleShow;
171 InputReady := False;
172 {$IFNDEF HEADLESS}
173 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
174 {$ENDIF}
175 end;
177 procedure g_Console_Chat_Switch (Team: Boolean = False);
178 begin
179 if not g_Game_IsNet then Exit;
180 gConsoleShow := False;
181 gChatShow := not gChatShow;
182 gChatTeam := Team;
183 InputReady := False;
184 Line := '';
185 CPos := 1;
186 {$IFNDEF HEADLESS}
187 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
188 {$ENDIF}
189 end;
191 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
192 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
193 var
194 pos: Integer = 1;
195 frac: Single = 1;
196 slen: Integer;
197 begin
198 result := false;
199 res := 0;
200 slen := Length(s);
201 while (slen > 0) and (s[slen] <= ' ') do Dec(slen);
202 while (pos <= slen) and (s[pos] <= ' ') do Inc(pos);
203 if (pos > slen) then exit;
204 if (slen-pos = 1) and (s[pos] = '.') then exit; // single dot
205 // integral part
206 while (pos <= slen) do
207 begin
208 if (s[pos] < '0') or (s[pos] > '9') then break;
209 res := res*10+Byte(s[pos])-48;
210 Inc(pos);
211 end;
212 if (pos <= slen) then
213 begin
214 // must be a dot
215 if (s[pos] <> '.') then exit;
216 Inc(pos);
217 while (pos <= slen) do
218 begin
219 if (s[pos] < '0') or (s[pos] > '9') then break;
220 frac := frac/10;
221 res += frac*(Byte(s[pos])-48);
222 Inc(pos);
223 end;
224 end;
225 if (pos <= slen) then exit; // oops
226 result := true;
227 end;
230 // ////////////////////////////////////////////////////////////////////////// //
231 // <0: no arg; 0/1: true/false; 666: toggle
232 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
233 begin
234 if (idx < 0) or (idx > High(p)) then begin result := -1; exit; end;
235 result := 0;
236 if (p[idx] = '1') or (CompareText(p[idx], 'on') = 0) or (CompareText(p[idx], 'true') = 0) or
237 (CompareText(p[idx], 'tan') = 0) or (CompareText(p[idx], 'yes') = 0) then result := 1
238 else if (CompareText(p[idx], 'toggle') = 0) or (CompareText(p[idx], 'switch') = 0) or
239 (CompareText(p[idx], 't') = 0) then result := 666;
240 end;
243 procedure boolVarHandler (me: PCommand; p: SSArray);
244 procedure binaryFlag (var flag: Boolean; msg: AnsiString);
245 var
246 old: Boolean;
247 begin
248 if (Length(p) > 2) then
249 begin
250 conwritefln('too many arguments to ''%s''', [p[0]]);
251 end
252 else
253 begin
254 old := flag;
255 case conGetBoolArg(p, 1) of
256 -1: begin end;
257 0: if not me.cheat or conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
258 1: if not me.cheat or conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
259 666: if not me.cheat or conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
260 end;
261 if flag <> old then
262 g_Console_WriteGameConfig();
263 if (Length(msg) = 0) then msg := p[0] else msg += ':';
264 if flag then conwritefln('%s tan', [msg]) else conwritefln('%s ona', [msg]);
265 end;
266 end;
267 begin
268 binaryFlag(PBoolean(me.ptr)^, me.msg);
269 end;
272 procedure intVarHandler (me: PCommand; p: SSArray);
273 var
274 old: Integer;
275 begin
276 if (Length(p) <> 2) then
277 begin
278 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
279 end
280 else
281 begin
282 try
283 old := PInteger(me.ptr)^;
284 PInteger(me.ptr)^ := StrToInt(p[1]);
285 if PInteger(me.ptr)^ <> old then
286 g_Console_WriteGameConfig();
287 except
288 conwritefln('invalid integer value: "%s"', [p[1]]);
289 end;
290 end;
291 end;
294 procedure wordVarHandler (me: PCommand; p: SSArray);
295 var
296 old: Integer;
297 begin
298 if (Length(p) <> 2) then
299 begin
300 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
301 end
302 else
303 begin
304 try
305 old := PWord(me.ptr)^;
306 PWord(me.ptr)^ := min($FFFF, StrToDWord(p[1]));
307 if PWord(me.ptr)^ <> old then
308 g_Console_WriteGameConfig();
309 except
310 conwritefln('invalid word value: "%s"', [p[1]]);
311 end;
312 end;
313 end;
316 procedure dwordVarHandler (me: PCommand; p: SSArray);
317 var
318 old: Integer;
319 begin
320 if (Length(p) <> 2) then
321 begin
322 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
323 end
324 else
325 begin
326 try
327 old := PCardinal(me.ptr)^;
328 PCardinal(me.ptr)^ := StrToDWord(p[1]);
329 if PCardinal(me.ptr)^ <> old then
330 g_Console_WriteGameConfig();
331 except
332 conwritefln('invalid dword value: "%s"', [p[1]]);
333 end;
334 end;
335 end;
338 procedure strVarHandler (me: PCommand; p: SSArray);
339 var
340 old: AnsiString;
341 begin
342 if (Length(p) <> 2) then
343 begin
344 conwritefln('%s %s', [me.cmd, QuoteStr(PAnsiString(me.ptr)^)]);
345 end
346 else
347 begin
348 old := PAnsiString(me.ptr)^;
349 PAnsiString(me.ptr)^ := p[1];
350 if PAnsiString(me.ptr)^ <> old then
351 g_Console_WriteGameConfig();
352 end;
353 end;
356 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
357 var
358 f: Integer;
359 cp: PCommand;
360 begin
361 f := Length(commands);
362 SetLength(commands, f+1);
363 cp := @commands[f];
364 cp.cmd := LowerCase(conname);
365 cp.proc := nil;
366 cp.procEx := boolVarHandler;
367 cp.help := ahelp;
368 cp.hidden := ahidden;
369 cp.ptr := pvar;
370 cp.msg := amsg;
371 cp.cheat := acheat;
372 cp.action := -1;
373 cp.player := -1;
374 end;
377 procedure conRegVar (const conname: AnsiString; pvar: PInteger; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
378 var
379 f: Integer;
380 cp: PCommand;
381 begin
382 f := Length(commands);
383 SetLength(commands, f+1);
384 cp := @commands[f];
385 cp.cmd := LowerCase(conname);
386 cp.proc := nil;
387 cp.procEx := intVarHandler;
388 cp.help := ahelp;
389 cp.hidden := ahidden;
390 cp.ptr := pvar;
391 cp.msg := amsg;
392 cp.cheat := acheat;
393 cp.action := -1;
394 cp.player := -1;
395 end;
398 procedure conRegVar (const conname: AnsiString; pvar: PWord; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
399 var
400 f: Integer;
401 cp: PCommand;
402 begin
403 f := Length(commands);
404 SetLength(commands, f+1);
405 cp := @commands[f];
406 cp.cmd := LowerCase(conname);
407 cp.proc := nil;
408 cp.procEx := wordVarHandler;
409 cp.help := ahelp;
410 cp.hidden := ahidden;
411 cp.ptr := pvar;
412 cp.msg := amsg;
413 cp.cheat := acheat;
414 cp.action := -1;
415 cp.player := -1;
416 end;
419 procedure conRegVar (const conname: AnsiString; pvar: PCardinal; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
420 var
421 f: Integer;
422 cp: PCommand;
423 begin
424 f := Length(commands);
425 SetLength(commands, f+1);
426 cp := @commands[f];
427 cp.cmd := LowerCase(conname);
428 cp.proc := nil;
429 cp.procEx := dwordVarHandler;
430 cp.help := ahelp;
431 cp.hidden := ahidden;
432 cp.ptr := pvar;
433 cp.msg := amsg;
434 cp.cheat := acheat;
435 cp.action := -1;
436 cp.player := -1;
437 end;
440 procedure conRegVar (const conname: AnsiString; pvar: PAnsiString; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
441 var
442 f: Integer;
443 cp: PCommand;
444 begin
445 f := Length(commands);
446 SetLength(commands, f+1);
447 cp := @commands[f];
448 cp.cmd := LowerCase(conname);
449 cp.proc := nil;
450 cp.procEx := strVarHandler;
451 cp.help := ahelp;
452 cp.hidden := ahidden;
453 cp.ptr := pvar;
454 cp.msg := amsg;
455 cp.cheat := acheat;
456 cp.action := -1;
457 cp.player := -1;
458 end;
460 // ////////////////////////////////////////////////////////////////////////// //
461 type
462 PVarSingle = ^TVarSingle;
463 TVarSingle = record
464 val: PSingle;
465 min, max, def: Single; // default will be starting value
466 end;
469 procedure singleVarHandler (me: PCommand; p: SSArray);
470 var
471 pv: PVarSingle;
472 nv, old: Single;
473 msg: AnsiString;
474 begin
475 if (Length(p) > 2) then
476 begin
477 conwritefln('too many arguments to ''%s''', [me.cmd]);
478 exit;
479 end;
480 pv := PVarSingle(me.ptr);
481 old := pv.val^;
482 if (Length(p) = 2) then
483 begin
484 if me.cheat and (not conIsCheatsEnabled) then begin conwriteln('not available'); exit; end;
485 if (CompareText(p[1], 'default') = 0) or (CompareText(p[1], 'def') = 0) or
486 (CompareText(p[1], 'd') = 0) or (CompareText(p[1], 'off') = 0) or
487 (CompareText(p[1], 'ona') = 0) then
488 begin
489 pv.val^ := pv.def;
490 end
491 else
492 begin
493 if not conParseFloat(nv, p[1]) then
494 begin
495 conwritefln('%s: ''%s'' doesn''t look like a floating number', [me.cmd, p[1]]);
496 exit;
497 end;
498 if (nv < pv.min) then nv := pv.min;
499 if (nv > pv.max) then nv := pv.max;
500 pv.val^ := nv;
501 end;
502 end;
503 if pv.val^ <> old then
504 g_Console_WriteGameConfig();
505 msg := me.msg;
506 if (Length(msg) = 0) then msg := me.cmd else msg += ':';
507 conwritefln('%s %s', [msg, pv.val^]);
508 end;
511 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
512 var
513 f: Integer;
514 cp: PCommand;
515 pv: PVarSingle;
516 begin
517 GetMem(pv, sizeof(TVarSingle));
518 pv.val := pvar;
519 pv.min := amin;
520 pv.max := amax;
521 pv.def := pvar^;
522 f := Length(commands);
523 SetLength(commands, f+1);
524 cp := @commands[f];
525 cp.cmd := LowerCase(conname);
526 cp.proc := nil;
527 cp.procEx := singleVarHandler;
528 cp.help := ahelp;
529 cp.hidden := ahidden;
530 cp.ptr := pv;
531 cp.msg := amsg;
532 cp.cheat := acheat;
533 cp.action := -1;
534 cp.player := -1;
535 end;
538 // ////////////////////////////////////////////////////////////////////////// //
539 function GetStrACmd(var Str: AnsiString): AnsiString;
540 var
541 a: Integer;
542 begin
543 Result := '';
544 for a := 1 to Length(Str) do
545 if (a = Length(Str)) or (Str[a+1] = ';') then
546 begin
547 Result := Copy(Str, 1, a);
548 Delete(Str, 1, a+1);
549 Str := Trim(Str);
550 Exit;
551 end;
552 end;
554 function ParseAlias(Str: AnsiString): SSArray;
555 begin
556 Result := nil;
558 Str := Trim(Str);
560 if Str = '' then
561 Exit;
563 while Str <> '' do
564 begin
565 SetLength(Result, Length(Result)+1);
566 Result[High(Result)] := GetStrACmd(Str);
567 end;
568 end;
570 procedure ConsoleCommands(p: SSArray);
571 var
572 cmd, s: AnsiString;
573 a, b: Integer;
574 (* F: TextFile; *)
575 begin
576 cmd := LowerCase(p[0]);
577 s := '';
579 if cmd = 'clear' then
580 begin
581 //ConsoleHistory := nil;
582 cbufClear();
583 conSkipLines := 0;
585 for a := 0 to High(MsgArray) do
586 with MsgArray[a] do
587 begin
588 Msg := '';
589 Time := 0;
590 end;
591 end;
593 if cmd = 'clearhistory' then
594 CommandHistory := nil;
596 if cmd = 'showhistory' then
597 if CommandHistory <> nil then
598 begin
599 g_Console_Add('');
600 for a := 0 to High(CommandHistory) do
601 g_Console_Add(' '+CommandHistory[a]);
602 end;
604 if cmd = 'commands' then
605 begin
606 g_Console_Add('');
607 g_Console_Add('commands list:');
608 for a := High(commands) downto 0 do
609 begin
610 if (Length(commands[a].help) > 0) then
611 begin
612 g_Console_Add(' '+commands[a].cmd+' -- '+commands[a].help);
613 end
614 else
615 begin
616 g_Console_Add(' '+commands[a].cmd);
617 end;
618 end;
619 end;
621 if cmd = 'time' then
622 g_Console_Add(TimeToStr(Now), True);
624 if cmd = 'date' then
625 g_Console_Add(DateToStr(Now), True);
627 if cmd = 'echo' then
628 if Length(p) > 1 then
629 begin
630 if p[1] = 'ololo' then
631 gCheats := True
632 else
633 begin
634 s := '';
635 for a := 1 to High(p) do
636 s := s + p[a] + ' ';
637 g_Console_Add(b_Text_Format(s), True);
638 end;
639 end
640 else
641 g_Console_Add('');
643 if cmd = 'dump' then
644 begin
645 (*
646 if ConsoleHistory <> nil then
647 begin
648 if Length(P) > 1 then
649 s := P[1]
650 else
651 s := GameDir+'/console.txt';
653 {$I-}
654 AssignFile(F, s);
655 Rewrite(F);
656 if IOResult <> 0 then
657 begin
658 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
659 CloseFile(F);
660 Exit;
661 end;
663 for a := 0 to High(ConsoleHistory) do
664 WriteLn(F, ConsoleHistory[a]);
666 CloseFile(F);
667 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
668 {$I+}
669 end;
670 *)
671 end;
673 if cmd = 'exec' then
674 begin
675 // exec <filename>
676 if Length(p) = 2 then
677 g_Console_ReadConfig(p[1])
678 else
679 g_Console_Add('exec <script file>');
680 end;
682 if cmd = 'writeconfig' then
683 begin
684 // writeconfig <filename>
685 if Length(p) = 2 then
686 begin
687 s := e_GetWriteableDir(ConfigDirs);
688 g_Console_WriteConfig(e_CatPath(s, p[1]))
689 end
690 else
691 begin
692 g_Console_Add('writeconfig <file>')
693 end
694 end;
696 if (cmd = 'ver') or (cmd = 'version') then
697 begin
698 conwriteln('Doom 2D: Forever v. ' + GAME_VERSION);
699 conwritefln('Net protocol v. %d', [NET_PROTOCOL_VER]);
700 conwritefln('Build date: %s at %s', [GAME_BUILDDATE, GAME_BUILDTIME]);
701 end;
703 if cmd = 'alias' then
704 begin
705 // alias [alias_name] [commands]
706 if Length(p) > 1 then
707 begin
708 for a := 0 to High(Aliases) do
709 if Aliases[a].name = p[1] then
710 begin
711 if Length(p) > 2 then
712 Aliases[a].commands := ParseAlias(p[2])
713 else
714 for b := 0 to High(Aliases[a].commands) do
715 g_Console_Add(Aliases[a].commands[b]);
716 Exit;
717 end;
718 SetLength(Aliases, Length(Aliases)+1);
719 a := High(Aliases);
720 Aliases[a].name := p[1];
721 if Length(p) > 2 then
722 Aliases[a].commands := ParseAlias(p[2])
723 else
724 for b := 0 to High(Aliases[a].commands) do
725 g_Console_Add(Aliases[a].commands[b]);
726 end else
727 for a := 0 to High(Aliases) do
728 if Aliases[a].commands <> nil then
729 g_Console_Add(Aliases[a].name);
730 end;
732 if cmd = 'call' then
733 begin
734 // call <alias_name>
735 if Length(p) > 1 then
736 begin
737 if Aliases = nil then
738 Exit;
739 for a := 0 to High(Aliases) do
740 if Aliases[a].name = p[1] then
741 begin
742 if Aliases[a].commands <> nil then
743 begin
744 // with this system proper endless loop detection seems either impossible
745 // or very dirty to implement, so let's have this instead
746 // prevents endless loops
747 for b := 0 to High(Aliases[a].commands) do
748 begin
749 Inc(RecursionDepth);
750 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
751 if not RecursionLimitHit then
752 g_Console_Process(Aliases[a].commands[b], True);
753 Dec(RecursionDepth);
754 end;
755 if (RecursionDepth = 0) and RecursionLimitHit then
756 begin
757 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
758 RecursionLimitHit := False;
759 end;
760 end;
761 Exit;
762 end;
763 end
764 else
765 g_Console_Add('call <alias name>');
766 end;
767 end;
769 procedure WhitelistCommand(cmd: AnsiString);
770 var
771 a: Integer;
772 begin
773 SetLength(Whitelist, Length(Whitelist)+1);
774 a := High(Whitelist);
775 Whitelist[a] := LowerCase(cmd);
776 end;
778 procedure segfault (p: SSArray);
779 var
780 pp: PByte = nil;
781 begin
782 pp^ := 0;
783 end;
785 function GetCommandString (p: SSArray): AnsiString;
786 var i: Integer;
787 begin
788 result := '';
789 if Length(p) >= 1 then
790 begin
791 result := p[0];
792 for i := 1 to High(p) do
793 result := result + '; ' + p[i]
794 end
795 end;
797 function QuoteStr(str: String): String;
798 begin
799 if Pos(' ', str) > 0 then
800 Result := '"' + str + '"'
801 else
802 Result := str;
803 end;
805 procedure BindCommands (p: SSArray);
806 var cmd, key: AnsiString; i: Integer;
807 begin
808 cmd := LowerCase(p[0]);
809 case cmd of
810 'bind':
811 // bind <key> [down [up]]
812 if (Length(p) >= 2) and (Length(p) <= 4) then
813 begin
814 i := 0;
815 key := LowerCase(p[1]);
816 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
817 if i < e_MaxInputKeys then
818 begin
819 if Length(p) = 2 then
820 g_Console_Add(QuoteStr(e_KeyNames[i]) + ' = ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)))
821 else if Length(p) = 3 then
822 g_Console_BindKey(i, p[2], '')
823 else (* len = 4 *)
824 g_Console_BindKey(i, p[2], p[3])
825 end
826 else
827 g_Console_Add('bind: "' + p[1] + '" is not a key')
828 end
829 else
830 begin
831 g_Console_Add('bind <key> <down action> [up action]')
832 end;
833 'bindrep':
834 // bindrep <key>
835 if Length(p) = 2 then
836 begin
837 key := LowerCase(p[1]);
838 i := 0;
839 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
840 if i < e_MaxInputKeys then
841 gInputBinds[i].rep := True
842 else
843 g_Console_Add('bindrep: "' + p[1] + '" is not a key')
844 end
845 else
846 g_Console_Add('bindrep <key>');
847 'bindunrep':
848 // bindunrep <key>
849 if Length(p) = 2 then
850 begin
851 key := LowerCase(p[1]);
852 i := 0;
853 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
854 if i < e_MaxInputKeys then
855 gInputBinds[i].rep := False
856 else
857 g_Console_Add('bindunrep: "' + p[1] + '" is not a key')
858 end
859 else
860 g_Console_Add('bindunrep <key>');
861 'bindlist':
862 for i := 0 to e_MaxInputKeys - 1 do
863 if (gInputBinds[i].down <> nil) or (gInputBinds[i].up <> nil) then
864 g_Console_Add(e_KeyNames[i] + ' ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)));
865 'unbind':
866 // unbind <key>
867 if Length(p) = 2 then
868 begin
869 key := LowerCase(p[1]);
870 i := 0;
871 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
872 if i < e_MaxInputKeys then
873 g_Console_BindKey(i, '')
874 else
875 g_Console_Add('unbind: "' + p[1] + '" is not a key')
876 end
877 else
878 g_Console_Add('unbind <key>');
879 'unbindall':
880 for i := 0 to e_MaxInputKeys - 1 do
881 g_Console_BindKey(i, '');
882 {$IFNDEF HEADLESS}
883 'showkeyboard':
884 g_Touch_ShowKeyboard(True);
885 'hidekeyboard':
886 g_Touch_ShowKeyboard(False);
887 {$ENDIF}
888 'togglemenu':
889 begin
890 if gConsoleShow then
891 g_Console_Switch
892 else if gChatShow then
893 g_Console_Chat_Switch
894 else
895 begin
896 KeyPress(VK_ESCAPE);
897 end;
898 menu_toggled := True
899 end;
900 'toggleconsole':
901 g_Console_Switch;
902 'togglechat':
903 g_Console_Chat_Switch;
904 'toggleteamchat':
905 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
906 g_Console_Chat_Switch(True);
907 end
908 end;
910 procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
911 var
912 a: Integer;
913 cp: PCommand;
914 begin
915 SetLength(commands, Length(commands)+1);
916 a := High(commands);
917 cp := @commands[a];
918 cp.cmd := LowerCase(cmd);
919 cp.proc := proc;
920 cp.procEx := nil;
921 cp.help := ahelp;
922 cp.hidden := ahidden;
923 cp.ptr := nil;
924 cp.msg := '';
925 cp.cheat := acheat;
926 cp.action := -1;
927 cp.player := -1;
928 end;
930 procedure AddAction (cmd: AnsiString; action: Integer; help: AnsiString = ''; hidden: Boolean = False; cheat: Boolean = False);
931 const
932 PrefixList: array [0..1] of AnsiString = ('+', '-');
933 PlayerList: array [0..1] of Integer = (1, 2);
934 var
935 s: AnsiString;
936 i: Integer;
938 procedure NewAction (cmd: AnsiString; player: Integer);
939 var cp: PCommand;
940 begin
941 SetLength(commands, Length(commands) + 1);
942 cp := @commands[High(commands)];
943 cp.cmd := LowerCase(cmd);
944 cp.proc := nil;
945 cp.procEx := nil;
946 cp.help := help;
947 cp.hidden := hidden;
948 cp.ptr := nil;
949 cp.msg := '';
950 cp.cheat := cheat;
951 cp.action := action;
952 cp.player := player;
953 end;
955 begin
956 ASSERT(action >= FIRST_ACTION);
957 ASSERT(action <= LAST_ACTION);
958 for s in PrefixList do
959 begin
960 NewAction(s + cmd, 0);
961 for i in PlayerList do
962 NewAction(s + 'p' + IntToStr(i) + '_' + cmd, i - 1)
963 end
964 end;
966 procedure ReadStdIn;
967 var
968 K: Char;
969 KEv: TKeyEvent;
970 begin
971 gConsoleShow := True;
972 InputReady := True;
973 // one key per frame
974 KEv := PollKeyEvent();
975 if KEv <> 0 then
976 begin
977 K := GetKeyEventChar(TranslateKeyEvent(GetKeyEvent()));
978 Write(K);
979 case K of
980 #8: g_Console_Control(IK_BACKSPACE);
981 #10, #13: g_Console_Control(IK_RETURN);
982 #32..#126: g_Console_Char(K);
983 // arrow keys and DEL all return 0 for some reason, so fuck em
984 end;
985 end;
986 end;
988 procedure g_Console_Initialize;
989 var a: Integer;
990 begin
991 gConsoleShow := False;
992 gChatShow := False;
993 InputReady := False;
994 CPos := 1;
996 for a := 0 to High(MsgArray) do
997 with MsgArray[a] do
998 begin
999 Msg := '';
1000 Time := 0;
1001 end;
1003 AddCommand('segfault', segfault, 'make segfault');
1005 AddCommand('quit', SystemCommands);
1006 AddCommand('exit', SystemCommands);
1007 AddCommand('r_reset', SystemCommands);
1008 AddCommand('r_maxfps', SystemCommands);
1009 AddCommand('g_language', SystemCommands);
1011 AddCommand('bind', BindCommands);
1012 AddCommand('bindrep', BindCommands);
1013 AddCommand('bindunrep', BindCommands);
1014 AddCommand('bindlist', BindCommands);
1015 AddCommand('unbind', BindCommands);
1016 AddCommand('unbindall', BindCommands);
1017 AddCommand('showkeyboard', BindCommands);
1018 AddCommand('hidekeyboard', BindCommands);
1019 AddCommand('togglemenu', BindCommands);
1020 AddCommand('toggleconsole', BindCommands);
1021 AddCommand('togglechat', BindCommands);
1022 AddCommand('toggleteamchat', BindCommands);
1024 AddCommand('clear', ConsoleCommands, 'clear console');
1025 AddCommand('clearhistory', ConsoleCommands);
1026 AddCommand('showhistory', ConsoleCommands);
1027 AddCommand('commands', ConsoleCommands);
1028 AddCommand('time', ConsoleCommands);
1029 AddCommand('date', ConsoleCommands);
1030 AddCommand('echo', ConsoleCommands);
1031 AddCommand('dump', ConsoleCommands);
1032 AddCommand('exec', ConsoleCommands);
1033 AddCommand('writeconfig', ConsoleCommands);
1034 AddCommand('alias', ConsoleCommands);
1035 AddCommand('call', ConsoleCommands);
1036 AddCommand('ver', ConsoleCommands);
1037 AddCommand('version', ConsoleCommands);
1039 AddCommand('d_window', DebugCommands);
1040 AddCommand('d_sounds', DebugCommands);
1041 AddCommand('d_frames', DebugCommands);
1042 AddCommand('d_winmsg', DebugCommands);
1043 AddCommand('d_monoff', DebugCommands);
1044 AddCommand('d_botoff', DebugCommands);
1045 AddCommand('d_monster', DebugCommands);
1046 AddCommand('d_health', DebugCommands);
1047 AddCommand('d_player', DebugCommands);
1048 AddCommand('d_joy', DebugCommands);
1049 AddCommand('d_mem', DebugCommands);
1051 AddCommand('p1_name', PlayerSettingsCVars);
1052 AddCommand('p2_name', PlayerSettingsCVars);
1053 AddCommand('p1_color', PlayerSettingsCVars);
1054 AddCommand('p2_color', PlayerSettingsCVars);
1055 AddCommand('p1_model', PlayerSettingsCVars);
1056 AddCommand('p2_model', PlayerSettingsCVars);
1057 AddCommand('p1_team', PlayerSettingsCVars);
1058 AddCommand('p2_team', PlayerSettingsCVars);
1059 AddCommand('p1_autoswitch', PlayerSettingsCVars);
1060 AddCommand('p2_autoswitch', PlayerSettingsCVars);
1061 AddCommand('p1_switch_empty', PlayerSettingsCVars);
1062 AddCommand('p2_switch_empty', PlayerSettingsCVars);
1063 AddCommand('p1_skip_fist', PlayerSettingsCVars);
1064 AddCommand('p2_skip_fist', PlayerSettingsCVars);
1065 AddCommand('p1_priority_kastet', PlayerSettingsCVars);
1066 AddCommand('p2_priority_kastet', PlayerSettingsCVars);
1067 AddCommand('p1_priority_saw', PlayerSettingsCVars);
1068 AddCommand('p2_priority_saw', PlayerSettingsCVars);
1069 AddCommand('p1_priority_pistol', PlayerSettingsCVars);
1070 AddCommand('p2_priority_pistol', PlayerSettingsCVars);
1071 AddCommand('p1_priority_shotgun1', PlayerSettingsCVars);
1072 AddCommand('p2_priority_shotgun1', PlayerSettingsCVars);
1073 AddCommand('p1_priority_shotgun2', PlayerSettingsCVars);
1074 AddCommand('p2_priority_shotgun2', PlayerSettingsCVars);
1075 AddCommand('p1_priority_chaingun', PlayerSettingsCVars);
1076 AddCommand('p2_priority_chaingun', PlayerSettingsCVars);
1077 AddCommand('p1_priority_rocketlauncher', PlayerSettingsCVars);
1078 AddCommand('p2_priority_rocketlauncher', PlayerSettingsCVars);
1079 AddCommand('p1_priority_plasma', PlayerSettingsCVars);
1080 AddCommand('p2_priority_plasma', PlayerSettingsCVars);
1081 AddCommand('p1_priority_bfg', PlayerSettingsCVars);
1082 AddCommand('p2_priority_bfg', PlayerSettingsCVars);
1083 AddCommand('p1_priority_super', PlayerSettingsCVars);
1084 AddCommand('p2_priority_super', PlayerSettingsCVars);
1085 AddCommand('p1_priority_flamethrower', PlayerSettingsCVars);
1086 AddCommand('p2_priority_flamethrower', PlayerSettingsCVars);
1087 AddCommand('p1_priority_berserk', PlayerSettingsCVars);
1088 AddCommand('p2_priority_berserk', PlayerSettingsCVars);
1090 AddCommand('g_max_particles', GameCVars);
1091 AddCommand('g_max_shells', GameCVars);
1092 AddCommand('g_max_gibs', GameCVars);
1093 AddCommand('g_max_corpses', GameCVars);
1094 AddCommand('g_force_model', GameCVars);
1095 AddCommand('g_force_model_name', GameCVars);
1096 AddCommand('g_gamemode', GameCVars);
1097 AddCommand('g_friendlyfire', GameCVars);
1098 AddCommand('g_friendly_hit_trace', GameCVars);
1099 AddCommand('g_friendly_hit_projectile', GameCVars);
1100 AddCommand('g_friendly_absorb_damage', GameCVars);
1101 AddCommand('g_weaponstay', GameCVars);
1102 AddCommand('g_allow_exit', GameCVars);
1103 AddCommand('g_dm_keys', GameCVars);
1104 AddCommand('g_allow_monsters', GameCVars);
1105 AddCommand('g_allow_dropflag', GameCVars);
1106 AddCommand('g_throw_flag', GameCVars);
1107 AddCommand('g_bot_vsmonsters', GameCVars);
1108 AddCommand('g_bot_vsplayers', GameCVars);
1109 AddCommand('g_max_bots', GameCVars); // intentionally not whitelisted
1110 AddCommand('g_scorelimit', GameCVars);
1111 AddCommand('g_timelimit', GameCVars);
1112 AddCommand('g_maxlives', GameCVars);
1113 AddCommand('g_warmup_time', GameCVars);
1114 AddCommand('g_spawn_invul', GameCVars);
1115 AddCommand('g_item_respawn_time', GameCVars);
1116 AddCommand('sv_intertime', GameCVars);
1118 AddCommand('sv_name', NetServerCVars);
1119 AddCommand('sv_passwd', NetServerCVars);
1120 AddCommand('sv_maxplrs', NetServerCVars);
1121 AddCommand('sv_public', NetServerCVars);
1122 AddCommand('sv_port', NetServerCVars);
1124 AddCommand('pause', GameCommands);
1125 AddCommand('endgame', GameCommands);
1126 AddCommand('restart', GameCommands);
1127 AddCommand('addbot', GameCommands);
1128 AddCommand('bot_add', GameCommands);
1129 AddCommand('bot_addlist', GameCommands);
1130 AddCommand('bot_addred', GameCommands);
1131 AddCommand('bot_addblue', GameCommands);
1132 AddCommand('bot_removeall', GameCommands);
1133 AddCommand('chat', GameCommands);
1134 AddCommand('teamchat', GameCommands);
1135 AddCommand('announce', GameCommands);
1136 AddCommand('an', GameCommands);
1137 AddCommand('game', GameCommands);
1138 AddCommand('host', GameCommands);
1139 AddCommand('map', GameCommands);
1140 AddCommand('nextmap', GameCommands);
1141 AddCommand('endmap', GameCommands);
1142 AddCommand('goodbye', GameCommands);
1143 AddCommand('suicide', GameCommands);
1144 AddCommand('spectate', GameCommands);
1145 AddCommand('ready', GameCommands);
1146 AddCommand('kick', GameCommands);
1147 AddCommand('kick_id', GameCommands);
1148 AddCommand('kick_pid', GameCommands);
1149 AddCommand('ban', GameCommands);
1150 AddCommand('ban_id', GameCommands);
1151 AddCommand('ban_pid', GameCommands);
1152 AddCommand('permban', GameCommands);
1153 AddCommand('permban_id', GameCommands);
1154 AddCommand('permban_pid', GameCommands);
1155 AddCommand('permban_ip', GameCommands);
1156 AddCommand('unban', GameCommands);
1157 AddCommand('connect', GameCommands);
1158 AddCommand('disconnect', GameCommands);
1159 AddCommand('reconnect', GameCommands);
1160 AddCommand('say', GameCommands);
1161 AddCommand('tell', GameCommands);
1162 AddCommand('centerprint', GameCommands);
1163 AddCommand('overtime', GameCommands);
1164 AddCommand('rcon_password', GameCommands);
1165 AddCommand('rcon', GameCommands);
1166 AddCommand('callvote', GameCommands);
1167 AddCommand('vote', GameCommands);
1168 AddCommand('clientlist', GameCommands);
1169 AddCommand('event', GameCommands);
1170 AddCommand('screenshot', GameCommands);
1171 AddCommand('weapnext', GameCommands);
1172 AddCommand('weapprev', GameCommands);
1173 AddCommand('weapon', GameCommands);
1174 AddCommand('dropflag', GameCommands);
1175 AddCommand('p1_weapnext', GameCommands);
1176 AddCommand('p1_weapprev', GameCommands);
1177 AddCommand('p1_weapon', GameCommands);
1178 AddCommand('p1_weapbest', GameCommands);
1179 AddCommand('p1_dropflag', GameCommands);
1180 AddCommand('p2_weapnext', GameCommands);
1181 AddCommand('p2_weapprev', GameCommands);
1182 AddCommand('p2_weapon', GameCommands);
1183 AddCommand('p2_weapbest', GameCommands);
1184 AddCommand('p2_dropflag', GameCommands);
1186 AddCommand('god', GameCheats);
1187 AddCommand('notarget', GameCheats);
1188 AddCommand('give', GameCheats); // "exit" too ;-)
1189 AddCommand('open', GameCheats);
1190 AddCommand('fly', GameCheats);
1191 AddCommand('noclip', GameCheats);
1192 AddCommand('speedy', GameCheats);
1193 AddCommand('jumpy', GameCheats);
1194 AddCommand('noreload', GameCheats);
1195 AddCommand('aimline', GameCheats);
1196 AddCommand('automap', GameCheats);
1198 AddAction('jump', ACTION_JUMP);
1199 AddAction('moveleft', ACTION_MOVELEFT);
1200 AddAction('moveright', ACTION_MOVERIGHT);
1201 AddAction('lookup', ACTION_LOOKUP);
1202 AddAction('lookdown', ACTION_LOOKDOWN);
1203 AddAction('attack', ACTION_ATTACK);
1204 AddAction('scores', ACTION_SCORES);
1205 AddAction('activate', ACTION_ACTIVATE);
1206 AddAction('strafe', ACTION_STRAFE);
1208 WhitelistCommand('say');
1209 WhitelistCommand('tell');
1210 WhitelistCommand('overtime');
1211 WhitelistCommand('ready');
1212 WhitelistCommand('map');
1213 WhitelistCommand('nextmap');
1214 WhitelistCommand('endmap');
1215 WhitelistCommand('restart');
1216 WhitelistCommand('kick');
1217 WhitelistCommand('kick_pid');
1218 WhitelistCommand('ban');
1219 WhitelistCommand('ban_pid');
1220 WhitelistCommand('centerprint');
1222 WhitelistCommand('addbot');
1223 WhitelistCommand('bot_add');
1224 WhitelistCommand('bot_addred');
1225 WhitelistCommand('bot_addblue');
1226 WhitelistCommand('bot_removeall');
1228 WhitelistCommand('g_gamemode');
1229 WhitelistCommand('g_friendlyfire');
1230 WhitelistCommand('g_friendly_hit_trace');
1231 WhitelistCommand('g_friendly_hit_projectile');
1232 WhitelistCommand('g_friendly_absorb_damage');
1233 WhitelistCommand('g_weaponstay');
1234 WhitelistCommand('g_allow_exit');
1235 WhitelistCommand('g_dm_keys');
1236 WhitelistCommand('g_allow_monsters');
1237 WhitelistCommand('g_bot_vsmonsters');
1238 WhitelistCommand('g_bot_vsplayers');
1239 WhitelistCommand('g_scorelimit');
1240 WhitelistCommand('g_timelimit');
1241 WhitelistCommand('g_maxlives');
1242 WhitelistCommand('g_warmup_time');
1243 WhitelistCommand('g_spawn_invul');
1244 WhitelistCommand('g_item_respawn_time');
1246 g_Console_ResetBinds;
1247 g_Console_ReadConfig(gConfigScript);
1248 // g_Console_ReadConfig(autoexecScript);
1249 g_Console_ReadConfig('autoexec.cfg');
1251 gParsingBinds := False;
1252 end;
1254 procedure g_Console_Finalize;
1255 begin
1257 end;
1259 procedure g_Console_Init;
1260 begin
1261 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
1262 g_Console_Add('');
1263 {$IFDEF HEADLESS}
1264 if ConsoleStdIn then
1265 begin
1266 InitKeyboard();
1267 conbufStdOutRawMode := true;
1268 end;
1269 {$ENDIF}
1270 end;
1272 procedure g_Console_Update;
1273 var
1274 a, b: Integer;
1275 begin
1276 {$IFDEF HEADLESS}
1277 if ConsoleStdIn then
1278 ReadStdIn();
1279 {$ENDIF}
1280 InputReady := gConsoleShow or gChatShow;
1282 a := 0;
1283 while a <= High(MsgArray) do
1284 begin
1285 if MsgArray[a].Time > 0 then
1286 begin
1287 if MsgArray[a].Time = 1 then
1288 begin
1289 if a < High(MsgArray) then
1290 begin
1291 for b := a to High(MsgArray)-1 do
1292 MsgArray[b] := MsgArray[b+1];
1294 MsgArray[High(MsgArray)].Time := 0;
1296 a := a - 1;
1297 end;
1298 end
1299 else
1300 Dec(MsgArray[a].Time);
1301 end;
1303 a := a + 1;
1304 end;
1305 end;
1307 procedure g_Console_Char(C: AnsiChar);
1308 begin
1309 if InputReady and (gConsoleShow or gChatShow) then
1310 begin
1311 Insert(C, Line, CPos);
1312 CPos := CPos + 1;
1313 end
1314 end;
1317 var
1318 tcomplist: array of AnsiString = nil;
1319 tcompidx: array of Integer = nil;
1321 procedure Complete ();
1322 var
1323 i, c: Integer;
1324 tused: Integer;
1325 ll, lpfx, cmd: AnsiString;
1326 begin
1327 if (Length(Line) = 0) then
1328 begin
1329 g_Console_Add('');
1330 for i := 0 to High(commands) do
1331 begin
1332 // hidden commands are hidden when cheats aren't enabled
1333 if commands[i].hidden and not conIsCheatsEnabled then continue;
1334 if (Length(commands[i].help) > 0) then
1335 begin
1336 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1337 end
1338 else
1339 begin
1340 g_Console_Add(' '+commands[i].cmd);
1341 end;
1342 end;
1343 exit;
1344 end;
1346 ll := LowerCase(Line);
1347 lpfx := '';
1349 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
1350 begin
1351 ll := Copy(ll, 0, Length(ll)-1);
1352 for i := 0 to High(commands) do
1353 begin
1354 // hidden commands are hidden when cheats aren't enabled
1355 if commands[i].hidden and not conIsCheatsEnabled then continue;
1356 if (commands[i].cmd = ll) then
1357 begin
1358 if (Length(commands[i].help) > 0) then
1359 begin
1360 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1361 end;
1362 end;
1363 end;
1364 exit;
1365 end;
1367 // build completion list
1368 tused := 0;
1369 for i := 0 to High(commands) do
1370 begin
1371 // hidden commands are hidden when cheats aren't enabled
1372 if commands[i].hidden and not conIsCheatsEnabled then continue;
1373 cmd := commands[i].cmd;
1374 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
1375 begin
1376 if (tused = Length(tcomplist)) then
1377 begin
1378 SetLength(tcomplist, Length(tcomplist)+128);
1379 SetLength(tcompidx, Length(tcompidx)+128);
1380 end;
1381 tcomplist[tused] := cmd;
1382 tcompidx[tused] := i;
1383 Inc(tused);
1384 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
1385 end;
1386 end;
1388 // get longest prefix
1389 for i := 0 to tused-1 do
1390 begin
1391 cmd := tcomplist[i];
1392 for c := 1 to Length(lpfx) do
1393 begin
1394 if (c > Length(cmd)) then break;
1395 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
1396 end;
1397 end;
1399 if (tused = 0) then exit;
1401 if (tused = 1) then
1402 begin
1403 Line := tcomplist[0]+' ';
1404 CPos := Length(Line)+1;
1405 end
1406 else
1407 begin
1408 // has longest prefix?
1409 if (Length(lpfx) > Length(ll)) then
1410 begin
1411 Line := lpfx;
1412 CPos:= Length(Line)+1;
1413 end
1414 else
1415 begin
1416 g_Console_Add('');
1417 for i := 0 to tused-1 do
1418 begin
1419 if (Length(commands[tcompidx[i]].help) > 0) then
1420 begin
1421 g_Console_Add(' '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
1422 end
1423 else
1424 begin
1425 g_Console_Add(' '+tcomplist[i]);
1426 end;
1427 end;
1428 end;
1429 end;
1430 end;
1433 procedure g_Console_Control(K: Word);
1434 begin
1435 case K of
1436 IK_BACKSPACE:
1437 if (Length(Line) > 0) and (CPos > 1) then
1438 begin
1439 Delete(Line, CPos-1, 1);
1440 CPos := CPos-1;
1441 end;
1442 IK_DELETE:
1443 if (Length(Line) > 0) and (CPos <= Length(Line)) then
1444 Delete(Line, CPos, 1);
1445 IK_LEFT, IK_KPLEFT, VK_LEFT, JOY0_LEFT, JOY1_LEFT, JOY2_LEFT, JOY3_LEFT:
1446 if CPos > 1 then
1447 CPos := CPos - 1;
1448 IK_RIGHT, IK_KPRIGHT, VK_RIGHT, JOY0_RIGHT, JOY1_RIGHT, JOY2_RIGHT, JOY3_RIGHT:
1449 if CPos <= Length(Line) then
1450 CPos := CPos + 1;
1451 IK_RETURN, IK_KPRETURN, VK_OPEN, VK_FIRE, JOY0_ATTACK, JOY1_ATTACK, JOY2_ATTACK, JOY3_ATTACK:
1452 begin
1453 if gConsoleShow then
1454 g_Console_Process(Line)
1455 else
1456 if gChatShow then
1457 begin
1458 if (Length(Line) > 0) and g_Game_IsNet then
1459 begin
1460 if gChatTeam then
1461 begin
1462 if g_Game_IsClient then
1463 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
1464 else
1465 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1466 NET_CHAT_TEAM, gPlayer1Settings.Team);
1467 end
1468 else
1469 begin
1470 if g_Game_IsClient then
1471 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
1472 else
1473 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1474 NET_CHAT_PLAYER);
1475 end;
1476 end;
1478 Line := '';
1479 CPos := 1;
1480 gJustChatted := True;
1481 g_Console_Chat_Switch;
1482 InputReady := False;
1483 end;
1484 end;
1485 IK_TAB:
1486 if not gChatShow then
1487 Complete();
1488 IK_DOWN, IK_KPDOWN, VK_DOWN, JOY0_DOWN, JOY1_DOWN, JOY2_DOWN, JOY3_DOWN:
1489 if not gChatShow then
1490 if (CommandHistory <> nil) and
1491 (CmdIndex < Length(CommandHistory)) then
1492 begin
1493 if CmdIndex < Length(CommandHistory)-1 then
1494 CmdIndex := CmdIndex + 1;
1495 Line := CommandHistory[CmdIndex];
1496 CPos := Length(Line) + 1;
1497 end;
1498 IK_UP, IK_KPUP, VK_UP, JOY0_UP, JOY1_UP, JOY2_UP, JOY3_UP:
1499 if not gChatShow then
1500 if (CommandHistory <> nil) and
1501 (CmdIndex <= Length(CommandHistory)) then
1502 begin
1503 if CmdIndex > 0 then
1504 CmdIndex := CmdIndex - 1;
1505 Line := CommandHistory[CmdIndex];
1506 Cpos := Length(Line) + 1;
1507 end;
1508 IK_PAGEUP, IK_KPPAGEUP, VK_PREV, JOY0_PREV, JOY1_PREV, JOY2_PREV, JOY3_PREV: // PgUp
1509 if not gChatShow then Inc(conSkipLines);
1510 IK_PAGEDN, IK_KPPAGEDN, VK_NEXT, JOY0_NEXT, JOY1_NEXT, JOY2_NEXT, JOY3_NEXT: // PgDown
1511 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1512 IK_HOME, IK_KPHOME:
1513 CPos := 1;
1514 IK_END, IK_KPEND:
1515 CPos := Length(Line) + 1;
1516 IK_A..IK_Z, IK_SPACE, IK_SHIFT, IK_RSHIFT, IK_CAPSLOCK, IK_LBRACKET, IK_RBRACKET,
1517 IK_SEMICOLON, IK_QUOTE, IK_BACKSLASH, IK_SLASH, IK_COMMA, IK_DOT, (*IK_EQUALS,*)
1518 IK_0, IK_1, IK_2, IK_3, IK_4, IK_5, IK_6, IK_7, IK_8, IK_9, IK_MINUS, IK_EQUALS:
1519 (* see TEXTINPUT event *)
1520 end
1521 end;
1523 function GetStr(var Str: AnsiString): AnsiString;
1524 var
1525 a, b: Integer;
1526 begin
1527 Result := '';
1528 if Str[1] = '"' then
1529 begin
1530 for b := 1 to Length(Str) do
1531 if (b = Length(Str)) or (Str[b+1] = '"') then
1532 begin
1533 Result := Copy(Str, 2, b-1);
1534 Delete(Str, 1, b+1);
1535 Str := Trim(Str);
1536 Exit;
1537 end;
1538 end;
1540 for a := 1 to Length(Str) do
1541 if (a = Length(Str)) or (Str[a+1] = ' ') then
1542 begin
1543 Result := Copy(Str, 1, a);
1544 Delete(Str, 1, a+1);
1545 Str := Trim(Str);
1546 Exit;
1547 end;
1548 end;
1550 function ParseString(Str: AnsiString): SSArray;
1551 begin
1552 Result := nil;
1554 Str := Trim(Str);
1556 if Str = '' then
1557 Exit;
1559 while Str <> '' do
1560 begin
1561 SetLength(Result, Length(Result)+1);
1562 Result[High(Result)] := GetStr(Str);
1563 end;
1564 end;
1566 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
1568 procedure conmsg (s: AnsiString);
1569 var
1570 a: Integer;
1571 begin
1572 if length(s) = 0 then exit;
1573 for a := 0 to High(MsgArray) do
1574 begin
1575 with MsgArray[a] do
1576 begin
1577 if Time = 0 then
1578 begin
1579 Msg := s;
1580 Time := MsgTime;
1581 exit;
1582 end;
1583 end;
1584 end;
1585 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1586 with MsgArray[High(MsgArray)] do
1587 begin
1588 Msg := L;
1589 Time := MsgTime;
1590 end;
1591 end;
1593 var
1594 f: Integer;
1595 begin
1596 // put it to console
1597 cbufPut(L);
1598 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1600 // now show 'em out of console too
1601 show := show and gAllowConsoleMessages;
1602 if show and gShowMessages then
1603 begin
1604 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1605 while length(L) > 0 do
1606 begin
1607 f := Pos(#10, L);
1608 if f <= 0 then f := length(L)+1;
1609 conmsg(Copy(L, 1, f-1));
1610 Delete(L, 1, f);
1611 end;
1612 end;
1614 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1615 //ConsoleHistory[High(ConsoleHistory)] := L;
1617 (*
1618 {$IFDEF HEADLESS}
1619 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1620 {$ENDIF}
1621 *)
1622 end;
1625 var
1626 consolewriterLastWasEOL: Boolean = false;
1628 procedure consolewriter (constref buf; len: SizeUInt);
1629 var
1630 b: PByte;
1631 begin
1632 if (len < 1) then exit;
1633 b := PByte(@buf);
1634 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1635 while (len > 0) do
1636 begin
1637 if (b[0] <> 13) and (b[0] <> 10) then
1638 begin
1639 cbufPut(AnsiChar(b[0]));
1640 end
1641 else
1642 begin
1643 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1644 cbufPut(#10);
1645 end;
1646 len -= 1;
1647 b += 1;
1648 end;
1649 end;
1652 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1653 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1654 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1655 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1656 begin
1657 g_Console_Add(s, show);
1658 end;
1661 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1662 begin
1663 if show then
1664 begin
1665 g_Console_Add(formatstrf(s, args), true);
1666 end
1667 else
1668 begin
1669 consolewriterLastWasEOL := false;
1670 formatstrf(s, args, consolewriter);
1671 if not consolewriterLastWasEOL then cbufPut(#10);
1672 end;
1673 end;
1676 procedure g_Console_Clear();
1677 begin
1678 //ConsoleHistory := nil;
1679 cbufClear();
1680 conSkipLines := 0;
1681 end;
1683 procedure AddToHistory(L: AnsiString);
1684 var
1685 len: Integer;
1686 begin
1687 len := Length(CommandHistory);
1689 if (len = 0) or
1690 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1691 begin
1692 SetLength(CommandHistory, len+1);
1693 CommandHistory[len] := L;
1694 end;
1696 CmdIndex := Length(CommandHistory);
1697 end;
1699 function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
1700 var
1701 Arr: SSArray;
1702 i: Integer;
1703 begin
1704 Result := True;
1706 Arr := nil;
1708 if Trim(C) = '' then
1709 Exit;
1711 Arr := ParseString(C);
1712 if Arr = nil then
1713 Exit;
1715 for i := 0 to High(Whitelist) do
1716 if Whitelist[i] = LowerCase(Arr[0]) then
1717 Result := False;
1718 end;
1720 procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
1721 var
1722 Arr: SSArray;
1723 i: Integer;
1724 begin
1725 Arr := nil;
1727 if Trim(L) = '' then
1728 Exit;
1730 conSkipLines := 0; // "unscroll"
1732 if L = 'goobers' then
1733 begin
1734 Line := '';
1735 CPos := 1;
1736 gCheats := true;
1737 g_Console_Add('Your memory serves you well.');
1738 exit;
1739 end;
1741 if not quiet then
1742 begin
1743 g_Console_Add('> '+L);
1744 Line := '';
1745 CPos := 1;
1746 end;
1748 Arr := ParseString(L);
1749 if Arr = nil then
1750 Exit;
1752 if commands = nil then
1753 Exit;
1755 if not quiet then
1756 AddToHistory(L);
1758 for i := 0 to High(commands) do
1759 begin
1760 if commands[i].cmd = LowerCase(Arr[0]) then
1761 begin
1762 if commands[i].action >= 0 then
1763 begin
1764 gPlayerAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+';
1765 exit
1766 end;
1767 if assigned(commands[i].procEx) then
1768 begin
1769 commands[i].procEx(@commands[i], Arr);
1770 exit
1771 end;
1772 if assigned(commands[i].proc) then
1773 begin
1774 commands[i].proc(Arr);
1775 exit
1776 end
1777 end
1778 end;
1780 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1781 end;
1784 function g_Console_Interactive: Boolean;
1785 begin
1786 Result := gConsoleShow
1787 end;
1789 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = ''; rep: Boolean = False);
1790 begin
1791 // e_LogWritefln('bind "%s" "%s" "%s" <%s>', [LowerCase(e_KeyNames[key]), down, up, key]);
1792 ASSERT(key >= 0);
1793 ASSERT(key < e_MaxInputKeys);
1794 if key > 0 then
1795 begin
1796 gInputBinds[key].rep := rep;
1797 gInputBinds[key].down := ParseAlias(down);
1798 gInputBinds[key].up := ParseAlias(up);
1799 end;
1800 g_Console_WriteGameConfig();
1801 end;
1803 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
1805 function EqualsCommandLists (a, b: SSArray): Boolean;
1806 var i, len: Integer;
1807 begin
1808 result := False;
1809 len := Length(a);
1810 if len = Length(b) then
1811 begin
1812 i := 0;
1813 while (i < len) and (a[i] = b[i]) do inc(i);
1814 if i >= len then
1815 result := True
1816 end
1817 end;
1819 begin
1820 ASSERT(key >= 0);
1821 ASSERT(key < e_MaxInputKeys);
1822 result := EqualsCommandLists(ParseAlias(down), gInputBinds[key].down) and EqualsCommandLists(ParseAlias(up), gInputBinds[key].up)
1823 end;
1825 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
1826 var i: Integer;
1827 begin
1828 ASSERT(n >= 1);
1829 result := 0;
1830 if commands = nil then Exit;
1831 i := 0;
1832 while (n >= 1) and (i < e_MaxInputKeys) do
1833 begin
1834 if (i < VK_FIRSTKEY) or (i > VK_LASTKEY) then (* never show virtual keys in gui *)
1835 begin
1836 if g_Console_MatchBind(i, down, up) then
1837 begin
1838 result := i;
1839 dec(n)
1840 end;
1841 end;
1842 inc(i)
1843 end;
1844 if n >= 1 then
1845 result := 0
1846 end;
1848 function g_Console_Action (action: Integer): Boolean;
1849 var i, len: Integer;
1850 begin
1851 ASSERT(action >= FIRST_ACTION);
1852 ASSERT(action <= LAST_ACTION);
1853 i := 0;
1854 len := Length(gPlayerAction);
1855 while (i < len) and (not gPlayerAction[i, action]) do inc(i);
1856 Result := i < len
1857 end;
1859 function BindsAllowed (key: Integer): Boolean;
1860 var grab, active: Boolean;
1861 begin
1862 Result := False;
1863 {$IFDEF DISABLE_MENU}
1864 grab := False;
1865 active := False;
1866 {$ELSE}
1867 grab := g_GUIGrabInput;
1868 active := g_ActiveWindow <> nil;
1869 {$ENDIF}
1870 if (not grab) and (key >= 0) and (key < e_MaxInputKeys) and ((gInputBinds[key].down <> nil) or (gInputBinds[key].up <> nil)) then
1871 begin
1872 if gChatShow then
1873 Result := g_Console_MatchBind(key, 'togglemenu') or
1874 g_Console_MatchBind(key, 'showkeyboard') or
1875 g_Console_MatchBind(key, 'hidekeyboard')
1876 else if gConsoleShow or active or (gGameSettings.GameType = GT_NONE) then
1877 Result := g_Console_MatchBind(key, 'togglemenu') or
1878 g_Console_MatchBind(key, 'toggleconsole') or
1879 g_Console_MatchBind(key, 'showkeyboard') or
1880 g_Console_MatchBind(key, 'hidekeyboard')
1881 else (* in game *)
1882 Result := True
1883 end
1884 end;
1886 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
1887 var i: Integer;
1888 begin
1889 if BindsAllowed(key) then
1890 begin
1891 if down then
1892 for i := 0 to High(gInputBinds[key].down) do
1893 g_Console_Process(gInputBinds[key].down[i], True)
1894 else
1895 for i := 0 to High(gInputBinds[key].up) do
1896 g_Console_Process(gInputBinds[key].up[i], True)
1897 end;
1898 if down and not menu_toggled then
1899 KeyPress(key);
1900 menu_toggled := False
1901 end;
1903 procedure g_Console_ProcessBindRepeat (key: Integer);
1904 var i: Integer; active: Boolean;
1905 begin
1906 {$IFDEF DISABLE_MENU}
1907 active := False;
1908 {$ELSE}
1909 active := g_ActiveWindow <> nil;
1910 {$ENDIF}
1911 if gConsoleShow or gChatShow or active then
1912 begin
1913 KeyPress(key); // key repeat in menus and shit
1914 Exit;
1915 end;
1916 if BindsAllowed(key) and gInputBinds[key].rep then
1917 begin
1918 for i := 0 to High(gInputBinds[key].down) do
1919 g_Console_Process(gInputBinds[key].down[i], True);
1920 end;
1921 end;
1923 procedure g_Console_ResetBinds;
1924 var i: Integer;
1925 begin
1926 for i := 0 to e_MaxInputKeys - 1 do
1927 g_Console_BindKey(i, '', '');
1929 g_Console_BindKey(IK_GRAVE, 'toggleconsole');
1930 g_Console_BindKey(IK_ESCAPE, 'togglemenu');
1931 g_Console_BindKey(IK_A, '+p1_moveleft', '-p1_moveleft');
1932 g_Console_BindKey(IK_D, '+p1_moveright', '-p1_moveright');
1933 g_Console_BindKey(IK_W, '+p1_lookup', '-p1_lookup');
1934 g_Console_BindKey(IK_S, '+p1_lookdown', '-p1_lookdown');
1935 g_Console_BindKey(IK_SPACE, '+p1_jump', '-p1_jump');
1936 g_Console_BindKey(IK_H, '+p1_attack', '-p1_attack');
1937 g_Console_BindKey(IK_J, '+p1_activate', '-p1_activate');
1938 g_Console_BindKey(IK_ALT, '+p1_strafe', '-p1_strafe');
1939 g_Console_BindKey(IK_E, 'p1_weapnext', '', True);
1940 g_Console_BindKey(IK_Q, 'p1_weapprev', '', True);
1941 g_Console_BindKey(IK_R, 'p1_dropflag', '');
1942 g_Console_BindKey(IK_1, 'p1_weapon 1');
1943 g_Console_BindKey(IK_2, 'p1_weapon 2');
1944 g_Console_BindKey(IK_3, 'p1_weapon 3');
1945 g_Console_BindKey(IK_4, 'p1_weapon 4');
1946 g_Console_BindKey(IK_5, 'p1_weapon 5');
1947 g_Console_BindKey(IK_6, 'p1_weapon 6');
1948 g_Console_BindKey(IK_7, 'p1_weapon 7');
1949 g_Console_BindKey(IK_8, 'p1_weapon 8');
1950 g_Console_BindKey(IK_9, 'p1_weapon 9');
1951 g_Console_BindKey(IK_0, 'p1_weapon 10');
1952 g_Console_BindKey(IK_MINUS, 'p1_weapon 11');
1953 g_Console_BindKey(IK_T, 'togglechat');
1954 g_Console_BindKey(IK_Y, 'toggleteamchat');
1955 g_Console_BindKey(IK_F11, 'screenshot');
1956 g_Console_BindKey(IK_TAB, '+scores', '-scores');
1957 g_Console_BindKey(IK_PAUSE, 'pause');
1958 g_Console_BindKey(IK_F1, 'vote');
1960 (* for i := 0 to e_MaxJoys - 1 do *)
1961 for i := 0 to 1 do
1962 begin
1963 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_LEFT), '+p' + IntToStr(i mod 2 + 1) + '_moveleft', '-p' + IntToStr(i mod 2 + 1) + '_moveleft');
1964 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_RIGHT), '+p' + IntToStr(i mod 2 + 1) + '_moveright', '-p' + IntToStr(i mod 2 + 1) + '_moveright');
1965 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_UP), '+p' + IntToStr(i mod 2 + 1) + '_lookup', '-p' + IntToStr(i mod 2 + 1) + '_lookup');
1966 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_DOWN), '+p' + IntToStr(i mod 2 + 1) + '_lookdown', '-p' + IntToStr(i mod 2 + 1) + '_lookdown');
1967 g_Console_BindKey(e_JoyButtonToKey(i, 2), '+p' + IntToStr(i mod 2 + 1) + '_jump', '-p' + IntToStr(i mod 2 + 1) + '_jump');
1968 g_Console_BindKey(e_JoyButtonToKey(i, 0), '+p' + IntToStr(i mod 2 + 1) + '_attack', '-p' + IntToStr(i mod 2 + 1) + '_attack');
1969 g_Console_BindKey(e_JoyButtonToKey(i, 3), '+p' + IntToStr(i mod 2 + 1) + '_activate', '-p' + IntToStr(i mod 2 + 1) + '_activate');
1970 g_Console_BindKey(e_JoyButtonToKey(i, 7), '+p' + IntToStr(i mod 2 + 1) + '_strafe', '-p' + IntToStr(i mod 2 + 1) + '_strafe');
1971 g_Console_BindKey(e_JoyButtonToKey(i, 1), 'p' + IntToStr(i mod 2 + 1) + '_weapnext', '', True);
1972 g_Console_BindKey(e_JoyButtonToKey(i, 4), 'p' + IntToStr(i mod 2 + 1) + '_weapprev', '', True);
1973 g_Console_BindKey(e_JoyButtonToKey(i, 10), 'togglemenu');
1974 end;
1976 g_Console_BindKey(VK_ESCAPE, 'togglemenu');
1977 g_Console_BindKey(VK_LSTRAFE, '+moveleft; +strafe', '-moveleft; -strafe');
1978 g_Console_BindKey(VK_RSTRAFE, '+moveright; +strafe', '-moveright; -strafe');
1979 g_Console_BindKey(VK_LEFT, '+moveleft', '-moveleft');
1980 g_Console_BindKey(VK_RIGHT, '+moveright', '-moveright');
1981 g_Console_BindKey(VK_UP, '+lookup', '-lookup');
1982 g_Console_BindKey(VK_DOWN, '+lookdown', '-lookdown');
1983 g_Console_BindKey(VK_JUMP, '+jump', '-jump');
1984 g_Console_BindKey(VK_FIRE, '+attack', '-attack');
1985 g_Console_BindKey(VK_OPEN, '+activate', '-activate');
1986 g_Console_BindKey(VK_STRAFE, '+strafe', '-strafe');
1987 g_Console_BindKey(VK_NEXT, 'weapnext', '', True);
1988 g_Console_BindKey(VK_PREV, 'weapprev', '', True);
1989 g_Console_BindKey(VK_0, 'weapon 1');
1990 g_Console_BindKey(VK_1, 'weapon 2');
1991 g_Console_BindKey(VK_2, 'weapon 3');
1992 g_Console_BindKey(VK_3, 'weapon 4');
1993 g_Console_BindKey(VK_4, 'weapon 5');
1994 g_Console_BindKey(VK_5, 'weapon 6');
1995 g_Console_BindKey(VK_6, 'weapon 7');
1996 g_Console_BindKey(VK_7, 'weapon 8');
1997 g_Console_BindKey(VK_8, 'weapon 9');
1998 g_Console_BindKey(VK_9, 'weapon 10');
1999 g_Console_BindKey(VK_A, 'weapon 11');
2000 g_Console_BindKey(VK_CHAT, 'togglechat');
2001 g_Console_BindKey(VK_TEAM, 'toggleteamchat');
2002 g_Console_BindKey(VK_CONSOLE, 'toggleconsole');
2003 g_Console_BindKey(VK_PRINTSCR, 'screenshot');
2004 g_Console_BindKey(VK_STATUS, '+scores', '-scores');
2005 g_Console_BindKey(VK_SHOWKBD, 'showkeyboard');
2006 g_Console_BindKey(VK_HIDEKBD, 'hidekeyboard');
2007 end;
2009 procedure g_Console_ReadConfig (filename: String);
2010 var f: TextFile; s: AnsiString; i, len: Integer;
2011 begin
2012 e_LogWritefln('g_Console_ReadConfig (1) "%s"', [filename]);
2013 if e_FindResource(ConfigDirs, filename, false) = true then
2014 begin
2015 e_LogWritefln('g_Console_ReadConfig (2) "%s"', [filename]);
2016 AssignFile(f, filename);
2017 Reset(f);
2018 while not EOF(f) do
2019 begin
2020 ReadLn(f, s);
2021 len := Length(s);
2022 if len > 0 then
2023 begin
2024 i := 1;
2025 (* skip spaces *)
2026 while (i <= len) and (s[i] <= ' ') do inc(i);
2027 (* skip comments *)
2028 if (i <= len) and ((s[i] <> '#') and ((i + 1 > len) or (s[i] <> '/') or (s[i + 1] <> '/'))) then
2029 g_Console_Process(s, True);
2030 end
2031 end;
2032 CloseFile(f);
2033 end
2034 end;
2036 procedure g_Console_WriteConfig (filename: String);
2037 var f: TextFile; i, j: Integer;
2039 procedure WriteFlag(name: string; flag: LongWord);
2040 begin
2041 WriteLn(f, name, IfThen(LongBool(gsGameFlags and flag), 1, 0));
2042 end;
2044 function FormatTeam(team: Byte): string;
2045 begin
2046 if team = TEAM_BLUE then
2047 result := 'blue'
2048 else
2049 result := 'red';
2050 end;
2052 begin
2053 // e_LogWritefln('g_Console_WriteConfig: %s', [filename]);
2054 AssignFile(f, filename);
2055 Rewrite(f);
2056 WriteLn(f, '// ' + configComment);
2058 // binds
2059 WriteLn(f, 'unbindall');
2060 for i := 0 to e_MaxInputKeys - 1 do
2061 if (Length(gInputBinds[i].down) > 0) or (Length(gInputBinds[i].up) > 0) then
2062 begin
2063 Write(f, 'bind ', e_KeyNames[i], ' ', QuoteStr(GetCommandString(gInputBinds[i].down)));
2064 if Length(gInputBinds[i].down) = 0 then
2065 Write(f, '""');
2066 if Length(gInputBinds[i].up) > 0 then
2067 Write(f, ' ', QuoteStr(GetCommandString(gInputBinds[i].up)));
2068 WriteLn(f, '');
2069 if gInputBinds[i].rep then
2070 WriteLn(f, 'bindrep ', e_KeyNames[i]);
2071 end;
2073 // lang
2074 if gAskLanguage then
2075 WriteLn(f, 'g_language ask')
2076 else
2077 WriteLn(f, 'g_language ', gLanguage);
2079 // net server
2080 WriteLn(f, 'sv_name ', QuoteStr(NetServerName));
2081 WriteLn(f, 'sv_passwd ', QuoteStr(NetPassword));
2082 WriteLn(f, 'sv_maxplrs ', NetMaxClients);
2083 WriteLn(f, 'sv_port ', NetPort);
2084 WriteLn(f, 'sv_public ', IfThen(NetUseMaster, 1, 0));
2086 // game settings
2087 WriteLn(f, 'g_max_particles ', g_GFX_GetMax());
2088 WriteLn(f, 'g_max_shells ', g_Shells_GetMax());
2089 WriteLn(f, 'g_max_gibs ', g_Gibs_GetMax());
2090 WriteLn(f, 'g_max_corpses ', g_Corpses_GetMax());
2091 WriteLn(f, 'g_force_model ', g_Force_Model_Get());
2092 WriteLn(f, 'g_force_model_name ', g_Forced_Model_GetName());
2093 WriteLn(f, 'sv_intertime ', gDefInterTime);
2095 // gameplay settings
2096 WriteLn(f, 'g_gamemode ', gsGameMode);
2097 WriteLn(f, 'g_scorelimit ', gsScoreLimit);
2098 WriteLn(f, 'g_timelimit ', gsTimeLimit);
2099 WriteLn(f, 'g_maxlives ', gsMaxLives);
2100 WriteLn(f, 'g_item_respawn_time ', gsItemRespawnTime);
2101 WriteLn(f, 'g_spawn_invul ', gsSpawnInvul);
2102 WriteLn(f, 'g_warmup_time ', gsWarmupTime);
2104 WriteFlag('g_friendlyfire ', GAME_OPTION_TEAMDAMAGE);
2105 WriteFlag('g_friendly_hit_trace ', GAME_OPTION_TEAMHITTRACE);
2106 WriteFlag('g_friendly_hit_projectile ', GAME_OPTION_TEAMHITPROJECTILE);
2107 WriteFlag('g_allow_exit ', GAME_OPTION_ALLOWEXIT);
2108 WriteFlag('g_allow_monsters ', GAME_OPTION_MONSTERS);
2109 WriteFlag('g_allow_dropflag ', GAME_OPTION_ALLOWDROPFLAG);
2110 WriteFlag('g_throw_flag ', GAME_OPTION_THROWFLAG);
2111 WriteFlag('g_dm_keys ', GAME_OPTION_DMKEYS);
2112 WriteFlag('g_weaponstay ', GAME_OPTION_WEAPONSTAY);
2113 WriteFlag('g_bot_vsmonsters ', GAME_OPTION_BOTVSMONSTER);
2114 WriteFlag('g_bot_vsplayers ', GAME_OPTION_BOTVSPLAYER);
2116 // players
2117 with gPlayer1Settings do
2118 begin
2119 WriteLn(f, 'p1_name ', QuoteStr(Name));
2120 WriteLn(f, 'p1_color ', Color.R, ' ', Color.G, ' ', Color.B);
2121 WriteLn(f, 'p1_model ', QuoteStr(Model));
2122 WriteLn(f, 'p1_team ', FormatTeam(Team));
2123 WriteLn(f, 'p1_autoswitch ', WeaponSwitch);
2124 WriteLn(f, 'p1_switch_empty ', SwitchToEmpty);
2125 WriteLn(f, 'p1_priority_kastet ', Max(0, WeaponPreferences[WEAPON_KASTET]));
2126 WriteLn(f, 'p1_priority_saw ', Max(0, WeaponPreferences[WEAPON_SAW]));
2127 WriteLn(f, 'p1_priority_pistol ', Max(0, WeaponPreferences[WEAPON_PISTOL]));
2128 WriteLn(f, 'p1_priority_shotgun1 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN1]));
2129 WriteLn(f, 'p1_priority_shotgun2 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN2] ));
2130 WriteLn(f, 'p1_priority_chaingun ', Max(0, WeaponPreferences[WEAPON_CHAINGUN]));
2131 WriteLn(f, 'p1_priority_rocketlauncher ', Max(0, WeaponPreferences[WEAPON_ROCKETLAUNCHER]));
2132 WriteLn(f, 'p1_priority_plasma ', Max(0, WeaponPreferences[WEAPON_PLASMA]));
2133 WriteLn(f, 'p1_priority_bfg ', Max(0, WeaponPreferences[WEAPON_BFG]));
2134 WriteLn(f, 'p1_priority_super ', Max(0, WeaponPreferences[WEAPON_SUPERPULEMET]));
2135 WriteLn(f, 'p1_priority_flamethrower ', Max(0, WeaponPreferences[WEAPON_FLAMETHROWER]));
2136 WriteLn(f, 'p1_priority_berserk ', Max(0, WeaponPreferences[WP_LAST+1]));
2137 //
2138 end;
2139 with gPlayer2Settings do
2140 begin
2141 WriteLn(f, 'p2_name ', QuoteStr(Name));
2142 WriteLn(f, 'p2_color ', Color.R, ' ', Color.G, ' ', Color.B);
2143 WriteLn(f, 'p2_model ', QuoteStr(Model));
2144 WriteLn(f, 'p2_team ', FormatTeam(Team));
2145 WriteLn(f, 'p2_autoswitch ', WeaponSwitch);
2146 WriteLn(f, 'p2_switch_empty ', SwitchToEmpty);
2147 WriteLn(f, 'p2_priority_kastet ', Max(0, WeaponPreferences[WEAPON_KASTET]));
2148 WriteLn(f, 'p2_priority_saw ', Max(0, WeaponPreferences[WEAPON_SAW]));
2149 WriteLn(f, 'p2_priority_pistol ', Max(0, WeaponPreferences[WEAPON_PISTOL]));
2150 WriteLn(f, 'p2_priority_shotgun1 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN1]));
2151 WriteLn(f, 'p2_priority_shotgun2 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN1]));
2152 WriteLn(f, 'p2_priority_chaingun ', Max(0, WeaponPreferences[WEAPON_CHAINGUN]));
2153 WriteLn(f, 'p2_priority_rocketlauncher ', Max(0, WeaponPreferences[WEAPON_ROCKETLAUNCHER]));
2154 WriteLn(f, 'p2_priority_plasma ', Max(0, WeaponPreferences[WEAPON_PLASMA]));
2155 WriteLn(f, 'p2_priority_bfg ', Max(0, WeaponPreferences[WEAPON_BFG]));
2156 WriteLn(f, 'p2_priority_super ', Max(0, WeaponPreferences[WEAPON_SUPERPULEMET]));
2157 WriteLn(f, 'p2_priority_flamethrower ', Max(0, WeaponPreferences[WEAPON_FLAMETHROWER]));
2158 WriteLn(f, 'p2_priority_berserk ', Max(0, WeaponPreferences[WP_LAST+1]));
2159 end;
2161 // all cvars
2162 for i := 0 to High(commands) do
2163 begin
2164 if not commands[i].cheat then
2165 begin
2166 if @commands[i].procEx = @boolVarHandler then
2167 begin
2168 if PBoolean(commands[i].ptr)^ then j := 1 else j := 0;
2169 WriteLn(f, commands[i].cmd, ' ', j)
2170 end
2171 else if @commands[i].procEx = @intVarHandler then
2172 begin
2173 WriteLn(f, commands[i].cmd, ' ', PInteger(commands[i].ptr)^)
2174 end
2175 else if @commands[i].procEx = @wordVarHandler then
2176 begin
2177 WriteLn(f, commands[i].cmd, ' ', PWord(commands[i].ptr)^)
2178 end
2179 else if @commands[i].procEx = @dwordVarHandler then
2180 begin
2181 WriteLn(f, commands[i].cmd, ' ', PCardinal(commands[i].ptr)^)
2182 end
2183 else if @commands[i].procEx = @singleVarHandler then
2184 begin
2185 WriteLn(f, commands[i].cmd, ' ', PVarSingle(commands[i].ptr).val^:0:6)
2186 end
2187 else if @commands[i].procEx = @strVarHandler then
2188 begin
2189 if Length(PAnsiString(commands[i].ptr)^) = 0 then
2190 WriteLn(f, commands[i].cmd, ' ""')
2191 else
2192 WriteLn(f, commands[i].cmd, ' ', QuoteStr(PAnsiString(commands[i].ptr)^))
2193 end
2194 end
2195 end;
2197 WriteLn(f, 'r_maxfps ', gMaxFPS);
2198 WriteLn(f, 'r_reset');
2199 CloseFile(f)
2200 end;
2202 procedure g_Console_WriteGameConfig;
2203 var s: AnsiString;
2204 begin
2205 if gParsingBinds = false then
2206 begin
2207 s := e_GetWriteableDir(ConfigDirs);
2208 g_Console_WriteConfig(e_CatPath(s, gConfigScript))
2209 end
2210 end;
2212 procedure Init;
2213 var i: Integer;
2214 begin
2215 conRegVar('console_stdin', @ConsoleStdIn, 'enable reading commands from stdin', 'enable reading commands from stdin');
2216 {$IFDEF HEADLESS}
2217 ConsoleStdIn := True;
2218 {$ELSE}
2219 ConsoleStdIn := False;
2220 {$ENDIF}
2221 conRegVar('d_eres', @debug_e_res, '', '');
2222 for i := 1 to e_MaxJoys do
2223 conRegVar('joy' + IntToStr(i) + '_deadzone', @e_JoystickDeadzones[i - 1], '', '')
2224 end;
2226 initialization
2227 Init
2229 {$IFDEF HEADLESS}
2230 finalization
2231 DoneKeyboard;
2232 conbufStdOutRawMode := false;
2233 {$ENDIF}
2235 end.