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}
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
: SSArray
; 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; ahidden
: Boolean=false); overload
;
44 procedure conRegVar (const conname
: AnsiString; pvar
: PSingle; amin
, amax
: Single; const ahelp
: AnsiString; const amsg
: AnsiString; acheat
: Boolean=false; ahidden
: Boolean=false); overload
;
46 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
47 function conParseFloat (var res
: Single; const s
: AnsiString): Boolean;
51 gConsoleShow
: Boolean = false; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
52 gChatShow
: Boolean = false;
53 gChatTeam
: Boolean = false;
54 gAllowConsoleMessages
: Boolean = true;
55 gChatEnter
: Boolean = true;
56 gJustChatted
: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
62 g_textures
, g_main
, e_graphics
, e_input
, g_game
,
63 SysUtils
, g_basic
, g_options
, Math
,
64 g_menu
, g_language
, g_net
, g_netmsg
, e_log
, conbuf
;
70 TCmdProc
= procedure (p
: SSArray
);
71 TCmdProcEx
= procedure (me
: PCommand
; p
: SSArray
);
79 ptr
: Pointer; // various data
80 msg
: AnsiString; // message for var changes
94 MaxScriptRecursion
= 16;
96 DEBUG_STRING
= 'DEBUG MODE';
100 RecursionDepth
: Word = 0;
101 RecursionLimitHit
: Boolean = False;
103 Cons_Shown
: Boolean; // Ðèñîâàòü ëè êîíñîëü?
106 //ConsoleHistory: SSArray;
107 CommandHistory
: SSArray
;
109 commands
: Array of TCommand
= nil;
110 Aliases
: Array of TAlias
= nil;
112 conSkipLines
: Integer = 0;
113 MsgArray
: Array [0..4] of record
119 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
120 function conParseFloat (var res
: Single; const s
: AnsiString): Boolean;
129 while (slen
> 0) and (s
[slen
] <= ' ') do Dec(slen
);
130 while (pos
<= slen
) and (s
[pos
] <= ' ') do Inc(pos
);
131 if (pos
> slen
) then exit
;
132 if (slen
-pos
= 1) and (s
[pos
] = '.') then exit
; // single dot
134 while (pos
<= slen
) do
136 if (s
[pos
] < '0') or (s
[pos
] > '9') then break
;
137 res
:= res
*10+Byte(s
[pos
])-48;
140 if (pos
<= slen
) then
143 if (s
[pos
] <> '.') then exit
;
145 while (pos
<= slen
) do
147 if (s
[pos
] < '0') or (s
[pos
] > '9') then break
;
149 res
+= frac
*(Byte(s
[pos
])-48);
153 if (pos
<= slen
) then exit
; // oops
158 // ////////////////////////////////////////////////////////////////////////// //
159 // <0: no arg; 0/1: true/false; 666: toggle
160 function conGetBoolArg (p
: SSArray
; idx
: Integer): Integer;
162 if (idx
< 0) or (idx
> High(p
)) then begin result
:= -1; exit
; end;
164 if (p
[idx
] = '1') or (CompareText(p
[idx
], 'on') = 0) or (CompareText(p
[idx
], 'true') = 0) or
165 (CompareText(p
[idx
], 'tan') = 0) or (CompareText(p
[idx
], 'yes') = 0) then result
:= 1
166 else if (CompareText(p
[idx
], 'toggle') = 0) or (CompareText(p
[idx
], 'switch') = 0) or
167 (CompareText(p
[idx
], 't') = 0) then result
:= 666;
171 procedure boolVarHandler (me
: PCommand
; p
: SSArray
);
172 procedure binaryFlag (var flag
: Boolean; msg
: AnsiString);
174 if (Length(p
) > 2) then
176 conwritefln('too many arguments to ''%s''', [p
[0]]);
180 case conGetBoolArg(p
, 1) of
182 0: if not me
.cheat
or conIsCheatsEnabled
then flag
:= false else begin conwriteln('not available'); exit
; end;
183 1: if not me
.cheat
or conIsCheatsEnabled
then flag
:= true else begin conwriteln('not available'); exit
; end;
184 666: if not me
.cheat
or conIsCheatsEnabled
then flag
:= not flag
else begin conwriteln('not available'); exit
; end;
186 if (Length(msg
) = 0) then msg
:= p
[0] else msg
+= ':';
187 if flag
then conwritefln('%s tan', [msg
]) else conwritefln('%s ona', [msg
]);
191 binaryFlag(PBoolean(me
.ptr
)^, me
.msg
);
195 procedure conRegVar (const conname
: AnsiString; pvar
: PBoolean; const ahelp
: AnsiString; const amsg
: AnsiString; acheat
: Boolean=false; ahidden
: Boolean=false); overload
;
200 f
:= Length(commands
);
201 SetLength(commands
, f
+1);
203 cp
.cmd
:= LowerCase(conname
);
205 cp
.procEx
:= boolVarHandler
;
207 cp
.hidden
:= ahidden
;
214 // ////////////////////////////////////////////////////////////////////////// //
216 PVarSingle
= ^TVarSingle
;
219 min
, max
, def
: Single; // default will be starting value
223 procedure singleVarHandler (me
: PCommand
; p
: SSArray
);
229 if (Length(p
) > 2) then
231 conwritefln('too many arguments to ''%s''', [me
.cmd
]);
234 pv
:= PVarSingle(me
.ptr
);
235 if (Length(p
) = 2) then
237 if me
.cheat
and (not conIsCheatsEnabled
) then begin conwriteln('not available'); exit
; end;
238 if (CompareText(p
[1], 'default') = 0) or (CompareText(p
[1], 'def') = 0) or
239 (CompareText(p
[1], 'd') = 0) or (CompareText(p
[1], 'off') = 0) or
240 (CompareText(p
[1], 'ona') = 0) then
246 if not conParseFloat(nv
, p
[1]) then
248 conwritefln('%s: ''%s'' doesn''t look like a floating number', [me
.cmd
, p
[1]]);
251 if (nv
< pv
.min
) then nv
:= pv
.min
;
252 if (nv
> pv
.max
) then nv
:= pv
.max
;
257 if (Length(msg
) = 0) then msg
:= me
.cmd
else msg
+= ':';
258 conwritefln('%s %s', [msg
, pv
.val
^]);
262 procedure conRegVar (const conname
: AnsiString; pvar
: PSingle; amin
, amax
: Single; const ahelp
: AnsiString; const amsg
: AnsiString; acheat
: Boolean=false; ahidden
: Boolean=false); overload
;
268 GetMem(pv
, sizeof(pv
^));
273 f
:= Length(commands
);
274 SetLength(commands
, f
+1);
276 cp
.cmd
:= LowerCase(conname
);
278 cp
.procEx
:= singleVarHandler
;
280 cp
.hidden
:= ahidden
;
287 // ////////////////////////////////////////////////////////////////////////// //
288 function GetStrACmd(var Str
: AnsiString): AnsiString;
293 for a
:= 1 to Length(Str
) do
294 if (a
= Length(Str
)) or (Str
[a
+1] = ';') then
296 Result
:= Copy(Str
, 1, a
);
303 function ParseAlias(Str
: AnsiString): SSArray
;
314 SetLength(Result
, Length(Result
)+1);
315 Result
[High(Result
)] := GetStrACmd(Str
);
319 procedure ConsoleCommands(p
: SSArray
);
325 cmd
:= LowerCase(p
[0]);
328 if cmd
= 'clear' then
330 //ConsoleHistory := nil;
334 for a
:= 0 to High(MsgArray
) do
342 if cmd
= 'clearhistory' then
343 CommandHistory
:= nil;
345 if cmd
= 'showhistory' then
346 if CommandHistory
<> nil then
349 for a
:= 0 to High(CommandHistory
) do
350 g_Console_Add(' '+CommandHistory
[a
]);
353 if cmd
= 'commands' then
356 g_Console_Add('commands list:');
357 for a
:= High(commands
) downto 0 do
359 if (Length(commands
[a
].help
) > 0) then
361 g_Console_Add(' '+commands
[a
].cmd
+' -- '+commands
[a
].help
);
365 g_Console_Add(' '+commands
[a
].cmd
);
371 g_Console_Add(TimeToStr(Now
), True);
374 g_Console_Add(DateToStr(Now
), True);
377 if Length(p
) > 1 then
379 if p
[1] = 'ololo' then
384 for a
:= 1 to High(p
) do
386 g_Console_Add(b_Text_Format(s
), True);
395 if ConsoleHistory <> nil then
397 if Length(P) > 1 then
400 s := GameDir+'/console.txt';
405 if IOResult <> 0 then
407 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
412 for a := 0 to High(ConsoleHistory) do
413 WriteLn(F, ConsoleHistory[a]);
416 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
425 if Length(p
) > 1 then
427 s
:= GameDir
+'/'+p
[1];
432 if IOResult
<> 0 then
434 g_Console_Add(Format(_lc
[I_CONSOLE_ERROR_READ
], [s
]));
438 g_Console_Add(Format(_lc
[I_CONSOLE_EXEC
], [s
]));
443 if IOResult
<> 0 then
445 g_Console_Add(Format(_lc
[I_CONSOLE_ERROR_READ
], [s
]));
449 if Pos('#', s
) <> 1 then // script comment
451 // prevents endless loops
453 RecursionLimitHit
:= (RecursionDepth
> MaxScriptRecursion
) or RecursionLimitHit
;
454 if not RecursionLimitHit
then
455 g_Console_Process(s
, True);
459 if (RecursionDepth
= 0) and RecursionLimitHit
then
461 g_Console_Add(Format(_lc
[I_CONSOLE_ERROR_CALL
], [s
]));
462 RecursionLimitHit
:= False;
469 g_Console_Add('exec <script file>');
472 if cmd
= 'alias' then
474 // alias [alias_name] [commands]
475 if Length(p
) > 1 then
477 for a
:= 0 to High(Aliases
) do
478 if Aliases
[a
].name
= p
[1] then
480 if Length(p
) > 2 then
481 Aliases
[a
].commands
:= ParseAlias(p
[2])
483 for b
:= 0 to High(Aliases
[a
].commands
) do
484 g_Console_Add(Aliases
[a
].commands
[b
]);
487 SetLength(Aliases
, Length(Aliases
)+1);
489 Aliases
[a
].name
:= p
[1];
490 if Length(p
) > 2 then
491 Aliases
[a
].commands
:= ParseAlias(p
[2])
493 for b
:= 0 to High(Aliases
[a
].commands
) do
494 g_Console_Add(Aliases
[a
].commands
[b
]);
496 for a
:= 0 to High(Aliases
) do
497 if Aliases
[a
].commands
<> nil then
498 g_Console_Add(Aliases
[a
].name
);
504 if Length(p
) > 1 then
506 if Aliases
= nil then
508 for a
:= 0 to High(Aliases
) do
509 if Aliases
[a
].name
= p
[1] then
511 if Aliases
[a
].commands
<> nil then
513 // with this system proper endless loop detection seems either impossible
514 // or very dirty to implement, so let's have this instead
515 // prevents endless loops
516 for b
:= 0 to High(Aliases
[a
].commands
) do
519 RecursionLimitHit
:= (RecursionDepth
> MaxScriptRecursion
) or RecursionLimitHit
;
520 if not RecursionLimitHit
then
521 g_Console_Process(Aliases
[a
].commands
[b
], True);
524 if (RecursionDepth
= 0) and RecursionLimitHit
then
526 g_Console_Add(Format(_lc
[I_CONSOLE_ERROR_CALL
], [s
]));
527 RecursionLimitHit
:= False;
534 g_Console_Add('call <alias name>');
538 procedure WhitelistCommand(cmd
: AnsiString);
542 SetLength(Whitelist
, Length(Whitelist
)+1);
543 a
:= High(Whitelist
);
544 Whitelist
[a
] := LowerCase(cmd
);
547 procedure AddCommand(cmd
: AnsiString; proc
: TCmdProc
; ahelp
: AnsiString=''; ahidden
: Boolean=false; acheat
: Boolean=false);
552 SetLength(commands
, Length(commands
)+1);
555 cp
.cmd
:= LowerCase(cmd
);
559 cp
.hidden
:= ahidden
;
566 procedure segfault (p
: SSArray
);
574 procedure g_Console_Init();
578 g_Texture_CreateWAD(ID
, GameWAD
+':TEXTURES\CONSOLE');
579 Cons_Y
:= -(gScreenHeight
div 2);
580 gConsoleShow
:= False;
585 for a
:= 0 to High(MsgArray
) do
592 AddCommand('segfault', segfault
, 'make segfault');
594 AddCommand('clear', ConsoleCommands
, 'clear console');
595 AddCommand('clearhistory', ConsoleCommands
);
596 AddCommand('showhistory', ConsoleCommands
);
597 AddCommand('commands', ConsoleCommands
);
598 AddCommand('time', ConsoleCommands
);
599 AddCommand('date', ConsoleCommands
);
600 AddCommand('echo', ConsoleCommands
);
601 AddCommand('dump', ConsoleCommands
);
602 AddCommand('exec', ConsoleCommands
);
603 AddCommand('alias', ConsoleCommands
);
604 AddCommand('call', ConsoleCommands
);
606 AddCommand('d_window', DebugCommands
);
607 AddCommand('d_sounds', DebugCommands
);
608 AddCommand('d_frames', DebugCommands
);
609 AddCommand('d_winmsg', DebugCommands
);
610 AddCommand('d_monoff', DebugCommands
);
611 AddCommand('d_botoff', DebugCommands
);
612 AddCommand('d_monster', DebugCommands
);
613 AddCommand('d_health', DebugCommands
);
614 AddCommand('d_player', DebugCommands
);
615 AddCommand('d_joy', DebugCommands
);
616 AddCommand('d_mem', DebugCommands
);
618 AddCommand('p1_name', GameCVars
);
619 AddCommand('p2_name', GameCVars
);
620 AddCommand('p1_color', GameCVars
);
621 AddCommand('p2_color', GameCVars
);
622 AddCommand('r_showfps', GameCVars
);
623 AddCommand('r_showtime', GameCVars
);
624 AddCommand('r_showscore', GameCVars
);
625 AddCommand('r_showlives', GameCVars
);
626 AddCommand('r_showstat', GameCVars
);
627 AddCommand('r_showkillmsg', GameCVars
);
628 AddCommand('r_showspect', GameCVars
);
629 AddCommand('r_showping', GameCVars
);
630 AddCommand('g_gamemode', GameCVars
);
631 AddCommand('g_friendlyfire', GameCVars
);
632 AddCommand('g_weaponstay', GameCVars
);
633 AddCommand('g_allow_exit', GameCVars
);
634 AddCommand('g_allow_monsters', GameCVars
);
635 AddCommand('g_bot_vsmonsters', GameCVars
);
636 AddCommand('g_bot_vsplayers', GameCVars
);
637 AddCommand('g_scorelimit', GameCVars
);
638 AddCommand('g_timelimit', GameCVars
);
639 AddCommand('g_maxlives', GameCVars
);
640 AddCommand('g_warmuptime', GameCVars
);
641 AddCommand('net_interp', GameCVars
);
642 AddCommand('net_forceplayerupdate', GameCVars
);
643 AddCommand('net_predictself', GameCVars
);
644 AddCommand('sv_name', GameCVars
);
645 AddCommand('sv_passwd', GameCVars
);
646 AddCommand('sv_maxplrs', GameCVars
);
647 AddCommand('sv_public', GameCVars
);
648 AddCommand('sv_intertime', GameCVars
);
650 AddCommand('quit', GameCommands
);
651 AddCommand('exit', GameCommands
);
652 AddCommand('pause', GameCommands
);
653 AddCommand('endgame', GameCommands
);
654 AddCommand('restart', GameCommands
);
655 AddCommand('addbot', GameCommands
);
656 AddCommand('bot_add', GameCommands
);
657 AddCommand('bot_addlist', GameCommands
);
658 AddCommand('bot_addred', GameCommands
);
659 AddCommand('bot_addblue', GameCommands
);
660 AddCommand('bot_removeall', GameCommands
);
661 AddCommand('chat', GameCommands
);
662 AddCommand('teamchat', GameCommands
);
663 AddCommand('game', GameCommands
);
664 AddCommand('host', GameCommands
);
665 AddCommand('map', GameCommands
);
666 AddCommand('nextmap', GameCommands
);
667 AddCommand('endmap', GameCommands
);
668 AddCommand('goodbye', GameCommands
);
669 AddCommand('suicide', GameCommands
);
670 AddCommand('spectate', GameCommands
);
671 AddCommand('ready', GameCommands
);
672 AddCommand('kick', GameCommands
);
673 AddCommand('kick_id', GameCommands
);
674 AddCommand('ban', GameCommands
);
675 AddCommand('permban', GameCommands
);
676 AddCommand('ban_id', GameCommands
);
677 AddCommand('permban_id', GameCommands
);
678 AddCommand('unban', GameCommands
);
679 AddCommand('connect', GameCommands
);
680 AddCommand('disconnect', GameCommands
);
681 AddCommand('reconnect', GameCommands
);
682 AddCommand('say', GameCommands
);
683 AddCommand('tell', GameCommands
);
684 AddCommand('overtime', GameCommands
);
685 AddCommand('rcon_password', GameCommands
);
686 AddCommand('rcon', GameCommands
);
687 AddCommand('callvote', GameCommands
);
688 AddCommand('vote', GameCommands
);
689 AddCommand('clientlist', GameCommands
);
690 AddCommand('event', GameCommands
);
692 AddCommand('god', GameCheats
);
693 AddCommand('notarget', GameCheats
);
694 AddCommand('give', GameCheats
); // "exit" too ;-)
695 AddCommand('open', GameCheats
);
696 AddCommand('fly', GameCheats
);
697 AddCommand('noclip', GameCheats
);
698 AddCommand('speedy', GameCheats
);
699 AddCommand('jumpy', GameCheats
);
700 AddCommand('noreload', GameCheats
);
701 AddCommand('aimline', GameCheats
);
702 AddCommand('automap', GameCheats
);
704 WhitelistCommand('say');
705 WhitelistCommand('tell');
706 WhitelistCommand('overtime');
707 WhitelistCommand('ready');
708 WhitelistCommand('map');
709 WhitelistCommand('nextmap');
710 WhitelistCommand('endmap');
711 WhitelistCommand('restart');
712 WhitelistCommand('kick');
713 WhitelistCommand('ban');
715 WhitelistCommand('addbot');
716 WhitelistCommand('bot_add');
717 WhitelistCommand('bot_addred');
718 WhitelistCommand('bot_addblue');
719 WhitelistCommand('bot_removeall');
721 WhitelistCommand('g_gamemode');
722 WhitelistCommand('g_friendlyfire');
723 WhitelistCommand('g_weaponstay');
724 WhitelistCommand('g_allow_exit');
725 WhitelistCommand('g_allow_monsters');
726 WhitelistCommand('g_scorelimit');
727 WhitelistCommand('g_timelimit');
729 g_Console_Add(Format(_lc
[I_CONSOLE_WELCOME
], [GAME_VERSION
]));
733 procedure g_Console_Update();
739 // Â ïðîöåññå îòêðûòèÿ:
740 if gConsoleShow
and (Cons_Y
< 0) then
742 Cons_Y
:= Cons_Y
+Step
;
745 // Â ïðîöåññå çàêðûòèÿ:
746 if (not gConsoleShow
) and
747 (Cons_Y
> -(gScreenHeight
div 2)) then
748 Cons_Y
:= Cons_Y
-Step
;
750 // Îêîí÷àòåëüíî îòêðûëàñü:
754 // Îêîí÷àòåëüíî çàêðûëàñü:
755 if Cons_Y
<= (-(gScreenHeight
div 2)) then
757 Cons_Y
:= -(gScreenHeight
div 2);
763 while a
<= High(MsgArray
) do
765 if MsgArray
[a
].Time
> 0 then
767 if MsgArray
[a
].Time
= 1 then
769 if a
< High(MsgArray
) then
771 for b
:= a
to High(MsgArray
)-1 do
772 MsgArray
[b
] := MsgArray
[b
+1];
774 MsgArray
[High(MsgArray
)].Time
:= 0;
780 Dec(MsgArray
[a
].Time
);
788 procedure drawConsoleText ();
790 CWidth
, CHeight
: Byte;
795 procedure putLine (sp
, ep
: LongWord);
804 cw
:= e_TextureFontCharWidth(cbufAt(p
), gStdFont
);
805 if wdt
+cw
> gScreenWidth
-8 then break
;
806 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
810 if p
<> ep
then putLine(p
, ep
); // do rest of the line first
811 // now print our part
819 cw
:= e_TextureFontCharWidth(cbufAt(p
), gStdFont
);
820 e_TextureFontPrintCharEx(wdt
, ty
, cbufAt(p
), gStdFont
);
833 e_TextureFontGetSize(gStdFont
, CWidth
, CHeight
);
834 ty
:= (gScreenHeight
div 2)-4-2*CHeight
-Abs(Cons_Y
);
835 skip
:= conSkipLines
;
836 cbufLastLine(sp
, ep
);
839 if ty
+CHeight
<= 0 then break
;
840 until not cbufLineUp(sp
, ep
);
843 procedure g_Console_Draw();
845 CWidth
, CHeight
: Byte;
849 e_TextureFontGetSize(gStdFont
, CWidth
, CHeight
);
851 for a
:= 0 to High(MsgArray
) do
852 if MsgArray
[a
].Time
> 0 then
853 e_TextureFontPrintFmt(0, CHeight
*a
, MsgArray
[a
].Msg
,
856 if not Cons_Shown
then
862 e_TextureFontPrintEx(0, gScreenHeight
- CHeight
- 1, 'say team> ' + Line
,
863 gStdFont
, 255, 255, 255, 1, True);
864 e_TextureFontPrintEx((CPos
+ 9)*CWidth
, gScreenHeight
- CHeight
- 1, '_',
865 gStdFont
, 255, 255, 255, 1, True);
869 e_TextureFontPrintEx(0, gScreenHeight
- CHeight
- 1, 'say> ' + Line
,
870 gStdFont
, 255, 255, 255, 1, True);
871 e_TextureFontPrintEx((CPos
+ 4)*CWidth
, gScreenHeight
- CHeight
- 1, '_',
872 gStdFont
, 255, 255, 255, 1, True);
880 e_CharFont_GetSize(gMenuFont
, DEBUG_STRING
, mfW
, mfH
);
881 a
:= (gScreenWidth
- 2*mfW
) div 2;
882 b
:= Cons_Y
+ ((gScreenHeight
div 2) - 2*mfH
) div 2;
883 e_CharFont_PrintEx(gMenuFont
, a
div 2, b
div 2, DEBUG_STRING
,
884 _RGB(128, 0, 0), 2.0);
887 e_DrawSize(ID
, 0, Cons_Y
, Alpha
, False, False, gScreenWidth
, gScreenHeight
div 2);
888 e_TextureFontPrint(0, Cons_Y
+(gScreenHeight
div 2)-CHeight
-4, '> '+Line
, gStdFont
);
892 if ConsoleHistory <> nil then
896 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
897 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
899 b := Max(b-Offset, 0);
900 d := Max(High(ConsoleHistory)-Offset, 0);
903 for a := d downto b do
905 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
912 e_TextureFontPrint((CPos
+1)*CWidth
, Cons_Y
+(gScreenHeight
div 2)-21, '_', gStdFont
);
915 procedure g_Console_Switch();
917 if gChatShow
then Exit
;
918 gConsoleShow
:= not gConsoleShow
;
922 procedure g_Console_Chat_Switch(Team
: Boolean = False);
924 if gConsoleShow
then Exit
;
925 if not g_Game_IsNet
then Exit
;
926 gChatShow
:= not gChatShow
;
934 procedure g_Console_Char(C
: AnsiChar);
936 if gChatShow
and (not gChatEnter
) then
938 Insert(C
, Line
, CPos
);
944 tcomplist
: array of AnsiString = nil;
945 tcompidx
: array of Integer = nil;
947 procedure Complete ();
951 ll
, lpfx
, cmd
: AnsiString;
953 if (Length(Line
) = 0) then
956 for i
:= 0 to High(commands
) do
958 // hidden commands are hidden when cheats aren't enabled
959 if commands
[i
].hidden
and not conIsCheatsEnabled
then continue
;
960 if (Length(commands
[i
].help
) > 0) then
962 g_Console_Add(' '+commands
[i
].cmd
+' -- '+commands
[i
].help
);
966 g_Console_Add(' '+commands
[i
].cmd
);
972 ll
:= LowerCase(Line
);
975 if (Length(ll
) > 1) and (ll
[Length(ll
)] = ' ') then
977 ll
:= Copy(ll
, 0, Length(ll
)-1);
978 for i
:= 0 to High(commands
) do
980 // hidden commands are hidden when cheats aren't enabled
981 if commands
[i
].hidden
and not conIsCheatsEnabled
then continue
;
982 if (commands
[i
].cmd
= ll
) then
984 if (Length(commands
[i
].help
) > 0) then
986 g_Console_Add(' '+commands
[i
].cmd
+' -- '+commands
[i
].help
);
993 // build completion list
995 for i
:= 0 to High(commands
) do
997 // hidden commands are hidden when cheats aren't enabled
998 if commands
[i
].hidden
and not conIsCheatsEnabled
then continue
;
999 cmd
:= commands
[i
].cmd
;
1000 if (Length(cmd
) >= Length(ll
)) and (ll
= Copy(cmd
, 0, Length(ll
))) then
1002 if (tused
= Length(tcomplist
)) then
1004 SetLength(tcomplist
, Length(tcomplist
)+128);
1005 SetLength(tcompidx
, Length(tcompidx
)+128);
1007 tcomplist
[tused
] := cmd
;
1008 tcompidx
[tused
] := i
;
1010 if (Length(cmd
) > Length(lpfx
)) then lpfx
:= cmd
;
1014 // get longest prefix
1015 for i
:= 0 to tused
-1 do
1017 cmd
:= tcomplist
[i
];
1018 for c
:= 1 to Length(lpfx
) do
1020 if (c
> Length(cmd
)) then break
;
1021 if (cmd
[c
] <> lpfx
[c
]) then begin lpfx
:= Copy(lpfx
, 0, c
-1); break
; end;
1025 if (tused
= 0) then exit
;
1029 Line
:= tcomplist
[0]+' ';
1030 CPos
:= Length(Line
)+1;
1034 // has longest prefix?
1035 if (Length(lpfx
) > Length(ll
)) then
1038 CPos
:= Length(Line
)+1;
1043 for i
:= 0 to tused
-1 do
1045 if (Length(commands
[tcompidx
[i
]].help
) > 0) then
1047 g_Console_Add(' '+tcomplist
[i
]+' -- '+commands
[tcompidx
[i
]].help
);
1051 g_Console_Add(' '+tcomplist
[i
]);
1059 procedure g_Console_Control(K
: Word);
1063 if (Length(Line
) > 0) and (CPos
> 1) then
1065 Delete(Line
, CPos
-1, 1);
1069 if (Length(Line
) > 0) and (CPos
<= Length(Line
)) then
1070 Delete(Line
, CPos
, 1);
1074 IK_RIGHT
, IK_KPRIGHT
:
1075 if CPos
<= Length(Line
) then
1077 IK_RETURN
, IK_KPRETURN
:
1080 g_Console_Process(Line
)
1084 if (Length(Line
) > 0) and g_Game_IsNet
then
1088 if g_Game_IsClient
then
1089 MC_SEND_Chat(b_Text_Format(Line
), NET_CHAT_TEAM
)
1091 MH_SEND_Chat('[' + gPlayer1Settings
.name
+ ']: ' + b_Text_Format(Line
),
1092 NET_CHAT_TEAM
, gPlayer1Settings
.Team
);
1096 if g_Game_IsClient
then
1097 MC_SEND_Chat(b_Text_Format(Line
), NET_CHAT_PLAYER
)
1099 MH_SEND_Chat('[' + gPlayer1Settings
.name
+ ']: ' + b_Text_Format(Line
),
1107 gJustChatted
:= True;
1111 if not gChatShow
then
1114 if not gChatShow
then
1115 if (CommandHistory
<> nil) and
1116 (CmdIndex
< Length(CommandHistory
)) then
1118 if CmdIndex
< Length(CommandHistory
)-1 then
1119 CmdIndex
:= CmdIndex
+ 1;
1120 Line
:= CommandHistory
[CmdIndex
];
1121 CPos
:= Length(Line
) + 1;
1124 if not gChatShow
then
1125 if (CommandHistory
<> nil) and
1126 (CmdIndex
<= Length(CommandHistory
)) then
1128 if CmdIndex
> 0 then
1129 CmdIndex
:= CmdIndex
- 1;
1130 Line
:= CommandHistory
[CmdIndex
];
1131 Cpos
:= Length(Line
) + 1;
1133 IK_PAGEUP
, IK_KPPAGEUP
: // PgUp
1134 if not gChatShow
then Inc(conSkipLines
);
1135 IK_PAGEDN
, IK_KPPAGEDN
: // PgDown
1136 if not gChatShow
and (conSkipLines
> 0) then Dec(conSkipLines
);
1140 CPos
:= Length(Line
) + 1;
1144 function GetStr(var Str
: AnsiString): AnsiString;
1149 if Str
[1] = '"' then
1151 for b
:= 1 to Length(Str
) do
1152 if (b
= Length(Str
)) or (Str
[b
+1] = '"') then
1154 Result
:= Copy(Str
, 2, b
-1);
1155 Delete(Str
, 1, b
+1);
1161 for a
:= 1 to Length(Str
) do
1162 if (a
= Length(Str
)) or (Str
[a
+1] = ' ') then
1164 Result
:= Copy(Str
, 1, a
);
1165 Delete(Str
, 1, a
+1);
1171 function ParseString(Str
: AnsiString): SSArray
;
1182 SetLength(Result
, Length(Result
)+1);
1183 Result
[High(Result
)] := GetStr(Str
);
1187 procedure g_Console_Add (L
: AnsiString; show
: Boolean=false);
1189 procedure conmsg (s
: AnsiString);
1193 if length(s
) = 0 then exit
;
1194 for a
:= 0 to High(MsgArray
) do
1206 for a
:= 0 to High(MsgArray
)-1 do MsgArray
[a
] := MsgArray
[a
+1];
1207 with MsgArray
[High(MsgArray
)] do
1217 // put it to console
1219 if (length(L
) = 0) or ((L
[length(L
)] <> #10) and (L
[length(L
)] <> #13)) then cbufPut(#10);
1221 // now show 'em out of console too
1222 show
:= show
and gAllowConsoleMessages
;
1223 if show
and gShowMessages
then
1225 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1226 while length(L
) > 0 do
1229 if f
<= 0 then f
:= length(L
)+1;
1230 conmsg(Copy(L
, 1, f
-1));
1235 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1236 //ConsoleHistory[High(ConsoleHistory)] := L;
1240 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1247 consolewriterLastWasEOL
: Boolean = false;
1249 procedure consolewriter (constref buf
; len
: SizeUInt
);
1253 if (len
< 1) then exit
;
1255 consolewriterLastWasEOL
:= (b
[len
-1] = 13) or (b
[len
-1] = 10);
1258 if (b
[0] <> 13) and (b
[0] <> 10) then
1260 cbufPut(AnsiChar(b
[0]));
1264 if (len
> 1) and (b
[0] = 13) then begin len
-= 1; b
+= 1; end;
1273 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1274 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1275 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1276 procedure conwriteln (const s
: AnsiString; show
: Boolean=false);
1278 g_Console_Add(s
, show
);
1282 procedure conwritefln (const s
: AnsiString; args
: array of const; show
: Boolean=false);
1286 g_Console_Add(formatstrf(s
, args
), true);
1290 consolewriterLastWasEOL
:= false;
1291 formatstrf(s
, args
, consolewriter
);
1292 if not consolewriterLastWasEOL
then cbufPut(#10);
1297 procedure g_Console_Clear();
1299 //ConsoleHistory := nil;
1304 procedure AddToHistory(L
: AnsiString);
1308 len
:= Length(CommandHistory
);
1311 (LowerCase(CommandHistory
[len
-1]) <> LowerCase(L
)) then
1313 SetLength(CommandHistory
, len
+1);
1314 CommandHistory
[len
] := L
;
1317 CmdIndex
:= Length(CommandHistory
);
1320 function g_Console_CommandBlacklisted(C
: AnsiString): Boolean;
1329 if Trim(C
) = '' then
1332 Arr
:= ParseString(C
);
1336 for i
:= 0 to High(Whitelist
) do
1337 if Whitelist
[i
] = LowerCase(Arr
[0]) then
1341 procedure g_Console_Process(L
: AnsiString; quiet
: Boolean = False);
1348 if Trim(L
) = '' then
1351 conSkipLines
:= 0; // "unscroll"
1353 if L
= 'goobers' then
1358 g_Console_Add('Your memory serves you well.');
1364 g_Console_Add('> '+L
);
1369 Arr
:= ParseString(L
);
1373 if commands
= nil then
1379 for i
:= 0 to High(commands
) do
1381 if commands
[i
].cmd
= LowerCase(Arr
[0]) then
1383 if assigned(commands
[i
].procEx
) then
1385 commands
[i
].procEx(@commands
[i
], Arr
);
1388 if assigned(commands
[i
].proc
) then
1390 commands
[i
].proc(Arr
);
1396 g_Console_Add(Format(_lc
[I_CONSOLE_UNKNOWN
], [Arr
[0]]));