yapf/yapf_rail.cpp
branchcustombridgeheads
changeset 5627 f5c656cf0a0e
parent 5623 ef2a8a524a95
equal deleted inserted replaced
5626:1811beeb472f 5627:f5c656cf0a0e
    47 	}
    47 	}
    48 
    48 
    49 	FORCEINLINE bool FindNearestDepotTwoWay(Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_distance, int reverse_penalty, TileIndex* depot_tile, bool* reversed)
    49 	FORCEINLINE bool FindNearestDepotTwoWay(Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_distance, int reverse_penalty, TileIndex* depot_tile, bool* reversed)
    50 	{
    50 	{
    51 		// set origin and destination nodes
    51 		// set origin and destination nodes
    52 		Yapf().SetOrigin(t1, td1, t2, td2, reverse_penalty, true);
    52 		Yapf().SetOrigin(t1, TrackdirToTrackdirBits(td1), t2, TrackdirToTrackdirBits(td2), reverse_penalty, true);
    53 		Yapf().SetDestination(v);
    53 		Yapf().SetDestination(v);
    54 		Yapf().SetMaxCost(YAPF_TILE_LENGTH * max_distance);
    54 		Yapf().SetMaxCost(YAPF_TILE_LENGTH * max_distance);
    55 
    55 
    56 		// find the best path
    56 		// find the best path
    57 		bool bFound = Yapf().FindPath(v);
    57 		bool bFound = Yapf().FindPath(v);
   101 	}
   101 	}
   102 
   102 
   103 	/// return debug report character to identify the transportation type
   103 	/// return debug report character to identify the transportation type
   104 	FORCEINLINE char TransportTypeChar() const {return 't';}
   104 	FORCEINLINE char TransportTypeChar() const {return 't';}
   105 
   105 
   106 	static Trackdir stChooseRailTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirs, bool *path_not_found)
   106 	static Trackdir stChooseRailTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found)
   107 	{
   107 	{
   108 		// create pathfinder instance
   108 		// create pathfinder instance
   109 		Tpf pf;
   109 		Tpf pf;
   110 		return pf.ChooseRailTrack(v, tile, enterdir, trackdirs, path_not_found);
   110 		return pf.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found);
   111 	}
   111 	}
   112 
   112 
   113 	FORCEINLINE Trackdir ChooseRailTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirs, bool *path_not_found)
   113 	FORCEINLINE Trackdir ChooseRailTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found)
   114 	{
   114 	{
   115 		// set origin and destination nodes
   115 		// set origin and destination nodes
   116 		Yapf().SetOrigin(v->tile, GetVehicleTrackdir(v), INVALID_TILE, INVALID_TRACKDIR, 1, true);
   116 		TrackdirBits trackdirs = (TrackdirBits)(0x101 * tracks);
       
   117 		trackdirs &= DiagdirReachesTrackdirs(enterdir);
       
   118 		Yapf().SetOrigin(tile, trackdirs, INVALID_TILE, INVALID_TRACKDIR_BIT, 1, true);
   117 		Yapf().SetDestination(v);
   119 		Yapf().SetDestination(v);
   118 
   120 
   119 		// find the best path
   121 		// find the best path
   120 		bool path_found = Yapf().FindPath(v);
   122 		bool path_found = Yapf().FindPath(v);
   121 		if (path_not_found != NULL) {
   123 		if (path_not_found != NULL) {
   128 		Trackdir next_trackdir = INVALID_TRACKDIR;
   130 		Trackdir next_trackdir = INVALID_TRACKDIR;
   129 		Node* pNode = &Yapf().GetBestNode();
   131 		Node* pNode = &Yapf().GetBestNode();
   130 		if (pNode != NULL) {
   132 		if (pNode != NULL) {
   131 			// path was found or at least suggested
   133 			// path was found or at least suggested
   132 			// walk through the path back to the origin
   134 			// walk through the path back to the origin
   133 			Node* pPrev = NULL;
       
   134 			while (pNode->m_parent != NULL) {
   135 			while (pNode->m_parent != NULL) {
   135 				pPrev = pNode;
       
   136 				pNode = pNode->m_parent;
   136 				pNode = pNode->m_parent;
   137 			}
   137 			}
   138 			// return trackdir from the best origin node (one of start nodes)
   138 			// return trackdir from the best origin node (one of start nodes)
   139 			Node& best_next_node = *pPrev;
   139 			Node& best_next_node = *pNode;
   140 			assert(best_next_node.GetTile() == tile);
   140 			assert(v->tile == tile || best_next_node.GetTile() == tile);
   141 			next_trackdir = best_next_node.GetTrackdir();
   141 			next_trackdir = best_next_node.GetTrackdir();
   142 		}
   142 		}
   143 		return next_trackdir;
   143 		return next_trackdir;
   144 	}
   144 	}
   145 
   145 
   151 
   151 
   152 	FORCEINLINE bool CheckReverseTrain(Vehicle* v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2)
   152 	FORCEINLINE bool CheckReverseTrain(Vehicle* v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2)
   153 	{
   153 	{
   154 		// create pathfinder instance
   154 		// create pathfinder instance
   155 		// set origin and destination nodes
   155 		// set origin and destination nodes
   156 		Yapf().SetOrigin(t1, td1, t2, td2, 1, false);
   156 		Yapf().SetOrigin(t1, TrackdirToTrackdirBits(td1), t2, TrackdirToTrackdirBits(td2), 1, false);
   157 		Yapf().SetDestination(v);
   157 		Yapf().SetDestination(v);
   158 
   158 
   159 		// find the best path
   159 		// find the best path
   160 		bool bFound = Yapf().FindPath(v);
   160 		bool bFound = Yapf().FindPath(v);
   161 
   161 
   198 struct CYapfAnyDepotRail1 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail1, CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
   198 struct CYapfAnyDepotRail1 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail1, CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
   199 struct CYapfAnyDepotRail2 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail2, CFollowTrackRail    , CRailNodeListExitDir , CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
   199 struct CYapfAnyDepotRail2 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail2, CFollowTrackRail    , CRailNodeListExitDir , CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
   200 struct CYapfAnyDepotRail3 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail3, CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
   200 struct CYapfAnyDepotRail3 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail3, CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
   201 
   201 
   202 
   202 
   203 Trackdir YapfChooseRailTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirs, bool *path_not_found)
   203 Trackdir YapfChooseRailTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found)
   204 {
   204 {
   205 	// default is YAPF type 2
   205 	// default is YAPF type 2
   206 	typedef Trackdir (*PfnChooseRailTrack)(Vehicle*, TileIndex, DiagDirection, TrackdirBits, bool*);
   206 	typedef Trackdir (*PfnChooseRailTrack)(Vehicle*, TileIndex, DiagDirection, TrackBits, bool*);
   207 	PfnChooseRailTrack pfnChooseRailTrack = &CYapfRail2::stChooseRailTrack;
   207 	PfnChooseRailTrack pfnChooseRailTrack = &CYapfRail2::stChooseRailTrack;
   208 
   208 
   209 	// check if non-default YAPF type needed
   209 	// check if non-default YAPF type needed
   210 	if (_patches.forbid_90_deg)
   210 	if (_patches.forbid_90_deg)
   211 		pfnChooseRailTrack = &CYapfRail3::stChooseRailTrack; // Trackdir, forbid 90-deg
   211 		pfnChooseRailTrack = &CYapfRail3::stChooseRailTrack; // Trackdir, forbid 90-deg
   212 	else if (_patches.yapf.disable_node_optimization)
   212 	else if (_patches.yapf.disable_node_optimization)
   213 		pfnChooseRailTrack = &CYapfRail1::stChooseRailTrack; // Trackdir, allow 90-deg
   213 		pfnChooseRailTrack = &CYapfRail1::stChooseRailTrack; // Trackdir, allow 90-deg
   214 
   214 
   215 	Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, trackdirs, path_not_found);
   215 	Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_not_found);
   216 
   216 
   217 	return td_ret;
   217 	return td_ret;
   218 }
   218 }
   219 
   219 
   220 bool YapfCheckReverseTrain(Vehicle* v)
   220 bool YapfCheckReverseTrain(Vehicle* v)