author | unc0rr |
Thu, 12 Oct 2006 18:52:00 +0000 | |
changeset 196 | 993cf173218b |
parent 183 | 57c2ef19f719 |
child 198 | 6f00e0f4cb88 |
permissions | -rw-r--r-- |
71 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
183 | 5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
71 | 8 |
* |
183 | 9 |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
71 | 13 |
* |
183 | 14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
71 | 17 |
*) |
18 |
||
4 | 19 |
unit uAIAmmoTests; |
20 |
interface |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
21 |
uses SDLh, uGears, uConsts; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
22 |
|
136 | 23 |
function TestBazooka(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
24 |
function TestGrenade(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
|
25 |
function TestShotgun(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
|
26 |
function TestDesertEagle(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
|
27 |
function TestBaseballBat(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
|
28 |
function TestFirePunch(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
|
4 | 29 |
|
136 | 30 |
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
|
31 |
const AmmoTests: array[TAmmoType] of TAmmoTestProc = |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
32 |
( |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
33 |
{amGrenade} TestGrenade, |
78 | 34 |
{amClusterBomb} nil, |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
35 |
{amBazooka} TestBazooka, |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
36 |
{amUFO} nil, |
70 | 37 |
{amShotgun} TestShotgun, |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
38 |
{amPickHammer} nil, |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
39 |
{amSkip} nil, |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
40 |
{amRope} nil, |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
41 |
{amMine} nil, |
75 | 42 |
{amDEagle} TestDesertEagle, |
79 | 43 |
{amDynamite} nil, |
143 | 44 |
{amFirePunch} TestFirePunch, |
45 |
{amBaseballBat} TestBaseballBat |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
46 |
); |
4 | 47 |
|
143 | 48 |
const BadTurn = Low(integer); |
49 |
||
4 | 50 |
implementation |
75 | 51 |
uses uMisc, uAIMisc, uLand; |
4 | 52 |
|
64 | 53 |
function Metric(x1, y1, x2, y2: integer): integer; |
4 | 54 |
begin |
64 | 55 |
Result:= abs(x1 - x2) + abs(y1 - y2) |
4 | 56 |
end; |
57 |
||
136 | 58 |
function TestBazooka(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
107 | 59 |
var Vx, Vy, r: Double; |
60 |
rTime: Double; |
|
71 | 61 |
Score, EX, EY: integer; |
4 | 62 |
|
64 | 63 |
function CheckTrace: integer; |
107 | 64 |
var x, y, dX, dY: Double; |
4 | 65 |
t: integer; |
66 |
begin |
|
53 | 67 |
x:= Me.X; |
68 |
y:= Me.Y; |
|
4 | 69 |
dX:= Vx; |
70 |
dY:= -Vy; |
|
64 | 71 |
t:= trunc(rTime); |
4 | 72 |
repeat |
73 |
x:= x + dX; |
|
74 |
y:= y + dY; |
|
75 |
dX:= dX + cWindSpeed; |
|
76 |
dY:= dY + cGravity; |
|
77 |
dec(t) |
|
64 | 78 |
until TestColl(round(x), round(y), 5) or (t <= 0); |
71 | 79 |
EX:= round(x); |
80 |
EY:= round(y); |
|
70 | 81 |
Result:= RateExplosion(Me, round(x), round(y), 101); |
82 |
if Result = 0 then Result:= - Metric(Targ.X, Targ.Y, round(x), round(y)) div 64 |
|
4 | 83 |
end; |
84 |
||
85 |
begin |
|
86 |
Time:= 0; |
|
82 | 87 |
rTime:= 50; |
71 | 88 |
ExplR:= 0; |
70 | 89 |
Result:= BadTurn; |
4 | 90 |
repeat |
136 | 91 |
rTime:= rTime + 150 + Level * 25 + random * 250; |
4 | 92 |
Vx:= - cWindSpeed * rTime / 2 + (Targ.X - Me.X) / rTime; |
93 |
Vy:= cGravity * rTime / 2 - (Targ.Y - Me.Y) / rTime; |
|
94 |
r:= sqr(Vx) + sqr(Vy); |
|
64 | 95 |
if r <= 1 then |
4 | 96 |
begin |
64 | 97 |
Score:= CheckTrace; |
98 |
if Result <= Score then |
|
99 |
begin |
|
100 |
r:= sqrt(r); |
|
141 | 101 |
Angle:= DxDy2AttackAngle(Vx, Vy) + rndSign(random((Level - 1) * 8)); |
136 | 102 |
Power:= round(r * cMaxPower) - random((Level - 1) * 15 + 1); |
74 | 103 |
ExplR:= 100; |
71 | 104 |
ExplX:= EX; |
105 |
ExplY:= EY; |
|
143 | 106 |
Result:= Score |
64 | 107 |
end; |
4 | 108 |
end |
82 | 109 |
until (rTime >= 4500) |
39 | 110 |
end; |
4 | 111 |
|
136 | 112 |
function TestGrenade(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
70 | 113 |
const tDelta = 24; |
107 | 114 |
var Vx, Vy, r: Double; |
71 | 115 |
Score, EX, EY: integer; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
116 |
TestTime: Longword; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
117 |
|
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
118 |
function CheckTrace: integer; |
107 | 119 |
var x, y, dY: Double; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
120 |
t: integer; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
121 |
begin |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
122 |
x:= Me.X; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
123 |
y:= Me.Y; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
124 |
dY:= -Vy; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
125 |
t:= TestTime; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
126 |
repeat |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
127 |
x:= x + Vx; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
128 |
y:= y + dY; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
129 |
dY:= dY + cGravity; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
130 |
dec(t) |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
131 |
until TestColl(round(x), round(y), 5) or (t = 0); |
71 | 132 |
EX:= round(x); |
133 |
EY:= round(y); |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
134 |
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
|
135 |
else Result:= Low(integer) |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
136 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
137 |
|
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
138 |
begin |
70 | 139 |
Result:= BadTurn; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
140 |
TestTime:= 0; |
71 | 141 |
ExplR:= 0; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
142 |
repeat |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
143 |
inc(TestTime, 1000); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
144 |
Vx:= (Targ.X - Me.X) / (TestTime + tDelta); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
145 |
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
|
146 |
r:= sqr(Vx) + sqr(Vy); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
147 |
if r <= 1 then |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
148 |
begin |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
149 |
Score:= CheckTrace; |
70 | 150 |
if Result < Score then |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
151 |
begin |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
152 |
r:= sqrt(r); |
136 | 153 |
Angle:= DxDy2AttackAngle(Vx, Vy) + rndSign(random(Level)); |
154 |
Power:= round(r * cMaxPower) + rndSign(random(Level) * 12); |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
155 |
Time:= TestTime; |
74 | 156 |
ExplR:= 100; |
71 | 157 |
ExplX:= EX; |
158 |
ExplY:= EY; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
159 |
Result:= Score |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
160 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
161 |
end |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
162 |
until (TestTime = 5000) |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
163 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
164 |
|
136 | 165 |
function TestShotgun(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
107 | 166 |
var Vx, Vy, x, y: Double; |
136 | 167 |
begin |
79 | 168 |
ExplR:= 0; |
70 | 169 |
if Metric(round(Me.X), round(Me.Y), Targ.X, Targ.Y) < 80 then |
170 |
begin |
|
171 |
Result:= BadTurn; |
|
172 |
exit |
|
173 |
end; |
|
174 |
Time:= 0; |
|
175 |
Power:= 1; |
|
176 |
Vx:= (Targ.X - Me.X)/1024; |
|
177 |
Vy:= (Targ.Y - Me.Y)/1024; |
|
178 |
x:= Me.X; |
|
179 |
y:= Me.Y; |
|
180 |
Angle:= DxDy2AttackAngle(Vx, -Vy); |
|
181 |
repeat |
|
182 |
x:= x + vX; |
|
183 |
y:= y + vY; |
|
184 |
if TestColl(round(x), round(y), 2) then |
|
185 |
begin |
|
143 | 186 |
Result:= RateShove(Me, round(x), round(y), 25, 25) * 2; |
187 |
if Result = 0 then Result:= - Metric(Targ.X, Targ.Y, round(x), round(y)) div 64 |
|
188 |
else dec(Result, Level * 4000); |
|
70 | 189 |
exit |
190 |
end |
|
191 |
until (abs(Targ.X - x) + abs(Targ.Y - y) < 4) or (x < 0) or (y < 0) or (x > 2048) or (y > 1024); |
|
192 |
Result:= BadTurn |
|
193 |
end; |
|
194 |
||
136 | 195 |
function TestDesertEagle(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
107 | 196 |
var Vx, Vy, x, y, t: Double; |
75 | 197 |
d: Longword; |
198 |
begin |
|
79 | 199 |
ExplR:= 0; |
75 | 200 |
if abs(Me.X - Targ.X) + abs(Me.Y - Targ.Y) < 80 then |
201 |
begin |
|
202 |
Result:= BadTurn; |
|
203 |
exit |
|
204 |
end; |
|
205 |
Time:= 0; |
|
206 |
Power:= 1; |
|
207 |
t:= sqrt(sqr(Targ.X - Me.X) + sqr(Targ.Y - Me.Y)) * 2; |
|
208 |
Vx:= (Targ.X - Me.X) / t; |
|
209 |
Vy:= (Targ.Y - Me.Y) / t; |
|
210 |
x:= Me.X; |
|
211 |
y:= Me.Y; |
|
212 |
Angle:= DxDy2AttackAngle(Vx, -Vy); |
|
213 |
d:= 0; |
|
214 |
repeat |
|
215 |
x:= x + vX; |
|
216 |
y:= y + vY; |
|
217 |
if ((round(x) and $FFFFF800) = 0)and((round(y) and $FFFFFC00) = 0) |
|
218 |
and (Land[round(y), round(x)] <> 0) then inc(d); |
|
219 |
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); |
|
220 |
if abs(Targ.X - x) + abs(Targ.Y - y) < 2 then Result:= max(0, (4 - d div 50) * 7 * 1024) |
|
221 |
else Result:= Low(integer) |
|
222 |
end; |
|
223 |
||
136 | 224 |
function TestBaseballBat(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
79 | 225 |
begin |
226 |
ExplR:= 0; |
|
143 | 227 |
if (Level > 2) and (abs(Me.X - Targ.X) + abs(Me.Y - Targ.Y) >= 25) then |
79 | 228 |
begin |
229 |
Result:= BadTurn; |
|
230 |
exit |
|
231 |
end; |
|
232 |
Time:= 0; |
|
233 |
Power:= 1; |
|
108 | 234 |
Angle:= DxDy2AttackAngle(hwSign(Targ.X - Me.X), 1); |
143 | 235 |
Result:= RateShove(Me, round(Me.X) + 10 * hwSign(Targ.X - Me.X), round(Me.Y), 15, 30); |
236 |
if Result <= 0 then Result:= BadTurn |
|
79 | 237 |
end; |
238 |
||
136 | 239 |
function TestFirePunch(Me: PGear; Targ: TPoint; Level: Integer; out Time: Longword; out Angle, Power: integer; out ExplX, ExplY, ExplR: integer): integer; |
82 | 240 |
var i: integer; |
241 |
begin |
|
242 |
ExplR:= 0; |
|
243 |
if (abs(Me.X - Targ.X) > 25) or (abs(Me.Y - 50 - Targ.Y) > 50) then |
|
244 |
begin |
|
245 |
Result:= BadTurn; |
|
246 |
exit |
|
247 |
end; |
|
248 |
Time:= 0; |
|
249 |
Power:= 1; |
|
108 | 250 |
Angle:= DxDy2AttackAngle(hwSign(Targ.X - Me.X), 1); |
82 | 251 |
Result:= 0; |
252 |
for i:= 0 to 4 do |
|
143 | 253 |
Result:= Result + RateShove(Me, round(Me.X) + 10 * hwSign(Targ.X - Me.X), round(Me.Y) - 20 * i - 5, 10, 30); |
254 |
if Result <= 0 then Result:= BadTurn |
|
82 | 255 |
end; |
256 |
||
4 | 257 |
end. |