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