yapf/yapf_costrail.hpp
branch0.5
changeset 5445 5af942067209
parent 5378 5b12ac7c90dd
child 5530 15f701e93291
equal deleted inserted replaced
5444:3209bb62403e 5445:5af942067209
    62 		}
    62 		}
    63 		return cost;
    63 		return cost;
    64 	}
    64 	}
    65 
    65 
    66 	/** return one tile cost. If tile is a tunnel entry, it is moved to the end of tunnel */
    66 	/** return one tile cost. If tile is a tunnel entry, it is moved to the end of tunnel */
    67 	FORCEINLINE int OneTileCost(TileIndex& tile, Trackdir trackdir)
    67 	FORCEINLINE int OneTileCost(TileIndex prev_tile, TileIndex& tile, Trackdir trackdir)
    68 	{
    68 	{
    69 		int cost = 0;
    69 		int cost = 0;
    70 		// set base cost
    70 		// set base cost
    71 		if (IsDiagonalTrackdir(trackdir)) {
    71 		if (IsDiagonalTrackdir(trackdir)) {
    72 			cost += YAPF_TILE_LENGTH;
    72 			cost += YAPF_TILE_LENGTH;
    73 			switch (GetTileType(tile)) {
    73 			switch (GetTileType(tile)) {
    74 				case MP_STREET:
    74 				case MP_STREET:
    75 					/* Increase the cost for level crossings */
    75 					/* Increase the cost for level crossings */
    76 					if (IsLevelCrossing(tile))
    76 					if (IsLevelCrossing(tile))
    77 						cost += Yapf().PfGetSettings().rail_crossing_penalty;
    77 						cost += Yapf().PfGetSettings().rail_crossing_penalty;
    78 					break;
       
    79 
       
    80 				case MP_STATION:
       
    81 					// penalty for passing station tiles
       
    82 					cost += Yapf().PfGetSettings().rail_station_penalty;
       
    83 					break;
    78 					break;
    84 
    79 
    85 				default:
    80 				default:
    86 					break;
    81 					break;
    87 			}
    82 			}
   174 	FORCEINLINE void SetMaxCost(int max_cost) {m_max_cost = max_cost;}
   169 	FORCEINLINE void SetMaxCost(int max_cost) {m_max_cost = max_cost;}
   175 
   170 
   176 	/** Called by YAPF to calculate the cost from the origin to the given node.
   171 	/** Called by YAPF to calculate the cost from the origin to the given node.
   177 	 *  Calculates only the cost of given node, adds it to the parent node cost
   172 	 *  Calculates only the cost of given node, adds it to the parent node cost
   178 	 *  and stores the result into Node::m_cost member */
   173 	 *  and stores the result into Node::m_cost member */
   179 	FORCEINLINE bool PfCalcCost(Node& n)
   174 	FORCEINLINE bool PfCalcCost(Node &n, const TrackFollower &tf)
   180 	{
   175 	{
   181 		assert(!n.flags_u.flags_s.m_targed_seen);
   176 		assert(!n.flags_u.flags_s.m_targed_seen);
   182 		CPerfStart perf_cost(Yapf().m_perf_cost);
   177 		CPerfStart perf_cost(Yapf().m_perf_cost);
   183 		int parent_cost = (n.m_parent != NULL) ? n.m_parent->m_cost : 0;
   178 		int parent_cost = (n.m_parent != NULL) ? n.m_parent->m_cost : 0;
   184 		int first_tile_cost = 0;
   179 		int first_tile_cost = 0;
   197 
   192 
   198 		RailType rail_type = GetTileRailType(tile, trackdir);
   193 		RailType rail_type = GetTileRailType(tile, trackdir);
   199 
   194 
   200 		bool target_seen = Yapf().PfDetectDestination(tile, trackdir);
   195 		bool target_seen = Yapf().PfDetectDestination(tile, trackdir);
   201 
   196 
       
   197 		if (tf.m_is_station) {
       
   198 			// station tiles have an extra penalty
       
   199 			segment_cost += Yapf().PfGetSettings().rail_station_penalty * (tf.m_tiles_skipped + 1);
       
   200 		}
       
   201 
   202 		while (true) {
   202 		while (true) {
   203 			segment_cost += Yapf().OneTileCost(tile, trackdir);
   203 			segment_cost += Yapf().OneTileCost(prev_tile, tile, trackdir);
   204 			segment_cost += Yapf().CurveCost(prev_trackdir, trackdir);
   204 			segment_cost += Yapf().CurveCost(prev_trackdir, trackdir);
   205 			segment_cost += Yapf().SlopeCost(tile, trackdir);
   205 			segment_cost += Yapf().SlopeCost(tile, trackdir);
   206 			segment_cost += Yapf().SignalCost(n, tile, trackdir);
   206 			segment_cost += Yapf().SignalCost(n, tile, trackdir);
   207 			if (n.m_segment->flags_u.flags_s.m_end_of_line) {
   207 			if (n.m_segment->flags_u.flags_s.m_end_of_line) {
   208 				break;
   208 				break;
   280 			segment_cost += YAPF_TILE_LENGTH * F.m_tiles_skipped;
   280 			segment_cost += YAPF_TILE_LENGTH * F.m_tiles_skipped;
   281 
   281 
   282 			// add penalty for skipped station tiles
   282 			// add penalty for skipped station tiles
   283 			if (F.m_is_station)
   283 			if (F.m_is_station)
   284 			{
   284 			{
       
   285 				uint platform_length = F.m_tiles_skipped + 1;
   285 				if (target_seen) {
   286 				if (target_seen) {
   286 					// it is our destination station
   287 					// it is our destination station
   287 					uint platform_length = F.m_tiles_skipped + 1;
       
   288 					segment_cost += PlatformLengthPenalty(platform_length);
   288 					segment_cost += PlatformLengthPenalty(platform_length);
   289 				} else {
   289 				} else {
   290 					// station is not our destination station, apply penalty for skipped platform tiles
   290 					// station is not our destination station, apply penalty for skipped platform tiles
   291 					segment_cost += Yapf().PfGetSettings().rail_station_penalty * F.m_tiles_skipped;
   291 					segment_cost += Yapf().PfGetSettings().rail_station_penalty * platform_length;
   292 				}
   292 				}
   293 			}
   293 			}
   294 
   294 
   295 			// add min/max speed penalties
   295 			// add min/max speed penalties
   296 			int min_speed = 0;
   296 			int min_speed = 0;