changeset 3153:90bcf130cd3b draft

(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
author tron <tron@openttd.org>
date Mon, 06 Mar 2006 20:28:28 +0000
parents feb87d62e9e2
children c655b0fa9fa6
files ai/default/default.c ai/trolly/trolly.c direction.h pathfind.c pathfind.h rail_cmd.c roadveh_cmd.c train_cmd.c tunnelbridge_cmd.c vehicle.h
diffstat 10 files changed, 83 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/ai/default/default.c
+++ b/ai/default/default.c
@@ -3636,7 +3636,7 @@
 		if (IsLevelCrossing(tile)) goto is_rail_crossing;
 
 		if (GetRoadType(tile) == ROAD_DEPOT) {
-			uint dir;
+			DiagDirection dir;
 
 			// Check if there are any stations around.
 			if (IsTileType(tile + TileDiffXY(-1, 0), MP_STATION) &&
@@ -3664,7 +3664,7 @@
 			DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 			DoCommandByTile(
 				TILE_MASK(tile + TileOffsByDir(dir)),
-				8 >> (dir ^ 2),
+				DiagDirToRoadBits(ReverseDiagDir(dir)),
 				0,
 				DC_EXEC,
 				CMD_REMOVE_ROAD);
--- a/ai/trolly/trolly.c
+++ b/ai/trolly/trolly.c
@@ -784,7 +784,7 @@
 	// But first we walk through the route see if we can find a depot that is ours
 	//  this keeps things nice ;)
 	int g, i, r;
-	uint j;
+	DiagDirection j;
 	TileIndex tile;
 	assert(p->ainew.state == AI_STATE_FIND_DEPOT);
 
@@ -793,20 +793,16 @@
 	for (i=2;i<p->ainew.path_info.route_length-2;i++) {
 		tile = p->ainew.path_info.route[i];
 		for (j = 0; j < 4; j++) {
-			if (IsTileType(tile + TileOffsByDir(j), MP_STREET)) {
-				if (GetRoadType(tile + TileOffsByDir(j)) == ROAD_DEPOT) {
-					// We found a depot, is it ours? (TELL ME!!!)
-					if (IsTileOwner(tile + TileOffsByDir(j), _current_player)) {
-						// Now, is it pointing to the right direction.........
-						if (GB(_m[tile + TileOffsByDir(j)].m5, 0, 2) == (j ^ 2)) {
-							// Yeah!!!
-							p->ainew.depot_tile = tile + TileOffsByDir(j);
-							p->ainew.depot_direction = j ^ 2; // Reverse direction
-							p->ainew.state = AI_STATE_VERIFY_ROUTE;
-							return;
-						}
-					}
-				}
+			TileIndex t = tile + TileOffsByDir(j);
+
+			if (IsTileType(t, MP_STREET) &&
+					GetRoadType(t) == ROAD_DEPOT &&
+					IsTileOwner(t, _current_player) &&
+					GB(_m[t].m5, 0, 2) == ReverseDiagDir(j)) { // right direction?
+				p->ainew.depot_tile = t;
+				p->ainew.depot_direction = ReverseDiagDir(j);
+				p->ainew.state = AI_STATE_VERIFY_ROUTE;
+				return;
 			}
 		}
 	}
@@ -828,27 +824,29 @@
 		tile = p->ainew.path_info.route[i];
 
 		for (j = 0; j < 4; j++) {
+			TileIndex t = tile + TileOffsByDir(j);
+
 			// It may not be placed on the road/rail itself
 			// And because it is not build yet, we can't see it on the tile..
 			// So check the surrounding tiles :)
-			if (tile + TileOffsByDir(j) == p->ainew.path_info.route[i-1] ||
-					tile + TileOffsByDir(j) == p->ainew.path_info.route[i+1])
+			if (t == p->ainew.path_info.route[i - 1] ||
+					t == p->ainew.path_info.route[i + 1]) {
 				continue;
+			}
 			// Not around a bridge?
 			if (p->ainew.path_info.route_extra[i] != 0) continue;
 			if (IsTileType(tile, MP_TUNNELBRIDGE)) continue;
 			// Is the terrain clear?
-			if (IsTileType(tile + TileOffsByDir(j), MP_CLEAR) ||
-					IsTileType(tile + TileOffsByDir(j), MP_TREES)) {
+			if (IsTileType(t, MP_CLEAR) || IsTileType(t, MP_TREES)) {
 				// If the current tile is on a slope (tileh != 0) then we do not allow this
 				if (GetTileSlope(tile, NULL) != 0) continue;
 				// Check if everything went okay..
-				r = AiNew_Build_Depot(p, tile + TileOffsByDir(j), j ^ 2, 0);
+				r = AiNew_Build_Depot(p, t, ReverseDiagDir(j), 0);
 				if (CmdFailed(r)) continue;
 				// Found a spot!
 				p->ainew.new_cost += r;
-				p->ainew.depot_tile = tile + TileOffsByDir(j);
-				p->ainew.depot_direction = j ^ 2; // Reverse direction
+				p->ainew.depot_tile = t;
+				p->ainew.depot_direction = ReverseDiagDir(j); // Reverse direction
 				p->ainew.state = AI_STATE_VERIFY_ROUTE;
 				return;
 			}
--- a/direction.h
+++ b/direction.h
@@ -34,12 +34,18 @@
 }
 
 
-static inline DiagDirection DirToDiagdir(Direction dir)
+static inline DiagDirection DirToDiagDir(Direction dir)
 {
 	return (DiagDirection)(dir >> 1);
 }
 
 
+static inline Direction DiagDirToDir(DiagDirection dir)
+{
+	return (Direction)(dir * 2 + 1);
+}
+
+
 /* the 2 axis */
 typedef enum Axis {
 	AXIS_X = 0,
--- a/pathfind.c
+++ b/pathfind.c
@@ -127,7 +127,7 @@
 	0x2A,
 };
 
-static void TPFMode2(TrackPathFinder *tpf, TileIndex tile, int direction)
+static void TPFMode2(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
 {
 	uint bits;
 	int i;
@@ -275,7 +275,7 @@
 48,56,56,58,56,60,60,62,
 };
 
-static void TPFMode1(TrackPathFinder *tpf, TileIndex tile, uint direction)
+static void TPFMode1(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
 {
 	uint bits;
 	int i;
@@ -343,7 +343,7 @@
 		return;
 
 	tile = tile_org;
-	direction ^= 2;
+	direction = ReverseDiagDir(direction);
 
 	bits = GetTileTrackStatus(tile, tpf->tracktype);
 	bits |= (bits >> 8);
@@ -370,7 +370,7 @@
 	} while (bits != 0);
 }
 
-void FollowTrack(TileIndex tile, uint16 flags, byte direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data)
+void FollowTrack(TileIndex tile, uint16 flags, DiagDirection direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data)
 {
 	TrackPathFinder tpf;
 
@@ -506,7 +506,7 @@
 // mark a tile as visited and store the length of the path.
 // if we already had a better path to this tile, return false.
 // otherwise return true.
-static bool NtpVisit(NewTrackPathFinder *tpf, TileIndex tile, uint dir, uint length)
+static bool NtpVisit(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection dir, uint length)
 {
 	uint hash,head;
 	HashLink *link, *new_link;
@@ -671,7 +671,7 @@
 // Tile is the tile the train is at.
 // direction is the tile the train is moving towards.
 
-static void NTPEnum(NewTrackPathFinder *tpf, TileIndex tile, uint direction)
+static void NTPEnum(NewTrackPathFinder* tpf, TileIndex tile, DiagDirection direction)
 {
 	TrackBits bits, allbits;
 	uint track;
@@ -912,7 +912,7 @@
 
 
 // new pathfinder for trains. better and faster.
-void NewTrainPathfind(TileIndex tile, TileIndex dest, byte direction, NTPEnumProc *enum_proc, void *data)
+void NewTrainPathfind(TileIndex tile, TileIndex dest, DiagDirection direction, NTPEnumProc* enum_proc, void* data)
 {
 	NewTrackPathFinder tpf;
 
--- a/pathfind.h
+++ b/pathfind.h
@@ -3,6 +3,8 @@
 #ifndef PATHFIND_H
 #define PATHFIND_H
 
+#include "direction.h"
+
 //#define PF_BENCH // perform simple benchmarks on the train pathfinder (not
 //supported on all archs)
 
@@ -58,7 +60,7 @@
 	TrackPathFinderLink links[0x400]; /* hopefully, this is enough. */
 };
 
-void FollowTrack(TileIndex tile, uint16 flags, byte direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data);
+void FollowTrack(TileIndex tile, uint16 flags, DiagDirection direction, TPFEnumProc* enum_proc, TPFAfterProc* after_proc, void* data);
 
 typedef struct {
 	TileIndex tile;
@@ -66,6 +68,6 @@
 } FindLengthOfTunnelResult;
 FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, uint direction);
 
-void NewTrainPathfind(TileIndex tile, TileIndex dest, byte direction, NTPEnumProc *enum_proc, void *data);
+void NewTrainPathfind(TileIndex tile, TileIndex dest, DiagDirection direction, NTPEnumProc* enum_proc, void* data);
 
 #endif /* PATHFIND_H */
--- a/rail_cmd.c
+++ b/rail_cmd.c
@@ -1755,13 +1755,13 @@
 }
 
 
-bool UpdateSignalsOnSegment(TileIndex tile, byte direction)
+bool UpdateSignalsOnSegment(TileIndex tile, Direction dir)
 {
 	SetSignalsData ssd;
 	int result = -1;
+	DiagDirection direction = DirToDiagDir(dir);
 
 	ssd.cur_stack = 0;
-	direction >>= 1;
 
 	for (;;) {
 		// go through one segment and update all signals pointing into that segment.
--- a/roadveh_cmd.c
+++ b/roadveh_cmd.c
@@ -309,7 +309,6 @@
 static const Depot* FindClosestRoadDepot(const Vehicle* v)
 {
 	TileIndex tile = v->tile;
-	int i;
 
 	if (v->u.road.state == 255) tile = GetVehicleOutOfTunnelTile(v);
 
@@ -327,6 +326,8 @@
 		/* We do not search in two directions here, why should we? We can't reverse right now can we? */
 	} else {
 		RoadFindDepotData rfdd;
+		DiagDirection i;
+
 		rfdd.owner = v->owner;
 		rfdd.best_length = (uint)-1;
 
@@ -1187,14 +1188,14 @@
 	if (v->current_order.type == OT_LOADING) return;
 
 	if (v->u.road.state == 254) {
-		int dir;
+		DiagDirection dir;
 		const RoadDriveEntry* rdp;
 		byte rd2;
 
 		v->cur_speed = 0;
 
 		dir = GB(_m[v->tile].m5, 0, 2);
-		v->direction = dir * 2 + 1;
+		v->direction = DiagDirToDir(dir);
 
 		rd2 = _roadveh_data_2[dir];
 		rdp = _road_drive_data[(_opt.road_side << 4) + rd2];
--- a/train_cmd.c
+++ b/train_cmd.c
@@ -522,7 +522,7 @@
 
 		if (flags & DC_EXEC) {
 			Vehicle *u, *w;
-			uint dir;
+			DiagDirection dir;
 
 			v = vl[0];
 			v->spritenum = rvi->image_index;
@@ -541,7 +541,7 @@
 
 			dir = GB(_m[tile].m5, 0, 2);
 
-			v->direction = dir * 2 + 1;
+			v->direction = DiagDirToDir(dir);
 			v->tile = tile;
 
 			x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir];
@@ -694,13 +694,13 @@
 			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
 
 		if (flags & DC_EXEC) {
-			uint dir;
+			DiagDirection dir;
 
 			v->unitnumber = unit_num;
 
 			dir = GB(_m[tile].m5, 0, 2);
 
-			v->direction = dir * 2 + 1;
+			v->direction = DiagDirToDir(dir);
 			v->tile = tile;
 			v->owner = _current_player;
 			v->x_pos = (x |= _vehicle_initial_x_fract[dir]);
@@ -1354,7 +1354,7 @@
 	return cost;
 }
 
-static void UpdateTrainDeltaXY(Vehicle *v, int direction)
+static void UpdateTrainDeltaXY(Vehicle *v, Direction direction)
 {
 #define MKIT(a,b,c,d) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0)
 	static const uint32 _delta_xy_table[8] = {
@@ -1744,7 +1744,6 @@
 // crashed!
 static TrainFindDepotData FindClosestTrainDepot(Vehicle *v)
 {
-	int i;
 	TrainFindDepotData tfdd;
 	TileIndex tile = v->tile;
 
@@ -1782,14 +1781,16 @@
 		}
 	} else {
 		// search in the forward direction first.
-		i = v->direction >> 1;
-		if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) i = (i - 1) & 3;
+		DiagDirection i;
+
+		i = DirToDiagDir(v->direction);
+		if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) i = (i + 3) & 3;
 		NewTrainPathfind(tile, 0, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
 		if (tfdd.best_length == (uint)-1){
 			tfdd.reverse = true;
 			// search in backwards direction
-			i = (v->direction^4) >> 1;
-			if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) i = (i - 1) & 3;
+			i = ReverseDiagDir(DirToDiagDir(v->direction));
+			if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) i = (i + 3) & 3;
 			NewTrainPathfind(tile, 0, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
 		}
 	}
@@ -2081,7 +2082,7 @@
 
 
 /* choose a track */
-static byte ChooseTrainTrack(Vehicle *v, TileIndex tile, int enterdir, TrackdirBits trackdirbits)
+static byte ChooseTrainTrack(Vehicle* v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirbits)
 {
 	TrainTrackFollowerData fd;
 	uint best_track;
@@ -2496,7 +2497,7 @@
 	2, 3, 4,
 };
 
-static int GetNewVehicleDirectionByTile(TileIndex new_tile, TileIndex old_tile)
+static Direction GetNewVehicleDirectionByTile(TileIndex new_tile, TileIndex old_tile)
 {
 	uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
 							TileX(new_tile) - TileX(old_tile) + 1;
@@ -2504,7 +2505,7 @@
 	return _new_vehicle_direction_table[offs];
 }
 
-static int GetNewVehicleDirection(const Vehicle *v, int x, int y)
+static Direction GetNewVehicleDirection(const Vehicle *v, int x, int y)
 {
 	uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
 	assert(offs < 11);
@@ -2620,7 +2621,7 @@
 	5, 7, 7, 5, 7, 1,
 };
 
-static void TrainMovedChangeSignals(TileIndex tile, int dir)
+static void TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
 {
 	if (IsTileType(tile, MP_RAILWAY) && (_m[tile].m5 & 0xC0) == 0x40) {
 		uint i = FindFirstBit2x64((_m[tile].m5 + (_m[tile].m5 << 8)) & _reachable_tracks[dir]);
@@ -2751,7 +2752,10 @@
 	Vehicle *prev;
 	GetNewVehiclePosResult gp;
 	uint32 r, tracks,ts;
-	int i, enterdir, newdir, dir;
+	int i;
+	DiagDirection enterdir;
+	Direction dir;
+	Direction newdir;
 	byte chosen_dir;
 	byte chosen_track;
 	byte old_z;
@@ -2795,7 +2799,7 @@
 				byte bits;
 				/* Determine what direction we're entering the new tile from */
 				dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile);
-				enterdir = dir >> 1;
+				enterdir = DirToDiagDir(dir);
 				assert(enterdir==0 || enterdir==1 || enterdir==2 || enterdir==3);
 
 				/* Get the status of the tracks in the new tile and mask
@@ -2871,7 +2875,7 @@
 				/* Signals can only change when the first
 				 * (above) or the last vehicle moves. */
 				if (v->next == NULL)
-					TrainMovedChangeSignals(gp.old_tile, (enterdir) ^ 2);
+					TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir));
 
 				if (prev == NULL) AffectSpeedByDirChange(v, chosen_dir);
 
@@ -3117,7 +3121,7 @@
 	tile = v->tile;
 
 	// tunnel entrance?
-	if (IsTunnelTile(tile) && GB(_m[tile].m5, 0, 2) * 2 + 1 == v->direction)
+	if (IsTunnelTile(tile) && DiagDirToDir(GB(_m[tile].m5, 0, 2)) == v->direction)
 		return true;
 
 	// depot?
--- a/tunnelbridge_cmd.c
+++ b/tunnelbridge_cmd.c
@@ -427,7 +427,7 @@
 	return cost;
 }
 
-static bool DoCheckTunnelInWay(TileIndex tile, uint z, uint dir)
+static bool DoCheckTunnelInWay(TileIndex tile, uint z, DiagDirection dir)
 {
 	TileIndexDiff delta = TileOffsByDir(dir);
 	uint height;
@@ -450,10 +450,11 @@
 
 bool CheckTunnelInWay(TileIndex tile, int z)
 {
-	return DoCheckTunnelInWay(tile,z,0) &&
-		DoCheckTunnelInWay(tile,z,1) &&
-		DoCheckTunnelInWay(tile,z,2) &&
-		DoCheckTunnelInWay(tile,z,3);
+	return
+		DoCheckTunnelInWay(tile, z, DIAGDIR_NE) &&
+		DoCheckTunnelInWay(tile, z, DIAGDIR_SE) &&
+		DoCheckTunnelInWay(tile, z, DIAGDIR_SW) &&
+		DoCheckTunnelInWay(tile, z, DIAGDIR_NW);
 }
 
 
@@ -535,7 +536,7 @@
 		_m[end_tile].m3 = GB(p1, 0, 4); // rail type (if any)
 		_m[end_tile].m5 = (GB(p1, 9, 1) << 2) | (direction ^ 2); // transport type and entrance direction
 
-		if (GB(p1, 9, 1) == 0) UpdateSignalsOnSegment(start_tile, direction << 1);
+		if (GB(p1, 9, 1) == 0) UpdateSignalsOnSegment(start_tile, DiagDirToDir(direction));
 	}
 
 	return cost;
@@ -545,7 +546,7 @@
 {
 	uint z = GetTileZ(tile);
 	byte m5 = _m[tile].m5;
-	int delta = TileOffsByDir(m5 & 3);
+	TileIndexDiff delta = TileOffsByDir(m5 & 3);
 	uint len = 0;
 	TileIndex starttile = tile;
 	Vehicle *v;
@@ -1407,11 +1408,11 @@
 
 static uint32 VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y)
 {
-	int dir, vdir;
-
 	if (GB(_m[tile].m5, 4, 4) == 0) {
 		int z = GetSlopeZ(x, y) - v->z_pos;
 		byte fc;
+		DiagDirection dir;
+		DiagDirection vdir;
 
 		if (myabs(z) > 2) return 8;
 
@@ -1419,7 +1420,7 @@
 			fc = (x & 0xF) + (y << 4);
 
 			dir = GB(_m[tile].m5, 0, 2);
-			vdir = v->direction >> 1;
+			vdir = DirToDiagDir(v->direction);
 
 			if (v->u.rail.track != 0x40 && dir == vdir) {
 				if (IsFrontEngine(v) && fc == _tunnel_fractcoord_1[dir]) {
@@ -1435,7 +1436,7 @@
 				}
 			}
 
-			if (dir == (vdir^2) && fc == _tunnel_fractcoord_3[dir] && z == 0) {
+			if (dir == ReverseDiagDir(vdir) && fc == _tunnel_fractcoord_3[dir] && z == 0) {
 				/* We're at the tunnel exit ?? */
 				v->tile = tile;
 				v->u.rail.track = _exit_tunnel_track[dir];
@@ -1446,7 +1447,7 @@
 		} else if (v->type == VEH_Road) {
 			fc = (x & 0xF) + (y << 4);
 			dir = GB(_m[tile].m5, 0, 2);
-			vdir = v->direction >> 1;
+			vdir = DirToDiagDir(v->direction);
 
 			// Enter tunnel?
 			if (v->u.road.state != 0xFF && dir == vdir) {
@@ -1461,7 +1462,7 @@
 				}
 			}
 
-			if (dir == (vdir ^ 2) && (
+			if (dir == ReverseDiagDir(vdir) && (
 						/* We're at the tunnel exit ?? */
 						fc == _tunnel_fractcoord_6[dir] ||
 						fc == _tunnel_fractcoord_7[dir]
--- a/vehicle.h
+++ b/vehicle.h
@@ -284,7 +284,7 @@
 Vehicle *FindVehicleBetween(TileIndex from, TileIndex to, byte z);
 TileIndex GetVehicleOutOfTunnelTile(const Vehicle *v);
 
-bool UpdateSignalsOnSegment(TileIndex tile, byte direction);
+bool UpdateSignalsOnSegment(TileIndex tile, Direction direction);
 void SetSignalsOnBothDir(TileIndex tile, byte track);
 
 Vehicle *CheckClickOnVehicle(const ViewPort *vp, int x, int y);