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