Changeset - r3163:2781d0c0b478
[Not reviewed]
master
0 2 0
tron - 19 years ago 2006-03-08 08:51:26
tron@openttd.org
(svn r3789) Add an enum and function to handle DiagDirection changes
2 files changed with 32 insertions and 14 deletions:
0 comments (0 inline, 0 general)
direction.h
Show inline comments
 
@@ -62,12 +62,25 @@ typedef enum DiagDirection {
 
static inline DiagDirection ReverseDiagDir(DiagDirection d)
 
{
 
	return (DiagDirection)(2 ^ d);
 
}
 

	
 

	
 
typedef enum DiagDirDiff {
 
	DIAGDIRDIFF_SAME    = 0,
 
	DIAGDIRDIFF_90RIGHT = 1,
 
	DIAGDIRDIFF_REVERSE = 2,
 
	DIAGDIRDIFF_90LEFT  = 3
 
} DiagDirDiff;
 

	
 
static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
 
{
 
	return (DiagDirection)((d + delta) % 4);
 
}
 

	
 

	
 
static inline DiagDirection DirToDiagDir(Direction dir)
 
{
 
	return (DiagDirection)(dir >> 1);
 
}
 

	
 

	
train_cmd.c
Show inline comments
 
@@ -1543,20 +1543,20 @@ static void ReverseTrainDirection(Vehicl
 
	if (IsTileDepotType(v->tile, TRANSPORT_RAIL))
 
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
 

	
 
	/* Check if we were approaching a rail/road-crossing */
 
	{
 
		TileIndex tile = v->tile;
 
		int t;
 
		DiagDirection dir = DirToDiagDir(v->direction);
 

	
 
		/* Determine the diagonal direction in which we will exit this tile */
 
		t = v->direction >> 1;
 
		if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) {
 
			t = (t - 1) & 3;
 
		if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[dir]) {
 
			dir = ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT);
 
		}
 
		/* Calculate next tile */
 
		tile += TileOffsByDir(t);
 
		tile += TileOffsByDir(dir);
 

	
 
		/* Check if the train left a rail/road-crossing */
 
		DisableTrainCrossing(tile);
 
	}
 

	
 
	// count number of vehicles
 
@@ -1782,19 +1782,23 @@ static TrainFindDepotData FindClosestTra
 
		}
 
	} else {
 
		// search in the forward direction first.
 
		DiagDirection i;
 

	
 
		i = DirToDiagDir(v->direction);
 
		if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) i = (i + 3) & 3;
 
		if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) {
 
			i = ChangeDiagDir(i, DIAGDIRDIFF_90LEFT);
 
		}
 
		NewTrainPathfind(tile, 0, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
 
		if (tfdd.best_length == (uint)-1){
 
			tfdd.reverse = true;
 
			// search in backwards direction
 
			i = ReverseDiagDir(DirToDiagDir(v->direction));
 
			if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) i = (i + 3) & 3;
 
			if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) {
 
				i = ChangeDiagDir(i, DIAGDIRDIFF_90LEFT);
 
			}
 
			NewTrainPathfind(tile, 0, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
 
		}
 
	}
 

	
 
	return tfdd;
 
}
 
@@ -3104,13 +3108,14 @@ static const byte _breakdown_speeds[16] 
 

	
 
static bool TrainCheckIfLineEnds(Vehicle *v)
 
{
 
	TileIndex tile;
 
	uint x,y;
 
	uint16 break_speed;
 
	DiagDirection t;
 
	DiagDirection dir;
 
	int t;
 
	uint32 ts;
 

	
 
	t = v->breakdown_ctr;
 
	if (t > 1) {
 
		v->vehstatus |= VS_TRAIN_SLOWING;
 

	
 
@@ -3135,20 +3140,20 @@ static bool TrainCheckIfLineEnds(Vehicle
 
	/* XXX -- When enabled, this makes it possible to crash trains of others
 
	     (by building a depot right against a station) */
 
/*	if (IsTileType(tile, MP_RAILWAY) && (_m[tile].m5 & 0xFC) == 0xC0)
 
		return true;*/
 

	
 
	/* Determine the non-diagonal direction in which we will exit this tile */
 
	t = v->direction >> 1;
 
	if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) {
 
		t = (t - 1) & 3;
 
	dir = DirToDiagDir(v->direction);
 
	if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[dir]) {
 
		dir = ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT);
 
	}
 
	/* Calculate next tile */
 
	tile += TileOffsByDir(t);
 
	tile += TileOffsByDir(dir);
 
	// determine the track status on the next tile.
 
	ts = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _reachable_tracks[t];
 
	ts = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _reachable_tracks[dir];
 

	
 
	/* Calc position within the current tile ?? */
 
	x = v->x_pos & 0xF;
 
	y = v->y_pos & 0xF;
 

	
 
	switch (v->direction) {
 
@@ -3163,13 +3168,13 @@ static bool TrainCheckIfLineEnds(Vehicle
 

	
 
	if (GB(ts, 0, 16) != 0) {
 
		/* If we approach a rail-piece which we can't enter, or the back of a depot, don't enter it! */
 
		if (x + 4 > 15 &&
 
				(!CheckCompatibleRail(v, tile) ||
 
				(IsTileDepotType(tile, TRANSPORT_RAIL) &&
 
				GetDepotDirection(tile, TRANSPORT_RAIL) == t))) {
 
				GetDepotDirection(tile, TRANSPORT_RAIL) == dir))) {
 
			v->cur_speed = 0;
 
			ReverseTrainDirection(v);
 
			return false;
 
		}
 
		if ((ts &= (ts >> 16)) == 0) {
 
			// make a rail/road crossing red
0 comments (0 inline, 0 general)