train_cmd.c
changeset 3900 2c84ed52709d
parent 3870 d9ebc4ea750a
child 3926 ea737ba47ba0
equal deleted inserted replaced
3899:5ba7f20a14ca 3900:2c84ed52709d
    27 #include "train.h"
    27 #include "train.h"
    28 #include "newgrf_callbacks.h"
    28 #include "newgrf_callbacks.h"
    29 #include "newgrf_engine.h"
    29 #include "newgrf_engine.h"
    30 #include "newgrf_text.h"
    30 #include "newgrf_text.h"
    31 #include "direction.h"
    31 #include "direction.h"
       
    32 #include "yapf/yapf.h"
    32 
    33 
    33 static bool TrainCheckIfLineEnds(Vehicle *v);
    34 static bool TrainCheckIfLineEnds(Vehicle *v);
    34 static void TrainController(Vehicle *v);
    35 static void TrainController(Vehicle *v);
    35 
    36 
    36 static const byte _vehicle_initial_x_fract[4] = {10,8,4,8};
    37 static const byte _vehicle_initial_x_fract[4] = {10,8,4,8};
  1832 	return false;
  1833 	return false;
  1833 }
  1834 }
  1834 
  1835 
  1835 // returns the tile of a depot to goto to. The given vehicle must not be
  1836 // returns the tile of a depot to goto to. The given vehicle must not be
  1836 // crashed!
  1837 // crashed!
  1837 static TrainFindDepotData FindClosestTrainDepot(Vehicle *v)
  1838 static TrainFindDepotData FindClosestTrainDepot(Vehicle *v, int max_distance)
  1838 {
  1839 {
  1839 	TrainFindDepotData tfdd;
  1840 	TrainFindDepotData tfdd;
  1840 	TileIndex tile = v->tile;
  1841 	TileIndex tile = v->tile;
  1841 
  1842 
  1842 	assert(!(v->vehstatus & VS_CRASHED));
  1843 	assert(!(v->vehstatus & VS_CRASHED));
  1851 		return tfdd;
  1852 		return tfdd;
  1852 	}
  1853 	}
  1853 
  1854 
  1854 	if (v->u.rail.track == 0x40) tile = GetVehicleOutOfTunnelTile(v);
  1855 	if (v->u.rail.track == 0x40) tile = GetVehicleOutOfTunnelTile(v);
  1855 
  1856 
  1856 	if (_patches.new_pathfinding_all) {
  1857 	if (_patches.yapf.rail_use_yapf) {
       
  1858 		bool found = YapfFindNearestRailDepotTwoWay(v, max_distance, NPF_INFINITE_PENALTY, &tfdd.tile, &tfdd.reverse);
       
  1859 		tfdd.best_length = found ? max_distance / 2 : -1; // some fake distance or NOT_FOUND
       
  1860 	} else if (_patches.new_pathfinding_all) {
  1857 		NPFFoundTargetData ftd;
  1861 		NPFFoundTargetData ftd;
  1858 		Vehicle* last = GetLastVehicleInChain(v);
  1862 		Vehicle* last = GetLastVehicleInChain(v);
  1859 		Trackdir trackdir = GetVehicleTrackdir(v);
  1863 		Trackdir trackdir = GetVehicleTrackdir(v);
  1860 		Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
  1864 		Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
  1861 
  1865 
  1924 			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
  1928 			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
  1925 		}
  1929 		}
  1926 		return 0;
  1930 		return 0;
  1927 	}
  1931 	}
  1928 
  1932 
  1929 	tfdd = FindClosestTrainDepot(v);
  1933 	tfdd = FindClosestTrainDepot(v, 0);
  1930 	if (tfdd.best_length == (uint)-1)
  1934 	if (tfdd.best_length == (uint)-1)
  1931 		return_cmd_error(STR_883A_UNABLE_TO_FIND_ROUTE_TO);
  1935 		return_cmd_error(STR_883A_UNABLE_TO_FIND_ROUTE_TO);
  1932 
  1936 
  1933 	if (flags & DC_EXEC) {
  1937 	if (flags & DC_EXEC) {
  1934 		v->dest_tile = tfdd.tile;
  1938 		v->dest_tile = tfdd.tile;
  2176 	assert((trackdirbits & ~0x3F) == 0);
  2180 	assert((trackdirbits & ~0x3F) == 0);
  2177 
  2181 
  2178 	/* quick return in case only one possible track is available */
  2182 	/* quick return in case only one possible track is available */
  2179 	if (KILL_FIRST_BIT(trackdirbits) == 0) return FIND_FIRST_BIT(trackdirbits);
  2183 	if (KILL_FIRST_BIT(trackdirbits) == 0) return FIND_FIRST_BIT(trackdirbits);
  2180 
  2184 
  2181 	if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */
  2185 	if (_patches.yapf.rail_use_yapf) {
       
  2186 		Trackdir trackdir = YapfChooseRailTrack(v, tile, enterdir, trackdirbits);
       
  2187 		if (trackdir != INVALID_TRACKDIR) {
       
  2188 			best_track = TrackdirToTrack(trackdir);
       
  2189 		} else {
       
  2190 			best_track = FIND_FIRST_BIT(TrackdirBitsToTrackBits(trackdirbits));
       
  2191 		}
       
  2192 	} else if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */
       
  2193 		void* perf = NpfBeginInterval();
       
  2194 		int time = 0;
       
  2195 
  2182 		NPFFindStationOrTileData fstd;
  2196 		NPFFindStationOrTileData fstd;
  2183 		NPFFoundTargetData ftd;
  2197 		NPFFoundTargetData ftd;
  2184 		Trackdir trackdir;
  2198 		Trackdir trackdir;
  2185 
  2199 
  2186 		NPFFillWithOrderData(&fstd, v);
  2200 		NPFFillWithOrderData(&fstd, v);
  2201 			we did not find our target, but ftd.best_trackdir contains the direction leading
  2215 			we did not find our target, but ftd.best_trackdir contains the direction leading
  2202 			to the tile closest to our target. */
  2216 			to the tile closest to our target. */
  2203 			/* Discard enterdir information, making it a normal track */
  2217 			/* Discard enterdir information, making it a normal track */
  2204 			best_track = TrackdirToTrack(ftd.best_trackdir);
  2218 			best_track = TrackdirToTrack(ftd.best_trackdir);
  2205 		}
  2219 		}
       
  2220 
       
  2221 		time = NpfEndInterval(perf);
       
  2222 		DEBUG(yapf, 1)("[YAPF][NPFT] %d us - %d rounds - %d open - %d closed -- ", time, 0, _aystar_stats_open_size, _aystar_stats_closed_size);
  2206 	} else {
  2223 	} else {
       
  2224 		void* perf = NpfBeginInterval();
       
  2225 		int time = 0;
       
  2226 
  2207 		FillWithStationData(&fd, v);
  2227 		FillWithStationData(&fd, v);
  2208 
  2228 
  2209 		/* New train pathfinding */
  2229 		/* New train pathfinding */
  2210 		fd.best_bird_dist = (uint)-1;
  2230 		fd.best_bird_dist = (uint)-1;
  2211 		fd.best_track_dist = (uint)-1;
  2231 		fd.best_track_dist = (uint)-1;
  2218 			// blaha
  2238 			// blaha
  2219 			best_track = FIND_FIRST_BIT(trackdirbits);
  2239 			best_track = FIND_FIRST_BIT(trackdirbits);
  2220 		} else {
  2240 		} else {
  2221 			best_track = fd.best_track & 7;
  2241 			best_track = fd.best_track & 7;
  2222 		}
  2242 		}
       
  2243 
       
  2244 		time = NpfEndInterval(perf);
       
  2245 		DEBUG(yapf, 1)("[YAPF][NTPT] %d us - %d rounds - %d open - %d closed -- ", time, 0, 0, 0);
  2223 	}
  2246 	}
  2224 
  2247 
  2225 #ifdef PF_BENCHMARK
  2248 #ifdef PF_BENCHMARK
  2226 	TOC("PF time = ", 1)
  2249 	TOC("PF time = ", 1)
  2227 #endif
  2250 #endif
  2251 
  2274 
  2252 	assert(v->u.rail.track);
  2275 	assert(v->u.rail.track);
  2253 
  2276 
  2254 	i = _search_directions[FIND_FIRST_BIT(v->u.rail.track)][DirToDiagDir(v->direction)];
  2277 	i = _search_directions[FIND_FIRST_BIT(v->u.rail.track)][DirToDiagDir(v->direction)];
  2255 
  2278 
  2256 	if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */
  2279 	if (_patches.yapf.rail_use_yapf) {
       
  2280 		reverse_best = YapfCheckReverseTrain(v);
       
  2281 	} else if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */
  2257 		NPFFindStationOrTileData fstd;
  2282 		NPFFindStationOrTileData fstd;
  2258 		NPFFoundTargetData ftd;
  2283 		NPFFoundTargetData ftd;
  2259 		byte trackdir, trackdir_rev;
  2284 		byte trackdir, trackdir_rev;
  2260 		Vehicle* last = GetLastVehicleInChain(v);
  2285 		Vehicle* last = GetLastVehicleInChain(v);
  2261 
  2286 
  2888 				/* Combine the from & to directions.
  2913 				/* Combine the from & to directions.
  2889 				 * Now, the lower byte contains the track status, and the byte at bit 16 contains
  2914 				 * Now, the lower byte contains the track status, and the byte at bit 16 contains
  2890 				 * the signal status. */
  2915 				 * the signal status. */
  2891 				tracks = ts | (ts >> 8);
  2916 				tracks = ts | (ts >> 8);
  2892 				bits = tracks & 0xFF;
  2917 				bits = tracks & 0xFF;
  2893 				if (_patches.new_pathfinding_all && _patches.forbid_90_deg && prev == NULL) {
  2918 				if ((_patches.new_pathfinding_all || _patches.yapf.rail_use_yapf) && _patches.forbid_90_deg && prev == NULL) {
  2894 					/* We allow wagons to make 90 deg turns, because forbid_90_deg
  2919 					/* We allow wagons to make 90 deg turns, because forbid_90_deg
  2895 					 * can be switched on halfway a turn */
  2920 					 * can be switched on halfway a turn */
  2896 					bits &= ~TrackCrossesTracks(FIND_FIRST_BIT(v->u.rail.track));
  2921 					bits &= ~TrackCrossesTracks(FIND_FIRST_BIT(v->u.rail.track));
  2897 				}
  2922 				}
  2898 
  2923 
  3410 		}
  3435 		}
  3411 	}
  3436 	}
  3412 	InvalidateWindowClasses(WC_TRAINS_LIST);
  3437 	InvalidateWindowClasses(WC_TRAINS_LIST);
  3413 }
  3438 }
  3414 
  3439 
       
  3440 #define MAX_ACCEPTABLE_DEPOT_DIST 16
       
  3441 
  3415 static void CheckIfTrainNeedsService(Vehicle *v)
  3442 static void CheckIfTrainNeedsService(Vehicle *v)
  3416 {
  3443 {
  3417 	const Depot* depot;
  3444 	const Depot* depot;
  3418 	TrainFindDepotData tfdd;
  3445 	TrainFindDepotData tfdd;
  3419 
  3446 
  3426 	// depot visit by the order list.
  3453 	// depot visit by the order list.
  3427 	if (v->current_order.type == OT_GOTO_DEPOT &&
  3454 	if (v->current_order.type == OT_GOTO_DEPOT &&
  3428 			(v->current_order.flags & (OF_HALT_IN_DEPOT | OF_PART_OF_ORDERS)) != 0)
  3455 			(v->current_order.flags & (OF_HALT_IN_DEPOT | OF_PART_OF_ORDERS)) != 0)
  3429 		return;
  3456 		return;
  3430 
  3457 
  3431 	tfdd = FindClosestTrainDepot(v);
  3458 	tfdd = FindClosestTrainDepot(v, MAX_ACCEPTABLE_DEPOT_DIST);
  3432 	/* Only go to the depot if it is not too far out of our way. */
  3459 	/* Only go to the depot if it is not too far out of our way. */
  3433 	if (tfdd.best_length == (uint)-1 || tfdd.best_length > 16 ) {
  3460 	if (tfdd.best_length == (uint)-1 || tfdd.best_length > MAX_ACCEPTABLE_DEPOT_DIST) {
  3434 		if (v->current_order.type == OT_GOTO_DEPOT) {
  3461 		if (v->current_order.type == OT_GOTO_DEPOT) {
  3435 			/* If we were already heading for a depot but it has
  3462 			/* If we were already heading for a depot but it has
  3436 			 * suddenly moved farther away, we continue our normal
  3463 			 * suddenly moved farther away, we continue our normal
  3437 			 * schedule? */
  3464 			 * schedule? */
  3438 			v->current_order.type = OT_DUMMY;
  3465 			v->current_order.type = OT_DUMMY;