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