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