hedgewars/uConsole.pas
changeset 371 731ad6d27bd1
parent 351 29bc9c36ad5f
child 377 d9b88dbdf5a9
equal deleted inserted replaced
370:c75410fe3133 371:731ad6d27bd1
    19 unit uConsole;
    19 unit uConsole;
    20 interface
    20 interface
    21 uses SDLh, uFloat;
    21 uses SDLh, uFloat;
    22 {$INCLUDE options.inc}
    22 {$INCLUDE options.inc}
    23 const isDeveloperMode: boolean = true;
    23 const isDeveloperMode: boolean = true;
    24 type TVariableType = (vtCommand, vtInteger, vthwFloat, vtBoolean);
    24 type TVariableType = (vtCommand, vtLongInt, vthwFloat, vtBoolean);
    25      TCommandHandler = procedure (var params: shortstring);
    25      TCommandHandler = procedure (var params: shortstring);
    26 
    26 
    27 procedure DrawConsole(Surface: PSDL_Surface);
    27 procedure DrawConsole(Surface: PSDL_Surface);
    28 procedure WriteToConsole(s: shortstring);
    28 procedure WriteToConsole(s: shortstring);
    29 procedure WriteLnToConsole(s: shortstring);
    29 procedure WriteLnToConsole(s: shortstring);
    33 
    33 
    34 implementation
    34 implementation
    35 {$J+}
    35 {$J+}
    36 uses uMisc, uStore, Types, uConsts, uGears, uTeams, uIO, uKeys, uWorld, uLand,
    36 uses uMisc, uStore, Types, uConsts, uGears, uTeams, uIO, uKeys, uWorld, uLand,
    37      uRandom, uAmmos;
    37      uRandom, uAmmos;
    38 const cLineWidth: integer = 0;
    38 const cLineWidth: LongInt = 0;
    39       cLinesCount = 256;
    39       cLinesCount = 256;
    40 
    40 
    41 type  PVariable = ^TVariable;
    41 type  PVariable = ^TVariable;
    42       TVariable = record
    42       TVariable = record
    43                      Next: PVariable;
    43                      Next: PVariable;
    46                   Handler: pointer;
    46                   Handler: pointer;
    47                   Trusted: boolean;
    47                   Trusted: boolean;
    48                   end;
    48                   end;
    49 
    49 
    50 var   ConsoleLines: array[byte] of ShortString;
    50 var   ConsoleLines: array[byte] of ShortString;
    51       CurrLine: integer = 0;
    51       CurrLine: LongInt = 0;
    52       InputStr: shortstring;
    52       InputStr: shortstring;
    53       Variables: PVariable = nil;
    53       Variables: PVariable = nil;
    54 
    54 
    55 function RegisterVariable(Name: string; VType: TVariableType; p: pointer; Trusted: boolean): PVariable;
    55 function RegisterVariable(Name: string; VType: TVariableType; p: pointer; Trusted: boolean): PVariable;
    56 var Result: PVariable;
    56 var Result: PVariable;
    84       Dispose(t)
    84       Dispose(t)
    85       end;
    85       end;
    86 end;
    86 end;
    87 
    87 
    88 procedure SplitBySpace(var a, b: shortstring);
    88 procedure SplitBySpace(var a, b: shortstring);
    89 var i, t: integer;
    89 var i, t: LongInt;
    90 begin
    90 begin
    91 i:= Pos(' ', a);
    91 i:= Pos(' ', a);
    92 if i>0 then
    92 if i>0 then
    93    begin
    93    begin
    94    for t:= 1 to Pred(i) do
    94    for t:= 1 to Pred(i) do
    98    byte(a[0]):= Pred(i)
    98    byte(a[0]):= Pred(i)
    99    end else b:= '';
    99    end else b:= '';
   100 end;
   100 end;
   101 
   101 
   102 procedure DrawConsole(Surface: PSDL_Surface);
   102 procedure DrawConsole(Surface: PSDL_Surface);
   103 var x, y: integer;
   103 var x, y: LongInt;
   104     r: TSDL_Rect;
   104     r: TSDL_Rect;
   105 begin
   105 begin
   106 with r do
   106 with r do
   107      begin
   107      begin
   108      x:= 0;
   108      x:= 0;
   118     DXOutText(4, cConsoleHeight - (y + 2) * (Fontz[fnt16].Height + 2), fnt16, ConsoleLines[(CurrLine - 1 - y + cLinesCount) mod cLinesCount], Surface);
   118     DXOutText(4, cConsoleHeight - (y + 2) * (Fontz[fnt16].Height + 2), fnt16, ConsoleLines[(CurrLine - 1 - y + cLinesCount) mod cLinesCount], Surface);
   119 DXOutText(4, cConsoleHeight - Fontz[fnt16].Height - 2, fnt16, '> '+InputStr, Surface);
   119 DXOutText(4, cConsoleHeight - Fontz[fnt16].Height - 2, fnt16, '> '+InputStr, Surface);
   120 end;
   120 end;
   121 
   121 
   122 procedure WriteToConsole(s: shortstring);
   122 procedure WriteToConsole(s: shortstring);
   123 var Len: integer;
   123 var Len: LongInt;
   124 begin
   124 begin
   125 {$IFDEF DEBUGFILE}AddFileLog('Console write: ' + s);{$ENDIF}
   125 {$IFDEF DEBUGFILE}AddFileLog('Console write: ' + s);{$ENDIF}
   126 Write(s);
   126 Write(s);
   127 repeat
   127 repeat
   128 Len:= cLineWidth - Length(ConsoleLines[CurrLine]);
   128 Len:= cLineWidth - Length(ConsoleLines[CurrLine]);
   145 if CurrLine = cLinesCount then CurrLine:= 0;
   145 if CurrLine = cLinesCount then CurrLine:= 0;
   146 PLongWord(@ConsoleLines[CurrLine])^:= 0
   146 PLongWord(@ConsoleLines[CurrLine])^:= 0
   147 end;
   147 end;
   148 
   148 
   149 procedure InitConsole;
   149 procedure InitConsole;
   150 var i: integer;
   150 var i: LongInt;
   151 begin
   151 begin
   152 cLineWidth:= cScreenWidth div 10;
   152 cLineWidth:= cScreenWidth div 10;
   153 if cLineWidth > 255 then cLineWidth:= 255;
   153 if cLineWidth > 255 then cLineWidth:= 255;
   154 for i:= 0 to Pred(cLinesCount) do PLongWord(@ConsoleLines[i])^:= 0
   154 for i:= 0 to Pred(cLinesCount) do PLongWord(@ConsoleLines[i])^:= 0
   155 end;
   155 end;
   156 
   156 
   157 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
   157 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
   158 type PhwFloat = ^hwFloat;
   158 type PhwFloat = ^hwFloat;
   159 var i, ii: integer;
   159 var i, ii: LongInt;
   160     s: shortstring;
   160     s: shortstring;
   161     t: PVariable;
   161     t: PVariable;
   162     c: char;
   162     c: char;
   163 begin
   163 begin
   164 //WriteLnToConsole(CmdStr);
   164 //WriteLnToConsole(CmdStr);
   176             case t^.VType of
   176             case t^.VType of
   177               vtCommand: if c='/' then
   177               vtCommand: if c='/' then
   178                          begin
   178                          begin
   179                          TCommandHandler(t^.Handler)(s);
   179                          TCommandHandler(t^.Handler)(s);
   180                          end;
   180                          end;
   181               vtInteger: if c='$' then
   181               vtLongInt: if c='$' then
   182                          if s[0]=#0 then
   182                          if s[0]=#0 then
   183                             begin
   183                             begin
   184                             str(PInteger(t^.Handler)^, s);
   184                             str(PLongInt(t^.Handler)^, s);
   185                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   185                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   186                             end else val(s, PInteger(t^.Handler)^, i);
   186                             end else val(s, PLongInt(t^.Handler)^, i);
   187                  vthwFloat: if c='$' then
   187                  vthwFloat: if c='$' then
   188                          if s[0]=#0 then
   188                          if s[0]=#0 then
   189                             begin
   189                             begin
   190                             //str(PhwFloat(t^.Handler)^:4:6, s);
   190                             //str(PhwFloat(t^.Handler)^:4:6, s);
   191                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   191                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   270 RegisterVariable('addteam' , vtCommand, @chAddTeam      , false);
   270 RegisterVariable('addteam' , vtCommand, @chAddTeam      , false);
   271 RegisterVariable('rdriven' , vtCommand, @chTeamLocal    , false);
   271 RegisterVariable('rdriven' , vtCommand, @chTeamLocal    , false);
   272 RegisterVariable('map'     , vtCommand, @chSetMap       , false);
   272 RegisterVariable('map'     , vtCommand, @chSetMap       , false);
   273 RegisterVariable('theme'   , vtCommand, @chSetTheme     , false);
   273 RegisterVariable('theme'   , vtCommand, @chSetTheme     , false);
   274 RegisterVariable('seed'    , vtCommand, @chSetSeed      , false);
   274 RegisterVariable('seed'    , vtCommand, @chSetSeed      , false);
   275 RegisterVariable('c_height', vtInteger, @cConsoleHeight , false);
   275 RegisterVariable('c_height', vtLongInt, @cConsoleHeight , false);
   276 RegisterVariable('gmflags' , vtInteger, @GameFlags      , false);
   276 RegisterVariable('gmflags' , vtLongInt, @GameFlags      , false);
   277 RegisterVariable('turntime', vtInteger, @cHedgehogTurnTime, false);
   277 RegisterVariable('turntime', vtLongInt, @cHedgehogTurnTime, false);
   278 RegisterVariable('name'    , vtCommand, @chName         , false);
   278 RegisterVariable('name'    , vtCommand, @chName         , false);
   279 RegisterVariable('fort'    , vtCommand, @chFort         , false);
   279 RegisterVariable('fort'    , vtCommand, @chFort         , false);
   280 RegisterVariable('grave'   , vtCommand, @chGrave        , false);
   280 RegisterVariable('grave'   , vtCommand, @chGrave        , false);
   281 RegisterVariable('bind'    , vtCommand, @chBind         , true );
   281 RegisterVariable('bind'    , vtCommand, @chBind         , true );
   282 RegisterVariable('addhh'   , vtCommand, @chAddHH        , false);
   282 RegisterVariable('addhh'   , vtCommand, @chAddHH        , false);