1 (* Copyright (C) DooM 2D:Forever Developers
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.
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.
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/>.
16 {$INCLUDE ../shared/a_modes.inc}
22 wadreader
; // for SArray
24 procedure g_Console_Init ();
25 procedure g_Console_Update ();
26 procedure g_Console_Draw ();
27 procedure g_Console_Switch ();
28 procedure g_Console_Char (C
: AnsiChar);
29 procedure g_Console_Control (K
: Word);
30 procedure g_Console_Process (L
: AnsiString; quiet
: Boolean=false);
31 procedure g_Console_Add (L
: AnsiString; show
: Boolean=false);
32 procedure g_Console_Clear ();
33 function g_Console_CommandBlacklisted (C
: AnsiString): Boolean;
35 procedure conwriteln (const s
: AnsiString; show
: Boolean=false);
36 procedure conwritefln (const s
: AnsiString; args
: array of const; show
: Boolean=false);
38 // <0: no arg; 0/1: true/false
39 function conGetBoolArg (p
: SArray
; idx
: Integer): Integer;
41 procedure g_Console_Chat_Switch (team
: Boolean=false);
43 procedure conRegVar (const conname
: AnsiString; pvar
: PBoolean; const ahelp
: AnsiString; const amsg
: AnsiString; acheat
: Boolean=false); overload
;
44 procedure conRegVar (const conname
: AnsiString; pvar
: PSingle; amin
, amax
: Single; const ahelp
: AnsiString; const amsg
: AnsiString; acheat
: Boolean=false); overload
;
48 gConsoleShow
: Boolean = false; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
49 gChatShow
: Boolean = false;
50 gChatTeam
: Boolean = false;
51 gAllowConsoleMessages
: Boolean = true;
52 gChatEnter
: Boolean = true;
53 gJustChatted
: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
59 g_textures
, g_main
, e_graphics
, e_input
, g_game
,
60 SysUtils
, g_basic
, g_options
, Math
,
61 g_menu
, g_language
, g_net
, g_netmsg
, e_log
, conbuf
, utils
;
67 TCmdProc
= procedure (p
: SArray
);
68 TCmdProcEx
= procedure (me
: PCommand
; p
: SArray
);
76 ptr
: Pointer; // various data
77 msg
: AnsiString; // message for var changes
91 MaxScriptRecursion
= 16;
93 DEBUG_STRING
= 'DEBUG MODE';
97 RecursionDepth
: Word = 0;
98 RecursionLimitHit
: Boolean = False;
100 Cons_Shown
: Boolean; // Ðèñîâàòü ëè êîíñîëü?
103 //ConsoleHistory: SArray;
104 CommandHistory
: SArray
;
106 commands
: Array of TCommand
= nil;
107 Aliases
: Array of TAlias
= nil;
109 conSkipLines
: Integer = 0;
110 MsgArray
: Array [0..4] of record
116 // ////////////////////////////////////////////////////////////////////////// //
117 // <0: no arg; 0/1: true/false; 666: toggle
118 function conGetBoolArg (p
: SArray
; idx
: Integer): Integer;
120 if (idx
< 0) or (idx
> High(p
)) then begin result
:= -1; exit
; end;
122 if (p
[idx
] = '1') or (CompareText(p
[idx
], 'on') = 0) or (CompareText(p
[idx
], 'true') = 0) or
123 (CompareText(p
[idx
], 'tan') = 0) or (CompareText(p
[idx
], 'yes') = 0) then result
:= 1
124 else if (CompareText(p
[idx
], 'toggle') = 0) or (CompareText(p
[idx
], 'switch') = 0) or
125 (CompareText(p
[idx
], 't') = 0) then result
:= 666;
129 procedure boolVarHandler (me
: PCommand
; p
: SArray
);
130 procedure binaryFlag (var flag
: Boolean; msg
: AnsiString);
132 if (Length(p
) > 2) then
134 conwritefln('too many arguments to ''%s''', [p
[0]]);
138 case conGetBoolArg(p
, 1) of
140 0: if conIsCheatsEnabled
then flag
:= false else begin conwriteln('not available'); exit
; end;
141 1: if conIsCheatsEnabled
then flag
:= true else begin conwriteln('not available'); exit
; end;
142 666: if conIsCheatsEnabled
then flag
:= not flag
else begin conwriteln('not available'); exit
; end;
144 if flag
then conwritefln('%s: tan', [msg
]) else conwritefln('%s: ona', [msg
]);
148 binaryFlag(PBoolean(me
.ptr
)^, me
.msg
);
152 procedure conRegVar (const conname
: AnsiString; pvar
: PBoolean; const ahelp
: AnsiString; const amsg
: AnsiString; acheat
: Boolean=false); overload
;
157 f
:= Length(commands
);
158 SetLength(commands
, f
+1);
160 cp
.cmd
:= LowerCase(conname
);
162 cp
.procEx
:= boolVarHandler
;
171 // ////////////////////////////////////////////////////////////////////////// //
173 PVarSingle
= ^TVarSingle
;
176 min
, max
, def
: Single; // default will be starting value
180 procedure singleVarHandler (me
: PCommand
; p
: SArray
);
181 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
182 function parseFloat (var res
: Single; const s
: AnsiString): Boolean;
191 while (slen
> 0) and (s
[slen
] <= ' ') do Dec(slen
);
192 while (pos
<= slen
) and (s
[pos
] <= ' ') do Inc(pos
);
193 if (pos
> slen
) then exit
;
194 if (slen
-pos
= 1) and (s
[pos
] = '.') then exit
; // single dot
196 while (pos
<= slen
) do
198 if (s
[pos
] < '0') or (s
[pos
] > '9') then break
;
199 res
:= res
*10+Byte(s
[pos
])-48;
202 if (pos
<= slen
) then
205 if (s
[pos
] <> '.') then exit
;
207 while (pos
<= slen
) do
209 if (s
[pos
] < '0') or (s
[pos
] > '9') then break
;
211 res
+= frac
*(Byte(s
[pos
])-48);
215 if (pos
<= slen
) then exit
; // oops
223 if (Length(p
) > 2) then
225 conwritefln('too many arguments to ''%s''', [p
[0]]);
228 pv
:= PVarSingle(me
.ptr
);
229 if (Length(p
) = 2) then
231 if not conIsCheatsEnabled
then begin conwriteln('not available'); exit
; end;
232 if (CompareText(p
[1], 'default') = 0) or (CompareText(p
[1], 'def') = 0) or
233 (CompareText(p
[1], 'd') = 0) or (CompareText(p
[1], 'off') = 0) or
234 (CompareText(p
[1], 'ona') = 0) then
240 if not parseFloat(nv
, p
[1]) then
242 conwritefln('%s: ''%s'' doesn''t look like a floating number', [p
[0], p
[1]]);
245 if (nv
< pv
.min
) then nv
:= pv
.min
;
246 if (nv
> pv
.max
) then nv
:= pv
.max
;
251 if (Length(msg
) = 0) then msg
:= p
[0] else msg
+= ':';
252 conwritefln('%s %s', [msg
, pv
.val
^]);
256 procedure conRegVar (const conname
: AnsiString; pvar
: PSingle; amin
, amax
: Single; const ahelp
: AnsiString; const amsg
: AnsiString; acheat
: Boolean=false); overload
;
262 GetMem(pv
, sizeof(pv
^));
267 f
:= Length(commands
);
268 SetLength(commands
, f
+1);
270 cp
.cmd
:= LowerCase(conname
);
272 cp
.procEx
:= singleVarHandler
;
281 // ////////////////////////////////////////////////////////////////////////// //
282 function GetStrACmd(var Str
: AnsiString): AnsiString;
287 for a
:= 1 to Length(Str
) do
288 if (a
= Length(Str
)) or (Str
[a
+1] = ';') then
290 Result
:= Copy(Str
, 1, a
);
297 function ParseAlias(Str
: AnsiString): SArray
;
308 SetLength(Result
, Length(Result
)+1);
309 Result
[High(Result
)] := GetStrACmd(Str
);
313 procedure ConsoleCommands(p
: SArray
);
319 cmd
:= LowerCase(p
[0]);
322 if cmd
= 'clear' then
324 //ConsoleHistory := nil;
328 for a
:= 0 to High(MsgArray
) do
336 if cmd
= 'clearhistory' then
337 CommandHistory
:= nil;
339 if cmd
= 'showhistory' then
340 if CommandHistory
<> nil then
343 for a
:= 0 to High(CommandHistory
) do
344 g_Console_Add(' '+CommandHistory
[a
]);
347 if cmd
= 'commands' then
350 g_Console_Add('commands list:');
351 for a
:= High(commands
) downto 0 do
353 if (Length(commands
[a
].help
) > 0) then
355 g_Console_Add(' '+commands
[a
].cmd
+' -- '+commands
[a
].help
);
359 g_Console_Add(' '+commands
[a
].cmd
);
365 g_Console_Add(TimeToStr(Now
), True);
368 g_Console_Add(DateToStr(Now
), True);
371 if Length(p
) > 1 then
373 if p
[1] = 'ololo' then
378 for a
:= 1 to High(p
) do
380 g_Console_Add(b_Text_Format(s
), True);
389 if ConsoleHistory <> nil then
391 if Length(P) > 1 then
394 s := GameDir+'/console.txt';
399 if IOResult <> 0 then
401 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
406 for a := 0 to High(ConsoleHistory) do
407 WriteLn(F, ConsoleHistory[a]);
410 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
419 if Length(p
) > 1 then
421 s
:= GameDir
+'/'+p
[1];
426 if IOResult
<> 0 then
428 g_Console_Add(Format(_lc
[I_CONSOLE_ERROR_READ
], [s
]));
432 g_Console_Add(Format(_lc
[I_CONSOLE_EXEC
], [s
]));
437 if IOResult
<> 0 then
439 g_Console_Add(Format(_lc
[I_CONSOLE_ERROR_READ
], [s
]));
443 if Pos('#', s
) <> 1 then // script comment
445 // prevents endless loops
447 RecursionLimitHit
:= (RecursionDepth
> MaxScriptRecursion
) or RecursionLimitHit
;
448 if not RecursionLimitHit
then
449 g_Console_Process(s
, True);
453 if (RecursionDepth
= 0) and RecursionLimitHit
then
455 g_Console_Add(Format(_lc
[I_CONSOLE_ERROR_CALL
], [s
]));
456 RecursionLimitHit
:= False;
463 g_Console_Add('exec <script file>');
466 if cmd
= 'alias' then
468 // alias [alias_name] [commands]
469 if Length(p
) > 1 then
471 for a
:= 0 to High(Aliases
) do
472 if Aliases
[a
].name
= p
[1] then
474 if Length(p
) > 2 then
475 Aliases
[a
].commands
:= ParseAlias(p
[2])
477 for b
:= 0 to High(Aliases
[a
].commands
) do
478 g_Console_Add(Aliases
[a
].commands
[b
]);
481 SetLength(Aliases
, Length(Aliases
)+1);
483 Aliases
[a
].name
:= p
[1];
484 if Length(p
) > 2 then
485 Aliases
[a
].commands
:= ParseAlias(p
[2])
487 for b
:= 0 to High(Aliases
[a
].commands
) do
488 g_Console_Add(Aliases
[a
].commands
[b
]);
490 for a
:= 0 to High(Aliases
) do
491 if Aliases
[a
].commands
<> nil then
492 g_Console_Add(Aliases
[a
].name
);
498 if Length(p
) > 1 then
500 if Aliases
= nil then
502 for a
:= 0 to High(Aliases
) do
503 if Aliases
[a
].name
= p
[1] then
505 if Aliases
[a
].commands
<> nil then
507 // with this system proper endless loop detection seems either impossible
508 // or very dirty to implement, so let's have this instead
509 // prevents endless loops
510 for b
:= 0 to High(Aliases
[a
].commands
) do
513 RecursionLimitHit
:= (RecursionDepth
> MaxScriptRecursion
) or RecursionLimitHit
;
514 if not RecursionLimitHit
then
515 g_Console_Process(Aliases
[a
].commands
[b
], True);
518 if (RecursionDepth
= 0) and RecursionLimitHit
then
520 g_Console_Add(Format(_lc
[I_CONSOLE_ERROR_CALL
], [s
]));
521 RecursionLimitHit
:= False;
528 g_Console_Add('call <alias name>');
532 procedure WhitelistCommand(cmd
: AnsiString);
536 SetLength(Whitelist
, Length(Whitelist
)+1);
537 a
:= High(Whitelist
);
538 Whitelist
[a
] := LowerCase(cmd
);
541 procedure AddCommand(cmd
: AnsiString; proc
: TCmdProc
; ahelp
: AnsiString=''; ahidden
: Boolean=false; acheat
: Boolean=false);
546 SetLength(commands
, Length(commands
)+1);
549 cp
.cmd
:= LowerCase(cmd
);
553 cp
.hidden
:= ahidden
;
559 procedure g_Console_Init();
563 g_Texture_CreateWAD(ID
, GameWAD
+':TEXTURES\CONSOLE');
564 Cons_Y
:= -(gScreenHeight
div 2);
565 gConsoleShow
:= False;
570 for a
:= 0 to High(MsgArray
) do
577 AddCommand('clear', ConsoleCommands
, 'clear console');
578 AddCommand('clearhistory', ConsoleCommands
);
579 AddCommand('showhistory', ConsoleCommands
);
580 AddCommand('commands', ConsoleCommands
);
581 AddCommand('time', ConsoleCommands
);
582 AddCommand('date', ConsoleCommands
);
583 AddCommand('echo', ConsoleCommands
);
584 AddCommand('dump', ConsoleCommands
);
585 AddCommand('exec', ConsoleCommands
);
586 AddCommand('alias', ConsoleCommands
);
587 AddCommand('call', ConsoleCommands
);
589 AddCommand('d_window', DebugCommands
);
590 AddCommand('d_sounds', DebugCommands
);
591 AddCommand('d_frames', DebugCommands
);
592 AddCommand('d_winmsg', DebugCommands
);
593 AddCommand('d_monoff', DebugCommands
);
594 AddCommand('d_botoff', DebugCommands
);
595 AddCommand('d_monster', DebugCommands
);
596 AddCommand('d_health', DebugCommands
);
597 AddCommand('d_player', DebugCommands
);
598 AddCommand('d_joy', DebugCommands
);
600 AddCommand('p1_name', GameCVars
);
601 AddCommand('p2_name', GameCVars
);
602 AddCommand('p1_color', GameCVars
);
603 AddCommand('p2_color', GameCVars
);
604 AddCommand('r_showfps', GameCVars
);
605 AddCommand('r_showtime', GameCVars
);
606 AddCommand('r_showscore', GameCVars
);
607 AddCommand('r_showlives', GameCVars
);
608 AddCommand('r_showstat', GameCVars
);
609 AddCommand('r_showkillmsg', GameCVars
);
610 AddCommand('r_showspect', GameCVars
);
611 AddCommand('r_showping', GameCVars
);
612 AddCommand('g_gamemode', GameCVars
);
613 AddCommand('g_friendlyfire', GameCVars
);
614 AddCommand('g_weaponstay', GameCVars
);
615 AddCommand('g_allow_exit', GameCVars
);
616 AddCommand('g_allow_monsters', GameCVars
);
617 AddCommand('g_bot_vsmonsters', GameCVars
);
618 AddCommand('g_bot_vsplayers', GameCVars
);
619 AddCommand('g_scorelimit', GameCVars
);
620 AddCommand('g_timelimit', GameCVars
);
621 AddCommand('g_maxlives', GameCVars
);
622 AddCommand('g_warmuptime', GameCVars
);
623 AddCommand('net_interp', GameCVars
);
624 AddCommand('net_forceplayerupdate', GameCVars
);
625 AddCommand('net_predictself', GameCVars
);
626 AddCommand('sv_name', GameCVars
);
627 AddCommand('sv_passwd', GameCVars
);
628 AddCommand('sv_maxplrs', GameCVars
);
629 AddCommand('sv_public', GameCVars
);
630 AddCommand('sv_intertime', GameCVars
);
632 AddCommand('quit', GameCommands
);
633 AddCommand('exit', GameCommands
);
634 AddCommand('pause', GameCommands
);
635 AddCommand('endgame', GameCommands
);
636 AddCommand('restart', GameCommands
);
637 AddCommand('addbot', GameCommands
);
638 AddCommand('bot_add', GameCommands
);
639 AddCommand('bot_addlist', GameCommands
);
640 AddCommand('bot_addred', GameCommands
);
641 AddCommand('bot_addblue', GameCommands
);
642 AddCommand('bot_removeall', GameCommands
);
643 AddCommand('chat', GameCommands
);
644 AddCommand('teamchat', GameCommands
);
645 AddCommand('game', GameCommands
);
646 AddCommand('host', GameCommands
);
647 AddCommand('map', GameCommands
);
648 AddCommand('nextmap', GameCommands
);
649 AddCommand('endmap', GameCommands
);
650 AddCommand('goodbye', GameCommands
);
651 AddCommand('suicide', GameCommands
);
652 AddCommand('spectate', GameCommands
);
653 AddCommand('ready', GameCommands
);
654 AddCommand('kick', GameCommands
);
655 AddCommand('kick_id', GameCommands
);
656 AddCommand('ban', GameCommands
);
657 AddCommand('permban', GameCommands
);
658 AddCommand('ban_id', GameCommands
);
659 AddCommand('permban_id', GameCommands
);
660 AddCommand('unban', GameCommands
);
661 AddCommand('connect', GameCommands
);
662 AddCommand('disconnect', GameCommands
);
663 AddCommand('reconnect', GameCommands
);
664 AddCommand('say', GameCommands
);
665 AddCommand('tell', GameCommands
);
666 AddCommand('overtime', GameCommands
);
667 AddCommand('rcon_password', GameCommands
);
668 AddCommand('rcon', GameCommands
);
669 AddCommand('callvote', GameCommands
);
670 AddCommand('vote', GameCommands
);
671 AddCommand('clientlist', GameCommands
);
672 AddCommand('event', GameCommands
);
674 AddCommand('god', GameCheats
);
675 AddCommand('notarget', GameCheats
);
676 AddCommand('give', GameCheats
); // "exit" too ;-)
677 AddCommand('open', GameCheats
);
678 AddCommand('fly', GameCheats
);
679 AddCommand('noclip', GameCheats
);
680 AddCommand('speedy', GameCheats
);
681 AddCommand('jumpy', GameCheats
);
682 AddCommand('noreload', GameCheats
);
683 AddCommand('aimline', GameCheats
);
684 AddCommand('automap', GameCheats
);
686 WhitelistCommand('say');
687 WhitelistCommand('tell');
688 WhitelistCommand('overtime');
689 WhitelistCommand('ready');
690 WhitelistCommand('map');
691 WhitelistCommand('nextmap');
692 WhitelistCommand('endmap');
693 WhitelistCommand('restart');
694 WhitelistCommand('kick');
695 WhitelistCommand('ban');
697 WhitelistCommand('addbot');
698 WhitelistCommand('bot_add');
699 WhitelistCommand('bot_addred');
700 WhitelistCommand('bot_addblue');
701 WhitelistCommand('bot_removeall');
703 WhitelistCommand('g_gamemode');
704 WhitelistCommand('g_friendlyfire');
705 WhitelistCommand('g_weaponstay');
706 WhitelistCommand('g_allow_exit');
707 WhitelistCommand('g_allow_monsters');
708 WhitelistCommand('g_scorelimit');
709 WhitelistCommand('g_timelimit');
711 g_Console_Add(Format(_lc
[I_CONSOLE_WELCOME
], [GAME_VERSION
]));
715 procedure g_Console_Update();
721 // Â ïðîöåññå îòêðûòèÿ:
722 if gConsoleShow
and (Cons_Y
< 0) then
724 Cons_Y
:= Cons_Y
+Step
;
727 // Â ïðîöåññå çàêðûòèÿ:
728 if (not gConsoleShow
) and
729 (Cons_Y
> -(gScreenHeight
div 2)) then
730 Cons_Y
:= Cons_Y
-Step
;
732 // Îêîí÷àòåëüíî îòêðûëàñü:
736 // Îêîí÷àòåëüíî çàêðûëàñü:
737 if Cons_Y
<= (-(gScreenHeight
div 2)) then
739 Cons_Y
:= -(gScreenHeight
div 2);
745 while a
<= High(MsgArray
) do
747 if MsgArray
[a
].Time
> 0 then
749 if MsgArray
[a
].Time
= 1 then
751 if a
< High(MsgArray
) then
753 for b
:= a
to High(MsgArray
)-1 do
754 MsgArray
[b
] := MsgArray
[b
+1];
756 MsgArray
[High(MsgArray
)].Time
:= 0;
762 Dec(MsgArray
[a
].Time
);
770 procedure drawConsoleText ();
772 CWidth
, CHeight
: Byte;
777 procedure putLine (sp
, ep
: LongWord);
786 cw
:= e_TextureFontCharWidth(cbufAt(p
), gStdFont
);
787 if wdt
+cw
> gScreenWidth
-8 then break
;
788 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
792 if p
<> ep
then putLine(p
, ep
); // do rest of the line first
793 // now print our part
801 cw
:= e_TextureFontCharWidth(cbufAt(p
), gStdFont
);
802 e_TextureFontPrintCharEx(wdt
, ty
, cbufAt(p
), gStdFont
);
815 e_TextureFontGetSize(gStdFont
, CWidth
, CHeight
);
816 ty
:= (gScreenHeight
div 2)-4-2*CHeight
-Abs(Cons_Y
);
817 skip
:= conSkipLines
;
818 cbufLastLine(sp
, ep
);
821 if ty
+CHeight
<= 0 then break
;
822 until not cbufLineUp(sp
, ep
);
825 procedure g_Console_Draw();
827 CWidth
, CHeight
: Byte;
831 e_TextureFontGetSize(gStdFont
, CWidth
, CHeight
);
833 for a
:= 0 to High(MsgArray
) do
834 if MsgArray
[a
].Time
> 0 then
835 e_TextureFontPrintFmt(0, CHeight
*a
, MsgArray
[a
].Msg
,
838 if not Cons_Shown
then
844 e_TextureFontPrintEx(0, gScreenHeight
- CHeight
- 1, 'say team> ' + Line
,
845 gStdFont
, 255, 255, 255, 1, True);
846 e_TextureFontPrintEx((CPos
+ 9)*CWidth
, gScreenHeight
- CHeight
- 1, '_',
847 gStdFont
, 255, 255, 255, 1, True);
851 e_TextureFontPrintEx(0, gScreenHeight
- CHeight
- 1, 'say> ' + Line
,
852 gStdFont
, 255, 255, 255, 1, True);
853 e_TextureFontPrintEx((CPos
+ 4)*CWidth
, gScreenHeight
- CHeight
- 1, '_',
854 gStdFont
, 255, 255, 255, 1, True);
862 e_CharFont_GetSize(gMenuFont
, DEBUG_STRING
, mfW
, mfH
);
863 a
:= (gScreenWidth
- 2*mfW
) div 2;
864 b
:= Cons_Y
+ ((gScreenHeight
div 2) - 2*mfH
) div 2;
865 e_CharFont_PrintEx(gMenuFont
, a
div 2, b
div 2, DEBUG_STRING
,
866 _RGB(128, 0, 0), 2.0);
869 e_DrawSize(ID
, 0, Cons_Y
, Alpha
, False, False, gScreenWidth
, gScreenHeight
div 2);
870 e_TextureFontPrint(0, Cons_Y
+(gScreenHeight
div 2)-CHeight
-4, '> '+Line
, gStdFont
);
874 if ConsoleHistory <> nil then
878 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
879 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
881 b := Max(b-Offset, 0);
882 d := Max(High(ConsoleHistory)-Offset, 0);
885 for a := d downto b do
887 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
894 e_TextureFontPrint((CPos
+1)*CWidth
, Cons_Y
+(gScreenHeight
div 2)-21, '_', gStdFont
);
897 procedure g_Console_Switch();
899 if gChatShow
then Exit
;
900 gConsoleShow
:= not gConsoleShow
;
904 procedure g_Console_Chat_Switch(Team
: Boolean = False);
906 if gConsoleShow
then Exit
;
907 if not g_Game_IsNet
then Exit
;
908 gChatShow
:= not gChatShow
;
916 procedure g_Console_Char(C
: AnsiChar);
918 if gChatShow
and (not gChatEnter
) then
920 Insert(C
, Line
, CPos
);
926 tcomplist
: array of AnsiString = nil;
927 tcompidx
: array of Integer = nil;
929 procedure Complete ();
933 ll
, lpfx
, cmd
: AnsiString;
935 if (Length(Line
) = 0) then
938 for i
:= 0 to High(commands
) do
940 if not commands
[i
].hidden
then
942 if (Length(commands
[i
].help
) > 0) then
944 g_Console_Add(' '+commands
[i
].cmd
+' -- '+commands
[i
].help
);
948 g_Console_Add(' '+commands
[i
].cmd
);
955 ll
:= LowerCase(Line
);
958 if (Length(ll
) > 1) and (ll
[Length(ll
)] = ' ') then
960 ll
:= Copy(ll
, 0, Length(ll
)-1);
961 for i
:= 0 to High(commands
) do
963 if commands
[i
].hidden
then continue
;
964 if (commands
[i
].cmd
= ll
) then
966 if (Length(commands
[i
].help
) > 0) then
968 g_Console_Add(' '+commands
[i
].cmd
+' -- '+commands
[i
].help
);
975 // build completion list
977 for i
:= 0 to High(commands
) do
979 if commands
[i
].hidden
then continue
;
980 cmd
:= commands
[i
].cmd
;
981 if (Length(cmd
) >= Length(ll
)) and (ll
= Copy(cmd
, 0, Length(ll
))) then
983 if (tused
= Length(tcomplist
)) then
985 SetLength(tcomplist
, Length(tcomplist
)+128);
986 SetLength(tcompidx
, Length(tcompidx
)+128);
988 tcomplist
[tused
] := cmd
;
989 tcompidx
[tused
] := i
;
991 if (Length(cmd
) > Length(lpfx
)) then lpfx
:= cmd
;
995 // get longest prefix
996 for i
:= 0 to tused
-1 do
999 for c
:= 1 to Length(lpfx
) do
1001 if (c
> Length(cmd
)) then break
;
1002 if (cmd
[c
] <> lpfx
[c
]) then begin lpfx
:= Copy(lpfx
, 0, c
-1); break
; end;
1006 if (tused
= 0) then exit
;
1010 Line
:= tcomplist
[0]+' ';
1011 CPos
:= Length(Line
)+1;
1015 // has longest prefix?
1016 if (Length(lpfx
) > Length(ll
)) then
1019 CPos
:= Length(Line
)+1;
1024 for i
:= 0 to tused
-1 do
1026 if (Length(commands
[tcompidx
[i
]].help
) > 0) then
1028 g_Console_Add(' '+tcomplist
[i
]+' -- '+commands
[tcompidx
[i
]].help
);
1032 g_Console_Add(' '+tcomplist
[i
]);
1040 procedure g_Console_Control(K
: Word);
1044 if (Length(Line
) > 0) and (CPos
> 1) then
1046 Delete(Line
, CPos
-1, 1);
1050 if (Length(Line
) > 0) and (CPos
<= Length(Line
)) then
1051 Delete(Line
, CPos
, 1);
1055 IK_RIGHT
, IK_KPRIGHT
:
1056 if CPos
<= Length(Line
) then
1058 IK_RETURN
, IK_KPRETURN
:
1061 g_Console_Process(Line
)
1065 if (Length(Line
) > 0) and g_Game_IsNet
then
1069 if g_Game_IsClient
then
1070 MC_SEND_Chat(b_Text_Format(Line
), NET_CHAT_TEAM
)
1072 MH_SEND_Chat('[' + gPlayer1Settings
.name
+ ']: ' + b_Text_Format(Line
),
1073 NET_CHAT_TEAM
, gPlayer1Settings
.Team
);
1077 if g_Game_IsClient
then
1078 MC_SEND_Chat(b_Text_Format(Line
), NET_CHAT_PLAYER
)
1080 MH_SEND_Chat('[' + gPlayer1Settings
.name
+ ']: ' + b_Text_Format(Line
),
1088 gJustChatted
:= True;
1092 if not gChatShow
then
1095 if not gChatShow
then
1096 if (CommandHistory
<> nil) and
1097 (CmdIndex
< Length(CommandHistory
)) then
1099 if CmdIndex
< Length(CommandHistory
)-1 then
1100 CmdIndex
:= CmdIndex
+ 1;
1101 Line
:= CommandHistory
[CmdIndex
];
1102 CPos
:= Length(Line
) + 1;
1105 if not gChatShow
then
1106 if (CommandHistory
<> nil) and
1107 (CmdIndex
<= Length(CommandHistory
)) then
1109 if CmdIndex
> 0 then
1110 CmdIndex
:= CmdIndex
- 1;
1111 Line
:= CommandHistory
[CmdIndex
];
1112 Cpos
:= Length(Line
) + 1;
1114 IK_PAGEUP
, IK_KPPAGEUP
: // PgUp
1115 if not gChatShow
then Inc(conSkipLines
);
1116 IK_PAGEDN
, IK_KPPAGEDN
: // PgDown
1117 if not gChatShow
and (conSkipLines
> 0) then Dec(conSkipLines
);
1121 CPos
:= Length(Line
) + 1;
1125 function GetStr(var Str
: AnsiString): AnsiString;
1130 if Str
[1] = '"' then
1132 for b
:= 1 to Length(Str
) do
1133 if (b
= Length(Str
)) or (Str
[b
+1] = '"') then
1135 Result
:= Copy(Str
, 2, b
-1);
1136 Delete(Str
, 1, b
+1);
1142 for a
:= 1 to Length(Str
) do
1143 if (a
= Length(Str
)) or (Str
[a
+1] = ' ') then
1145 Result
:= Copy(Str
, 1, a
);
1146 Delete(Str
, 1, a
+1);
1152 function ParseString(Str
: AnsiString): SArray
;
1163 SetLength(Result
, Length(Result
)+1);
1164 Result
[High(Result
)] := GetStr(Str
);
1168 procedure g_Console_Add (L
: AnsiString; show
: Boolean=false);
1170 procedure conmsg (s
: AnsiString);
1174 if length(s
) = 0 then exit
;
1175 for a
:= 0 to High(MsgArray
) do
1187 for a
:= 0 to High(MsgArray
)-1 do MsgArray
[a
] := MsgArray
[a
+1];
1188 with MsgArray
[High(MsgArray
)] do
1198 // put it to console
1200 if (length(L
) = 0) or ((L
[length(L
)] <> #10) and (L
[length(L
)] <> #13)) then cbufPut(#10);
1202 // now show 'em out of console too
1203 show
:= show
and gAllowConsoleMessages
;
1204 if show
and gShowMessages
then
1206 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1207 while length(L
) > 0 do
1210 if f
<= 0 then f
:= length(L
)+1;
1211 conmsg(Copy(L
, 1, f
-1));
1216 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1217 //ConsoleHistory[High(ConsoleHistory)] := L;
1221 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1228 consolewriterLastWasEOL
: Boolean = false;
1230 procedure consolewriter (constref buf
; len
: SizeUInt
);
1234 if (len
< 1) then exit
;
1236 consolewriterLastWasEOL
:= (b
[len
-1] = 13) or (b
[len
-1] = 10);
1239 if (b
[0] <> 13) and (b
[0] <> 10) then
1241 cbufPut(AnsiChar(b
[0]));
1245 if (len
> 1) and (b
[0] = 13) then begin len
-= 1; b
+= 1; end;
1254 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1255 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1256 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1257 procedure conwriteln (const s
: AnsiString; show
: Boolean=false);
1259 g_Console_Add(s
, show
);
1263 procedure conwritefln (const s
: AnsiString; args
: array of const; show
: Boolean=false);
1267 g_Console_Add(formatstrf(s
, args
), true);
1271 consolewriterLastWasEOL
:= false;
1272 formatstrf(s
, args
, consolewriter
);
1273 if not consolewriterLastWasEOL
then cbufPut(#10);
1278 procedure g_Console_Clear();
1280 //ConsoleHistory := nil;
1285 procedure AddToHistory(L
: AnsiString);
1289 len
:= Length(CommandHistory
);
1292 (LowerCase(CommandHistory
[len
-1]) <> LowerCase(L
)) then
1294 SetLength(CommandHistory
, len
+1);
1295 CommandHistory
[len
] := L
;
1298 CmdIndex
:= Length(CommandHistory
);
1301 function g_Console_CommandBlacklisted(C
: AnsiString): Boolean;
1310 if Trim(C
) = '' then
1313 Arr
:= ParseString(C
);
1317 for i
:= 0 to High(Whitelist
) do
1318 if Whitelist
[i
] = LowerCase(Arr
[0]) then
1322 procedure g_Console_Process(L
: AnsiString; quiet
: Boolean = False);
1329 if Trim(L
) = '' then
1332 conSkipLines
:= 0; // "unscroll"
1334 if L
= 'goobers' then
1339 g_Console_Add('Your memory serves you well.');
1345 g_Console_Add('> '+L
);
1350 Arr
:= ParseString(L
);
1354 if commands
= nil then
1360 for i
:= 0 to High(commands
) do
1362 if commands
[i
].cmd
= LowerCase(Arr
[0]) then
1364 if assigned(commands
[i
].procEx
) then
1366 commands
[i
].procEx(@commands
[i
], Arr
);
1369 if assigned(commands
[i
].proc
) then
1371 commands
[i
].proc(Arr
);
1377 g_Console_Add(Format(_lc
[I_CONSOLE_UNKNOWN
], [Arr
[0]]));