hedgewars/uCommands.pas
branchhedgeroid
changeset 7855 ddcdedd3330b
parent 7850 fcbb024090a4
child 8498 eecadca7db50
equal deleted inserted replaced
6350:41b0a9955c47 7855:ddcdedd3330b
     1 (*
     1 (*
     2  * Hedgewars, a free turn based strategy game
     2  * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2004-2011 Andrey Korotaev <unC0Rr@gmail.com>
     3  * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
     4  *
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; version 2 of the License
     7  * the Free Software Foundation; version 2 of the License
     8  *
     8  *
    21 unit uCommands;
    21 unit uCommands;
    22 
    22 
    23 interface
    23 interface
    24 
    24 
    25 var isDeveloperMode: boolean;
    25 var isDeveloperMode: boolean;
    26 type TVariableType = (vtCommand, vtLongInt, vthwFloat, vtBoolean);
    26 var isExternalSource: boolean;
    27      TCommandHandler = procedure (var params: shortstring);
    27 type TCommandHandler = procedure (var params: shortstring);
    28 
    28 
    29 procedure initModule;
    29 procedure initModule;
    30 procedure freeModule;
    30 procedure freeModule;
    31 procedure RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean);
    31 procedure RegisterVariable(Name: shortstring; p: TCommandHandler; Trusted: boolean; Rand: boolean);
    32 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
    32 procedure RegisterVariable(Name: shortstring; p: TCommandHandler; Trusted: boolean);
       
    33 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean); inline;
       
    34 procedure ParseCommand(CmdStr: shortstring; TrustedSource, ExternalSource: boolean);
       
    35 procedure ParseTeamCommand(s: shortstring);
    33 procedure StopMessages(Message: Longword);
    36 procedure StopMessages(Message: Longword);
    34 
    37 
    35 implementation
    38 implementation
    36 uses Types, uConsts, uVariables, uConsole, uUtils, uDebug;
    39 uses uConsts, uVariables, uConsole, uUtils, uDebug, SDLh;
    37 
    40 
    38 type  PVariable = ^TVariable;
    41 type  PVariable = ^TVariable;
    39       TVariable = record
    42     TVariable = record
    40                      Next: PVariable;
    43         Next: PVariable;
    41                      Name: string[15];
    44         Name: string[15];
    42                     VType: TVariableType;
    45         Handler: TCommandHandler;
    43                   Handler: pointer;
    46         Trusted, Rand: boolean;
    44                   Trusted: boolean;
    47         end;
    45                   end;
       
    46 
    48 
       
    49 var Variables: PVariable;
       
    50 
       
    51 procedure RegisterVariable(Name: shortstring; p: TCommandHandler; Trusted: boolean);
       
    52 begin
       
    53 RegisterVariable(Name, p, Trusted, false);
       
    54 end;
       
    55 procedure RegisterVariable(Name: shortstring; p: TCommandHandler; Trusted: boolean; Rand: boolean);
    47 var
    56 var
    48       Variables: PVariable;
    57     value: PVariable;
    49 
       
    50 procedure RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean);
       
    51 var value: PVariable;
       
    52 begin
    58 begin
    53 New(value);
    59 New(value);
    54 TryDo(value <> nil, 'RegisterVariable: value = nil', true);
    60 TryDo(value <> nil, 'RegisterVariable: value = nil', true);
    55 FillChar(value^, sizeof(TVariable), 0);
    61 FillChar(value^, sizeof(TVariable), 0);
    56 value^.Name:= Name;
    62 value^.Name:= Name;
    57 value^.VType:= VType;
       
    58 value^.Handler:= p;
    63 value^.Handler:= p;
    59 value^.Trusted:= Trusted;
    64 value^.Trusted:= Trusted;
       
    65 value^.Rand:= Rand;
    60 
    66 
    61 if Variables = nil then Variables:= value
    67 if Variables = nil then
    62                    else begin
    68     Variables:= value
    63                         value^.Next:= Variables;
    69 else
    64                         Variables:= value
    70     begin
    65                         end;
    71     value^.Next:= Variables;
       
    72     Variables:= value
       
    73     end;
    66 end;
    74 end;
    67 
    75 
    68 
    76 
    69 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
    77 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean); inline;
    70 var ii: LongInt;
    78 begin
    71     s: shortstring;
    79     ParseCommand(CmdStr, TrustedSource, false)
       
    80 end;
       
    81 
       
    82 procedure ParseCommand(CmdStr: shortstring; TrustedSource, ExternalSource: boolean);
       
    83 var s: shortstring;
    72     t: PVariable;
    84     t: PVariable;
    73     c: char;
    85     c: char;
    74 begin
    86 begin
       
    87 isExternalSource:= ExternalSource or ((CurrentTeam <> nil) and CurrentTeam^.ExtDriven);
    75 //WriteLnToConsole(CmdStr);
    88 //WriteLnToConsole(CmdStr);
    76 if CmdStr[0]=#0 then exit;
    89 if CmdStr[0]=#0 then
       
    90     exit;
    77 c:= CmdStr[1];
    91 c:= CmdStr[1];
    78 if c in ['/', '$'] then Delete(CmdStr, 1, 1) else c:= '/';
    92 if (c = '/') or (c = '$') then
       
    93     Delete(CmdStr, 1, 1);
    79 s:= '';
    94 s:= '';
    80 SplitBySpace(CmdStr, s);
    95 SplitBySpace(CmdStr, s);
    81 AddFileLog('[Cmd] ' + c + CmdStr + ' (' + inttostr(length(s)) + ')');
    96 AddFileLog('[Cmd] ' + CmdStr + ' (' + inttostr(length(s)) + ')');
       
    97 
    82 t:= Variables;
    98 t:= Variables;
    83 while t <> nil do
    99 while t <> nil do
    84       begin
   100     begin
    85       if t^.Name = CmdStr then
   101     if t^.Name = CmdStr then
    86          begin
   102         begin
    87          if TrustedSource or t^.Trusted then
   103         if TrustedSource or t^.Trusted then
    88             case t^.VType of
   104             begin
    89               vtCommand: if c='/' then
   105             if t^.Rand and (not CheckNoTeamOrHH) then 
    90                          begin
   106                 CheckSum:= CheckSum xor LongWord(SDLNet_Read32(@CmdStr)) xor LongWord(s[0]) xor GameTicks;
    91                          TCommandHandler(t^.Handler)(s);
   107             t^.Handler(s);
    92                          end;
   108             end;
    93               vtLongInt: if c='$' then
   109         exit
    94                          if s[0]=#0 then
   110         end
    95                             begin
   111     else
    96                             str(PLongInt(t^.Handler)^, s);
   112         t:= t^.Next
    97                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
   113     end;
    98                             end else val(s, PLongInt(t^.Handler)^);
       
    99               vthwFloat: if c='$' then
       
   100                          if s[0]=#0 then
       
   101                             begin
       
   102                             //str(PhwFloat(t^.Handler)^:4:6, s);
       
   103                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
       
   104                             end else; //val(s, PhwFloat(t^.Handler)^, i);
       
   105              vtBoolean: if c='$' then
       
   106                          if s[0]=#0 then
       
   107                             begin
       
   108                             str(ord(boolean(t^.Handler^)), s);
       
   109                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
       
   110                             end else
       
   111                             begin
       
   112                             val(s, ii);
       
   113                             boolean(t^.Handler^):= not (ii = 0)
       
   114                             end;
       
   115               end;
       
   116          exit
       
   117          end else t:= t^.Next
       
   118       end;
       
   119 case c of
   114 case c of
   120      '$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"')
   115     '$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"')
   121      else WriteLnToConsole(errmsgUnknownCommand  + ': "/' + CmdStr + '"') end
   116     else
       
   117         WriteLnToConsole(errmsgUnknownCommand  + ': "/' + CmdStr + '"') end
   122 end;
   118 end;
       
   119 
       
   120 procedure ParseTeamCommand(s: shortstring);
       
   121 var Trusted: boolean;
       
   122 begin
       
   123 Trusted:= (CurrentTeam <> nil)
       
   124           and (not CurrentTeam^.ExtDriven)
       
   125           and (CurrentHedgehog^.BotLevel = 0);
       
   126 ParseCommand(s, Trusted);
       
   127 if (CurrentTeam <> nil) and (not CurrentTeam^.ExtDriven) and (ReadyTimeLeft > 1) then
       
   128     ParseCommand('gencmd R', true)
       
   129 end;
       
   130 
   123 
   131 
   124 
   132 
   125 procedure StopMessages(Message: Longword);
   133 procedure StopMessages(Message: Longword);
   126 begin
   134 begin
   127 if (Message and gmLeft) <> 0 then ParseCommand('/-left', true) else
   135 if (Message and gmLeft) <> 0 then
   128 if (Message and gmRight) <> 0 then ParseCommand('/-right', true) else
   136     ParseCommand('/-left', true)
   129 if (Message and gmUp) <> 0 then ParseCommand('/-up', true) else
   137 else if (Message and gmRight) <> 0 then
   130 if (Message and gmDown) <> 0 then ParseCommand('/-down', true) else
   138     ParseCommand('/-right', true) 
   131 if (Message and gmAttack) <> 0 then ParseCommand('/-attack', true)
   139 else if (Message and gmUp) <> 0 then
       
   140     ParseCommand('/-up', true)
       
   141 else if (Message and gmDown) <> 0 then
       
   142     ParseCommand('/-down', true)
       
   143 else if (Message and gmAttack) <> 0 then
       
   144     ParseCommand('/-attack', true)
   132 end;
   145 end;
   133 
   146 
   134 procedure initModule;
   147 procedure initModule;
   135 begin
   148 begin
   136     Variables:= nil;
   149     Variables:= nil;