author | unc0rr |
Thu, 13 Dec 2007 22:46:36 +0000 | |
changeset 656 | 6d6d9d7b1054 |
parent 544 | 9e068d2398ca |
child 676 | 2aa3082937ac |
permissions | -rw-r--r-- |
71 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
393 | 3 |
* Copyright (c) 2005-2007 Andrey Korotaev <unC0Rr@gmail.com> |
71 | 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 |
|
71 | 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. |
|
71 | 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 |
|
71 | 17 |
*) |
18 |
||
4 | 19 |
unit uAIActions; |
20 |
interface |
|
351 | 21 |
uses uGears, uFloat; |
80 | 22 |
{$INCLUDE options.inc} |
369 | 23 |
const MAXACTIONS = 96; |
64 | 24 |
aia_none = 0; |
4 | 25 |
aia_Left = 1; |
26 |
aia_Right = 2; |
|
27 |
aia_Timer = 3; |
|
70 | 28 |
aia_attack = 4; |
29 |
aia_Up = 5; |
|
30 |
aia_Down = 6; |
|
4 | 31 |
|
195
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
32 |
aia_Weapon = $8000; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
33 |
aia_WaitXL = $8001; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
34 |
aia_WaitXR = $8002; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
35 |
aia_LookLeft = $8003; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
36 |
aia_LookRight = $8004; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
37 |
aia_AwareExpl = $8005; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
38 |
aia_HJump = $8006; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
39 |
aia_LJump = $8007; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
40 |
aia_Skip = $8008; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
41 |
aia_Wait = $8009; |
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
433
diff
changeset
|
42 |
aia_Put = $800A; |
4 | 43 |
|
195
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
44 |
aim_push = $8000; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
45 |
aim_release = $8001; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
46 |
ai_specmask = $8000; |
4 | 47 |
|
64 | 48 |
type TAction = record |
371 | 49 |
Action: Longword; |
50 |
X, Y, Param: LongInt; |
|
4 | 51 |
Time: Longword; |
52 |
end; |
|
64 | 53 |
TActions = record |
54 |
Count, Pos: Longword; |
|
55 |
actions: array[0..Pred(MAXACTIONS)] of TAction; |
|
371 | 56 |
Score: LongInt; |
64 | 57 |
end; |
4 | 58 |
|
371 | 59 |
procedure AddAction(var Actions: TActions; Action: Longword; Param: LongInt; TimeDelta: Longword; X, Y: LongInt); |
64 | 60 |
procedure ProcessAction(var Actions: TActions; Me: PGear); |
369 | 61 |
|
4 | 62 |
implementation |
433 | 63 |
uses uMisc, uTeams, uConsts, uConsole, uAIMisc, uAI; |
4 | 64 |
|
70 | 65 |
const ActionIdToStr: array[0..6] of string[16] = ( |
4 | 66 |
{aia_none} '', |
67 |
{aia_Left} 'left', |
|
68 |
{aia_Right} 'right', |
|
69 |
{aia_Timer} 'timer', |
|
70 |
{aia_attack} 'attack', |
|
71 |
{aia_Up} 'up', |
|
72 |
{aia_Down} 'down' |
|
64 | 73 |
); |
4 | 74 |
|
80 | 75 |
{$IFDEF TRACEAIACTIONS} |
195
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
76 |
const SpecActionIdToStr: array[$8000..$8009] of string[16] = ( |
80 | 77 |
{aia_Weapon} 'aia_Weapon', |
78 |
{aia_WaitX} 'aia_WaitX', |
|
79 |
{aia_WaitY} 'aia_WaitY', |
|
80 |
{aia_LookLeft} 'aia_LookLeft', |
|
81 |
{aia_LookRight} 'aia_LookRight', |
|
82 |
{aia_AwareExpl} 'aia_AwareExpl', |
|
83 |
{aia_HJump} 'aia_HJump', |
|
144 | 84 |
{aia_LJump} 'aia_LJump', |
193 | 85 |
{aia_Skip} 'aia_Skip', |
86 |
{aia_Wait} 'aia_Wait' |
|
80 | 87 |
); |
88 |
||
89 |
procedure DumpAction(Action: TAction; Me: PGear); |
|
90 |
begin |
|
91 |
if (Action.Action and ai_specmask) = 0 then |
|
92 |
WriteLnToConsole('AI action: '+ActionIdToStr[Action.Action]) |
|
93 |
else begin |
|
94 |
WriteLnToConsole('AI action: '+SpecActionIdToStr[Action.Action]); |
|
193 | 95 |
if (Action.Action = aia_WaitXL) or (Action.Action = aia_WaitXR) then |
544 | 96 |
WriteLnToConsole('AI action Wait X = '+inttostr(Action.Param)+', current X = '+inttostr(hwRound(Me^.X))); |
80 | 97 |
end |
98 |
end; |
|
99 |
{$ENDIF} |
|
100 |
||
371 | 101 |
procedure AddAction(var Actions: TActions; Action: Longword; Param: LongInt; TimeDelta: Longword; X, Y: LongInt); |
4 | 102 |
begin |
64 | 103 |
with Actions do |
104 |
begin |
|
105 |
actions[Count].Action:= Action; |
|
106 |
actions[Count].Param:= Param; |
|
71 | 107 |
actions[Count].X:= X; |
108 |
actions[Count].Y:= Y; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
109 |
if Count > 0 then actions[Count].Time:= TimeDelta |
64 | 110 |
else actions[Count].Time:= GameTicks + TimeDelta; |
111 |
inc(Count); |
|
112 |
TryDo(Count < MAXACTIONS, 'AI: actions overflow', true); |
|
113 |
end |
|
4 | 114 |
end; |
115 |
||
64 | 116 |
procedure ProcessAction(var Actions: TActions; Me: PGear); |
4 | 117 |
var s: shortstring; |
280
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
118 |
|
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
119 |
procedure CheckHang; |
371 | 120 |
const PrevX: LongInt = 0; |
280
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
121 |
timedelta: Longword = 0; |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
122 |
begin |
369 | 123 |
if hwRound(Me^.X) <> PrevX then |
280
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
124 |
begin |
369 | 125 |
PrevX:= hwRound(Me^.X); |
280
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
126 |
timedelta:= 0 |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
127 |
end else |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
128 |
begin |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
129 |
inc(timedelta); |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
130 |
if timedelta > 2500 then |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
131 |
begin |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
132 |
timedelta:= 0; |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
133 |
Actions.Count:= 0 |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
134 |
end |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
135 |
end |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
136 |
end; |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
137 |
|
4 | 138 |
begin |
194 | 139 |
repeat |
64 | 140 |
if Actions.Pos >= Actions.Count then exit; |
141 |
with Actions.actions[Actions.Pos] do |
|
4 | 142 |
begin |
143 |
if Time > GameTicks then exit; |
|
80 | 144 |
{$IFDEF TRACEAIACTIONS} |
145 |
DumpAction(Actions.actions[Actions.Pos], Me); |
|
146 |
{$ENDIF} |
|
4 | 147 |
if (Action and ai_specmask) <> 0 then |
148 |
case Action of |
|
165 | 149 |
aia_Weapon: SetWeapon(TAmmoType(Param)); |
544 | 150 |
aia_WaitXL: if hwRound(Me^.X) = Param then |
151 |
begin |
|
152 |
Action:= aia_LookLeft; |
|
153 |
Time:= GameTicks; |
|
154 |
exit |
|
155 |
end |
|
369 | 156 |
else if hwRound(Me^.X) < Param then |
193 | 157 |
begin |
544 | 158 |
OutError('AI: WaitXL assert (' + IntToStr(hwRound(Me^.X)) + ' < ' + IntToStr(Param) + ')', false); |
433 | 159 |
FreeActionsList; |
160 |
exit |
|
193 | 161 |
end |
280
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
162 |
else begin CheckHang; exit end; |
544 | 163 |
aia_WaitXR: if hwRound(Me^.X) = Param then |
164 |
begin |
|
165 |
Action:= aia_LookRight; |
|
166 |
Time:= GameTicks; |
|
167 |
exit |
|
168 |
end |
|
369 | 169 |
else if hwRound(Me^.X) > Param then |
193 | 170 |
begin |
544 | 171 |
OutError('AI: WaitXR assert (' + IntToStr(hwRound(Me^.X)) + ' > ' + IntToStr(Param) + ')', false); |
433 | 172 |
FreeActionsList; |
173 |
exit |
|
193 | 174 |
end |
280
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
175 |
else begin CheckHang; exit end; |
369 | 176 |
aia_LookLeft: if not Me^.dX.isNegative then |
64 | 177 |
begin |
369 | 178 |
ParseCommand('+left', true); |
64 | 179 |
exit |
369 | 180 |
end else ParseCommand('-left', true); |
181 |
aia_LookRight: if Me^.dX.isNegative then |
|
64 | 182 |
begin |
369 | 183 |
ParseCommand('+right', true); |
64 | 184 |
exit |
369 | 185 |
end else ParseCommand('-right', true); |
71 | 186 |
aia_AwareExpl: AwareOfExplosion(X, Y, Param); |
369 | 187 |
aia_HJump: ParseCommand('hjump', true); |
188 |
aia_LJump: ParseCommand('ljump', true); |
|
189 |
aia_Skip: ParseCommand('skip', true); |
|
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
433
diff
changeset
|
190 |
aia_Put: doPut(X, Y, true); |
4 | 191 |
end else |
192 |
begin |
|
193 |
s:= ActionIdToStr[Action]; |
|
194 |
if (Param and ai_specmask) <> 0 then |
|
195 |
case Param of |
|
196 |
aim_push: s:= '+' + s; |
|
197 |
aim_release: s:= '-' + s; |
|
198 |
end |
|
199 |
else if Param <> 0 then s:= s + ' ' + inttostr(Param); |
|
369 | 200 |
ParseCommand(s, true) |
4 | 201 |
end |
202 |
end; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
203 |
inc(Actions.Pos); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
204 |
if Actions.Pos <= Actions.Count then |
194 | 205 |
inc(Actions.actions[Actions.Pos].Time, GameTicks); |
206 |
until false |
|
4 | 207 |
end; |
369 | 208 |
|
4 | 209 |
end. |