hedgewars/uAI.pas
changeset 70 82d93eeecebe
parent 66 9643d75baf1e
child 71 5f56c6979496
equal deleted inserted replaced
69:d8a526934b9f 70:82d93eeecebe
    63     a, aa: TAmmoType;
    63     a, aa: TAmmoType;
    64 begin
    64 begin
    65 for i:= 0 to Pred(Targets.Count) do
    65 for i:= 0 to Pred(Targets.Count) do
    66     if Targets.ar[i].Score >= 0 then
    66     if Targets.ar[i].Score >= 0 then
    67        begin
    67        begin
    68        a:= Low(TAmmoType);
    68        if (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].AttacksNum > 0)
       
    69           then with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
       
    70                     a:= Ammo[CurSlot, CurAmmo].AmmoType
       
    71           else a:= Low(TAmmoType);
    69        aa:= a;
    72        aa:= a;
    70        repeat
    73        repeat
    71         if Assigned(AmmoTests[a]) then
    74         if Assigned(AmmoTests[a]) then
    72            begin
    75            begin
    73            Score:= AmmoTests[a](Me, Targets.ar[i].Point, Time, Angle, Power);
    76            Score:= AmmoTests[a](Me, Targets.ar[i].Point, Time, Angle, Power);
    87                  end else if Angle < 0 then
    90                  end else if Angle < 0 then
    88                  begin
    91                  begin
    89                  AddAction(BestActions, aia_Down, aim_push, 500);
    92                  AddAction(BestActions, aia_Down, aim_push, 500);
    90                  AddAction(BestActions, aia_Down, aim_release, -Angle)
    93                  AddAction(BestActions, aia_Down, aim_release, -Angle)
    91                  end;
    94                  end;
    92               AddAction(BestActions, aia_attack, aim_push, 300);
    95               AddAction(BestActions, aia_attack, aim_push, 800);
    93               AddAction(BestActions, aia_attack, aim_release, Power);
    96               AddAction(BestActions, aia_attack, aim_release, Power);
    94               end
    97               end
    95            end;
    98            end;
    96         if a = High(TAmmoType) then a:= Low(TAmmoType)
    99         if a = High(TAmmoType) then a:= Low(TAmmoType)
    97                                else inc(a)
   100                                else inc(a)
    98        until isInMultiShoot or (a = aa) or (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].AttacksNum > 0)
   101        until (a = aa) or (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].AttacksNum > 0)
    99        end
   102        end
   100 end;
   103 end;
   101 
   104 
   102 procedure Walk(Me: PGear);
   105 procedure Walk(Me: PGear);
   103 var Actions: TActions;
   106 var Actions: TActions;
   104     BackMe: TGear;
   107     BackMe: TGear;
   105     Dir, t, avoidt, steps: integer;
   108     Dir, steps, maxsteps: integer;
       
   109     BestRate, Rate: integer;
   106 begin
   110 begin
   107 Actions.Score:= 0;
   111 Actions.Score:= 0;
   108 Actions.Count:= 0;
   112 Actions.Count:= 0;
   109 Actions.Pos:= 0;
   113 Actions.Pos:= 0;
       
   114 BestActions.Count:= 0;
       
   115 if (Me.State and gstAttacked) = 0 then maxsteps:= (TurnTimeLeft - 4000) div cHHStepTicks
       
   116                                   else maxsteps:= 3000;
   110 BackMe:= Me^;
   117 BackMe:= Me^;
   111 if (Me.State and gstAttacked) = 0 then TestAmmos(Actions, Me);
   118 if (Me.State and gstAttacked) = 0 then TestAmmos(Actions, Me);
   112 avoidt:= CheckBonuses(Me);
   119 BestRate:= RatePlace(Me);
   113 for Dir:= aia_Left to aia_Right do
   120 for Dir:= aia_Left to aia_Right do
   114     begin
   121     begin
   115     Me.Message:= Dir;
   122     Me.Message:= Dir;
   116     steps:= 0;
   123     steps:= 0;
   117     while HHGo(Me) do
   124     while HHGo(Me) and (steps < maxsteps) do
   118        begin
   125        begin
   119        inc(steps);
   126        inc(steps);
   120        Actions.Count:= 0;
   127        Actions.Count:= 0;
   121        AddAction(Actions, Dir, aim_push, 50);
   128        AddAction(Actions, Dir, aim_push, 250);
   122        AddAction(Actions, aia_WaitX, round(Me.X), 0);
   129        AddAction(Actions, aia_WaitX, round(Me.X), 0);
   123        AddAction(Actions, Dir, aim_release, 0);
   130        AddAction(Actions, Dir, aim_release, 0);
   124        t:= CheckBonuses(Me);
   131        Rate:= RatePlace(Me);
   125        if t < avoidt then break
   132        if Rate > BestRate then
   126        else if (t > 0) or (t > avoidt) then
   133           begin
   127             begin
   134           BestActions:= Actions;
   128             BestActions:= Actions;
   135           BestRate:= Rate;
   129             exit
   136           Me.State:= Me.State or gstAttacked // we have better place, go to it and don't use ammo
   130             end;
   137           end
       
   138        else if Rate < BestRate then
       
   139                if BestRate > 0 then exit
       
   140                                else break;
   131        if ((Me.State and gstAttacked) = 0)
   141        if ((Me.State and gstAttacked) = 0)
   132        and ((steps mod 4) = 0) then TestAmmos(Actions, Me);
   142            and ((steps mod 4) = 0) then TestAmmos(Actions, Me);
   133        if StopThinking then exit;
   143        if StopThinking then exit;
   134        end;
   144        end;
   135     Me^:= BackMe
   145     Me^:= BackMe
   136     end
   146     end
   137 end;
   147 end;
   138 
   148 
   139 procedure Think(Me: PGear); cdecl;
   149 procedure Think(Me: PGear); cdecl;
   140 var BackMe: TGear;
   150 var BackMe: TGear;
   141     StartTicks: Longword;
   151     StartTicks: Longword;
   142 begin
   152 begin
   143 {$IFDEF DEBUGFILE}AddFileLog('Enter Think Thread');{$ENDIF}
       
   144 StartTicks:= GameTicks;
   153 StartTicks:= GameTicks;
   145 ThinkingHH:= Me;
       
   146 FillTargets;
       
   147 FillBonuses;
       
   148 BestActions.Score:= Low(integer);
   154 BestActions.Score:= Low(integer);
   149 if Targets.Count > 0 then
   155 if Targets.Count > 0 then
   150    begin
   156    begin
   151    BackMe:= Me^;
   157    BackMe:= Me^;
   152    Walk(@BackMe);
   158    Walk(@BackMe);
   153    end;
   159    end;
   154 if StartTicks > GameTicks - 1000 then SDL_Delay(500);
   160 if ((Me.State and gstAttacked) = 0)
   155 Me.State:= Me.State and not gstHHThinking;
   161     and (StartTicks > GameTicks - 1000) then SDL_Delay(1000);
   156 {$IFDEF DEBUGFILE}AddFileLog('Exit Think Thread');{$ENDIF}
   162     
       
   163 if BestActions.Count > 0 then Me.State:= Me.State and not gstHHThinking;
   157 ThinkThread:= nil
   164 ThinkThread:= nil
   158 end;
   165 end;
   159 
   166 
   160 procedure StartThink(Me: PGear);
   167 procedure StartThink(Me: PGear);
   161 begin
   168 begin
       
   169 if ((Me.State and gstAttacking) <> 0) or isInMultiShoot then exit;
   162 Me.State:= Me.State or gstHHThinking;
   170 Me.State:= Me.State or gstHHThinking;
   163 StopThinking:= false;
   171 StopThinking:= false;
       
   172 ThinkingHH:= Me;
       
   173 FillTargets;
       
   174 FillBonuses((Me.State and gstAttacked) <> 0);
       
   175 {$IFDEF DEBUGFILE}AddFileLog('Enter Think Thread');{$ENDIF}
   164 ThinkThread:= SDL_CreateThread(@Think, Me)
   176 ThinkThread:= SDL_CreateThread(@Think, Me)
   165 end;
   177 end;
   166 
   178 
   167 procedure ProcessBot;
   179 procedure ProcessBot;
   168 begin
   180 begin