author | unc0rr |
Fri, 15 Sep 2006 13:47:01 +0000 | |
changeset 146 | 458f4f58c1b6 |
parent 143 | 3dacbd83209b |
child 183 | 57c2ef19f719 |
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 |
|
136 | 38 |
function TestBazooka(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
39 |
function TestGrenade(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
|
40 |
function TestShotgun(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
|
41 |
function TestDesertEagle(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
|
42 |
function TestBaseballBat(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
|
43 |
function TestFirePunch(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
|
4 | 44 |
|
136 | 45 |
type TAmmoTestProc = function (Me: PGear; Targ: TPoint; Level: Integer; 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, |
143 | 59 |
{amFirePunch} TestFirePunch, |
60 |
{amBaseballBat} TestBaseballBat |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
61 |
); |
4 | 62 |
|
143 | 63 |
const BadTurn = Low(integer); |
64 |
||
4 | 65 |
implementation |
75 | 66 |
uses uMisc, uAIMisc, uLand; |
4 | 67 |
|
64 | 68 |
function Metric(x1, y1, x2, y2: integer): integer; |
4 | 69 |
begin |
64 | 70 |
Result:= abs(x1 - x2) + abs(y1 - y2) |
4 | 71 |
end; |
72 |
||
136 | 73 |
function TestBazooka(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
107 | 74 |
var Vx, Vy, r: Double; |
75 |
rTime: Double; |
|
71 | 76 |
Score, EX, EY: integer; |
4 | 77 |
|
64 | 78 |
function CheckTrace: integer; |
107 | 79 |
var x, y, dX, dY: Double; |
4 | 80 |
t: integer; |
81 |
begin |
|
53 | 82 |
x:= Me.X; |
83 |
y:= Me.Y; |
|
4 | 84 |
dX:= Vx; |
85 |
dY:= -Vy; |
|
64 | 86 |
t:= trunc(rTime); |
4 | 87 |
repeat |
88 |
x:= x + dX; |
|
89 |
y:= y + dY; |
|
90 |
dX:= dX + cWindSpeed; |
|
91 |
dY:= dY + cGravity; |
|
92 |
dec(t) |
|
64 | 93 |
until TestColl(round(x), round(y), 5) or (t <= 0); |
71 | 94 |
EX:= round(x); |
95 |
EY:= round(y); |
|
70 | 96 |
Result:= RateExplosion(Me, round(x), round(y), 101); |
97 |
if Result = 0 then Result:= - Metric(Targ.X, Targ.Y, round(x), round(y)) div 64 |
|
4 | 98 |
end; |
99 |
||
100 |
begin |
|
101 |
Time:= 0; |
|
82 | 102 |
rTime:= 50; |
71 | 103 |
ExplR:= 0; |
70 | 104 |
Result:= BadTurn; |
4 | 105 |
repeat |
136 | 106 |
rTime:= rTime + 150 + Level * 25 + random * 250; |
4 | 107 |
Vx:= - cWindSpeed * rTime / 2 + (Targ.X - Me.X) / rTime; |
108 |
Vy:= cGravity * rTime / 2 - (Targ.Y - Me.Y) / rTime; |
|
109 |
r:= sqr(Vx) + sqr(Vy); |
|
64 | 110 |
if r <= 1 then |
4 | 111 |
begin |
64 | 112 |
Score:= CheckTrace; |
113 |
if Result <= Score then |
|
114 |
begin |
|
115 |
r:= sqrt(r); |
|
141 | 116 |
Angle:= DxDy2AttackAngle(Vx, Vy) + rndSign(random((Level - 1) * 8)); |
136 | 117 |
Power:= round(r * cMaxPower) - random((Level - 1) * 15 + 1); |
74 | 118 |
ExplR:= 100; |
71 | 119 |
ExplX:= EX; |
120 |
ExplY:= EY; |
|
143 | 121 |
Result:= Score |
64 | 122 |
end; |
4 | 123 |
end |
82 | 124 |
until (rTime >= 4500) |
39 | 125 |
end; |
4 | 126 |
|
136 | 127 |
function TestGrenade(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
70 | 128 |
const tDelta = 24; |
107 | 129 |
var Vx, Vy, r: Double; |
71 | 130 |
Score, EX, EY: integer; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
131 |
TestTime: Longword; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
132 |
|
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
133 |
function CheckTrace: integer; |
107 | 134 |
var x, y, dY: Double; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
135 |
t: integer; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
136 |
begin |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
137 |
x:= Me.X; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
138 |
y:= Me.Y; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
139 |
dY:= -Vy; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
140 |
t:= TestTime; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
141 |
repeat |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
142 |
x:= x + Vx; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
143 |
y:= y + dY; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
144 |
dY:= dY + cGravity; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
145 |
dec(t) |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
146 |
until TestColl(round(x), round(y), 5) or (t = 0); |
71 | 147 |
EX:= round(x); |
148 |
EY:= round(y); |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
149 |
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
|
150 |
else Result:= Low(integer) |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
151 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
152 |
|
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
153 |
begin |
70 | 154 |
Result:= BadTurn; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
155 |
TestTime:= 0; |
71 | 156 |
ExplR:= 0; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
157 |
repeat |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
158 |
inc(TestTime, 1000); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
159 |
Vx:= (Targ.X - Me.X) / (TestTime + tDelta); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
160 |
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
|
161 |
r:= sqr(Vx) + sqr(Vy); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
162 |
if r <= 1 then |
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 |
Score:= CheckTrace; |
70 | 165 |
if Result < Score then |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
166 |
begin |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
167 |
r:= sqrt(r); |
136 | 168 |
Angle:= DxDy2AttackAngle(Vx, Vy) + rndSign(random(Level)); |
169 |
Power:= round(r * cMaxPower) + rndSign(random(Level) * 12); |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
170 |
Time:= TestTime; |
74 | 171 |
ExplR:= 100; |
71 | 172 |
ExplX:= EX; |
173 |
ExplY:= EY; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
174 |
Result:= Score |
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 |
end |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
177 |
until (TestTime = 5000) |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
178 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
179 |
|
136 | 180 |
function TestShotgun(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
107 | 181 |
var Vx, Vy, x, y: Double; |
136 | 182 |
begin |
79 | 183 |
ExplR:= 0; |
70 | 184 |
if Metric(round(Me.X), round(Me.Y), Targ.X, Targ.Y) < 80 then |
185 |
begin |
|
186 |
Result:= BadTurn; |
|
187 |
exit |
|
188 |
end; |
|
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:= DxDy2AttackAngle(Vx, -Vy); |
|
196 |
repeat |
|
197 |
x:= x + vX; |
|
198 |
y:= y + vY; |
|
199 |
if TestColl(round(x), round(y), 2) then |
|
200 |
begin |
|
143 | 201 |
Result:= RateShove(Me, round(x), round(y), 25, 25) * 2; |
202 |
if Result = 0 then Result:= - Metric(Targ.X, Targ.Y, round(x), round(y)) div 64 |
|
203 |
else dec(Result, Level * 4000); |
|
70 | 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:= BadTurn |
|
208 |
end; |
|
209 |
||
136 | 210 |
function TestDesertEagle(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
107 | 211 |
var Vx, Vy, x, y, t: Double; |
75 | 212 |
d: Longword; |
213 |
begin |
|
79 | 214 |
ExplR:= 0; |
75 | 215 |
if abs(Me.X - Targ.X) + abs(Me.Y - Targ.Y) < 80 then |
216 |
begin |
|
217 |
Result:= BadTurn; |
|
218 |
exit |
|
219 |
end; |
|
220 |
Time:= 0; |
|
221 |
Power:= 1; |
|
222 |
t:= sqrt(sqr(Targ.X - Me.X) + sqr(Targ.Y - Me.Y)) * 2; |
|
223 |
Vx:= (Targ.X - Me.X) / t; |
|
224 |
Vy:= (Targ.Y - Me.Y) / t; |
|
225 |
x:= Me.X; |
|
226 |
y:= Me.Y; |
|
227 |
Angle:= DxDy2AttackAngle(Vx, -Vy); |
|
228 |
d:= 0; |
|
229 |
repeat |
|
230 |
x:= x + vX; |
|
231 |
y:= y + vY; |
|
232 |
if ((round(x) and $FFFFF800) = 0)and((round(y) and $FFFFFC00) = 0) |
|
233 |
and (Land[round(y), round(x)] <> 0) then inc(d); |
|
234 |
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); |
|
235 |
if abs(Targ.X - x) + abs(Targ.Y - y) < 2 then Result:= max(0, (4 - d div 50) * 7 * 1024) |
|
236 |
else Result:= Low(integer) |
|
237 |
end; |
|
238 |
||
136 | 239 |
function TestBaseballBat(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
79 | 240 |
begin |
241 |
ExplR:= 0; |
|
143 | 242 |
if (Level > 2) and (abs(Me.X - Targ.X) + abs(Me.Y - Targ.Y) >= 25) then |
79 | 243 |
begin |
244 |
Result:= BadTurn; |
|
245 |
exit |
|
246 |
end; |
|
247 |
Time:= 0; |
|
248 |
Power:= 1; |
|
108 | 249 |
Angle:= DxDy2AttackAngle(hwSign(Targ.X - Me.X), 1); |
143 | 250 |
Result:= RateShove(Me, round(Me.X) + 10 * hwSign(Targ.X - Me.X), round(Me.Y), 15, 30); |
251 |
if Result <= 0 then Result:= BadTurn |
|
79 | 252 |
end; |
253 |
||
136 | 254 |
function TestFirePunch(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
82 | 255 |
var i: integer; |
256 |
begin |
|
257 |
ExplR:= 0; |
|
258 |
if (abs(Me.X - Targ.X) > 25) or (abs(Me.Y - 50 - Targ.Y) > 50) then |
|
259 |
begin |
|
260 |
Result:= BadTurn; |
|
261 |
exit |
|
262 |
end; |
|
263 |
Time:= 0; |
|
264 |
Power:= 1; |
|
108 | 265 |
Angle:= DxDy2AttackAngle(hwSign(Targ.X - Me.X), 1); |
82 | 266 |
Result:= 0; |
267 |
for i:= 0 to 4 do |
|
143 | 268 |
Result:= Result + RateShove(Me, round(Me.X) + 10 * hwSign(Targ.X - Me.X), round(Me.Y) - 20 * i - 5, 10, 30); |
269 |
if Result <= 0 then Result:= BadTurn |
|
82 | 270 |
end; |
271 |
||
4 | 272 |
end. |