pathfind.c
changeset 2136 c87ed6a3c15f
parent 2125 edc17858f9f6
child 2137 bbf04957feec
equal deleted inserted replaced
2135:08120c3cf43e 2136:c87ed6a3c15f
   662 	uint bits, tile_org, track;
   662 	uint bits, tile_org, track;
   663 	StackedItem si;
   663 	StackedItem si;
   664 	FindLengthOfTunnelResult flotr;
   664 	FindLengthOfTunnelResult flotr;
   665 	int estimation;
   665 	int estimation;
   666 
   666 
       
   667 	
       
   668 
   667 	// Need to have a special case for the start.
   669 	// Need to have a special case for the start.
   668 	// We shouldn't call the callback for the current tile.
   670 	// We shouldn't call the callback for the current tile.
   669 	si.cur_length = 1; // Need to start at 1 cause 0 is a reserved value.
   671 	si.cur_length = 1; // Need to start at 1 cause 0 is a reserved value.
   670 	si.depth = 0;
   672 	si.depth = 0;
   671 	si.state = 0;
   673 	si.state = 0;
       
   674 	si.first_track = 0xFF;
   672 	goto start_at;
   675 	goto start_at;
   673 
   676 
   674 	for(;;) {
   677 	for(;;) {
   675 		// Get the next item to search from from the priority queue
   678 		// Get the next item to search from from the priority queue
   676 		do {
   679 		do {
   688 
   691 
   689 callback_and_continue:
   692 callback_and_continue:
   690 		if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length))
   693 		if (tpf->enum_proc(tile, tpf->userdata, si.first_track, si.cur_length))
   691 			return;
   694 			return;
   692 
   695 
       
   696 		assert(si.track <= 13);
   693 		direction = _tpf_new_direction[si.track];
   697 		direction = _tpf_new_direction[si.track];
       
   698 		assert(direction <= 3);
   694 
   699 
   695 start_at:
   700 start_at:
   696 		// If the tile is the entry tile of a tunnel, and we're not going out of the tunnel,
   701 		// If the tile is the entry tile of a tunnel, and we're not going out of the tunnel,
   697 		//   need to find the exit of the tunnel.
   702 		//   need to find the exit of the tunnel.
   698 		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
   703 		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
   713 
   718 
   714 		// This is a special loop used to go through
   719 		// This is a special loop used to go through
   715 		// a rail net and find the first intersection
   720 		// a rail net and find the first intersection
   716 		tile_org = tile;
   721 		tile_org = tile;
   717 		for(;;) {
   722 		for(;;) {
       
   723 			assert(direction <= 3);
   718 			tile += TileOffsByDir(direction);
   724 			tile += TileOffsByDir(direction);
   719 
   725 
   720 			// too long search length? bail out.
   726 			// too long search length? bail out.
   721 			if (si.cur_length >= tpf->maxlength) {
   727 			if (si.cur_length >= tpf->maxlength) {
   722 				DEBUG(ntp,1) ("[NTP] cur_length too big");
   728 				DEBUG(ntp,1) ("[NTP] cur_length too big");
   810 					return;
   816 					return;
   811 			}
   817 			}
   812 
   818 
   813 			// continue with the next track
   819 			// continue with the next track
   814 			direction = _tpf_new_direction[track];
   820 			direction = _tpf_new_direction[track];
   815 			assert(direction != 0xFF);
   821 			assert(direction <= 3);
   816 
   822 
   817 			// safety check if we're running around chasing our tail... (infinite loop)
   823 			// safety check if we're running around chasing our tail... (infinite loop)
   818 			if (tile == tile_org) {
   824 			if (tile == tile_org) {
   819 				bits = 0;
   825 				bits = 0;
   820 				break;
   826 				break;
   848 			estimation = DistanceMoo(tile, tpf->dest);
   854 			estimation = DistanceMoo(tile, tpf->dest);
   849 
   855 
   850 		si.depth++;
   856 		si.depth++;
   851 		si.tile = tile;
   857 		si.tile = tile;
   852 		do {
   858 		do {
       
   859 			assert(direction <= 3);
   853 			si.track = _new_track[FIND_FIRST_BIT(bits)][direction];
   860 			si.track = _new_track[FIND_FIRST_BIT(bits)][direction];
       
   861 			assert(si.track <= 13);
   854 			si.priority = si.cur_length + estimation;
   862 			si.priority = si.cur_length + estimation;
   855 
   863 
   856 			// out of stack items, bail out?
   864 			// out of stack items, bail out?
   857 			if (tpf->nstack >= lengthof(tpf->stack)) {
   865 			if (tpf->nstack >= lengthof(tpf->stack)) {
   858 				DEBUG(ntp, 1) ("[NTP] out of stack");
   866 				DEBUG(ntp, 1) ("[NTP] out of stack");