hedgewars/uAIAmmoTests.pas
changeset 71 5f56c6979496
parent 70 82d93eeecebe
child 74 42257fee61ae
equal deleted inserted replaced
70:82d93eeecebe 71:5f56c6979496
       
     1 (*
       
     2  * Hedgewars, a worms-like game
       
     3  * Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * Distributed under the terms of the BSD-modified licence:
       
     6  *
       
     7  * Permission is hereby granted, free of charge, to any person obtaining a copy
       
     8  * of this software and associated documentation files (the "Software"), to deal
       
     9  * with the Software without restriction, including without limitation the
       
    10  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
       
    11  * sell copies of the Software, and to permit persons to whom the Software is
       
    12  * furnished to do so, subject to the following conditions:
       
    13  *
       
    14  * 1. Redistributions of source code must retain the above copyright notice,
       
    15  *    this list of conditions and the following disclaimer.
       
    16  * 2. Redistributions in binary form must reproduce the above copyright notice,
       
    17  *    this list of conditions and the following disclaimer in the documentation
       
    18  *    and/or other materials provided with the distribution.
       
    19  * 3. The name of the author may not be used to endorse or promote products
       
    20  *    derived from this software without specific prior written permission.
       
    21  *
       
    22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
       
    23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
       
    24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
       
    25  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
       
    28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
       
    29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
       
    30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
       
    31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    32  *)
       
    33 
     1 unit uAIAmmoTests;
    34 unit uAIAmmoTests;
     2 interface
    35 interface
     3 uses SDLh, uGears, uConsts;
    36 uses SDLh, uGears, uConsts;
     4 
    37 
     5 function TestBazooka(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
    38 function TestBazooka(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
     6 function TestGrenade(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
    39 function TestGrenade(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
     7 function TestShotgun(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
    40 function TestShotgun(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
     8 
    41 
     9 type TAmmoTestProc = function (Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
    42 type TAmmoTestProc = function (Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
    10 const AmmoTests: array[TAmmoType] of TAmmoTestProc =
    43 const AmmoTests: array[TAmmoType] of TAmmoTestProc =
    11                  (
    44                  (
    12 {amGrenade}       TestGrenade,
    45 {amGrenade}       TestGrenade,
    13 {amBazooka}       TestBazooka,
    46 {amBazooka}       TestBazooka,
    14 {amUFO}           nil,
    47 {amUFO}           nil,
    28 function Metric(x1, y1, x2, y2: integer): integer;
    61 function Metric(x1, y1, x2, y2: integer): integer;
    29 begin
    62 begin
    30 Result:= abs(x1 - x2) + abs(y1 - y2)
    63 Result:= abs(x1 - x2) + abs(y1 - y2)
    31 end;
    64 end;
    32 
    65 
    33 function TestBazooka(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
    66 function TestBazooka(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
    34 var Vx, Vy, r: real;
    67 var Vx, Vy, r: real;
    35     rTime: real;
    68     rTime: real;
    36     Score: integer;
    69     Score, EX, EY: integer;
    37 
    70 
    38     function CheckTrace: integer;
    71     function CheckTrace: integer;
    39     var x, y, dX, dY: real;
    72     var x, y, dX, dY: real;
    40         t: integer;
    73         t: integer;
    41     begin
    74     begin
    49       y:= y + dY;
    82       y:= y + dY;
    50       dX:= dX + cWindSpeed;
    83       dX:= dX + cWindSpeed;
    51       dY:= dY + cGravity;
    84       dY:= dY + cGravity;
    52       dec(t)
    85       dec(t)
    53     until TestColl(round(x), round(y), 5) or (t <= 0);
    86     until TestColl(round(x), round(y), 5) or (t <= 0);
       
    87     EX:= round(x);
       
    88     EY:= round(y);
    54     Result:= RateExplosion(Me, round(x), round(y), 101);
    89     Result:= RateExplosion(Me, round(x), round(y), 101);
    55     if Result = 0 then Result:= - Metric(Targ.X, Targ.Y, round(x), round(y)) div 64
    90     if Result = 0 then Result:= - Metric(Targ.X, Targ.Y, round(x), round(y)) div 64
    56     end;
    91     end;
    57 
    92 
    58 begin
    93 begin
    59 Time:= 0;
    94 Time:= 0;
    60 rTime:= 10;
    95 rTime:= 10;
       
    96 ExplR:= 0;
    61 Result:= BadTurn;
    97 Result:= BadTurn;
    62 repeat
    98 repeat
    63   rTime:= rTime + 100 + random*250;
    99   rTime:= rTime + 100 + random*250;
    64   Vx:= - cWindSpeed * rTime / 2 + (Targ.X - Me.X) / rTime;
   100   Vx:= - cWindSpeed * rTime / 2 + (Targ.X - Me.X) / rTime;
    65   Vy:= cGravity * rTime / 2 - (Targ.Y - Me.Y) / rTime;
   101   Vy:= cGravity * rTime / 2 - (Targ.Y - Me.Y) / rTime;
    70      if Result <= Score then
   106      if Result <= Score then
    71         begin
   107         begin
    72         r:= sqrt(r);
   108         r:= sqrt(r);
    73         Angle:= DxDy2AttackAngle(Vx, Vy);
   109         Angle:= DxDy2AttackAngle(Vx, Vy);
    74         Power:= round(r * cMaxPower);
   110         Power:= round(r * cMaxPower);
       
   111         ExplR:= 50;
       
   112         ExplX:= EX;
       
   113         ExplY:= EY;
    75         Result:= Score
   114         Result:= Score
    76         end;
   115         end;
    77      end
   116      end
    78 until (rTime >= 5000)
   117 until (rTime >= 5000)
    79 end;
   118 end;
    80 
   119 
    81 function TestGrenade(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
   120 function TestGrenade(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
    82 const tDelta = 24;
   121 const tDelta = 24;
    83 var Vx, Vy, r: real;
   122 var Vx, Vy, r: real;
    84     Score: integer;
   123     Score, EX, EY: integer;
    85     TestTime: Longword;
   124     TestTime: Longword;
    86 
   125 
    87     function CheckTrace: integer;
   126     function CheckTrace: integer;
    88     var x, y, dY: real;
   127     var x, y, dY: real;
    89         t: integer;
   128         t: integer;
    96       x:= x + Vx;
   135       x:= x + Vx;
    97       y:= y + dY;
   136       y:= y + dY;
    98       dY:= dY + cGravity;
   137       dY:= dY + cGravity;
    99       dec(t)
   138       dec(t)
   100     until TestColl(round(x), round(y), 5) or (t = 0);
   139     until TestColl(round(x), round(y), 5) or (t = 0);
       
   140     EX:= round(x);
       
   141     EY:= round(y);
   101     if t < 50 then Result:= RateExplosion(Me, round(x), round(y), 101)
   142     if t < 50 then Result:= RateExplosion(Me, round(x), round(y), 101)
   102               else Result:= Low(integer)
   143               else Result:= Low(integer)
   103     end;
   144     end;
   104 
   145 
   105 begin
   146 begin
   106 Result:= BadTurn;
   147 Result:= BadTurn;
   107 TestTime:= 0;
   148 TestTime:= 0;
       
   149 ExplR:= 0;
   108 repeat
   150 repeat
   109   inc(TestTime, 1000);
   151   inc(TestTime, 1000);
   110   Vx:= (Targ.X - Me.X) / (TestTime + tDelta);
   152   Vx:= (Targ.X - Me.X) / (TestTime + tDelta);
   111   Vy:= cGravity*((TestTime + tDelta) div 2) - (Targ.Y - Me.Y) / (TestTime + tDelta);
   153   Vy:= cGravity*((TestTime + tDelta) div 2) - (Targ.Y - Me.Y) / (TestTime + tDelta);
   112   r:= sqr(Vx) + sqr(Vy);
   154   r:= sqr(Vx) + sqr(Vy);
   117         begin
   159         begin
   118         r:= sqrt(r);
   160         r:= sqrt(r);
   119         Angle:= DxDy2AttackAngle(Vx, Vy);
   161         Angle:= DxDy2AttackAngle(Vx, Vy);
   120         Power:= round(r * cMaxPower);
   162         Power:= round(r * cMaxPower);
   121         Time:= TestTime;
   163         Time:= TestTime;
       
   164         ExplR:= 50;
       
   165         ExplX:= EX;
       
   166         ExplY:= EY;
   122         Result:= Score
   167         Result:= Score
   123         end;
   168         end;
   124      end
   169      end
   125 until (TestTime = 5000)
   170 until (TestTime = 5000)
   126 end;
   171 end;
   127 
   172 
   128 function TestShotgun(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer): integer;
   173 function TestShotgun(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer;
   129 var Vx, Vy, x, y: real;
   174 var Vx, Vy, x, y: real;
   130 begin
   175 begin
   131 if Metric(round(Me.X), round(Me.Y), Targ.X, Targ.Y) < 80 then
   176 if Metric(round(Me.X), round(Me.Y), Targ.X, Targ.Y) < 80 then
   132    begin
   177    begin
   133    Result:= BadTurn;
   178    Result:= BadTurn;
   134    exit
   179    exit
   135    end;
   180    end;
   136 Time:= 0;
   181 Time:= 0;
   137 Power:= 1;
   182 Power:= 1;
       
   183 ExplR:= 0;
   138 Vx:= (Targ.X - Me.X)/1024;
   184 Vx:= (Targ.X - Me.X)/1024;
   139 Vy:= (Targ.Y - Me.Y)/1024;
   185 Vy:= (Targ.Y - Me.Y)/1024;
   140 x:= Me.X;
   186 x:= Me.X;
   141 y:= Me.Y;
   187 y:= Me.Y;
   142 Angle:= DxDy2AttackAngle(Vx, -Vy);
   188 Angle:= DxDy2AttackAngle(Vx, -Vy);