hedgewars/uAI.pas
changeset 543 465e2ec8f05f
parent 542 ec26095f1bed
child 544 9e068d2398ca
equal deleted inserted replaced
542:ec26095f1bed 543:465e2ec8f05f
    55      
    55      
    56 BestActions.Count:= 0;
    56 BestActions.Count:= 0;
    57 BestActions.Pos:= 0
    57 BestActions.Pos:= 0
    58 end;
    58 end;
    59 
    59 
    60 procedure TestAmmos(var Actions: TActions; Me: PGear);
    60 procedure TestAmmos(var Actions: TActions; Me: PGear; isMoved: boolean);
    61 var Time, BotLevel: Longword;
    61 var Time, BotLevel: Longword;
    62     Angle, Power, Score, ExplX, ExplY, ExplR: LongInt;
    62     ap: TAttackParams;
    63     i: LongInt;
    63     Score, i: LongInt;
    64     a, aa: TAmmoType;
    64     a, aa: TAmmoType;
    65 begin
    65 begin
    66 BotLevel:= PHedgehog(Me^.Hedgehog)^.BotLevel;
    66 BotLevel:= PHedgehog(Me^.Hedgehog)^.BotLevel;
    67 
    67 
    68 for i:= 0 to Pred(Targets.Count) do
    68 for i:= 0 to Pred(Targets.Count) do
    70        begin
    70        begin
    71        with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do
    71        with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do
    72             a:= Ammo^[CurSlot, CurAmmo].AmmoType;
    72             a:= Ammo^[CurSlot, CurAmmo].AmmoType;
    73        aa:= a;
    73        aa:= a;
    74        repeat
    74        repeat
    75         if CanUseAmmo[a] then
    75         if (CanUseAmmo[a]) and
       
    76            ((not isMoved) or ((AmmoTests[a].flags and amtest_OnTurn) = 0)) then
    76            begin
    77            begin
    77            Score:= AmmoTests[a](Me, Targets.ar[i].Point, BotLevel, Time, Angle, Power, ExplX, ExplY, ExplR);
    78            Score:= AmmoTests[a].proc(Me, Targets.ar[i].Point, BotLevel, ap);
    78            if Actions.Score + Score > BestActions.Score then
    79            if Actions.Score + Score > BestActions.Score then
       
    80             if (BestActions.Score < 0) or (Actions.Score + Score > BestActions.Score + LongInt(BotLevel) * 2048) then
    79               begin
    81               begin
    80               BestActions:= Actions;
    82               BestActions:= Actions;
    81               inc(BestActions.Score, Score);
    83               inc(BestActions.Score, Score);
    82 
    84 
    83               AddAction(BestActions, aia_Weapon, Longword(a), 500, 0, 0);
    85               AddAction(BestActions, aia_Weapon, Longword(a), 300 + random(400), 0, 0);
    84               if Time <> 0 then AddAction(BestActions, aia_Timer, Time div 1000, 400, 0, 0);
    86               if (ap.Time <> 0) then AddAction(BestActions, aia_Timer, ap.Time div 1000, 400, 0, 0);
    85               if (Angle > 0) then AddAction(BestActions, aia_LookRight, 0, 200, 0, 0)
    87               if (ap.Angle > 0) then AddAction(BestActions, aia_LookRight, 0, 200, 0, 0)
    86               else if (Angle < 0) then AddAction(BestActions, aia_LookLeft, 0, 200, 0, 0);
    88               else if (ap.Angle < 0) then AddAction(BestActions, aia_LookLeft, 0, 200, 0, 0);
    87               if (Ammoz[a].Ammo.Propz and ammoprop_NoCrosshair) = 0 then
    89               if (Ammoz[a].Ammo.Propz and ammoprop_NoCrosshair) = 0 then
    88                  begin
    90                  begin
    89                  Angle:= LongInt(Me^.Angle) - Abs(Angle);
    91                  ap.Angle:= LongInt(Me^.Angle) - Abs(ap.Angle);
    90                  if Angle > 0 then
    92                  if ap.Angle > 0 then
    91                     begin
    93                     begin
    92                     AddAction(BestActions, aia_Up, aim_push, 500, 0, 0);
    94                     AddAction(BestActions, aia_Up, aim_push, 300 + random(250), 0, 0);
    93                     AddAction(BestActions, aia_Up, aim_release, Angle, 0, 0)
    95                     AddAction(BestActions, aia_Up, aim_release, ap.Angle, 0, 0)
    94                     end else if Angle < 0 then
    96                     end else if ap.Angle < 0 then
    95                     begin
    97                     begin
    96                     AddAction(BestActions, aia_Down, aim_push, 500, 0, 0);
    98                     AddAction(BestActions, aia_Down, aim_push, 300 + random(250), 0, 0);
    97                     AddAction(BestActions, aia_Down, aim_release, -Angle, 0, 0)
    99                     AddAction(BestActions, aia_Down, aim_release, -ap.Angle, 0, 0)
    98                     end
   100                     end
    99                  end;
   101                  end;
   100               AddAction(BestActions, aia_attack, aim_push, 800, 0, 0);
   102               if (Ammoz[a].Ammo.Propz and ammoprop_NeedTarget) <> 0 then
   101               AddAction(BestActions, aia_attack, aim_release, Power, 0, 0);
   103                  begin
   102               if ExplR > 0 then
   104                  AddAction(BestActions, aia_Put, 0, 1, ap.AttackPutX, ap.AttackPutY)
   103                  AddAction(BestActions, aia_AwareExpl, ExplR, 10, ExplX, ExplY);
   105                  end;
       
   106               if (Ammoz[a].Ammo.Propz and ammoprop_AttackingPut) = 0 then
       
   107                  begin
       
   108                  AddAction(BestActions, aia_attack, aim_push, 650 + random(300), 0, 0);
       
   109                  AddAction(BestActions, aia_attack, aim_release, ap.Power, 0, 0);
       
   110                  end;
       
   111               if ap.ExplR > 0 then
       
   112                  AddAction(BestActions, aia_AwareExpl, ap.ExplR, 10, ap.ExplX, ap.ExplY);
   104               end
   113               end
   105            end;
   114            end;
   106         if a = High(TAmmoType) then a:= Low(TAmmoType)
   115         if a = High(TAmmoType) then a:= Low(TAmmoType)
   107                                else inc(a)
   116                                else inc(a)
   108        until (a = aa) or
   117        until (a = aa) or
   186 Push(0, Actions, Me^, tmp xor 3);
   195 Push(0, Actions, Me^, tmp xor 3);
   187 
   196 
   188 if (Me^.State and gstAttacked) = 0 then maxticks:= max(0, TurnTimeLeft - 5000 - 4000 * BotLevel)
   197 if (Me^.State and gstAttacked) = 0 then maxticks:= max(0, TurnTimeLeft - 5000 - 4000 * BotLevel)
   189                                    else maxticks:= TurnTimeLeft;
   198                                    else maxticks:= TurnTimeLeft;
   190 
   199 
   191 if (Me^.State and gstAttacked) = 0 then TestAmmos(Actions, Me);
   200 if (Me^.State and gstAttacked) = 0 then TestAmmos(Actions, Me, false);
   192 BestRate:= RatePlace(Me);
   201 BestRate:= RatePlace(Me);
   193 BaseRate:= max(BestRate, 0);
   202 BaseRate:= max(BestRate, 0);
   194 
   203 
   195 while (Stack.Count > 0) and not StopThinking do
   204 while (Stack.Count > 0) and not StopThinking do
   196     begin
   205     begin
   230           BestRate:= Rate;
   239           BestRate:= Rate;
   231           Me^.State:= Me^.State or gstAttacked // we have better place, go there and don't use ammo
   240           Me^.State:= Me^.State or gstAttacked // we have better place, go there and don't use ammo
   232           end
   241           end
   233        else if Rate < BestRate then break;
   242        else if Rate < BestRate then break;
   234        if ((Me^.State and gstAttacked) = 0)
   243        if ((Me^.State and gstAttacked) = 0)
   235            and ((steps mod 4) = 0) then TestAmmos(Actions, Me);
   244            and ((steps mod 4) = 0) then TestAmmos(Actions, Me, true);
   236        if GoInfo.FallPix >= FallPixForBranching then
   245        if GoInfo.FallPix >= FallPixForBranching then
   237           Push(ticks, Actions, Me^, Me^.Message xor 3); // aia_Left xor 3 = aia_Right
   246           Push(ticks, Actions, Me^, Me^.Message xor 3); // aia_Left xor 3 = aia_Right
   238        end;
   247        end;
   239 
   248 
   240     if BestRate > BaseRate then exit
   249     if BestRate > BaseRate then exit
   298    OutError('AI: no targets!?', false);
   307    OutError('AI: no targets!?', false);
   299    exit
   308    exit
   300    end;
   309    end;
   301 FillBonuses((Me^.State and gstAttacked) <> 0);
   310 FillBonuses((Me^.State and gstAttacked) <> 0);
   302 for a:= Low(TAmmoType) to High(TAmmoType) do
   311 for a:= Low(TAmmoType) to High(TAmmoType) do
   303     CanUseAmmo[a]:= Assigned(AmmoTests[a]) and HHHasAmmo(PHedgehog(Me^.Hedgehog), a);
   312     CanUseAmmo[a]:= Assigned(AmmoTests[a].proc) and HHHasAmmo(PHedgehog(Me^.Hedgehog), a);
   304 {$IFDEF DEBUGFILE}AddFileLog('Enter Think Thread');{$ENDIF}
   313 {$IFDEF DEBUGFILE}AddFileLog('Enter Think Thread');{$ENDIF}
   305 BeginThread(@Think, Me, ThinkThread)
   314 BeginThread(@Think, Me, ThinkThread)
   306 end;
   315 end;
   307 
   316 
   308 procedure ProcessBot;
   317 procedure ProcessBot;