hedgewars/uAI.pas
changeset 3407 dcc129c4352e
parent 3236 4ab3917d7d44
child 3615 b78d7959540a
equal deleted inserted replaced
3406:f4bdebced042 3407:dcc129c4352e
    28 procedure ProcessBot;
    28 procedure ProcessBot;
    29 procedure FreeActionsList;
    29 procedure FreeActionsList;
    30 
    30 
    31 implementation
    31 implementation
    32 uses uTeams, uConsts, SDLh, uAIMisc, uGears, uAIAmmoTests, uAIActions, uMisc,
    32 uses uTeams, uConsts, SDLh, uAIMisc, uGears, uAIAmmoTests, uAIActions, uMisc,
    33      uAmmos, uConsole, uCollisions, SysUtils{$IFDEF UNIX}, cthreads{$ENDIF};
    33      uAmmos, uConsole, SysUtils{$IFDEF UNIX}, cthreads{$ENDIF};
    34 
    34 
    35 var BestActions: TActions;
    35 var BestActions: TActions;
    36     CanUseAmmo: array [TAmmoType] of boolean;
    36     CanUseAmmo: array [TAmmoType] of boolean;
    37     StopThinking: boolean;
    37     StopThinking: boolean;
    38     ThinkThread: TThreadID;
    38     ThinkThread: TThreadID;
    58 BestActions.Count:= 0;
    58 BestActions.Count:= 0;
    59 BestActions.Pos:= 0
    59 BestActions.Pos:= 0
    60 end;
    60 end;
    61 
    61 
    62 procedure TestAmmos(var Actions: TActions; Me: PGear; isMoved: boolean);
    62 procedure TestAmmos(var Actions: TActions; Me: PGear; isMoved: boolean);
    63 var BotLevel: Longword;
    63 var BotLevel: Byte;
    64     ap: TAttackParams;
    64     ap: TAttackParams;
    65     Score, i: LongInt;
    65     Score, i: LongInt;
    66     a, aa: TAmmoType;
    66     a, aa: TAmmoType;
    67 begin
    67 begin
    68 BotLevel:= PHedgehog(Me^.Hedgehog)^.BotLevel;
    68 BotLevel:= PHedgehog(Me^.Hedgehog)^.BotLevel;
    75        aa:= a;
    75        aa:= a;
    76        repeat
    76        repeat
    77         if (CanUseAmmo[a]) and
    77         if (CanUseAmmo[a]) and
    78            ((not isMoved) or ((AmmoTests[a].flags and amtest_OnTurn) = 0)) then
    78            ((not isMoved) or ((AmmoTests[a].flags and amtest_OnTurn) = 0)) then
    79            begin
    79            begin
       
    80 {$HINTS OFF}
    80            Score:= AmmoTests[a].proc(Me, Targets.ar[i].Point, BotLevel, ap);
    81            Score:= AmmoTests[a].proc(Me, Targets.ar[i].Point, BotLevel, ap);
       
    82 {$HINTS ON}
    81            if Actions.Score + Score > BestActions.Score then
    83            if Actions.Score + Score > BestActions.Score then
    82             if (BestActions.Score < 0) or (Actions.Score + Score > BestActions.Score + LongInt(BotLevel) * 2048) then
    84             if (BestActions.Score < 0) or (Actions.Score + Score > BestActions.Score + Byte(BotLevel) * 2048) then
    83               begin
    85               begin
    84               BestActions:= Actions;
    86               BestActions:= Actions;
    85               inc(BestActions.Score, Score);
    87               inc(BestActions.Score, Score);
    86 
    88 
    87               AddAction(BestActions, aia_Weapon, Longword(a), 300 + random(400), 0, 0);
    89               AddAction(BestActions, aia_Weapon, Longword(a), 300 + random(400), 0, 0);
   178     PosInThinkStack:= false
   180     PosInThinkStack:= false
   179     end;
   181     end;
   180 
   182 
   181 
   183 
   182 var Actions: TActions;
   184 var Actions: TActions;
   183     ticks, maxticks, steps, BotLevel, tmp: Longword;
   185     ticks, maxticks, steps, tmp: Longword;
   184     BaseRate, BestRate, Rate: integer;
   186     BaseRate, BestRate, Rate: integer;
   185     GoInfo: TGoInfo;
   187     GoInfo: TGoInfo;
   186     CanGo: boolean;
   188     CanGo: boolean;
   187     AltMe: TGear;
   189     AltMe: TGear;
   188 begin
   190     BotLevel: Byte;
       
   191 begin
       
   192 ticks:= 0; // avoid compiler hint
   189 Actions.Count:= 0;
   193 Actions.Count:= 0;
   190 Actions.Pos:= 0;
   194 Actions.Pos:= 0;
   191 Actions.Score:= 0;
   195 Actions.Score:= 0;
   192 Stack.Count:= 0;
   196 Stack.Count:= 0;
   193 BotLevel:= PHedgehog(Me^.Hedgehog)^.BotLevel;
   197 BotLevel:= PHedgehog(Me^.Hedgehog)^.BotLevel;
   194 
   198 
   195 tmp:= random(2) + 1;
   199 tmp:= random(2) + 1;
   196 Push(0, Actions, Me^, tmp);
   200 Push(0, Actions, Me^, tmp);
   197 Push(0, Actions, Me^, tmp xor 3);
   201 Push(0, Actions, Me^, tmp xor 3);
   198 
   202 
   199 if (Me^.State and gstAttacked) = 0 then maxticks:= max(0, TurnTimeLeft - 5000 - 4000 * BotLevel)
   203 if (Me^.State and gstAttacked) = 0 then maxticks:= max(0, TurnTimeLeft - 5000 - LongWord(4000 * BotLevel))
   200                                    else maxticks:= TurnTimeLeft;
   204                                    else maxticks:= TurnTimeLeft;
   201 
   205 
   202 if (Me^.State and gstAttacked) = 0 then TestAmmos(Actions, Me, false);
   206 if (Me^.State and gstAttacked) = 0 then TestAmmos(Actions, Me, false);
   203 BestRate:= RatePlace(Me);
   207 BestRate:= RatePlace(Me);
   204 BaseRate:= max(BestRate, 0);
   208 BaseRate:= max(BestRate, 0);
   212                                       else AddAction(Actions, aia_WaitXR, hwRound(Me^.X), 0, 0, 0);
   216                                       else AddAction(Actions, aia_WaitXR, hwRound(Me^.X), 0, 0, 0);
   213     steps:= 0;
   217     steps:= 0;
   214 
   218 
   215     while (not StopThinking) and (not PosInThinkStack(Me)) do
   219     while (not StopThinking) and (not PosInThinkStack(Me)) do
   216        begin
   220        begin
       
   221 {$HINTS OFF}
   217        CanGo:= HHGo(Me, @AltMe, GoInfo);
   222        CanGo:= HHGo(Me, @AltMe, GoInfo);
       
   223 {$HINTS ON}
   218        inc(ticks, GoInfo.Ticks);
   224        inc(ticks, GoInfo.Ticks);
   219        if ticks > maxticks then break;
   225        if ticks > maxticks then break;
   220 
   226 
   221        if (BotLevel < 5) and (GoInfo.JumpType = jmpHJump) then // hjump support
   227        if (BotLevel < 5) and (GoInfo.JumpType = jmpHJump) then // hjump support
   222           if Push(ticks, Actions, AltMe, Me^.Message) then
   228           if Push(ticks, Actions, AltMe, Me^.Message) then