author | unc0rr |
Thu, 05 Oct 2006 16:33:18 +0000 | |
changeset 183 | 57c2ef19f719 |
parent 165 | 9b9144948668 |
child 193 | 56885ea29202 |
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; |
|
33 |
aia_WaitX = $80000001; |
|
34 |
aia_WaitY = $80000002; |
|
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; |
4 | 41 |
|
42 |
aim_push = $80000000; |
|
43 |
aim_release = $80000001; |
|
44 |
ai_specmask = $80000000; |
|
45 |
||
64 | 46 |
type TAction = record |
4 | 47 |
Action, Param: Longword; |
71 | 48 |
X, Y: integer; |
4 | 49 |
Time: Longword; |
50 |
end; |
|
64 | 51 |
TActions = record |
52 |
Count, Pos: Longword; |
|
53 |
actions: array[0..Pred(MAXACTIONS)] of TAction; |
|
54 |
Score: integer; |
|
55 |
end; |
|
4 | 56 |
|
71 | 57 |
procedure AddAction(var Actions: TActions; Action, Param, TimeDelta: Longword; const X: integer = 0; Y: integer = 0); |
64 | 58 |
procedure ProcessAction(var Actions: TActions; Me: PGear); |
4 | 59 |
|
60 |
implementation |
|
71 | 61 |
uses uMisc, uTeams, uConsts, uConsole, uAIMisc; |
4 | 62 |
|
70 | 63 |
const ActionIdToStr: array[0..6] of string[16] = ( |
4 | 64 |
{aia_none} '', |
65 |
{aia_Left} 'left', |
|
66 |
{aia_Right} 'right', |
|
67 |
{aia_Timer} 'timer', |
|
68 |
{aia_attack} 'attack', |
|
69 |
{aia_Up} 'up', |
|
70 |
{aia_Down} 'down' |
|
64 | 71 |
); |
4 | 72 |
|
80 | 73 |
{$IFDEF TRACEAIACTIONS} |
144 | 74 |
const SpecActionIdToStr: array[$80000000..$80000008] of string[16] = ( |
80 | 75 |
{aia_Weapon} 'aia_Weapon', |
76 |
{aia_WaitX} 'aia_WaitX', |
|
77 |
{aia_WaitY} 'aia_WaitY', |
|
78 |
{aia_LookLeft} 'aia_LookLeft', |
|
79 |
{aia_LookRight} 'aia_LookRight', |
|
80 |
{aia_AwareExpl} 'aia_AwareExpl', |
|
81 |
{aia_HJump} 'aia_HJump', |
|
144 | 82 |
{aia_LJump} 'aia_LJump', |
83 |
{aia_Skip} 'aia_Skip' |
|
80 | 84 |
); |
85 |
||
86 |
procedure DumpAction(Action: TAction; Me: PGear); |
|
87 |
begin |
|
88 |
if (Action.Action and ai_specmask) = 0 then |
|
89 |
WriteLnToConsole('AI action: '+ActionIdToStr[Action.Action]) |
|
90 |
else begin |
|
91 |
WriteLnToConsole('AI action: '+SpecActionIdToStr[Action.Action]); |
|
92 |
if Action.Action = aia_WaitX then |
|
93 |
WriteLnToConsole('AI action Wait X = '+inttostr(Action.Param)+', current X = '+inttostr(round(Me.X))); |
|
94 |
end |
|
95 |
end; |
|
96 |
{$ENDIF} |
|
97 |
||
71 | 98 |
procedure AddAction(var Actions: TActions; Action, Param, TimeDelta: Longword; const X: integer = 0; Y: integer = 0); |
4 | 99 |
begin |
64 | 100 |
with Actions do |
101 |
begin |
|
102 |
actions[Count].Action:= Action; |
|
103 |
actions[Count].Param:= Param; |
|
71 | 104 |
actions[Count].X:= X; |
105 |
actions[Count].Y:= Y; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
106 |
if Count > 0 then actions[Count].Time:= TimeDelta |
64 | 107 |
else actions[Count].Time:= GameTicks + TimeDelta; |
108 |
inc(Count); |
|
109 |
TryDo(Count < MAXACTIONS, 'AI: actions overflow', true); |
|
110 |
end |
|
4 | 111 |
end; |
112 |
||
64 | 113 |
procedure ProcessAction(var Actions: TActions; Me: PGear); |
4 | 114 |
var s: shortstring; |
115 |
begin |
|
64 | 116 |
if Actions.Pos >= Actions.Count then exit; |
117 |
with Actions.actions[Actions.Pos] do |
|
4 | 118 |
begin |
119 |
if Time > GameTicks then exit; |
|
80 | 120 |
{$IFDEF TRACEAIACTIONS} |
121 |
DumpAction(Actions.actions[Actions.Pos], Me); |
|
122 |
{$ENDIF} |
|
4 | 123 |
if (Action and ai_specmask) <> 0 then |
124 |
case Action of |
|
165 | 125 |
aia_Weapon: SetWeapon(TAmmoType(Param)); |
64 | 126 |
aia_WaitX: if round(Me.X) = Param then Time:= GameTicks |
127 |
else exit; |
|
128 |
aia_WaitY: if round(Me.Y) = Param then Time:= GameTicks |
|
129 |
else exit; |
|
130 |
aia_LookLeft: if Me.dX >= 0 then |
|
131 |
begin |
|
132 |
ParseCommand('+left'); |
|
133 |
exit |
|
134 |
end else ParseCommand('-left'); |
|
135 |
aia_LookRight: if Me.dX < 0 then |
|
136 |
begin |
|
137 |
ParseCommand('+right'); |
|
138 |
exit |
|
139 |
end else ParseCommand('-right'); |
|
71 | 140 |
aia_AwareExpl: AwareOfExplosion(X, Y, Param); |
80 | 141 |
aia_HJump: ParseCommand('hjump'); |
142 |
aia_LJump: ParseCommand('ljump'); |
|
143 | 143 |
aia_Skip: ParseCommand('skip'); |
4 | 144 |
end else |
145 |
begin |
|
146 |
s:= ActionIdToStr[Action]; |
|
147 |
if (Param and ai_specmask) <> 0 then |
|
148 |
case Param of |
|
149 |
aim_push: s:= '+' + s; |
|
150 |
aim_release: s:= '-' + s; |
|
151 |
end |
|
152 |
else if Param <> 0 then s:= s + ' ' + inttostr(Param); |
|
153 |
ParseCommand(s) |
|
154 |
end |
|
155 |
end; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
156 |
inc(Actions.Pos); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
157 |
if Actions.Pos <= Actions.Count then |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
158 |
inc(Actions.actions[Actions.Pos].Time, GameTicks) |
4 | 159 |
end; |
160 |
||
161 |
end. |