X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fgame%2Fg_console.pas;h=fb0b9eb66abf0a78e6ec32ff519ab0c350426463;hb=8ef4762ff23dd290f3db936eafb12b247b5a9ac6;hp=d07bf3b4636efd661ddc6ec1768e0f0d1a683679;hpb=6b8ffe735038ca0a7704e7fad15868ef32a07925;p=d2df-sdl.git
diff --git a/src/game/g_console.pas b/src/game/g_console.pas
index d07bf3b..fb0b9eb 100644
--- a/src/game/g_console.pas
+++ b/src/game/g_console.pas
@@ -1,49 +1,92 @@
-{$MODE DELPHI}
+(* Copyright (C) DooM 2D:Forever Developers
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ *)
+{$INCLUDE ../shared/a_modes.inc}
unit g_console;
interface
-procedure g_Console_Init();
-procedure g_Console_Update();
-procedure g_Console_Draw();
-procedure g_Console_Switch();
-procedure g_Console_Char(C: Char);
-procedure g_Console_Control(K: Word);
-procedure g_Console_Process(L: String; Quiet: Boolean = False);
-procedure g_Console_Add(L: String; Show: Boolean = False);
-procedure g_Console_Clear();
-function g_Console_CommandBlacklisted(C: String): Boolean;
+uses
+ utils; // for SSArray
+
+procedure g_Console_Init ();
+procedure g_Console_Update ();
+procedure g_Console_Draw ();
+procedure g_Console_Switch ();
+procedure g_Console_Char (C: AnsiChar);
+procedure g_Console_Control (K: Word);
+procedure g_Console_Process (L: AnsiString; quiet: Boolean=false);
+procedure g_Console_Add (L: AnsiString; show: Boolean=false);
+procedure g_Console_Clear ();
+function g_Console_CommandBlacklisted (C: AnsiString): Boolean;
+
+procedure conwriteln (const s: AnsiString; show: Boolean=false);
+procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
+
+// <0: no arg; 0/1: true/false
+function conGetBoolArg (p: SSArray; idx: Integer): Integer;
+
+procedure g_Console_Chat_Switch (team: Boolean=false);
+
+procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
+procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
+
+// poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
+function conParseFloat (var res: Single; const s: AnsiString): Boolean;
-procedure g_Console_Chat_Switch(Team: Boolean = False);
var
- gConsoleShow: Boolean; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
- gChatShow: Boolean;
- gChatTeam: Boolean = False;
- gAllowConsoleMessages: Boolean = True;
- gChatEnter: Boolean = True;
- gJustChatted: Boolean = False; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
+ gConsoleShow: Boolean = false; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
+ gChatShow: Boolean = false;
+ gChatTeam: Boolean = false;
+ gAllowConsoleMessages: Boolean = true;
+ gChatEnter: Boolean = true;
+ gJustChatted: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
+
implementation
uses
g_textures, g_main, e_graphics, e_input, g_game,
- SysUtils, g_basic, g_options, wadreader, Math,
+ SysUtils, g_basic, g_options, Math,
g_menu, g_language, g_net, g_netmsg, e_log, conbuf;
+
type
- TCmdProc = procedure (P: SArray);
+ PCommand = ^TCommand;
+
+ TCmdProc = procedure (p: SSArray);
+ TCmdProcEx = procedure (me: PCommand; p: SSArray);
TCommand = record
- Cmd: String;
- Proc: TCmdProc;
+ cmd: AnsiString;
+ proc: TCmdProc;
+ procEx: TCmdProcEx;
+ help: AnsiString;
+ hidden: Boolean;
+ ptr: Pointer; // various data
+ msg: AnsiString; // message for var changes
+ cheat: Boolean;
end;
TAlias = record
- Name: String;
- Commands: SArray;
+ name: AnsiString;
+ commands: SSArray;
end;
+
const
Step = 32;
Alpha = 25;
@@ -58,21 +101,191 @@ var
RecursionLimitHit: Boolean = False;
Cons_Y: SmallInt;
Cons_Shown: Boolean; // Ðèñîâàòü ëè êîíñîëü?
- Line: String;
+ Line: AnsiString;
CPos: Word;
- //ConsoleHistory: SArray;
- CommandHistory: SArray;
- Whitelist: SArray;
- Commands: Array of TCommand;
- Aliases: Array of TAlias;
+ //ConsoleHistory: SSArray;
+ CommandHistory: SSArray;
+ Whitelist: SSArray;
+ commands: Array of TCommand = nil;
+ Aliases: Array of TAlias = nil;
CmdIndex: Word;
conSkipLines: Integer = 0;
MsgArray: Array [0..4] of record
- Msg: String;
+ Msg: AnsiString;
Time: Word;
end;
-function GetStrACmd(var Str: String): String;
+
+// poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
+function conParseFloat (var res: Single; const s: AnsiString): Boolean;
+var
+ pos: Integer = 1;
+ frac: Single = 1;
+ slen: Integer;
+begin
+ result := false;
+ res := 0;
+ slen := Length(s);
+ while (slen > 0) and (s[slen] <= ' ') do Dec(slen);
+ while (pos <= slen) and (s[pos] <= ' ') do Inc(pos);
+ if (pos > slen) then exit;
+ if (slen-pos = 1) and (s[pos] = '.') then exit; // single dot
+ // integral part
+ while (pos <= slen) do
+ begin
+ if (s[pos] < '0') or (s[pos] > '9') then break;
+ res := res*10+Byte(s[pos])-48;
+ Inc(pos);
+ end;
+ if (pos <= slen) then
+ begin
+ // must be a dot
+ if (s[pos] <> '.') then exit;
+ Inc(pos);
+ while (pos <= slen) do
+ begin
+ if (s[pos] < '0') or (s[pos] > '9') then break;
+ frac := frac/10;
+ res += frac*(Byte(s[pos])-48);
+ Inc(pos);
+ end;
+ end;
+ if (pos <= slen) then exit; // oops
+ result := true;
+end;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
+// <0: no arg; 0/1: true/false; 666: toggle
+function conGetBoolArg (p: SSArray; idx: Integer): Integer;
+begin
+ if (idx < 0) or (idx > High(p)) then begin result := -1; exit; end;
+ result := 0;
+ if (p[idx] = '1') or (CompareText(p[idx], 'on') = 0) or (CompareText(p[idx], 'true') = 0) or
+ (CompareText(p[idx], 'tan') = 0) or (CompareText(p[idx], 'yes') = 0) then result := 1
+ else if (CompareText(p[idx], 'toggle') = 0) or (CompareText(p[idx], 'switch') = 0) or
+ (CompareText(p[idx], 't') = 0) then result := 666;
+end;
+
+
+procedure boolVarHandler (me: PCommand; p: SSArray);
+ procedure binaryFlag (var flag: Boolean; msg: AnsiString);
+ begin
+ if (Length(p) > 2) then
+ begin
+ conwritefln('too many arguments to ''%s''', [p[0]]);
+ end
+ else
+ begin
+ case conGetBoolArg(p, 1) of
+ -1: begin end;
+ 0: if not me.cheat or conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
+ 1: if not me.cheat or conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
+ 666: if not me.cheat or conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
+ end;
+ if (Length(msg) = 0) then msg := p[0] else msg += ':';
+ if flag then conwritefln('%s tan', [msg]) else conwritefln('%s ona', [msg]);
+ end;
+ end;
+begin
+ binaryFlag(PBoolean(me.ptr)^, me.msg);
+end;
+
+
+procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
+var
+ f: Integer;
+ cp: PCommand;
+begin
+ f := Length(commands);
+ SetLength(commands, f+1);
+ cp := @commands[f];
+ cp.cmd := LowerCase(conname);
+ cp.proc := nil;
+ cp.procEx := boolVarHandler;
+ cp.help := ahelp;
+ cp.hidden := ahidden;
+ cp.ptr := pvar;
+ cp.msg := amsg;
+ cp.cheat := acheat;
+end;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
+type
+ PVarSingle = ^TVarSingle;
+ TVarSingle = record
+ val: PSingle;
+ min, max, def: Single; // default will be starting value
+ end;
+
+
+procedure singleVarHandler (me: PCommand; p: SSArray);
+var
+ pv: PVarSingle;
+ nv: Single;
+ msg: AnsiString;
+begin
+ if (Length(p) > 2) then
+ begin
+ conwritefln('too many arguments to ''%s''', [me.cmd]);
+ exit;
+ end;
+ pv := PVarSingle(me.ptr);
+ if (Length(p) = 2) then
+ begin
+ if me.cheat and (not conIsCheatsEnabled) then begin conwriteln('not available'); exit; end;
+ if (CompareText(p[1], 'default') = 0) or (CompareText(p[1], 'def') = 0) or
+ (CompareText(p[1], 'd') = 0) or (CompareText(p[1], 'off') = 0) or
+ (CompareText(p[1], 'ona') = 0) then
+ begin
+ pv.val^ := pv.def;
+ end
+ else
+ begin
+ if not conParseFloat(nv, p[1]) then
+ begin
+ conwritefln('%s: ''%s'' doesn''t look like a floating number', [me.cmd, p[1]]);
+ exit;
+ end;
+ if (nv < pv.min) then nv := pv.min;
+ if (nv > pv.max) then nv := pv.max;
+ pv.val^ := nv;
+ end;
+ end;
+ msg := me.msg;
+ if (Length(msg) = 0) then msg := me.cmd else msg += ':';
+ conwritefln('%s %s', [msg, pv.val^]);
+end;
+
+
+procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
+var
+ f: Integer;
+ cp: PCommand;
+ pv: PVarSingle;
+begin
+ GetMem(pv, sizeof(TVarSingle));
+ pv.val := pvar;
+ pv.min := amin;
+ pv.max := amax;
+ pv.def := pvar^;
+ f := Length(commands);
+ SetLength(commands, f+1);
+ cp := @commands[f];
+ cp.cmd := LowerCase(conname);
+ cp.proc := nil;
+ cp.procEx := singleVarHandler;
+ cp.help := ahelp;
+ cp.hidden := ahidden;
+ cp.ptr := pv;
+ cp.msg := amsg;
+ cp.cheat := acheat;
+end;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
+function GetStrACmd(var Str: AnsiString): AnsiString;
var
a: Integer;
begin
@@ -87,7 +300,7 @@ begin
end;
end;
-function ParseAlias(Str: String): SArray;
+function ParseAlias(Str: AnsiString): SSArray;
begin
Result := nil;
@@ -103,16 +316,16 @@ begin
end;
end;
-procedure ConsoleCommands(P: SArray);
+procedure ConsoleCommands(p: SSArray);
var
- Cmd, s: String;
+ cmd, s: AnsiString;
a, b: Integer;
F: TextFile;
begin
- Cmd := LowerCase(P[0]);
+ cmd := LowerCase(p[0]);
s := '';
- if Cmd = 'clear' then
+ if cmd = 'clear' then
begin
//ConsoleHistory := nil;
cbufClear();
@@ -126,10 +339,10 @@ begin
end;
end;
- if Cmd = 'clearhistory' then
+ if cmd = 'clearhistory' then
CommandHistory := nil;
- if Cmd = 'showhistory' then
+ if cmd = 'showhistory' then
if CommandHistory <> nil then
begin
g_Console_Add('');
@@ -137,37 +350,46 @@ begin
g_Console_Add(' '+CommandHistory[a]);
end;
- if Cmd = 'commands' then
+ if cmd = 'commands' then
begin
g_Console_Add('');
- g_Console_Add('Commands list:');
- for a := High(Commands) downto 0 do
- g_Console_Add(' '+Commands[a].Cmd);
+ g_Console_Add('commands list:');
+ for a := High(commands) downto 0 do
+ begin
+ if (Length(commands[a].help) > 0) then
+ begin
+ g_Console_Add(' '+commands[a].cmd+' -- '+commands[a].help);
+ end
+ else
+ begin
+ g_Console_Add(' '+commands[a].cmd);
+ end;
+ end;
end;
- if Cmd = 'time' then
+ if cmd = 'time' then
g_Console_Add(TimeToStr(Now), True);
- if Cmd = 'date' then
+ if cmd = 'date' then
g_Console_Add(DateToStr(Now), True);
- if Cmd = 'echo' then
- if Length(P) > 1 then
+ if cmd = 'echo' then
+ if Length(p) > 1 then
begin
- if P[1] = 'ololo' then
+ if p[1] = 'ololo' then
gCheats := True
else
begin
s := '';
- for a := 1 to High(P) do
- s := s + P[a] + ' ';
+ for a := 1 to High(p) do
+ s := s + p[a] + ' ';
g_Console_Add(b_Text_Format(s), True);
end;
end
else
g_Console_Add('');
- if Cmd = 'dump' then
+ if cmd = 'dump' then
begin
(*
if ConsoleHistory <> nil then
@@ -197,12 +419,12 @@ begin
*)
end;
- if Cmd = 'exec' then
+ if cmd = 'exec' then
begin
// exec
- if Length(P) > 1 then
+ if Length(p) > 1 then
begin
- s := GameDir+'/'+P[1];
+ s := GameDir+'/'+p[1];
{$I-}
AssignFile(F, s);
@@ -247,56 +469,56 @@ begin
g_Console_Add('exec