author | unc0rr |
Thu, 30 Jul 2009 17:32:46 +0000 | |
changeset 2289 | cb850ba733bd |
parent 1066 | 1f1b3686a2b0 |
child 2376 | ece7b87f1334 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
883 | 3 |
* Copyright (c) 2005-2008 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; |
599 | 33 |
ThinkThread: TThreadID; |
509 | 34 |
hasThread: LongInt = 0; |
599 | 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 |
|
602 | 48 |
with CurrentHedgehog^ do |
445 | 49 |
if Gear <> nil then |
50 |
if BotLevel <> 0 then |
|
2289 | 51 |
StopMessages(Gear^.Message); |
740 | 52 |
|
64 | 53 |
BestActions.Count:= 0; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
54 |
BestActions.Pos:= 0 |
369 | 55 |
end; |
56 |
||
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
57 |
procedure TestAmmos(var Actions: TActions; Me: PGear; isMoved: boolean); |
545 | 58 |
var BotLevel: Longword; |
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
59 |
ap: TAttackParams; |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
60 |
Score, i: LongInt; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
61 |
a, aa: TAmmoType; |
4 | 62 |
begin |
369 | 63 |
BotLevel:= PHedgehog(Me^.Hedgehog)^.BotLevel; |
433 | 64 |
|
64 | 65 |
for i:= 0 to Pred(Targets.Count) do |
509 | 66 |
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
|
67 |
begin |
602 | 68 |
with CurrentHedgehog^ do |
369 | 69 |
a:= Ammo^[CurSlot, CurAmmo].AmmoType; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
70 |
aa:= a; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
71 |
repeat |
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
72 |
if (CanUseAmmo[a]) and |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
73 |
((not isMoved) or ((AmmoTests[a].flags and amtest_OnTurn) = 0)) then |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
74 |
begin |
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
75 |
Score:= AmmoTests[a].proc(Me, Targets.ar[i].Point, BotLevel, ap); |
139 | 76 |
if Actions.Score + Score > BestActions.Score then |
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
77 |
if (BestActions.Score < 0) or (Actions.Score + Score > BestActions.Score + LongInt(BotLevel) * 2048) then |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
78 |
begin |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
79 |
BestActions:= Actions; |
136 | 80 |
inc(BestActions.Score, Score); |
194 | 81 |
|
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
82 |
AddAction(BestActions, aia_Weapon, Longword(a), 300 + random(400), 0, 0); |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
83 |
if (ap.Time <> 0) then AddAction(BestActions, aia_Timer, ap.Time div 1000, 400, 0, 0); |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
84 |
if (ap.Angle > 0) then AddAction(BestActions, aia_LookRight, 0, 200, 0, 0) |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
85 |
else if (ap.Angle < 0) then AddAction(BestActions, aia_LookLeft, 0, 200, 0, 0); |
83 | 86 |
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
|
87 |
begin |
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
88 |
ap.Angle:= LongInt(Me^.Angle) - Abs(ap.Angle); |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
89 |
if ap.Angle > 0 then |
83 | 90 |
begin |
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
91 |
AddAction(BestActions, aia_Up, aim_push, 300 + random(250), 0, 0); |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
92 |
AddAction(BestActions, aia_Up, aim_release, ap.Angle, 0, 0) |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
93 |
end else if ap.Angle < 0 then |
83 | 94 |
begin |
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
95 |
AddAction(BestActions, aia_Down, aim_push, 300 + random(250), 0, 0); |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
96 |
AddAction(BestActions, aia_Down, aim_release, -ap.Angle, 0, 0) |
83 | 97 |
end |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
98 |
end; |
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
99 |
if (Ammoz[a].Ammo.Propz and ammoprop_NeedTarget) <> 0 then |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
100 |
begin |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
101 |
AddAction(BestActions, aia_Put, 0, 1, ap.AttackPutX, ap.AttackPutY) |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
102 |
end; |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
103 |
if (Ammoz[a].Ammo.Propz and ammoprop_AttackingPut) = 0 then |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
104 |
begin |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
105 |
AddAction(BestActions, aia_attack, aim_push, 650 + random(300), 0, 0); |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
106 |
AddAction(BestActions, aia_attack, aim_release, ap.Power, 0, 0); |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
107 |
end; |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
108 |
if ap.ExplR > 0 then |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
109 |
AddAction(BestActions, aia_AwareExpl, ap.ExplR, 10, ap.ExplX, ap.ExplY); |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
110 |
end |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
111 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
112 |
if a = High(TAmmoType) then a:= Low(TAmmoType) |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
113 |
else inc(a) |
509 | 114 |
until (a = aa) or |
602 | 115 |
(CurrentHedgehog^.AttacksNum > 0) or |
509 | 116 |
StopThinking |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
117 |
end |
64 | 118 |
end; |
4 | 119 |
|
64 | 120 |
procedure Walk(Me: PGear); |
80 | 121 |
const FallPixForBranching = cHHRadius * 2 + 8; |
433 | 122 |
cBranchStackSize = 12; |
123 |
||
124 |
type TStackEntry = record |
|
125 |
WastedTicks: Longword; |
|
126 |
MadeActions: TActions; |
|
127 |
Hedgehog: TGear; |
|
128 |
end; |
|
941
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
936
diff
changeset
|
129 |
|
433 | 130 |
var Stack: record |
131 |
Count: Longword; |
|
132 |
States: array[0..Pred(cBranchStackSize)] of TStackEntry; |
|
133 |
end; |
|
134 |
||
135 |
function Push(Ticks: Longword; const Actions: TActions; const Me: TGear; Dir: integer): boolean; |
|
136 |
var Result: boolean; |
|
137 |
begin |
|
138 |
Result:= (Stack.Count < cBranchStackSize) and (Actions.Count < MAXACTIONS - 5); |
|
139 |
if Result then |
|
140 |
with Stack.States[Stack.Count] do |
|
141 |
begin |
|
142 |
WastedTicks:= Ticks; |
|
143 |
MadeActions:= Actions; |
|
144 |
Hedgehog:= Me; |
|
145 |
Hedgehog.Message:= Dir; |
|
146 |
inc(Stack.Count) |
|
147 |
end; |
|
148 |
Push:= Result |
|
149 |
end; |
|
150 |
||
151 |
procedure Pop(var Ticks: Longword; var Actions: TActions; var Me: TGear); |
|
152 |
begin |
|
153 |
dec(Stack.Count); |
|
154 |
with Stack.States[Stack.Count] do |
|
155 |
begin |
|
156 |
Ticks:= WastedTicks; |
|
157 |
Actions:= MadeActions; |
|
158 |
Me:= Hedgehog |
|
159 |
end |
|
160 |
end; |
|
161 |
||
162 |
function PosInThinkStack(Me: PGear): boolean; |
|
163 |
var i: Longword; |
|
164 |
begin |
|
165 |
i:= 0; |
|
166 |
while (i < Stack.Count) do |
|
167 |
begin |
|
168 |
if(not(hwAbs(Stack.States[i].Hedgehog.X - Me^.X) + |
|
498 | 169 |
hwAbs(Stack.States[i].Hedgehog.Y - Me^.Y) > _2)) and |
433 | 170 |
(Stack.States[i].Hedgehog.Message = Me^.Message) then exit(true); |
171 |
inc(i) |
|
172 |
end; |
|
173 |
PosInThinkStack:= false |
|
174 |
end; |
|
175 |
||
176 |
||
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
177 |
var Actions: TActions; |
433 | 178 |
ticks, maxticks, steps, BotLevel, tmp: Longword; |
179 |
BaseRate, BestRate, Rate: integer; |
|
75 | 180 |
GoInfo: TGoInfo; |
80 | 181 |
CanGo: boolean; |
182 |
AltMe: TGear; |
|
64 | 183 |
begin |
433 | 184 |
Actions.Count:= 0; |
185 |
Actions.Pos:= 0; |
|
186 |
Actions.Score:= 0; |
|
187 |
Stack.Count:= 0; |
|
369 | 188 |
BotLevel:= PHedgehog(Me^.Hedgehog)^.BotLevel; |
75 | 189 |
|
433 | 190 |
tmp:= random(2) + 1; |
191 |
Push(0, Actions, Me^, tmp); |
|
192 |
Push(0, Actions, Me^, tmp xor 3); |
|
193 |
||
369 | 194 |
if (Me^.State and gstAttacked) = 0 then maxticks:= max(0, TurnTimeLeft - 5000 - 4000 * BotLevel) |
433 | 195 |
else maxticks:= TurnTimeLeft; |
75 | 196 |
|
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
197 |
if (Me^.State and gstAttacked) = 0 then TestAmmos(Actions, Me, false); |
433 | 198 |
BestRate:= RatePlace(Me); |
199 |
BaseRate:= max(BestRate, 0); |
|
75 | 200 |
|
433 | 201 |
while (Stack.Count > 0) and not StopThinking do |
202 |
begin |
|
203 |
Pop(ticks, Actions, Me^); |
|
193 | 204 |
|
433 | 205 |
AddAction(Actions, Me^.Message, aim_push, 250, 0, 0); |
369 | 206 |
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
|
207 |
else AddAction(Actions, aia_WaitXR, hwRound(Me^.X), 0, 0, 0); |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
208 |
steps:= 0; |
82 | 209 |
|
433 | 210 |
while (not StopThinking) and (not PosInThinkStack(Me)) do |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
211 |
begin |
80 | 212 |
CanGo:= HHGo(Me, @AltMe, GoInfo); |
75 | 213 |
inc(ticks, GoInfo.Ticks); |
214 |
if ticks > maxticks then break; |
|
194 | 215 |
|
136 | 216 |
if (BotLevel < 5) and (GoInfo.JumpType = jmpHJump) then // hjump support |
80 | 217 |
if Push(ticks, Actions, AltMe, Me^.Message) then |
433 | 218 |
with Stack.States[Pred(Stack.Count)] do |
80 | 219 |
begin |
791
928d2830fd0c
Make AI aware of new high jump technique (there's still an issue with two successive jumps)
unc0rr
parents:
741
diff
changeset
|
220 |
if Me^.dX.isNegative then AddAction(MadeActions, aia_LookRight, 0, 200, 0, 0) |
928d2830fd0c
Make AI aware of new high jump technique (there's still an issue with two successive jumps)
unc0rr
parents:
741
diff
changeset
|
221 |
else AddAction(MadeActions, aia_LookLeft, 0, 200, 0, 0); |
928d2830fd0c
Make AI aware of new high jump technique (there's still an issue with two successive jumps)
unc0rr
parents:
741
diff
changeset
|
222 |
AddAction(MadeActions, aia_HJump, 0, 305 + random(50), 0, 0); |
369 | 223 |
AddAction(MadeActions, aia_HJump, 0, 350, 0, 0); |
791
928d2830fd0c
Make AI aware of new high jump technique (there's still an issue with two successive jumps)
unc0rr
parents:
741
diff
changeset
|
224 |
if Me^.dX.isNegative then AddAction(MadeActions, aia_LookLeft, 0, 200, 0, 0) |
928d2830fd0c
Make AI aware of new high jump technique (there's still an issue with two successive jumps)
unc0rr
parents:
741
diff
changeset
|
225 |
else AddAction(MadeActions, aia_LookRight, 0, 200, 0, 0); |
80 | 226 |
end; |
136 | 227 |
if (BotLevel < 3) and (GoInfo.JumpType = jmpLJump) then // ljump support |
80 | 228 |
if Push(ticks, Actions, AltMe, Me^.Message) then |
433 | 229 |
with Stack.States[Pred(Stack.Count)] do |
791
928d2830fd0c
Make AI aware of new high jump technique (there's still an issue with two successive jumps)
unc0rr
parents:
741
diff
changeset
|
230 |
AddAction(MadeActions, aia_LJump, 0, 305 + random(50), 0, 0); |
433 | 231 |
|
80 | 232 |
if not CanGo then break; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
233 |
inc(steps); |
544 | 234 |
Actions.actions[Pred(Actions.Count)].Param:= hwRound(Me^.X); |
70 | 235 |
Rate:= RatePlace(Me); |
433 | 236 |
if Rate > BestRate then |
70 | 237 |
begin |
238 |
BestActions:= Actions; |
|
433 | 239 |
BestRate:= Rate; |
240 |
Me^.State:= Me^.State or gstAttacked // we have better place, go there and don't use ammo |
|
70 | 241 |
end |
433 | 242 |
else if Rate < BestRate then break; |
243 |
if ((Me^.State and gstAttacked) = 0) |
|
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
542
diff
changeset
|
244 |
and ((steps mod 4) = 0) then TestAmmos(Actions, Me, true); |
193 | 245 |
if GoInfo.FallPix >= FallPixForBranching then |
246 |
Push(ticks, Actions, Me^, Me^.Message xor 3); // aia_Left xor 3 = aia_Right |
|
433 | 247 |
end; |
193 | 248 |
|
433 | 249 |
if BestRate > BaseRate then exit |
250 |
end |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
251 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
252 |
|
508 | 253 |
function Think(Me: Pointer): ptrint; |
74 | 254 |
var BackMe, WalkMe: TGear; |
433 | 255 |
StartTicks: Longword; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
256 |
begin |
509 | 257 |
InterlockedIncrement(hasThread); |
433 | 258 |
StartTicks:= GameTicks; |
500 | 259 |
BackMe:= PGear(Me)^; |
509 | 260 |
|
500 | 261 |
if (PGear(Me)^.State and gstAttacked) = 0 then |
74 | 262 |
if Targets.Count > 0 then |
263 |
begin |
|
509 | 264 |
WalkMe:= BackMe; |
74 | 265 |
Walk(@WalkMe); |
433 | 266 |
if (StartTicks > GameTicks - 1500) and not StopThinking then SDL_Delay(2000); |
267 |
if BestActions.Score < -1023 then |
|
146 | 268 |
begin |
433 | 269 |
BestActions.Count:= 0; |
270 |
AddAction(BestActions, aia_Skip, 0, 250, 0, 0); |
|
271 |
end; |
|
80 | 272 |
end else |
74 | 273 |
else begin |
433 | 274 |
while (not StopThinking) and (BestActions.Count = 0) do |
275 |
begin |
|
276 |
FillBonuses(true); |
|
277 |
WalkMe:= BackMe; |
|
509 | 278 |
Walk(@WalkMe); |
279 |
if not StopThinking then SDL_Delay(100) |
|
433 | 280 |
end |
281 |
end; |
|
500 | 282 |
PGear(Me)^.State:= PGear(Me)^.State and not gstHHThinking; |
509 | 283 |
Think:= 0; |
284 |
InterlockedDecrement(hasThread) |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
285 |
end; |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
286 |
|
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
287 |
procedure StartThink(Me: PGear); |
75 | 288 |
var a: TAmmoType; |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
289 |
begin |
542 | 290 |
if ((Me^.State and (gstAttacking or gstHHJumping or gstMoving)) <> 0) |
439 | 291 |
or isInMultiShoot then exit; |
506 | 292 |
|
522 | 293 |
//DeleteCI(Me); // this might break demo |
369 | 294 |
Me^.State:= Me^.State or gstHHThinking; |
295 |
Me^.Message:= 0; |
|
509 | 296 |
|
297 |
BestActions.Count:= 0; |
|
298 |
BestActions.Pos:= 0; |
|
299 |
BestActions.Score:= Low(integer); |
|
300 |
||
433 | 301 |
StopThinking:= false; |
302 |
ThinkingHH:= Me; |
|
509 | 303 |
|
70 | 304 |
FillTargets; |
80 | 305 |
if Targets.Count = 0 then |
306 |
begin |
|
369 | 307 |
OutError('AI: no targets!?', false); |
80 | 308 |
exit |
309 |
end; |
|
941
b5222ddafe1f
- Fix bug with picking up ammos from cases, when total ammo count may become more than AMMO_INFINITE
unc0rr
parents:
936
diff
changeset
|
310 |
|
369 | 311 |
FillBonuses((Me^.State and gstAttacked) <> 0); |
75 | 312 |
for a:= Low(TAmmoType) to High(TAmmoType) do |
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
545
diff
changeset
|
313 |
CanUseAmmo[a]:= Assigned(AmmoTests[a].proc) and HHHasAmmo(PHedgehog(Me^.Hedgehog)^, a); |
433 | 314 |
{$IFDEF DEBUGFILE}AddFileLog('Enter Think Thread');{$ENDIF} |
500 | 315 |
BeginThread(@Think, Me, ThinkThread) |
433 | 316 |
end; |
191
a03c2d037e24
Bots are in the same thread as game. Fixes FreePascal issues.
unc0rr
parents:
183
diff
changeset
|
317 |
|
433 | 318 |
procedure ProcessBot; |
319 |
const StartTicks: Longword = 0; |
|
509 | 320 |
cStopThinkTime = 40; |
4 | 321 |
begin |
602 | 322 |
with CurrentHedgehog^ do |
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
323 |
if (Gear <> nil) |
369 | 324 |
and ((Gear^.State and gstHHDriven) <> 0) |
144 | 325 |
and (TurnTimeLeft < cHedgehogTurnTime - 50) then |
433 | 326 |
if ((Gear^.State and gstHHThinking) = 0) then |
509 | 327 |
if (BestActions.Pos >= BestActions.Count) |
328 |
and (TurnTimeLeft > cStopThinkTime) then |
|
433 | 329 |
begin |
936
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
883
diff
changeset
|
330 |
if Gear^.Message <> 0 then |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
883
diff
changeset
|
331 |
begin |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
883
diff
changeset
|
332 |
StopMessages(Gear^.Message); |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
883
diff
changeset
|
333 |
TryDo((Gear^.Message and gmAllStoppable) = 0, 'Engine bug: AI may break demos playing', true); |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
883
diff
changeset
|
334 |
end; |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
883
diff
changeset
|
335 |
if Gear^.Message <> 0 then exit; |
433 | 336 |
StartThink(Gear); |
337 |
StartTicks:= GameTicks |
|
338 |
end else ProcessAction(BestActions, Gear) |
|
509 | 339 |
else if ((GameTicks - StartTicks) > cMaxAIThinkTime) |
340 |
or (TurnTimeLeft <= cStopThinkTime) then StopThinking:= true |
|
369 | 341 |
end; |
4 | 342 |
|
343 |
end. |