author | unc0rr |
Tue, 14 Nov 2006 20:39:30 +0000 | |
changeset 239 | f9adf3c73bed |
parent 195 | edae828322a0 |
child 280 | 4d6e365befc1 |
permissions | -rw-r--r-- |
71 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com> |
|
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 |
|
64 | 21 |
uses uGears; |
80 | 22 |
{$INCLUDE options.inc} |
75 | 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 |
4 | 48 |
Action, Param: Longword; |
71 | 49 |
X, Y: integer; |
4 | 50 |
Time: Longword; |
51 |
end; |
|
64 | 52 |
TActions = record |
53 |
Count, Pos: Longword; |
|
54 |
actions: array[0..Pred(MAXACTIONS)] of TAction; |
|
55 |
Score: integer; |
|
56 |
end; |
|
4 | 57 |
|
71 | 58 |
procedure AddAction(var Actions: TActions; Action, Param, TimeDelta: Longword; const X: integer = 0; Y: integer = 0); |
64 | 59 |
procedure ProcessAction(var Actions: TActions; Me: PGear); |
4 | 60 |
|
61 |
implementation |
|
71 | 62 |
uses uMisc, uTeams, uConsts, uConsole, uAIMisc; |
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 |
||
71 | 100 |
procedure AddAction(var Actions: TActions; Action, Param, TimeDelta: Longword; const X: integer = 0; Y: integer = 0); |
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; |
117 |
begin |
|
194 | 118 |
repeat |
64 | 119 |
if Actions.Pos >= Actions.Count then exit; |
120 |
with Actions.actions[Actions.Pos] do |
|
4 | 121 |
begin |
122 |
if Time > GameTicks then exit; |
|
80 | 123 |
{$IFDEF TRACEAIACTIONS} |
124 |
DumpAction(Actions.actions[Actions.Pos], Me); |
|
125 |
{$ENDIF} |
|
4 | 126 |
if (Action and ai_specmask) <> 0 then |
127 |
case Action of |
|
165 | 128 |
aia_Weapon: SetWeapon(TAmmoType(Param)); |
193 | 129 |
aia_WaitXL: if round(Me.X) = Param then Time:= GameTicks |
130 |
else if Round(Me.X) < Param then |
|
131 |
begin |
|
132 |
OutError('AI: WaitXL assert'); |
|
133 |
Actions.Count:= 0 |
|
134 |
end |
|
135 |
else exit; |
|
136 |
aia_WaitXR: if round(Me.X) = Param then Time:= GameTicks |
|
137 |
else if Round(Me.X) > Param then |
|
138 |
begin |
|
139 |
OutError('AI: WaitXR assert'); |
|
140 |
Actions.Count:= 0 |
|
141 |
end |
|
142 |
else exit; |
|
64 | 143 |
aia_LookLeft: if Me.dX >= 0 then |
144 |
begin |
|
145 |
ParseCommand('+left'); |
|
146 |
exit |
|
147 |
end else ParseCommand('-left'); |
|
148 |
aia_LookRight: if Me.dX < 0 then |
|
149 |
begin |
|
150 |
ParseCommand('+right'); |
|
151 |
exit |
|
152 |
end else ParseCommand('-right'); |
|
71 | 153 |
aia_AwareExpl: AwareOfExplosion(X, Y, Param); |
80 | 154 |
aia_HJump: ParseCommand('hjump'); |
155 |
aia_LJump: ParseCommand('ljump'); |
|
143 | 156 |
aia_Skip: ParseCommand('skip'); |
4 | 157 |
end else |
158 |
begin |
|
159 |
s:= ActionIdToStr[Action]; |
|
160 |
if (Param and ai_specmask) <> 0 then |
|
161 |
case Param of |
|
162 |
aim_push: s:= '+' + s; |
|
163 |
aim_release: s:= '-' + s; |
|
164 |
end |
|
165 |
else if Param <> 0 then s:= s + ' ' + inttostr(Param); |
|
166 |
ParseCommand(s) |
|
167 |
end |
|
168 |
end; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
169 |
inc(Actions.Pos); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
170 |
if Actions.Pos <= Actions.Count then |
194 | 171 |
inc(Actions.actions[Actions.Pos].Time, GameTicks); |
172 |
until false |
|
4 | 173 |
end; |
174 |
||
175 |
end. |