author | unc0rr |
Wed, 21 Feb 2007 20:31:50 +0000 | |
changeset 474 | 2e3c1b9ad495 |
parent 445 | fb66abeb551f |
child 498 | 9c8b385dc9a1 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
393 | 3 |
* Copyright (c) 2005-2007 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 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 |
|
4 | 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. |
|
4 | 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 |
|
4 | 17 |
*) |
18 |
||
19 |
unit uAI; |
|
20 |
interface |
|
351 | 21 |
uses uFloat; |
4 | 22 |
{$INCLUDE options.inc} |
433 | 23 |
procedure ProcessBot; |
64 | 24 |
procedure FreeActionsList; |
4 | 25 |
|
26 |
implementation |
|
369 | 27 |
uses uTeams, uConsts, SDLh, uAIMisc, uGears, uAIAmmoTests, uAIActions, uMisc, |
445 | 28 |
uAmmos, uConsole; |
4 | 29 |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
30 |
var BestActions: TActions; |
433 | 31 |
ThinkThread: PSDL_Thread = nil; |
32 |
StopThinking: boolean; |
|
75 | 33 |
CanUseAmmo: array [TAmmoType] of boolean; |
4 | 34 |
|
369 | 35 |
procedure FreeActionsList; |
64 | 36 |
begin |
433 | 37 |
{$IFDEF DEBUGFILE}AddFileLog('FreeActionsList called');{$ENDIF} |
38 |
if ThinkThread <> nil then |
|
39 |
begin |
|
40 |
{$IFDEF DEBUGFILE}AddFileLog('Waiting AI thread to finish');{$ENDIF} |
|
41 |
StopThinking:= true; |
|
42 |
SDL_WaitThread(ThinkThread, nil); |
|
43 |
ThinkThread:= nil |
|
44 |
end; |
|
434 | 45 |
|
442 | 46 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
445 | 47 |
if Gear <> nil then |
48 |
if BotLevel <> 0 then |
|
49 |
begin |
|
50 |
if (Gear^.Message and gm_Left) <> 0 then ParseCommand('-left', true); |
|
51 |
if (Gear^.Message and gm_Right) <> 0 then ParseCommand('-right', true); |
|
52 |
end; |
|
442 | 53 |
|
64 | 54 |
BestActions.Count:= 0; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
55 |
BestActions.Pos:= 0 |
369 | 56 |
end; |
57 |
||
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
58 |
procedure TestAmmos(var Actions: TActions; Me: PGear); |
136 | 59 |
var Time, BotLevel: Longword; |
371 | 60 |
Angle, Power, Score, ExplX, ExplY, ExplR: LongInt; |
61 |
i: LongInt; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
62 |
a, aa: TAmmoType; |
4 | 63 |
begin |
369 | 64 |
BotLevel:= PHedgehog(Me^.Hedgehog)^.BotLevel; |
433 | 65 |
|
64 | 66 |
for i:= 0 to Pred(Targets.Count) do |
80 | 67 |
if (Targets.ar[i].Score >= 0) then |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
68 |
begin |
369 | 69 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
70 |
a:= Ammo^[CurSlot, CurAmmo].AmmoType; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
71 |
aa:= a; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
72 |
repeat |
75 | 73 |
if CanUseAmmo[a] then |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
74 |
begin |
136 | 75 |
Score:= AmmoTests[a](Me, Targets.ar[i].Point, BotLevel, Time, Angle, Power, ExplX, ExplY, ExplR); |
139 | 76 |
if Actions.Score + Score > BestActions.Score then |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
77 |
begin |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
78 |
BestActions:= Actions; |
136 | 79 |
inc(BestActions.Score, Score); |
194 | 80 |
|
369 | 81 |
AddAction(BestActions, aia_Weapon, Longword(a), 500, 0, 0); |
82 |
if Time <> 0 then AddAction(BestActions, aia_Timer, Time div 1000, 400, 0, 0); |
|
83 |
if (Angle > 0) then AddAction(BestActions, aia_LookRight, 0, 200, 0, 0) |
|
84 |
else if (Angle < 0) then AddAction(BestActions, aia_LookLeft, 0, 200, 0, 0); |
|
83 | 85 |
if (Ammoz[a].Ammo.Propz and ammoprop_NoCrosshair) = 0 then |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
86 |
begin |
439 | 87 |
Angle:= LongInt(Me^.Angle) - Abs(Angle); |
83 | 88 |
if Angle > 0 then |
89 |
begin |
|
369 | 90 |
AddAction(BestActions, aia_Up, aim_push, 500, 0, 0); |
91 |
AddAction(BestActions, aia_Up, aim_release, Angle, 0, 0) |
|
83 | 92 |
end else if Angle < 0 then |
93 |
begin |
|
369 | 94 |
AddAction(BestActions, aia_Down, aim_push, 500, 0, 0); |
95 |
AddAction(BestActions, aia_Down, aim_release, -Angle, 0, 0) |
|
83 | 96 |
end |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
97 |
end; |
369 | 98 |
AddAction(BestActions, aia_attack, aim_push, 800, 0, 0); |
99 |
AddAction(BestActions, aia_attack, aim_release, Power, 0, 0); |
|
71 | 100 |
if ExplR > 0 then |
101 |
AddAction(BestActions, aia_AwareExpl, ExplR, 10, ExplX, ExplY); |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
102 |
end |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
103 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
104 |
if a = High(TAmmoType) then a:= Low(TAmmoType) |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
105 |
else inc(a) |
433 | 106 |
until (a = aa) or (CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].AttacksNum > 0) |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
107 |
end |
64 | 108 |
end; |
4 | 109 |
|
64 | 110 |
procedure Walk(Me: PGear); |
80 | 111 |
const FallPixForBranching = cHHRadius * 2 + 8; |
433 | 112 |
cBranchStackSize = 12; |
113 |
||
114 |
type TStackEntry = record |
|
115 |
WastedTicks: Longword; |
|
116 |
MadeActions: TActions; |
|
117 |
Hedgehog: TGear; |
|
118 |
end; |
|
119 |
||
120 |
var Stack: record |
|
121 |
Count: Longword; |
|
122 |
States: array[0..Pred(cBranchStackSize)] of TStackEntry; |
|
123 |
end; |
|
124 |
||
125 |
function Push(Ticks: Longword; const Actions: TActions; const Me: TGear; Dir: integer): boolean; |
|
126 |
var Result: boolean; |
|
127 |
begin |
|
128 |
Result:= (Stack.Count < cBranchStackSize) and (Actions.Count < MAXACTIONS - 5); |
|
129 |
if Result then |
|
130 |
with Stack.States[Stack.Count] do |
|
131 |
begin |
|
132 |
WastedTicks:= Ticks; |
|
133 |
MadeActions:= Actions; |
|
134 |
Hedgehog:= Me; |
|
135 |
Hedgehog.Message:= Dir; |
|
136 |
inc(Stack.Count) |
|
137 |
end; |
|
138 |
Push:= Result |
|
139 |
end; |
|
140 |
||
141 |
procedure Pop(var Ticks: Longword; var Actions: TActions; var Me: TGear); |
|
142 |
begin |
|
143 |
dec(Stack.Count); |
|
144 |
with Stack.States[Stack.Count] do |
|
145 |
begin |
|
146 |
Ticks:= WastedTicks; |
|
147 |
Actions:= MadeActions; |
|
148 |
Me:= Hedgehog |
|
149 |
end |
|
150 |
end; |
|
151 |
||
152 |
function PosInThinkStack(Me: PGear): boolean; |
|
153 |
var i: Longword; |
|
154 |
begin |
|
155 |
i:= 0; |
|
156 |
while (i < Stack.Count) do |
|
157 |
begin |
|
158 |
if(not(hwAbs(Stack.States[i].Hedgehog.X - Me^.X) + |
|
159 |
hwAbs(Stack.States[i].Hedgehog.Y - Me^.Y) > 2)) and |
|
160 |
(Stack.States[i].Hedgehog.Message = Me^.Message) then exit(true); |
|
161 |
inc(i) |
|
162 |
end; |
|
163 |
PosInThinkStack:= false |
|
164 |
end; |
|
165 |
||
166 |
||
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
167 |
var Actions: TActions; |
433 | 168 |
ticks, maxticks, steps, BotLevel, tmp: Longword; |
169 |
BaseRate, BestRate, Rate: integer; |
|
75 | 170 |
GoInfo: TGoInfo; |
80 | 171 |
CanGo: boolean; |
172 |
AltMe: TGear; |
|
64 | 173 |
begin |
433 | 174 |
Actions.Count:= 0; |
175 |
Actions.Pos:= 0; |
|
176 |
Actions.Score:= 0; |
|
177 |
Stack.Count:= 0; |
|
369 | 178 |
BotLevel:= PHedgehog(Me^.Hedgehog)^.BotLevel; |
75 | 179 |
|
433 | 180 |
tmp:= random(2) + 1; |
181 |
Push(0, Actions, Me^, tmp); |
|
182 |
Push(0, Actions, Me^, tmp xor 3); |
|
183 |
||
369 | 184 |
if (Me^.State and gstAttacked) = 0 then maxticks:= max(0, TurnTimeLeft - 5000 - 4000 * BotLevel) |
433 | 185 |
else maxticks:= TurnTimeLeft; |
75 | 186 |
|
433 | 187 |
if (Me^.State and gstAttacked) = 0 then TestAmmos(Actions, Me); |
188 |
BestRate:= RatePlace(Me); |
|
189 |
BaseRate:= max(BestRate, 0); |
|
75 | 190 |
|
433 | 191 |
while (Stack.Count > 0) and not StopThinking do |
192 |
begin |
|
193 |
Pop(ticks, Actions, Me^); |
|
193 | 194 |
|
433 | 195 |
AddAction(Actions, Me^.Message, aim_push, 250, 0, 0); |
369 | 196 |
if (Me^.Message and gm_Left) <> 0 then AddAction(Actions, aia_WaitXL, hwRound(Me^.X), 0, 0, 0) |
370
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
369
diff
changeset
|
197 |
else AddAction(Actions, aia_WaitXR, hwRound(Me^.X), 0, 0, 0); |
369 | 198 |
AddAction(Actions, Me^.Message, aim_release, 0, 0, 0); |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
199 |
steps:= 0; |
82 | 200 |
|
433 | 201 |
while (not StopThinking) and (not PosInThinkStack(Me)) do |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
202 |
begin |
80 | 203 |
CanGo:= HHGo(Me, @AltMe, GoInfo); |
75 | 204 |
inc(ticks, GoInfo.Ticks); |
205 |
if ticks > maxticks then break; |
|
194 | 206 |
|
136 | 207 |
if (BotLevel < 5) and (GoInfo.JumpType = jmpHJump) then // hjump support |
80 | 208 |
if Push(ticks, Actions, AltMe, Me^.Message) then |
433 | 209 |
with Stack.States[Pred(Stack.Count)] do |
80 | 210 |
begin |
369 | 211 |
AddAction(MadeActions, aia_HJump, 0, 305, 0, 0); |
212 |
AddAction(MadeActions, aia_HJump, 0, 350, 0, 0); |
|
80 | 213 |
end; |
136 | 214 |
if (BotLevel < 3) and (GoInfo.JumpType = jmpLJump) then // ljump support |
80 | 215 |
if Push(ticks, Actions, AltMe, Me^.Message) then |
433 | 216 |
with Stack.States[Pred(Stack.Count)] do |
369 | 217 |
AddAction(MadeActions, aia_LJump, 0, 305, 0, 0); |
433 | 218 |
|
80 | 219 |
if not CanGo then break; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
220 |
inc(steps); |
369 | 221 |
Actions.actions[Actions.Count - 2].Param:= hwRound(Me^.X); |
70 | 222 |
Rate:= RatePlace(Me); |
433 | 223 |
if Rate > BestRate then |
70 | 224 |
begin |
225 |
BestActions:= Actions; |
|
433 | 226 |
BestRate:= Rate; |
227 |
Me^.State:= Me^.State or gstAttacked // we have better place, go there and don't use ammo |
|
70 | 228 |
end |
433 | 229 |
else if Rate < BestRate then break; |
230 |
if ((Me^.State and gstAttacked) = 0) |
|
231 |
and ((steps mod 4) = 0) then TestAmmos(Actions, Me); |
|
193 | 232 |
if GoInfo.FallPix >= FallPixForBranching then |
233 |
Push(ticks, Actions, Me^, Me^.Message xor 3); // aia_Left xor 3 = aia_Right |
|
433 | 234 |
end; |
193 | 235 |
|
433 | 236 |
if BestRate > BaseRate then exit |
237 |
end |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
238 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
239 |
|
433 | 240 |
procedure Think(Me: PGear); cdecl; |
74 | 241 |
var BackMe, WalkMe: TGear; |
433 | 242 |
StartTicks: Longword; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
243 |
begin |
433 | 244 |
StartTicks:= GameTicks; |
245 |
BestActions.Count:= 0; |
|
246 |
BestActions.Pos:= 0; |
|
247 |
BestActions.Score:= Low(integer); |
|
74 | 248 |
BackMe:= Me^; |
249 |
WalkMe:= BackMe; |
|
369 | 250 |
if (Me^.State and gstAttacked) = 0 then |
74 | 251 |
if Targets.Count > 0 then |
252 |
begin |
|
253 |
Walk(@WalkMe); |
|
433 | 254 |
if (StartTicks > GameTicks - 1500) and not StopThinking then SDL_Delay(2000); |
255 |
if BestActions.Score < -1023 then |
|
146 | 256 |
begin |
433 | 257 |
BestActions.Count:= 0; |
258 |
AddAction(BestActions, aia_Skip, 0, 250, 0, 0); |
|
259 |
end; |
|
80 | 260 |
end else |
74 | 261 |
else begin |
193 | 262 |
Walk(@WalkMe); |
433 | 263 |
while (not StopThinking) and (BestActions.Count = 0) do |
264 |
begin |
|
265 |
SDL_Delay(100); |
|
266 |
FillBonuses(true); |
|
267 |
WalkMe:= BackMe; |
|
268 |
Walk(@WalkMe) |
|
269 |
end |
|
270 |
end; |
|
271 |
Me^.State:= Me^.State and not gstHHThinking |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
272 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
273 |
|
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
274 |
procedure StartThink(Me: PGear); |
75 | 275 |
var a: TAmmoType; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
276 |
begin |
439 | 277 |
if ((Me^.State and (gstAttacking or gstHHJumping or gstFalling or gstMoving)) <> 0) |
278 |
or isInMultiShoot then exit; |
|
369 | 279 |
Me^.State:= Me^.State or gstHHThinking; |
280 |
Me^.Message:= 0; |
|
433 | 281 |
StopThinking:= false; |
282 |
ThinkingHH:= Me; |
|
70 | 283 |
FillTargets; |
80 | 284 |
if Targets.Count = 0 then |
285 |
begin |
|
369 | 286 |
OutError('AI: no targets!?', false); |
80 | 287 |
exit |
288 |
end; |
|
369 | 289 |
FillBonuses((Me^.State and gstAttacked) <> 0); |
75 | 290 |
for a:= Low(TAmmoType) to High(TAmmoType) do |
369 | 291 |
CanUseAmmo[a]:= Assigned(AmmoTests[a]) and HHHasAmmo(PHedgehog(Me^.Hedgehog), a); |
433 | 292 |
{$IFDEF DEBUGFILE}AddFileLog('Enter Think Thread');{$ENDIF} |
293 |
ThinkThread:= SDL_CreateThread(@Think, Me) |
|
294 |
end; |
|
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
295 |
|
433 | 296 |
procedure ProcessBot; |
297 |
const StartTicks: Longword = 0; |
|
4 | 298 |
begin |
369 | 299 |
with CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog] do |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
300 |
if (Gear <> nil) |
369 | 301 |
and ((Gear^.State and gstHHDriven) <> 0) |
144 | 302 |
and (TurnTimeLeft < cHedgehogTurnTime - 50) then |
433 | 303 |
if ((Gear^.State and gstHHThinking) = 0) then |
304 |
if (BestActions.Pos >= BestActions.Count) then |
|
305 |
begin |
|
306 |
StartThink(Gear); |
|
307 |
StartTicks:= GameTicks |
|
308 |
end else ProcessAction(BestActions, Gear) |
|
309 |
else if (GameTicks - StartTicks) > cMaxAIThinkTime then StopThinking:= true |
|
369 | 310 |
end; |
4 | 311 |
|
433 | 312 |
|
4 | 313 |
end. |