src/npf.cpp
branchnoai
changeset 9624 b71483f2330f
parent 9599 949374e83b78
child 7266 4b75e7b9fa91
equal deleted inserted replaced
9623:ee0173281563 9624:b71483f2330f
   521 	TileIndex dst_tile = INVALID_TILE;
   521 	TileIndex dst_tile = INVALID_TILE;
   522 	int i;
   522 	int i;
   523 	uint32 ts;
   523 	uint32 ts;
   524 	TrackdirBits trackdirbits;
   524 	TrackdirBits trackdirbits;
   525 	TransportType type = (TransportType)aystar->user_data[NPF_TYPE];
   525 	TransportType type = (TransportType)aystar->user_data[NPF_TYPE];
       
   526 	uint subtype = aystar->user_data[NPF_SUB_TYPE];
   526 	bool override_dst_check = false;
   527 	bool override_dst_check = false;
   527 	/* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */
   528 	/* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */
   528 	aystar->num_neighbours = 0;
   529 	aystar->num_neighbours = 0;
   529 	DEBUG(npf, 4, "Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
   530 	DEBUG(npf, 4, "Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
   530 
   531 
   620 		 * orientation. They are only "inwards", since we are reaching this tile
   621 		 * orientation. They are only "inwards", since we are reaching this tile
   621 		 * from some other tile. This prevents vehicles driving into depots from
   622 		 * from some other tile. This prevents vehicles driving into depots from
   622 		 * the back */
   623 		 * the back */
   623 		ts = TrackdirToTrackdirBits(DiagdirToDiagTrackdir(ReverseDiagDir(exitdir)));
   624 		ts = TrackdirToTrackdirBits(DiagdirToDiagTrackdir(ReverseDiagDir(exitdir)));
   624 	} else {
   625 	} else {
   625 		ts = GetTileTrackStatus(dst_tile, type);
   626 		ts = GetTileTrackStatus(dst_tile, type, subtype);
   626 	}
   627 	}
   627 	trackdirbits = (TrackdirBits)(ts & TRACKDIR_BIT_MASK); /* Filter out signal status and the unused bits */
   628 	trackdirbits = (TrackdirBits)(ts & TRACKDIR_BIT_MASK); /* Filter out signal status and the unused bits */
   628 
   629 
   629 	DEBUG(npf, 4, "Next node: (%d, %d) [%d], possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits);
   630 	DEBUG(npf, 4, "Next node: (%d, %d) [%d], possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits);
   630 	/* Select only trackdirs we can reach from our current trackdir */
   631 	/* Select only trackdirs we can reach from our current trackdir */
   668  * When we are looking for one specific target (optionally multiple tiles), we
   669  * When we are looking for one specific target (optionally multiple tiles), we
   669  * should use a good heuristic to perform aystar search. When we search for
   670  * should use a good heuristic to perform aystar search. When we search for
   670  * multiple targets that are spread around, we should perform a breadth first
   671  * multiple targets that are spread around, we should perform a breadth first
   671  * search by specifiying CalcZero as our heuristic.
   672  * search by specifiying CalcZero as our heuristic.
   672  */
   673  */
   673 static NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, Owner owner, RailTypeMask railtypes, uint reverse_penalty)
   674 static NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes, uint reverse_penalty)
   674 {
   675 {
   675 	int r;
   676 	int r;
   676 	NPFFoundTargetData result;
   677 	NPFFoundTargetData result;
   677 
   678 
   678 	/* Initialize procs */
   679 	/* Initialize procs */
   707 	/* Initialize target */
   708 	/* Initialize target */
   708 	_npf_aystar.user_target = target;
   709 	_npf_aystar.user_target = target;
   709 
   710 
   710 	/* Initialize user_data */
   711 	/* Initialize user_data */
   711 	_npf_aystar.user_data[NPF_TYPE] = type;
   712 	_npf_aystar.user_data[NPF_TYPE] = type;
       
   713 	_npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
   712 	_npf_aystar.user_data[NPF_OWNER] = owner;
   714 	_npf_aystar.user_data[NPF_OWNER] = owner;
   713 	_npf_aystar.user_data[NPF_RAILTYPES] = railtypes;
   715 	_npf_aystar.user_data[NPF_RAILTYPES] = railtypes;
   714 
   716 
   715 	/* GO! */
   717 	/* GO! */
   716 	r = AyStarMain_Main(&_npf_aystar);
   718 	r = AyStarMain_Main(&_npf_aystar);
   726 
   728 
   727 	}
   729 	}
   728 	return result;
   730 	return result;
   729 }
   731 }
   730 
   732 
   731 NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailTypeMask railtypes)
   733 NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes)
   732 {
   734 {
   733 	AyStarNode start1;
   735 	AyStarNode start1;
   734 	AyStarNode start2;
   736 	AyStarNode start2;
   735 
   737 
   736 	start1.tile = tile1;
   738 	start1.tile = tile1;
   740 	start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
   742 	start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
   741 	start1.direction = trackdir1;
   743 	start1.direction = trackdir1;
   742 	start2.direction = trackdir2;
   744 	start2.direction = trackdir2;
   743 	start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
   745 	start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
   744 
   746 
   745 	return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, owner, railtypes, 0);
   747 	return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, sub_type, owner, railtypes, 0);
   746 }
   748 }
   747 
   749 
   748 NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailTypeMask railtypes)
   750 NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes)
   749 {
   751 {
   750 	return NPFRouteToStationOrTileTwoWay(tile, trackdir, INVALID_TILE, INVALID_TRACKDIR, target, type, owner, railtypes);
   752 	return NPFRouteToStationOrTileTwoWay(tile, trackdir, INVALID_TILE, INVALID_TRACKDIR, target, type, sub_type, owner, railtypes);
   751 }
   753 }
   752 
   754 
   753 NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, Owner owner, RailTypeMask railtypes, uint reverse_penalty)
   755 NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes, uint reverse_penalty)
   754 {
   756 {
   755 	AyStarNode start1;
   757 	AyStarNode start1;
   756 	AyStarNode start2;
   758 	AyStarNode start2;
   757 
   759 
   758 	start1.tile = tile1;
   760 	start1.tile = tile1;
   764 	start2.direction = trackdir2;
   766 	start2.direction = trackdir2;
   765 	start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
   767 	start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
   766 
   768 
   767 	/* perform a breadth first search. Target is NULL,
   769 	/* perform a breadth first search. Target is NULL,
   768 	 * since we are just looking for any depot...*/
   770 	 * since we are just looking for any depot...*/
   769 	return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), NULL, NPFFindDepot, NPFCalcZero, type, owner, railtypes, reverse_penalty);
   771 	return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), NULL, NPFFindDepot, NPFCalcZero, type, sub_type, owner, railtypes, reverse_penalty);
   770 }
   772 }
   771 
   773 
   772 NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, Owner owner, RailTypeMask railtypes)
   774 NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes)
   773 {
   775 {
   774 	return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, INVALID_TILE, INVALID_TRACKDIR, type, owner, railtypes, 0);
   776 	return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, INVALID_TILE, INVALID_TRACKDIR, type, sub_type, owner, railtypes, 0);
   775 }
   777 }
   776 
   778 
   777 NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, Owner owner, RailTypeMask railtypes)
   779 NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes)
   778 {
   780 {
   779 	/* Okay, what we're gonna do. First, we look at all depots, calculate
   781 	/* Okay, what we're gonna do. First, we look at all depots, calculate
   780 	 * the manhatten distance to get to each depot. We then sort them by
   782 	 * the manhatten distance to get to each depot. We then sort them by
   781 	 * distance. We start by trying to plan a route to the closest, then
   783 	 * distance. We start by trying to plan a route to the closest, then
   782 	 * the next closest, etc. We stop when the best route we have found so
   784 	 * the next closest, etc. We stop when the best route we have found so
   821 	target.station_index = INVALID_STATION; /* We will initialize dest_coords inside the loop below */
   823 	target.station_index = INVALID_STATION; /* We will initialize dest_coords inside the loop below */
   822 	_npf_aystar.user_target = ⌖
   824 	_npf_aystar.user_target = ⌖
   823 
   825 
   824 	/* Initialize user_data */
   826 	/* Initialize user_data */
   825 	_npf_aystar.user_data[NPF_TYPE] = type;
   827 	_npf_aystar.user_data[NPF_TYPE] = type;
       
   828 	_npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
   826 	_npf_aystar.user_data[NPF_OWNER] = owner;
   829 	_npf_aystar.user_data[NPF_OWNER] = owner;
   827 
   830 
   828 	/* Initialize Start Node */
   831 	/* Initialize Start Node */
   829 	start.tile = tile;
   832 	start.tile = tile;
   830 	start.direction = trackdir; /* We will initialize user_data inside the loop below */
   833 	start.direction = trackdir; /* We will initialize user_data inside the loop below */