src/train_cmd.cpp
changeset 8794 4a3ee468d97a
parent 8793 5e35232593c3
child 8827 730524764a69
equal deleted inserted replaced
8793:5e35232593c3 8794:4a3ee468d97a
  2382 {{ 15, 8, 2 }, { 0, 0, 0 }, { 0, 0, 0 }, { 8, 15, 6 }},
  2382 {{ 15, 8, 2 }, { 0, 0, 0 }, { 0, 0, 0 }, { 8, 15, 6 }},
  2383 {{ 15, 7, 0 }, { 8, 0, 4 }, { 0, 0, 0 }, { 0,  0, 0 }},
  2383 {{ 15, 7, 0 }, { 8, 0, 4 }, { 0, 0, 0 }, { 0,  0, 0 }},
  2384 {{  0, 0, 0 }, { 0, 0, 0 }, { 0, 8, 4 }, { 7, 15, 0 }},
  2384 {{  0, 0, 0 }, { 0, 0, 0 }, { 0, 8, 4 }, { 7, 15, 0 }},
  2385 };
  2385 };
  2386 
  2386 
  2387 static const uint32 _reachable_tracks[4] = {
       
  2388 	0x10091009,
       
  2389 	0x00160016,
       
  2390 	0x05200520,
       
  2391 	0x2A002A00,
       
  2392 };
       
  2393 
       
  2394 static const byte _search_directions[6][4] = {
  2387 static const byte _search_directions[6][4] = {
  2395 	{ 0, 9, 2, 9 }, ///< track 1
  2388 	{ 0, 9, 2, 9 }, ///< track 1
  2396 	{ 9, 1, 9, 3 }, ///< track 2
  2389 	{ 9, 1, 9, 3 }, ///< track 2
  2397 	{ 9, 0, 3, 9 }, ///< track upper
  2390 	{ 9, 0, 3, 9 }, ///< track upper
  2398 	{ 1, 9, 9, 2 }, ///< track lower
  2391 	{ 1, 9, 9, 2 }, ///< track lower
  2533 
  2526 
  2534 	uint reverse_best = 0;
  2527 	uint reverse_best = 0;
  2535 
  2528 
  2536 	assert(v->u.rail.track);
  2529 	assert(v->u.rail.track);
  2537 
  2530 
  2538 	int i = _search_directions[FIND_FIRST_BIT(v->u.rail.track)][DirToDiagDir(v->direction)];
       
  2539 
       
  2540 	switch (_patches.pathfinder_for_trains) {
  2531 	switch (_patches.pathfinder_for_trains) {
  2541 		case VPF_YAPF: { /* YAPF */
  2532 		case VPF_YAPF: { /* YAPF */
  2542 			reverse_best = YapfCheckReverseTrain(v);
  2533 			reverse_best = YapfCheckReverseTrain(v);
  2543 		} break;
  2534 		} break;
  2544 
  2535 
  2567 			}
  2558 			}
  2568 		} break;
  2559 		} break;
  2569 
  2560 
  2570 		default:
  2561 		default:
  2571 		case VPF_NTP: { /* NTP */
  2562 		case VPF_NTP: { /* NTP */
       
  2563 			int i = _search_directions[FindFirstTrack(v->u.rail.track)][DirToDiagDir(v->direction)];
       
  2564 
  2572 			int best_track = -1;
  2565 			int best_track = -1;
  2573 			uint reverse = 0;
  2566 			uint reverse = 0;
  2574 			uint best_bird_dist  = 0;
  2567 			uint best_bird_dist  = 0;
  2575 			uint best_track_dist = 0;
  2568 			uint best_track_dist = 0;
  2576 
  2569 
  2899 		uint16 spd = v->cur_speed + rsp->z_down;
  2892 		uint16 spd = v->cur_speed + rsp->z_down;
  2900 		if (spd <= v->max_speed) v->cur_speed = spd;
  2893 		if (spd <= v->max_speed) v->cur_speed = spd;
  2901 	}
  2894 	}
  2902 }
  2895 }
  2903 
  2896 
  2904 static const DiagDirection _otherside_signal_directions[] = {
       
  2905 	DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, INVALID_DIAGDIR, INVALID_DIAGDIR,
       
  2906 	DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE
       
  2907 };
       
  2908 
       
  2909 static void TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
  2897 static void TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
  2910 {
  2898 {
  2911 	if (IsTileType(tile, MP_RAILWAY) &&
  2899 	if (IsTileType(tile, MP_RAILWAY) &&
  2912 			GetRailTileType(tile) == RAIL_TILE_SIGNALS) {
  2900 			GetRailTileType(tile) == RAIL_TILE_SIGNALS) {
  2913 		uint i = FindFirstBit2x64(GetTrackBits(tile) * 0x101 & _reachable_tracks[dir]);
  2901 		TrackdirBits tracks = TrackBitsToTrackdirBits(GetTrackBits(tile)) & DiagdirReachesTrackdirs(dir);
  2914 		UpdateSignalsOnSegment(tile, _otherside_signal_directions[i], GetTileOwner(tile));
  2902 		Trackdir trackdir = FindFirstTrackdir(tracks);
       
  2903 		UpdateSignalsOnSegment(tile, TrackdirToExitdir(trackdir), GetTileOwner(tile));
  2915 	}
  2904 	}
  2916 }
  2905 }
  2917 
  2906 
  2918 
  2907 
  2919 static void SetVehicleCrashed(Vehicle *v)
  2908 static void SetVehicleCrashed(Vehicle *v)
  3087 				enterdir = DirToDiagDir(dir);
  3076 				enterdir = DirToDiagDir(dir);
  3088 				assert(IsValidDiagDirection(enterdir));
  3077 				assert(IsValidDiagDirection(enterdir));
  3089 
  3078 
  3090 				/* Get the status of the tracks in the new tile and mask
  3079 				/* Get the status of the tracks in the new tile and mask
  3091 				 * away the bits that aren't reachable. */
  3080 				 * away the bits that aren't reachable. */
  3092 				TrackStatus ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL, 0, ReverseDiagDir(enterdir)) & _reachable_tracks[enterdir];
  3081 				TrackStatus ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL, 0, ReverseDiagDir(enterdir));
  3093 				TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts);
  3082 				TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(enterdir);
  3094 				TrackBits red_signals = TrackdirBitsToTrackBits(TrackStatusToRedSignals(ts));
  3083 
       
  3084 				TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs;
       
  3085 				TrackBits red_signals = TrackdirBitsToTrackBits(TrackStatusToRedSignals(ts) & reachable_trackdirs);
  3095 
  3086 
  3096 				TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
  3087 				TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
  3097 				if (_patches.pathfinder_for_trains != VPF_NTP && _patches.forbid_90_deg && prev == NULL) {
  3088 				if (_patches.pathfinder_for_trains != VPF_NTP && _patches.forbid_90_deg && prev == NULL) {
  3098 					/* We allow wagons to make 90 deg turns, because forbid_90_deg
  3089 					/* We allow wagons to make 90 deg turns, because forbid_90_deg
  3099 					 * can be switched on halfway a turn */
  3090 					 * can be switched on halfway a turn */
  3517 	DiagDirection dir = TrainExitDir(v->direction, v->u.rail.track);
  3508 	DiagDirection dir = TrainExitDir(v->direction, v->u.rail.track);
  3518 	/* Calculate next tile */
  3509 	/* Calculate next tile */
  3519 	TileIndex tile = v->tile + TileOffsByDiagDir(dir);
  3510 	TileIndex tile = v->tile + TileOffsByDiagDir(dir);
  3520 
  3511 
  3521 	/* Determine the track status on the next tile */
  3512 	/* Determine the track status on the next tile */
  3522 	TrackStatus ts = GetTileTrackStatus(tile, TRANSPORT_RAIL, 0, ReverseDiagDir(dir)) & _reachable_tracks[dir];
  3513 	TrackStatus ts = GetTileTrackStatus(tile, TRANSPORT_RAIL, 0, ReverseDiagDir(dir));
  3523 	TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts);
  3514 	TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(dir);
  3524 	TrackdirBits red_signals = TrackStatusToRedSignals(ts);
  3515 
       
  3516 	TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs;
       
  3517 	TrackdirBits red_signals = TrackStatusToRedSignals(ts) & reachable_trackdirs;
  3525 
  3518 
  3526 	/* We are sure the train is not entering a depot, it is detected above */
  3519 	/* We are sure the train is not entering a depot, it is detected above */
  3527 
  3520 
  3528 	/* mask unreachable track bits if we are forbidden to do 90deg turns */
  3521 	/* mask unreachable track bits if we are forbidden to do 90deg turns */
  3529 	TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
  3522 	TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);