author | unc0rr |
Wed, 07 Dec 2011 22:54:39 +0300 | |
changeset 6516 | addaeb1b9539 |
parent 6474 | 42e9773eedfd |
child 6535 | 51a7e71ad317 |
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; |
|
6393 | 33 |
aia_Switch = 7; |
4 | 34 |
|
195
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
35 |
aia_Weapon = $8000; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
36 |
aia_WaitXL = $8001; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
37 |
aia_WaitXR = $8002; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
38 |
aia_LookLeft = $8003; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
39 |
aia_LookRight = $8004; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
40 |
aia_AwareExpl = $8005; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
41 |
aia_HJump = $8006; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
42 |
aia_LJump = $8007; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
43 |
aia_Skip = $8008; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
44 |
aia_Wait = $8009; |
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
433
diff
changeset
|
45 |
aia_Put = $800A; |
4 | 46 |
|
195
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
47 |
aim_push = $8000; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
48 |
aim_release = $8001; |
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
49 |
ai_specmask = $8000; |
4 | 50 |
|
64 | 51 |
type TAction = record |
371 | 52 |
Action: Longword; |
53 |
X, Y, Param: LongInt; |
|
4 | 54 |
Time: Longword; |
55 |
end; |
|
64 | 56 |
TActions = record |
57 |
Count, Pos: Longword; |
|
58 |
actions: array[0..Pred(MAXACTIONS)] of TAction; |
|
371 | 59 |
Score: LongInt; |
6395
bb04d7a9f7e2
Make AI be less scared by crates. Actually, now it starts using switcher just to pick a crate up.
unc0rr
parents:
6393
diff
changeset
|
60 |
isWalkingToABetterPlace: boolean; |
64 | 61 |
end; |
4 | 62 |
|
371 | 63 |
procedure AddAction(var Actions: TActions; Action: Longword; Param: LongInt; TimeDelta: Longword; X, Y: LongInt); |
64 | 64 |
procedure ProcessAction(var Actions: TActions; Me: PGear); |
369 | 65 |
|
4 | 66 |
implementation |
4414 | 67 |
uses uAIMisc, uAI, uAmmos, uVariables, uCommands, uUtils, uDebug, uIO; |
4 | 68 |
|
6393 | 69 |
const ActionIdToStr: array[0..7] of string[16] = ( |
4 | 70 |
{aia_none} '', |
71 |
{aia_Left} 'left', |
|
72 |
{aia_Right} 'right', |
|
73 |
{aia_Timer} 'timer', |
|
74 |
{aia_attack} 'attack', |
|
75 |
{aia_Up} 'up', |
|
6393 | 76 |
{aia_Down} 'down', |
77 |
{aia_Switch} 'switch' |
|
64 | 78 |
); |
4 | 79 |
|
80 | 80 |
{$IFDEF TRACEAIACTIONS} |
195
edae828322a0
Fixed compilation with freepascal version prior to 1.9.6
unc0rr
parents:
194
diff
changeset
|
81 |
const SpecActionIdToStr: array[$8000..$8009] of string[16] = ( |
80 | 82 |
{aia_Weapon} 'aia_Weapon', |
83 |
{aia_WaitX} 'aia_WaitX', |
|
84 |
{aia_WaitY} 'aia_WaitY', |
|
85 |
{aia_LookLeft} 'aia_LookLeft', |
|
86 |
{aia_LookRight} 'aia_LookRight', |
|
87 |
{aia_AwareExpl} 'aia_AwareExpl', |
|
88 |
{aia_HJump} 'aia_HJump', |
|
144 | 89 |
{aia_LJump} 'aia_LJump', |
193 | 90 |
{aia_Skip} 'aia_Skip', |
91 |
{aia_Wait} 'aia_Wait' |
|
80 | 92 |
); |
93 |
||
94 |
procedure DumpAction(Action: TAction; Me: PGear); |
|
95 |
begin |
|
96 |
if (Action.Action and ai_specmask) = 0 then |
|
97 |
WriteLnToConsole('AI action: '+ActionIdToStr[Action.Action]) |
|
98 |
else begin |
|
99 |
WriteLnToConsole('AI action: '+SpecActionIdToStr[Action.Action]); |
|
193 | 100 |
if (Action.Action = aia_WaitXL) or (Action.Action = aia_WaitXR) then |
4374 | 101 |
WriteLnToConsole('AI action Wait X = '+IntToStr(Action.Param)+', current X = '+IntToStr(hwRound(Me^.X))) |
102 |
else if (Action.Action = aia_AwareExpl) then WriteLnToConsole('Aware X = ' + IntToStr(Action.X) + ', Y = ' + IntToStr(Action.Y)); |
|
80 | 103 |
end |
104 |
end; |
|
105 |
{$ENDIF} |
|
106 |
||
371 | 107 |
procedure AddAction(var Actions: TActions; Action: Longword; Param: LongInt; TimeDelta: Longword; X, Y: LongInt); |
4 | 108 |
begin |
64 | 109 |
with Actions do |
110 |
begin |
|
111 |
actions[Count].Action:= Action; |
|
112 |
actions[Count].Param:= Param; |
|
71 | 113 |
actions[Count].X:= X; |
114 |
actions[Count].Y:= Y; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
115 |
if Count > 0 then actions[Count].Time:= TimeDelta |
64 | 116 |
else actions[Count].Time:= GameTicks + TimeDelta; |
117 |
inc(Count); |
|
118 |
TryDo(Count < MAXACTIONS, 'AI: actions overflow', true); |
|
119 |
end |
|
4 | 120 |
end; |
121 |
||
6474
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
122 |
procedure CheckHang(Me: PGear); |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
123 |
const PrevX: LongInt = 0; |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
124 |
timedelta: Longword = 0; |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
125 |
begin |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
126 |
if hwRound(Me^.X) <> PrevX then |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
127 |
begin |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
128 |
PrevX:= hwRound(Me^.X); |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
129 |
timedelta:= 0 |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
130 |
end else |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
131 |
begin |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
132 |
inc(timedelta); |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
133 |
if timedelta > 1700 then |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
134 |
begin |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
135 |
timedelta:= 0; |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
136 |
FreeActionsList |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
137 |
end |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
138 |
end |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
139 |
end; |
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
140 |
|
64 | 141 |
procedure ProcessAction(var Actions: TActions; Me: PGear); |
4 | 142 |
var s: shortstring; |
143 |
begin |
|
194 | 144 |
repeat |
64 | 145 |
if Actions.Pos >= Actions.Count then exit; |
146 |
with Actions.actions[Actions.Pos] do |
|
4 | 147 |
begin |
148 |
if Time > GameTicks then exit; |
|
80 | 149 |
{$IFDEF TRACEAIACTIONS} |
150 |
DumpAction(Actions.actions[Actions.Pos], Me); |
|
151 |
{$ENDIF} |
|
4 | 152 |
if (Action and ai_specmask) <> 0 then |
153 |
case Action of |
|
165 | 154 |
aia_Weapon: SetWeapon(TAmmoType(Param)); |
544 | 155 |
aia_WaitXL: if hwRound(Me^.X) = Param then |
156 |
begin |
|
157 |
Action:= aia_LookLeft; |
|
158 |
Time:= GameTicks; |
|
159 |
exit |
|
160 |
end |
|
369 | 161 |
else if hwRound(Me^.X) < Param then |
193 | 162 |
begin |
5419 | 163 |
//OutError('AI: WaitXL assert (' + IntToStr(hwRound(Me^.X)) + ' < ' + IntToStr(Param) + ')', false); |
433 | 164 |
FreeActionsList; |
165 |
exit |
|
193 | 166 |
end |
6474
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
167 |
else begin CheckHang(Me); exit end; |
544 | 168 |
aia_WaitXR: if hwRound(Me^.X) = Param then |
169 |
begin |
|
170 |
Action:= aia_LookRight; |
|
171 |
Time:= GameTicks; |
|
172 |
exit |
|
173 |
end |
|
369 | 174 |
else if hwRound(Me^.X) > Param then |
193 | 175 |
begin |
5419 | 176 |
//OutError('AI: WaitXR assert (' + IntToStr(hwRound(Me^.X)) + ' > ' + IntToStr(Param) + ')', false); |
433 | 177 |
FreeActionsList; |
178 |
exit |
|
193 | 179 |
end |
6474
42e9773eedfd
- Improve renderer a bit, disallow nested functions
unc0rr
parents:
6395
diff
changeset
|
180 |
else begin CheckHang(Me); exit end; |
369 | 181 |
aia_LookLeft: if not Me^.dX.isNegative then |
64 | 182 |
begin |
369 | 183 |
ParseCommand('+left', true); |
64 | 184 |
exit |
369 | 185 |
end else ParseCommand('-left', true); |
186 |
aia_LookRight: if Me^.dX.isNegative then |
|
64 | 187 |
begin |
369 | 188 |
ParseCommand('+right', true); |
64 | 189 |
exit |
369 | 190 |
end else ParseCommand('-right', true); |
71 | 191 |
aia_AwareExpl: AwareOfExplosion(X, Y, Param); |
369 | 192 |
aia_HJump: ParseCommand('hjump', true); |
193 |
aia_LJump: ParseCommand('ljump', true); |
|
194 |
aia_Skip: ParseCommand('skip', true); |
|
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
433
diff
changeset
|
195 |
aia_Put: doPut(X, Y, true); |
4 | 196 |
end else |
197 |
begin |
|
198 |
s:= ActionIdToStr[Action]; |
|
199 |
if (Param and ai_specmask) <> 0 then |
|
200 |
case Param of |
|
201 |
aim_push: s:= '+' + s; |
|
202 |
aim_release: s:= '-' + s; |
|
203 |
end |
|
4374 | 204 |
else if Param <> 0 then s:= s + ' ' + IntToStr(Param); |
369 | 205 |
ParseCommand(s, true) |
4 | 206 |
end |
207 |
end; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
208 |
inc(Actions.Pos); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
209 |
if Actions.Pos <= Actions.Count then |
194 | 210 |
inc(Actions.actions[Actions.Pos].Time, GameTicks); |
211 |
until false |
|
4 | 212 |
end; |
369 | 213 |
|
4 | 214 |
end. |