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