hedgewars/uAIAmmoTests.pas
author unc0rr
Fri, 23 Jun 2006 20:02:41 +0000
changeset 70 82d93eeecebe
parent 66 9643d75baf1e
child 71 5f56c6979496
permissions -rw-r--r--
- Many AI improvements - New 'spray objects' on generated land - Many small fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     1
unit uAIAmmoTests;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     2
interface
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
     3
uses SDLh, uGears, uConsts;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
     4
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
     5
function TestBazooka(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
     6
function TestGrenade(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
     7
function TestShotgun(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     8
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
     9
type TAmmoTestProc = function (Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    10
const AmmoTests: array[TAmmoType] of TAmmoTestProc =
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    11
                 (
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    12
{amGrenade}       TestGrenade,
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    13
{amBazooka}       TestBazooka,
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    14
{amUFO}           nil,
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    15
{amShotgun}       TestShotgun,
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    16
{amPickHammer}    nil,
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    17
{amSkip}          nil,
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    18
{amRope}          nil,
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    19
{amMine}          nil,
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    20
{amDEagle}        nil,
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    21
{amDynamite}      nil
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    22
                  );
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    23
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    24
implementation
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    25
uses uMisc, uAIMisc;
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    26
const BadTurn = Low(integer);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    27
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    28
function Metric(x1, y1, x2, y2: integer): integer;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    29
begin
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    30
Result:= abs(x1 - x2) + abs(y1 - y2)
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    31
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    32
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    33
function TestBazooka(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    34
var Vx, Vy, r: real;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    35
    rTime: real;
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    36
    Score: integer;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    37
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    38
    function CheckTrace: integer;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    39
    var x, y, dX, dY: real;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    40
        t: integer;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    41
    begin
53
0e27949850e3 - Fixed bubble theme object
unc0rr
parents: 51
diff changeset
    42
    x:= Me.X;
0e27949850e3 - Fixed bubble theme object
unc0rr
parents: 51
diff changeset
    43
    y:= Me.Y;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    44
    dX:= Vx;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    45
    dY:= -Vy;
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    46
    t:= trunc(rTime);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    47
    repeat
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    48
      x:= x + dX;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    49
      y:= y + dY;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    50
      dX:= dX + cWindSpeed;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    51
      dY:= dY + cGravity;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    52
      dec(t)
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    53
    until TestColl(round(x), round(y), 5) or (t <= 0);
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    54
    Result:= RateExplosion(Me, round(x), round(y), 101);
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    55
    if Result = 0 then Result:= - Metric(Targ.X, Targ.Y, round(x), round(y)) div 64
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    56
    end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    57
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    58
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    59
Time:= 0;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    60
rTime:= 10;
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    61
Result:= BadTurn;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    62
repeat
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    63
  rTime:= rTime + 100 + random*250;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    64
  Vx:= - cWindSpeed * rTime / 2 + (Targ.X - Me.X) / rTime;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    65
  Vy:= cGravity * rTime / 2 - (Targ.Y - Me.Y) / rTime;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    66
  r:= sqr(Vx) + sqr(Vy);
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    67
  if r <= 1 then
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    68
     begin
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    69
     Score:= CheckTrace;
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    70
     if Result <= Score then
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    71
        begin
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    72
        r:= sqrt(r);
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    73
        Angle:= DxDy2AttackAngle(Vx, Vy);
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    74
        Power:= round(r * cMaxPower);
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    75
        Result:= Score
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    76
        end;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    77
     end
64
9df467527ae5 - Start AI rewrite
unc0rr
parents: 53
diff changeset
    78
until (rTime >= 5000)
39
b78e7185ed13 - Increased FPS
unc0rr
parents: 37
diff changeset
    79
end;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    80
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    81
function TestGrenade(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
    82
const tDelta = 24;
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    83
var Vx, Vy, r: real;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    84
    Score: integer;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    85
    TestTime: Longword;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    86
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    87
    function CheckTrace: integer;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    88
    var x, y, dY: real;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    89
        t: integer;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    90
    begin
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    91
    x:= Me.X;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    92
    y:= Me.Y;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    93
    dY:= -Vy;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    94
    t:= TestTime;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    95
    repeat
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    96
      x:= x + Vx;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    97
      y:= y + dY;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    98
      dY:= dY + cGravity;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
    99
      dec(t)
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   100
    until TestColl(round(x), round(y), 5) or (t = 0);
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   101
    if t < 50 then Result:= RateExplosion(Me, round(x), round(y), 101)
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   102
              else Result:= Low(integer)
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   103
    end;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   104
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   105
begin
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   106
Result:= BadTurn;
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   107
TestTime:= 0;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   108
repeat
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   109
  inc(TestTime, 1000);
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   110
  Vx:= (Targ.X - Me.X) / (TestTime + tDelta);
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   111
  Vy:= cGravity*((TestTime + tDelta) div 2) - (Targ.Y - Me.Y) / (TestTime + tDelta);
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   112
  r:= sqr(Vx) + sqr(Vy);
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   113
  if r <= 1 then
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   114
     begin
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   115
     Score:= CheckTrace;
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   116
     if Result < Score then
66
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   117
        begin
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   118
        r:= sqrt(r);
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   119
        Angle:= DxDy2AttackAngle(Vx, Vy);
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   120
        Power:= round(r * cMaxPower);
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   121
        Time:= TestTime;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   122
        Result:= Score
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   123
        end;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   124
     end
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   125
until (TestTime = 5000)
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   126
end;
9643d75baf1e Many AI improvements, bots do think in separate thread
unc0rr
parents: 64
diff changeset
   127
70
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   128
function TestShotgun(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   129
var Vx, Vy, x, y: real;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   130
begin
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   131
if Metric(round(Me.X), round(Me.Y), Targ.X, Targ.Y) < 80 then
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   132
   begin
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   133
   Result:= BadTurn;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   134
   exit
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   135
   end;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   136
Time:= 0;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   137
Power:= 1;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   138
Vx:= (Targ.X - Me.X)/1024;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   139
Vy:= (Targ.Y - Me.Y)/1024;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   140
x:= Me.X;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   141
y:= Me.Y;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   142
Angle:= DxDy2AttackAngle(Vx, -Vy);
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   143
repeat
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   144
  x:= x + vX;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   145
  y:= y + vY;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   146
  if TestColl(round(x), round(y), 2) then
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   147
     begin
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   148
     Result:= RateExplosion(Me, round(x), round(y), 25) * 2;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   149
     if Result = 0 then Result:= - Metric(Targ.X, Targ.Y, round(x), round(y)) div 64;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   150
     exit
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   151
     end
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   152
until (abs(Targ.X - x) + abs(Targ.Y - y) < 4) or (x < 0) or (y < 0) or (x > 2048) or (y > 1024);
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   153
Result:= BadTurn
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   154
end;
82d93eeecebe - Many AI improvements
unc0rr
parents: 66
diff changeset
   155
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   156
end.