hedgewars/uAI.pas
changeset 500 d9b140e9d2c2
parent 498 9c8b385dc9a1
child 506 0889d833d47e
equal deleted inserted replaced
499:d948311b33a3 500:d9b140e9d2c2
    23 procedure ProcessBot;
    23 procedure ProcessBot;
    24 procedure FreeActionsList;
    24 procedure FreeActionsList;
    25 
    25 
    26 implementation
    26 implementation
    27 uses uTeams, uConsts, SDLh, uAIMisc, uGears, uAIAmmoTests, uAIActions, uMisc,
    27 uses uTeams, uConsts, SDLh, uAIMisc, uGears, uAIAmmoTests, uAIActions, uMisc,
    28      uAmmos, uConsole;
    28      uAmmos, uConsole{$IFDEF UNIX}, cthreads{$ENDIF};
    29 
    29 
    30 var BestActions: TActions;
    30 var BestActions: TActions;
    31     ThinkThread: PSDL_Thread = nil;
    31     ThinkThread: THandle = 0;
    32     StopThinking: boolean;
    32     StopThinking: boolean;
    33     CanUseAmmo: array [TAmmoType] of boolean;
    33     CanUseAmmo: array [TAmmoType] of boolean;
    34 
    34 
    35 procedure FreeActionsList;
    35 procedure FreeActionsList;
    36 begin
    36 begin
    37 {$IFDEF DEBUGFILE}AddFileLog('FreeActionsList called');{$ENDIF}
    37 {$IFDEF DEBUGFILE}AddFileLog('FreeActionsList called');{$ENDIF}
    38 if ThinkThread <> nil then
    38 if ThinkThread <> 0 then
    39    begin
    39    begin
    40    {$IFDEF DEBUGFILE}AddFileLog('Waiting AI thread to finish');{$ENDIF}
    40    {$IFDEF DEBUGFILE}AddFileLog('Waiting AI thread to finish');{$ENDIF}
    41    StopThinking:= true;
    41    StopThinking:= true;
    42    SDL_WaitThread(ThinkThread, nil);
    42    WaitForThreadTerminate(ThinkThread, 5000);
    43    ThinkThread:= nil
    43    ThinkThread:= 0
    44    end;
    44    end;
    45 
    45 
    46    with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do
    46 with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do
    47      if Gear <> nil then
    47      if Gear <> nil then
    48         if BotLevel <> 0 then
    48         if BotLevel <> 0 then
    49            begin
    49            begin
    50            if (Gear^.Message and gm_Left) <> 0 then ParseCommand('-left', true);
    50            if (Gear^.Message and gm_Left) <> 0 then ParseCommand('-left', true);
    51            if (Gear^.Message and gm_Right) <> 0 then ParseCommand('-right', true);
    51            if (Gear^.Message and gm_Right) <> 0 then ParseCommand('-right', true);
   235 
   235 
   236     if BestRate > BaseRate then exit
   236     if BestRate > BaseRate then exit
   237     end
   237     end
   238 end;
   238 end;
   239 
   239 
   240 procedure Think(Me: PGear); cdecl;
   240 function Think(Me: Pointer): LongInt;
   241 var BackMe, WalkMe: TGear;
   241 var BackMe, WalkMe: TGear;
   242     StartTicks: Longword;
   242     StartTicks: Longword;
   243 begin
   243 begin
   244 StartTicks:= GameTicks;
   244 StartTicks:= GameTicks;
   245 BestActions.Count:= 0;
   245 BestActions.Count:= 0;
   246 BestActions.Pos:= 0;
   246 BestActions.Pos:= 0;
   247 BestActions.Score:= Low(integer);
   247 BestActions.Score:= Low(integer);
   248 BackMe:= Me^;
   248 BackMe:= PGear(Me)^;
   249 WalkMe:= BackMe;
   249 WalkMe:= BackMe;
   250 if (Me^.State and gstAttacked) = 0 then
   250 if (PGear(Me)^.State and gstAttacked) = 0 then
   251    if Targets.Count > 0 then
   251    if Targets.Count > 0 then
   252       begin
   252       begin
   253       Walk(@WalkMe);
   253       Walk(@WalkMe);
   254       if (StartTicks > GameTicks - 1500) and not StopThinking then SDL_Delay(2000);
   254       if (StartTicks > GameTicks - 1500) and not StopThinking then SDL_Delay(2000);
   255       if BestActions.Score < -1023 then
   255       if BestActions.Score < -1023 then
   266             FillBonuses(true);
   266             FillBonuses(true);
   267             WalkMe:= BackMe;
   267             WalkMe:= BackMe;
   268             Walk(@WalkMe)
   268             Walk(@WalkMe)
   269             end
   269             end
   270       end;
   270       end;
   271 Me^.State:= Me^.State and not gstHHThinking
   271 PGear(Me)^.State:= PGear(Me)^.State and not gstHHThinking;
       
   272 Think:= 0
   272 end;
   273 end;
   273 
   274 
   274 procedure StartThink(Me: PGear);
   275 procedure StartThink(Me: PGear);
   275 var a: TAmmoType;
   276 var a: TAmmoType;
   276 begin
   277 begin
   288    end;
   289    end;
   289 FillBonuses((Me^.State and gstAttacked) <> 0);
   290 FillBonuses((Me^.State and gstAttacked) <> 0);
   290 for a:= Low(TAmmoType) to High(TAmmoType) do
   291 for a:= Low(TAmmoType) to High(TAmmoType) do
   291     CanUseAmmo[a]:= Assigned(AmmoTests[a]) and HHHasAmmo(PHedgehog(Me^.Hedgehog), a);
   292     CanUseAmmo[a]:= Assigned(AmmoTests[a]) and HHHasAmmo(PHedgehog(Me^.Hedgehog), a);
   292 {$IFDEF DEBUGFILE}AddFileLog('Enter Think Thread');{$ENDIF}
   293 {$IFDEF DEBUGFILE}AddFileLog('Enter Think Thread');{$ENDIF}
   293 ThinkThread:= SDL_CreateThread(@Think, Me)
   294 BeginThread(@Think, Me, ThinkThread)
   294 end;
   295 end;
   295 
   296 
   296 procedure ProcessBot;
   297 procedure ProcessBot;
   297 const StartTicks: Longword = 0;
   298 const StartTicks: Longword = 0;
   298 begin
   299 begin