hedgewars/uCommands.pas
author unc0rr
Mon, 22 Nov 2010 21:43:31 +0300
changeset 4413 46caab3a8f84
parent 4406 beb4de0af990
child 4414 cb90b7f82cd5
permissions -rw-r--r--
uCommandHandlers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
     1
{$INCLUDE "options.inc"}
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
     2
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
     3
unit uCommands;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
     4
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
     5
interface
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
     6
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
     7
var isDeveloperMode: boolean;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
     8
type TVariableType = (vtCommand, vtLongInt, vthwFloat, vtBoolean);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
     9
     TCommandHandler = procedure (var params: shortstring);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    10
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    11
procedure initModule;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    12
procedure freeModule;
4398
36d7e4b6ca81 Move some command handlers out of uCommands into more appropriate places, thus removing some dependencies. Ideally uCommands shouldn't depend on anything (except for uTypes and uConsts probably)
unc0rr
parents: 4396
diff changeset
    13
procedure RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean);
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    14
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    15
procedure StopMessages(Message: Longword);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    16
procedure doPut(putX, putY: LongInt; fromAI: boolean);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    17
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    18
implementation
4413
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
    19
uses Types, uConsts, uIO, uVariables, uConsole, uUtils, uDebug;
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    20
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    21
type  PVariable = ^TVariable;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    22
      TVariable = record
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    23
                     Next: PVariable;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    24
                     Name: string[15];
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    25
                    VType: TVariableType;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    26
                  Handler: pointer;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    27
                  Trusted: boolean;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    28
                  end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    29
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    30
var
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    31
      Variables: PVariable;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    32
4398
36d7e4b6ca81 Move some command handlers out of uCommands into more appropriate places, thus removing some dependencies. Ideally uCommands shouldn't depend on anything (except for uTypes and uConsts probably)
unc0rr
parents: 4396
diff changeset
    33
procedure RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean);
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    34
var value: PVariable;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    35
begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    36
New(value);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    37
TryDo(value <> nil, 'RegisterVariable: value = nil', true);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    38
FillChar(value^, sizeof(TVariable), 0);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    39
value^.Name:= Name;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    40
value^.VType:= VType;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    41
value^.Handler:= p;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    42
value^.Trusted:= Trusted;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    43
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    44
if Variables = nil then Variables:= value
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    45
                   else begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    46
                        value^.Next:= Variables;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    47
                        Variables:= value
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    48
                        end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    49
end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    50
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    51
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    52
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    53
var ii: LongInt;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    54
    s: shortstring;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    55
    t: PVariable;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    56
    c: char;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    57
begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    58
//WriteLnToConsole(CmdStr);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    59
if CmdStr[0]=#0 then exit;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    60
{$IFDEF DEBUGFILE}AddFileLog('ParseCommand "' + CmdStr + '"');{$ENDIF}
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    61
c:= CmdStr[1];
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    62
if c in ['/', '$'] then Delete(CmdStr, 1, 1) else c:= '/';
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    63
s:= '';
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    64
SplitBySpace(CmdStr, s);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    65
t:= Variables;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    66
while t <> nil do
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    67
      begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    68
      if t^.Name = CmdStr then
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    69
         begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    70
         if TrustedSource or t^.Trusted then
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    71
            case t^.VType of
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    72
              vtCommand: if c='/' then
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    73
                         begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    74
                         TCommandHandler(t^.Handler)(s);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    75
                         end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    76
              vtLongInt: if c='$' then
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    77
                         if s[0]=#0 then
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    78
                            begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    79
                            str(PLongInt(t^.Handler)^, s);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    80
                            WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    81
                            end else val(s, PLongInt(t^.Handler)^);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    82
              vthwFloat: if c='$' then
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    83
                         if s[0]=#0 then
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    84
                            begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    85
                            //str(PhwFloat(t^.Handler)^:4:6, s);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    86
                            WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    87
                            end else; //val(s, PhwFloat(t^.Handler)^, i);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    88
             vtBoolean: if c='$' then
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    89
                         if s[0]=#0 then
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    90
                            begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    91
                            str(ord(boolean(t^.Handler^)), s);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    92
                            WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    93
                            end else
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    94
                            begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    95
                            val(s, ii);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    96
                            boolean(t^.Handler^):= not (ii = 0)
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    97
                            end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    98
              end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
    99
         exit
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   100
         end else t:= t^.Next
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   101
      end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   102
case c of
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   103
     '$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"')
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   104
     else WriteLnToConsole(errmsgUnknownCommand  + ': "/' + CmdStr + '"') end
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   105
end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   106
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   107
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   108
procedure StopMessages(Message: Longword);
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   109
begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   110
if (Message and gmLeft) <> 0 then ParseCommand('/-left', true) else
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   111
if (Message and gmRight) <> 0 then ParseCommand('/-right', true) else
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   112
if (Message and gmUp) <> 0 then ParseCommand('/-up', true) else
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   113
if (Message and gmDown) <> 0 then ParseCommand('/-down', true) else
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   114
if (Message and gmAttack) <> 0 then ParseCommand('/-attack', true)
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   115
end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   116
4413
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   117
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   118
procedure doPut(putX, putY: LongInt; fromAI: boolean);
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   119
begin
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   120
if CheckNoTeamOrHH or isPaused then exit;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   121
if ReadyTimeLeft > 1 then ReadyTimeLeft:= 1;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   122
bShowFinger:= false;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   123
if not CurrentTeam^.ExtDriven and bShowAmmoMenu then
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   124
    begin
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   125
    bSelected:= true;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   126
    exit
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   127
    end;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   128
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   129
with CurrentHedgehog^.Gear^,
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   130
    CurrentHedgehog^ do
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   131
    if (State and gstHHChooseTarget) <> 0 then
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   132
        begin
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   133
        isCursorVisible:= false;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   134
        if not CurrentTeam^.ExtDriven then
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   135
            begin
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   136
            if fromAI then
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   137
                begin
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   138
                TargetPoint.X:= putX;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   139
                TargetPoint.Y:= putY
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   140
                end else
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   141
                begin
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   142
                TargetPoint.X:= CursorPoint.X - WorldDx;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   143
                TargetPoint.Y:= cScreenHeight - CursorPoint.Y - WorldDy;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   144
                end;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   145
            SendIPCXY('p', TargetPoint.X, TargetPoint.Y);
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   146
            end
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   147
        else
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   148
            begin
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   149
            TargetPoint.X:= putX;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   150
            TargetPoint.Y:= putY
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   151
            end;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   152
        {$IFDEF DEBUGFILE}AddFilelog('put: ' + inttostr(TargetPoint.X) + ', ' + inttostr(TargetPoint.Y));{$ENDIF}
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   153
        State:= State and not gstHHChooseTarget;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   154
        if (Ammoz[CurAmmoType].Ammo.Propz and ammoprop_AttackingPut) <> 0 then
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   155
            Message:= Message or gmAttack;
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   156
        end
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   157
    else
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   158
        if CurrentTeam^.ExtDriven then
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   159
            OutError('got /put while not being in choose target mode', false)
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4406
diff changeset
   160
end;
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   161
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   162
procedure initModule;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   163
begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   164
    Variables:= nil;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   165
    isDeveloperMode:= true;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   166
end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   167
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   168
procedure freeModule;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   169
var t, tt: PVariable;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   170
begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   171
    tt:= Variables;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   172
    Variables:= nil;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   173
    while tt <> nil do
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   174
    begin
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   175
        t:= tt;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   176
        tt:= tt^.Next;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   177
        Dispose(t)
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   178
    end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   179
end;
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents:
diff changeset
   180
4406
beb4de0af990 Increase teams to 8 to match the 8 colours, fix issue #108, reenable rope length modifier
nemo
parents: 4403
diff changeset
   181
end.