hedgewars/uConsole.pas
changeset 351 29bc9c36ad5f
parent 312 c36d0b34ac3d
child 371 731ad6d27bd1
equal deleted inserted replaced
350:c3ccec3834e8 351:29bc9c36ad5f
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  *)
    17  *)
    18 
    18 
    19 unit uConsole;
    19 unit uConsole;
    20 interface
    20 interface
    21 uses SDLh;
    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, vtDouble, vtBoolean);
    24 type TVariableType = (vtCommand, vtInteger, 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);
    30 procedure KeyPressConsole(Key: Longword);
    30 procedure KeyPressConsole(Key: Longword);
    31 procedure ParseCommand(CmdStr: shortstring; const TrustedSource: boolean = true);
    31 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
    32 function  GetLastConsoleLine: shortstring;
    32 function  GetLastConsoleLine: 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: integer = 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;
    44                      Name: string[15];
    44                      Name: string[15];
    45                     VType: TVariableType;
    45                     VType: TVariableType;
    51       CurrLine: integer = 0;
    51       CurrLine: integer = 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 begin
    57 begin
    57 New(Result);
    58 New(Result);
    58 TryDo(Result <> nil, 'RegisterVariable: Result = nil', true);
    59 TryDo(Result <> nil, 'RegisterVariable: Result = nil', true);
    59 FillChar(Result^, sizeof(TVariable), 0);
    60 FillChar(Result^, sizeof(TVariable), 0);
    60 Result.Name:= Name;
    61 Result^.Name:= Name;
    61 Result.VType:= VType;
    62 Result^.VType:= VType;
    62 Result.Handler:= p;
    63 Result^.Handler:= p;
    63 Result.Trusted:= Trusted;
    64 Result^.Trusted:= Trusted;
    64 
    65 
    65 if Variables = nil then Variables:= Result
    66 if Variables = nil then Variables:= Result
    66                    else begin
    67                    else begin
    67                         Result.Next:= Variables;
    68                         Result^.Next:= Variables;
    68                         Variables:= Result
    69                         Variables:= Result
    69                         end
    70                         end;
       
    71 
       
    72 RegisterVariable:= Result
    70 end;
    73 end;
    71 
    74 
    72 procedure FreeVariablesList;
    75 procedure FreeVariablesList;
    73 var t, tt: PVariable;
    76 var t, tt: PVariable;
    74 begin
    77 begin
    75 tt:= Variables;
    78 tt:= Variables;
    76 Variables:= nil;
    79 Variables:= nil;
    77 while tt<>nil do
    80 while tt <> nil do
    78       begin
    81       begin
    79       t:= tt;
    82       t:= tt;
    80       tt:= tt.Next;
    83       tt:= tt^.Next;
    81       Dispose(t)
    84       Dispose(t)
    82       end;
    85       end;
    83 end;
    86 end;
    84 
    87 
    85 procedure SplitBySpace(var a, b: shortstring);
    88 procedure SplitBySpace(var a, b: shortstring);
   149 cLineWidth:= cScreenWidth div 10;
   152 cLineWidth:= cScreenWidth div 10;
   150 if cLineWidth > 255 then cLineWidth:= 255;
   153 if cLineWidth > 255 then cLineWidth:= 255;
   151 for i:= 0 to Pred(cLinesCount) do PLongWord(@ConsoleLines[i])^:= 0
   154 for i:= 0 to Pred(cLinesCount) do PLongWord(@ConsoleLines[i])^:= 0
   152 end;
   155 end;
   153 
   156 
   154 procedure ParseCommand(CmdStr: shortstring; const TrustedSource: boolean = true);
   157 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
   155 type PDouble = ^Double;
   158 type PhwFloat = ^hwFloat;
   156 var i, ii: integer;
   159 var i, ii: integer;
   157     s: shortstring;
   160     s: shortstring;
   158     t: PVariable;
   161     t: PVariable;
   159     c: char;
   162     c: char;
   160 begin
   163 begin
   165 if c in ['/', '$'] then Delete(CmdStr, 1, 1) else c:= '/';
   168 if c in ['/', '$'] then Delete(CmdStr, 1, 1) else c:= '/';
   166 SplitBySpace(CmdStr, s);
   169 SplitBySpace(CmdStr, s);
   167 t:= Variables;
   170 t:= Variables;
   168 while t <> nil do
   171 while t <> nil do
   169       begin
   172       begin
   170       if t.Name = CmdStr then
   173       if t^.Name = CmdStr then
   171          begin
   174          begin
   172          if TrustedSource or t.Trusted then
   175          if TrustedSource or t^.Trusted then
   173             case t.VType of
   176             case t^.VType of
   174               vtCommand: if c='/' then
   177               vtCommand: if c='/' then
   175                          begin
   178                          begin
   176                          TCommandHandler(t.Handler)(s);
   179                          TCommandHandler(t^.Handler)(s);
   177                          end;
   180                          end;
   178               vtInteger: if c='$' then
   181               vtInteger: if c='$' then
   179                          if s[0]=#0 then
   182                          if s[0]=#0 then
   180                             begin
   183                             begin
   181                             str(PInteger(t.Handler)^, s);
   184                             str(PInteger(t^.Handler)^, s);
   182                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   185                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   183                             end else val(s, PInteger(t.Handler)^, i);
   186                             end else val(s, PInteger(t^.Handler)^, i);
   184                  vtDouble: if c='$' then
   187                  vthwFloat: if c='$' then
   185                          if s[0]=#0 then
   188                          if s[0]=#0 then
   186                             begin
   189                             begin
   187                             str(PDouble(t.Handler)^:4:6, s);
   190                             //str(PhwFloat(t^.Handler)^:4:6, s);
   188                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   191                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   189                             end else val(s, PDouble(t.Handler)^   , i);
   192                             end else; //val(s, PhwFloat(t^.Handler)^, i);
   190              vtBoolean: if c='$' then
   193              vtBoolean: if c='$' then
   191                          if s[0]=#0 then
   194                          if s[0]=#0 then
   192                             begin
   195                             begin
   193                             str(ord(boolean(t.Handler^)), s);
   196                             str(ord(boolean(t^.Handler^)), s);
   194                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   197                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   195                             end else
   198                             end else
   196                             begin
   199                             begin
   197                             val(s, ii, i);
   200                             val(s, ii, i);
   198                             boolean(t.Handler^):= not (ii = 0)
   201                             boolean(t^.Handler^):= not (ii = 0)
   199                             end;
   202                             end;
   200               end;
   203               end;
   201          exit
   204          exit
   202          end else t:= t.Next
   205          end else t:= t^.Next
   203       end;
   206       end;
   204 case c of
   207 case c of
   205      '$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"')
   208      '$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"')
   206      else WriteLnToConsole(errmsgUnknownCommand  + ': "/' + CmdStr + '"') end
   209      else WriteLnToConsole(errmsgUnknownCommand  + ': "/' + CmdStr + '"') end
   207 end;
   210 end;
   216                    else c:= #0;
   219                    else c:= #0;
   217 if InputStr[byte(InputStr[0])] = #32 then dec(InputStr[0]);
   220 if InputStr[byte(InputStr[0])] = #32 then dec(InputStr[0]);
   218 t:= Variables;
   221 t:= Variables;
   219 while t <> nil do
   222 while t <> nil do
   220       begin
   223       begin
   221       if (c=#0) or ((t.VType =  vtCommand) and (c='/'))or
   224       if (c=#0) or ((t^.VType =  vtCommand) and (c='/'))or
   222                    ((t.VType <> vtCommand) and (c='$'))then
   225                    ((t^.VType <> vtCommand) and (c='$'))then
   223          if copy(t.Name, 1, Length(InputStr)) = InputStr then
   226          if copy(t^.Name, 1, Length(InputStr)) = InputStr then
   224             begin
   227             begin
   225             if t.VType = vtCommand then InputStr:= '/' + t.Name + ' '
   228             if t^.VType = vtCommand then InputStr:= '/' + t^.Name + ' '
   226                                    else InputStr:= '$' + t.Name + ' ';
   229                                     else InputStr:= '$' + t^.Name + ' ';
   227             exit
   230             exit
   228             end;
   231             end;
   229       t:= t.Next
   232       t:= t^.Next
   230       end
   233       end
   231 end;
   234 end;
   232 
   235 
   233 procedure KeyPressConsole(Key: Longword);
   236 procedure KeyPressConsole(Key: Longword);
   234 begin
   237 begin
   251      end
   254      end
   252 end;
   255 end;
   253 
   256 
   254 function GetLastConsoleLine: shortstring;
   257 function GetLastConsoleLine: shortstring;
   255 begin
   258 begin
   256 if CurrLine = 0 then Result:= ConsoleLines[Pred(cLinesCount)]
   259 if CurrLine = 0 then GetLastConsoleLine:= ConsoleLines[Pred(cLinesCount)]
   257                 else Result:= ConsoleLines[Pred(CurrLine)]
   260                 else GetLastConsoleLine:= ConsoleLines[Pred(CurrLine)]
   258 end;
   261 end;
   259 
   262 
   260 {$INCLUDE CCHandlers.inc}
   263 {$INCLUDE CCHandlers.inc}
   261 
   264 
   262 initialization
   265 initialization