hedgewars/uAI.pas
changeset 64 9df467527ae5
parent 53 0e27949850e3
child 66 9643d75baf1e
equal deleted inserted replaced
63:27e2b5bb6d4b 64:9df467527ae5
    33 
    33 
    34 unit uAI;
    34 unit uAI;
    35 interface
    35 interface
    36 {$INCLUDE options.inc}
    36 {$INCLUDE options.inc}
    37 procedure ProcessBot;
    37 procedure ProcessBot;
       
    38 procedure FreeActionsList;
    38 
    39 
    39 implementation
    40 implementation
    40 uses uAIActions, uAIMisc, uMisc, uTeams, uConsts, uAIAmmoTests, uGears, SDLh, uConsole;
    41 uses uTeams, uConsts, SDLh, uAIMisc, uGears, uAIAmmoTests, uAIActions, uMisc;
    41 
    42 
    42 procedure Think;
       
    43 var Targets: TTargets;
    43 var Targets: TTargets;
    44     Angle, Power: integer;
    44     Actions, BestActions: TActions;
       
    45 
       
    46 procedure FreeActionsList;
       
    47 begin
       
    48 BestActions.Count:= 0;
       
    49 BestActions.Pos:= 0;
       
    50 end;
       
    51 
       
    52 procedure TestAmmos(Me: PGear);
       
    53 var MyPoint: TPoint;
    45     Time: Longword;
    54     Time: Longword;
       
    55     Angle, Power, Score: integer;
       
    56     i: integer;
       
    57 begin
       
    58 Mypoint.x:= round(Me.X);
       
    59 Mypoint.y:= round(Me.Y);
       
    60 for i:= 0 to Pred(Targets.Count) do
       
    61   begin
       
    62   Score:= TestBazooka(MyPoint, Targets.ar[i].Point, Time, Angle, Power);
       
    63   if Actions.Score + Score + Targets.ar[i].Score > BestActions.Score then
       
    64    begin
       
    65    BestActions:= Actions;
       
    66    inc(BestActions.Score, Score + Targets.ar[i].Score);
       
    67    AddAction(BestActions, aia_Weapon, Longword(amBazooka), 500);
       
    68    if (Angle > 0) then AddAction(BestActions, aia_LookRight, 0, 200)
       
    69    else if (Angle < 0) then AddAction(BestActions, aia_LookLeft, 0, 200);
       
    70    Angle:= integer(Me.Angle) - Abs(Angle);
       
    71    if Angle > 0 then
       
    72       begin
       
    73       AddAction(BestActions, aia_Up, aim_push, 500);
       
    74       AddAction(BestActions, aia_Up, aim_release, Angle)
       
    75       end else if Angle < 0 then
       
    76       begin
       
    77       AddAction(BestActions, aia_Down, aim_push, 500);
       
    78       AddAction(BestActions, aia_Down, aim_release, -Angle)
       
    79       end;
       
    80    AddAction(BestActions, aia_attack, aim_push, 300);
       
    81    AddAction(BestActions, aia_attack, aim_release, Power);
       
    82    end
       
    83   end
       
    84 end;
    46 
    85 
    47     procedure FindTarget(Flags: Longword);
    86 procedure Walk(Me: PGear);
    48     var t: integer;
    87 begin
    49         a, aa: TAmmoType;
    88 TestAmmos(Me)
    50         Me: TPoint;
    89 end;
    51     begin
       
    52     t:= 0;
       
    53     with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
       
    54          begin
       
    55          Me.X:= round(Gear.X);
       
    56          Me.Y:= round(Gear.Y);
       
    57          end;
       
    58     repeat
       
    59       if isInMultiShoot or (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].AttacksNum > 0)
       
    60                         then with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
       
    61                              a:= Ammo[CurSlot, CurAmmo].AmmoType
       
    62                         else a:= TAmmoType(random(ord(High(TAmmoType))));
       
    63       aa:= a;
       
    64       repeat
       
    65         if Assigned(AmmoTests[a].Test)
       
    66            and ((Flags = 0) or ((Flags and AmmoTests[a].Flags) <> 0)) then
       
    67            if AmmoTests[a].Test(Me, Targets.ar[t], Flags, Time, Angle, Power) then
       
    68               begin
       
    69               AddAction(aia_Weapon, ord(a), 1000);
       
    70               if Time <> 0 then AddAction(aia_Timer, Time div 1000, 400);
       
    71               exit
       
    72               end;
       
    73       if a = High(TAmmoType) then a:= Low(TAmmoType)
       
    74                              else inc(a)
       
    75       until isInMultiShoot or (a = aa) or (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].AttacksNum > 0);
       
    76     inc(t)
       
    77     until (t >= Targets.Count)
       
    78     end;
       
    79 
    90 
    80     procedure TryGo(lvl, Flags: Longword);
    91 procedure Think(Me: PGear);
    81     var tmpGear: TGear;
       
    82         i, t: integer;
       
    83     begin
       
    84     with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
       
    85     for t:= aia_Left to aia_Right do
       
    86         if IsActionListEmpty then
       
    87            begin
       
    88            tmpGear:= Gear^;
       
    89            i:= 0;
       
    90            Gear.Message:= t;
       
    91            while HHGo(Gear) do
       
    92                  begin
       
    93                  if (i mod 5 = 0) then
       
    94                     begin
       
    95                     FindTarget(Flags);
       
    96                     if not IsActionListEmpty then
       
    97                        begin
       
    98                        if i > 0 then
       
    99                           begin
       
   100                           AddAction(t, aim_push, 1000);
       
   101                           AddAction(aia_WaitX, round(Gear.X), 0);
       
   102                           AddAction(t, aim_release, 0)
       
   103                           end;
       
   104                        Gear^:= tmpGear;
       
   105                        exit
       
   106                        end
       
   107                     end;
       
   108                  inc(i)
       
   109                  end;
       
   110            Gear^:= tmpGear
       
   111            end
       
   112     end;
       
   113 
       
   114 begin
    92 begin
   115 with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
       
   116      if ((Gear.State and (gstAttacked or gstAttacking or gstMoving or gstFalling)) <> 0)
       
   117         or isInMultiShoot then exit;
       
   118 
       
   119 FillTargets(Targets);
    93 FillTargets(Targets);
   120 
    94 Actions.Score:= 0;
   121 TryGo(0, 0);
    95 Actions.Count:= 0;
   122 
    96 Actions.Pos:= 0;
   123 if IsActionListEmpty then
    97 BestActions.Score:= Low(integer);
   124    TryGo(0, ctfNotFull);
    98 if Targets.Count > 0 then
   125 if IsActionListEmpty then
    99    Walk(Me)
   126    TryGo(0, ctfBreach);
       
   127 
       
   128 if IsActionListEmpty then
       
   129    begin
       
   130    if CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].AttacksNum = 0 then
       
   131       begin
       
   132       AddAction(aia_Weapon, ord(amSkip), 1000);
       
   133       AddAction(aia_Attack, aim_push, 1000);
       
   134       end else ParseCommand('skip');
       
   135    exit
       
   136    end;
       
   137 
       
   138 with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
       
   139      begin
       
   140      if (Angle > 0) then AddAction(aia_LookRight, 0, 200)
       
   141         else if (Angle < 0) then AddAction(aia_LookLeft, 0, 200);
       
   142      Angle:= integer(Gear.Angle) - Abs(Angle);
       
   143      if Angle > 0 then
       
   144         begin
       
   145         AddAction(aia_Up, aim_push, 500);
       
   146         AddAction(aia_Up, aim_release, Angle)
       
   147         end else if Angle < 0 then
       
   148         begin
       
   149         AddAction(aia_Down, aim_push, 500);
       
   150         AddAction(aia_Down, aim_release, -Angle)
       
   151         end;
       
   152      AddAction(aia_attack, aim_push, 300);
       
   153      AddAction(aia_attack, aim_release, Power);
       
   154      end
       
   155 end;
   100 end;
   156 
   101 
   157 procedure ProcessBot;
   102 procedure ProcessBot;
       
   103 var Me: PGear;
   158 begin
   104 begin
   159 with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do          //HACK: v--- temp hack to make AI work
   105 with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
   160      if (Gear <> nil)and((Gear.State and gstHHDriven) <> 0) and (TurnTimeLeft < 29990) then
   106      if (Gear <> nil)and((Gear.State and gstHHDriven) <> 0) and (TurnTimeLeft < 29990) then
   161         begin
   107         begin
   162         if IsActionListEmpty then Think;
   108         Me:= CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear;
   163         ProcessAction
   109         if BestActions.Count = BestActions.Pos then Think(Me);
       
   110         ProcessAction(BestActions, Me)
   164         end
   111         end
   165 end;
   112 end;
   166 
   113 
   167 end.
   114 end.