DEADSOFTWARE

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