hedgewars/uAI.pas
changeset 66 9643d75baf1e
parent 64 9df467527ae5
child 70 82d93eeecebe
equal deleted inserted replaced
65:8c4c6ad6ca99 66:9643d75baf1e
    38 procedure FreeActionsList;
    38 procedure FreeActionsList;
    39 
    39 
    40 implementation
    40 implementation
    41 uses uTeams, uConsts, SDLh, uAIMisc, uGears, uAIAmmoTests, uAIActions, uMisc;
    41 uses uTeams, uConsts, SDLh, uAIMisc, uGears, uAIAmmoTests, uAIActions, uMisc;
    42 
    42 
    43 var Targets: TTargets;
    43 var BestActions: TActions;
    44     Actions, BestActions: TActions;
    44     ThinkThread: PSDL_Thread = nil;
       
    45     StopThinking: boolean;
    45 
    46 
    46 procedure FreeActionsList;
    47 procedure FreeActionsList;
    47 begin
    48 begin
       
    49 if ThinkThread <> nil then
       
    50    begin
       
    51    StopThinking:= true;
       
    52    SDL_WaitThread(ThinkThread, nil);
       
    53    ThinkThread:= nil
       
    54    end;
    48 BestActions.Count:= 0;
    55 BestActions.Count:= 0;
    49 BestActions.Pos:= 0;
    56 BestActions.Pos:= 0
    50 end;
    57 end;
    51 
    58 
    52 procedure TestAmmos(Me: PGear);
    59 procedure TestAmmos(var Actions: TActions; Me: PGear);
    53 var MyPoint: TPoint;
    60 var Time: Longword;
    54     Time: Longword;
       
    55     Angle, Power, Score: integer;
    61     Angle, Power, Score: integer;
    56     i: integer;
    62     i: integer;
       
    63     a, aa: TAmmoType;
    57 begin
    64 begin
    58 Mypoint.x:= round(Me.X);
       
    59 Mypoint.y:= round(Me.Y);
       
    60 for i:= 0 to Pred(Targets.Count) do
    65 for i:= 0 to Pred(Targets.Count) do
    61   begin
    66     if Targets.ar[i].Score >= 0 then
    62   Score:= TestBazooka(MyPoint, Targets.ar[i].Point, Time, Angle, Power);
    67        begin
    63   if Actions.Score + Score + Targets.ar[i].Score > BestActions.Score then
    68        a:= Low(TAmmoType);
    64    begin
    69        aa:= a;
    65    BestActions:= Actions;
    70        repeat
    66    inc(BestActions.Score, Score + Targets.ar[i].Score);
    71         if Assigned(AmmoTests[a]) then
    67    AddAction(BestActions, aia_Weapon, Longword(amBazooka), 500);
    72            begin
    68    if (Angle > 0) then AddAction(BestActions, aia_LookRight, 0, 200)
    73            Score:= AmmoTests[a](Me, Targets.ar[i].Point, Time, Angle, Power);
    69    else if (Angle < 0) then AddAction(BestActions, aia_LookLeft, 0, 200);
    74            if Actions.Score + Score + Targets.ar[i].Score > BestActions.Score then
    70    Angle:= integer(Me.Angle) - Abs(Angle);
    75               begin
    71    if Angle > 0 then
    76               BestActions:= Actions;
    72       begin
    77               inc(BestActions.Score, Score + Targets.ar[i].Score);
    73       AddAction(BestActions, aia_Up, aim_push, 500);
    78               AddAction(BestActions, aia_Weapon, Longword(a), 500);
    74       AddAction(BestActions, aia_Up, aim_release, Angle)
    79               if Time <> 0 then AddAction(BestActions, aia_Timer, Time div 1000, 400);
    75       end else if Angle < 0 then
    80               if (Angle > 0) then AddAction(BestActions, aia_LookRight, 0, 200)
    76       begin
    81               else if (Angle < 0) then AddAction(BestActions, aia_LookLeft, 0, 200);
    77       AddAction(BestActions, aia_Down, aim_push, 500);
    82               Angle:= integer(Me.Angle) - Abs(Angle);
    78       AddAction(BestActions, aia_Down, aim_release, -Angle)
    83               if Angle > 0 then
    79       end;
    84                  begin
    80    AddAction(BestActions, aia_attack, aim_push, 300);
    85                  AddAction(BestActions, aia_Up, aim_push, 500);
    81    AddAction(BestActions, aia_attack, aim_release, Power);
    86                  AddAction(BestActions, aia_Up, aim_release, Angle)
    82    end
    87                  end else if Angle < 0 then
    83   end
    88                  begin
       
    89                  AddAction(BestActions, aia_Down, aim_push, 500);
       
    90                  AddAction(BestActions, aia_Down, aim_release, -Angle)
       
    91                  end;
       
    92               AddAction(BestActions, aia_attack, aim_push, 300);
       
    93               AddAction(BestActions, aia_attack, aim_release, Power);
       
    94               end
       
    95            end;
       
    96         if a = High(TAmmoType) then a:= Low(TAmmoType)
       
    97                                else inc(a)
       
    98        until isInMultiShoot or (a = aa) or (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].AttacksNum > 0)
       
    99        end
    84 end;
   100 end;
    85 
   101 
    86 procedure Walk(Me: PGear);
   102 procedure Walk(Me: PGear);
       
   103 var Actions: TActions;
       
   104     BackMe: TGear;
       
   105     Dir, t, avoidt, steps: integer;
    87 begin
   106 begin
    88 TestAmmos(Me)
       
    89 end;
       
    90 
       
    91 procedure Think(Me: PGear);
       
    92 begin
       
    93 FillTargets(Targets);
       
    94 Actions.Score:= 0;
   107 Actions.Score:= 0;
    95 Actions.Count:= 0;
   108 Actions.Count:= 0;
    96 Actions.Pos:= 0;
   109 Actions.Pos:= 0;
       
   110 BackMe:= Me^;
       
   111 if (Me.State and gstAttacked) = 0 then TestAmmos(Actions, Me);
       
   112 avoidt:= CheckBonuses(Me);
       
   113 for Dir:= aia_Left to aia_Right do
       
   114     begin
       
   115     Me.Message:= Dir;
       
   116     steps:= 0;
       
   117     while HHGo(Me) do
       
   118        begin
       
   119        inc(steps);
       
   120        Actions.Count:= 0;
       
   121        AddAction(Actions, Dir, aim_push, 50);
       
   122        AddAction(Actions, aia_WaitX, round(Me.X), 0);
       
   123        AddAction(Actions, Dir, aim_release, 0);
       
   124        t:= CheckBonuses(Me);
       
   125        if t < avoidt then break
       
   126        else if (t > 0) or (t > avoidt) then
       
   127             begin
       
   128             BestActions:= Actions;
       
   129             exit
       
   130             end;
       
   131        if ((Me.State and gstAttacked) = 0)
       
   132        and ((steps mod 4) = 0) then TestAmmos(Actions, Me);
       
   133        if StopThinking then exit;
       
   134        end;
       
   135     Me^:= BackMe
       
   136     end
       
   137 end;
       
   138 
       
   139 procedure Think(Me: PGear); cdecl;
       
   140 var BackMe: TGear;
       
   141     StartTicks: Longword;
       
   142 begin
       
   143 {$IFDEF DEBUGFILE}AddFileLog('Enter Think Thread');{$ENDIF}
       
   144 StartTicks:= GameTicks;
       
   145 ThinkingHH:= Me;
       
   146 FillTargets;
       
   147 FillBonuses;
    97 BestActions.Score:= Low(integer);
   148 BestActions.Score:= Low(integer);
    98 if Targets.Count > 0 then
   149 if Targets.Count > 0 then
    99    Walk(Me)
   150    begin
       
   151    BackMe:= Me^;
       
   152    Walk(@BackMe);
       
   153    end;
       
   154 if StartTicks > GameTicks - 1000 then SDL_Delay(500);
       
   155 Me.State:= Me.State and not gstHHThinking;
       
   156 {$IFDEF DEBUGFILE}AddFileLog('Exit Think Thread');{$ENDIF}
       
   157 ThinkThread:= nil
       
   158 end;
       
   159 
       
   160 procedure StartThink(Me: PGear);
       
   161 begin
       
   162 Me.State:= Me.State or gstHHThinking;
       
   163 StopThinking:= false;
       
   164 ThinkThread:= SDL_CreateThread(@Think, Me)
   100 end;
   165 end;
   101 
   166 
   102 procedure ProcessBot;
   167 procedure ProcessBot;
   103 var Me: PGear;
       
   104 begin
   168 begin
   105 with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
   169 with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
   106      if (Gear <> nil)and((Gear.State and gstHHDriven) <> 0) and (TurnTimeLeft < 29990) then
   170      if (Gear <> nil)
   107         begin
   171         and ((Gear.State and gstHHDriven) <> 0)
   108         Me:= CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear;
   172         and (TurnTimeLeft < 29990)
   109         if BestActions.Count = BestActions.Pos then Think(Me);
   173         and ((Gear.State and gstHHThinking) = 0) then
   110         ProcessAction(BestActions, Me)
   174            if (BestActions.Pos = BestActions.Count) then StartThink(Gear)
   111         end
   175                                                     else ProcessAction(BestActions, Gear)
   112 end;
   176 end;
   113 
   177 
   114 end.
   178 end.