DEADSOFTWARE

improved bind command and co
[d2df-sdl.git] / src / game / g_console.pas
index 230e5b6e8cf4234d88c3a020fe854b79e8aaeb6d..21b056f66130f629832193af63b51ed096abd1e5 100644 (file)
@@ -21,6 +21,22 @@ interface
 uses
   utils; // for SSArray
 
+  const
+    ACTION_JUMP = 0;
+    ACTION_MOVELEFT = 1;
+    ACTION_MOVERIGHT = 2;
+    ACTION_LOOKDOWN = 3;
+    ACTION_LOOKUP = 4;
+    ACTION_ATTACK = 5;
+    ACTION_SCORES = 6;
+    ACTION_ACTIVATE = 7;
+    ACTION_STRAFE = 8;
+    ACTION_WEAPNEXT = 9;
+    ACTION_WEAPPREV = 10;
+
+    FIRST_ACTION = ACTION_JUMP;
+    LAST_ACTION = ACTION_WEAPPREV;
+
 procedure g_Console_Init ();
 procedure g_Console_Update ();
 procedure g_Console_Draw ();
@@ -31,6 +47,11 @@ 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 g_Console_ReadConfig (filename: String);
+
+function  g_Console_Interactive: Boolean;
+function  g_Console_Action (action: Integer): Boolean;
+procedure g_Console_ProcessBind (key: Integer; down: Boolean);
 
 procedure conwriteln (const s: AnsiString; show: Boolean=false);
 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
@@ -42,6 +63,7 @@ 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;
+procedure conRegVar (const conname: AnsiString; pvar: PInteger; 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;
@@ -52,9 +74,8 @@ var
   gChatShow: Boolean = false;
   gChatTeam: Boolean = false;
   gAllowConsoleMessages: Boolean = true;
-  gChatEnter: Boolean = true;
   gJustChatted: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
-
+  gPlayerAction, gDefaultAction: Array [0..1, 0..LAST_ACTION] of Boolean; // [player, action]
 
 implementation
 
@@ -79,6 +100,8 @@ type
     ptr: Pointer; // various data
     msg: AnsiString; // message for var changes
     cheat: Boolean;
+    action: Integer; // >= 0 for action commands
+    player: Integer; // used for action commands
   end;
 
   TAlias = record
@@ -115,6 +138,11 @@ var
                               Time: Word;
                             end;
 
+  bindDown, bindProcess: Boolean;
+  gInputBinds: Array [0..e_MaxInputKeys - 1] of record
+    commands: SSArray;
+  end;
+
 
 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
@@ -192,6 +220,41 @@ begin
 end;
 
 
+procedure intVarHandler (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
+  if (Length(p) <> 2) then
+  begin
+    conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
+  end
+  else
+  begin
+    try
+      PInteger(me.ptr)^ := StrToInt(p[1]);
+    except
+      conwritefln('invalid integer value: "%s"', [p[1]]);
+    end;
+  end;
+end;
+
+
 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
 var
   f: Integer;
@@ -208,6 +271,29 @@ begin
   cp.ptr := pvar;
   cp.msg := amsg;
   cp.cheat := acheat;
+  cp.action := -1;
+  cp.player := -1;
+end;
+
+
+procedure conRegVar (const conname: AnsiString; pvar: PInteger; 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 := intVarHandler;
+  cp.help := ahelp;
+  cp.hidden := ahidden;
+  cp.ptr := pvar;
+  cp.msg := amsg;
+  cp.cheat := acheat;
+  cp.action := -1;
+  cp.player := -1;
 end;
 
 
@@ -281,6 +367,8 @@ begin
   cp.ptr := pv;
   cp.msg := amsg;
   cp.cheat := acheat;
+  cp.action := -1;
+  cp.player := -1;
 end;
 
 
@@ -320,7 +408,7 @@ procedure ConsoleCommands(p: SSArray);
 var
   cmd, s: AnsiString;
   a, b: Integer;
-  F: TextFile;
+  (* F: TextFile; *)
 begin
   cmd := LowerCase(p[0]);
   s := '';
@@ -422,48 +510,10 @@ begin
   if cmd = 'exec' then
   begin
     // exec <filename>
-    if Length(p) > 1 then
+    if Length(p) = 2 then
     begin
-      s := GameDir+'/'+p[1];
-
-      {$I-}
-      AssignFile(F, s);
-      Reset(F);
-      if IOResult <> 0 then
-      begin
-        g_Console_Add(Format(_lc[I_CONSOLE_ERROR_READ], [s]));
-        CloseFile(F);
-        Exit;
-      end;
-      g_Console_Add(Format(_lc[I_CONSOLE_EXEC], [s]));
-
-      while not EOF(F) do
-      begin
-        ReadLn(F, s);
-        if IOResult <> 0 then
-        begin
-          g_Console_Add(Format(_lc[I_CONSOLE_ERROR_READ], [s]));
-          CloseFile(F);
-          Exit;
-        end;
-        if Pos('#', s) <> 1 then // script comment
-        begin
-          // prevents endless loops
-          Inc(RecursionDepth);
-          RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
-          if not RecursionLimitHit then
-            g_Console_Process(s, True);
-          Dec(RecursionDepth);
-        end;
-      end;
-      if (RecursionDepth = 0) and RecursionLimitHit then
-      begin
-        g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
-        RecursionLimitHit := False;
-      end;
-
-      CloseFile(F);
-      {$I+}
+      s := GameDir + '/' + p[1];
+      g_Console_ReadConfig(s);
     end
     else
       g_Console_Add('exec <script file>');
@@ -540,6 +590,9 @@ begin
     else
       g_Console_Add('call <alias name>');
   end;
+
+  if cmd = '//' then
+    Exit;
 end;
 
 procedure WhitelistCommand(cmd: AnsiString);
@@ -551,6 +604,60 @@ begin
   Whitelist[a] := LowerCase(cmd);
 end;
 
+procedure segfault (p: SSArray);
+var
+  pp: PByte = nil;
+begin
+  pp^ := 0;
+end;
+
+procedure BindCommands (p: SSArray);
+  var cmd, key, act: AnsiString; i, j: Integer;
+begin
+  cmd := LowerCase(p[0]);
+  case cmd of
+  'bind':
+    // bind <key> <action>
+    if Length(p) = 3 then
+    begin
+      key := LowerCase(p[1]);
+      i := 0;
+      while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
+      if i < e_MaxInputKeys then
+        gInputBinds[i].commands := ParseAlias(p[2])
+    end;
+  'bindlist':
+    for i := 0 to e_MaxInputKeys - 1 do
+    begin
+      if gInputBinds[i].commands <> nil then
+      begin
+        act := gInputBinds[i].commands[0];
+        for j := 1 to High(gInputBinds[i].commands) do
+          act := act + ' ;' + gInputBinds[i].commands[j];
+        g_Console_Add(LowerCase(e_KeyNames[i]) + ' "' + act + '"')
+      end
+    end;
+  'unbind':
+    // unbind <key>
+    if Length(p) = 2 then
+    begin
+      key := LowerCase(p[1]);
+      i := 0;
+      while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
+      if i < e_MaxInputKeys then
+        gInputBinds[i].commands := nil
+    end;
+  'unbindall':
+    for i := 0 to e_MaxInputKeys - 1 do
+      if gInputBinds[i].commands <> nil then
+        gInputBinds[i].commands := nil;
+  'bindkeys':
+    for i := 0 to e_MaxInputKeys - 1 do
+      if e_KeyNames[i] <> '' then
+        g_Console_Add(LowerCase(e_KeyNames[i]));
+  end
+end;
+
 procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
 var
   a: Integer;
@@ -567,20 +674,49 @@ begin
   cp.ptr := nil;
   cp.msg := '';
   cp.cheat := acheat;
+  cp.action := -1;
+  cp.player := -1;
 end;
 
+procedure AddAction (cmd: AnsiString; action: Integer; help: AnsiString = ''; hidden: Boolean = False; cheat: Boolean = False);
+  const
+    PrefixList: array [0..1] of AnsiString = ('+', '-');
+    PlayerList: array [0..1] of Integer = (1, 2);
+  var
+    s: AnsiString;
+    i: Integer;
+
+  procedure NewAction (cmd: AnsiString; player: Integer);
+    var cp: PCommand;
+  begin
+    SetLength(commands, Length(commands) + 1);
+    cp := @commands[High(commands)];
+    cp.cmd := LowerCase(cmd);
+    cp.proc := nil;
+    cp.procEx := nil;
+    cp.help := help;
+    cp.hidden := hidden;
+    cp.ptr := nil;
+    cp.msg := '';
+    cp.cheat := cheat;
+    cp.action := action;
+    cp.player := player;
+  end;
 
-procedure segfault (p: SSArray);
-var
-  pp: PByte = nil;
 begin
-  pp^ := 0;
+  ASSERT(action >= 0);
+  ASSERT(action <= LAST_ACTION);
+  for s in PrefixList do
+  begin
+    NewAction(s + cmd, 0);
+    for i in PlayerList do
+      NewAction(s + 'p' + IntToStr(i) + '_' + cmd, i - 1)
+  end
 end;
 
-
 procedure g_Console_Init();
-var
-  a: Integer;
+  var
+    a: Integer;
 begin
   g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
   Cons_Y := -(gScreenHeight div 2);
@@ -598,6 +734,24 @@ begin
 
   AddCommand('segfault', segfault, 'make segfault');
 
+  AddCommand('bind', BindCommands);
+  AddCommand('bindlist', BindCommands);
+  AddCommand('unbind', BindCommands);
+  AddCommand('unbindall', BindCommands);
+  AddCommand('bindkeys', BindCommands);
+
+  AddAction('jump', ACTION_JUMP);
+  AddAction('moveleft', ACTION_MOVELEFT);
+  AddAction('moveright', ACTION_MOVERIGHT);
+  AddAction('lookup', ACTION_LOOKUP);
+  AddAction('lookdown', ACTION_LOOKDOWN);
+  AddAction('attack', ACTION_ATTACK);
+  AddAction('scores', ACTION_SCORES);
+  AddAction('activate', ACTION_ACTIVATE);
+  AddAction('strafe', ACTION_STRAFE);
+  AddAction('weapnext', ACTION_WEAPNEXT);
+  AddAction('weapprev', ACTION_WEAPPREV);
+
   AddCommand('clear', ConsoleCommands, 'clear console');
   AddCommand('clearhistory', ConsoleCommands);
   AddCommand('showhistory', ConsoleCommands);
@@ -697,6 +851,12 @@ begin
   AddCommand('vote', GameCommands);
   AddCommand('clientlist', GameCommands);
   AddCommand('event', GameCommands);
+  AddCommand('screenshot', GameCommands);
+  AddCommand('togglechat', GameCommands);
+  AddCommand('toggleteamchat', GameCommands);
+  AddCommand('weapon', GameCommands);
+  AddCommand('p1_weapon', GameCommands);
+  AddCommand('p2_weapon', GameCommands);
 
   AddCommand('god', GameCheats);
   AddCommand('notarget', GameCheats);
@@ -735,6 +895,9 @@ begin
   WhitelistCommand('g_scorelimit');
   WhitelistCommand('g_timelimit');
 
+  g_Console_ReadConfig(GameDir + '/dfconfig.cfg');
+  g_Console_ReadConfig(GameDir + '/autoexec.cfg');
+
   g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
   g_Console_Add('');
 end;
@@ -923,7 +1086,7 @@ end;
 
 procedure g_Console_Switch();
 begin
-  if gChatShow then Exit;
+  gChatShow := False;
   gConsoleShow := not gConsoleShow;
   Cons_Shown := True;
   g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
@@ -931,12 +1094,10 @@ end;
 
 procedure g_Console_Chat_Switch(Team: Boolean = False);
 begin
-  if gConsoleShow then Exit;
   if not g_Game_IsNet then Exit;
+  gConsoleShow := False;
   gChatShow := not gChatShow;
   gChatTeam := Team;
-  if gChatShow then
-    gChatEnter := False;
   Line := '';
   CPos := 1;
   g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
@@ -944,8 +1105,8 @@ end;
 
 procedure g_Console_Char(C: AnsiChar);
 begin
-  if gChatShow and (not gChatEnter) then
-    Exit;
+//  if gChatShow then
+//    Exit;
   Insert(C, Line, CPos);
   CPos := CPos + 1;
 end;
@@ -1079,13 +1240,13 @@ begin
     IK_DELETE:
       if (Length(Line) > 0) and (CPos <= Length(Line)) then
         Delete(Line, CPos, 1);
-    IK_LEFT, IK_KPLEFT:
+    IK_LEFT, IK_KPLEFT, VK_LEFT:
       if CPos > 1 then
         CPos := CPos - 1;
-    IK_RIGHT, IK_KPRIGHT:
+    IK_RIGHT, IK_KPRIGHT, VK_RIGHT:
       if CPos <= Length(Line) then
         CPos := CPos + 1;
-    IK_RETURN, IK_KPRETURN:
+    IK_RETURN, IK_KPRETURN, VK_OPEN, VK_FIRE:
     begin
       if Cons_Shown then
         g_Console_Process(Line)
@@ -1122,7 +1283,7 @@ begin
     IK_TAB:
       if not gChatShow then
         Complete();
-    IK_DOWN, IK_KPDOWN:
+    IK_DOWN, IK_KPDOWN, VK_DOWN:
       if not gChatShow then
         if (CommandHistory <> nil) and
            (CmdIndex < Length(CommandHistory)) then
@@ -1132,7 +1293,7 @@ begin
           Line := CommandHistory[CmdIndex];
           CPos := Length(Line) + 1;
         end;
-    IK_UP, IK_KPUP:
+    IK_UP, IK_KPUP, VK_UP:
       if not gChatShow then
         if (CommandHistory <> nil) and
            (CmdIndex <= Length(CommandHistory)) then
@@ -1142,9 +1303,9 @@ begin
           Line := CommandHistory[CmdIndex];
           Cpos := Length(Line) + 1;
         end;
-    IK_PAGEUP, IK_KPPAGEUP: // PgUp
+    IK_PAGEUP, IK_KPPAGEUP, VK_PREV: // PgUp
       if not gChatShow then Inc(conSkipLines);
-    IK_PAGEDN, IK_KPPAGEDN: // PgDown
+    IK_PAGEDN, IK_KPPAGEDN, VK_NEXT: // PgDown
       if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
     IK_HOME, IK_KPHOME:
       CPos := 1;
@@ -1392,21 +1553,99 @@ begin
   begin
     if commands[i].cmd = LowerCase(Arr[0]) then
     begin
+      if commands[i].action >= 0 then
+      begin
+        if bindProcess then
+        begin
+          if bindDown then
+            gPlayerAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+'
+          else
+            gPlayerAction[commands[i].player, commands[i].action] := gDefaultAction[commands[i].player, commands[i].action]
+        end
+        else
+        begin
+          gPlayerAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+';
+          gDefaultAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+'
+        end;
+        exit
+      end
+      else if bindProcess and not bindDown then
+      begin
+        (* command is not action, so do not execute it again after button release *)
+        exit
+      end;
+
       if assigned(commands[i].procEx) then
       begin
         commands[i].procEx(@commands[i], Arr);
-        exit;
+        exit
       end;
       if assigned(commands[i].proc) then
       begin
         commands[i].proc(Arr);
-        exit;
-      end;
-    end;
+        exit
+      end
+    end
   end;
 
   g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
 end;
 
 
+function g_Console_Interactive: Boolean;
+begin
+  Result := not bindProcess
+end;
+
+function g_Console_Action (action: Integer): Boolean;
+  var i, len: Integer;
+begin
+  ASSERT(action >= 0);
+  ASSERT(action <= LAST_ACTION);
+  i := 0;
+  len := Length(gPlayerAction);
+  while (i < len) and (not gPlayerAction[i, action]) do inc(i);
+  Result := i < len
+end;
+
+procedure g_Console_ProcessBind (key: Integer; down: Boolean);
+  var i: Integer;
+begin
+  if (not gChatShow) and (not gConsoleShow) and (key >= 0) and (key < e_MaxInputKeys) and (gInputBinds[key].commands <> nil) then
+  begin
+    bindDown := down;
+    bindProcess := True;
+    for i := 0 to High(gInputBinds[key].commands) do
+      g_Console_Process(gInputBinds[key].commands[i], True);
+    bindProcess := False;
+  end
+end;
+
+
+procedure g_Console_ReadConfig (filename: String);
+  var f: TextFile; s: AnsiString; i, len: Integer;
+begin
+  if FileExists(filename) then
+  begin
+    AssignFile(f, filename);
+    Reset(f);
+    while not EOF(f) do
+    begin
+      ReadLn(f, s);
+      len := Length(s);
+      if len > 0 then
+      begin
+        i := 1;
+        (* skip spaces *)
+        while (i <= len) and (s[i] <= ' ') do inc(i);
+        (* skip comments *)
+        if (i <= len) and ((s[i] <> '#') and ((i + 1 > len) or (s[i] <> '/') or (s[i + 1] <> '/'))) then
+          g_Console_Process(s, True)
+      end
+    end;
+    CloseFile(f)
+  end
+end;
+
+
 end.