yapf/yapf_rail.cpp
changeset 4865 8c4fb07e56f6
parent 4549 106ed18a7675
child 4870 2d8c7ffbbac9
equal deleted inserted replaced
4864:58c28d9e3d9b 4865:8c4fb07e56f6
   237 	bool reverse = pfnCheckReverseTrain(v, tile, td, last_veh->tile, td_rev);
   237 	bool reverse = pfnCheckReverseTrain(v, tile, td, last_veh->tile, td_rev);
   238 
   238 
   239 	return reverse;
   239 	return reverse;
   240 }
   240 }
   241 
   241 
       
   242 static TileIndex YapfGetVehicleOutOfTunnelTile(const Vehicle *v, bool bReverse);
       
   243 
   242 bool YapfFindNearestRailDepotTwoWay(Vehicle *v, int max_distance, int reverse_penalty, TileIndex* depot_tile, bool* reversed)
   244 bool YapfFindNearestRailDepotTwoWay(Vehicle *v, int max_distance, int reverse_penalty, TileIndex* depot_tile, bool* reversed)
   243 {
   245 {
   244 	*depot_tile = INVALID_TILE;
   246 	*depot_tile = INVALID_TILE;
   245 	*reversed = false;
   247 	*reversed = false;
   246 
   248 
   248 
   250 
   249 	bool first_in_tunnel = v->u.rail.track == 0x40;
   251 	bool first_in_tunnel = v->u.rail.track == 0x40;
   250 	bool last_in_tunnel = last_veh->u.rail.track == 0x40;
   252 	bool last_in_tunnel = last_veh->u.rail.track == 0x40;
   251 
   253 
   252 	// tile where the engine and last wagon are
   254 	// tile where the engine and last wagon are
   253 	TileIndex tile = first_in_tunnel ? INVALID_TILE : v->tile;
   255 	TileIndex tile = first_in_tunnel ? YapfGetVehicleOutOfTunnelTile(v, false) : v->tile;
   254 	TileIndex last_tile = last_in_tunnel ? INVALID_TILE : last_veh->tile;
   256 	TileIndex last_tile = last_in_tunnel ? YapfGetVehicleOutOfTunnelTile(last_veh, true) : last_veh->tile;
   255 
   257 
   256 	// their trackdirs
   258 	// their trackdirs
   257 	Trackdir td = first_in_tunnel ? INVALID_TRACKDIR : GetVehicleTrackdir(v);
   259 	Trackdir td = GetVehicleTrackdir(v);
   258 	Trackdir td_rev = last_in_tunnel ? INVALID_TRACKDIR : ReverseTrackdir(GetVehicleTrackdir(last_veh));
   260 	Trackdir td_rev = ReverseTrackdir(GetVehicleTrackdir(last_veh));
   259 
   261 
   260 	typedef bool (*PfnFindNearestDepotTwoWay)(Vehicle*, TileIndex, Trackdir, TileIndex, Trackdir, int, int, TileIndex*, bool*);
   262 	typedef bool (*PfnFindNearestDepotTwoWay)(Vehicle*, TileIndex, Trackdir, TileIndex, Trackdir, int, int, TileIndex*, bool*);
   261 	PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay;
   263 	PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail2::stFindNearestDepotTwoWay;
   262 
   264 
   263 	// check if non-default YAPF type needed
   265 	// check if non-default YAPF type needed
   268 
   270 
   269 	bool ret = pfnFindNearestDepotTwoWay(v, tile, td, last_tile, td_rev, max_distance, reverse_penalty, depot_tile, reversed);
   271 	bool ret = pfnFindNearestDepotTwoWay(v, tile, td, last_tile, td_rev, max_distance, reverse_penalty, depot_tile, reversed);
   270 	return ret;
   272 	return ret;
   271 }
   273 }
   272 
   274 
       
   275 /** Retrieve the exit-tile of the vehicle from inside a tunnel
       
   276 * Very similar to GetOtherTunnelEnd(), but we use the vehicle's
       
   277 * direction for determining which end of the tunnel to find
       
   278 * @param v the vehicle which is inside the tunnel and needs an exit
       
   279 * @param bReverse should we search for the tunnel exit in the opposite direction?
       
   280 * @return the exit-tile of the tunnel based on the vehicle's direction
       
   281 * taken from tunnelbridge_cmd.c where the function body was disabled by
       
   282 * #if 1 #else #endif (at r5951). Added bReverse argument to allow two-way
       
   283 * operation (YapfFindNearestRailDepotTwoWay).                              */
       
   284 static TileIndex YapfGetVehicleOutOfTunnelTile(const Vehicle *v, bool bReverse)
       
   285 {
       
   286 	TileIndex tile = v->tile;
       
   287 	DiagDirection dir = DirToDiagDir((Direction)v->direction);
       
   288 	TileIndexDiff delta = TileOffsByDiagDir(dir);
       
   289 	byte z = v->z_pos;
       
   290 
       
   291 	if (bReverse) {
       
   292 		delta = -delta;
       
   293 	} else {
       
   294 		dir = ReverseDiagDir(dir);
       
   295 	}
       
   296 	while (
       
   297 		!IsTunnelTile(tile) ||
       
   298 		GetTunnelDirection(tile) != dir ||
       
   299 		GetTileZ(tile) != z
       
   300 		) {
       
   301 			tile += delta;
       
   302 	}
       
   303 	return tile;
       
   304 }
       
   305 
       
   306 
   273 /** if any track changes, this counter is incremented - that will invalidate segment cost cache */
   307 /** if any track changes, this counter is incremented - that will invalidate segment cost cache */
   274 int CSegmentCostCacheBase::s_rail_change_counter = 0;
   308 int CSegmentCostCacheBase::s_rail_change_counter = 0;
   275 
   309 
   276 void YapfNotifyTrackLayoutChange(TileIndex tile, Track track) {CSegmentCostCacheBase::NotifyTrackLayoutChange(tile, track);}
   310 void YapfNotifyTrackLayoutChange(TileIndex tile, Track track) {CSegmentCostCacheBase::NotifyTrackLayoutChange(tile, track);}