Changeset - r6009:d2757f681649
[Not reviewed]
master
0 3 0
rubidium - 17 years ago 2007-02-14 10:33:36
rubidium@openttd.org
(svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
3 files changed with 38 insertions and 44 deletions:
0 comments (0 inline, 0 general)
src/road.h
Show inline comments
 
@@ -25,7 +25,19 @@ static inline RoadBits ComplementRoadBit
 

	
 
static inline RoadBits DiagDirToRoadBits(DiagDirection d)
 
{
 
	return (RoadBits)(1U << (3 ^ d));
 
}
 

	
 
/** Checks whether the trackdir means that we are reversing */
 
static inline bool IsReversingRoadTrackdir(Trackdir dir)
 
{
 
	return (dir & 0x07) >= 6;
 
}
 

	
 
/** Checks whether the given trackdir is a straight road */
 
static inline bool IsStraightRoadTrackdir(Trackdir dir)
 
{
 
	return (dir & 0x06) == 0;
 
}
 

	
 
#endif /* ROAD_H */
src/roadveh_cmd.cpp
Show inline comments
 
@@ -76,24 +76,12 @@ static const TrackdirBits _road_exit_dir
 

	
 
/** Converts the exit direction of a depot to trackdir the vehicle is going to drive to */
 
static const Trackdir _roadveh_depot_exit_trackdir[DIAGDIR_END] = {
 
	TRACKDIR_X_NE, TRACKDIR_Y_SE, TRACKDIR_X_SW, TRACKDIR_Y_NW
 
};
 

	
 
/** Checks whether the trackdir means that we are reversing */
 
static bool IsReversingRoadTrackdir(Trackdir dir)
 
{
 
	return (dir & 0x07) >= 6;
 
}
 

	
 
/** Checks whether the given trackdir is a straight road */
 
static bool IsStraightRoadTrackdir(Trackdir dir)
 
{
 
	return (dir & 0x06) == 0;
 
}
 

	
 
int GetRoadVehImage(const Vehicle* v, Direction direction)
 
{
 
	int img = v->spritenum;
 
	int image;
 

	
 
	if (is_custom_sprite(img)) {
 
@@ -1247,12 +1235,19 @@ static uint RoadFindPathToStop(const Veh
 
	return dist;
 
}
 

	
 
enum {
 
	RDE_NEXT_TILE = 0x80,
 
	RDE_TURNED    = 0x40,
 

	
 
	/* Start frames for when a vehicle enters a tile/changes its state.
 
	 * The start frame is different for vehicles that turned around or
 
	 * are leaving the depot as the do not start at the edge of the tile */
 
	RVC_DEFAULT_START_FRAME     = 0,
 
	RVC_TURN_AROUND_START_FRAME = 1,
 
	RVC_DEPOT_START_FRAME       = 6
 
};
 

	
 
typedef struct RoadDriveEntry {
 
	byte x,y;
 
} RoadDriveEntry;
 

	
 
@@ -1310,28 +1305,28 @@ static void RoadVehController(Vehicle *v
 
		v->cur_speed = 0;
 

	
 
		dir = GetRoadDepotDirection(v->tile);
 
		v->direction = DiagDirToDir(dir);
 

	
 
		tdir = _roadveh_depot_exit_trackdir[dir];
 
		rdp = _road_drive_data[(_opt.road_side << 4) + tdir];
 
		rdp = _road_drive_data[(_opt.road_side << RVS_DRIVE_SIDE) + tdir];
 

	
 
		x = TileX(v->tile) * TILE_SIZE + (rdp[6].x & 0xF);
 
		y = TileY(v->tile) * TILE_SIZE + (rdp[6].y & 0xF);
 
		x = TileX(v->tile) * TILE_SIZE + (rdp[RVC_DEPOT_START_FRAME].x & 0xF);
 
		y = TileY(v->tile) * TILE_SIZE + (rdp[RVC_DEPOT_START_FRAME].y & 0xF);
 

	
 
		if (RoadVehFindCloseTo(v, x, y, v->direction) != NULL) return;
 

	
 
		VehicleServiceInDepot(v);
 

	
 
		StartRoadVehSound(v);
 

	
 
		BeginVehicleMove(v);
 

	
 
		v->vehstatus &= ~VS_HIDDEN;
 
		v->u.road.state = tdir;
 
		v->u.road.frame = 6;
 
		v->u.road.frame = RVC_DEPOT_START_FRAME;
 

	
 
		v->cur_image = GetRoadVehImage(v, v->direction);
 
		UpdateRoadVehDeltaXY(v);
 
		SetRoadVehPosition(v,x,y);
 

	
 
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
 
@@ -1352,13 +1347,13 @@ static void RoadVehController(Vehicle *v
 
	}
 

	
 
	/* Save old vehicle position to use at end of move to set viewport area dirty */
 
	BeginVehicleMove(v);
 

	
 
	if (v->u.road.state == RVSB_WORMHOLE) {
 
		/* Vehicle is on a bridge or in a tunnel */
 
		/* Vehicle is entering a depot or is on a bridge or in a tunnel */
 
		GetNewVehiclePosResult gp;
 

	
 
		GetNewVehiclePos(v, &gp);
 

	
 
		const Vehicle *u = RoadVehFindCloseTo(v, gp.x, gp.y, v->direction);
 
		if (u != NULL && u->cur_speed < v->cur_speed) {
 
@@ -1382,43 +1377,40 @@ static void RoadVehController(Vehicle *v
 
	}
 

	
 
	/* Get move position data for next frame */
 
	rd = _road_drive_data[(v->u.road.state + (_opt.road_side << RVS_DRIVE_SIDE)) ^ v->u.road.overtaking][v->u.road.frame + 1];
 

	
 
	if (rd.x & RDE_NEXT_TILE) {
 
		/* Vehicle is moving to the next tile */
 
		TileIndex tile = v->tile + TileOffsByDiagDir(rd.x & 3);
 
		Trackdir dir = RoadFindPathToDest(v, tile, (DiagDirection)(rd.x & 3));
 
		uint32 r;
 
		Direction newdir;
 
		const RoadDriveEntry *rdp;
 

	
 
		if (dir == INVALID_TRACKDIR) {
 
			/* No path was found to destination */
 
			v->cur_speed = 0;
 
			return;
 
		}
 

	
 
again:
 
		if (IsReversingRoadTrackdir(dir)) {
 
			/* Turning around */
 
			tile = v->tile;
 
		}
 

	
 
		/* Get position data for first frame on the new tile */
 
		rdp = _road_drive_data[(dir + (_opt.road_side << 4)) ^ v->u.road.overtaking];
 
		rdp = _road_drive_data[(dir + (_opt.road_side << RVS_DRIVE_SIDE)) ^ v->u.road.overtaking];
 

	
 
		x = TileX(tile) * TILE_SIZE + rdp[0].x;
 
		y = TileY(tile) * TILE_SIZE + rdp[0].y;
 
		x = TileX(tile) * TILE_SIZE + rdp[RVC_DEFAULT_START_FRAME].x;
 
		y = TileY(tile) * TILE_SIZE + rdp[RVC_DEFAULT_START_FRAME].y;
 

	
 
		newdir = RoadVehGetSlidingDirection(v, x, y);
 
		if (RoadVehFindCloseTo(v, x, y, newdir) != NULL) return;
 

	
 
		r = VehicleEnterTile(v, tile, x, y);
 
		if (HASBIT(r, VETS_CANNOT_ENTER)) {
 
			/* Vehicle cannot enter the tile */
 
			if (!IsTileType(tile, MP_TUNNELBRIDGE)) {
 
				v->cur_speed = 0;
 
				return;
 
			}
 
			/* Try an about turn to re-enter the previous tile */
 
			dir = _road_reverse_table[rd.x & 3];
 
@@ -1430,26 +1422,24 @@ again:
 
				/* New direction is trying to turn vehicle around.
 
				 * We can't turn at the exit of a road stop so wait.*/
 
				v->cur_speed = 0;
 
				return;
 
			}
 
			if (IsRoadStop(v->tile)) {
 
				/* The tile that the vehicle is leaving is a road stop */
 
				RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
 

	
 
				/* Vehicle is leaving a road stop tile, mark bay as free and clear the usage bit */
 
				/* Vehicle is leaving a road stop tile, mark bay as free */
 
				rs->FreeBay(HASBIT(v->u.road.state, RVS_USING_SECOND_BAY));
 
				rs->SetEntranceBusy(false);
 
			}
 
		}
 

	
 
		if (!HASBIT(r, VETS_ENTERED_WORMHOLE)) {
 
			/* Set vehicle to first frame on new tile */
 
			v->tile = tile;
 
			v->u.road.state = (byte)dir;
 
			v->u.road.frame = 0;
 
			v->u.road.frame = RVC_DEFAULT_START_FRAME;
 
		}
 
		if (newdir != v->direction) {
 
			v->direction = newdir;
 
			v->cur_speed -= v->cur_speed >> 2;
 
		}
 

	
 
@@ -1460,41 +1450,36 @@ again:
 
	}
 

	
 
	if (rd.x & RDE_TURNED) {
 
		/* Vehicle has finished turning around, it will now head back onto the same tile */
 
		Trackdir dir = RoadFindPathToDest(v, v->tile, (DiagDirection)(rd.x & 3));
 
		uint32 r;
 
		int tmp;
 
		Direction newdir;
 
		const RoadDriveEntry *rdp;
 

	
 
		if (dir == INVALID_TRACKDIR) {
 
			/* No path was found to destination */
 
			v->cur_speed = 0;
 
			return;
 
		}
 

	
 
		tmp = (_opt.road_side << 4) + dir;
 
		rdp = _road_drive_data[tmp];
 
		rdp = _road_drive_data[(_opt.road_side << RVS_DRIVE_SIDE) + dir];
 

	
 
		x = TileX(v->tile) * TILE_SIZE + rdp[1].x;
 
		y = TileY(v->tile) * TILE_SIZE + rdp[1].y;
 
		x = TileX(v->tile) * TILE_SIZE + rdp[RVC_TURN_AROUND_START_FRAME].x;
 
		y = TileY(v->tile) * TILE_SIZE + rdp[RVC_TURN_AROUND_START_FRAME].y;
 

	
 
		newdir = RoadVehGetSlidingDirection(v, x, y);
 
		if (RoadVehFindCloseTo(v, x, y, newdir) != NULL) return;
 

	
 
		r = VehicleEnterTile(v, v->tile, x, y);
 
		if (HASBIT(r, VETS_CANNOT_ENTER)) {
 
			/* Vehicle cannot enter the tile */
 
			v->cur_speed = 0;
 
			return;
 
		}
 

	
 
		/* Set vehicle to second frame on the tile */
 
		v->u.road.state = tmp & ~(1 << RVS_DRIVE_SIDE);
 
		v->u.road.frame = 1;
 
		v->u.road.state = dir;
 
		v->u.road.frame = RVC_TURN_AROUND_START_FRAME;
 

	
 
		if (newdir != v->direction) {
 
			v->direction = newdir;
 
			v->cur_speed -= v->cur_speed >> 2;
 
		}
 

	
 
@@ -1521,23 +1506,22 @@ again:
 
			return;
 
		}
 
	}
 

	
 
	old_dir = v->direction;
 
	if (new_dir != old_dir) {
 
		/* The vehicle's direction has changed. */
 
		v->direction = new_dir;
 
		v->cur_speed -= (v->cur_speed >> 2);
 
		if (old_dir != v->u.road.state) {
 
			/* The vehicle is in a road stop */
 
			v->cur_image = GetRoadVehImage(v, new_dir);
 
			UpdateRoadVehDeltaXY(v);
 
			SetRoadVehPosition(v, v->x_pos, v->y_pos);
 
			/* Note, return here means that the frame counter is not incremented
 
			 * for vehicles changing direction in a road stop. This causes frames to
 
			 * be repeated. Is this intended? */
 
			 * be repeated. (XXX) Is this intended? */
 
			return;
 
		}
 
	}
 

	
 
	if (v->u.road.state >= RVSB_IN_ROAD_STOP &&
 
			_road_veh_data_1[v->u.road.state - RVSB_IN_ROAD_STOP + (_opt.road_side << RVS_DRIVE_SIDE)] == v->u.road.frame) {
 
@@ -1549,13 +1533,12 @@ again:
 
		 * so if we get here the vehicle has just arrived or is just ready to leave. */
 
		if (v->current_order.type != OT_LEAVESTATION &&
 
				v->current_order.type != OT_GOTO_DEPOT) {
 
			/* Vehicle has arrived at a bay in a road stop */
 
			Order old_order;
 

	
 
			/* Clear road stop busy bit to allow another vehicle to enter or leave */
 
			rs->SetEntranceBusy(false);
 

	
 
			v->last_station_visited = GetStationIndex(v->tile);
 

	
 
			RoadVehArrivesAt(v, st);
 

	
 
@@ -1578,21 +1561,21 @@ again:
 
			return;
 
		}
 

	
 
		/* Vehicle is ready to leave a bay in a road stop */
 
		if (v->current_order.type != OT_GOTO_DEPOT) {
 
			if (rs->IsEntranceBusy()) {
 
				/* Road stop entrance is busy, so wait */
 
				/* Road stop entrance is busy, so wait as there is nowhere else to go */
 
				v->cur_speed = 0;
 
				return;
 
			}
 
			v->current_order.type = OT_NOTHING;
 
			v->current_order.flags = 0;
 
			ClearSlot(v);
 
		}
 
		/* Set road stop busy bit to prevent another vehicle trying to enter or leave */
 

	
 
		rs->SetEntranceBusy(true);
 

	
 
		if (rs == v->u.road.slot) {
 
			/* We are leaving the correct station */
 
			ClearSlot(v);
 
		} else if (v->u.road.slot != NULL) {
 
@@ -1623,13 +1606,12 @@ again:
 
	}
 

	
 
	/* Check tile position conditions - i.e. stop position in depot,
 
	 * entry onto bridge or into tunnel */
 
	r = VehicleEnterTile(v, v->tile, x, y);
 
	if (HASBIT(r, VETS_CANNOT_ENTER)) {
 
		/* Vehicle cannot continue */
 
		v->cur_speed = 0;
 
		return;
 
	}
 

	
 
	/* Move to next frame unless vehicle arrived at a stop position
 
	 * in a depot or entered a tunnel/bridge */
src/vehicle.cpp
Show inline comments
 
@@ -2757,13 +2757,13 @@ Trackdir GetVehicleTrackdir(const Vehicl
 
				return DiagdirToDiagTrackdir(GetRoadDepotDirection(v->tile));
 

	
 
			if (IsRoadStopTile(v->tile)) /* We'll assume the road vehicle is facing outwards */
 
				return DiagdirToDiagTrackdir(GetRoadStopDir(v->tile)); /* Road vehicle in a station */
 

	
 
			/* If vehicle's state is a valid track direction (vehicle is not turning around) return it */
 
			if ((v->u.road.state & 7) < 6) return (Trackdir)v->u.road.state;
 
			if (!IsReversingRoadTrackdir((Trackdir)v->u.road.state)) return (Trackdir)v->u.road.state;
 

	
 
			/* Vehicle is turning around, get the direction from vehicle's direction */
 
			return DiagdirToDiagTrackdir(DirToDiagDir(v->direction));
 

	
 
		/* case VEH_Aircraft: case VEH_Special: case VEH_Disaster: */
 
		default: return INVALID_TRACKDIR;
0 comments (0 inline, 0 general)