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, uFloat; |
|
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: LongInt): boolean; |
|
36 function Pop(var Ticks: Longword; var Actions: TActions; var 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: LongInt): boolean; |
|
43 var Result: boolean; |
|
44 begin |
|
45 Result:= (ThinkStack.Count < cBranchStackSize) and (Actions.Count < MAXACTIONS - 5); |
|
46 if Result then |
|
47 with ThinkStack.States[ThinkStack.Count] do |
|
48 begin |
|
49 WastedTicks:= Ticks; |
|
50 MadeActions:= Actions; |
|
51 Hedgehog:= Me; |
|
52 Hedgehog.Message:= Dir; |
|
53 inc(ThinkStack.Count) |
|
54 end; |
|
55 Push:= Result |
|
56 end; |
|
57 |
|
58 function Pop(var Ticks: Longword; var Actions: TActions; var Me: TGear): boolean; |
|
59 var Result: boolean; |
|
60 begin |
|
61 Result:= ThinkStack.Count > 0; |
|
62 if Result then |
|
63 begin |
|
64 dec(ThinkStack.Count); |
|
65 with ThinkStack.States[ThinkStack.Count] do |
|
66 begin |
|
67 Ticks:= WastedTicks; |
|
68 Actions:= MadeActions; |
|
69 Me:= Hedgehog |
|
70 end |
|
71 end; |
|
72 Pop:= Result |
|
73 end; |
|
74 |
|
75 function PosInThinkStack(Me: PGear): boolean; |
|
76 var i: Longword; |
|
77 begin |
|
78 i:= 0; |
|
79 while (i < ThinkStack.Count) do |
|
80 begin |
|
81 if (not (2 < hwAbs(ThinkStack.States[i].Hedgehog.X - Me^.X) + |
|
82 hwAbs(ThinkStack.States[i].Hedgehog.Y - Me^.Y))) |
|
83 and (ThinkStack.States[i].Hedgehog.Message = Me^.Message) then exit(true); |
|
84 inc(i) |
|
85 end; |
|
86 PosInThinkStack:= false |
|
87 end; |
|
88 |
|
89 procedure ClearThinkStack; |
|
90 begin |
|
91 ThinkStack.Count:= 0 |
|
92 end; |
|
93 |
|
94 end. |
|