author | unc0rr |
Sun, 03 Sep 2006 13:54:45 +0000 | |
changeset 133 | c79cb5e5b24a |
parent 108 | 08f1fe6f21f8 |
child 136 | 89970b70b076 |
permissions | -rw-r--r-- |
71 | 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 |
||
4 | 34 |
unit uAIAmmoTests; |
35 |
interface |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
36 |
uses SDLh, uGears, uConsts; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
37 |
|
71 | 38 |
function TestBazooka(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
39 |
function TestGrenade(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
|
40 |
function TestShotgun(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
|
75 | 41 |
function TestDesertEagle(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
79 | 42 |
function TestBaseballBat(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
82 | 43 |
function TestFirePunch(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
4 | 44 |
|
71 | 45 |
type TAmmoTestProc = function (Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
46 |
const AmmoTests: array[TAmmoType] of TAmmoTestProc = |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
47 |
( |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
48 |
{amGrenade} TestGrenade, |
78 | 49 |
{amClusterBomb} nil, |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
50 |
{amBazooka} TestBazooka, |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
51 |
{amUFO} nil, |
70 | 52 |
{amShotgun} TestShotgun, |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
53 |
{amPickHammer} nil, |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
54 |
{amSkip} nil, |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
55 |
{amRope} nil, |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
56 |
{amMine} nil, |
75 | 57 |
{amDEagle} TestDesertEagle, |
79 | 58 |
{amDynamite} nil, |
82 | 59 |
{amBaseballBat} TestBaseballBat, |
60 |
{amFirePunch} TestFirePunch |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
61 |
); |
4 | 62 |
|
63 |
implementation |
|
75 | 64 |
uses uMisc, uAIMisc, uLand; |
70 | 65 |
const BadTurn = Low(integer); |
4 | 66 |
|
64 | 67 |
function Metric(x1, y1, x2, y2: integer): integer; |
4 | 68 |
begin |
64 | 69 |
Result:= abs(x1 - x2) + abs(y1 - y2) |
4 | 70 |
end; |
71 |
||
71 | 72 |
function TestBazooka(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
107 | 73 |
var Vx, Vy, r: Double; |
74 |
rTime: Double; |
|
71 | 75 |
Score, EX, EY: integer; |
4 | 76 |
|
64 | 77 |
function CheckTrace: integer; |
107 | 78 |
var x, y, dX, dY: Double; |
4 | 79 |
t: integer; |
80 |
begin |
|
53 | 81 |
x:= Me.X; |
82 |
y:= Me.Y; |
|
4 | 83 |
dX:= Vx; |
84 |
dY:= -Vy; |
|
64 | 85 |
t:= trunc(rTime); |
4 | 86 |
repeat |
87 |
x:= x + dX; |
|
88 |
y:= y + dY; |
|
89 |
dX:= dX + cWindSpeed; |
|
90 |
dY:= dY + cGravity; |
|
91 |
dec(t) |
|
64 | 92 |
until TestColl(round(x), round(y), 5) or (t <= 0); |
71 | 93 |
EX:= round(x); |
94 |
EY:= round(y); |
|
70 | 95 |
Result:= RateExplosion(Me, round(x), round(y), 101); |
96 |
if Result = 0 then Result:= - Metric(Targ.X, Targ.Y, round(x), round(y)) div 64 |
|
4 | 97 |
end; |
98 |
||
99 |
begin |
|
100 |
Time:= 0; |
|
82 | 101 |
rTime:= 50; |
71 | 102 |
ExplR:= 0; |
70 | 103 |
Result:= BadTurn; |
4 | 104 |
repeat |
82 | 105 |
rTime:= rTime + 150 + random*250; |
4 | 106 |
Vx:= - cWindSpeed * rTime / 2 + (Targ.X - Me.X) / rTime; |
107 |
Vy:= cGravity * rTime / 2 - (Targ.Y - Me.Y) / rTime; |
|
108 |
r:= sqr(Vx) + sqr(Vy); |
|
64 | 109 |
if r <= 1 then |
4 | 110 |
begin |
64 | 111 |
Score:= CheckTrace; |
112 |
if Result <= Score then |
|
113 |
begin |
|
114 |
r:= sqrt(r); |
|
115 |
Angle:= DxDy2AttackAngle(Vx, Vy); |
|
116 |
Power:= round(r * cMaxPower); |
|
74 | 117 |
ExplR:= 100; |
71 | 118 |
ExplX:= EX; |
119 |
ExplY:= EY; |
|
64 | 120 |
Result:= Score |
121 |
end; |
|
4 | 122 |
end |
82 | 123 |
until (rTime >= 4500) |
39 | 124 |
end; |
4 | 125 |
|
71 | 126 |
function TestGrenade(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
70 | 127 |
const tDelta = 24; |
107 | 128 |
var Vx, Vy, r: Double; |
71 | 129 |
Score, EX, EY: integer; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
130 |
TestTime: Longword; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
131 |
|
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
132 |
function CheckTrace: integer; |
107 | 133 |
var x, y, dY: Double; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
134 |
t: integer; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
135 |
begin |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
136 |
x:= Me.X; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
137 |
y:= Me.Y; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
138 |
dY:= -Vy; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
139 |
t:= TestTime; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
140 |
repeat |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
141 |
x:= x + Vx; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
142 |
y:= y + dY; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
143 |
dY:= dY + cGravity; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
144 |
dec(t) |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
145 |
until TestColl(round(x), round(y), 5) or (t = 0); |
71 | 146 |
EX:= round(x); |
147 |
EY:= round(y); |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
148 |
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
|
149 |
else Result:= Low(integer) |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
150 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
151 |
|
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
152 |
begin |
70 | 153 |
Result:= BadTurn; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
154 |
TestTime:= 0; |
71 | 155 |
ExplR:= 0; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
156 |
repeat |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
157 |
inc(TestTime, 1000); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
158 |
Vx:= (Targ.X - Me.X) / (TestTime + tDelta); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
159 |
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
|
160 |
r:= sqr(Vx) + sqr(Vy); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
161 |
if r <= 1 then |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
162 |
begin |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
163 |
Score:= CheckTrace; |
70 | 164 |
if Result < Score then |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
165 |
begin |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
166 |
r:= sqrt(r); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
167 |
Angle:= DxDy2AttackAngle(Vx, Vy); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
168 |
Power:= round(r * cMaxPower); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
169 |
Time:= TestTime; |
74 | 170 |
ExplR:= 100; |
71 | 171 |
ExplX:= EX; |
172 |
ExplY:= EY; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
173 |
Result:= Score |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
174 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
175 |
end |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
176 |
until (TestTime = 5000) |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
177 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
178 |
|
71 | 179 |
function TestShotgun(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
107 | 180 |
var Vx, Vy, x, y: Double; |
70 | 181 |
begin |
79 | 182 |
ExplR:= 0; |
70 | 183 |
if Metric(round(Me.X), round(Me.Y), Targ.X, Targ.Y) < 80 then |
184 |
begin |
|
185 |
Result:= BadTurn; |
|
186 |
exit |
|
187 |
end; |
|
188 |
Time:= 0; |
|
189 |
Power:= 1; |
|
190 |
Vx:= (Targ.X - Me.X)/1024; |
|
191 |
Vy:= (Targ.Y - Me.Y)/1024; |
|
192 |
x:= Me.X; |
|
193 |
y:= Me.Y; |
|
194 |
Angle:= DxDy2AttackAngle(Vx, -Vy); |
|
195 |
repeat |
|
196 |
x:= x + vX; |
|
197 |
y:= y + vY; |
|
198 |
if TestColl(round(x), round(y), 2) then |
|
199 |
begin |
|
80 | 200 |
Result:= RateShove(Me, round(x), round(y), 25, 25); |
70 | 201 |
if Result = 0 then Result:= - Metric(Targ.X, Targ.Y, round(x), round(y)) div 64; |
202 |
exit |
|
203 |
end |
|
204 |
until (abs(Targ.X - x) + abs(Targ.Y - y) < 4) or (x < 0) or (y < 0) or (x > 2048) or (y > 1024); |
|
205 |
Result:= BadTurn |
|
206 |
end; |
|
207 |
||
75 | 208 |
function TestDesertEagle(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
107 | 209 |
var Vx, Vy, x, y, t: Double; |
75 | 210 |
d: Longword; |
211 |
begin |
|
79 | 212 |
ExplR:= 0; |
75 | 213 |
if abs(Me.X - Targ.X) + abs(Me.Y - Targ.Y) < 80 then |
214 |
begin |
|
215 |
Result:= BadTurn; |
|
216 |
exit |
|
217 |
end; |
|
218 |
Time:= 0; |
|
219 |
Power:= 1; |
|
220 |
t:= sqrt(sqr(Targ.X - Me.X) + sqr(Targ.Y - Me.Y)) * 2; |
|
221 |
Vx:= (Targ.X - Me.X) / t; |
|
222 |
Vy:= (Targ.Y - Me.Y) / t; |
|
223 |
x:= Me.X; |
|
224 |
y:= Me.Y; |
|
225 |
Angle:= DxDy2AttackAngle(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) or (d > 200); |
|
233 |
if abs(Targ.X - x) + abs(Targ.Y - y) < 2 then Result:= max(0, (4 - d div 50) * 7 * 1024) |
|
234 |
else Result:= Low(integer) |
|
235 |
end; |
|
236 |
||
79 | 237 |
function TestBaseballBat(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
238 |
begin |
|
239 |
ExplR:= 0; |
|
240 |
if abs(Me.X - Targ.X) + abs(Me.Y - Targ.Y) >= 25 then |
|
241 |
begin |
|
242 |
Result:= BadTurn; |
|
243 |
exit |
|
244 |
end; |
|
245 |
Time:= 0; |
|
246 |
Power:= 1; |
|
108 | 247 |
Angle:= DxDy2AttackAngle(hwSign(Targ.X - Me.X), 1); |
248 |
Result:= RateShove(Me, round(Me.X) + 10 * hwSign(Targ.X - Me.X), round(Me.Y), 15, 30) |
|
79 | 249 |
end; |
250 |
||
82 | 251 |
function TestFirePunch(Me: PGear; Targ: TPoint; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
252 |
var i: integer; |
|
253 |
begin |
|
254 |
ExplR:= 0; |
|
255 |
if (abs(Me.X - Targ.X) > 25) or (abs(Me.Y - 50 - Targ.Y) > 50) then |
|
256 |
begin |
|
257 |
Result:= BadTurn; |
|
258 |
exit |
|
259 |
end; |
|
260 |
Time:= 0; |
|
261 |
Power:= 1; |
|
108 | 262 |
Angle:= DxDy2AttackAngle(hwSign(Targ.X - Me.X), 1); |
82 | 263 |
Result:= 0; |
264 |
for i:= 0 to 4 do |
|
108 | 265 |
Result:= Result + RateShove(Me, round(Me.X) + 10 * hwSign(Targ.X - Me.X), round(Me.Y) - 20 * i - 5, 10, 30) |
82 | 266 |
end; |
267 |
||
4 | 268 |
end. |