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