hedgewars/uAI.pas
changeset 70 82d93eeecebe
parent 66 9643d75baf1e
child 71 5f56c6979496
--- a/hedgewars/uAI.pas	Tue Jun 20 21:22:15 2006 +0000
+++ b/hedgewars/uAI.pas	Fri Jun 23 20:02:41 2006 +0000
@@ -65,7 +65,10 @@
 for i:= 0 to Pred(Targets.Count) do
     if Targets.ar[i].Score >= 0 then
        begin
-       a:= Low(TAmmoType);
+       if (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].AttacksNum > 0)
+          then with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
+                    a:= Ammo[CurSlot, CurAmmo].AmmoType
+          else a:= Low(TAmmoType);
        aa:= a;
        repeat
         if Assigned(AmmoTests[a]) then
@@ -89,47 +92,54 @@
                  AddAction(BestActions, aia_Down, aim_push, 500);
                  AddAction(BestActions, aia_Down, aim_release, -Angle)
                  end;
-              AddAction(BestActions, aia_attack, aim_push, 300);
+              AddAction(BestActions, aia_attack, aim_push, 800);
               AddAction(BestActions, aia_attack, aim_release, Power);
               end
            end;
         if a = High(TAmmoType) then a:= Low(TAmmoType)
                                else inc(a)
-       until isInMultiShoot or (a = aa) or (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].AttacksNum > 0)
+       until (a = aa) or (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].AttacksNum > 0)
        end
 end;
 
 procedure Walk(Me: PGear);
 var Actions: TActions;
     BackMe: TGear;
-    Dir, t, avoidt, steps: integer;
+    Dir, steps, maxsteps: integer;
+    BestRate, Rate: integer;
 begin
 Actions.Score:= 0;
 Actions.Count:= 0;
 Actions.Pos:= 0;
+BestActions.Count:= 0;
+if (Me.State and gstAttacked) = 0 then maxsteps:= (TurnTimeLeft - 4000) div cHHStepTicks
+                                  else maxsteps:= 3000;
 BackMe:= Me^;
 if (Me.State and gstAttacked) = 0 then TestAmmos(Actions, Me);
-avoidt:= CheckBonuses(Me);
+BestRate:= RatePlace(Me);
 for Dir:= aia_Left to aia_Right do
     begin
     Me.Message:= Dir;
     steps:= 0;
-    while HHGo(Me) do
+    while HHGo(Me) and (steps < maxsteps) do
        begin
        inc(steps);
        Actions.Count:= 0;
-       AddAction(Actions, Dir, aim_push, 50);
+       AddAction(Actions, Dir, aim_push, 250);
        AddAction(Actions, aia_WaitX, round(Me.X), 0);
        AddAction(Actions, Dir, aim_release, 0);
-       t:= CheckBonuses(Me);
-       if t < avoidt then break
-       else if (t > 0) or (t > avoidt) then
-            begin
-            BestActions:= Actions;
-            exit
-            end;
+       Rate:= RatePlace(Me);
+       if Rate > BestRate then
+          begin
+          BestActions:= Actions;
+          BestRate:= Rate;
+          Me.State:= Me.State or gstAttacked // we have better place, go to it and don't use ammo
+          end
+       else if Rate < BestRate then
+               if BestRate > 0 then exit
+                               else break;
        if ((Me.State and gstAttacked) = 0)
-       and ((steps mod 4) = 0) then TestAmmos(Actions, Me);
+           and ((steps mod 4) = 0) then TestAmmos(Actions, Me);
        if StopThinking then exit;
        end;
     Me^:= BackMe
@@ -140,27 +150,29 @@
 var BackMe: TGear;
     StartTicks: Longword;
 begin
-{$IFDEF DEBUGFILE}AddFileLog('Enter Think Thread');{$ENDIF}
 StartTicks:= GameTicks;
-ThinkingHH:= Me;
-FillTargets;
-FillBonuses;
 BestActions.Score:= Low(integer);
 if Targets.Count > 0 then
    begin
    BackMe:= Me^;
    Walk(@BackMe);
    end;
-if StartTicks > GameTicks - 1000 then SDL_Delay(500);
-Me.State:= Me.State and not gstHHThinking;
-{$IFDEF DEBUGFILE}AddFileLog('Exit Think Thread');{$ENDIF}
+if ((Me.State and gstAttacked) = 0)
+    and (StartTicks > GameTicks - 1000) then SDL_Delay(1000);
+    
+if BestActions.Count > 0 then Me.State:= Me.State and not gstHHThinking;
 ThinkThread:= nil
 end;
 
 procedure StartThink(Me: PGear);
 begin
+if ((Me.State and gstAttacking) <> 0) or isInMultiShoot then exit;
 Me.State:= Me.State or gstHHThinking;
 StopThinking:= false;
+ThinkingHH:= Me;
+FillTargets;
+FillBonuses((Me.State and gstAttacked) <> 0);
+{$IFDEF DEBUGFILE}AddFileLog('Enter Think Thread');{$ENDIF}
 ThinkThread:= SDL_CreateThread(@Think, Me)
 end;