DEADSOFTWARE

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