src/pathfind.cpp
branchNewGRF_ports
changeset 6872 1c4a4a609f85
parent 6871 5a9dc001e1ad
child 6877 889301acc299
equal deleted inserted replaced
6871:5a9dc001e1ad 6872:1c4a4a609f85
     5 #include "stdafx.h"
     5 #include "stdafx.h"
     6 #include "openttd.h"
     6 #include "openttd.h"
     7 #include "bridge_map.h"
     7 #include "bridge_map.h"
     8 #include "station_map.h"
     8 #include "station_map.h"
     9 #include "depot.h"
     9 #include "depot.h"
    10 #include "functions.h"
    10 #include "tile_cmd.h"
    11 #include "landscape.h"
    11 #include "landscape.h"
    12 #include "map.h"
       
    13 #include "tile.h"
       
    14 #include "pathfind.h"
    12 #include "pathfind.h"
    15 #include "rail.h"
    13 #include "rail_type.h"
    16 #include "debug.h"
    14 #include "debug.h"
    17 #include "tunnel_map.h"
    15 #include "tunnel_map.h"
    18 #include "variables.h"
    16 #include "settings_type.h"
    19 #include "depot.h"
    17 #include "depot.h"
       
    18 #include "tunnelbridge_map.h"
       
    19 #include "core/random_func.hpp"
    20 
    20 
    21 /* remember which tiles we have already visited so we don't visit them again. */
    21 /* remember which tiles we have already visited so we don't visit them again. */
    22 static bool TPFSetTileBit(TrackPathFinder *tpf, TileIndex tile, int dir)
    22 static bool TPFSetTileBit(TrackPathFinder *tpf, TileIndex tile, int dir)
    23 {
    23 {
    24 	uint hash, val, offs;
    24 	uint hash, val, offs;
   208 	do {
   208 	do {
   209 		flotr.length++;
   209 		flotr.length++;
   210 		tile += delta;
   210 		tile += delta;
   211 	} while(
   211 	} while(
   212 		!IsTunnelTile(tile) ||
   212 		!IsTunnelTile(tile) ||
   213 		GetTunnelDirection(tile) != dir ||
   213 		GetTunnelBridgeDirection(tile) != dir ||
   214 		GetTileZ(tile) != z
   214 		GetTileZ(tile) != z
   215 	);
   215 	);
   216 
   216 
   217 	flotr.tile = tile;
   217 	flotr.tile = tile;
   218 	return flotr;
   218 	return flotr;
   257 	}
   257 	}
   258 
   258 
   259 	/* Check if the new tile is a tunnel or bridge head and that the direction
   259 	/* Check if the new tile is a tunnel or bridge head and that the direction
   260 	 * and transport type match */
   260 	 * and transport type match */
   261 	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
   261 	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
   262 		if (IsTunnel(tile)) {
   262 		if (GetTunnelBridgeDirection(tile) != direction ||
   263 			if (GetTunnelDirection(tile) != direction ||
   263 				GetTunnelBridgeTransportType(tile) != tpf->tracktype) {
   264 					GetTunnelTransportType(tile) != tpf->tracktype) {
   264 			return;
   265 				return;
       
   266 			}
       
   267 		} else if (IsBridge(tile)) {
       
   268 			if (GetBridgeRampDirection(tile) != direction ||
       
   269 					GetBridgeTransportType(tile) != tpf->tracktype) {
       
   270 				return;
       
   271 			}
       
   272 		}
   265 		}
   273 	}
   266 	}
   274 
   267 
   275 	tpf->rd.cur_length++;
   268 	tpf->rd.cur_length++;
   276 
   269 
   305 {
   298 {
   306 	TileIndex tile_org = tile;
   299 	TileIndex tile_org = tile;
   307 
   300 
   308 	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
   301 	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
   309 		if (IsTunnel(tile)) {
   302 		if (IsTunnel(tile)) {
   310 			if (GetTunnelTransportType(tile) != tpf->tracktype) {
   303 			if (GetTunnelBridgeTransportType(tile) != tpf->tracktype) {
   311 				return;
   304 				return;
   312 			}
   305 			}
   313 			/* Only skip through the tunnel if heading inwards. We can
   306 			/* Only skip through the tunnel if heading inwards. We can
   314 			 * be headed outwards if our starting position was in a
   307 			 * be headed outwards if our starting position was in a
   315 			 * tunnel and we're pathfinding backwards */
   308 			 * tunnel and we're pathfinding backwards */
   316 			if (GetTunnelDirection(tile) == direction) {
   309 			if (GetTunnelBridgeDirection(tile) == direction) {
   317 				tile = SkipToEndOfTunnel(tpf, tile, direction);
   310 				tile = SkipToEndOfTunnel(tpf, tile, direction);
   318 			} else if (GetTunnelDirection(tile) != ReverseDiagDir(direction)) {
   311 			} else if (GetTunnelBridgeDirection(tile) != ReverseDiagDir(direction)) {
   319 				/* We don't support moving through the sides of a tunnel
   312 				/* We don't support moving through the sides of a tunnel
   320 				 * entrance :-) */
   313 				 * entrance :-) */
   321 				return;
   314 				return;
   322 			}
   315 			}
   323 		} else {
   316 		} else {
   324 			TileIndex tile_end;
   317 			TileIndex tile_end;
   325 			if (GetBridgeRampDirection(tile) != direction ||
   318 			if (GetTunnelBridgeDirection(tile) != direction ||
   326 					GetBridgeTransportType(tile) != tpf->tracktype) {
   319 					GetTunnelBridgeTransportType(tile) != tpf->tracktype) {
   327 				return;
   320 				return;
   328 			}
   321 			}
   329 			//fprintf(stderr, "%s: Planning over bridge\n", __func__);
   322 			//fprintf(stderr, "%s: Planning over bridge\n", __func__);
   330 			// TODO doesn't work - WHAT doesn't work?
   323 			// TODO doesn't work - WHAT doesn't work?
   331 			TPFSetTileBit(tpf, tile, 14);
   324 			TPFSetTileBit(tpf, tile, 14);
   447 	NTPEnumProc *enum_proc;
   440 	NTPEnumProc *enum_proc;
   448 	void *userdata;
   441 	void *userdata;
   449 	TileIndex dest;
   442 	TileIndex dest;
   450 
   443 
   451 	TransportType tracktype;
   444 	TransportType tracktype;
   452 	RailTypeMask railtypes;
   445 	RailTypes railtypes;
   453 	uint maxlength;
   446 	uint maxlength;
   454 
   447 
   455 	HashLink *new_link;
   448 	HashLink *new_link;
   456 	uint num_links_left;
   449 	uint num_links_left;
   457 
   450 
   720 start_at:
   713 start_at:
   721 		/* If the tile is the entry tile of a tunnel, and we're not going out of the tunnel,
   714 		/* If the tile is the entry tile of a tunnel, and we're not going out of the tunnel,
   722 		 *   need to find the exit of the tunnel. */
   715 		 *   need to find the exit of the tunnel. */
   723 		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
   716 		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
   724 			if (IsTunnel(tile)) {
   717 			if (IsTunnel(tile)) {
   725 				if (GetTunnelDirection(tile) != ReverseDiagDir(direction)) {
   718 				if (GetTunnelBridgeDirection(tile) != ReverseDiagDir(direction)) {
   726 					FindLengthOfTunnelResult flotr;
   719 					FindLengthOfTunnelResult flotr;
   727 
   720 
   728 					/* We are not just driving out of the tunnel */
   721 					/* We are not just driving out of the tunnel */
   729 					if (GetTunnelDirection(tile) != direction ||
   722 					if (GetTunnelBridgeDirection(tile) != direction ||
   730 							GetTunnelTransportType(tile) != tpf->tracktype) {
   723 							GetTunnelBridgeTransportType(tile) != tpf->tracktype) {
   731 						/* We are not driving into the tunnel, or it is an invalid tunnel */
   724 						/* We are not driving into the tunnel, or it is an invalid tunnel */
   732 						continue;
   725 						continue;
   733 					}
   726 					}
   734 					if (!HasBit(tpf->railtypes, GetRailType(tile))) {
   727 					if (!HasBit(tpf->railtypes, GetRailType(tile))) {
   735 						bits = TRACK_BIT_NONE;
   728 						bits = TRACK_BIT_NONE;
   740 					tile = flotr.tile;
   733 					tile = flotr.tile;
   741 					/* tile now points to the exit tile of the tunnel */
   734 					/* tile now points to the exit tile of the tunnel */
   742 				}
   735 				}
   743 			} else {
   736 			} else {
   744 				TileIndex tile_end;
   737 				TileIndex tile_end;
   745 				if (GetBridgeRampDirection(tile) != ReverseDiagDir(direction)) {
   738 				if (GetTunnelBridgeDirection(tile) != ReverseDiagDir(direction)) {
   746 					/* We are not just leaving the bridge */
   739 					/* We are not just leaving the bridge */
   747 					if (GetBridgeRampDirection(tile) != direction ||
   740 					if (GetTunnelBridgeDirection(tile) != direction ||
   748 							GetBridgeTransportType(tile) != tpf->tracktype) {
   741 							GetTunnelBridgeTransportType(tile) != tpf->tracktype) {
   749 						/* Not entering the bridge or not compatible */
   742 						/* Not entering the bridge or not compatible */
   750 						continue;
   743 						continue;
   751 					}
   744 					}
   752 				}
   745 				}
   753 				tile_end = GetOtherBridgeEnd(tile);
   746 				tile_end = GetOtherBridgeEnd(tile);
   943 	}
   936 	}
   944 }
   937 }
   945 
   938 
   946 
   939 
   947 /** new pathfinder for trains. better and faster. */
   940 /** new pathfinder for trains. better and faster. */
   948 void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypeMask railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data)
   941 void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypes railtypes, DiagDirection direction, NTPEnumProc* enum_proc, void* data)
   949 {
   942 {
   950 	NewTrackPathFinder tpf;
   943 	NewTrackPathFinder tpf;
   951 
   944 
   952 	tpf.dest = dest;
   945 	tpf.dest = dest;
   953 	tpf.userdata = data;
   946 	tpf.userdata = data;