4
|
1 |
(*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2005 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 |
|
|
34 |
unit uAIAmmoTests;
|
|
35 |
interface
|
|
36 |
uses uConsts, SDLh;
|
|
37 |
{$INCLUDE options.inc}
|
|
38 |
const ctfNotFull = $00000001;
|
|
39 |
ctfBreach = $00000002;
|
|
40 |
|
|
41 |
function TestGrenade(Me, Targ: TPoint; Flags: Longword; out Time: Longword; out Angle, Power: integer): boolean;
|
|
42 |
function TestBazooka(Me, Targ: TPoint; Flags: Longword; out Time: Longword; out Angle, Power: integer): boolean;
|
|
43 |
function TestShotgun(Me, Targ: TPoint; Flags: Longword; out Time: Longword; out Angle, Power: integer): boolean;
|
|
44 |
|
|
45 |
type TAmmoTestProc = function (Me, Targ: TPoint; Flags: Longword; out Time: Longword; out Angle, Power: integer): boolean;
|
|
46 |
const AmmoTests: array[TAmmoType] of
|
|
47 |
record
|
|
48 |
Test: TAmmoTestProc;
|
|
49 |
Flags: Longword;
|
|
50 |
end = (
|
|
51 |
( Test: TestGrenade;
|
|
52 |
Flags: ctfNotFull;
|
|
53 |
),
|
|
54 |
( Test: TestBazooka;
|
|
55 |
Flags: ctfNotFull or ctfBreach;
|
|
56 |
),
|
|
57 |
( Test: nil;
|
|
58 |
Flags: 0;
|
|
59 |
),
|
|
60 |
( Test: TestShotgun;
|
|
61 |
Flags: ctfBreach;
|
|
62 |
),
|
|
63 |
( Test: nil;
|
|
64 |
Flags: 0;
|
|
65 |
),
|
|
66 |
( Test: nil;
|
|
67 |
Flags: 0;
|
|
68 |
),
|
|
69 |
( Test: nil;
|
|
70 |
Flags: 0;
|
10
|
71 |
),
|
|
72 |
( Test: nil;
|
|
73 |
Flags: 0;
|
37
|
74 |
),
|
|
75 |
( Test: nil;
|
|
76 |
Flags: 0;
|
4
|
77 |
)
|
|
78 |
);
|
|
79 |
|
|
80 |
implementation
|
|
81 |
uses uMisc, uAIMisc;
|
|
82 |
|
|
83 |
function TestGrenade(Me, Targ: TPoint; Flags: Longword; out Time: Longword; out Angle, Power: integer): boolean;
|
|
84 |
var Vx, Vy, r: real;
|
|
85 |
flHasTrace: boolean;
|
|
86 |
|
|
87 |
function CheckTrace: boolean;
|
|
88 |
var x, y, dY: real;
|
|
89 |
t: integer;
|
|
90 |
begin
|
|
91 |
x:= Me.X;
|
|
92 |
y:= Me.Y;
|
|
93 |
dY:= -Vy;
|
|
94 |
Result:= false;
|
|
95 |
if (Flags and ctfNotFull) = 0 then t:= Time
|
|
96 |
else t:= Time - 100;
|
|
97 |
repeat
|
|
98 |
x:= x + Vx;
|
|
99 |
y:= y + dY;
|
|
100 |
dY:= dY + cGravity;
|
|
101 |
if TestColl(round(x), round(y), 5) then exit;
|
|
102 |
dec(t);
|
|
103 |
until t <= 0;
|
|
104 |
Result:= true
|
|
105 |
end;
|
|
106 |
|
|
107 |
begin
|
|
108 |
Result:= false;
|
|
109 |
Time:= 0;
|
|
110 |
flHasTrace:= false;
|
|
111 |
repeat
|
|
112 |
inc(Time, 1000);
|
|
113 |
Vx:= (Targ.X - Me.X) / Time;
|
|
114 |
Vy:= cGravity*(Time div 2) - (Targ.Y - Me.Y) / Time;
|
|
115 |
r:= sqr(Vx) + sqr(Vy);
|
|
116 |
if r <= 1 then flHasTrace:= CheckTrace
|
|
117 |
else exit
|
|
118 |
until flHasTrace or (Time = 5000);
|
|
119 |
if not flHasTrace then exit;
|
|
120 |
r:= sqrt(r);
|
|
121 |
Angle:= DxDy2Angle(Vx, Vy);
|
|
122 |
Power:= round(r * cMaxPower);
|
|
123 |
Result:= true
|
|
124 |
end;
|
|
125 |
|
|
126 |
function TestBazooka(Me, Targ: TPoint; Flags: Longword; out Time: Longword; out Angle, Power: integer): boolean;
|
|
127 |
var Vx, Vy, r: real;
|
|
128 |
rTime: real;
|
|
129 |
flHasTrace: boolean;
|
|
130 |
|
|
131 |
function CheckTrace: boolean;
|
|
132 |
var x, y, dX, dY: real;
|
|
133 |
t: integer;
|
|
134 |
begin
|
|
135 |
x:= Me.X + Vx*20;
|
|
136 |
y:= Me.Y + Vy*20;
|
|
137 |
dX:= Vx;
|
|
138 |
dY:= -Vy;
|
|
139 |
Result:= false;
|
|
140 |
if (Flags and ctfNotFull) = 0 then t:= trunc(rTime)
|
|
141 |
else t:= trunc(rTime) - 100;
|
|
142 |
repeat
|
|
143 |
x:= x + dX;
|
|
144 |
y:= y + dY;
|
|
145 |
dX:= dX + cWindSpeed;
|
|
146 |
dY:= dY + cGravity;
|
|
147 |
if TestColl(round(x), round(y), 5) then
|
|
148 |
begin
|
|
149 |
if (Flags and ctfBreach) <> 0 then
|
|
150 |
Result:= NoMyHHNear(round(x), round(y), 110);
|
|
151 |
exit
|
|
152 |
end;
|
|
153 |
dec(t)
|
|
154 |
until t <= 0;
|
|
155 |
Result:= true
|
|
156 |
end;
|
|
157 |
|
|
158 |
begin
|
|
159 |
Time:= 0;
|
|
160 |
Result:= false;
|
|
161 |
rTime:= 10;
|
|
162 |
flHasTrace:= false;
|
|
163 |
repeat
|
|
164 |
rTime:= rTime + 100 + random*300;
|
|
165 |
Vx:= - cWindSpeed * rTime / 2 + (Targ.X - Me.X) / rTime;
|
|
166 |
Vy:= cGravity * rTime / 2 - (Targ.Y - Me.Y) / rTime;
|
|
167 |
r:= sqr(Vx) + sqr(Vy);
|
|
168 |
if r <= 1 then flHasTrace:= CheckTrace
|
|
169 |
until flHasTrace or (rTime >= 5000);
|
|
170 |
if not flHasTrace then exit;
|
|
171 |
r:= sqrt(r);
|
|
172 |
Angle:= DxDy2Angle(Vx, Vy);
|
|
173 |
Power:= round(r * cMaxPower);
|
|
174 |
Result:= true
|
|
175 |
end;
|
|
176 |
|
|
177 |
function TestShotgun(Me, Targ: TPoint; Flags: Longword; out Time: Longword; out Angle, Power: integer): boolean;
|
|
178 |
var Vx, Vy, x, y: real;
|
|
179 |
begin
|
|
180 |
Time:= 0;
|
|
181 |
Power:= 1;
|
|
182 |
Vx:= (Targ.X - Me.X)/1024;
|
|
183 |
Vy:= (Targ.Y - Me.Y)/1024;
|
|
184 |
x:= Me.X;
|
|
185 |
y:= Me.Y;
|
|
186 |
Angle:= DxDy2Angle(Vx, -Vy);
|
|
187 |
repeat
|
|
188 |
x:= x + vX;
|
|
189 |
y:= y + vY;
|
|
190 |
if TestColl(round(x), round(y), 2) then
|
|
191 |
begin
|
|
192 |
if (Flags and ctfBreach) <> 0 then
|
|
193 |
Result:= NoMyHHNear(round(x), round(y), 27)
|
|
194 |
else Result:= false;
|
|
195 |
exit
|
|
196 |
end
|
|
197 |
until (abs(Targ.X - x) + abs(Targ.Y - y) < 4) or (x < 0) or (y < 0) or (x > 2048) or (y > 1024);
|
|
198 |
Result:= true
|
|
199 |
end;
|
|
200 |
|
|
201 |
|
|
202 |
end.
|