yapf/unittest/test_yapf.h
changeset 3914 f5118020cd84
parent 3900 4984308f9125
child 3971 2116cd6db7fd
equal deleted inserted replaced
3913:fb3f48c82de6 3914:f5118020cd84
   130 typedef CTestYapfNodeT<CNodeKey2> CYapfNode2;
   130 typedef CTestYapfNodeT<CNodeKey2> CYapfNode2;
   131 
   131 
   132 template <class Types>
   132 template <class Types>
   133 struct CYapfTestBaseT
   133 struct CYapfTestBaseT
   134 {
   134 {
   135 	typedef typename Types::Tpf Tpf;
   135 	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
   136 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
   136 	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
   137 	typedef typename Node::Key Key;    ///< key to hash tables
   137 	typedef typename Node::Key Key;               ///< key to hash tables
   138 	typedef typename Types::Map Map;
   138 	typedef typename Types::Map Map;
   139 
   139 
   140 	int m_x1, m_y1;
   140 	int m_x1, m_y1;
   141 	int m_x2, m_y2;
   141 	int m_x2, m_y2;
   142 	Trackdir m_td1;
   142 	Trackdir m_td1;
   153 		m_x2 = x2;
   153 		m_x2 = x2;
   154 		m_y2 = y2;
   154 		m_y2 = y2;
   155 		m_td1 = td1;
   155 		m_td1 = td1;
   156 	}
   156 	}
   157 
   157 
       
   158 	/// to access inherited path finder
   158 	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
   159 	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
   159 	FORCEINLINE char TransportTypeChar() const {return 'T';}
   160 	FORCEINLINE char TransportTypeChar() const {return 'T';}
   160 
   161 
       
   162 	/** Called by YAPF to move from the given node to the next tile. For each
       
   163 	*   reachable trackdir on the new tile creates new node, initializes it
       
   164 	*   and adds it to the open list by calling Yapf().AddNewNode(n) */
   161 	FORCEINLINE void PfFollowNode(Node& org)
   165 	FORCEINLINE void PfFollowNode(Node& org)
   162 	{
   166 	{
   163 		int x_org = org.m_key.m_x;
   167 		int x_org = org.m_key.m_x;
   164 		int y_org = org.m_key.m_y;
   168 		int y_org = org.m_key.m_y;
   165 		int z_org = Map::MapZ(x_org, y_org);
   169 		int z_org = Map::MapZ(x_org, y_org);
   189 			n.m_parent   = &org;
   193 			n.m_parent   = &org;
   190 			Yapf().AddNewNode(n);
   194 			Yapf().AddNewNode(n);
   191 		}
   195 		}
   192 	}
   196 	}
   193 
   197 
       
   198 	/// Called when YAPF needs to place origin nodes into open list
   194 	FORCEINLINE void PfSetStartupNodes()
   199 	FORCEINLINE void PfSetStartupNodes()
   195 	{
   200 	{
   196 		Node& n1 = Yapf().CreateNewNode();
   201 		Node& n1 = Yapf().CreateNewNode();
   197 		n1.m_key.m_x = m_x1;
   202 		n1.m_key.m_x = m_x1;
   198 		n1.m_key.m_y = m_y1;
   203 		n1.m_key.m_y = m_y1;
   199 		n1.m_key.m_td = m_td1;
   204 		n1.m_key.m_td = m_td1;
   200 		n1.m_key.m_exitdir = TrackdirToExitdir(n1.m_key.m_td);
   205 		n1.m_key.m_exitdir = TrackdirToExitdir(n1.m_key.m_td);
   201 		Yapf().AddStartupNode(n1);
   206 		Yapf().AddStartupNode(n1);
   202 	}
   207 	}
   203 
   208 
       
   209 	/** Called by YAPF to calculate the cost from the origin to the given node.
       
   210 	*   Calculates only the cost of given node, adds it to the parent node cost
       
   211 	*   and stores the result into Node::m_cost member */
   204 	FORCEINLINE bool PfCalcCost(Node& n)
   212 	FORCEINLINE bool PfCalcCost(Node& n)
   205 	{
   213 	{
   206 		// base tile cost depending on distance
   214 		// base tile cost depending on distance
   207 		int c = IsDiagonalTrackdir(n.m_key.m_td) ? 10 : 7;
   215 		int c = IsDiagonalTrackdir(n.m_key.m_td) ? 10 : 7;
   208 		// additional penalty for curve
   216 		// additional penalty for curve
   214 		// apply it
   222 		// apply it
   215 		n.m_cost = n.m_parent->m_cost + c;
   223 		n.m_cost = n.m_parent->m_cost + c;
   216 		return true;
   224 		return true;
   217 	}
   225 	}
   218 
   226 
       
   227 	/** Called by YAPF to calculate cost estimate. Calculates distance to the destination
       
   228 	*   adds it to the actual cost from origin and stores the sum to the Node::m_estimate */
   219 	FORCEINLINE bool PfCalcEstimate(Node& n)
   229 	FORCEINLINE bool PfCalcEstimate(Node& n)
   220 	{
   230 	{
   221 		int dx = abs(n.m_key.m_x - m_x2);
   231 		int dx = abs(n.m_key.m_x - m_x2);
   222 		int dy = abs(n.m_key.m_y - m_y2);
   232 		int dy = abs(n.m_key.m_y - m_y2);
   223 		int dd = min(dx, dy);
   233 		int dd = min(dx, dy);
   225 		int d = 14 * dd + 10 * dxy;
   235 		int d = 14 * dd + 10 * dxy;
   226 		n.m_estimate = n.m_cost + d /*+ d / 4*/;
   236 		n.m_estimate = n.m_cost + d /*+ d / 4*/;
   227 		return true;
   237 		return true;
   228 	}
   238 	}
   229 
   239 
       
   240 	/// Called by YAPF to detect if node ends in the desired destination
   230 	FORCEINLINE bool PfDetectDestination(Node& n)
   241 	FORCEINLINE bool PfDetectDestination(Node& n)
   231 	{
   242 	{
   232 		bool bDest = (n.m_key.m_x == m_x2) && (n.m_key.m_y == m_y2);
   243 		bool bDest = (n.m_key.m_x == m_x2) && (n.m_key.m_y == m_y2);
   233 		return bDest;
   244 		return bDest;
   234 	}
   245 	}