ScummVM API documentation
dgPathFinder.h
1 /* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef __dgPathFinder__
23 #define __dgPathFinder__
24 
25 #include "dgStdafx.h"
26 #include "dgTree.h"
27 #include "dgHeap.h"
28 
29 template<class NODEID, class COST> class dgPathFinder;
30 
31 
32 #define DG_MAX_PATH_ENUMERATION_CHILDREN 128
33 
34 template<class NODEID, class COST>
35 class dgPathNode {
36  friend class dgPathFinder<NODEID, COST>;
37  NODEID m_Id;
38  COST m_NodeCostToGoal;
39  COST m_NodeCostFromSource;
40  dgPathNode *m_Next;
41 
42 public:
43  dgPathNode();
44  NODEID GetId() const;
45  const dgPathNode *GetNext() const;
46  const dgPathNode *GetParent() const;
47 };
48 
49 template<class NODEID, class COST>
50 class dgPathCloseList: public dgTree<dgPathNode<NODEID, COST>, NODEID> {
51 protected:
53 };
54 
55 
56 template<class NODEID, class COST>
57 class dgPathOpenHeap: public dgUpHeap<typename dgPathCloseList<NODEID, COST>::dgTreeNode *, COST> {
58 protected:
59  dgPathOpenHeap(dgInt32 maxElements)
61  }
62 
63  friend class dgPathFinder<NODEID, COST>;
64 };
65 
66 
67 template<class NODEID, class COST>
68 class dgPathFinder: public dgPathCloseList<NODEID, COST> {
69  dgInt32 maxChildren;
70 
71 public:
72  dgPathFinder(dgInt32 maxElementsInOpenList, dgInt32 nodeMaxChildren);
73  virtual ~dgPathFinder();
74 
75  virtual const dgPathNode<NODEID, COST> *CalCulatePath(NODEID source, NODEID goal);
76 
77  // this funtions must be overloaded by the user
78  virtual COST GetCostFromParent(const dgPathNode<NODEID, COST> &node) const;
79  virtual COST GetEstimatedCostToGoal(NODEID id) const;
80  virtual dgInt32 EnumerateChildren(NODEID parent, NODEID array[]) const;
81 
83 };
84 
85 
86 template<class NODEID, class COST>
88 }
89 
90 template<class NODEID, class COST>
91 NODEID dgPathNode<NODEID, COST>::GetId() const {
92  return m_Id;
93 }
94 
95 
96 template<class NODEID, class COST>
98  return m_Next;
99 }
100 
101 
102 template<class NODEID, class COST>
104  return m_Next;
105 }
106 
107 
108 template<class NODEID, class COST>
110  dgInt32 maxElementsInOpenList,
111  dgInt32 nodeMaxChildren)
113  m_openList(maxElementsInOpenList) {
114  maxChildren = nodeMaxChildren;
115 }
116 
117 
118 template<class NODEID, class COST>
120 }
121 
122 template<class NODEID, class COST>
123 const dgPathNode<NODEID, COST> *dgPathFinder<NODEID, COST>::CalCulatePath(NODEID source, NODEID goal) {
124  dgInt32 count;
125  dgInt32 heapMaxCount;
131  NODEID idArray[DG_MAX_PATH_ENUMERATION_CHILDREN];
132 
133  dgPathCloseList<NODEID, COST> &close = *this;
134 
135  m_openList.Flush();
136  close.RemoveAll();
137 
138  cell.m_Id = source;
139  cell.m_Next = NULL;
140  cell.m_NodeCostFromSource = COST(0);
141  cell.m_NodeCostToGoal = GetEstimatedCostToGoal(cell.m_Id);
142 
143  node = close.Insert(cell, source);
144 
145  heapMaxCount = m_openList.GetMaxCount();
146  m_openList.Push(node, cell.m_NodeCostFromSource + cell.m_NodeCostToGoal);
147  while (m_openList.GetCount()) {
148  node = m_openList[0];
149  dgPathNode<NODEID, COST> &parent = node->GetInfo();
150  if (parent.m_Id == goal) {
151  link = &parent;
152  next = NULL;
153  do {
154  prev = link->m_Next;
155  link->m_Next = next;
156  next = link;
157  link = prev;
158  } while (link);
159  return next;
160  }
161 
162  m_openList.Pop();
163  count = EnumerateChildren(node->GetKey(), idArray);
164  cell.m_Next = &parent;
165  for (int i = 0; i < count; i ++) {
166  bool state;
167  cell.m_Id = idArray[i];
168 
169  COST newCostFromSource(GetCostFromParent(cell) + parent.m_NodeCostFromSource);
170  node = close.Insert(cell, cell.m_Id, state);
171  dgPathNode<NODEID, COST> &newCell = node->GetInfo();
172  if (state) {
173  if (newCell.m_NodeCostFromSource <= newCostFromSource) {
174  continue;
175  }
176  //newCell.m_Next = cell.m_Next;
177  }
178 
179  newCell.m_NodeCostFromSource = newCostFromSource;
180  newCell.m_NodeCostToGoal = GetEstimatedCostToGoal(newCell.m_Id);
181  if (m_openList.GetCount() >= heapMaxCount) {
182  m_openList.Remove(heapMaxCount);
183  }
184  m_openList.Push(node, newCell.m_NodeCostFromSource + newCell.m_NodeCostToGoal);
185  }
186  }
187  return NULL;
188 }
189 
190 template<class NODEID, class COST>
192  return COST(1);
193 }
194 
195 template<class NODEID, class COST>
197  return COST(1);
198 }
199 
200 template<class NODEID, class COST>
201 dgInt32 dgPathFinder<NODEID, COST>::EnumerateChildren(NODEID parent, NODEID array[]) const {
202  return 0;
203 }
204 
205 
206 
207 
208 #endif
209 
210 
211 
Definition: dgPathFinder.h:29
Definition: dgPathFinder.h:57
Definition: dgPathFinder.h:35
Definition: dgTree.h:88
Definition: dgPathFinder.h:50
Definition: dgHeap.h:91