|
1 (* |
|
2 * Hedgewars, a worms-like game |
|
3 * Copyright (c) 2006 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 * |
|
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 |
|
8 * |
|
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. |
|
13 * |
|
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 |
|
17 *) |
|
18 |
|
19 unit uAIThinkStack; |
|
20 interface |
|
21 uses uAIActions, uGears; |
|
22 {$INCLUDE options.inc} |
|
23 const cBranchStackSize = 12; |
|
24 type TStackEntry = record |
|
25 WastedTicks: Longword; |
|
26 MadeActions: TActions; |
|
27 Hedgehog: TGear; |
|
28 end; |
|
29 |
|
30 var ThinkStack: record |
|
31 Count: Longword; |
|
32 States: array[0..Pred(cBranchStackSize)] of TStackEntry; |
|
33 end; |
|
34 |
|
35 function Push(Ticks: Longword; const Actions: TActions; const Me: TGear; Dir: integer): boolean; |
|
36 function Pop(out Ticks: Longword; out Actions: TActions; out Me: TGear): boolean; |
|
37 function PosInThinkStack(Me: PGear): boolean; |
|
38 procedure ClearThinkStack; |
|
39 |
|
40 implementation |
|
41 |
|
42 function Push(Ticks: Longword; const Actions: TActions; const Me: TGear; Dir: integer): boolean; |
|
43 begin |
|
44 Result:= (ThinkStack.Count < cBranchStackSize) and (Actions.Count < MAXACTIONS - 5); |
|
45 if Result then |
|
46 with ThinkStack.States[ThinkStack.Count] do |
|
47 begin |
|
48 WastedTicks:= Ticks; |
|
49 MadeActions:= Actions; |
|
50 Hedgehog:= Me; |
|
51 Hedgehog.Message:= Dir; |
|
52 inc(ThinkStack.Count) |
|
53 end |
|
54 end; |
|
55 |
|
56 function Pop(out Ticks: Longword; out Actions: TActions; out Me: TGear): boolean; |
|
57 begin |
|
58 Result:= ThinkStack.Count > 0; |
|
59 if Result then |
|
60 begin |
|
61 dec(ThinkStack.Count); |
|
62 with ThinkStack.States[ThinkStack.Count] do |
|
63 begin |
|
64 Ticks:= WastedTicks; |
|
65 Actions:= MadeActions; |
|
66 Me:= Hedgehog |
|
67 end |
|
68 end |
|
69 end; |
|
70 |
|
71 function PosInThinkStack(Me: PGear): boolean; |
|
72 var i: Longword; |
|
73 begin |
|
74 i:= 0; |
|
75 Result:= false; |
|
76 while (i < ThinkStack.Count) and not Result do |
|
77 begin |
|
78 Result:= (abs(ThinkStack.States[i].Hedgehog.X - Me.X) + |
|
79 abs(ThinkStack.States[i].Hedgehog.Y - Me.Y) <= 2) |
|
80 and (ThinkStack.States[i].Hedgehog.Message = Me.Message); |
|
81 inc(i) |
|
82 end |
|
83 end; |
|
84 |
|
85 procedure ClearThinkStack; |
|
86 begin |
|
87 ThinkStack.Count:= 0 |
|
88 end; |
|
89 |
|
90 end. |