DEADSOFTWARE

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