hedgewars/uAI.pas
branchhedgeroid
changeset 5504 96d735b83d43
parent 5495 272ed78e59a7
child 5516 8710987d3484
equal deleted inserted replaced
5502:0d1a1aeed00a 5504:96d735b83d43
    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{$IFDEF UNIX}{$IFNDEF ANDROID}, cthreads{$ENDIF}{$ENDIF}, uTypes,
    33      uAmmos, SysUtils{$IFDEF UNIX}{$IFNDEF ANDROID}, cthreads{$ENDIF}{$ENDIF}, uTypes,
    34      uVariables, uCommands, uUtils, uDebug;
    34      uVariables, uCommands, uUtils, uDebug, uConsole;
    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     ThinkThread: TThreadID;
    39     ThinkThread: PSDL_Thread = nil;
    40     hasThread: LongInt;
    40     hasThread: LongInt;
    41 
    41 
    42 procedure FreeActionsList;
    42 procedure FreeActionsList;
    43 begin
    43 begin
    44 AddFileLog('FreeActionsList called');
    44 AddFileLog('FreeActionsList called');
    64 var BotLevel: Byte;
    64 var BotLevel: Byte;
    65     ap: TAttackParams;
    65     ap: TAttackParams;
    66     Score, i: LongInt;
    66     Score, i: LongInt;
    67     a, aa: TAmmoType;
    67     a, aa: TAmmoType;
    68 begin
    68 begin
       
    69 WriteToConsole('testing ammo');
    69 BotLevel:= Me^.Hedgehog^.BotLevel;
    70 BotLevel:= Me^.Hedgehog^.BotLevel;
    70 
    71 
    71 for i:= 0 to Pred(Targets.Count) do
    72 for i:= 0 to Pred(Targets.Count) do
    72     if (Targets.ar[i].Score >= 0) and (not StopThinking) then
    73     if (Targets.ar[i].Score >= 0) and (not StopThinking) then
    73        begin
    74        begin
    74        with CurrentHedgehog^ do
    75        with CurrentHedgehog^ do
    75             a:= CurAmmoType;
    76             a:= CurAmmoType;
    76        aa:= a;
    77        aa:= a;
    77        
    78 WriteToConsole('start walking');
    78        ThreadSwitch();
    79 SDL_delay(0);
       
    80 //       ThreadSwitch();
       
    81 WriteToConsole('start walking');
    79        
    82        
    80        repeat
    83        repeat
    81         if (CanUseAmmo[a]) and
    84         if (CanUseAmmo[a]) and
    82            ((not isMoved) or ((AmmoTests[a].flags and amtest_OnTurn) = 0)) then
    85            ((not isMoved) or ((AmmoTests[a].flags and amtest_OnTurn) = 0)) then
    83            begin
    86            begin
   202 BotLevel:= Me^.Hedgehog^.BotLevel;
   205 BotLevel:= Me^.Hedgehog^.BotLevel;
   203 
   206 
   204 tmp:= random(2) + 1;
   207 tmp:= random(2) + 1;
   205 Push(0, Actions, Me^, tmp);
   208 Push(0, Actions, Me^, tmp);
   206 Push(0, Actions, Me^, tmp xor 3);
   209 Push(0, Actions, Me^, tmp xor 3);
       
   210 WriteToConsole('start walking');
   207 
   211 
   208 if (Me^.State and gstAttacked) = 0 then maxticks:= Max(0, TurnTimeLeft - 5000 - LongWord(4000 * BotLevel))
   212 if (Me^.State and gstAttacked) = 0 then maxticks:= Max(0, TurnTimeLeft - 5000 - LongWord(4000 * BotLevel))
   209                                    else maxticks:= TurnTimeLeft;
   213                                    else maxticks:= TurnTimeLeft;
       
   214 WriteToConsole('start walking');
   210 
   215 
   211 if (Me^.State and gstAttacked) = 0 then TestAmmos(Actions, Me, false);
   216 if (Me^.State and gstAttacked) = 0 then TestAmmos(Actions, Me, false);
       
   217 WriteToConsole('start walking');
   212 BestRate:= RatePlace(Me);
   218 BestRate:= RatePlace(Me);
   213 BaseRate:= Max(BestRate, 0);
   219 BaseRate:= Max(BestRate, 0);
       
   220 WriteToConsole('start walking');
   214 
   221 
   215 if (Ammoz[Me^.Hedgehog^.CurAmmoType].Ammo.Propz and ammoprop_NeedTarget) <> 0 then
   222 if (Ammoz[Me^.Hedgehog^.CurAmmoType].Ammo.Propz and ammoprop_NeedTarget) <> 0 then
   216     AddAction(Actions, aia_Weapon, Longword(amNothing), 100 + random(200), 0, 0);
   223     AddAction(Actions, aia_Weapon, Longword(amNothing), 100 + random(200), 0, 0);
       
   224 WriteToConsole('start walking');
   217 
   225 
   218 while (Stack.Count > 0) and (not StopThinking) and (GameFlags and gfArtillery = 0) do
   226 while (Stack.Count > 0) and (not StopThinking) and (GameFlags and gfArtillery = 0) do
   219     begin
   227     begin
       
   228 WriteToConsole('start walking');
   220     Pop(ticks, Actions, Me^);
   229     Pop(ticks, Actions, Me^);
   221 
   230 
   222     AddAction(Actions, Me^.Message, aim_push, 250, 0, 0);
   231     AddAction(Actions, Me^.Message, aim_push, 250, 0, 0);
   223     if (Me^.Message and gmLeft) <> 0 then AddAction(Actions, aia_WaitXL, hwRound(Me^.X), 0, 0, 0)
   232     if (Me^.Message and gmLeft) <> 0 then AddAction(Actions, aia_WaitXL, hwRound(Me^.X), 0, 0, 0)
   224                                       else AddAction(Actions, aia_WaitXR, hwRound(Me^.X), 0, 0, 0);
   233                                       else AddAction(Actions, aia_WaitXR, hwRound(Me^.X), 0, 0, 0);
   229 {$HINTS OFF}
   238 {$HINTS OFF}
   230        CanGo:= HHGo(Me, @AltMe, GoInfo);
   239        CanGo:= HHGo(Me, @AltMe, GoInfo);
   231 {$HINTS ON}
   240 {$HINTS ON}
   232        inc(ticks, GoInfo.Ticks);
   241        inc(ticks, GoInfo.Ticks);
   233        if ticks > maxticks then break;
   242        if ticks > maxticks then break;
       
   243 WriteToConsole('start walking');
   234 
   244 
   235        if (BotLevel < 5) and (GoInfo.JumpType = jmpHJump) then // hjump support
   245        if (BotLevel < 5) and (GoInfo.JumpType = jmpHJump) then // hjump support
   236           if Push(ticks, Actions, AltMe, Me^.Message) then
   246           if Push(ticks, Actions, AltMe, Me^.Message) then
   237              with Stack.States[Pred(Stack.Count)] do
   247              with Stack.States[Pred(Stack.Count)] do
   238                   begin
   248                   begin
   250 
   260 
   251        if not CanGo then break;
   261        if not CanGo then break;
   252        inc(steps);
   262        inc(steps);
   253        Actions.actions[Pred(Actions.Count)].Param:= hwRound(Me^.X);
   263        Actions.actions[Pred(Actions.Count)].Param:= hwRound(Me^.X);
   254        Rate:= RatePlace(Me);
   264        Rate:= RatePlace(Me);
       
   265 WriteToConsole('start walking');
   255        if Rate > BestRate then
   266        if Rate > BestRate then
   256           begin
   267           begin
   257           BestActions:= Actions;
   268           BestActions:= Actions;
   258           BestRate:= Rate;
   269           BestRate:= Rate;
   259           Me^.State:= Me^.State or gstAttacked // we have better place, go there and do not use ammo
   270           Me^.State:= Me^.State or gstAttacked // we have better place, go there and do not use ammo
   260           end
   271           end
   261        else if Rate < BestRate then break;
   272        else if Rate < BestRate then break;
       
   273 WriteToConsole('start walking');
   262        if ((Me^.State and gstAttacked) = 0)
   274        if ((Me^.State and gstAttacked) = 0)
   263            and ((steps mod 4) = 0) then TestAmmos(Actions, Me, true);
   275            and ((steps mod 4) = 0) then TestAmmos(Actions, Me, true);
       
   276 WriteToConsole('start walking');
   264        if GoInfo.FallPix >= FallPixForBranching then
   277        if GoInfo.FallPix >= FallPixForBranching then
   265           Push(ticks, Actions, Me^, Me^.Message xor 3); // aia_Left xor 3 = aia_Right
   278           Push(ticks, Actions, Me^, Me^.Message xor 3); // aia_Left xor 3 = aia_Right
   266        end;
   279        end;
   267 
   280 
   268     if BestRate > BaseRate then exit
   281     if BestRate > BaseRate then exit
   271 
   284 
   272 function Think(Me: Pointer): ptrint;
   285 function Think(Me: Pointer): ptrint;
   273 var BackMe, WalkMe: TGear;
   286 var BackMe, WalkMe: TGear;
   274     StartTicks: Longword;
   287     StartTicks: Longword;
   275 begin
   288 begin
       
   289 WriteToConsole('starting to think');
   276 InterlockedIncrement(hasThread);
   290 InterlockedIncrement(hasThread);
       
   291 WriteToConsole('bla');
   277 StartTicks:= GameTicks;
   292 StartTicks:= GameTicks;
       
   293 WriteToConsole('bla');
   278 BackMe:= PGear(Me)^;
   294 BackMe:= PGear(Me)^;
       
   295 WriteToConsole('bla');
   279 
   296 
   280 if (PGear(Me)^.State and gstAttacked) = 0 then
   297 if (PGear(Me)^.State and gstAttacked) = 0 then
   281    if Targets.Count > 0 then
   298    if Targets.Count > 0 then
   282       begin
   299       begin
       
   300 WriteToConsole('blaaa');
       
   301 
   283       WalkMe:= BackMe;
   302       WalkMe:= BackMe;
       
   303 WriteToConsole('blaa');
   284       Walk(@WalkMe);
   304       Walk(@WalkMe);
       
   305 WriteToConsole('blaa');
   285       if (StartTicks > GameTicks - 1500) and not StopThinking then SDL_Delay(1000);
   306       if (StartTicks > GameTicks - 1500) and not StopThinking then SDL_Delay(1000);
       
   307 	 WriteToConsole('bla');
       
   308 
   286       if BestActions.Score < -1023 then
   309       if BestActions.Score < -1023 then
   287          begin
   310          begin
       
   311 	 WriteToConsole('bla');
   288          BestActions.Count:= 0;
   312          BestActions.Count:= 0;
   289          AddAction(BestActions, aia_Skip, 0, 250, 0, 0);
   313          AddAction(BestActions, aia_Skip, 0, 250, 0, 0);
       
   314 	 WriteToConsole('bla');
   290          end;
   315          end;
   291       end else
   316       end else
   292 else begin
   317 else begin
   293       while (not StopThinking) and (BestActions.Count = 0) do
   318       while (not StopThinking) and (BestActions.Count = 0) do
   294             begin
   319             begin
       
   320 WriteToConsole('bla loop');
   295             FillBonuses(true);
   321             FillBonuses(true);
   296             WalkMe:= BackMe;
   322             WalkMe:= BackMe;
   297             Walk(@WalkMe);
   323             Walk(@WalkMe);
   298             if not StopThinking then SDL_Delay(100)
   324             if not StopThinking then SDL_Delay(100)
   299             end
   325             end
   300       end;
   326       end;
   301 PGear(Me)^.State:= PGear(Me)^.State and not gstHHThinking;
   327 PGear(Me)^.State:= PGear(Me)^.State and not gstHHThinking;
   302 Think:= 0;
   328 Think:= 0;
   303 InterlockedDecrement(hasThread)
   329 WriteToConsole('bla');
       
   330 
       
   331 InterlockedDecrement(hasThread);
       
   332 WriteToConsole('bla end');
       
   333 
   304 end;
   334 end;
   305 
   335 
   306 procedure StartThink(Me: PGear);
   336 procedure StartThink(Me: PGear);
   307 var a: TAmmoType;
   337 var a: TAmmoType;
   308 begin
   338 begin
   329 
   359 
   330 FillBonuses((Me^.State and gstAttacked) <> 0);
   360 FillBonuses((Me^.State and gstAttacked) <> 0);
   331 for a:= Low(TAmmoType) to High(TAmmoType) do
   361 for a:= Low(TAmmoType) to High(TAmmoType) do
   332     CanUseAmmo[a]:= Assigned(AmmoTests[a].proc) and HHHasAmmo(Me^.Hedgehog^, a);
   362     CanUseAmmo[a]:= Assigned(AmmoTests[a].proc) and HHHasAmmo(Me^.Hedgehog^, a);
   333 AddFileLog('Enter Think Thread');
   363 AddFileLog('Enter Think Thread');
   334 BeginThread(@Think, Me, ThinkThread)
   364 //BeginThread(@Think, Me, ThinkThread)
       
   365 ThinkThread := SDL_CreateThread(@Think, Me);
       
   366 AddFileLog('Thread started');
   335 end;
   367 end;
   336 
   368 
   337 procedure ProcessBot;
   369 procedure ProcessBot;
   338 const StartTicks: Longword = 0;
   370 const StartTicks: Longword = 0;
   339       cStopThinkTime = 40;
   371       cStopThinkTime = 40;