DEADSOFTWARE

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