"Trusted" binds
authorunc0rr
Tue, 26 Sep 2006 11:53:12 +0000
changeset 167 805fa9a27e9e
parent 166 2920ab2bf329
child 168 679e9b8912f5
"Trusted" binds
QTfrontend/binds.h
hedgewars/CCHandlers.inc
hedgewars/hwengine.dpr
hedgewars/uConsole.pas
hedgewars/uGame.pas
hedgewars/uKeys.pas
hedgewars/uTeams.pas
--- a/QTfrontend/binds.h	Mon Sep 25 19:48:51 2006 +0000
+++ b/QTfrontend/binds.h	Tue Sep 26 11:53:12 2006 +0000
@@ -73,8 +73,8 @@
 	{"timer 4",	"4",	QT_TRANSLATE_NOOP("binds", "timer 4 sec"),	false},
 	{"timer 5",	"5",	QT_TRANSLATE_NOOP("binds", "timer 5 sec"),	true},
 	{"fullscr",	"f",	QT_TRANSLATE_NOOP("binds", "change mode"),	false},
-	{"capture",	"f11",	QT_TRANSLATE_NOOP("binds", "capture"),	false},
-	{"quit",	"f10",	QT_TRANSLATE_NOOP("binds", "quit"),	true}
+	{"capture",	"c",	QT_TRANSLATE_NOOP("binds", "capture"),	false},
+	{"quit",	"escape",	QT_TRANSLATE_NOOP("binds", "quit"),	true}
 };
 
 #endif // BINDS_H
--- a/hedgewars/CCHandlers.inc	Mon Sep 25 19:48:51 2006 +0000
+++ b/hedgewars/CCHandlers.inc	Tue Sep 26 11:53:12 2006 +0000
@@ -134,7 +134,7 @@
 if s[byte(s[0])]='"' then Delete(s, byte(s[0]), 1);
 b:= KeyNameToCode(id);
 if b = 0 then OutError(errmsgUnknownVariable + ' "' + id + '"')
-         else CurrentTeam.Aliases[b]:= s
+         else CurrentTeam.Binds[b]:= s
 end;
 
 procedure chLeft_p(var s: shortstring);
--- a/hedgewars/hwengine.dpr	Mon Sep 25 19:48:51 2006 +0000
+++ b/hedgewars/hwengine.dpr	Tue Sep 26 11:53:12 2006 +0000
@@ -95,6 +95,7 @@
               GameState:= gsGame
               end;
      gsGame : begin
+              ProcessKbd;
               DoGameTick(Lag);
               DrawWorld(Lag, SDLPrimSurface);
               end;
--- a/hedgewars/uConsole.pas	Mon Sep 25 19:48:51 2006 +0000
+++ b/hedgewars/uConsole.pas	Tue Sep 26 11:53:12 2006 +0000
@@ -43,7 +43,7 @@
 procedure WriteToConsole(s: shortstring);
 procedure WriteLnToConsole(s: shortstring);
 procedure KeyPressConsole(Key: Longword);
-procedure ParseCommand(CmdStr: shortstring);
+procedure ParseCommand(CmdStr: shortstring; const TrustedSource: boolean = true);
 function  GetLastConsoleLine: shortstring;
 
 implementation
@@ -58,6 +58,7 @@
                      Name: string[15];
                     VType: TVariableType;
                   Handler: pointer;
+                  Trusted: boolean;
                   end;
 
 var   ConsoleLines: array[byte] of ShortString;
@@ -65,7 +66,7 @@
       InputStr: shortstring;
       Variables: PVariable = nil;
 
-function RegisterVariable(Name: string; VType: TVariableType; p: pointer): PVariable;
+function RegisterVariable(Name: string; VType: TVariableType; p: pointer; Trusted: boolean): PVariable;
 begin
 New(Result);
 TryDo(Result <> nil, 'RegisterVariable: Result = nil', true);
@@ -73,6 +74,8 @@
 Result.Name:= Name;
 Result.VType:= VType;
 Result.Handler:= p;
+Result.Trusted:= Trusted;
+
 if Variables = nil then Variables:= Result
                    else begin
                         Result.Next:= Variables;
@@ -162,7 +165,7 @@
 for i:= 0 to Pred(cLinesCount) do PLongWord(@ConsoleLines[i])^:= 0
 end;
 
-procedure ParseCommand(CmdStr: shortstring);
+procedure ParseCommand(CmdStr: shortstring; const TrustedSource: boolean = true);
 type PDouble = ^Double;
 var i, ii: integer;
     s: shortstring;
@@ -180,7 +183,8 @@
       begin
       if t.Name = CmdStr then
          begin
-         case t.VType of
+         if TrustedSource or t.Trusted then
+            case t.VType of
               vtCommand: if c='/' then
                          begin
                          TCommandHandler(t.Handler)(s);
@@ -247,9 +251,9 @@
       9: AutoComplete;
  13,271: begin
          if InputStr[1] in ['/', '$'] then
-            ParseCommand(InputStr)
+            ParseCommand(InputStr, false)
          else
-            ParseCommand('/say ' + InputStr);
+            ParseCommand('/say ' + InputStr, false);
          InputStr:= ''
          end;
      96: begin
@@ -271,43 +275,43 @@
 
 initialization
 InitConsole;
-RegisterVariable('quit'    , vtCommand, @chQuit         );
-RegisterVariable('capture' , vtCommand, @chCapture      );
-RegisterVariable('addteam' , vtCommand, @chAddTeam      );
-RegisterVariable('rdriven' , vtCommand, @chTeamLocal    );
-RegisterVariable('map'     , vtCommand, @chSetMap       );
-RegisterVariable('theme'   , vtCommand, @chSetTheme     );
-RegisterVariable('seed'    , vtCommand, @chSetSeed      );
-RegisterVariable('c_height', vtInteger, @cConsoleHeight );
-RegisterVariable('gmflags' , vtInteger, @GameFlags      );
-RegisterVariable('turntime', vtInteger, @cHedgehogTurnTime);
-RegisterVariable('name'    , vtCommand, @chName         );
-RegisterVariable('fort'    , vtCommand, @chFort         );
-RegisterVariable('grave'   , vtCommand, @chGrave        );
-RegisterVariable('bind'    , vtCommand, @chBind         );
-RegisterVariable('add'     , vtCommand, @chAdd          );
-RegisterVariable('skip'    , vtCommand, @chSkip         );
-RegisterVariable('say'     , vtCommand, @chSay          );
-RegisterVariable('ammomenu', vtCommand, @chAmmoMenu     );
-RegisterVariable('+left'   , vtCommand, @chLeft_p       );
-RegisterVariable('-left'   , vtCommand, @chLeft_m       );
-RegisterVariable('+right'  , vtCommand, @chRight_p      );
-RegisterVariable('-right'  , vtCommand, @chRight_m      );
-RegisterVariable('+up'     , vtCommand, @chUp_p         );
-RegisterVariable('-up'     , vtCommand, @chUp_m         );
-RegisterVariable('+down'   , vtCommand, @chDown_p       );
-RegisterVariable('-down'   , vtCommand, @chDown_m       );
-RegisterVariable('+attack' , vtCommand, @chAttack_p     );
-RegisterVariable('-attack' , vtCommand, @chAttack_m     );
-RegisterVariable('color'   , vtCommand, @chColor        );
-RegisterVariable('switch'  , vtCommand, @chSwitch       );
-RegisterVariable('nextturn', vtCommand, @chNextTurn     );
-RegisterVariable('timer'   , vtCommand, @chTimer        );
-RegisterVariable('slot'    , vtCommand, @chSlot         );
-RegisterVariable('put'     , vtCommand, @chPut          );
-RegisterVariable('ljump'   , vtCommand, @chLJump        );
-RegisterVariable('hjump'   , vtCommand, @chHJump        );
-RegisterVariable('fullscr' , vtCommand, @chFullScr      );
+RegisterVariable('quit'    , vtCommand, @chQuit         , true );
+RegisterVariable('capture' , vtCommand, @chCapture      , true );
+RegisterVariable('addteam' , vtCommand, @chAddTeam      , false);
+RegisterVariable('rdriven' , vtCommand, @chTeamLocal    , false);
+RegisterVariable('map'     , vtCommand, @chSetMap       , false);
+RegisterVariable('theme'   , vtCommand, @chSetTheme     , false);
+RegisterVariable('seed'    , vtCommand, @chSetSeed      , false);
+RegisterVariable('c_height', vtInteger, @cConsoleHeight , false);
+RegisterVariable('gmflags' , vtInteger, @GameFlags      , false);
+RegisterVariable('turntime', vtInteger, @cHedgehogTurnTime, false);
+RegisterVariable('name'    , vtCommand, @chName         , false);
+RegisterVariable('fort'    , vtCommand, @chFort         , false);
+RegisterVariable('grave'   , vtCommand, @chGrave        , false);
+RegisterVariable('bind'    , vtCommand, @chBind         , true );
+RegisterVariable('add'     , vtCommand, @chAdd          , false);
+RegisterVariable('skip'    , vtCommand, @chSkip         , false);
+RegisterVariable('say'     , vtCommand, @chSay          , true );
+RegisterVariable('ammomenu', vtCommand, @chAmmoMenu     , false);
+RegisterVariable('+left'   , vtCommand, @chLeft_p       , false);
+RegisterVariable('-left'   , vtCommand, @chLeft_m       , false);
+RegisterVariable('+right'  , vtCommand, @chRight_p      , false);
+RegisterVariable('-right'  , vtCommand, @chRight_m      , false);
+RegisterVariable('+up'     , vtCommand, @chUp_p         , false);
+RegisterVariable('-up'     , vtCommand, @chUp_m         , false);
+RegisterVariable('+down'   , vtCommand, @chDown_p       , false);
+RegisterVariable('-down'   , vtCommand, @chDown_m       , false);
+RegisterVariable('+attack' , vtCommand, @chAttack_p     , false);
+RegisterVariable('-attack' , vtCommand, @chAttack_m     , false);
+RegisterVariable('color'   , vtCommand, @chColor        , false);
+RegisterVariable('switch'  , vtCommand, @chSwitch       , false);
+RegisterVariable('nextturn', vtCommand, @chNextTurn     , false);
+RegisterVariable('timer'   , vtCommand, @chTimer        , false);
+RegisterVariable('slot'    , vtCommand, @chSlot         , false);
+RegisterVariable('put'     , vtCommand, @chPut          , false);
+RegisterVariable('ljump'   , vtCommand, @chLJump        , false);
+RegisterVariable('hjump'   , vtCommand, @chHJump        , false);
+RegisterVariable('fullscr' , vtCommand, @chFullScr      , true );
 
 finalization
 FreeVariablesList
--- a/hedgewars/uGame.pas	Mon Sep 25 19:48:51 2006 +0000
+++ b/hedgewars/uGame.pas	Tue Sep 26 11:53:12 2006 +0000
@@ -47,13 +47,8 @@
 const SendEmptyPacketTicks: LongWord = 0;
 var i: integer;
 begin
-if CurrentTeam.ExtDriven then
+if not CurrentTeam.ExtDriven then
    begin
-   if (GameType = gmtDemo) then
-      ProcessKbdDemo;
-   end
-   else begin
-   ProcessKbd;
    NetGetNextCmd; // its for the case when receiving "/say" message
    isInLag:= false;
    inc(SendEmptyPacketTicks, Lag);
--- a/hedgewars/uKeys.pas	Mon Sep 25 19:48:51 2006 +0000
+++ b/hedgewars/uKeys.pas	Tue Sep 26 11:53:12 2006 +0000
@@ -34,21 +34,28 @@
 unit uKeys;
 interface
 {$INCLUDE options.inc}
+uses uConsts;
+
+type TBinds = array[0..cKeyMaxIndex] of shortstring;
 
 function KeyNameToCode(name: string): word;
 procedure ProcessKbd;
 procedure ResetKbd;
-procedure ProcessKbdDemo;
 procedure InitKbdKeyTable;
 
+procedure SetBinds(var binds: TBinds);
+procedure SetDefaultBinds;
+
 var KbdKeyPressed: boolean;
 
 implementation
-uses SDLh, uTeams, uConsole, uConsts, uMisc;
+uses SDLh, uTeams, uConsole, uMisc;
 const KeyNumber = 1024;
 type TKeyboardState = array[0..cKeyMaxIndex] of Byte;
+
 var tkbd: TKeyboardState;
     KeyNames: array [0..cKeyMaxIndex] of string[15];
+    DefaultBinds, CurrentBinds: TBinds;
 
 function KeyNameToCode(name: string): word;
 begin
@@ -60,47 +67,37 @@
 var  i: integer;
      s: shortstring;
      pkbd: PByteArray;
+     Trusted: boolean;
 begin
-if (CurrentTeam = nil)
-   or (GameState = gsConsole)
-   or (CurrentTeam.ExtDriven)
-   or (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].BotLevel <> 0) then exit;
+KbdKeyPressed:= false;
+Trusted:= (CurrentTeam <> nil)
+          and (not CurrentTeam.ExtDriven)
+          and (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].BotLevel = 0);
 
-KbdKeyPressed:= false;
 pkbd:= SDL_GetKeyState(nil);
 i:= SDL_GetMouseState(nil, nil);
 pkbd^[1]:= (i and 1);
 pkbd^[2]:= ((i shr 1) and 1);
 pkbd^[3]:= ((i shr 2) and 1);
 for i:= 1 to cKeyMaxIndex do
-    if CurrentTeam.Aliases[i][0] <> #0 then
+    if CurrentBinds[i][0] <> #0 then
       begin
       if (i > 3) and (pkbd^[i] <> 0) then KbdKeyPressed:= true;
-      if CurrentTeam.Aliases[i][1] = '+' then
+      if CurrentBinds[i][1] = '+' then
           begin
-          if (pkbd^[i] <> 0)and(tkbd[i]  = 0) then ParseCommand(CurrentTeam.Aliases[i]) else
+          if (pkbd^[i] <> 0)and(tkbd[i]  = 0) then ParseCommand(CurrentBinds[i], Trusted) else
           if (pkbd^[i] =  0)and(tkbd[i] <> 0) then
              begin
-             s:= CurrentTeam.Aliases[i];
+             s:= CurrentBinds[i];
              s[1]:= '-';
              ParseCommand(s)
              end;
           end else
-          if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentTeam.Aliases[i]);
+          if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted);
        tkbd[i]:= pkbd^[i]
        end
 end;
 
-procedure ProcessKbdDemo;
-var pkbd: PByteArray;
-begin
-pkbd:= PByteArray(SDL_GetKeyState(nil));
-if pkbd^[27] <> 0 then
-   begin
-   ParseCommand('/quit');
-   end;
-end;
-
 procedure ResetKbd;
 var i, t: integer;
     pkbd: PByteArray;
@@ -127,9 +124,25 @@
            if s[t] = ' ' then s[t]:= '_';
        KeyNames[i]:= s
        end;
-    end
+    end;
+
+DefaultBinds[ 27]:= 'quit';
+DefaultBinds[ 99]:= 'capture';
+DefaultBinds[102]:= 'fullscr';
+SetDefaultBinds
 end;
 
+procedure SetBinds(var binds: TBinds);
+begin
+CurrentBinds:= binds
+end;
+
+procedure SetDefaultBinds;
+begin
+CurrentBinds:= DefaultBinds
+end;
+
+
 initialization
 
 end.
--- a/hedgewars/uTeams.pas	Mon Sep 25 19:48:51 2006 +0000
+++ b/hedgewars/uTeams.pas	Tue Sep 26 11:53:12 2006 +0000
@@ -56,7 +56,7 @@
              Color: Cardinal;
              TeamName: string[MAXNAMELEN];
              ExtDriven: boolean;
-             Aliases: array[0..cKeyMaxIndex] of shortstring;
+             Binds: TBinds;
              Hedgehogs: array[0..cMaxHHIndex] of THedgehog;
              Ammos: array[0..cMaxHHIndex] of THHAmmo;
              CurrHedgehog: integer;
@@ -162,6 +162,8 @@
 AddGear(0, 0, gtATSmoothWindCh, 0, 0, 0, 1).Tag:= round(72 * cWindSpeed / cMaxWindSpeed);
 {$IFDEF DEBUGFILE}AddFileLog('Wind = '+FloatToStr(cWindSpeed));{$ENDIF}
 ApplyAmmoChanges(CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog]);
+if CurrentTeam.ExtDriven then SetDefaultBinds
+                         else SetBinds(CurrentTeam.Binds);
 TurnTimeLeft:= cHedgehogTurnTime
 end;