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