npf.c
changeset 1942 634961366cdc
parent 1941 b1cb02c0401c
child 1944 012fa5e69118
equal deleted inserted replaced
1941:b1cb02c0401c 1942:634961366cdc
     7 #include "pathfind.h"
     7 #include "pathfind.h"
     8 #include "station.h"
     8 #include "station.h"
     9 #include "tile.h"
     9 #include "tile.h"
    10 #include "depot.h"
    10 #include "depot.h"
    11 
    11 
    12 AyStar _train_find_station;
       
    13 AyStar _train_find_depot;
       
    14 AyStar _road_find_station;
       
    15 AyStar _road_find_depot;
       
    16 AyStar _npf_aystar;
    12 AyStar _npf_aystar;
    17 
       
    18 /* Maps a trackdir to the bit that stores its status in the map arrays, in the
       
    19  * direction along with the trackdir */
       
    20 const byte _signal_along_trackdir[14] = {
       
    21 	0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0,
       
    22 	0x40, 0x40, 0x40, 0x10, 0x80, 0x20
       
    23 };
       
    24 
       
    25 /* Maps a trackdir to the bit that stores its status in the map arrays, in the
       
    26  * direction against the trackdir */
       
    27 const byte _signal_against_trackdir[14] = {
       
    28 	0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0,
       
    29 	0x80, 0x80, 0x80, 0x20, 0x40, 0x10
       
    30 };
       
    31 
       
    32 /* Maps a trackdir to the trackdirs that can be reached from it (ie, when
       
    33  * entering the next tile */
       
    34 const uint16 _trackdir_reaches_trackdirs[14] = {
       
    35 	0x1009, 0x0016, 0x1009, 0x0016, 0x0520, 0x0016, 0, 0,
       
    36 	0x0520, 0x2A00, 0x2A00, 0x0520, 0x2A00, 0x1009
       
    37 };
       
    38 
       
    39 const uint16 _next_trackdir[14] = {
       
    40 	0,  1,  3,  2,  5,  4, 0, 0,
       
    41 	8,  9,  11, 10, 13, 12
       
    42 };
       
    43 
       
    44 /* Maps a trackdir to all trackdirs that make 90 deg turns with it. */
       
    45 const uint16 _trackdir_crosses_trackdirs[14] = {
       
    46 	0x0202, 0x0101, 0x3030, 0x3030, 0x0C0C, 0x0C0C, 0, 0,
       
    47 	0x0202, 0x0101, 0x3030, 0x3030, 0x0C0C, 0x0C0C
       
    48 };
       
    49 
       
    50 /* Maps a track to all tracks that make 90 deg turns with it. */
       
    51 const byte _track_crosses_tracks[6] = {
       
    52 	0x2, /* Track 1 -> Track 2 */
       
    53 	0x1, /* Track 2 -> Track 1 */
       
    54 	0x30, /* Upper -> Left | Right */
       
    55 	0x30, /* Lower -> Left | Right */
       
    56 	0x0C, /* Left -> Upper | Lower */
       
    57 	0x0C, /* Right -> Upper | Lower */
       
    58 };
       
    59 
       
    60 /* Maps a trackdir to the (4-way) direction the tile is exited when following
       
    61  * that trackdir */
       
    62 const byte _trackdir_to_exitdir[14] = {
       
    63 	0,1,0,1,2,1, 0,0,
       
    64 	2,3,3,2,3,0,
       
    65 };
       
    66 
       
    67 const byte _track_exitdir_to_trackdir[6][4] = {
       
    68 	{0,    0xff, 8,    0xff},
       
    69 	{0xff, 1,    0xff, 9},
       
    70 	{2,    0xff, 0xff, 10},
       
    71 	{0xff, 3,    11,   0xf},
       
    72 	{0xff, 0xff, 4,    12},
       
    73 	{13,   5,    0xff, 0xff}
       
    74 };
       
    75 
       
    76 const byte _track_direction_to_trackdir[6][8] = {
       
    77 	{0xff, 0,    0xff, 0xff, 0xff, 8,    0xff, 0xff},
       
    78 	{0xff, 0xff, 0xff, 1,    0xff, 0xff, 0xff, 9},
       
    79 	{0xff, 0xff, 2,    0xff, 0xff, 0xff, 10,   0xff},
       
    80 	{0xff, 0xff, 3,    0xff, 0xff, 0xff, 11,   0xff},
       
    81 	{12,   0xff, 0xff, 0xff, 4,    0xff, 0xff, 0xff},
       
    82 	{13,   0xff, 0xff, 0xff, 5,    0xff, 0xff, 0xff}
       
    83 };
       
    84 
       
    85 const byte _dir_to_diag_trackdir[4] = {
       
    86 	0, 1, 8, 9,
       
    87 };
       
    88 
       
    89 const byte _reverse_dir[4] = {
       
    90 	2, 3, 0, 1
       
    91 };
       
    92 
       
    93 const byte _reverse_trackdir[14] = {
       
    94 	8, 9, 10, 11, 12, 13, 0xFF, 0xFF,
       
    95 	0, 1, 2,  3,  4,  5
       
    96 };
       
    97 
    13 
    98 /* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH,
    14 /* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH,
    99  * the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071
    15  * the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071
   100  */
    16  */
   101 #define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH)
    17 #define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH)
   212  * cost of that tile. If the tile is an exit, it will return the tunnel length
   128  * cost of that tile. If the tile is an exit, it will return the tunnel length
   213  * including the exit tile. Requires that this is a Tunnel tile */
   129  * including the exit tile. Requires that this is a Tunnel tile */
   214 uint NPFTunnelCost(AyStarNode* current) {
   130 uint NPFTunnelCost(AyStarNode* current) {
   215 	byte exitdir = _trackdir_to_exitdir[current->direction];
   131 	byte exitdir = _trackdir_to_exitdir[current->direction];
   216 	TileIndex tile = current->tile;
   132 	TileIndex tile = current->tile;
   217 	if ( (uint)(_map5[tile] & 3) == _reverse_dir[exitdir]) {
   133 	if ( (uint)(_map5[tile] & 3) == ReverseDiagdir(exitdir)) {
   218 		/* We just popped out if this tunnel, since were
   134 		/* We just popped out if this tunnel, since were
   219 		 * facing the tunnel exit */
   135 		 * facing the tunnel exit */
   220 		FindLengthOfTunnelResult flotr;
   136 		FindLengthOfTunnelResult flotr;
   221 		flotr = FindLengthOfTunnel(tile, _reverse_dir[exitdir]);
   137 		flotr = FindLengthOfTunnel(tile, ReverseDiagdir(exitdir));
   222 		return flotr.length * NPF_TILE_LENGTH;
   138 		return flotr.length * NPF_TILE_LENGTH;
   223 		//TODO: Penalty for tunnels?
   139 		//TODO: Penalty for tunnels?
   224 	} else {
   140 	} else {
   225 		/* We are entering the tunnel, the enter tile is just a
   141 		/* We are entering the tunnel, the enter tile is just a
   226 		 * straight track */
   142 		 * straight track */
   231 uint NPFSlopeCost(AyStarNode* current) {
   147 uint NPFSlopeCost(AyStarNode* current) {
   232 	TileIndex next = current->tile + TileOffsByDir(_trackdir_to_exitdir[current->direction]);
   148 	TileIndex next = current->tile + TileOffsByDir(_trackdir_to_exitdir[current->direction]);
   233 	int x,y;
   149 	int x,y;
   234 	int8 z1,z2;
   150 	int8 z1,z2;
   235 
   151 
   236 	x = TileX(current->tile) * 16;
   152 	x = TileX(current->tile) * TILE_SIZE;
   237 	y = TileY(current->tile) * 16;
   153 	y = TileY(current->tile) * TILE_SIZE;
   238 	z1 = GetSlopeZ(x+8, y+8);
   154 	/* get the height of the center of the current tile */
   239 
   155 	z1 = GetSlopeZ(x+TILE_HEIGHT, y+TILE_HEIGHT);
   240 	x = TileX(next) * 16;
   156 
   241 	y = TileY(next) * 16;
   157 	x = TileX(next) * TILE_SIZE;
   242 	z2 = GetSlopeZ(x+8, y+8);
   158 	y = TileY(next) * TILE_SIZE;
       
   159 	/* get the height of the center of the next tile */
       
   160 	z2 = GetSlopeZ(x+TILE_HEIGHT, y+TILE_HEIGHT);
   243 
   161 
   244 	if ((z2 - z1) > 1) {
   162 	if ((z2 - z1) > 1) {
   245 		/* Slope up */
   163 		/* Slope up */
   246 		return _patches.npf_rail_slope_penalty;
   164 		return _patches.npf_rail_slope_penalty;
   247 	}
   165 	}
   497 			/* railway tunnel */
   415 			/* railway tunnel */
   498 			if ((_map5[tile] & 0xFC) == 0) type = _map3_lo[tile] & RAILTYPE_MASK;
   416 			if ((_map5[tile] & 0xFC) == 0) type = _map3_lo[tile] & RAILTYPE_MASK;
   499 			/* railway bridge ending */
   417 			/* railway bridge ending */
   500 			if ((_map5[tile] & 0xC6) == 0x80) type = _map3_lo[tile] & RAILTYPE_MASK;
   418 			if ((_map5[tile] & 0xC6) == 0x80) type = _map3_lo[tile] & RAILTYPE_MASK;
   501 			/* on railway bridge */
   419 			/* on railway bridge */
   502 			if ((_map5[tile] & 0xC6) == 0xC0 && (_map5[tile] & 0x1) == (_trackdir_to_exitdir[trackdir] & 0x1))
   420 			if ((_map5[tile] & 0xC6) == 0xC0 && ((unsigned)(_map5[tile] & 0x1)) == (TrackdirToExitdir(trackdir) & 0x1))
   503 				type = (_map3_lo[tile] >> 4) & RAILTYPE_MASK;
   421 				type = (_map3_lo[tile] >> 4) & RAILTYPE_MASK;
   504 			/* under bridge (any type) */
   422 			/* under bridge (any type) */
   505 			if ((_map5[tile] & 0xC0) == 0xC0 && (_map5[tile] & 0x1) != (trackdir & 0x1))
   423 			if ((_map5[tile] & 0xC0) == 0xC0 && (_map5[tile] & 0x1) != (trackdir & 0x1))
   506 				type = _map3_lo[tile] & RAILTYPE_MASK;
   424 				type = _map3_lo[tile] & RAILTYPE_MASK;
   507 			break;
   425 			break;
   552 
   470 
   553 			/* Let's see if were headed the right way into the depot, and reverse
   471 			/* Let's see if were headed the right way into the depot, and reverse
   554 			 * otherwise (only for trains, since only with trains you can
   472 			 * otherwise (only for trains, since only with trains you can
   555 			 * (sometimes) reach tiles after reversing that you couldn't reach
   473 			 * (sometimes) reach tiles after reversing that you couldn't reach
   556 			 * without reversing. */
   474 			 * without reversing. */
   557 			if (src_trackdir == _dir_to_diag_trackdir[_reverse_dir[exitdir]] && type == TRANSPORT_RAIL)
   475 			if (src_trackdir == _dir_to_diag_trackdir[ReverseDiagdir(exitdir)] && type == TRANSPORT_RAIL)
   558 				/* We are headed inwards. We can only reverse here, so we'll not
   476 				/* We are headed inwards. We can only reverse here, so we'll not
   559 				 * consider this direction, but jump ahead to the reverse direction.
   477 				 * consider this direction, but jump ahead to the reverse direction.
   560 				 * It would be nicer to return one neighbour here (the reverse
   478 				 * It would be nicer to return one neighbour here (the reverse
   561 				 * trackdir of the one we are considering now) and then considering
   479 				 * trackdir of the one we are considering now) and then considering
   562 				 * that one to return the tracks outside of the depot. But, because
   480 				 * that one to return the tracks outside of the depot. But, because
   607 			exitdir = GetDepotDirection(dst_tile, type);
   525 			exitdir = GetDepotDirection(dst_tile, type);
   608 		/* Find the trackdirs that are available for a depot or station with this
   526 		/* Find the trackdirs that are available for a depot or station with this
   609 		 * orientation. They are only "inwards", since we are reaching this tile
   527 		 * orientation. They are only "inwards", since we are reaching this tile
   610 		 * from some other tile. This prevents vehicles driving into depots from
   528 		 * from some other tile. This prevents vehicles driving into depots from
   611 		 * the back */
   529 		 * the back */
   612 		ts = (1 << _dir_to_diag_trackdir[_reverse_dir[exitdir]]);
   530 		ts = TrackdirToTrackdirBits(DiagdirToDiagTrackdir(ReverseDiagdir(exitdir)));
   613 	} else {
   531 	} else {
   614 		ts = GetTileTrackStatus(dst_tile, type);
   532 		ts = GetTileTrackStatus(dst_tile, type);
   615 	}
   533 	}
   616 	trackdirs = ts & 0x3F3F; /* Filter out signal status and the unused bits */
   534 	trackdirs = ts & 0x3F3F; /* Filter out signal status and the unused bits */
   617 
   535 
   618 	DEBUG(npf, 4)("Next node: (%d, %d) [%d], possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirs);
   536 	DEBUG(npf, 4)("Next node: (%d, %d) [%d], possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirs);
   619 	/* Select only trackdirs we can reach from our current trackdir */
   537 	/* Select only trackdirs we can reach from our current trackdir */
   620 	trackdirs &= _trackdir_reaches_trackdirs[src_trackdir];
   538 	trackdirs &= TrackdirReachesTrackdirs(src_trackdir);
   621 	if (_patches.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) /* Filter out trackdirs that would make 90 deg turns for trains */
   539 	if (_patches.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) /* Filter out trackdirs that would make 90 deg turns for trains */
   622 		trackdirs &= ~_trackdir_crosses_trackdirs[src_trackdir];
   540 		trackdirs &= ~_trackdir_crosses_trackdirs[src_trackdir];
   623 	DEBUG(npf,6)("After filtering: (%d, %d), possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), trackdirs);
   541 	DEBUG(npf,6)("After filtering: (%d, %d), possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), trackdirs);
   624 
   542 
   625 	/* Enumerate possible track */
   543 	/* Enumerate possible track */
   680 		_npf_aystar.CalculateG = NPFWaterPathCost;
   598 		_npf_aystar.CalculateG = NPFWaterPathCost;
   681 	else
   599 	else
   682 		assert(0);
   600 		assert(0);
   683 
   601 
   684 	/* Initialize Start Node(s) */
   602 	/* Initialize Start Node(s) */
   685 	start1->user_data[NPF_TRACKDIR_CHOICE] = 0xff;
   603 	start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
   686 	start1->user_data[NPF_NODE_FLAGS] = 0;
   604 	start1->user_data[NPF_NODE_FLAGS] = 0;
   687 	_npf_aystar.addstart(&_npf_aystar, start1, 0);
   605 	_npf_aystar.addstart(&_npf_aystar, start1, 0);
   688 	if (start2) {
   606 	if (start2) {
   689 		start2->user_data[NPF_TRACKDIR_CHOICE] = 0xff;
   607 		start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
   690 		start2->user_data[NPF_NODE_FLAGS] = 0;
   608 		start2->user_data[NPF_NODE_FLAGS] = 0;
   691 		NPFSetFlag(start2, NPF_FLAG_REVERSE, true);
   609 		NPFSetFlag(start2, NPF_FLAG_REVERSE, true);
   692 		_npf_aystar.addstart(&_npf_aystar, start2, reverse_penalty);
   610 		_npf_aystar.addstart(&_npf_aystar, start2, reverse_penalty);
   693 	}
   611 	}
   694 
   612 
   695 	/* Initialize result */
   613 	/* Initialize result */
   696 	result.best_bird_dist = (uint)-1;
   614 	result.best_bird_dist = (uint)-1;
   697 	result.best_path_dist = (uint)-1;
   615 	result.best_path_dist = (uint)-1;
   698 	result.best_trackdir = 0xff;
   616 	result.best_trackdir = INVALID_TRACKDIR;
   699 	_npf_aystar.user_path = &result;
   617 	_npf_aystar.user_path = &result;
   700 
   618 
   701 	/* Initialize target */
   619 	/* Initialize target */
   702 	_npf_aystar.user_target = target;
   620 	_npf_aystar.user_target = target;
   703 
   621 
   719 
   637 
   720 	}
   638 	}
   721 	return result;
   639 	return result;
   722 }
   640 }
   723 
   641 
   724 NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, byte trackdir1, TileIndex tile2, byte trackdir2, NPFFindStationOrTileData* target, TransportType type, Owner owner) {
   642 NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, Owner owner) {
   725 	AyStarNode start1;
   643 	AyStarNode start1;
   726 	AyStarNode start2;
   644 	AyStarNode start2;
   727 
   645 
   728 	start1.tile = tile1;
   646 	start1.tile = tile1;
   729 	start2.tile = tile2;
   647 	start2.tile = tile2;
   730 	/* We set this in case the target is also the start tile, we will just
   648 	/* We set this in case the target is also the start tile, we will just
   731 	 * return a not found then */
   649 	 * return a not found then */
   732 	start1.user_data[NPF_TRACKDIR_CHOICE] = 0xff;
   650 	start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
   733 	start1.direction = trackdir1;
   651 	start1.direction = trackdir1;
   734 	start2.direction = trackdir2;
   652 	start2.direction = trackdir2;
   735 	start2.user_data[NPF_TRACKDIR_CHOICE] = 0xff;
   653 	start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
   736 
   654 
   737 	return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, owner, 0);
   655 	return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, owner, 0);
   738 }
   656 }
   739 
   657 
   740 NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, byte trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner) {
   658 NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner) {
   741 	return NPFRouteToStationOrTileTwoWay(tile, trackdir, INVALID_TILE, 0, target, type, owner);
   659 	return NPFRouteToStationOrTileTwoWay(tile, trackdir, INVALID_TILE, 0, target, type, owner);
   742 }
   660 }
   743 
   661 
   744 NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, byte trackdir1, TileIndex tile2, byte trackdir2, TransportType type, Owner owner, uint reverse_penalty) {
   662 NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, Owner owner, uint reverse_penalty) {
   745 	AyStarNode start1;
   663 	AyStarNode start1;
   746 	AyStarNode start2;
   664 	AyStarNode start2;
   747 
   665 
   748 	start1.tile = tile1;
   666 	start1.tile = tile1;
   749 	start2.tile = tile2;
   667 	start2.tile = tile2;
   750 	/* We set this in case the target is also the start tile, we will just
   668 	/* We set this in case the target is also the start tile, we will just
   751 	 * return a not found then */
   669 	 * return a not found then */
   752 	start1.user_data[NPF_TRACKDIR_CHOICE] = 0xff;
   670 	start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
   753 	start1.direction = trackdir1;
   671 	start1.direction = trackdir1;
   754 	start2.direction = trackdir2;
   672 	start2.direction = trackdir2;
   755 	start2.user_data[NPF_TRACKDIR_CHOICE] = 0xff;
   673 	start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
   756 
   674 
   757 	/* perform a breadth first search. Target is NULL,
   675 	/* perform a breadth first search. Target is NULL,
   758 	 * since we are just looking for any depot...*/
   676 	 * since we are just looking for any depot...*/
   759 	return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), NULL, NPFFindDepot, NPFCalcZero, type, owner, reverse_penalty);
   677 	return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), NULL, NPFFindDepot, NPFCalcZero, type, owner, reverse_penalty);
   760 }
   678 }
   761 
   679 
   762 NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, byte trackdir, TransportType type, Owner owner) {
   680 NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, Owner owner) {
   763 	return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, INVALID_TILE, 0, type, owner, 0);
   681 	return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, INVALID_TILE, 0, type, owner, 0);
   764 }
   682 }
   765 
   683 
   766 NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, byte trackdir, TransportType type, Owner owner) {
   684 NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, Owner owner) {
   767 	/* Okay, what we're gonna do. First, we look at all depots, calculate
   685 	/* Okay, what we're gonna do. First, we look at all depots, calculate
   768 	 * the manhatten distance to get to each depot. We then sort them by
   686 	 * the manhatten distance to get to each depot. We then sort them by
   769 	 * distance. We start by trying to plan a route to the closest, then
   687 	 * distance. We start by trying to plan a route to the closest, then
   770 	 * the next closest, etc. We stop when the best route we have found so
   688 	 * the next closest, etc. We stop when the best route we have found so
   771 	 * far, is shorter than the manhattan distance. This will obviously
   689 	 * far, is shorter than the manhattan distance. This will obviously
   834 			break;
   752 			break;
   835 
   753 
   836 		/* Initialize Start Node */
   754 		/* Initialize Start Node */
   837 		/* We set this in case the target is also the start tile, we will just
   755 		/* We set this in case the target is also the start tile, we will just
   838 		 * return a not found then */
   756 		 * return a not found then */
   839 		start.user_data[NPF_TRACKDIR_CHOICE] = 0xff;
   757 		start.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
   840 		start.user_data[NPF_NODE_FLAGS] = 0;
   758 		start.user_data[NPF_NODE_FLAGS] = 0;
   841 		_npf_aystar.addstart(&_npf_aystar, &start, 0);
   759 		_npf_aystar.addstart(&_npf_aystar, &start, 0);
   842 
   760 
   843 		/* Initialize result */
   761 		/* Initialize result */
   844 		result.best_bird_dist = (uint)-1;
   762 		result.best_bird_dist = (uint)-1;
   845 		result.best_path_dist = (uint)-1;
   763 		result.best_path_dist = (uint)-1;
   846 		result.best_trackdir = 0xff;
   764 		result.best_trackdir = INVALID_TRACKDIR;
   847 
   765 
   848 		/* Initialize target */
   766 		/* Initialize target */
   849 		target.dest_coords = current->xy;
   767 		target.dest_coords = current->xy;
   850 
   768 
   851 		/* GO! */
   769 		/* GO! */
   869 	_npf_aystar.max_path_cost = 0;
   787 	_npf_aystar.max_path_cost = 0;
   870 	//_npf_aystar.max_search_nodes = 0;
   788 	//_npf_aystar.max_search_nodes = 0;
   871 	/* We will limit the number of nodes for now, until we have a better
   789 	/* We will limit the number of nodes for now, until we have a better
   872 	 * solution to really fix performance */
   790 	 * solution to really fix performance */
   873 	_npf_aystar.max_search_nodes = _patches.npf_max_search_nodes;
   791 	_npf_aystar.max_search_nodes = _patches.npf_max_search_nodes;
   874 #if 0
       
   875 	init_AyStar(&_train_find_station, NTPHash, 1024);
       
   876 	init_AyStar(&_train_find_depot, NTPHash, 1024);
       
   877 	init_AyStar(&_road_find_station, NTPHash, 1024);
       
   878 	init_AyStar(&_road_find_depot, NTPHash, 1024);
       
   879 
       
   880 	_train_find_station.loops_per_tick = 0;
       
   881 	_train_find_depot.loops_per_tick = 0;
       
   882 	_road_find_station.loops_per_tick = 0;
       
   883 	_road_find_depot.loops_per_tick = 0;
       
   884 
       
   885 	_train_find_station.max_path_cost = 0;
       
   886 	_train_find_depot.max_path_cost = 0;
       
   887 	_road_find_station.max_path_cost = 0;
       
   888 	_road_find_depot.max_path_cost = 0;
       
   889 
       
   890 	_train_find_station.max_search_nodes = 0;
       
   891 	_train_find_depot.max_search_nodes = 0;
       
   892 	_road_find_station.max_search_nodes = 0;
       
   893 	_road_find_depot.max_search_nodes = 0;
       
   894 
       
   895 	_train_find_station.CalculateG = NPFRailPathCost;
       
   896 	_train_find_depot.CalculateG = NPFRailPathCost;
       
   897 	_road_find_station.CalculateG = NPFRoadPathCost;
       
   898 	_road_find_depot.CalculateG = NPFRoadPathCost;
       
   899 
       
   900 	_train_find_station.CalculateH = NPFCalcStationHeuristic;
       
   901 	_train_find_depot.CalculateH = NPFCalcStationHeuristic;
       
   902 	_road_find_station.CalculateH = NPFCalcStationHeuristic;
       
   903 	_road_find_depot.CalculateH = NPFCalcStationHeuristic;
       
   904 
       
   905 	_train_find_station.EndNodeCheck = NPFFindStationOrTile;
       
   906 	_train_find_depot.EndNodeCheck = NPFFindStationOrTile;
       
   907 	_road_find_station.EndNodeCheck = NPFFindStationOrTile;
       
   908 	_road_find_depot.EndNodeCheck = NPFFindStationOrTile;
       
   909 
       
   910 	_train_find_station.FoundEndNode = NPFSaveTargetData;
       
   911 	_train_find_depot.FoundEndNode = NPFSaveTargetData;
       
   912 	_road_find_station.FoundEndNode = NPFSaveTargetData;
       
   913 	_road_find_depot.FoundEndNode = NPFSaveTargetData;
       
   914 
       
   915 	_train_find_station.GetNeighbours = NPFFollowTrack;
       
   916 	_train_find_depot.GetNeighbours = NPFFollowTrack;
       
   917 	_road_find_station.GetNeighbours = NPFFollowTrack;
       
   918 	_road_find_depot.GetNeighbours = NPFFollowTrack;
       
   919 #endif
       
   920 }
   792 }
   921 
   793 
   922 void NPFFillWithOrderData(NPFFindStationOrTileData* fstd, Vehicle* v) {
   794 void NPFFillWithOrderData(NPFFindStationOrTileData* fstd, Vehicle* v) {
   923 	/* Ships don't really reach their stations, but the tile in front. So don't
   795 	/* Ships don't really reach their stations, but the tile in front. So don't
   924 	 * save the station id for ships. For roadvehs we don't store it either,
   796 	 * save the station id for ships. For roadvehs we don't store it either,