DEADSOFTWARE

Console: Automatically save user-defined binds
[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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_console;
19 interface
21 uses
22 utils; // for SSArray
24 const
25 ACTION_JUMP = 0;
26 ACTION_MOVELEFT = 1;
27 ACTION_MOVERIGHT = 2;
28 ACTION_LOOKDOWN = 3;
29 ACTION_LOOKUP = 4;
30 ACTION_ATTACK = 5;
31 ACTION_SCORES = 6;
32 ACTION_ACTIVATE = 7;
33 ACTION_STRAFE = 8;
34 ACTION_WEAPNEXT = 9;
35 ACTION_WEAPPREV = 10;
37 FIRST_ACTION = ACTION_JUMP;
38 LAST_ACTION = ACTION_WEAPPREV;
40 procedure g_Console_Init;
41 procedure g_Console_Update;
42 procedure g_Console_Draw;
43 procedure g_Console_Char (C: AnsiChar);
44 procedure g_Console_Control (K: Word);
45 procedure g_Console_Process (L: AnsiString; quiet: Boolean=false);
46 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
47 procedure g_Console_Clear;
48 function g_Console_CommandBlacklisted (C: AnsiString): Boolean;
49 procedure g_Console_ReadConfig (filename: String);
50 procedure g_Console_WriteConfig (filename: String);
51 procedure g_Console_WriteGameConfig;
53 function g_Console_Interactive: Boolean;
54 function g_Console_Action (action: Integer): Boolean;
55 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
56 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
57 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = '');
58 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
59 procedure g_Console_ResetBinds;
61 procedure conwriteln (const s: AnsiString; show: Boolean=false);
62 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
64 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
65 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
66 procedure conRegVar (const conname: AnsiString; pvar: PInteger; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
68 // <0: no arg; 0/1: true/false
69 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
71 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
72 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
75 var
76 gConsoleShow: Boolean = false; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
77 gChatShow: Boolean = false;
78 gChatTeam: Boolean = false;
79 gAllowConsoleMessages: Boolean = true;
80 gJustChatted: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
81 gParsingBinds: Boolean = false; // íå ïåðåñîõðàíÿòü êîíôèã âî âðåìÿ ïàðñèíãà
82 gPlayerAction: Array [0..1, 0..LAST_ACTION] of Boolean; // [player, action]
84 implementation
86 uses
87 g_textures, g_main, e_graphics, e_input, g_game,
88 SysUtils, g_basic, g_options, Math, g_touch,
89 g_menu, g_gui, g_language, g_net, g_netmsg, e_log, conbuf;
92 type
93 PCommand = ^TCommand;
95 TCmdProc = procedure (p: SSArray);
96 TCmdProcEx = procedure (me: PCommand; p: SSArray);
98 TCommand = record
99 cmd: AnsiString;
100 proc: TCmdProc;
101 procEx: TCmdProcEx;
102 help: AnsiString;
103 hidden: Boolean;
104 ptr: Pointer; // various data
105 msg: AnsiString; // message for var changes
106 cheat: Boolean;
107 action: Integer; // >= 0 for action commands
108 player: Integer; // used for action commands
109 end;
111 TAlias = record
112 name: AnsiString;
113 commands: SSArray;
114 end;
117 const
118 Step = 32;
119 Alpha = 25;
120 MsgTime = 144;
121 MaxScriptRecursion = 16;
123 DEBUG_STRING = 'DEBUG MODE';
125 var
126 ID: DWORD;
127 RecursionDepth: Word = 0;
128 RecursionLimitHit: Boolean = False;
129 Cons_Y: SmallInt;
130 Cons_Shown: Boolean; // Ðèñîâàòü ëè êîíñîëü?
131 Line: AnsiString;
132 CPos: Word;
133 //ConsoleHistory: SSArray;
134 CommandHistory: SSArray;
135 Whitelist: SSArray;
136 commands: Array of TCommand = nil;
137 Aliases: Array of TAlias = nil;
138 CmdIndex: Word;
139 conSkipLines: Integer = 0;
140 MsgArray: Array [0..4] of record
141 Msg: AnsiString;
142 Time: Word;
143 end;
145 gInputBinds: Array [0..e_MaxInputKeys - 1] of record
146 down, up: SSArray;
147 end;
148 menu_toggled: BOOLEAN; (* hack for menu controls *)
149 gSkipFirstChar: Boolean; (* hack for console/chat input *)
152 procedure g_Console_Switch;
153 begin
154 gChatShow := False;
155 gConsoleShow := not gConsoleShow;
156 Cons_Shown := True;
157 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
158 end;
160 procedure g_Console_Chat_Switch (Team: Boolean = False);
161 begin
162 if not g_Game_IsNet then Exit;
163 gConsoleShow := False;
164 gChatShow := not gChatShow;
165 gChatTeam := Team;
166 Line := '';
167 CPos := 1;
168 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
169 end;
171 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
172 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
173 var
174 pos: Integer = 1;
175 frac: Single = 1;
176 slen: Integer;
177 begin
178 result := false;
179 res := 0;
180 slen := Length(s);
181 while (slen > 0) and (s[slen] <= ' ') do Dec(slen);
182 while (pos <= slen) and (s[pos] <= ' ') do Inc(pos);
183 if (pos > slen) then exit;
184 if (slen-pos = 1) and (s[pos] = '.') then exit; // single dot
185 // integral part
186 while (pos <= slen) do
187 begin
188 if (s[pos] < '0') or (s[pos] > '9') then break;
189 res := res*10+Byte(s[pos])-48;
190 Inc(pos);
191 end;
192 if (pos <= slen) then
193 begin
194 // must be a dot
195 if (s[pos] <> '.') then exit;
196 Inc(pos);
197 while (pos <= slen) do
198 begin
199 if (s[pos] < '0') or (s[pos] > '9') then break;
200 frac := frac/10;
201 res += frac*(Byte(s[pos])-48);
202 Inc(pos);
203 end;
204 end;
205 if (pos <= slen) then exit; // oops
206 result := true;
207 end;
210 // ////////////////////////////////////////////////////////////////////////// //
211 // <0: no arg; 0/1: true/false; 666: toggle
212 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
213 begin
214 if (idx < 0) or (idx > High(p)) then begin result := -1; exit; end;
215 result := 0;
216 if (p[idx] = '1') or (CompareText(p[idx], 'on') = 0) or (CompareText(p[idx], 'true') = 0) or
217 (CompareText(p[idx], 'tan') = 0) or (CompareText(p[idx], 'yes') = 0) then result := 1
218 else if (CompareText(p[idx], 'toggle') = 0) or (CompareText(p[idx], 'switch') = 0) or
219 (CompareText(p[idx], 't') = 0) then result := 666;
220 end;
223 procedure boolVarHandler (me: PCommand; p: SSArray);
224 procedure binaryFlag (var flag: Boolean; msg: AnsiString);
225 begin
226 if (Length(p) > 2) then
227 begin
228 conwritefln('too many arguments to ''%s''', [p[0]]);
229 end
230 else
231 begin
232 case conGetBoolArg(p, 1) of
233 -1: begin end;
234 0: if not me.cheat or conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
235 1: if not me.cheat or conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
236 666: if not me.cheat or conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
237 end;
238 if (Length(msg) = 0) then msg := p[0] else msg += ':';
239 if flag then conwritefln('%s tan', [msg]) else conwritefln('%s ona', [msg]);
240 end;
241 end;
242 begin
243 binaryFlag(PBoolean(me.ptr)^, me.msg);
244 end;
247 procedure intVarHandler (me: PCommand; p: SSArray);
248 begin
249 if (Length(p) <> 2) then
250 begin
251 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
252 end
253 else
254 begin
255 try
256 PInteger(me.ptr)^ := StrToInt(p[1]);
257 except
258 conwritefln('invalid integer value: "%s"', [p[1]]);
259 end;
260 end;
261 end;
264 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
265 var
266 f: Integer;
267 cp: PCommand;
268 begin
269 f := Length(commands);
270 SetLength(commands, f+1);
271 cp := @commands[f];
272 cp.cmd := LowerCase(conname);
273 cp.proc := nil;
274 cp.procEx := boolVarHandler;
275 cp.help := ahelp;
276 cp.hidden := ahidden;
277 cp.ptr := pvar;
278 cp.msg := amsg;
279 cp.cheat := acheat;
280 cp.action := -1;
281 cp.player := -1;
282 end;
285 procedure conRegVar (const conname: AnsiString; pvar: PInteger; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
286 var
287 f: Integer;
288 cp: PCommand;
289 begin
290 f := Length(commands);
291 SetLength(commands, f+1);
292 cp := @commands[f];
293 cp.cmd := LowerCase(conname);
294 cp.proc := nil;
295 cp.procEx := intVarHandler;
296 cp.help := ahelp;
297 cp.hidden := ahidden;
298 cp.ptr := pvar;
299 cp.msg := amsg;
300 cp.cheat := acheat;
301 cp.action := -1;
302 cp.player := -1;
303 end;
306 // ////////////////////////////////////////////////////////////////////////// //
307 type
308 PVarSingle = ^TVarSingle;
309 TVarSingle = record
310 val: PSingle;
311 min, max, def: Single; // default will be starting value
312 end;
315 procedure singleVarHandler (me: PCommand; p: SSArray);
316 var
317 pv: PVarSingle;
318 nv: Single;
319 msg: AnsiString;
320 begin
321 if (Length(p) > 2) then
322 begin
323 conwritefln('too many arguments to ''%s''', [me.cmd]);
324 exit;
325 end;
326 pv := PVarSingle(me.ptr);
327 if (Length(p) = 2) then
328 begin
329 if me.cheat and (not conIsCheatsEnabled) then begin conwriteln('not available'); exit; end;
330 if (CompareText(p[1], 'default') = 0) or (CompareText(p[1], 'def') = 0) or
331 (CompareText(p[1], 'd') = 0) or (CompareText(p[1], 'off') = 0) or
332 (CompareText(p[1], 'ona') = 0) then
333 begin
334 pv.val^ := pv.def;
335 end
336 else
337 begin
338 if not conParseFloat(nv, p[1]) then
339 begin
340 conwritefln('%s: ''%s'' doesn''t look like a floating number', [me.cmd, p[1]]);
341 exit;
342 end;
343 if (nv < pv.min) then nv := pv.min;
344 if (nv > pv.max) then nv := pv.max;
345 pv.val^ := nv;
346 end;
347 end;
348 msg := me.msg;
349 if (Length(msg) = 0) then msg := me.cmd else msg += ':';
350 conwritefln('%s %s', [msg, pv.val^]);
351 end;
354 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
355 var
356 f: Integer;
357 cp: PCommand;
358 pv: PVarSingle;
359 begin
360 GetMem(pv, sizeof(TVarSingle));
361 pv.val := pvar;
362 pv.min := amin;
363 pv.max := amax;
364 pv.def := pvar^;
365 f := Length(commands);
366 SetLength(commands, f+1);
367 cp := @commands[f];
368 cp.cmd := LowerCase(conname);
369 cp.proc := nil;
370 cp.procEx := singleVarHandler;
371 cp.help := ahelp;
372 cp.hidden := ahidden;
373 cp.ptr := pv;
374 cp.msg := amsg;
375 cp.cheat := acheat;
376 cp.action := -1;
377 cp.player := -1;
378 end;
381 // ////////////////////////////////////////////////////////////////////////// //
382 function GetStrACmd(var Str: AnsiString): AnsiString;
383 var
384 a: Integer;
385 begin
386 Result := '';
387 for a := 1 to Length(Str) do
388 if (a = Length(Str)) or (Str[a+1] = ';') then
389 begin
390 Result := Copy(Str, 1, a);
391 Delete(Str, 1, a+1);
392 Str := Trim(Str);
393 Exit;
394 end;
395 end;
397 function ParseAlias(Str: AnsiString): SSArray;
398 begin
399 Result := nil;
401 Str := Trim(Str);
403 if Str = '' then
404 Exit;
406 while Str <> '' do
407 begin
408 SetLength(Result, Length(Result)+1);
409 Result[High(Result)] := GetStrACmd(Str);
410 end;
411 end;
413 procedure ConsoleCommands(p: SSArray);
414 var
415 cmd, s: AnsiString;
416 a, b: Integer;
417 (* F: TextFile; *)
418 begin
419 cmd := LowerCase(p[0]);
420 s := '';
422 if cmd = 'clear' then
423 begin
424 //ConsoleHistory := nil;
425 cbufClear();
426 conSkipLines := 0;
428 for a := 0 to High(MsgArray) do
429 with MsgArray[a] do
430 begin
431 Msg := '';
432 Time := 0;
433 end;
434 end;
436 if cmd = 'clearhistory' then
437 CommandHistory := nil;
439 if cmd = 'showhistory' then
440 if CommandHistory <> nil then
441 begin
442 g_Console_Add('');
443 for a := 0 to High(CommandHistory) do
444 g_Console_Add(' '+CommandHistory[a]);
445 end;
447 if cmd = 'commands' then
448 begin
449 g_Console_Add('');
450 g_Console_Add('commands list:');
451 for a := High(commands) downto 0 do
452 begin
453 if (Length(commands[a].help) > 0) then
454 begin
455 g_Console_Add(' '+commands[a].cmd+' -- '+commands[a].help);
456 end
457 else
458 begin
459 g_Console_Add(' '+commands[a].cmd);
460 end;
461 end;
462 end;
464 if cmd = 'time' then
465 g_Console_Add(TimeToStr(Now), True);
467 if cmd = 'date' then
468 g_Console_Add(DateToStr(Now), True);
470 if cmd = 'echo' then
471 if Length(p) > 1 then
472 begin
473 if p[1] = 'ololo' then
474 gCheats := True
475 else
476 begin
477 s := '';
478 for a := 1 to High(p) do
479 s := s + p[a] + ' ';
480 g_Console_Add(b_Text_Format(s), True);
481 end;
482 end
483 else
484 g_Console_Add('');
486 if cmd = 'dump' then
487 begin
488 (*
489 if ConsoleHistory <> nil then
490 begin
491 if Length(P) > 1 then
492 s := P[1]
493 else
494 s := GameDir+'/console.txt';
496 {$I-}
497 AssignFile(F, s);
498 Rewrite(F);
499 if IOResult <> 0 then
500 begin
501 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
502 CloseFile(F);
503 Exit;
504 end;
506 for a := 0 to High(ConsoleHistory) do
507 WriteLn(F, ConsoleHistory[a]);
509 CloseFile(F);
510 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
511 {$I+}
512 end;
513 *)
514 end;
516 if cmd = 'exec' then
517 begin
518 // exec <filename>
519 if Length(p) = 2 then
520 g_Console_ReadConfig(GameDir + '/' + p[1])
521 else
522 g_Console_Add('exec <script file>');
523 end;
525 if cmd = 'writeconfig' then
526 begin
527 // writeconfig <filename>
528 if Length(p) = 2 then
529 g_Console_WriteConfig(GameDir + '/' + p[1])
530 else
531 g_Console_Add('writeconfig <file>');
532 end;
534 if (cmd = 'ver') or (cmd = 'version') then
535 begin
536 conwriteln('Doom 2D: Forever v. ' + GAME_VERSION);
537 conwritefln('Net protocol v. %d', [NET_PROTOCOL_VER]);
538 conwritefln('Build date: %s at %s', [GAME_BUILDDATE, GAME_BUILDTIME]);
539 end;
541 if cmd = 'alias' then
542 begin
543 // alias [alias_name] [commands]
544 if Length(p) > 1 then
545 begin
546 for a := 0 to High(Aliases) do
547 if Aliases[a].name = p[1] then
548 begin
549 if Length(p) > 2 then
550 Aliases[a].commands := ParseAlias(p[2])
551 else
552 for b := 0 to High(Aliases[a].commands) do
553 g_Console_Add(Aliases[a].commands[b]);
554 Exit;
555 end;
556 SetLength(Aliases, Length(Aliases)+1);
557 a := High(Aliases);
558 Aliases[a].name := p[1];
559 if Length(p) > 2 then
560 Aliases[a].commands := ParseAlias(p[2])
561 else
562 for b := 0 to High(Aliases[a].commands) do
563 g_Console_Add(Aliases[a].commands[b]);
564 end else
565 for a := 0 to High(Aliases) do
566 if Aliases[a].commands <> nil then
567 g_Console_Add(Aliases[a].name);
568 end;
570 if cmd = 'call' then
571 begin
572 // call <alias_name>
573 if Length(p) > 1 then
574 begin
575 if Aliases = nil then
576 Exit;
577 for a := 0 to High(Aliases) do
578 if Aliases[a].name = p[1] then
579 begin
580 if Aliases[a].commands <> nil then
581 begin
582 // with this system proper endless loop detection seems either impossible
583 // or very dirty to implement, so let's have this instead
584 // prevents endless loops
585 for b := 0 to High(Aliases[a].commands) do
586 begin
587 Inc(RecursionDepth);
588 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
589 if not RecursionLimitHit then
590 g_Console_Process(Aliases[a].commands[b], True);
591 Dec(RecursionDepth);
592 end;
593 if (RecursionDepth = 0) and RecursionLimitHit then
594 begin
595 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
596 RecursionLimitHit := False;
597 end;
598 end;
599 Exit;
600 end;
601 end
602 else
603 g_Console_Add('call <alias name>');
604 end;
605 end;
607 procedure WhitelistCommand(cmd: AnsiString);
608 var
609 a: Integer;
610 begin
611 SetLength(Whitelist, Length(Whitelist)+1);
612 a := High(Whitelist);
613 Whitelist[a] := LowerCase(cmd);
614 end;
616 procedure segfault (p: SSArray);
617 var
618 pp: PByte = nil;
619 begin
620 pp^ := 0;
621 end;
623 function GetCommandString (p: SSArray): AnsiString;
624 var i: Integer;
625 begin
626 result := '';
627 if Length(p) >= 1 then
628 begin
629 result := p[0];
630 for i := 1 to High(p) do
631 result := result + '; ' + p[i]
632 end
633 end;
635 procedure BindCommands (p: SSArray);
636 var cmd, key: AnsiString; i: Integer;
637 begin
638 cmd := LowerCase(p[0]);
639 case cmd of
640 'bind':
641 // bind <key> [down [up]]
642 if (Length(p) >= 2) and (Length(p) <= 4) then
643 begin
644 i := 0;
645 key := LowerCase(p[1]);
646 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
647 if i < e_MaxInputKeys then
648 begin
649 if Length(p) = 2 then
650 g_Console_Add('"' + e_KeyNames[i] + '" = "' + GetCommandString(gInputBinds[i].down) + '" "' + GetCommandString(gInputBinds[i].up) + '"')
651 else if Length(p) = 3 then
652 g_Console_BindKey(i, p[2], '')
653 else (* len = 4 *)
654 g_Console_BindKey(i, p[2], p[3])
655 end
656 else
657 g_Console_Add('bind: "' + p[1] + '" is not a key')
658 end
659 else
660 begin
661 g_Console_Add('bind <key> <down action> [up action]')
662 end;
663 'bindlist':
664 for i := 0 to e_MaxInputKeys - 1 do
665 if (gInputBinds[i].down <> nil) or (gInputBinds[i].up <> nil) then
666 g_Console_Add(e_KeyNames[i] + ' "' + GetCommandString(gInputBinds[i].down) + '" "' + GetCommandString(gInputBinds[i].up) + '"');
667 'unbind':
668 // unbind <key>
669 if Length(p) = 2 then
670 begin
671 key := LowerCase(p[1]);
672 i := 0;
673 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
674 if i < e_MaxInputKeys then
675 g_Console_BindKey(i, '')
676 else
677 g_Console_Add('unbind: "' + p[1] + '" is not a key')
678 end
679 else
680 g_Console_Add('unbind <key>');
681 'unbindall':
682 for i := 0 to e_MaxInputKeys - 1 do
683 g_Console_BindKey(i, '');
684 'showkeyboard':
685 g_Touch_ShowKeyboard(True);
686 'hidekeyboard':
687 g_Touch_ShowKeyboard(False);
688 'togglemenu':
689 if gConsoleShow then
690 begin
691 g_Console_Switch;
692 menu_toggled := True
693 end
694 else
695 begin
696 KeyPress(VK_ESCAPE);
697 menu_toggled := True
698 end;
699 'toggleconsole':
700 begin
701 g_Console_Switch;
702 gSkipFirstChar := g_Console_Interactive();
703 end;
704 'togglechat':
705 begin
706 if not gConsoleShow and (g_ActiveWindow = nil) then
707 g_Console_Chat_Switch;
708 gSkipFirstChar := not g_Console_Interactive()
709 end;
710 'toggleteamchat':
711 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
712 begin
713 if not gConsoleShow and (g_ActiveWindow = nil) then
714 g_Console_Chat_Switch(True);
715 gSkipFirstChar := not g_Console_Interactive()
716 end;
717 end
718 end;
720 procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
721 var
722 a: Integer;
723 cp: PCommand;
724 begin
725 SetLength(commands, Length(commands)+1);
726 a := High(commands);
727 cp := @commands[a];
728 cp.cmd := LowerCase(cmd);
729 cp.proc := proc;
730 cp.procEx := nil;
731 cp.help := ahelp;
732 cp.hidden := ahidden;
733 cp.ptr := nil;
734 cp.msg := '';
735 cp.cheat := acheat;
736 cp.action := -1;
737 cp.player := -1;
738 end;
740 procedure AddAction (cmd: AnsiString; action: Integer; help: AnsiString = ''; hidden: Boolean = False; cheat: Boolean = False);
741 const
742 PrefixList: array [0..1] of AnsiString = ('+', '-');
743 PlayerList: array [0..1] of Integer = (1, 2);
744 var
745 s: AnsiString;
746 i: Integer;
748 procedure NewAction (cmd: AnsiString; player: Integer);
749 var cp: PCommand;
750 begin
751 SetLength(commands, Length(commands) + 1);
752 cp := @commands[High(commands)];
753 cp.cmd := LowerCase(cmd);
754 cp.proc := nil;
755 cp.procEx := nil;
756 cp.help := help;
757 cp.hidden := hidden;
758 cp.ptr := nil;
759 cp.msg := '';
760 cp.cheat := cheat;
761 cp.action := action;
762 cp.player := player;
763 end;
765 begin
766 ASSERT(action >= FIRST_ACTION);
767 ASSERT(action <= LAST_ACTION);
768 for s in PrefixList do
769 begin
770 NewAction(s + cmd, 0);
771 for i in PlayerList do
772 NewAction(s + 'p' + IntToStr(i) + '_' + cmd, i - 1)
773 end
774 end;
776 procedure g_Console_Init();
777 var
778 a: Integer;
779 begin
780 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
781 Cons_Y := -(gScreenHeight div 2);
782 gConsoleShow := False;
783 gChatShow := False;
784 Cons_Shown := False;
785 CPos := 1;
787 for a := 0 to High(MsgArray) do
788 with MsgArray[a] do
789 begin
790 Msg := '';
791 Time := 0;
792 end;
794 AddCommand('segfault', segfault, 'make segfault');
796 AddCommand('bind', BindCommands);
797 AddCommand('bindlist', BindCommands);
798 AddCommand('unbind', BindCommands);
799 AddCommand('unbindall', BindCommands);
800 AddCommand('showkeyboard', BindCommands);
801 AddCommand('hidekeyboard', BindCommands);
802 AddCommand('togglemenu', BindCommands);
803 AddCommand('toggleconsole', BindCommands);
804 AddCommand('togglechat', BindCommands);
805 AddCommand('toggleteamchat', BindCommands);
807 AddCommand('clear', ConsoleCommands, 'clear console');
808 AddCommand('clearhistory', ConsoleCommands);
809 AddCommand('showhistory', ConsoleCommands);
810 AddCommand('commands', ConsoleCommands);
811 AddCommand('time', ConsoleCommands);
812 AddCommand('date', ConsoleCommands);
813 AddCommand('echo', ConsoleCommands);
814 AddCommand('dump', ConsoleCommands);
815 AddCommand('exec', ConsoleCommands);
816 AddCommand('writeconfig', ConsoleCommands);
817 AddCommand('alias', ConsoleCommands);
818 AddCommand('call', ConsoleCommands);
819 AddCommand('ver', ConsoleCommands);
820 AddCommand('version', ConsoleCommands);
822 AddCommand('d_window', DebugCommands);
823 AddCommand('d_sounds', DebugCommands);
824 AddCommand('d_frames', DebugCommands);
825 AddCommand('d_winmsg', DebugCommands);
826 AddCommand('d_monoff', DebugCommands);
827 AddCommand('d_botoff', DebugCommands);
828 AddCommand('d_monster', DebugCommands);
829 AddCommand('d_health', DebugCommands);
830 AddCommand('d_player', DebugCommands);
831 AddCommand('d_joy', DebugCommands);
832 AddCommand('d_mem', DebugCommands);
834 AddCommand('p1_name', GameCVars);
835 AddCommand('p2_name', GameCVars);
836 AddCommand('p1_color', GameCVars);
837 AddCommand('p2_color', GameCVars);
838 AddCommand('r_showscore', GameCVars);
839 AddCommand('r_showlives', GameCVars);
840 AddCommand('r_showstat', GameCVars);
841 AddCommand('r_showkillmsg', GameCVars);
842 AddCommand('r_showspect', GameCVars);
843 AddCommand('r_showping', GameCVars);
844 AddCommand('g_gamemode', GameCVars);
845 AddCommand('g_friendlyfire', GameCVars);
846 AddCommand('g_weaponstay', GameCVars);
847 AddCommand('g_allow_exit', GameCVars);
848 AddCommand('g_allow_monsters', GameCVars);
849 AddCommand('g_bot_vsmonsters', GameCVars);
850 AddCommand('g_bot_vsplayers', GameCVars);
851 AddCommand('g_scorelimit', GameCVars);
852 AddCommand('g_timelimit', GameCVars);
853 AddCommand('g_maxlives', GameCVars);
854 AddCommand('g_warmuptime', GameCVars);
855 AddCommand('net_interp', GameCVars);
856 AddCommand('net_forceplayerupdate', GameCVars);
857 AddCommand('net_predictself', GameCVars);
858 AddCommand('sv_name', GameCVars);
859 AddCommand('sv_passwd', GameCVars);
860 AddCommand('sv_maxplrs', GameCVars);
861 AddCommand('sv_public', GameCVars);
862 AddCommand('sv_intertime', GameCVars);
864 AddCommand('quit', GameCommands);
865 AddCommand('exit', GameCommands);
866 AddCommand('pause', GameCommands);
867 AddCommand('endgame', GameCommands);
868 AddCommand('restart', GameCommands);
869 AddCommand('addbot', GameCommands);
870 AddCommand('bot_add', GameCommands);
871 AddCommand('bot_addlist', GameCommands);
872 AddCommand('bot_addred', GameCommands);
873 AddCommand('bot_addblue', GameCommands);
874 AddCommand('bot_removeall', GameCommands);
875 AddCommand('chat', GameCommands);
876 AddCommand('teamchat', GameCommands);
877 AddCommand('game', GameCommands);
878 AddCommand('host', GameCommands);
879 AddCommand('map', GameCommands);
880 AddCommand('nextmap', GameCommands);
881 AddCommand('endmap', GameCommands);
882 AddCommand('goodbye', GameCommands);
883 AddCommand('suicide', GameCommands);
884 AddCommand('spectate', GameCommands);
885 AddCommand('ready', GameCommands);
886 AddCommand('kick', GameCommands);
887 AddCommand('kick_id', GameCommands);
888 AddCommand('ban', GameCommands);
889 AddCommand('permban', GameCommands);
890 AddCommand('ban_id', GameCommands);
891 AddCommand('permban_id', GameCommands);
892 AddCommand('unban', GameCommands);
893 AddCommand('connect', GameCommands);
894 AddCommand('disconnect', GameCommands);
895 AddCommand('reconnect', GameCommands);
896 AddCommand('say', GameCommands);
897 AddCommand('tell', GameCommands);
898 AddCommand('overtime', GameCommands);
899 AddCommand('rcon_password', GameCommands);
900 AddCommand('rcon', GameCommands);
901 AddCommand('callvote', GameCommands);
902 AddCommand('vote', GameCommands);
903 AddCommand('clientlist', GameCommands);
904 AddCommand('event', GameCommands);
905 AddCommand('screenshot', GameCommands);
906 AddCommand('weapon', GameCommands);
907 AddCommand('p1_weapon', GameCommands);
908 AddCommand('p2_weapon', GameCommands);
910 AddCommand('god', GameCheats);
911 AddCommand('notarget', GameCheats);
912 AddCommand('give', GameCheats); // "exit" too ;-)
913 AddCommand('open', GameCheats);
914 AddCommand('fly', GameCheats);
915 AddCommand('noclip', GameCheats);
916 AddCommand('speedy', GameCheats);
917 AddCommand('jumpy', GameCheats);
918 AddCommand('noreload', GameCheats);
919 AddCommand('aimline', GameCheats);
920 AddCommand('automap', GameCheats);
922 AddAction('jump', ACTION_JUMP);
923 AddAction('moveleft', ACTION_MOVELEFT);
924 AddAction('moveright', ACTION_MOVERIGHT);
925 AddAction('lookup', ACTION_LOOKUP);
926 AddAction('lookdown', ACTION_LOOKDOWN);
927 AddAction('attack', ACTION_ATTACK);
928 AddAction('scores', ACTION_SCORES);
929 AddAction('activate', ACTION_ACTIVATE);
930 AddAction('strafe', ACTION_STRAFE);
931 AddAction('weapnext', ACTION_WEAPNEXT);
932 AddAction('weapprev', ACTION_WEAPPREV);
934 WhitelistCommand('say');
935 WhitelistCommand('tell');
936 WhitelistCommand('overtime');
937 WhitelistCommand('ready');
938 WhitelistCommand('map');
939 WhitelistCommand('nextmap');
940 WhitelistCommand('endmap');
941 WhitelistCommand('restart');
942 WhitelistCommand('kick');
943 WhitelistCommand('ban');
945 WhitelistCommand('addbot');
946 WhitelistCommand('bot_add');
947 WhitelistCommand('bot_addred');
948 WhitelistCommand('bot_addblue');
949 WhitelistCommand('bot_removeall');
951 WhitelistCommand('g_gamemode');
952 WhitelistCommand('g_friendlyfire');
953 WhitelistCommand('g_weaponstay');
954 WhitelistCommand('g_allow_exit');
955 WhitelistCommand('g_allow_monsters');
956 WhitelistCommand('g_scorelimit');
957 WhitelistCommand('g_timelimit');
959 gParsingBinds := True;
960 g_Console_ResetBinds;
961 g_Console_ReadConfig(GameDir + '/dfconfig.cfg');
962 g_Console_ReadConfig(GameDir + '/autoexec.cfg');
963 gParsingBinds := False;
965 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
966 g_Console_Add('');
967 end;
969 procedure g_Console_Update();
970 var
971 a, b: Integer;
972 begin
973 if Cons_Shown then
974 begin
975 // Â ïðîöåññå îòêðûòèÿ:
976 if gConsoleShow and (Cons_Y < 0) then
977 begin
978 Cons_Y := Cons_Y+Step;
979 end;
981 // Â ïðîöåññå çàêðûòèÿ:
982 if (not gConsoleShow) and
983 (Cons_Y > -(gScreenHeight div 2)) then
984 Cons_Y := Cons_Y-Step;
986 // Îêîí÷àòåëüíî îòêðûëàñü:
987 if Cons_Y > 0 then
988 Cons_Y := 0;
990 // Îêîí÷àòåëüíî çàêðûëàñü:
991 if Cons_Y <= (-(gScreenHeight div 2)) then
992 begin
993 Cons_Y := -(gScreenHeight div 2);
994 Cons_Shown := False;
995 end;
996 end;
998 a := 0;
999 while a <= High(MsgArray) do
1000 begin
1001 if MsgArray[a].Time > 0 then
1002 begin
1003 if MsgArray[a].Time = 1 then
1004 begin
1005 if a < High(MsgArray) then
1006 begin
1007 for b := a to High(MsgArray)-1 do
1008 MsgArray[b] := MsgArray[b+1];
1010 MsgArray[High(MsgArray)].Time := 0;
1012 a := a - 1;
1013 end;
1014 end
1015 else
1016 Dec(MsgArray[a].Time);
1017 end;
1019 a := a + 1;
1020 end;
1021 end;
1024 procedure drawConsoleText ();
1025 var
1026 CWidth, CHeight: Byte;
1027 ty: Integer;
1028 sp, ep: LongWord;
1029 skip: Integer;
1031 procedure putLine (sp, ep: LongWord);
1032 var
1033 p: LongWord;
1034 wdt, cw: Integer;
1035 begin
1036 p := sp;
1037 wdt := 0;
1038 while p <> ep do
1039 begin
1040 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1041 if wdt+cw > gScreenWidth-8 then break;
1042 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1043 Inc(wdt, cw);
1044 cbufNext(p);
1045 end;
1046 if p <> ep then putLine(p, ep); // do rest of the line first
1047 // now print our part
1048 if skip = 0 then
1049 begin
1050 ep := p;
1051 p := sp;
1052 wdt := 2;
1053 while p <> ep do
1054 begin
1055 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1056 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
1057 Inc(wdt, cw);
1058 cbufNext(p);
1059 end;
1060 Dec(ty, CHeight);
1061 end
1062 else
1063 begin
1064 Dec(skip);
1065 end;
1066 end;
1068 begin
1069 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1070 ty := (gScreenHeight div 2)-4-2*CHeight-Abs(Cons_Y);
1071 skip := conSkipLines;
1072 cbufLastLine(sp, ep);
1073 repeat
1074 putLine(sp, ep);
1075 if ty+CHeight <= 0 then break;
1076 until not cbufLineUp(sp, ep);
1077 end;
1079 procedure g_Console_Draw();
1080 var
1081 CWidth, CHeight: Byte;
1082 mfW, mfH: Word;
1083 a, b: Integer;
1084 begin
1085 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1087 for a := 0 to High(MsgArray) do
1088 if MsgArray[a].Time > 0 then
1089 e_TextureFontPrintFmt(0, CHeight*a, MsgArray[a].Msg,
1090 gStdFont, True);
1092 if not Cons_Shown then
1093 begin
1094 if gChatShow then
1095 begin
1096 if gChatTeam then
1097 begin
1098 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say team> ' + Line,
1099 gStdFont, 255, 255, 255, 1, True);
1100 e_TextureFontPrintEx((CPos + 9)*CWidth, gScreenHeight - CHeight - 1, '_',
1101 gStdFont, 255, 255, 255, 1, True);
1102 end
1103 else
1104 begin
1105 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say> ' + Line,
1106 gStdFont, 255, 255, 255, 1, True);
1107 e_TextureFontPrintEx((CPos + 4)*CWidth, gScreenHeight - CHeight - 1, '_',
1108 gStdFont, 255, 255, 255, 1, True);
1109 end;
1110 end;
1111 Exit;
1112 end;
1114 if gDebugMode then
1115 begin
1116 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
1117 a := (gScreenWidth - 2*mfW) div 2;
1118 b := Cons_Y + ((gScreenHeight div 2) - 2*mfH) div 2;
1119 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
1120 _RGB(128, 0, 0), 2.0);
1121 end;
1123 e_DrawSize(ID, 0, Cons_Y, Alpha, False, False, gScreenWidth, gScreenHeight div 2);
1124 e_TextureFontPrint(0, Cons_Y+(gScreenHeight div 2)-CHeight-4, '> '+Line, gStdFont);
1126 drawConsoleText();
1127 (*
1128 if ConsoleHistory <> nil then
1129 begin
1130 b := 0;
1131 if CHeight > 0 then
1132 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
1133 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
1135 b := Max(b-Offset, 0);
1136 d := Max(High(ConsoleHistory)-Offset, 0);
1138 c := 2;
1139 for a := d downto b do
1140 begin
1141 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
1142 gStdFont, True);
1143 c := c + 1;
1144 end;
1145 end;
1146 *)
1148 e_TextureFontPrint((CPos+1)*CWidth, Cons_Y+(gScreenHeight div 2)-21, '_', gStdFont);
1149 end;
1151 procedure g_Console_Char(C: AnsiChar);
1152 begin
1153 if not gSkipFirstChar then
1154 begin
1155 Insert(C, Line, CPos);
1156 CPos := CPos + 1;
1157 end;
1158 gSkipFirstChar := False
1159 end;
1162 var
1163 tcomplist: array of AnsiString = nil;
1164 tcompidx: array of Integer = nil;
1166 procedure Complete ();
1167 var
1168 i, c: Integer;
1169 tused: Integer;
1170 ll, lpfx, cmd: AnsiString;
1171 begin
1172 if (Length(Line) = 0) then
1173 begin
1174 g_Console_Add('');
1175 for i := 0 to High(commands) do
1176 begin
1177 // hidden commands are hidden when cheats aren't enabled
1178 if commands[i].hidden and not conIsCheatsEnabled then continue;
1179 if (Length(commands[i].help) > 0) then
1180 begin
1181 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1182 end
1183 else
1184 begin
1185 g_Console_Add(' '+commands[i].cmd);
1186 end;
1187 end;
1188 exit;
1189 end;
1191 ll := LowerCase(Line);
1192 lpfx := '';
1194 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
1195 begin
1196 ll := Copy(ll, 0, Length(ll)-1);
1197 for i := 0 to High(commands) do
1198 begin
1199 // hidden commands are hidden when cheats aren't enabled
1200 if commands[i].hidden and not conIsCheatsEnabled then continue;
1201 if (commands[i].cmd = ll) then
1202 begin
1203 if (Length(commands[i].help) > 0) then
1204 begin
1205 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1206 end;
1207 end;
1208 end;
1209 exit;
1210 end;
1212 // build completion list
1213 tused := 0;
1214 for i := 0 to High(commands) do
1215 begin
1216 // hidden commands are hidden when cheats aren't enabled
1217 if commands[i].hidden and not conIsCheatsEnabled then continue;
1218 cmd := commands[i].cmd;
1219 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
1220 begin
1221 if (tused = Length(tcomplist)) then
1222 begin
1223 SetLength(tcomplist, Length(tcomplist)+128);
1224 SetLength(tcompidx, Length(tcompidx)+128);
1225 end;
1226 tcomplist[tused] := cmd;
1227 tcompidx[tused] := i;
1228 Inc(tused);
1229 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
1230 end;
1231 end;
1233 // get longest prefix
1234 for i := 0 to tused-1 do
1235 begin
1236 cmd := tcomplist[i];
1237 for c := 1 to Length(lpfx) do
1238 begin
1239 if (c > Length(cmd)) then break;
1240 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
1241 end;
1242 end;
1244 if (tused = 0) then exit;
1246 if (tused = 1) then
1247 begin
1248 Line := tcomplist[0]+' ';
1249 CPos := Length(Line)+1;
1250 end
1251 else
1252 begin
1253 // has longest prefix?
1254 if (Length(lpfx) > Length(ll)) then
1255 begin
1256 Line := lpfx;
1257 CPos:= Length(Line)+1;
1258 end
1259 else
1260 begin
1261 g_Console_Add('');
1262 for i := 0 to tused-1 do
1263 begin
1264 if (Length(commands[tcompidx[i]].help) > 0) then
1265 begin
1266 g_Console_Add(' '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
1267 end
1268 else
1269 begin
1270 g_Console_Add(' '+tcomplist[i]);
1271 end;
1272 end;
1273 end;
1274 end;
1275 end;
1278 procedure g_Console_Control(K: Word);
1279 begin
1280 case K of
1281 IK_BACKSPACE:
1282 if (Length(Line) > 0) and (CPos > 1) then
1283 begin
1284 Delete(Line, CPos-1, 1);
1285 CPos := CPos-1;
1286 end;
1287 IK_DELETE:
1288 if (Length(Line) > 0) and (CPos <= Length(Line)) then
1289 Delete(Line, CPos, 1);
1290 IK_LEFT, IK_KPLEFT, VK_LEFT, JOY0_LEFT, JOY1_LEFT, JOY2_LEFT, JOY3_LEFT:
1291 if CPos > 1 then
1292 CPos := CPos - 1;
1293 IK_RIGHT, IK_KPRIGHT, VK_RIGHT, JOY0_RIGHT, JOY1_RIGHT, JOY2_RIGHT, JOY3_RIGHT:
1294 if CPos <= Length(Line) then
1295 CPos := CPos + 1;
1296 IK_RETURN, IK_KPRETURN, VK_OPEN, VK_FIRE, JOY0_ATTACK, JOY1_ATTACK, JOY2_ATTACK, JOY3_ATTACK:
1297 begin
1298 if Cons_Shown then
1299 g_Console_Process(Line)
1300 else
1301 if gChatShow then
1302 begin
1303 if (Length(Line) > 0) and g_Game_IsNet then
1304 begin
1305 if gChatTeam then
1306 begin
1307 if g_Game_IsClient then
1308 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
1309 else
1310 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1311 NET_CHAT_TEAM, gPlayer1Settings.Team);
1312 end
1313 else
1314 begin
1315 if g_Game_IsClient then
1316 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
1317 else
1318 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1319 NET_CHAT_PLAYER);
1320 end;
1321 end;
1323 Line := '';
1324 CPos := 1;
1325 gChatShow := False;
1326 gJustChatted := True;
1327 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
1328 end;
1329 end;
1330 IK_TAB:
1331 if not gChatShow then
1332 Complete();
1333 IK_DOWN, IK_KPDOWN, VK_DOWN, JOY0_DOWN, JOY1_DOWN, JOY2_DOWN, JOY3_DOWN:
1334 if not gChatShow then
1335 if (CommandHistory <> nil) and
1336 (CmdIndex < Length(CommandHistory)) then
1337 begin
1338 if CmdIndex < Length(CommandHistory)-1 then
1339 CmdIndex := CmdIndex + 1;
1340 Line := CommandHistory[CmdIndex];
1341 CPos := Length(Line) + 1;
1342 end;
1343 IK_UP, IK_KPUP, VK_UP, JOY0_UP, JOY1_UP, JOY2_UP, JOY3_UP:
1344 if not gChatShow then
1345 if (CommandHistory <> nil) and
1346 (CmdIndex <= Length(CommandHistory)) then
1347 begin
1348 if CmdIndex > 0 then
1349 CmdIndex := CmdIndex - 1;
1350 Line := CommandHistory[CmdIndex];
1351 Cpos := Length(Line) + 1;
1352 end;
1353 IK_PAGEUP, IK_KPPAGEUP, VK_PREV, JOY0_PREV, JOY1_PREV, JOY2_PREV, JOY3_PREV: // PgUp
1354 if not gChatShow then Inc(conSkipLines);
1355 IK_PAGEDN, IK_KPPAGEDN, VK_NEXT, JOY0_NEXT, JOY1_NEXT, JOY2_NEXT, JOY3_NEXT: // PgDown
1356 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1357 IK_HOME, IK_KPHOME:
1358 CPos := 1;
1359 IK_END, IK_KPEND:
1360 CPos := Length(Line) + 1;
1361 IK_A..IK_Z, IK_SPACE, IK_SHIFT, IK_RSHIFT, IK_CAPSLOCK, IK_LBRACKET, IK_RBRACKET,
1362 IK_SEMICOLON, IK_QUOTE, IK_BACKSLASH, IK_SLASH, IK_COMMA, IK_DOT, IK_EQUALS,
1363 IK_0, IK_1, IK_2, IK_3, IK_4, IK_5, IK_6, IK_7, IK_8, IK_9, IK_MINUS, IK_EQUALS:
1364 (* see TEXTINPUT event *)
1365 else
1366 if not gSkipFirstChar then
1367 begin
1368 if gConsoleShow and g_Console_MatchBind(K, 'toggleconsole') then
1369 g_Console_Switch;
1371 if g_Console_MatchBind(K, 'togglemenu') then
1372 begin
1373 menu_toggled := True;
1374 if gChatShow then
1375 g_Console_Chat_Switch
1376 else if gConsoleShow then
1377 g_Console_Switch
1378 end
1379 end
1380 end
1381 end;
1383 function GetStr(var Str: AnsiString): AnsiString;
1384 var
1385 a, b: Integer;
1386 begin
1387 Result := '';
1388 if Str[1] = '"' then
1389 begin
1390 for b := 1 to Length(Str) do
1391 if (b = Length(Str)) or (Str[b+1] = '"') then
1392 begin
1393 Result := Copy(Str, 2, b-1);
1394 Delete(Str, 1, b+1);
1395 Str := Trim(Str);
1396 Exit;
1397 end;
1398 end;
1400 for a := 1 to Length(Str) do
1401 if (a = Length(Str)) or (Str[a+1] = ' ') then
1402 begin
1403 Result := Copy(Str, 1, a);
1404 Delete(Str, 1, a+1);
1405 Str := Trim(Str);
1406 Exit;
1407 end;
1408 end;
1410 function ParseString(Str: AnsiString): SSArray;
1411 begin
1412 Result := nil;
1414 Str := Trim(Str);
1416 if Str = '' then
1417 Exit;
1419 while Str <> '' do
1420 begin
1421 SetLength(Result, Length(Result)+1);
1422 Result[High(Result)] := GetStr(Str);
1423 end;
1424 end;
1426 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
1428 procedure conmsg (s: AnsiString);
1429 var
1430 a: Integer;
1431 begin
1432 if length(s) = 0 then exit;
1433 for a := 0 to High(MsgArray) do
1434 begin
1435 with MsgArray[a] do
1436 begin
1437 if Time = 0 then
1438 begin
1439 Msg := s;
1440 Time := MsgTime;
1441 exit;
1442 end;
1443 end;
1444 end;
1445 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1446 with MsgArray[High(MsgArray)] do
1447 begin
1448 Msg := L;
1449 Time := MsgTime;
1450 end;
1451 end;
1453 var
1454 f: Integer;
1455 begin
1456 // put it to console
1457 cbufPut(L);
1458 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1460 // now show 'em out of console too
1461 show := show and gAllowConsoleMessages;
1462 if show and gShowMessages then
1463 begin
1464 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1465 while length(L) > 0 do
1466 begin
1467 f := Pos(#10, L);
1468 if f <= 0 then f := length(L)+1;
1469 conmsg(Copy(L, 1, f-1));
1470 Delete(L, 1, f);
1471 end;
1472 end;
1474 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1475 //ConsoleHistory[High(ConsoleHistory)] := L;
1477 (*
1478 {$IFDEF HEADLESS}
1479 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1480 {$ENDIF}
1481 *)
1482 end;
1485 var
1486 consolewriterLastWasEOL: Boolean = false;
1488 procedure consolewriter (constref buf; len: SizeUInt);
1489 var
1490 b: PByte;
1491 begin
1492 if (len < 1) then exit;
1493 b := PByte(@buf);
1494 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1495 while (len > 0) do
1496 begin
1497 if (b[0] <> 13) and (b[0] <> 10) then
1498 begin
1499 cbufPut(AnsiChar(b[0]));
1500 end
1501 else
1502 begin
1503 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1504 cbufPut(#10);
1505 end;
1506 len -= 1;
1507 b += 1;
1508 end;
1509 end;
1512 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1513 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1514 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1515 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1516 begin
1517 g_Console_Add(s, show);
1518 end;
1521 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1522 begin
1523 if show then
1524 begin
1525 g_Console_Add(formatstrf(s, args), true);
1526 end
1527 else
1528 begin
1529 consolewriterLastWasEOL := false;
1530 formatstrf(s, args, consolewriter);
1531 if not consolewriterLastWasEOL then cbufPut(#10);
1532 end;
1533 end;
1536 procedure g_Console_Clear();
1537 begin
1538 //ConsoleHistory := nil;
1539 cbufClear();
1540 conSkipLines := 0;
1541 end;
1543 procedure AddToHistory(L: AnsiString);
1544 var
1545 len: Integer;
1546 begin
1547 len := Length(CommandHistory);
1549 if (len = 0) or
1550 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1551 begin
1552 SetLength(CommandHistory, len+1);
1553 CommandHistory[len] := L;
1554 end;
1556 CmdIndex := Length(CommandHistory);
1557 end;
1559 function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
1560 var
1561 Arr: SSArray;
1562 i: Integer;
1563 begin
1564 Result := True;
1566 Arr := nil;
1568 if Trim(C) = '' then
1569 Exit;
1571 Arr := ParseString(C);
1572 if Arr = nil then
1573 Exit;
1575 for i := 0 to High(Whitelist) do
1576 if Whitelist[i] = LowerCase(Arr[0]) then
1577 Result := False;
1578 end;
1580 procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
1581 var
1582 Arr: SSArray;
1583 i: Integer;
1584 begin
1585 Arr := nil;
1587 if Trim(L) = '' then
1588 Exit;
1590 conSkipLines := 0; // "unscroll"
1592 if L = 'goobers' then
1593 begin
1594 Line := '';
1595 CPos := 1;
1596 gCheats := true;
1597 g_Console_Add('Your memory serves you well.');
1598 exit;
1599 end;
1601 if not quiet then
1602 begin
1603 g_Console_Add('> '+L);
1604 Line := '';
1605 CPos := 1;
1606 end;
1608 Arr := ParseString(L);
1609 if Arr = nil then
1610 Exit;
1612 if commands = nil then
1613 Exit;
1615 if not quiet then
1616 AddToHistory(L);
1618 for i := 0 to High(commands) do
1619 begin
1620 if commands[i].cmd = LowerCase(Arr[0]) then
1621 begin
1622 if commands[i].action >= 0 then
1623 begin
1624 gPlayerAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+';
1625 exit
1626 end;
1627 if assigned(commands[i].procEx) then
1628 begin
1629 commands[i].procEx(@commands[i], Arr);
1630 exit
1631 end;
1632 if assigned(commands[i].proc) then
1633 begin
1634 commands[i].proc(Arr);
1635 exit
1636 end
1637 end
1638 end;
1640 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1641 end;
1644 function g_Console_Interactive: Boolean;
1645 begin
1646 Result := gConsoleShow
1647 end;
1649 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = '');
1650 begin
1651 //e_LogWritefln('bind "%s" "%s" <%s>', [LowerCase(e_KeyNames[key]), cmd, key]);
1652 ASSERT(key >= 0);
1653 ASSERT(key < e_MaxInputKeys);
1654 if key > 0 then
1655 begin
1656 gInputBinds[key].down := ParseAlias(down);
1657 gInputBinds[key].up := ParseAlias(up);
1658 end;
1659 g_Console_WriteGameConfig();
1660 end;
1662 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
1664 function EqualsCommandLists (a, b: SSArray): Boolean;
1665 var i, len: Integer;
1666 begin
1667 result := False;
1668 len := Length(a);
1669 if len = Length(b) then
1670 begin
1671 i := 0;
1672 while (i < len) and (a[i] = b[i]) do inc(i);
1673 if i >= len then
1674 result := True
1675 end
1676 end;
1678 begin
1679 ASSERT(key >= 0);
1680 ASSERT(key < e_MaxInputKeys);
1681 result := EqualsCommandLists(ParseAlias(down), gInputBinds[key].down) and EqualsCommandLists(ParseAlias(up), gInputBinds[key].up)
1682 end;
1684 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
1685 var i: Integer;
1686 begin
1687 ASSERT(n >= 1);
1688 result := 0;
1689 if commands = nil then Exit;
1690 i := 0;
1691 while (n >= 1) and (i < e_MaxInputKeys) do
1692 begin
1693 if g_Console_MatchBind(i, down, up) then
1694 begin
1695 result := i;
1696 dec(n)
1697 end;
1698 inc(i)
1699 end;
1700 if n >= 1 then
1701 result := 0
1702 end;
1704 function g_Console_Action (action: Integer): Boolean;
1705 var i, len: Integer;
1706 begin
1707 ASSERT(action >= FIRST_ACTION);
1708 ASSERT(action <= LAST_ACTION);
1709 i := 0;
1710 len := Length(gPlayerAction);
1711 while (i < len) and (not gPlayerAction[i, action]) do inc(i);
1712 Result := i < len
1713 end;
1715 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
1716 var i: Integer;
1717 begin
1718 if (not g_GUIGrabInput) and (key >= 0) and (key < e_MaxInputKeys) and ((gInputBinds[key].down <> nil) or (gInputBinds[key].up <> nil)) then
1719 begin
1720 if down then
1721 begin
1722 if not gChatShow then
1723 for i := 0 to High(gInputBinds[key].down) do
1724 g_Console_Process(gInputBinds[key].down[i], True);
1725 end
1726 else
1727 for i := 0 to High(gInputBinds[key].up) do
1728 g_Console_Process(gInputBinds[key].up[i], True)
1729 end;
1730 if down and not menu_toggled then
1731 KeyPress(key);
1732 menu_toggled := False
1733 end;
1735 procedure g_Console_ResetBinds;
1736 var i: Integer;
1737 begin
1738 for i := 0 to e_MaxInputKeys - 1 do
1739 g_Console_BindKey(i, '', '');
1741 g_Console_BindKey(IK_GRAVE, 'toggleconsole');
1742 g_Console_BindKey(IK_ESCAPE, 'togglemenu');
1743 g_Console_BindKey(IK_A, '+p1_moveleft', '-p1_moveleft');
1744 g_Console_BindKey(IK_D, '+p1_moveright', '-p1_moveright');
1745 g_Console_BindKey(IK_W, '+p1_lookup', '-p1_lookup');
1746 g_Console_BindKey(IK_S, '+p1_lookdown', '-p1_lookdown');
1747 g_Console_BindKey(IK_SPACE, '+p1_jump', '-p1_jump');
1748 g_Console_BindKey(IK_H, '+p1_attack', '-p1_attack');
1749 g_Console_BindKey(IK_J, '+p1_activate', '-p1_activate');
1750 g_Console_BindKey(IK_E, '+p1_weapnext', '-p1_weapnext');
1751 g_Console_BindKey(IK_Q, '+p1_weapprev', '-p1_weapprev');
1752 g_Console_BindKey(IK_ALT, '+p1_strafe', '-p1_strafe');
1753 g_Console_BindKey(IK_1, 'p1_weapon 1');
1754 g_Console_BindKey(IK_2, 'p1_weapon 2');
1755 g_Console_BindKey(IK_3, 'p1_weapon 3');
1756 g_Console_BindKey(IK_4, 'p1_weapon 4');
1757 g_Console_BindKey(IK_5, 'p1_weapon 5');
1758 g_Console_BindKey(IK_6, 'p1_weapon 6');
1759 g_Console_BindKey(IK_7, 'p1_weapon 7');
1760 g_Console_BindKey(IK_8, 'p1_weapon 8');
1761 g_Console_BindKey(IK_9, 'p1_weapon 9');
1762 g_Console_BindKey(IK_0, 'p1_weapon 10');
1763 g_Console_BindKey(IK_MINUS, 'p1_weapon 11');
1764 g_Console_BindKey(IK_T, 'togglechat');
1765 g_Console_BindKey(IK_Y, 'toggleteamchat');
1766 g_Console_BindKey(IK_F11, 'screenshot');
1767 g_Console_BindKey(IK_TAB, '+p1_scores', '-p1_scores');
1768 g_Console_BindKey(IK_PAUSE, 'pause');
1769 g_Console_BindKey(IK_F1, 'vote');
1771 (* for i := 0 to e_MaxJoys - 1 do *)
1772 for i := 0 to 1 do
1773 begin
1774 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_LEFT), '+p' + IntToStr(i mod 2 + 1) + '_moveleft', '-p' + IntToStr(i mod 2 + 1) + '_moveleft');
1775 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_RIGHT), '+p' + IntToStr(i mod 2 + 1) + '_moveright', '-p' + IntToStr(i mod 2 + 1) + '_moveright');
1776 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_UP), '+p' + IntToStr(i mod 2 + 1) + '_lookup', '-p' + IntToStr(i mod 2 + 1) + '_lookup');
1777 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_DOWN), '+p' + IntToStr(i mod 2 + 1) + '_lookdown', '-p' + IntToStr(i mod 2 + 1) + '_lookdown');
1778 g_Console_BindKey(e_JoyButtonToKey(i, 2), '+p' + IntToStr(i mod 2 + 1) + '_jump', '-p' + IntToStr(i mod 2 + 1) + '_jump');
1779 g_Console_BindKey(e_JoyButtonToKey(i, 0), '+p' + IntToStr(i mod 2 + 1) + '_attack', '-p' + IntToStr(i mod 2 + 1) + '_attack');
1780 g_Console_BindKey(e_JoyButtonToKey(i, 3), '+p' + IntToStr(i mod 2 + 1) + '_activate', '-p' + IntToStr(i mod 2 + 1) + '_activate');
1781 g_Console_BindKey(e_JoyButtonToKey(i, 1), '+p' + IntToStr(i mod 2 + 1) + '_weapnext', '-p' + IntToStr(i mod 2 + 1) + '_weapnext');
1782 g_Console_BindKey(e_JoyButtonToKey(i, 4), '+p' + IntToStr(i mod 2 + 1) + '_weapprev', '-p' + IntToStr(i mod 2 + 1) + '_weapprev');
1783 g_Console_BindKey(e_JoyButtonToKey(i, 7), '+p' + IntToStr(i mod 2 + 1) + '_strafe', '-p' + IntToStr(i mod 2 + 1) + '_strafe');
1784 g_Console_BindKey(e_JoyButtonToKey(i, 10), 'togglemenu');
1785 end;
1787 g_Console_BindKey(VK_ESCAPE, 'togglemenu');
1788 g_Console_BindKey(VK_LSTRAFE, '+moveleft; +strafe', '-moveleft; -strafe');
1789 g_Console_BindKey(VK_RSTRAFE, '+moveright; +strafe', '-moveright; -strafe');
1790 g_Console_BindKey(VK_LEFT, '+moveleft', '-moveleft');
1791 g_Console_BindKey(VK_RIGHT, '+moveright', '-moveright');
1792 g_Console_BindKey(VK_UP, '+lookup', '-lookup');
1793 g_Console_BindKey(VK_DOWN, '+lookdown', '-lookdown');
1794 g_Console_BindKey(VK_JUMP, '+jump', '-jump');
1795 g_Console_BindKey(VK_FIRE, '+attack', '-attack');
1796 g_Console_BindKey(VK_OPEN, '+activate', '-activate');
1797 g_Console_BindKey(VK_NEXT, '+weapnext', '-weapnext');
1798 g_Console_BindKey(VK_PREV, '+weapprev', '-weapprev');
1799 g_Console_BindKey(VK_STRAFE, '+strafe', '-strafe');
1800 g_Console_BindKey(VK_0, 'weapon 1');
1801 g_Console_BindKey(VK_1, 'weapon 2');
1802 g_Console_BindKey(VK_2, 'weapon 3');
1803 g_Console_BindKey(VK_3, 'weapon 4');
1804 g_Console_BindKey(VK_4, 'weapon 5');
1805 g_Console_BindKey(VK_5, 'weapon 6');
1806 g_Console_BindKey(VK_6, 'weapon 7');
1807 g_Console_BindKey(VK_7, 'weapon 8');
1808 g_Console_BindKey(VK_8, 'weapon 9');
1809 g_Console_BindKey(VK_9, 'weapon 10');
1810 g_Console_BindKey(VK_A, 'weapon 11');
1811 g_Console_BindKey(VK_CHAT, 'togglechat');
1812 g_Console_BindKey(VK_TEAM, 'toggleteamchat');
1813 g_Console_BindKey(VK_CONSOLE, 'toggleconsole');
1814 g_Console_BindKey(VK_PRINTSCR, 'screenshot');
1815 g_Console_BindKey(VK_STATUS, '+scores', '-scores');
1816 g_Console_BindKey(VK_SHOWKBD, 'showkeyboard');
1817 g_Console_BindKey(VK_HIDEKBD, 'hidekeyboard');
1818 end;
1820 procedure g_Console_ReadConfig (filename: String);
1821 var f: TextFile; s: AnsiString; i, len: Integer;
1822 begin
1823 if FileExists(filename) then
1824 begin
1825 AssignFile(f, filename);
1826 Reset(f);
1827 while not EOF(f) do
1828 begin
1829 ReadLn(f, s);
1830 len := Length(s);
1831 if len > 0 then
1832 begin
1833 i := 1;
1834 (* skip spaces *)
1835 while (i <= len) and (s[i] <= ' ') do inc(i);
1836 (* skip comments *)
1837 if (i <= len) and ((s[i] <> '#') and ((i + 1 > len) or (s[i] <> '/') or (s[i + 1] <> '/'))) then
1838 g_Console_Process(s, True);
1839 end
1840 end;
1841 CloseFile(f);
1842 end
1843 end;
1845 procedure g_Console_WriteConfig (filename: String);
1846 var f: TextFile; i, j: Integer;
1847 begin
1848 AssignFile(f, filename);
1849 Rewrite(f);
1850 WriteLn(f, '// generated by doom2d, do not modify');
1851 WriteLn(f, 'unbindall');
1852 for i := 0 to e_MaxInputKeys - 1 do
1853 if (Length(gInputBinds[i].down) > 0) or (Length(gInputBinds[i].up) > 0) then
1854 WriteLn(f, 'bind ', e_KeyNames[i], ' "', GetCommandString(gInputBinds[i].down), '" "', GetCommandString(gInputBinds[i].up), '"');
1855 for i := 0 to High(commands) do
1856 begin
1857 if not commands[i].cheat then
1858 begin
1859 if @commands[i].procEx = @boolVarHandler then
1860 begin
1861 if PBoolean(commands[i].ptr)^ then j := 1 else j := 0;
1862 WriteLn(f, commands[i].cmd, ' "', j, '"')
1863 end
1864 else if @commands[i].procEx = @intVarHandler then
1865 begin
1866 WriteLn(f, commands[i].cmd, ' "', PInteger(commands[i].ptr)^, '"')
1867 end
1868 else if @commands[i].procEx = @singleVarHandler then
1869 begin
1870 WriteLn(f, commands[i].cmd, ' "', PVarSingle(commands[i].ptr).val^:0:6, '"')
1871 end
1872 end
1873 end;
1874 CloseFile(f)
1875 end;
1877 procedure g_Console_WriteGameConfig;
1878 begin
1879 if gParsingBinds then
1880 Exit;
1881 g_Console_WriteConfig(GameDir + '/dfconfig.cfg');
1882 end;
1884 end.