author | unc0rr |
Tue, 10 Oct 2006 18:03:48 +0000 | |
changeset 193 | 56885ea29202 |
parent 183 | 57c2ef19f719 |
child 194 | 88652abdce9a |
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 |
|
32 |
aia_Weapon = $80000000; |
|
193 | 33 |
aia_WaitXL = $80000001; |
34 |
aia_WaitXR = $80000002; |
|
4 | 35 |
aia_LookLeft = $80000003; |
36 |
aia_LookRight = $80000004; |
|
71 | 37 |
aia_AwareExpl = $80000005; |
80 | 38 |
aia_HJump = $80000006; |
39 |
aia_LJump = $80000007; |
|
143 | 40 |
aia_Skip = $80000008; |
193 | 41 |
aia_Wait = $80000009; |
4 | 42 |
|
43 |
aim_push = $80000000; |
|
44 |
aim_release = $80000001; |
|
45 |
ai_specmask = $80000000; |
|
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} |
193 | 75 |
const SpecActionIdToStr: array[$80000000..$80000009] 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 |
|
64 | 118 |
if Actions.Pos >= Actions.Count then exit; |
119 |
with Actions.actions[Actions.Pos] do |
|
4 | 120 |
begin |
121 |
if Time > GameTicks then exit; |
|
80 | 122 |
{$IFDEF TRACEAIACTIONS} |
123 |
DumpAction(Actions.actions[Actions.Pos], Me); |
|
124 |
{$ENDIF} |
|
4 | 125 |
if (Action and ai_specmask) <> 0 then |
126 |
case Action of |
|
165 | 127 |
aia_Weapon: SetWeapon(TAmmoType(Param)); |
193 | 128 |
aia_WaitXL: if round(Me.X) = Param then Time:= GameTicks |
129 |
else if Round(Me.X) < Param then |
|
130 |
begin |
|
131 |
OutError('AI: WaitXL assert'); |
|
132 |
Actions.Count:= 0 |
|
133 |
end |
|
134 |
else exit; |
|
135 |
aia_WaitXR: if round(Me.X) = Param then Time:= GameTicks |
|
136 |
else if Round(Me.X) > Param then |
|
137 |
begin |
|
138 |
OutError('AI: WaitXR assert'); |
|
139 |
Actions.Count:= 0 |
|
140 |
end |
|
141 |
else exit; |
|
64 | 142 |
aia_LookLeft: if Me.dX >= 0 then |
143 |
begin |
|
144 |
ParseCommand('+left'); |
|
145 |
exit |
|
146 |
end else ParseCommand('-left'); |
|
147 |
aia_LookRight: if Me.dX < 0 then |
|
148 |
begin |
|
149 |
ParseCommand('+right'); |
|
150 |
exit |
|
151 |
end else ParseCommand('-right'); |
|
71 | 152 |
aia_AwareExpl: AwareOfExplosion(X, Y, Param); |
80 | 153 |
aia_HJump: ParseCommand('hjump'); |
154 |
aia_LJump: ParseCommand('ljump'); |
|
143 | 155 |
aia_Skip: ParseCommand('skip'); |
193 | 156 |
aia_Wait: if Param > GameTicks then exit |
157 |
else with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do |
|
158 |
Gear.State:= Gear.State and not gstHHThinking |
|
4 | 159 |
end else |
160 |
begin |
|
161 |
s:= ActionIdToStr[Action]; |
|
162 |
if (Param and ai_specmask) <> 0 then |
|
163 |
case Param of |
|
164 |
aim_push: s:= '+' + s; |
|
165 |
aim_release: s:= '-' + s; |
|
166 |
end |
|
167 |
else if Param <> 0 then s:= s + ' ' + inttostr(Param); |
|
168 |
ParseCommand(s) |
|
169 |
end |
|
170 |
end; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
171 |
inc(Actions.Pos); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
172 |
if Actions.Pos <= Actions.Count then |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
173 |
inc(Actions.actions[Actions.Pos].Time, GameTicks) |
4 | 174 |
end; |
175 |
||
176 |
end. |