DEADSOFTWARE

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