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