hedgewars/uConsole.pas
changeset 167 805fa9a27e9e
parent 166 2920ab2bf329
child 174 0b2c5b22f644
equal deleted inserted replaced
166:2920ab2bf329 167:805fa9a27e9e
    41 
    41 
    42 procedure DrawConsole(Surface: PSDL_Surface);
    42 procedure DrawConsole(Surface: PSDL_Surface);
    43 procedure WriteToConsole(s: shortstring);
    43 procedure WriteToConsole(s: shortstring);
    44 procedure WriteLnToConsole(s: shortstring);
    44 procedure WriteLnToConsole(s: shortstring);
    45 procedure KeyPressConsole(Key: Longword);
    45 procedure KeyPressConsole(Key: Longword);
    46 procedure ParseCommand(CmdStr: shortstring);
    46 procedure ParseCommand(CmdStr: shortstring; const TrustedSource: boolean = true);
    47 function  GetLastConsoleLine: shortstring;
    47 function  GetLastConsoleLine: shortstring;
    48 
    48 
    49 implementation
    49 implementation
    50 {$J+}
    50 {$J+}
    51 uses uMisc, uStore, Types, uConsts, uGears, uTeams, uIO, uKeys, uSound, uWorld, uLand, uRandom;
    51 uses uMisc, uStore, Types, uConsts, uGears, uTeams, uIO, uKeys, uSound, uWorld, uLand, uRandom;
    56       TVariable = record
    56       TVariable = record
    57                      Next: PVariable;
    57                      Next: PVariable;
    58                      Name: string[15];
    58                      Name: string[15];
    59                     VType: TVariableType;
    59                     VType: TVariableType;
    60                   Handler: pointer;
    60                   Handler: pointer;
       
    61                   Trusted: boolean;
    61                   end;
    62                   end;
    62 
    63 
    63 var   ConsoleLines: array[byte] of ShortString;
    64 var   ConsoleLines: array[byte] of ShortString;
    64       CurrLine: integer = 0;
    65       CurrLine: integer = 0;
    65       InputStr: shortstring;
    66       InputStr: shortstring;
    66       Variables: PVariable = nil;
    67       Variables: PVariable = nil;
    67 
    68 
    68 function RegisterVariable(Name: string; VType: TVariableType; p: pointer): PVariable;
    69 function RegisterVariable(Name: string; VType: TVariableType; p: pointer; Trusted: boolean): PVariable;
    69 begin
    70 begin
    70 New(Result);
    71 New(Result);
    71 TryDo(Result <> nil, 'RegisterVariable: Result = nil', true);
    72 TryDo(Result <> nil, 'RegisterVariable: Result = nil', true);
    72 FillChar(Result^, sizeof(TVariable), 0);
    73 FillChar(Result^, sizeof(TVariable), 0);
    73 Result.Name:= Name;
    74 Result.Name:= Name;
    74 Result.VType:= VType;
    75 Result.VType:= VType;
    75 Result.Handler:= p;
    76 Result.Handler:= p;
       
    77 Result.Trusted:= Trusted;
       
    78 
    76 if Variables = nil then Variables:= Result
    79 if Variables = nil then Variables:= Result
    77                    else begin
    80                    else begin
    78                         Result.Next:= Variables;
    81                         Result.Next:= Variables;
    79                         Variables:= Result
    82                         Variables:= Result
    80                         end
    83                         end
   160 cLineWidth:= cScreenWidth div 10;
   163 cLineWidth:= cScreenWidth div 10;
   161 if cLineWidth > 255 then cLineWidth:= 255;
   164 if cLineWidth > 255 then cLineWidth:= 255;
   162 for i:= 0 to Pred(cLinesCount) do PLongWord(@ConsoleLines[i])^:= 0
   165 for i:= 0 to Pred(cLinesCount) do PLongWord(@ConsoleLines[i])^:= 0
   163 end;
   166 end;
   164 
   167 
   165 procedure ParseCommand(CmdStr: shortstring);
   168 procedure ParseCommand(CmdStr: shortstring; const TrustedSource: boolean = true);
   166 type PDouble = ^Double;
   169 type PDouble = ^Double;
   167 var i, ii: integer;
   170 var i, ii: integer;
   168     s: shortstring;
   171     s: shortstring;
   169     t: PVariable;
   172     t: PVariable;
   170     c: char;
   173     c: char;
   178 t:= Variables;
   181 t:= Variables;
   179 while t <> nil do
   182 while t <> nil do
   180       begin
   183       begin
   181       if t.Name = CmdStr then
   184       if t.Name = CmdStr then
   182          begin
   185          begin
   183          case t.VType of
   186          if TrustedSource or t.Trusted then
       
   187             case t.VType of
   184               vtCommand: if c='/' then
   188               vtCommand: if c='/' then
   185                          begin
   189                          begin
   186                          TCommandHandler(t.Handler)(s);
   190                          TCommandHandler(t.Handler)(s);
   187                          end;
   191                          end;
   188               vtInteger: if c='$' then
   192               vtInteger: if c='$' then
   245 case Key of
   249 case Key of
   246       8: if Length(InputStr)>0 then dec(InputStr[0]);
   250       8: if Length(InputStr)>0 then dec(InputStr[0]);
   247       9: AutoComplete;
   251       9: AutoComplete;
   248  13,271: begin
   252  13,271: begin
   249          if InputStr[1] in ['/', '$'] then
   253          if InputStr[1] in ['/', '$'] then
   250             ParseCommand(InputStr)
   254             ParseCommand(InputStr, false)
   251          else
   255          else
   252             ParseCommand('/say ' + InputStr);
   256             ParseCommand('/say ' + InputStr, false);
   253          InputStr:= ''
   257          InputStr:= ''
   254          end;
   258          end;
   255      96: begin
   259      96: begin
   256          GameState:= gsGame;
   260          GameState:= gsGame;
   257          cConsoleYAdd:= 0;
   261          cConsoleYAdd:= 0;
   269 
   273 
   270 {$INCLUDE CCHandlers.inc}
   274 {$INCLUDE CCHandlers.inc}
   271 
   275 
   272 initialization
   276 initialization
   273 InitConsole;
   277 InitConsole;
   274 RegisterVariable('quit'    , vtCommand, @chQuit         );
   278 RegisterVariable('quit'    , vtCommand, @chQuit         , true );
   275 RegisterVariable('capture' , vtCommand, @chCapture      );
   279 RegisterVariable('capture' , vtCommand, @chCapture      , true );
   276 RegisterVariable('addteam' , vtCommand, @chAddTeam      );
   280 RegisterVariable('addteam' , vtCommand, @chAddTeam      , false);
   277 RegisterVariable('rdriven' , vtCommand, @chTeamLocal    );
   281 RegisterVariable('rdriven' , vtCommand, @chTeamLocal    , false);
   278 RegisterVariable('map'     , vtCommand, @chSetMap       );
   282 RegisterVariable('map'     , vtCommand, @chSetMap       , false);
   279 RegisterVariable('theme'   , vtCommand, @chSetTheme     );
   283 RegisterVariable('theme'   , vtCommand, @chSetTheme     , false);
   280 RegisterVariable('seed'    , vtCommand, @chSetSeed      );
   284 RegisterVariable('seed'    , vtCommand, @chSetSeed      , false);
   281 RegisterVariable('c_height', vtInteger, @cConsoleHeight );
   285 RegisterVariable('c_height', vtInteger, @cConsoleHeight , false);
   282 RegisterVariable('gmflags' , vtInteger, @GameFlags      );
   286 RegisterVariable('gmflags' , vtInteger, @GameFlags      , false);
   283 RegisterVariable('turntime', vtInteger, @cHedgehogTurnTime);
   287 RegisterVariable('turntime', vtInteger, @cHedgehogTurnTime, false);
   284 RegisterVariable('name'    , vtCommand, @chName         );
   288 RegisterVariable('name'    , vtCommand, @chName         , false);
   285 RegisterVariable('fort'    , vtCommand, @chFort         );
   289 RegisterVariable('fort'    , vtCommand, @chFort         , false);
   286 RegisterVariable('grave'   , vtCommand, @chGrave        );
   290 RegisterVariable('grave'   , vtCommand, @chGrave        , false);
   287 RegisterVariable('bind'    , vtCommand, @chBind         );
   291 RegisterVariable('bind'    , vtCommand, @chBind         , true );
   288 RegisterVariable('add'     , vtCommand, @chAdd          );
   292 RegisterVariable('add'     , vtCommand, @chAdd          , false);
   289 RegisterVariable('skip'    , vtCommand, @chSkip         );
   293 RegisterVariable('skip'    , vtCommand, @chSkip         , false);
   290 RegisterVariable('say'     , vtCommand, @chSay          );
   294 RegisterVariable('say'     , vtCommand, @chSay          , true );
   291 RegisterVariable('ammomenu', vtCommand, @chAmmoMenu     );
   295 RegisterVariable('ammomenu', vtCommand, @chAmmoMenu     , false);
   292 RegisterVariable('+left'   , vtCommand, @chLeft_p       );
   296 RegisterVariable('+left'   , vtCommand, @chLeft_p       , false);
   293 RegisterVariable('-left'   , vtCommand, @chLeft_m       );
   297 RegisterVariable('-left'   , vtCommand, @chLeft_m       , false);
   294 RegisterVariable('+right'  , vtCommand, @chRight_p      );
   298 RegisterVariable('+right'  , vtCommand, @chRight_p      , false);
   295 RegisterVariable('-right'  , vtCommand, @chRight_m      );
   299 RegisterVariable('-right'  , vtCommand, @chRight_m      , false);
   296 RegisterVariable('+up'     , vtCommand, @chUp_p         );
   300 RegisterVariable('+up'     , vtCommand, @chUp_p         , false);
   297 RegisterVariable('-up'     , vtCommand, @chUp_m         );
   301 RegisterVariable('-up'     , vtCommand, @chUp_m         , false);
   298 RegisterVariable('+down'   , vtCommand, @chDown_p       );
   302 RegisterVariable('+down'   , vtCommand, @chDown_p       , false);
   299 RegisterVariable('-down'   , vtCommand, @chDown_m       );
   303 RegisterVariable('-down'   , vtCommand, @chDown_m       , false);
   300 RegisterVariable('+attack' , vtCommand, @chAttack_p     );
   304 RegisterVariable('+attack' , vtCommand, @chAttack_p     , false);
   301 RegisterVariable('-attack' , vtCommand, @chAttack_m     );
   305 RegisterVariable('-attack' , vtCommand, @chAttack_m     , false);
   302 RegisterVariable('color'   , vtCommand, @chColor        );
   306 RegisterVariable('color'   , vtCommand, @chColor        , false);
   303 RegisterVariable('switch'  , vtCommand, @chSwitch       );
   307 RegisterVariable('switch'  , vtCommand, @chSwitch       , false);
   304 RegisterVariable('nextturn', vtCommand, @chNextTurn     );
   308 RegisterVariable('nextturn', vtCommand, @chNextTurn     , false);
   305 RegisterVariable('timer'   , vtCommand, @chTimer        );
   309 RegisterVariable('timer'   , vtCommand, @chTimer        , false);
   306 RegisterVariable('slot'    , vtCommand, @chSlot         );
   310 RegisterVariable('slot'    , vtCommand, @chSlot         , false);
   307 RegisterVariable('put'     , vtCommand, @chPut          );
   311 RegisterVariable('put'     , vtCommand, @chPut          , false);
   308 RegisterVariable('ljump'   , vtCommand, @chLJump        );
   312 RegisterVariable('ljump'   , vtCommand, @chLJump        , false);
   309 RegisterVariable('hjump'   , vtCommand, @chHJump        );
   313 RegisterVariable('hjump'   , vtCommand, @chHJump        , false);
   310 RegisterVariable('fullscr' , vtCommand, @chFullScr      );
   314 RegisterVariable('fullscr' , vtCommand, @chFullScr      , true );
   311 
   315 
   312 finalization
   316 finalization
   313 FreeVariablesList
   317 FreeVariablesList
   314 
   318 
   315 end.
   319 end.