src/yapf/yapf_rail.cpp
branchNewGRF_ports
changeset 6720 35756db7e577
parent 6719 4cc327ad39d5
child 6872 1c4a4a609f85
equal deleted inserted replaced
6719:4cc327ad39d5 6720:35756db7e577
     6 
     6 
     7 #include "yapf.hpp"
     7 #include "yapf.hpp"
     8 #include "yapf_node_rail.hpp"
     8 #include "yapf_node_rail.hpp"
     9 #include "yapf_costrail.hpp"
     9 #include "yapf_costrail.hpp"
    10 #include "yapf_destrail.hpp"
    10 #include "yapf_destrail.hpp"
       
    11 
       
    12 #define DEBUG_YAPF_CACHE 0
    11 
    13 
    12 int _total_pf_time_us = 0;
    14 int _total_pf_time_us = 0;
    13 
    15 
    14 
    16 
    15 
    17 
    42 	/// return debug report character to identify the transportation type
    44 	/// return debug report character to identify the transportation type
    43 	FORCEINLINE char TransportTypeChar() const {return 't';}
    45 	FORCEINLINE char TransportTypeChar() const {return 't';}
    44 
    46 
    45 	static bool stFindNearestDepotTwoWay(Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_distance, int reverse_penalty, TileIndex* depot_tile, bool* reversed)
    47 	static bool stFindNearestDepotTwoWay(Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_distance, int reverse_penalty, TileIndex* depot_tile, bool* reversed)
    46 	{
    48 	{
    47 		Tpf pf;
    49 		Tpf pf1;
    48 		return pf.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_distance, reverse_penalty, depot_tile, reversed);
    50 		bool result1 = pf1.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_distance, reverse_penalty, depot_tile, reversed);
       
    51 
       
    52 #if DEBUG_YAPF_CACHE
       
    53 		Tpf pf2;
       
    54 		TileIndex depot_tile2 = INVALID_TILE;
       
    55 		bool reversed2 = false;
       
    56 		pf2.DisableCache(true);
       
    57 		bool result2 = pf2.FindNearestDepotTwoWay(v, t1, td1, t2, td2, max_distance, reverse_penalty, &depot_tile2, &reversed2);
       
    58 		if (result1 != result2 || (result1 && (*depot_tile != depot_tile2 || *reversed != reversed2))) {
       
    59 			DEBUG(yapf, 0, "CACHE ERROR: FindNearestDepotTwoWay() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
       
    60 		}
       
    61 #endif
       
    62 
       
    63 		return result1;
    49 	}
    64 	}
    50 
    65 
    51 	FORCEINLINE bool FindNearestDepotTwoWay(Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_distance, int reverse_penalty, TileIndex* depot_tile, bool* reversed)
    66 	FORCEINLINE bool FindNearestDepotTwoWay(Vehicle *v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2, int max_distance, int reverse_penalty, TileIndex* depot_tile, bool* reversed)
    52 	{
    67 	{
    53 		// set origin and destination nodes
    68 		// set origin and destination nodes
   106 	FORCEINLINE char TransportTypeChar() const {return 't';}
   121 	FORCEINLINE char TransportTypeChar() const {return 't';}
   107 
   122 
   108 	static Trackdir stChooseRailTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found)
   123 	static Trackdir stChooseRailTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found)
   109 	{
   124 	{
   110 		// create pathfinder instance
   125 		// create pathfinder instance
   111 		Tpf pf;
   126 		Tpf pf1;
   112 		return pf.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found);
   127 		Trackdir result1 = pf1.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found);
       
   128 
       
   129 #if DEBUG_YAPF_CACHE
       
   130 		Tpf pf2;
       
   131 		pf2.DisableCache(true);
       
   132 		Trackdir result2 = pf2.ChooseRailTrack(v, tile, enterdir, tracks, path_not_found);
       
   133 		if (result1 != result2) {
       
   134 			DEBUG(yapf, 0, "CACHE ERROR: ChooseRailTrack() = [%d, %d]", result1, result2);
       
   135 			DumpTarget dmp1, dmp2;
       
   136 			pf1.DumpBase(dmp1);
       
   137 			pf2.DumpBase(dmp2);
       
   138 			FILE *f1 = fopen("C:\\yapf1.txt", "wt");
       
   139 			FILE *f2 = fopen("C:\\yapf2.txt", "wt");
       
   140 			fwrite(dmp1.m_out.Data(), 1, dmp1.m_out.Size(), f1);
       
   141 			fwrite(dmp2.m_out.Data(), 1, dmp2.m_out.Size(), f2);
       
   142 			fclose(f1);
       
   143 			fclose(f2);
       
   144 		}
       
   145 #endif
       
   146 
       
   147 		return result1;
   113 	}
   148 	}
   114 
   149 
   115 	FORCEINLINE Trackdir ChooseRailTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found)
   150 	FORCEINLINE Trackdir ChooseRailTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found)
   116 	{
   151 	{
   117 		// set origin and destination nodes
   152 		// set origin and destination nodes
   145 		return next_trackdir;
   180 		return next_trackdir;
   146 	}
   181 	}
   147 
   182 
   148 	static bool stCheckReverseTrain(Vehicle* v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2)
   183 	static bool stCheckReverseTrain(Vehicle* v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2)
   149 	{
   184 	{
   150 		Tpf pf;
   185 		Tpf pf1;
   151 		return pf.CheckReverseTrain(v, t1, td1, t2, td2);
   186 		bool result1 = pf1.CheckReverseTrain(v, t1, td1, t2, td2);
       
   187 
       
   188 #if DEBUG_YAPF_CACHE
       
   189 		Tpf pf2;
       
   190 		pf2.DisableCache(true);
       
   191 		bool result2 = pf2.CheckReverseTrain(v, t1, td1, t2, td2);
       
   192 		if (result1 != result2) {
       
   193 			DEBUG(yapf, 0, "CACHE ERROR: CheckReverseTrain() = [%s, %s]", result1 ? "T" : "F", result2 ? "T" : "F");
       
   194 		}
       
   195 #endif
       
   196 
       
   197 		return result1;
   152 	}
   198 	}
   153 
   199 
   154 	FORCEINLINE bool CheckReverseTrain(Vehicle* v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2)
   200 	FORCEINLINE bool CheckReverseTrain(Vehicle* v, TileIndex t1, Trackdir td1, TileIndex t2, Trackdir td2)
   155 	{
   201 	{
   156 		// create pathfinder instance
   202 		// create pathfinder instance
   192 	typedef CYapfSegmentCostCacheGlobalT<Types> PfCache;
   238 	typedef CYapfSegmentCostCacheGlobalT<Types> PfCache;
   193 	typedef CYapfCostRailT<Types>               PfCost;
   239 	typedef CYapfCostRailT<Types>               PfCost;
   194 };
   240 };
   195 
   241 
   196 struct CYapfRail1         : CYapfT<CYapfRail_TypesT<CYapfRail1        , CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
   242 struct CYapfRail1         : CYapfT<CYapfRail_TypesT<CYapfRail1        , CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
   197 struct CYapfRail2         : CYapfT<CYapfRail_TypesT<CYapfRail2        , CFollowTrackRail    , CRailNodeListExitDir , CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
   243 struct CYapfRail2         : CYapfT<CYapfRail_TypesT<CYapfRail2        , CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
   198 struct CYapfRail3         : CYapfT<CYapfRail_TypesT<CYapfRail3        , CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationTileOrStationRailT, CYapfFollowRailT> > {};
       
   199 
   244 
   200 struct CYapfAnyDepotRail1 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail1, CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
   245 struct CYapfAnyDepotRail1 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail1, CFollowTrackRail    , CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
   201 struct CYapfAnyDepotRail2 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail2, CFollowTrackRail    , CRailNodeListExitDir , CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
   246 struct CYapfAnyDepotRail2 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail2, CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
   202 struct CYapfAnyDepotRail3 : CYapfT<CYapfRail_TypesT<CYapfAnyDepotRail3, CFollowTrackRailNo90, CRailNodeListTrackDir, CYapfDestinationAnyDepotRailT     , CYapfFollowAnyDepotRailT> > {};
       
   203 
   247 
   204 
   248 
   205 Trackdir YapfChooseRailTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found)
   249 Trackdir YapfChooseRailTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found)
   206 {
   250 {
   207 	// default is YAPF type 2
   251 	// default is YAPF type 2
   208 	typedef Trackdir (*PfnChooseRailTrack)(Vehicle*, TileIndex, DiagDirection, TrackBits, bool*);
   252 	typedef Trackdir (*PfnChooseRailTrack)(Vehicle*, TileIndex, DiagDirection, TrackBits, bool*);
   209 	PfnChooseRailTrack pfnChooseRailTrack = &CYapfRail2::stChooseRailTrack;
   253 	PfnChooseRailTrack pfnChooseRailTrack = &CYapfRail1::stChooseRailTrack;
   210 
   254 
   211 	// check if non-default YAPF type needed
   255 	// check if non-default YAPF type needed
   212 	if (_patches.forbid_90_deg)
   256 	if (_patches.forbid_90_deg) {
   213 		pfnChooseRailTrack = &CYapfRail3::stChooseRailTrack; // Trackdir, forbid 90-deg
   257 		pfnChooseRailTrack = &CYapfRail2::stChooseRailTrack; // Trackdir, forbid 90-deg
   214 	else if (_patches.yapf.disable_node_optimization)
   258 	}
   215 		pfnChooseRailTrack = &CYapfRail1::stChooseRailTrack; // Trackdir, allow 90-deg
       
   216 
   259 
   217 	Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_not_found);
   260 	Trackdir td_ret = pfnChooseRailTrack(v, tile, enterdir, tracks, path_not_found);
   218 
   261 
   219 	return td_ret;
   262 	return td_ret;
   220 }
   263 }
   231 	Trackdir td = GetVehicleTrackdir(v);
   274 	Trackdir td = GetVehicleTrackdir(v);
   232 	Trackdir td_rev = ReverseTrackdir(GetVehicleTrackdir(last_veh));
   275 	Trackdir td_rev = ReverseTrackdir(GetVehicleTrackdir(last_veh));
   233 
   276 
   234 
   277 
   235 	typedef bool (*PfnCheckReverseTrain)(Vehicle*, TileIndex, Trackdir, TileIndex, Trackdir);
   278 	typedef bool (*PfnCheckReverseTrain)(Vehicle*, TileIndex, Trackdir, TileIndex, Trackdir);
   236 	PfnCheckReverseTrain pfnCheckReverseTrain = CYapfRail2::stCheckReverseTrain;
   279 	PfnCheckReverseTrain pfnCheckReverseTrain = CYapfRail1::stCheckReverseTrain;
   237 
   280 
   238 	// check if non-default YAPF type needed
   281 	// check if non-default YAPF type needed
   239 	if (_patches.forbid_90_deg)
   282 	if (_patches.forbid_90_deg) {
   240 		pfnCheckReverseTrain = &CYapfRail3::stCheckReverseTrain; // Trackdir, forbid 90-deg
   283 		pfnCheckReverseTrain = &CYapfRail2::stCheckReverseTrain; // Trackdir, forbid 90-deg
   241 	else if (_patches.yapf.disable_node_optimization)
   284 	}
   242 		pfnCheckReverseTrain = &CYapfRail1::stCheckReverseTrain; // Trackdir, allow 90-deg
       
   243 
   285 
   244 	bool reverse = pfnCheckReverseTrain(v, tile, td, last_veh->tile, td_rev);
   286 	bool reverse = pfnCheckReverseTrain(v, tile, td, last_veh->tile, td_rev);
   245 
   287 
   246 	return reverse;
   288 	return reverse;
   247 }
   289 }
   259 	// their trackdirs
   301 	// their trackdirs
   260 	Trackdir td = GetVehicleTrackdir(v);
   302 	Trackdir td = GetVehicleTrackdir(v);
   261 	Trackdir td_rev = ReverseTrackdir(GetVehicleTrackdir(last_veh));
   303 	Trackdir td_rev = ReverseTrackdir(GetVehicleTrackdir(last_veh));
   262 
   304 
   263 	typedef bool (*PfnFindNearestDepotTwoWay)(Vehicle*, TileIndex, Trackdir, TileIndex, Trackdir, int, int, TileIndex*, bool*);
   305 	typedef bool (*PfnFindNearestDepotTwoWay)(Vehicle*, TileIndex, Trackdir, TileIndex, Trackdir, int, int, TileIndex*, bool*);
   264 	PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay;
   306 	PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail1::stFindNearestDepotTwoWay;
   265 
   307 
   266 	// check if non-default YAPF type needed
   308 	// check if non-default YAPF type needed
   267 	if (_patches.forbid_90_deg)
   309 	if (_patches.forbid_90_deg) {
   268 		pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail3::stFindNearestDepotTwoWay; // Trackdir, forbid 90-deg
   310 		pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay; // Trackdir, forbid 90-deg
   269 	else if (_patches.yapf.disable_node_optimization)
   311 	}
   270 		pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail1::stFindNearestDepotTwoWay; // Trackdir, allow 90-deg
       
   271 
   312 
   272 	bool ret = pfnFindNearestDepotTwoWay(v, tile, td, last_tile, td_rev, max_distance, reverse_penalty, depot_tile, reversed);
   313 	bool ret = pfnFindNearestDepotTwoWay(v, tile, td, last_tile, td_rev, max_distance, reverse_penalty, depot_tile, reversed);
   273 	return ret;
   314 	return ret;
   274 }
   315 }
   275 
   316