DEADSOFTWARE

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