hedgewars/uAI.pas
changeset 7628 bc7b1d228a2c
parent 7453 92bdcd69bf2a
child 7787 67c96b9c179c
equal deleted inserted replaced
7533:7ee319134713 7628:bc7b1d228a2c
    29 procedure FreeActionsList;
    29 procedure FreeActionsList;
    30 
    30 
    31 implementation
    31 implementation
    32 uses uConsts, SDLh, uAIMisc, uAIAmmoTests, uAIActions,
    32 uses uConsts, SDLh, uAIMisc, uAIAmmoTests, uAIActions,
    33     uAmmos, SysUtils{$IFNDEF USE_SDLTHREADS} {$IFDEF UNIX}, cthreads{$ENDIF} {$ENDIF}, uTypes,
    33     uAmmos, SysUtils{$IFNDEF USE_SDLTHREADS} {$IFDEF UNIX}, cthreads{$ENDIF} {$ENDIF}, uTypes,
    34     uVariables, uCommands, uUtils, uDebug;
    34     uVariables, uCommands, uUtils, uDebug, uAILandMarks;
    35 
    35 
    36 var BestActions: TActions;
    36 var BestActions: TActions;
    37     CanUseAmmo: array [TAmmoType] of boolean;
    37     CanUseAmmo: array [TAmmoType] of boolean;
    38     StopThinking: boolean;
    38     StopThinking: boolean;
    39 {$IFDEF USE_SDLTHREADS} 
    39 {$IFDEF USE_SDLTHREADS} 
   176                         
   176                         
   177                     if (Ammoz[a].Ammo.Propz and ammoprop_OscAim) <> 0 then
   177                     if (Ammoz[a].Ammo.Propz and ammoprop_OscAim) <> 0 then
   178                         begin
   178                         begin
   179                         AddAction(BestActions, aia_attack, aim_push, 350 + random(200), 0, 0);
   179                         AddAction(BestActions, aia_attack, aim_push, 350 + random(200), 0, 0);
   180                         AddAction(BestActions, aia_attack, aim_release, 1, 0, 0);
   180                         AddAction(BestActions, aia_attack, aim_release, 1, 0, 0);
   181                                                 
   181                          
   182                         AddAction(BestActions, aia_Down, aim_push, 100 + random(150), 0, 0);
   182                         if abs(ap.Angle) > 32 then
   183                         AddAction(BestActions, aia_Down, aim_release, 32, 0, 0);
   183                            begin
       
   184                            AddAction(BestActions, aia_Down, aim_push, 100 + random(150), 0, 0);
       
   185                            AddAction(BestActions, aia_Down, aim_release, 32, 0, 0);
       
   186                            end;
   184                         
   187                         
   185                         AddAction(BestActions, aia_waitAngle, ap.Angle, 250, 0, 0);
   188                         AddAction(BestActions, aia_waitAngle, ap.Angle, 250, 0, 0);
   186                         AddAction(BestActions, aia_attack, aim_push, 1, 0, 0);
   189                         AddAction(BestActions, aia_attack, aim_push, 1, 0, 0);
   187                         AddAction(BestActions, aia_attack, aim_release, 1, 0, 0);
   190                         AddAction(BestActions, aia_attack, aim_release, 1, 0, 0);
   188                         end else
   191                         end else
   189                         if (Ammoz[a].Ammo.Propz and ammoprop_AttackingPut) = 0 then
   192                         if (Ammoz[a].Ammo.Propz and ammoprop_AttackingPut) = 0 then
   190                             begin
   193                             begin
   191                             AddAction(BestActions, aia_attack, aim_push, 650 + random(300), 0, 0);
   194                             AddAction(BestActions, aia_attack, aim_push, 650 + random(300), 0, 0);
   192                             AddAction(BestActions, aia_attack, aim_release, ap.Power, 0, 0);
   195                             AddAction(BestActions, aia_attack, aim_release, ap.Power, 0, 0);
   193                             end;
   196                             end;
   194                             
   197 
       
   198                     if (Ammoz[a].Ammo.Propz and ammoprop_Track) <> 0 then
       
   199                         begin
       
   200                         AddAction(BestActions, aia_waitAmmoXY, 0, 12, ap.ExplX, ap.ExplY);
       
   201                         AddAction(BestActions, aia_attack, aim_push, 1, 0, 0);
       
   202                         AddAction(BestActions, aia_attack, aim_release, 7, 0, 0);
       
   203                         end;
       
   204 
   195                     if ap.ExplR > 0 then
   205                     if ap.ExplR > 0 then
   196                         AddAction(BestActions, aia_AwareExpl, ap.ExplR, 10, ap.ExplX, ap.ExplY);
   206                         AddAction(BestActions, aia_AwareExpl, ap.ExplR, 10, ap.ExplX, ap.ExplY);
   197                     end
   207                     end
   198             end;
   208             end;
   199         if a = High(TAmmoType) then
   209         if a = High(TAmmoType) then
   203             or StopThinking
   213             or StopThinking
   204         end
   214         end
   205 end;
   215 end;
   206 
   216 
   207 procedure Walk(Me: PGear; var Actions: TActions);
   217 procedure Walk(Me: PGear; var Actions: TActions);
   208 const FallPixForBranching = cHHRadius * 2 + 8;
   218 const FallPixForBranching = cHHRadius;
   209 var
   219 var
   210     ticks, maxticks, steps, tmp: Longword;
   220     ticks, maxticks, steps, tmp: Longword;
   211     BaseRate, BestRate, Rate: integer;
   221     BaseRate, BestRate, Rate: integer;
   212     GoInfo: TGoInfo;
   222     GoInfo: TGoInfo;
   213     CanGo: boolean;
   223     CanGo: boolean;
   265             inc(ticks, GoInfo.Ticks);
   275             inc(ticks, GoInfo.Ticks);
   266             if ticks > maxticks then
   276             if ticks > maxticks then
   267                 break;
   277                 break;
   268 
   278 
   269             if (BotLevel < 5) and (GoInfo.JumpType = jmpHJump) then // hjump support
   279             if (BotLevel < 5) and (GoInfo.JumpType = jmpHJump) then // hjump support
   270                 if Push(ticks, Actions, AltMe, Me^.Message) then
   280                 // check if we could go backwards and maybe ljump over a gap after this hjump
       
   281                 if Push(ticks, Actions, AltMe, Me^.Message xor 3) then
       
   282                     begin
   271                     with Stack.States[Pred(Stack.Count)] do
   283                     with Stack.States[Pred(Stack.Count)] do
   272                         begin
   284                         begin
   273                         if Me^.dX.isNegative then
   285                         if Me^.dX.isNegative then
   274                             AddAction(MadeActions, aia_LookRight, 0, 200, 0, 0)
   286                             AddAction(MadeActions, aia_LookRight, 0, 200, 0, 0)
   275                         else
   287                         else
   281                         if Me^.dX.isNegative then
   293                         if Me^.dX.isNegative then
   282                             AddAction(MadeActions, aia_LookLeft, 0, 200, 0, 0)
   294                             AddAction(MadeActions, aia_LookLeft, 0, 200, 0, 0)
   283                         else
   295                         else
   284                             AddAction(MadeActions, aia_LookRight, 0, 200, 0, 0);
   296                             AddAction(MadeActions, aia_LookRight, 0, 200, 0, 0);
   285                         end;
   297                         end;
       
   298                     // but first check walking forward
       
   299                     Push(ticks, Stack.States[Pred(Stack.Count)].MadeActions, AltMe, Me^.Message)
       
   300                     end;
   286             if (BotLevel < 3) and (GoInfo.JumpType = jmpLJump) then // ljump support
   301             if (BotLevel < 3) and (GoInfo.JumpType = jmpLJump) then // ljump support
   287                 begin
   302                 begin
   288                 // push current position so we proceed from it after checking jump opportunities
   303                 // at final check where we go after jump walking backward
       
   304                 if Push(ticks, Actions, AltMe, Me^.Message xor 3) then
       
   305                     with Stack.States[Pred(Stack.Count)] do
       
   306                         AddAction(MadeActions, aia_LJump, 0, 305 + random(50), 0, 0);
       
   307 
       
   308                 // push current position so we proceed from it after checking jump+forward walk opportunities
   289                 if CanGo then Push(ticks, Actions, Me^, Me^.Message);
   309                 if CanGo then Push(ticks, Actions, Me^, Me^.Message);
   290                 // first check where we go after jump
   310                 
       
   311                 // first check where we go after jump walking forward
   291                 if Push(ticks, Actions, AltMe, Me^.Message) then
   312                 if Push(ticks, Actions, AltMe, Me^.Message) then
   292                     with Stack.States[Pred(Stack.Count)] do
   313                     with Stack.States[Pred(Stack.Count)] do
   293                         AddAction(MadeActions, aia_LJump, 0, 305 + random(50), 0, 0);
   314                         AddAction(MadeActions, aia_LJump, 0, 305 + random(50), 0, 0);
   294                 break
   315                 break
   295                 end;
   316                 end;
   308                 BestRate:= Rate;
   329                 BestRate:= Rate;
   309                 Me^.State:= Me^.State or gstAttacked // we have better place, go there and do not use ammo
   330                 Me^.State:= Me^.State or gstAttacked // we have better place, go there and do not use ammo
   310                 end
   331                 end
   311             else if Rate < BestRate then
   332             else if Rate < BestRate then
   312                 break;
   333                 break;
       
   334                 
   313             if ((Me^.State and gstAttacked) = 0) and ((steps mod 4) = 0) then
   335             if ((Me^.State and gstAttacked) = 0) and ((steps mod 4) = 0) then
       
   336                 begin
       
   337                 if (steps > 4) and checkMark(hwRound(Me^.X), hwRound(Me^.Y), markWasHere) then
       
   338                     break;                    
       
   339                 addMark(hwRound(Me^.X), hwRound(Me^.Y), markWasHere);
       
   340                 
   314                 TestAmmos(Actions, Me, true);
   341                 TestAmmos(Actions, Me, true);
       
   342                 end;
       
   343                 
   315             if GoInfo.FallPix >= FallPixForBranching then
   344             if GoInfo.FallPix >= FallPixForBranching then
   316                 Push(ticks, Actions, Me^, Me^.Message xor 3); // aia_Left xor 3 = aia_Right
   345                 Push(ticks, Actions, Me^, Me^.Message xor 3); // aia_Left xor 3 = aia_Right
   317             end {while};
   346             end {while};
   318 
   347 
   319         if BestRate > BaseRate then
   348         if BestRate > BaseRate then
   406         if not StopThinking then
   435         if not StopThinking then
   407             SDL_Delay(100)
   436             SDL_Delay(100)
   408         end
   437         end
   409     end;
   438     end;
   410 
   439 
   411 PGear(Me)^.State:= PGear(Me)^.State and not gstHHThinking;
   440 PGear(Me)^.State:= PGear(Me)^.State and (not gstHHThinking);
   412 Think:= 0;
   441 Think:= 0;
   413 InterlockedDecrement(hasThread)
   442 InterlockedDecrement(hasThread)
   414 end;
   443 end;
   415 
   444 
   416 procedure StartThink(Me: PGear);
   445 procedure StartThink(Me: PGear);
   417 begin
   446 begin
   418 if ((Me^.State and (gstAttacking or gstHHJumping or gstMoving)) <> 0)
   447 if ((Me^.State and (gstAttacking or gstHHJumping or gstMoving)) <> 0)
   419 or isInMultiShoot then
   448 or isInMultiShoot then
   420     exit;
   449     exit;
   421 
   450 
   422 //DeleteCI(Me); // this might break demo
   451 //DeleteCI(Me); // this will break demo/netplay
       
   452 clearAllMarks;
       
   453 
   423 Me^.State:= Me^.State or gstHHThinking;
   454 Me^.State:= Me^.State or gstHHThinking;
   424 Me^.Message:= 0;
   455 Me^.Message:= 0;
   425 
   456 
   426 BestActions.Count:= 0;
   457 BestActions.Count:= 0;
   427 BestActions.Pos:= 0;
   458 BestActions.Pos:= 0;
   474                 StartThink(Gear);
   505                 StartThink(Gear);
   475                 StartTicks:= GameTicks
   506                 StartTicks:= GameTicks
   476                 
   507                 
   477             end else
   508             end else
   478                 begin
   509                 begin
   479                 (*
   510                 {if not scoreShown then
   480                 if not scoreShown then
       
   481                     begin
   511                     begin
   482                     if BestActions.Score > 0 then ParseCommand('/say Expected score = ' + inttostr(BestActions.Score div 1024), true);
   512                     if BestActions.Score > 0 then ParseCommand('/say Expected score = ' + inttostr(BestActions.Score div 1024), true);
   483                     scoreShown:= true
   513                     scoreShown:= true
   484                     end;*)
   514                     end;}
   485                 ProcessAction(BestActions, Gear)
   515                 ProcessAction(BestActions, Gear)
   486                 end
   516                 end
   487         else if ((GameTicks - StartTicks) > cMaxAIThinkTime)
   517         else if ((GameTicks - StartTicks) > cMaxAIThinkTime)
   488             or (TurnTimeLeft <= cStopThinkTime) then
   518             or (TurnTimeLeft <= cStopThinkTime) then
   489                 StopThinking:= true
   519                 StopThinking:= true