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