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
;
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
, utils
;
70 TCmdProc
= procedure (p
: SArray
);
71 TCmdProcEx
= procedure (me
: PCommand
; p
: SArray
);
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: SArray;
107 CommandHistory
: SArray
;
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
: SArray
; 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
: SArray
);
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 flag
then conwritefln('%s: tan', [msg
]) else conwritefln('%s: ona', [msg
]);
190 binaryFlag(PBoolean(me
.ptr
)^, me
.msg
);
194 procedure conRegVar (const conname
: AnsiString; pvar
: PBoolean; const ahelp
: AnsiString; const amsg
: AnsiString; acheat
: Boolean=false); overload
;
199 f
:= Length(commands
);
200 SetLength(commands
, f
+1);
202 cp
.cmd
:= LowerCase(conname
);
204 cp
.procEx
:= boolVarHandler
;
213 // ////////////////////////////////////////////////////////////////////////// //
215 PVarSingle
= ^TVarSingle
;
218 min
, max
, def
: Single; // default will be starting value
222 procedure singleVarHandler (me
: PCommand
; p
: SArray
);
228 if (Length(p
) > 2) then
230 conwritefln('too many arguments to ''%s''', [p
[0]]);
233 pv
:= PVarSingle(me
.ptr
);
234 if (Length(p
) = 2) then
236 if me
.cheat
and (not conIsCheatsEnabled
) then begin conwriteln('not available'); exit
; end;
237 if (CompareText(p
[1], 'default') = 0) or (CompareText(p
[1], 'def') = 0) or
238 (CompareText(p
[1], 'd') = 0) or (CompareText(p
[1], 'off') = 0) or
239 (CompareText(p
[1], 'ona') = 0) then
245 if not conParseFloat(nv
, p
[1]) then
247 conwritefln('%s: ''%s'' doesn''t look like a floating number', [p
[0], p
[1]]);
250 if (nv
< pv
.min
) then nv
:= pv
.min
;
251 if (nv
> pv
.max
) then nv
:= pv
.max
;
256 if (Length(msg
) = 0) then msg
:= p
[0] else msg
+= ':';
257 conwritefln('%s %s', [msg
, pv
.val
^]);
261 procedure conRegVar (const conname
: AnsiString; pvar
: PSingle; amin
, amax
: Single; const ahelp
: AnsiString; const amsg
: AnsiString; acheat
: Boolean=false); overload
;
267 GetMem(pv
, sizeof(pv
^));
272 f
:= Length(commands
);
273 SetLength(commands
, f
+1);
275 cp
.cmd
:= LowerCase(conname
);
277 cp
.procEx
:= singleVarHandler
;
286 // ////////////////////////////////////////////////////////////////////////// //
287 function GetStrACmd(var Str
: AnsiString): AnsiString;
292 for a
:= 1 to Length(Str
) do
293 if (a
= Length(Str
)) or (Str
[a
+1] = ';') then
295 Result
:= Copy(Str
, 1, a
);
302 function ParseAlias(Str
: AnsiString): SArray
;
313 SetLength(Result
, Length(Result
)+1);
314 Result
[High(Result
)] := GetStrACmd(Str
);
318 procedure ConsoleCommands(p
: SArray
);
324 cmd
:= LowerCase(p
[0]);
327 if cmd
= 'clear' then
329 //ConsoleHistory := nil;
333 for a
:= 0 to High(MsgArray
) do
341 if cmd
= 'clearhistory' then
342 CommandHistory
:= nil;
344 if cmd
= 'showhistory' then
345 if CommandHistory
<> nil then
348 for a
:= 0 to High(CommandHistory
) do
349 g_Console_Add(' '+CommandHistory
[a
]);
352 if cmd
= 'commands' then
355 g_Console_Add('commands list:');
356 for a
:= High(commands
) downto 0 do
358 if (Length(commands
[a
].help
) > 0) then
360 g_Console_Add(' '+commands
[a
].cmd
+' -- '+commands
[a
].help
);
364 g_Console_Add(' '+commands
[a
].cmd
);
370 g_Console_Add(TimeToStr(Now
), True);
373 g_Console_Add(DateToStr(Now
), True);
376 if Length(p
) > 1 then
378 if p
[1] = 'ololo' then
383 for a
:= 1 to High(p
) do
385 g_Console_Add(b_Text_Format(s
), True);
394 if ConsoleHistory <> nil then
396 if Length(P) > 1 then
399 s := GameDir+'/console.txt';
404 if IOResult <> 0 then
406 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
411 for a := 0 to High(ConsoleHistory) do
412 WriteLn(F, ConsoleHistory[a]);
415 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
424 if Length(p
) > 1 then
426 s
:= GameDir
+'/'+p
[1];
431 if IOResult
<> 0 then
433 g_Console_Add(Format(_lc
[I_CONSOLE_ERROR_READ
], [s
]));
437 g_Console_Add(Format(_lc
[I_CONSOLE_EXEC
], [s
]));
442 if IOResult
<> 0 then
444 g_Console_Add(Format(_lc
[I_CONSOLE_ERROR_READ
], [s
]));
448 if Pos('#', s
) <> 1 then // script comment
450 // prevents endless loops
452 RecursionLimitHit
:= (RecursionDepth
> MaxScriptRecursion
) or RecursionLimitHit
;
453 if not RecursionLimitHit
then
454 g_Console_Process(s
, True);
458 if (RecursionDepth
= 0) and RecursionLimitHit
then
460 g_Console_Add(Format(_lc
[I_CONSOLE_ERROR_CALL
], [s
]));
461 RecursionLimitHit
:= False;
468 g_Console_Add('exec <script file>');
471 if cmd
= 'alias' then
473 // alias [alias_name] [commands]
474 if Length(p
) > 1 then
476 for a
:= 0 to High(Aliases
) do
477 if Aliases
[a
].name
= p
[1] then
479 if Length(p
) > 2 then
480 Aliases
[a
].commands
:= ParseAlias(p
[2])
482 for b
:= 0 to High(Aliases
[a
].commands
) do
483 g_Console_Add(Aliases
[a
].commands
[b
]);
486 SetLength(Aliases
, Length(Aliases
)+1);
488 Aliases
[a
].name
:= p
[1];
489 if Length(p
) > 2 then
490 Aliases
[a
].commands
:= ParseAlias(p
[2])
492 for b
:= 0 to High(Aliases
[a
].commands
) do
493 g_Console_Add(Aliases
[a
].commands
[b
]);
495 for a
:= 0 to High(Aliases
) do
496 if Aliases
[a
].commands
<> nil then
497 g_Console_Add(Aliases
[a
].name
);
503 if Length(p
) > 1 then
505 if Aliases
= nil then
507 for a
:= 0 to High(Aliases
) do
508 if Aliases
[a
].name
= p
[1] then
510 if Aliases
[a
].commands
<> nil then
512 // with this system proper endless loop detection seems either impossible
513 // or very dirty to implement, so let's have this instead
514 // prevents endless loops
515 for b
:= 0 to High(Aliases
[a
].commands
) do
518 RecursionLimitHit
:= (RecursionDepth
> MaxScriptRecursion
) or RecursionLimitHit
;
519 if not RecursionLimitHit
then
520 g_Console_Process(Aliases
[a
].commands
[b
], True);
523 if (RecursionDepth
= 0) and RecursionLimitHit
then
525 g_Console_Add(Format(_lc
[I_CONSOLE_ERROR_CALL
], [s
]));
526 RecursionLimitHit
:= False;
533 g_Console_Add('call <alias name>');
537 procedure WhitelistCommand(cmd
: AnsiString);
541 SetLength(Whitelist
, Length(Whitelist
)+1);
542 a
:= High(Whitelist
);
543 Whitelist
[a
] := LowerCase(cmd
);
546 procedure AddCommand(cmd
: AnsiString; proc
: TCmdProc
; ahelp
: AnsiString=''; ahidden
: Boolean=false; acheat
: Boolean=false);
551 SetLength(commands
, Length(commands
)+1);
554 cp
.cmd
:= LowerCase(cmd
);
558 cp
.hidden
:= ahidden
;
565 procedure segfault (p
: SArray
);
573 procedure g_Console_Init();
577 g_Texture_CreateWAD(ID
, GameWAD
+':TEXTURES\CONSOLE');
578 Cons_Y
:= -(gScreenHeight
div 2);
579 gConsoleShow
:= False;
584 for a
:= 0 to High(MsgArray
) do
591 AddCommand('segfault', segfault
, 'make segfault');
593 AddCommand('clear', ConsoleCommands
, 'clear console');
594 AddCommand('clearhistory', ConsoleCommands
);
595 AddCommand('showhistory', ConsoleCommands
);
596 AddCommand('commands', ConsoleCommands
);
597 AddCommand('time', ConsoleCommands
);
598 AddCommand('date', ConsoleCommands
);
599 AddCommand('echo', ConsoleCommands
);
600 AddCommand('dump', ConsoleCommands
);
601 AddCommand('exec', ConsoleCommands
);
602 AddCommand('alias', ConsoleCommands
);
603 AddCommand('call', ConsoleCommands
);
605 AddCommand('d_window', DebugCommands
);
606 AddCommand('d_sounds', DebugCommands
);
607 AddCommand('d_frames', DebugCommands
);
608 AddCommand('d_winmsg', DebugCommands
);
609 AddCommand('d_monoff', DebugCommands
);
610 AddCommand('d_botoff', DebugCommands
);
611 AddCommand('d_monster', DebugCommands
);
612 AddCommand('d_health', DebugCommands
);
613 AddCommand('d_player', DebugCommands
);
614 AddCommand('d_joy', DebugCommands
);
616 AddCommand('p1_name', GameCVars
);
617 AddCommand('p2_name', GameCVars
);
618 AddCommand('p1_color', GameCVars
);
619 AddCommand('p2_color', GameCVars
);
620 AddCommand('r_showfps', GameCVars
);
621 AddCommand('r_showtime', GameCVars
);
622 AddCommand('r_showscore', GameCVars
);
623 AddCommand('r_showlives', GameCVars
);
624 AddCommand('r_showstat', GameCVars
);
625 AddCommand('r_showkillmsg', GameCVars
);
626 AddCommand('r_showspect', GameCVars
);
627 AddCommand('r_showping', GameCVars
);
628 AddCommand('g_gamemode', GameCVars
);
629 AddCommand('g_friendlyfire', GameCVars
);
630 AddCommand('g_weaponstay', GameCVars
);
631 AddCommand('g_allow_exit', GameCVars
);
632 AddCommand('g_allow_monsters', GameCVars
);
633 AddCommand('g_bot_vsmonsters', GameCVars
);
634 AddCommand('g_bot_vsplayers', GameCVars
);
635 AddCommand('g_scorelimit', GameCVars
);
636 AddCommand('g_timelimit', GameCVars
);
637 AddCommand('g_maxlives', GameCVars
);
638 AddCommand('g_warmuptime', GameCVars
);
639 AddCommand('net_interp', GameCVars
);
640 AddCommand('net_forceplayerupdate', GameCVars
);
641 AddCommand('net_predictself', GameCVars
);
642 AddCommand('sv_name', GameCVars
);
643 AddCommand('sv_passwd', GameCVars
);
644 AddCommand('sv_maxplrs', GameCVars
);
645 AddCommand('sv_public', GameCVars
);
646 AddCommand('sv_intertime', GameCVars
);
648 AddCommand('quit', GameCommands
);
649 AddCommand('exit', GameCommands
);
650 AddCommand('pause', GameCommands
);
651 AddCommand('endgame', GameCommands
);
652 AddCommand('restart', GameCommands
);
653 AddCommand('addbot', GameCommands
);
654 AddCommand('bot_add', GameCommands
);
655 AddCommand('bot_addlist', GameCommands
);
656 AddCommand('bot_addred', GameCommands
);
657 AddCommand('bot_addblue', GameCommands
);
658 AddCommand('bot_removeall', GameCommands
);
659 AddCommand('chat', GameCommands
);
660 AddCommand('teamchat', GameCommands
);
661 AddCommand('game', GameCommands
);
662 AddCommand('host', GameCommands
);
663 AddCommand('map', GameCommands
);
664 AddCommand('nextmap', GameCommands
);
665 AddCommand('endmap', GameCommands
);
666 AddCommand('goodbye', GameCommands
);
667 AddCommand('suicide', GameCommands
);
668 AddCommand('spectate', GameCommands
);
669 AddCommand('ready', GameCommands
);
670 AddCommand('kick', GameCommands
);
671 AddCommand('kick_id', GameCommands
);
672 AddCommand('ban', GameCommands
);
673 AddCommand('permban', GameCommands
);
674 AddCommand('ban_id', GameCommands
);
675 AddCommand('permban_id', GameCommands
);
676 AddCommand('unban', GameCommands
);
677 AddCommand('connect', GameCommands
);
678 AddCommand('disconnect', GameCommands
);
679 AddCommand('reconnect', GameCommands
);
680 AddCommand('say', GameCommands
);
681 AddCommand('tell', GameCommands
);
682 AddCommand('overtime', GameCommands
);
683 AddCommand('rcon_password', GameCommands
);
684 AddCommand('rcon', GameCommands
);
685 AddCommand('callvote', GameCommands
);
686 AddCommand('vote', GameCommands
);
687 AddCommand('clientlist', GameCommands
);
688 AddCommand('event', GameCommands
);
690 AddCommand('god', GameCheats
);
691 AddCommand('notarget', GameCheats
);
692 AddCommand('give', GameCheats
); // "exit" too ;-)
693 AddCommand('open', GameCheats
);
694 AddCommand('fly', GameCheats
);
695 AddCommand('noclip', GameCheats
);
696 AddCommand('speedy', GameCheats
);
697 AddCommand('jumpy', GameCheats
);
698 AddCommand('noreload', GameCheats
);
699 AddCommand('aimline', GameCheats
);
700 AddCommand('automap', GameCheats
);
702 WhitelistCommand('say');
703 WhitelistCommand('tell');
704 WhitelistCommand('overtime');
705 WhitelistCommand('ready');
706 WhitelistCommand('map');
707 WhitelistCommand('nextmap');
708 WhitelistCommand('endmap');
709 WhitelistCommand('restart');
710 WhitelistCommand('kick');
711 WhitelistCommand('ban');
713 WhitelistCommand('addbot');
714 WhitelistCommand('bot_add');
715 WhitelistCommand('bot_addred');
716 WhitelistCommand('bot_addblue');
717 WhitelistCommand('bot_removeall');
719 WhitelistCommand('g_gamemode');
720 WhitelistCommand('g_friendlyfire');
721 WhitelistCommand('g_weaponstay');
722 WhitelistCommand('g_allow_exit');
723 WhitelistCommand('g_allow_monsters');
724 WhitelistCommand('g_scorelimit');
725 WhitelistCommand('g_timelimit');
727 g_Console_Add(Format(_lc
[I_CONSOLE_WELCOME
], [GAME_VERSION
]));
731 procedure g_Console_Update();
737 // Â ïðîöåññå îòêðûòèÿ:
738 if gConsoleShow
and (Cons_Y
< 0) then
740 Cons_Y
:= Cons_Y
+Step
;
743 // Â ïðîöåññå çàêðûòèÿ:
744 if (not gConsoleShow
) and
745 (Cons_Y
> -(gScreenHeight
div 2)) then
746 Cons_Y
:= Cons_Y
-Step
;
748 // Îêîí÷àòåëüíî îòêðûëàñü:
752 // Îêîí÷àòåëüíî çàêðûëàñü:
753 if Cons_Y
<= (-(gScreenHeight
div 2)) then
755 Cons_Y
:= -(gScreenHeight
div 2);
761 while a
<= High(MsgArray
) do
763 if MsgArray
[a
].Time
> 0 then
765 if MsgArray
[a
].Time
= 1 then
767 if a
< High(MsgArray
) then
769 for b
:= a
to High(MsgArray
)-1 do
770 MsgArray
[b
] := MsgArray
[b
+1];
772 MsgArray
[High(MsgArray
)].Time
:= 0;
778 Dec(MsgArray
[a
].Time
);
786 procedure drawConsoleText ();
788 CWidth
, CHeight
: Byte;
793 procedure putLine (sp
, ep
: LongWord);
802 cw
:= e_TextureFontCharWidth(cbufAt(p
), gStdFont
);
803 if wdt
+cw
> gScreenWidth
-8 then break
;
804 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
808 if p
<> ep
then putLine(p
, ep
); // do rest of the line first
809 // now print our part
817 cw
:= e_TextureFontCharWidth(cbufAt(p
), gStdFont
);
818 e_TextureFontPrintCharEx(wdt
, ty
, cbufAt(p
), gStdFont
);
831 e_TextureFontGetSize(gStdFont
, CWidth
, CHeight
);
832 ty
:= (gScreenHeight
div 2)-4-2*CHeight
-Abs(Cons_Y
);
833 skip
:= conSkipLines
;
834 cbufLastLine(sp
, ep
);
837 if ty
+CHeight
<= 0 then break
;
838 until not cbufLineUp(sp
, ep
);
841 procedure g_Console_Draw();
843 CWidth
, CHeight
: Byte;
847 e_TextureFontGetSize(gStdFont
, CWidth
, CHeight
);
849 for a
:= 0 to High(MsgArray
) do
850 if MsgArray
[a
].Time
> 0 then
851 e_TextureFontPrintFmt(0, CHeight
*a
, MsgArray
[a
].Msg
,
854 if not Cons_Shown
then
860 e_TextureFontPrintEx(0, gScreenHeight
- CHeight
- 1, 'say team> ' + Line
,
861 gStdFont
, 255, 255, 255, 1, True);
862 e_TextureFontPrintEx((CPos
+ 9)*CWidth
, gScreenHeight
- CHeight
- 1, '_',
863 gStdFont
, 255, 255, 255, 1, True);
867 e_TextureFontPrintEx(0, gScreenHeight
- CHeight
- 1, 'say> ' + Line
,
868 gStdFont
, 255, 255, 255, 1, True);
869 e_TextureFontPrintEx((CPos
+ 4)*CWidth
, gScreenHeight
- CHeight
- 1, '_',
870 gStdFont
, 255, 255, 255, 1, True);
878 e_CharFont_GetSize(gMenuFont
, DEBUG_STRING
, mfW
, mfH
);
879 a
:= (gScreenWidth
- 2*mfW
) div 2;
880 b
:= Cons_Y
+ ((gScreenHeight
div 2) - 2*mfH
) div 2;
881 e_CharFont_PrintEx(gMenuFont
, a
div 2, b
div 2, DEBUG_STRING
,
882 _RGB(128, 0, 0), 2.0);
885 e_DrawSize(ID
, 0, Cons_Y
, Alpha
, False, False, gScreenWidth
, gScreenHeight
div 2);
886 e_TextureFontPrint(0, Cons_Y
+(gScreenHeight
div 2)-CHeight
-4, '> '+Line
, gStdFont
);
890 if ConsoleHistory <> nil then
894 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
895 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
897 b := Max(b-Offset, 0);
898 d := Max(High(ConsoleHistory)-Offset, 0);
901 for a := d downto b do
903 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
910 e_TextureFontPrint((CPos
+1)*CWidth
, Cons_Y
+(gScreenHeight
div 2)-21, '_', gStdFont
);
913 procedure g_Console_Switch();
915 if gChatShow
then Exit
;
916 gConsoleShow
:= not gConsoleShow
;
920 procedure g_Console_Chat_Switch(Team
: Boolean = False);
922 if gConsoleShow
then Exit
;
923 if not g_Game_IsNet
then Exit
;
924 gChatShow
:= not gChatShow
;
932 procedure g_Console_Char(C
: AnsiChar);
934 if gChatShow
and (not gChatEnter
) then
936 Insert(C
, Line
, CPos
);
942 tcomplist
: array of AnsiString = nil;
943 tcompidx
: array of Integer = nil;
945 procedure Complete ();
949 ll
, lpfx
, cmd
: AnsiString;
951 if (Length(Line
) = 0) then
954 for i
:= 0 to High(commands
) do
956 if not commands
[i
].hidden
then
958 if (Length(commands
[i
].help
) > 0) then
960 g_Console_Add(' '+commands
[i
].cmd
+' -- '+commands
[i
].help
);
964 g_Console_Add(' '+commands
[i
].cmd
);
971 ll
:= LowerCase(Line
);
974 if (Length(ll
) > 1) and (ll
[Length(ll
)] = ' ') then
976 ll
:= Copy(ll
, 0, Length(ll
)-1);
977 for i
:= 0 to High(commands
) do
979 if commands
[i
].hidden
then continue
;
980 if (commands
[i
].cmd
= ll
) then
982 if (Length(commands
[i
].help
) > 0) then
984 g_Console_Add(' '+commands
[i
].cmd
+' -- '+commands
[i
].help
);
991 // build completion list
993 for i
:= 0 to High(commands
) do
995 if commands
[i
].hidden
then continue
;
996 cmd
:= commands
[i
].cmd
;
997 if (Length(cmd
) >= Length(ll
)) and (ll
= Copy(cmd
, 0, Length(ll
))) then
999 if (tused
= Length(tcomplist
)) then
1001 SetLength(tcomplist
, Length(tcomplist
)+128);
1002 SetLength(tcompidx
, Length(tcompidx
)+128);
1004 tcomplist
[tused
] := cmd
;
1005 tcompidx
[tused
] := i
;
1007 if (Length(cmd
) > Length(lpfx
)) then lpfx
:= cmd
;
1011 // get longest prefix
1012 for i
:= 0 to tused
-1 do
1014 cmd
:= tcomplist
[i
];
1015 for c
:= 1 to Length(lpfx
) do
1017 if (c
> Length(cmd
)) then break
;
1018 if (cmd
[c
] <> lpfx
[c
]) then begin lpfx
:= Copy(lpfx
, 0, c
-1); break
; end;
1022 if (tused
= 0) then exit
;
1026 Line
:= tcomplist
[0]+' ';
1027 CPos
:= Length(Line
)+1;
1031 // has longest prefix?
1032 if (Length(lpfx
) > Length(ll
)) then
1035 CPos
:= Length(Line
)+1;
1040 for i
:= 0 to tused
-1 do
1042 if (Length(commands
[tcompidx
[i
]].help
) > 0) then
1044 g_Console_Add(' '+tcomplist
[i
]+' -- '+commands
[tcompidx
[i
]].help
);
1048 g_Console_Add(' '+tcomplist
[i
]);
1056 procedure g_Console_Control(K
: Word);
1060 if (Length(Line
) > 0) and (CPos
> 1) then
1062 Delete(Line
, CPos
-1, 1);
1066 if (Length(Line
) > 0) and (CPos
<= Length(Line
)) then
1067 Delete(Line
, CPos
, 1);
1071 IK_RIGHT
, IK_KPRIGHT
:
1072 if CPos
<= Length(Line
) then
1074 IK_RETURN
, IK_KPRETURN
:
1077 g_Console_Process(Line
)
1081 if (Length(Line
) > 0) and g_Game_IsNet
then
1085 if g_Game_IsClient
then
1086 MC_SEND_Chat(b_Text_Format(Line
), NET_CHAT_TEAM
)
1088 MH_SEND_Chat('[' + gPlayer1Settings
.name
+ ']: ' + b_Text_Format(Line
),
1089 NET_CHAT_TEAM
, gPlayer1Settings
.Team
);
1093 if g_Game_IsClient
then
1094 MC_SEND_Chat(b_Text_Format(Line
), NET_CHAT_PLAYER
)
1096 MH_SEND_Chat('[' + gPlayer1Settings
.name
+ ']: ' + b_Text_Format(Line
),
1104 gJustChatted
:= True;
1108 if not gChatShow
then
1111 if not gChatShow
then
1112 if (CommandHistory
<> nil) and
1113 (CmdIndex
< Length(CommandHistory
)) then
1115 if CmdIndex
< Length(CommandHistory
)-1 then
1116 CmdIndex
:= CmdIndex
+ 1;
1117 Line
:= CommandHistory
[CmdIndex
];
1118 CPos
:= Length(Line
) + 1;
1121 if not gChatShow
then
1122 if (CommandHistory
<> nil) and
1123 (CmdIndex
<= Length(CommandHistory
)) then
1125 if CmdIndex
> 0 then
1126 CmdIndex
:= CmdIndex
- 1;
1127 Line
:= CommandHistory
[CmdIndex
];
1128 Cpos
:= Length(Line
) + 1;
1130 IK_PAGEUP
, IK_KPPAGEUP
: // PgUp
1131 if not gChatShow
then Inc(conSkipLines
);
1132 IK_PAGEDN
, IK_KPPAGEDN
: // PgDown
1133 if not gChatShow
and (conSkipLines
> 0) then Dec(conSkipLines
);
1137 CPos
:= Length(Line
) + 1;
1141 function GetStr(var Str
: AnsiString): AnsiString;
1146 if Str
[1] = '"' then
1148 for b
:= 1 to Length(Str
) do
1149 if (b
= Length(Str
)) or (Str
[b
+1] = '"') then
1151 Result
:= Copy(Str
, 2, b
-1);
1152 Delete(Str
, 1, b
+1);
1158 for a
:= 1 to Length(Str
) do
1159 if (a
= Length(Str
)) or (Str
[a
+1] = ' ') then
1161 Result
:= Copy(Str
, 1, a
);
1162 Delete(Str
, 1, a
+1);
1168 function ParseString(Str
: AnsiString): SArray
;
1179 SetLength(Result
, Length(Result
)+1);
1180 Result
[High(Result
)] := GetStr(Str
);
1184 procedure g_Console_Add (L
: AnsiString; show
: Boolean=false);
1186 procedure conmsg (s
: AnsiString);
1190 if length(s
) = 0 then exit
;
1191 for a
:= 0 to High(MsgArray
) do
1203 for a
:= 0 to High(MsgArray
)-1 do MsgArray
[a
] := MsgArray
[a
+1];
1204 with MsgArray
[High(MsgArray
)] do
1214 // put it to console
1216 if (length(L
) = 0) or ((L
[length(L
)] <> #10) and (L
[length(L
)] <> #13)) then cbufPut(#10);
1218 // now show 'em out of console too
1219 show
:= show
and gAllowConsoleMessages
;
1220 if show
and gShowMessages
then
1222 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1223 while length(L
) > 0 do
1226 if f
<= 0 then f
:= length(L
)+1;
1227 conmsg(Copy(L
, 1, f
-1));
1232 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1233 //ConsoleHistory[High(ConsoleHistory)] := L;
1237 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1244 consolewriterLastWasEOL
: Boolean = false;
1246 procedure consolewriter (constref buf
; len
: SizeUInt
);
1250 if (len
< 1) then exit
;
1252 consolewriterLastWasEOL
:= (b
[len
-1] = 13) or (b
[len
-1] = 10);
1255 if (b
[0] <> 13) and (b
[0] <> 10) then
1257 cbufPut(AnsiChar(b
[0]));
1261 if (len
> 1) and (b
[0] = 13) then begin len
-= 1; b
+= 1; end;
1270 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1271 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1272 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1273 procedure conwriteln (const s
: AnsiString; show
: Boolean=false);
1275 g_Console_Add(s
, show
);
1279 procedure conwritefln (const s
: AnsiString; args
: array of const; show
: Boolean=false);
1283 g_Console_Add(formatstrf(s
, args
), true);
1287 consolewriterLastWasEOL
:= false;
1288 formatstrf(s
, args
, consolewriter
);
1289 if not consolewriterLastWasEOL
then cbufPut(#10);
1294 procedure g_Console_Clear();
1296 //ConsoleHistory := nil;
1301 procedure AddToHistory(L
: AnsiString);
1305 len
:= Length(CommandHistory
);
1308 (LowerCase(CommandHistory
[len
-1]) <> LowerCase(L
)) then
1310 SetLength(CommandHistory
, len
+1);
1311 CommandHistory
[len
] := L
;
1314 CmdIndex
:= Length(CommandHistory
);
1317 function g_Console_CommandBlacklisted(C
: AnsiString): Boolean;
1326 if Trim(C
) = '' then
1329 Arr
:= ParseString(C
);
1333 for i
:= 0 to High(Whitelist
) do
1334 if Whitelist
[i
] = LowerCase(Arr
[0]) then
1338 procedure g_Console_Process(L
: AnsiString; quiet
: Boolean = False);
1345 if Trim(L
) = '' then
1348 conSkipLines
:= 0; // "unscroll"
1350 if L
= 'goobers' then
1355 g_Console_Add('Your memory serves you well.');
1361 g_Console_Add('> '+L
);
1366 Arr
:= ParseString(L
);
1370 if commands
= nil then
1376 for i
:= 0 to High(commands
) do
1378 if commands
[i
].cmd
= LowerCase(Arr
[0]) then
1380 if assigned(commands
[i
].procEx
) then
1382 commands
[i
].procEx(@commands
[i
], Arr
);
1385 if assigned(commands
[i
].proc
) then
1387 commands
[i
].proc(Arr
);
1393 g_Console_Add(Format(_lc
[I_CONSOLE_UNKNOWN
], [Arr
[0]]));