author | unc0rr |
Mon, 07 May 2007 20:21:26 +0000 | |
changeset 509 | fd58135a4407 |
parent 433 | 9f8f22094c0e |
child 543 | 465e2ec8f05f |
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; |
4 | 42 |
|
195
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
43 |
aim_push = $8000; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
44 |
aim_release = $8001; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
45 |
ai_specmask = $8000; |
4 | 46 |
|
64 | 47 |
type TAction = record |
371 | 48 |
Action: Longword; |
49 |
X, Y, Param: LongInt; |
|
4 | 50 |
Time: Longword; |
51 |
end; |
|
64 | 52 |
TActions = record |
53 |
Count, Pos: Longword; |
|
54 |
actions: array[0..Pred(MAXACTIONS)] of TAction; |
|
371 | 55 |
Score: LongInt; |
64 | 56 |
end; |
4 | 57 |
|
371 | 58 |
procedure AddAction(var Actions: TActions; Action: Longword; Param: LongInt; TimeDelta: Longword; X, Y: LongInt); |
64 | 59 |
procedure ProcessAction(var Actions: TActions; Me: PGear); |
369 | 60 |
|
4 | 61 |
implementation |
433 | 62 |
uses uMisc, uTeams, uConsts, uConsole, uAIMisc, uAI; |
4 | 63 |
|
70 | 64 |
const ActionIdToStr: array[0..6] of string[16] = ( |
4 | 65 |
{aia_none} '', |
66 |
{aia_Left} 'left', |
|
67 |
{aia_Right} 'right', |
|
68 |
{aia_Timer} 'timer', |
|
69 |
{aia_attack} 'attack', |
|
70 |
{aia_Up} 'up', |
|
71 |
{aia_Down} 'down' |
|
64 | 72 |
); |
4 | 73 |
|
80 | 74 |
{$IFDEF TRACEAIACTIONS} |
195
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
75 |
const SpecActionIdToStr: array[$8000..$8009] of string[16] = ( |
80 | 76 |
{aia_Weapon} 'aia_Weapon', |
77 |
{aia_WaitX} 'aia_WaitX', |
|
78 |
{aia_WaitY} 'aia_WaitY', |
|
79 |
{aia_LookLeft} 'aia_LookLeft', |
|
80 |
{aia_LookRight} 'aia_LookRight', |
|
81 |
{aia_AwareExpl} 'aia_AwareExpl', |
|
82 |
{aia_HJump} 'aia_HJump', |
|
144 | 83 |
{aia_LJump} 'aia_LJump', |
193 | 84 |
{aia_Skip} 'aia_Skip', |
85 |
{aia_Wait} 'aia_Wait' |
|
80 | 86 |
); |
87 |
||
88 |
procedure DumpAction(Action: TAction; Me: PGear); |
|
89 |
begin |
|
90 |
if (Action.Action and ai_specmask) = 0 then |
|
91 |
WriteLnToConsole('AI action: '+ActionIdToStr[Action.Action]) |
|
92 |
else begin |
|
93 |
WriteLnToConsole('AI action: '+SpecActionIdToStr[Action.Action]); |
|
193 | 94 |
if (Action.Action = aia_WaitXL) or (Action.Action = aia_WaitXR) then |
80 | 95 |
WriteLnToConsole('AI action Wait X = '+inttostr(Action.Param)+', current X = '+inttostr(round(Me.X))); |
96 |
end |
|
97 |
end; |
|
98 |
{$ENDIF} |
|
99 |
||
371 | 100 |
procedure AddAction(var Actions: TActions; Action: Longword; Param: LongInt; TimeDelta: Longword; X, Y: LongInt); |
4 | 101 |
begin |
64 | 102 |
with Actions do |
103 |
begin |
|
104 |
actions[Count].Action:= Action; |
|
105 |
actions[Count].Param:= Param; |
|
71 | 106 |
actions[Count].X:= X; |
107 |
actions[Count].Y:= Y; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
108 |
if Count > 0 then actions[Count].Time:= TimeDelta |
64 | 109 |
else actions[Count].Time:= GameTicks + TimeDelta; |
110 |
inc(Count); |
|
111 |
TryDo(Count < MAXACTIONS, 'AI: actions overflow', true); |
|
112 |
end |
|
4 | 113 |
end; |
114 |
||
64 | 115 |
procedure ProcessAction(var Actions: TActions; Me: PGear); |
4 | 116 |
var s: shortstring; |
280
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
117 |
|
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
118 |
procedure CheckHang; |
371 | 119 |
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
|
120 |
timedelta: Longword = 0; |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
121 |
begin |
369 | 122 |
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
|
123 |
begin |
369 | 124 |
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
|
125 |
timedelta:= 0 |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
126 |
end else |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
127 |
begin |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
128 |
inc(timedelta); |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
129 |
if timedelta > 2500 then |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
130 |
begin |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
131 |
timedelta:= 0; |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
132 |
Actions.Count:= 0 |
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
133 |
end |
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 |
|
4 | 137 |
begin |
194 | 138 |
repeat |
64 | 139 |
if Actions.Pos >= Actions.Count then exit; |
140 |
with Actions.actions[Actions.Pos] do |
|
4 | 141 |
begin |
142 |
if Time > GameTicks then exit; |
|
80 | 143 |
{$IFDEF TRACEAIACTIONS} |
144 |
DumpAction(Actions.actions[Actions.Pos], Me); |
|
145 |
{$ENDIF} |
|
4 | 146 |
if (Action and ai_specmask) <> 0 then |
147 |
case Action of |
|
165 | 148 |
aia_Weapon: SetWeapon(TAmmoType(Param)); |
369 | 149 |
aia_WaitXL: if hwRound(Me^.X) = Param then Time:= GameTicks |
150 |
else if hwRound(Me^.X) < Param then |
|
193 | 151 |
begin |
369 | 152 |
OutError('AI: WaitXL assert', false); |
433 | 153 |
FreeActionsList; |
154 |
exit |
|
193 | 155 |
end |
280
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
156 |
else begin CheckHang; exit end; |
369 | 157 |
aia_WaitXR: if hwRound(Me^.X) = Param then Time:= GameTicks |
158 |
else if hwRound(Me^.X) > Param then |
|
193 | 159 |
begin |
369 | 160 |
OutError('AI: WaitXR assert', false); |
433 | 161 |
FreeActionsList; |
162 |
exit |
|
193 | 163 |
end |
280
4d6e365befc1
Watchdog for bots to prevent them from being stacked by bugs of engine physics
unc0rr
parents:
195
diff
changeset
|
164 |
else begin CheckHang; exit end; |
369 | 165 |
aia_LookLeft: if not Me^.dX.isNegative then |
64 | 166 |
begin |
369 | 167 |
ParseCommand('+left', true); |
64 | 168 |
exit |
369 | 169 |
end else ParseCommand('-left', true); |
170 |
aia_LookRight: if Me^.dX.isNegative then |
|
64 | 171 |
begin |
369 | 172 |
ParseCommand('+right', true); |
64 | 173 |
exit |
369 | 174 |
end else ParseCommand('-right', true); |
71 | 175 |
aia_AwareExpl: AwareOfExplosion(X, Y, Param); |
369 | 176 |
aia_HJump: ParseCommand('hjump', true); |
177 |
aia_LJump: ParseCommand('ljump', true); |
|
178 |
aia_Skip: ParseCommand('skip', true); |
|
4 | 179 |
end else |
180 |
begin |
|
181 |
s:= ActionIdToStr[Action]; |
|
182 |
if (Param and ai_specmask) <> 0 then |
|
183 |
case Param of |
|
184 |
aim_push: s:= '+' + s; |
|
185 |
aim_release: s:= '-' + s; |
|
186 |
end |
|
187 |
else if Param <> 0 then s:= s + ' ' + inttostr(Param); |
|
369 | 188 |
ParseCommand(s, true) |
4 | 189 |
end |
190 |
end; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
191 |
inc(Actions.Pos); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
192 |
if Actions.Pos <= Actions.Count then |
194 | 193 |
inc(Actions.actions[Actions.Pos].Time, GameTicks); |
194 |
until false |
|
4 | 195 |
end; |
369 | 196 |
|
4 | 197 |
end. |