hedgewars/uAIMisc.pas
changeset 371 731ad6d27bd1
parent 370 c75410fe3133
child 375 18012da67681
equal deleted inserted replaced
370:c75410fe3133 371:731ad6d27bd1
    21 uses SDLh, uConsts, uGears, uFloat;
    21 uses SDLh, uConsts, uGears, uFloat;
    22 {$INCLUDE options.inc}
    22 {$INCLUDE options.inc}
    23 
    23 
    24 type TTarget = record
    24 type TTarget = record
    25                Point: TPoint;
    25                Point: TPoint;
    26                Score: integer;
    26                Score: LongInt;
    27                end;
    27                end;
    28      TTargets = record
    28      TTargets = record
    29                 Count: Longword;
    29                 Count: Longword;
    30                 ar: array[0..cMaxHHIndex*5] of TTarget;
    30                 ar: array[0..cMaxHHIndex*5] of TTarget;
    31                 end;
    31                 end;
    36                JumpType: TJumpType;
    36                JumpType: TJumpType;
    37                end;
    37                end;
    38 
    38 
    39 procedure FillTargets;
    39 procedure FillTargets;
    40 procedure FillBonuses(isAfterAttack: boolean);
    40 procedure FillBonuses(isAfterAttack: boolean);
    41 procedure AwareOfExplosion(x, y, r: integer);
    41 procedure AwareOfExplosion(x, y, r: LongInt);
    42 function RatePlace(Gear: PGear): integer;
    42 function RatePlace(Gear: PGear): LongInt;
    43 function TestColl(x, y, r: integer): boolean;
    43 function TestColl(x, y, r: LongInt): boolean;
    44 function RateExplosion(Me: PGear; x, y, r: integer): integer;
    44 function RateExplosion(Me: PGear; x, y, r: LongInt): LongInt;
    45 function RateShove(Me: PGear; x, y, r, power: integer): integer;
    45 function RateShove(Me: PGear; x, y, r, power: LongInt): LongInt;
    46 function HHGo(Gear, AltGear: PGear; var GoInfo: TGoInfo): boolean;
    46 function HHGo(Gear, AltGear: PGear; var GoInfo: TGoInfo): boolean;
    47 function AIrndSign(num: integer): integer;
    47 function AIrndSign(num: LongInt): LongInt;
    48 
    48 
    49 var ThinkingHH: PGear;
    49 var ThinkingHH: PGear;
    50     Targets: TTargets;
    50     Targets: TTargets;
    51                        
    51                        
    52 implementation
    52 implementation
    53 uses uTeams, uMisc, uLand, uCollisions;
    53 uses uTeams, uMisc, uLand, uCollisions;
    54 const KillScore = 200;
    54 const KillScore = 200;
    55       MAXBONUS = 1024;
    55       MAXBONUS = 1024;
    56 
    56 
    57 type TBonus = record
    57 type TBonus = record
    58               X, Y: integer;
    58               X, Y: LongInt;
    59               Radius: integer;
    59               Radius: LongInt;
    60               Score: integer;
    60               Score: LongInt;
    61               end;
    61               end;
    62 var bonuses: record
    62 var bonuses: record
    63              Count: Longword;
    63              Count: Longword;
    64              ar: array[0..Pred(MAXBONUS)] of TBonus;
    64              ar: array[0..Pred(MAXBONUS)] of TBonus;
    65              end;
    65              end;
    66     KnownExplosion: record
    66     KnownExplosion: record
    67                     X, Y, Radius: integer
    67                     X, Y, Radius: LongInt
    68                     end = (X: 0; Y: 0; Radius: 0);
    68                     end = (X: 0; Y: 0; Radius: 0);
    69 
    69 
    70 procedure FillTargets;
    70 procedure FillTargets;
    71 var t: PTeam;
    71 var t: PTeam;
    72     i: Longword;
    72     i: Longword;
    94 
    94 
    95 procedure FillBonuses(isAfterAttack: boolean);
    95 procedure FillBonuses(isAfterAttack: boolean);
    96 var Gear: PGear;
    96 var Gear: PGear;
    97     MyColor: Longword;
    97     MyColor: Longword;
    98 
    98 
    99     procedure AddBonus(x, y: integer; r: Longword; s: integer);
    99     procedure AddBonus(x, y: LongInt; r: Longword; s: LongInt);
   100     begin
   100     begin
   101     bonuses.ar[bonuses.Count].x:= x;
   101     bonuses.ar[bonuses.Count].x:= x;
   102     bonuses.ar[bonuses.Count].y:= y;
   102     bonuses.ar[bonuses.Count].y:= y;
   103     bonuses.ar[bonuses.Count].Radius:= r;
   103     bonuses.ar[bonuses.Count].Radius:= r;
   104     bonuses.ar[bonuses.Count].Score:= s;
   104     bonuses.ar[bonuses.Count].Score:= s;
   129 if isAfterAttack and (KnownExplosion.Radius > 0) then
   129 if isAfterAttack and (KnownExplosion.Radius > 0) then
   130    with KnownExplosion do
   130    with KnownExplosion do
   131         AddBonus(X, Y, Radius + 10, -Radius);
   131         AddBonus(X, Y, Radius + 10, -Radius);
   132 end;
   132 end;
   133 
   133 
   134 procedure AwareOfExplosion(x, y, r: integer);
   134 procedure AwareOfExplosion(x, y, r: LongInt);
   135 begin
   135 begin
   136 KnownExplosion.X:= x;
   136 KnownExplosion.X:= x;
   137 KnownExplosion.Y:= y;
   137 KnownExplosion.Y:= y;
   138 KnownExplosion.Radius:= r
   138 KnownExplosion.Radius:= r
   139 end;
   139 end;
   140 
   140 
   141 function RatePlace(Gear: PGear): integer;
   141 function RatePlace(Gear: PGear): LongInt;
   142 var i, r: integer;
   142 var i, r: LongInt;
   143     Result: integer;
   143     Result: LongInt;
   144 begin
   144 begin
   145 Result:= 0;
   145 Result:= 0;
   146 for i:= 0 to Pred(bonuses.Count) do
   146 for i:= 0 to Pred(bonuses.Count) do
   147     with bonuses.ar[i] do
   147     with bonuses.ar[i] do
   148          begin
   148          begin
   151             inc(Result, Score * (Radius - r))
   151             inc(Result, Score * (Radius - r))
   152          end;
   152          end;
   153     RatePlace:= Result
   153     RatePlace:= Result
   154 end;
   154 end;
   155 
   155 
   156 function TestColl(x, y, r: integer): boolean;
   156 function TestColl(x, y, r: LongInt): boolean;
   157 var b: boolean;
   157 var b: boolean;
   158 begin
   158 begin
   159 b:= (((x-r) and $FFFFF800) = 0)and(((y-r) and $FFFFFC00) = 0) and (Land[y-r, x-r] <> 0);
   159 b:= (((x-r) and $FFFFF800) = 0)and(((y-r) and $FFFFFC00) = 0) and (Land[y-r, x-r] <> 0);
   160 if b then exit(true);
   160 if b then exit(true);
   161 b:=(((x-r) and $FFFFF800) = 0)and(((y+r) and $FFFFFC00) = 0) and (Land[y+r, x-r] <> 0);
   161 b:=(((x-r) and $FFFFF800) = 0)and(((y+r) and $FFFFFC00) = 0) and (Land[y+r, x-r] <> 0);
   163 b:=(((x+r) and $FFFFF800) = 0)and(((y-r) and $FFFFFC00) = 0) and (Land[y-r, x+r] <> 0);
   163 b:=(((x+r) and $FFFFF800) = 0)and(((y-r) and $FFFFFC00) = 0) and (Land[y-r, x+r] <> 0);
   164 if b then exit(true);
   164 if b then exit(true);
   165 TestColl:=(((x+r) and $FFFFF800) = 0)and(((y+r) and $FFFFFC00) = 0) and (Land[y+r, x+r] <> 0)
   165 TestColl:=(((x+r) and $FFFFF800) = 0)and(((y+r) and $FFFFFC00) = 0) and (Land[y+r, x+r] <> 0)
   166 end;
   166 end;
   167 
   167 
   168 function RateExplosion(Me: PGear; x, y, r: integer): integer;
   168 function RateExplosion(Me: PGear; x, y, r: LongInt): LongInt;
   169 var i, dmg, Result: integer;
   169 var i, dmg, Result: LongInt;
   170 begin
   170 begin
   171 Result:= 0;
   171 Result:= 0;
   172 // add our virtual position
   172 // add our virtual position
   173 with Targets.ar[Targets.Count] do
   173 with Targets.ar[Targets.Count] do
   174      begin
   174      begin
   193             end;
   193             end;
   194          end;
   194          end;
   195 RateExplosion:= Result * 1024
   195 RateExplosion:= Result * 1024
   196 end;
   196 end;
   197 
   197 
   198 function RateShove(Me: PGear; x, y, r, power: integer): integer;
   198 function RateShove(Me: PGear; x, y, r, power: LongInt): LongInt;
   199 var i, dmg, Result: integer;
   199 var i, dmg, Result: LongInt;
   200 begin
   200 begin
   201 Result:= 0;
   201 Result:= 0;
   202 for i:= 0 to Targets.Count do
   202 for i:= 0 to Targets.Count do
   203     with Targets.ar[i] do
   203     with Targets.ar[i] do
   204          begin
   204          begin
   215          end;
   215          end;
   216 RateShove:= Result * 1024
   216 RateShove:= Result * 1024
   217 end;
   217 end;
   218 
   218 
   219 function HHJump(Gear: PGear; JumpType: TJumpType; var GoInfo: TGoInfo): boolean;
   219 function HHJump(Gear: PGear; JumpType: TJumpType; var GoInfo: TGoInfo): boolean;
   220 var bX, bY: integer;
   220 var bX, bY: LongInt;
   221     Result: boolean;
   221     Result: boolean;
   222 begin
   222 begin
   223 Result:= false;
   223 Result:= false;
   224 GoInfo.Ticks:= 0;
   224 GoInfo.Ticks:= 0;
   225 GoInfo.FallPix:= 0;
   225 GoInfo.FallPix:= 0;
   288    end;
   288    end;
   289 until false
   289 until false
   290 end;
   290 end;
   291 
   291 
   292 function HHGo(Gear, AltGear: PGear; var GoInfo: TGoInfo): boolean;
   292 function HHGo(Gear, AltGear: PGear; var GoInfo: TGoInfo): boolean;
   293 var pX, pY: integer;
   293 var pX, pY: LongInt;
   294     Result: boolean;
   294     Result: boolean;
   295 begin
   295 begin
   296 Result:= false;
   296 Result:= false;
   297 AltGear^:= Gear^;
   297 AltGear^:= Gear^;
   298 
   298 
   386    end
   386    end
   387 until (pX = hwRound(Gear^.X)) and (pY = hwRound(Gear^.Y)) and ((Gear^.State and gstFalling) = 0);
   387 until (pX = hwRound(Gear^.X)) and (pY = hwRound(Gear^.Y)) and ((Gear^.State and gstFalling) = 0);
   388 HHJump(AltGear, jmpHJump, GoInfo)
   388 HHJump(AltGear, jmpHJump, GoInfo)
   389 end;
   389 end;
   390 
   390 
   391 function AIrndSign(num: integer): integer;
   391 function AIrndSign(num: LongInt): LongInt;
   392 begin
   392 begin
   393 if random(2) = 0 then AIrndSign:=   num
   393 if random(2) = 0 then AIrndSign:=   num
   394                  else AIrndSign:= - num
   394                  else AIrndSign:= - num
   395 end;  
   395 end;  
   396 
   396