author | unc0rr |
Thu, 21 Sep 2006 21:29:02 +0000 | |
changeset 159 | 63909aecb0ed |
parent 144 | e6084b0c9316 |
child 165 | 9b9144948668 |
permissions | -rw-r--r-- |
71 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* Distributed under the terms of the BSD-modified licence: |
|
6 |
* |
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 |
* of this software and associated documentation files (the "Software"), to deal |
|
9 |
* with the Software without restriction, including without limitation the |
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is |
|
12 |
* furnished to do so, subject to the following conditions: |
|
13 |
* |
|
14 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
15 |
* this list of conditions and the following disclaimer. |
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
17 |
* this list of conditions and the following disclaimer in the documentation |
|
18 |
* and/or other materials provided with the distribution. |
|
19 |
* 3. The name of the author may not be used to endorse or promote products |
|
20 |
* derived from this software without specific prior written permission. |
|
21 |
* |
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 |
*) |
|
33 |
||
4 | 34 |
unit uAIActions; |
35 |
interface |
|
64 | 36 |
uses uGears; |
80 | 37 |
{$INCLUDE options.inc} |
75 | 38 |
const MAXACTIONS = 96; |
64 | 39 |
aia_none = 0; |
4 | 40 |
aia_Left = 1; |
41 |
aia_Right = 2; |
|
42 |
aia_Timer = 3; |
|
70 | 43 |
aia_attack = 4; |
44 |
aia_Up = 5; |
|
45 |
aia_Down = 6; |
|
4 | 46 |
|
47 |
aia_Weapon = $80000000; |
|
48 |
aia_WaitX = $80000001; |
|
49 |
aia_WaitY = $80000002; |
|
50 |
aia_LookLeft = $80000003; |
|
51 |
aia_LookRight = $80000004; |
|
71 | 52 |
aia_AwareExpl = $80000005; |
80 | 53 |
aia_HJump = $80000006; |
54 |
aia_LJump = $80000007; |
|
143 | 55 |
aia_Skip = $80000008; |
4 | 56 |
|
57 |
aim_push = $80000000; |
|
58 |
aim_release = $80000001; |
|
59 |
ai_specmask = $80000000; |
|
60 |
||
64 | 61 |
type TAction = record |
4 | 62 |
Action, Param: Longword; |
71 | 63 |
X, Y: integer; |
4 | 64 |
Time: Longword; |
65 |
end; |
|
64 | 66 |
TActions = record |
67 |
Count, Pos: Longword; |
|
68 |
actions: array[0..Pred(MAXACTIONS)] of TAction; |
|
69 |
Score: integer; |
|
70 |
end; |
|
4 | 71 |
|
71 | 72 |
procedure AddAction(var Actions: TActions; Action, Param, TimeDelta: Longword; const X: integer = 0; Y: integer = 0); |
64 | 73 |
procedure ProcessAction(var Actions: TActions; Me: PGear); |
4 | 74 |
|
75 |
implementation |
|
71 | 76 |
uses uMisc, uTeams, uConsts, uConsole, uAIMisc; |
4 | 77 |
|
70 | 78 |
const ActionIdToStr: array[0..6] of string[16] = ( |
4 | 79 |
{aia_none} '', |
80 |
{aia_Left} 'left', |
|
81 |
{aia_Right} 'right', |
|
82 |
{aia_Timer} 'timer', |
|
83 |
{aia_attack} 'attack', |
|
84 |
{aia_Up} 'up', |
|
85 |
{aia_Down} 'down' |
|
64 | 86 |
); |
4 | 87 |
|
80 | 88 |
{$IFDEF TRACEAIACTIONS} |
144 | 89 |
const SpecActionIdToStr: array[$80000000..$80000008] of string[16] = ( |
80 | 90 |
{aia_Weapon} 'aia_Weapon', |
91 |
{aia_WaitX} 'aia_WaitX', |
|
92 |
{aia_WaitY} 'aia_WaitY', |
|
93 |
{aia_LookLeft} 'aia_LookLeft', |
|
94 |
{aia_LookRight} 'aia_LookRight', |
|
95 |
{aia_AwareExpl} 'aia_AwareExpl', |
|
96 |
{aia_HJump} 'aia_HJump', |
|
144 | 97 |
{aia_LJump} 'aia_LJump', |
98 |
{aia_Skip} 'aia_Skip' |
|
80 | 99 |
); |
100 |
||
101 |
procedure DumpAction(Action: TAction; Me: PGear); |
|
102 |
begin |
|
103 |
if (Action.Action and ai_specmask) = 0 then |
|
104 |
WriteLnToConsole('AI action: '+ActionIdToStr[Action.Action]) |
|
105 |
else begin |
|
106 |
WriteLnToConsole('AI action: '+SpecActionIdToStr[Action.Action]); |
|
107 |
if Action.Action = aia_WaitX then |
|
108 |
WriteLnToConsole('AI action Wait X = '+inttostr(Action.Param)+', current X = '+inttostr(round(Me.X))); |
|
109 |
end |
|
110 |
end; |
|
111 |
{$ENDIF} |
|
112 |
||
71 | 113 |
procedure AddAction(var Actions: TActions; Action, Param, TimeDelta: Longword; const X: integer = 0; Y: integer = 0); |
4 | 114 |
begin |
64 | 115 |
with Actions do |
116 |
begin |
|
117 |
actions[Count].Action:= Action; |
|
118 |
actions[Count].Param:= Param; |
|
71 | 119 |
actions[Count].X:= X; |
120 |
actions[Count].Y:= Y; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
121 |
if Count > 0 then actions[Count].Time:= TimeDelta |
64 | 122 |
else actions[Count].Time:= GameTicks + TimeDelta; |
123 |
inc(Count); |
|
124 |
TryDo(Count < MAXACTIONS, 'AI: actions overflow', true); |
|
125 |
end |
|
4 | 126 |
end; |
127 |
||
128 |
procedure SetWeapon(weap: Longword); |
|
129 |
begin |
|
130 |
with CurrentTeam^ do |
|
131 |
with Hedgehogs[CurrHedgehog] do |
|
70 | 132 |
while Ammo[CurSlot, CurAmmo].AmmoType <> TAmmoType(weap) do |
4 | 133 |
ParseCommand('/slot ' + chr(49 + Ammoz[TAmmoType(weap)].Slot)); |
134 |
end; |
|
135 |
||
64 | 136 |
procedure ProcessAction(var Actions: TActions; Me: PGear); |
4 | 137 |
var s: shortstring; |
138 |
begin |
|
64 | 139 |
if Actions.Pos >= Actions.Count then exit; |
140 |
with Actions.actions[Actions.Pos] do |
|
4 | 141 |
begin |
142 |
if Time > GameTicks then exit; |
|
80 | 143 |
{$IFDEF TRACEAIACTIONS} |
144 |
DumpAction(Actions.actions[Actions.Pos], Me); |
|
145 |
{$ENDIF} |
|
4 | 146 |
if (Action and ai_specmask) <> 0 then |
147 |
case Action of |
|
148 |
aia_Weapon: SetWeapon(Param); |
|
64 | 149 |
aia_WaitX: if round(Me.X) = Param then Time:= GameTicks |
150 |
else exit; |
|
151 |
aia_WaitY: if round(Me.Y) = Param then Time:= GameTicks |
|
152 |
else exit; |
|
153 |
aia_LookLeft: if Me.dX >= 0 then |
|
154 |
begin |
|
155 |
ParseCommand('+left'); |
|
156 |
exit |
|
157 |
end else ParseCommand('-left'); |
|
158 |
aia_LookRight: if Me.dX < 0 then |
|
159 |
begin |
|
160 |
ParseCommand('+right'); |
|
161 |
exit |
|
162 |
end else ParseCommand('-right'); |
|
71 | 163 |
aia_AwareExpl: AwareOfExplosion(X, Y, Param); |
80 | 164 |
aia_HJump: ParseCommand('hjump'); |
165 |
aia_LJump: ParseCommand('ljump'); |
|
143 | 166 |
aia_Skip: ParseCommand('skip'); |
4 | 167 |
end else |
168 |
begin |
|
169 |
s:= ActionIdToStr[Action]; |
|
170 |
if (Param and ai_specmask) <> 0 then |
|
171 |
case Param of |
|
172 |
aim_push: s:= '+' + s; |
|
173 |
aim_release: s:= '-' + s; |
|
174 |
end |
|
175 |
else if Param <> 0 then s:= s + ' ' + inttostr(Param); |
|
176 |
ParseCommand(s) |
|
177 |
end |
|
178 |
end; |
|
66
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
179 |
inc(Actions.Pos); |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
180 |
if Actions.Pos <= Actions.Count then |
9643d75baf1e
Many AI improvements, bots do think in separate thread
unc0rr
parents:
64
diff
changeset
|
181 |
inc(Actions.actions[Actions.Pos].Time, GameTicks) |
4 | 182 |
end; |
183 |
||
184 |
end. |