changeset 3069:a7c09d88896f draft

(svn r3658) Add functions and symbolic names to retrieve road tile types and road pieces
author tron <tron@openttd.org>
date Thu, 23 Feb 2006 08:20:28 +0000
parents 010d0d9b87cd
children 9c9666dd671a
files rail_cmd.c road.h road_cmd.c roadveh_cmd.c tunnelbridge_cmd.c
diffstat 5 files changed, 108 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/rail_cmd.c
+++ b/rail_cmd.c
@@ -4,6 +4,7 @@
 #include "openttd.h"
 #include "debug.h"
 #include "functions.h"
+#include "road.h"
 #include "table/sprites.h"
 #include "table/strings.h"
 #include "map.h"
@@ -348,9 +349,9 @@
 				return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
 			if (!EnsureNoVehicle(tile)) return CMD_ERROR;
 
-			if ((m5 & 0xF0) == 0 && ( // normal road?
-						(track == TRACK_DIAG1 && m5 == 0x05) ||
-						(track == TRACK_DIAG2 && m5 == 0x0A) // correct direction?
+			if (GetRoadType(tile) == ROAD_NORMAL && (
+						(track == TRACK_DIAG1 && m5 == ROAD_Y) ||
+						(track == TRACK_DIAG2 && m5 == ROAD_X) // correct direction?
 					)) {
 				if (flags & DC_EXEC) {
 					_m[tile].m3 = GetTileOwner(tile);
new file mode 100644
--- /dev/null
+++ b/road.h
@@ -0,0 +1,34 @@
+/* $Id$ */
+
+#ifndef ROAD_H
+#define ROAD_H
+
+#include "macros.h"
+
+typedef enum RoadBits {
+	ROAD_NW  = 1,
+	ROAD_SW  = 2,
+	ROAD_SE  = 4,
+	ROAD_NE  = 8,
+	ROAD_X   = ROAD_SW | ROAD_NE,
+	ROAD_Y   = ROAD_NW | ROAD_SE,
+	ROAD_ALL = ROAD_X  | ROAD_Y
+} RoadBits;
+
+static inline RoadBits GetRoadBits(TileIndex tile)
+{
+	return GB(_m[tile].m5, 0, 4);
+}
+
+typedef enum RoadType {
+	ROAD_NORMAL,
+	ROAD_CROSSING,
+	ROAD_DEPOT
+} RoadType;
+
+static inline RoadType GetRoadType(TileIndex tile)
+{
+	return GB(_m[tile].m5, 4, 4);
+}
+
+#endif
--- a/road_cmd.c
+++ b/road_cmd.c
@@ -2,6 +2,7 @@
 
 #include "stdafx.h"
 #include "openttd.h"
+#include "road.h"
 #include "table/sprites.h"
 #include "table/strings.h"
 #include "functions.h"
@@ -27,17 +28,15 @@
 
 static bool HasTileRoadAt(TileIndex tile, int i)
 {
-	byte b;
+	RoadBits b;
 
 	switch (GetTileType(tile)) {
 		case MP_STREET:
-			b = _m[tile].m5;
-
-			switch (GB(b, 4, 4)) {
-				case 0: break; // normal road
-				case 1: b = (b & 8 ? 5 : 10); break; // level crossing
-				case 2: return (~b & 3) == i; // depot
-				default: return false;
+			switch (GetRoadType(tile)) {
+				case ROAD_NORMAL:   b = GetRoadBits(tile); break;
+				case ROAD_CROSSING: b = (_m[tile].m5 & 8 ? ROAD_Y : ROAD_X); break;
+				case ROAD_DEPOT:    return (~_m[tile].m5 & 3) == i;
+				default:            return false;
 			}
 			break;
 
@@ -50,7 +49,7 @@
 			// bail out, if not a bridge middle part with road underneath
 			if ((_m[tile].m5 & 0xF8) != 0xE8) return false;
 			// road direction perpendicular to bridge
-			b = (_m[tile].m5 & 0x01) ? 10 : 5;
+			b = (_m[tile].m5 & 0x01) ? ROAD_X : ROAD_Y;
 
 		default:
 			return false;
@@ -137,12 +136,13 @@
 	/* true if the roadpiece was always removeable,
 	 * false if it was a center piece. Affects town ratings drop */
 	bool edge_road;
-	byte pieces = (byte)p1;
+	RoadBits pieces;
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
 	/* Road pieces are max 4 bitset values (NE, NW, SE, SW) */
-	if (pieces >> 4) return CMD_ERROR;
+	if (p1 >> 4) return CMD_ERROR;
+	pieces = p1;
 
 	FindLandscapeHeight(&ti, x, y);
 	tile = ti.tile;
@@ -179,9 +179,9 @@
 			if (!EnsureNoVehicleZ(tile, TilePixelHeight(tile))) return CMD_ERROR;
 
 			if ((ti.map5 & 0xE9) == 0xE8) {
-				if (pieces & 10) goto return_error;
+				if (pieces & ROAD_X) goto return_error;
 			} else if ((ti.map5 & 0xE9) == 0xE9) {
-				if (pieces & 5) goto return_error;
+				if (pieces & ROAD_Y) goto return_error;
 			} else {
 				goto return_error;
 			}
@@ -201,11 +201,11 @@
 			// removal allowance depends on difficulty setting
 			if (!CheckforTownRating(flags, t, ROAD_REMOVE)) return CMD_ERROR;
 
-			switch (GB(ti.map5, 4, 4)) {
-				case 0: { // normal road
+			switch (GetRoadType(ti.tile)) {
+				case ROAD_NORMAL: {
 					byte c = pieces, t2;
 
-					if (ti.tileh != 0  && (ti.map5 == 5 || ti.map5 == 10)) {
+					if (ti.tileh != 0  && (ti.map5 == ROAD_Y || ti.map5 == ROAD_X)) {
 						c |= (c & 0xC) >> 2;
 						c |= (c & 0x3) << 2;
 					}
@@ -233,15 +233,15 @@
 					return cost;
 				}
 
-				case 1: { // level crossing
+				case ROAD_CROSSING: {
 					byte c;
 
 					if (!(ti.map5 & 8)) {
 						c = 2;
-						if (pieces & 5) goto return_error;
+						if (pieces & ROAD_Y) goto return_error;
 					} else {
 						c = 1;
-						if (pieces & 10) goto return_error;
+						if (pieces & ROAD_X) goto return_error;
 					}
 
 					cost = _price.remove_road * 2;
@@ -258,7 +258,8 @@
 					return cost;
 				}
 
-				default: // depot
+				default:
+				case ROAD_DEPOT:
 					goto return_error;
 			}
 
@@ -269,14 +270,6 @@
 }
 
 
-enum {
-	ROAD_NW = 1, // NW road track
-	ROAD_SW = 2, // SW road track
-	ROAD_SE = 4, // SE road track
-	ROAD_NE = 8, // NE road track
-	ROAD_ALL = (ROAD_NW | ROAD_SW | ROAD_SE | ROAD_NE)
-};
-
 static const byte _valid_tileh_slopes_road[3][15] = {
 	// set of normal ones
 	{
@@ -372,21 +365,22 @@
 
 	switch (ti.type) {
 		case MP_STREET:
-			switch (GB(ti.map5, 4, 4)) {
-				case 0: // normal road
-					if ((pieces & (byte)ti.map5) == pieces) {
+			switch (GetRoadType(ti.tile)) {
+				case ROAD_NORMAL:
+					if ((GetRoadBits(ti.tile) & pieces) == pieces) {
 						return_cmd_error(STR_1007_ALREADY_BUILT);
 					}
 					existing = ti.map5;
 					break;
 
-				case 1: // level crossing
-					if (pieces != (ti.map5 & 8 ? 5 : 10)) {
+				case ROAD_CROSSING:
+					if (pieces != (ti.map5 & 8 ? ROAD_Y : ROAD_X)) { // XXX is this correct?
 						return_cmd_error(STR_1007_ALREADY_BUILT);
 					}
 					goto do_clear;
 
-				default: // depot
+				default:
+				case ROAD_DEPOT:
 					goto do_clear;
 			}
 			break;
@@ -403,10 +397,10 @@
 			}
 
 			if (ti.map5 == 2) {
-				if (pieces & 5) goto do_clear;
+				if (pieces & ROAD_Y) goto do_clear;
 				m5 = 0x10;
 			} else if (ti.map5 == 1) {
-				if (pieces & 10) goto do_clear;
+				if (pieces & ROAD_X) goto do_clear;
 				m5 = 0x18;
 			} else {
 				goto do_clear;
@@ -468,7 +462,7 @@
 	if (cost && (!_patches.build_on_slopes || _is_old_ai_player))
 		return CMD_ERROR;
 
-	if (ti.type != MP_STREET || (ti.map5 & 0xF0) != 0) {
+	if (ti.type != MP_STREET || GetRoadType(ti.tile) != ROAD_NORMAL) {
 		cost += DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 	} else {
 		// Don't put the pieces that already exist
@@ -698,11 +692,9 @@
 
 static int32 ClearTile_Road(TileIndex tile, byte flags)
 {
-	byte m5 = _m[tile].m5;
-
-	switch (GB(_m[tile].m5, 4, 4)) {
-		case 0: { // normal road
-			byte b = m5 & 0xF;
+	switch (GetRoadType(tile)) {
+		case ROAD_NORMAL: {
+			RoadBits b = GetRoadBits(tile);
 
 			if (!((1 << b) & (M(1)|M(2)|M(4)|M(8))) &&
 					(!(flags & DC_AI_BUILDING) || !IsTileOwner(tile, OWNER_TOWN)) &&
@@ -712,14 +704,12 @@
 			return DoCommandByTile(tile, b, 0, flags, CMD_REMOVE_ROAD);
 		}
 
-		case 1: { // level crossing
+		case ROAD_CROSSING: {
 			int32 ret;
 
-			if (flags & DC_AUTO) {
-				return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
-			}
+			if (flags & DC_AUTO) return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
 
-			ret = DoCommandByTile(tile, (m5 & 8 ? 5 : 10), 0, flags, CMD_REMOVE_ROAD);
+			ret = DoCommandByTile(tile, (_m[tile].m5 & 8 ? ROAD_Y : ROAD_X), 0, flags, CMD_REMOVE_ROAD);
 			if (CmdFailed(ret)) return CMD_ERROR;
 
 			if (flags & DC_EXEC) {
@@ -728,7 +718,8 @@
 			return ret;
 		}
 
-		default: // depot
+		default:
+		case ROAD_DEPOT:
 			if (flags & DC_AUTO) {
 				return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
 			}
@@ -1040,7 +1031,7 @@
 			break;
 	}
 
-	if (_m[tile].m5 & 0xE0) return;
+	if (GetRoadType(tile) == ROAD_DEPOT) return;
 
 	if (GB(_m[tile].m4, 4, 3) < 6) {
 		t = ClosestTownFromTile(tile, (uint)-1);
@@ -1052,7 +1043,7 @@
 			// Show an animation to indicate road work
 			if (t->road_build_months != 0 &&
 					!(DistanceManhattan(t->xy, tile) >= 8 && grp == 0) &&
-					(_m[tile].m5 == 5 || _m[tile].m5 == 10)) {
+					(_m[tile].m5 == ROAD_Y || _m[tile].m5 == ROAD_X)) {
 				if (GetTileSlope(tile, NULL) == 0 && EnsureNoVehicle(tile) && CHANCE16(1, 20)) {
 					_m[tile].m4 |= (GB(_m[tile].m4, 4, 3) <=  2 ? 7 : 6) << 4;
 
@@ -1106,7 +1097,7 @@
 
 static void ClickTile_Road(TileIndex tile)
 {
-	if (GB(_m[tile].m5, 4, 4) == 2) ShowRoadDepotWindow(tile);
+	if (GetRoadType(tile) == ROAD_DEPOT) ShowRoadDepotWindow(tile);
 }
 
 static const byte _road_trackbits[16] = {
@@ -1122,19 +1113,18 @@
 
 		case TRANSPORT_ROAD:
 			switch (GB(_m[tile].m5, 4, 4)) {
-				case 0: // normal road
-					if (!_road_special_gettrackstatus && GB(_m[tile].m4, 4, 3) >= 6) {
-						return 0;
-					}
-					return _road_trackbits[GB(_m[tile].m5, 0, 4)] * 0x101;
+				case ROAD_NORMAL:
+					return !_road_special_gettrackstatus && GB(_m[tile].m4, 4, 3) >= 6 ?
+						0 : _road_trackbits[GetRoadBits(tile)] * 0x101;
 
-				case 1: { // level crossing
+				case ROAD_CROSSING: {
 					uint32 r = (_m[tile].m5 & 8 ? 0x202 : 0x101);
 					if (_m[tile].m5 & 4) r *= 0x10001;
 					return r;
 				}
 
-				default: // depot
+				default:
+				case ROAD_DEPOT:
 					break;
 			}
 			break;
@@ -1145,9 +1135,6 @@
 }
 
 static const StringID _road_tile_strings[] = {
-	STR_1818_ROAD_RAIL_LEVEL_CROSSING,
-	STR_1817_ROAD_VEHICLE_DEPOT,
-
 	STR_1814_ROAD,
 	STR_1814_ROAD,
 	STR_1814_ROAD,
@@ -1160,10 +1147,12 @@
 
 static void GetTileDesc_Road(TileIndex tile, TileDesc *td)
 {
-	int i = GB(_m[tile].m5, 4, 4);
-	if (i == 0) i = GB(_m[tile].m4, 4, 3) + 3;
-	td->str = _road_tile_strings[i - 1];
 	td->owner = GetTileOwner(tile);
+	switch (GetRoadType(tile)) {
+		case ROAD_CROSSING: td->str = STR_1818_ROAD_RAIL_LEVEL_CROSSING; break;
+		case ROAD_DEPOT: td->str = STR_1817_ROAD_VEHICLE_DEPOT; break;
+		default: td->str = _road_tile_strings[GB(_m[tile].m4, 4, 3)]; break;
+	}
 }
 
 static const byte _roadveh_enter_depot_unk0[4] = {
@@ -1172,8 +1161,8 @@
 
 static uint32 VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
 {
-	switch (GB(_m[tile].m5, 4, 4)) {
-		case 1: // level crossing
+	switch (GetRoadType(tile)) {
+		case ROAD_CROSSING:
 			if (v->type == VEH_Train && GB(_m[tile].m5, 2, 1) == 0) {
 				/* train crossing a road */
 				SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v);
@@ -1182,7 +1171,7 @@
 			}
 			break;
 
-		case 2: // depot
+		case ROAD_DEPOT:
 			if (v->type == VEH_Road && v->u.road.frame == 11) {
 				if (_roadveh_enter_depot_unk0[GB(_m[tile].m5, 0, 2)] == v->u.road.state) {
 					RoadVehEnterDepot(v);
@@ -1217,19 +1206,20 @@
 	if (new_player != OWNER_SPECTATOR) {
 		SetTileOwner(tile, new_player);
 	}	else {
-		switch (GB(_m[tile].m5, 4, 4)) {
-			case 0: // normal road
+		switch (GetRoadType(tile)) {
+			case ROAD_NORMAL:
 				SetTileOwner(tile, OWNER_NONE);
 				break;
 
-			case 1: // level crossing
+			case ROAD_CROSSING:
 				_m[tile].m5 = (_m[tile].m5&8) ? 0x5 : 0xA;
 				SetTileOwner(tile, _m[tile].m3);
 				_m[tile].m3 = 0;
 				_m[tile].m4 &= 0x80;
 				break;
 
-			default: // depot
+			default:
+			case ROAD_DEPOT:
 				DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 				break;
 		}
--- a/roadveh_cmd.c
+++ b/roadveh_cmd.c
@@ -4,6 +4,7 @@
 #include "openttd.h"
 #include "debug.h"
 #include "functions.h"
+#include "road.h"
 #include "table/strings.h"
 #include "map.h"
 #include "tile.h"
@@ -288,7 +289,7 @@
 	tile += TileOffsByDir(_road_pf_directions[track]);
 
 	if (IsTileType(tile, MP_STREET) &&
-			GB(_m[tile].m5, 4, 4) == 2 &&
+			GetRoadType(tile) == ROAD_DEPOT &&
 			IsTileOwner(tile, rfdd->owner)) {
 
 		if (length < rfdd->best_length) {
@@ -978,7 +979,7 @@
 	}
 
 	if (IsTileType(tile, MP_STREET)) {
-		if (GB(_m[tile].m5, 4, 4) == 2 && IsTileOwner(tile, v->owner)) {
+		if (GetRoadType(tile) == ROAD_DEPOT && IsTileOwner(tile, v->owner)) {
 			/* Road depot */
 			bitmask |= _road_veh_fp_ax_or[GB(_m[tile].m5, 0, 2)];
 		}
@@ -1052,9 +1053,7 @@
 	} else {
 		if (IsTileType(desttile, MP_STREET)) {
 			m5 = _m[desttile].m5;
-			if ((m5 & 0xF0) == 0x20)
-				/* We are heading for a Depot */
-				goto do_it;
+			if (GetRoadType(desttile) == ROAD_DEPOT) goto do_it;
 		} else if (IsTileType(desttile, MP_STATION)) {
 			m5 = _m[desttile].m5;
 			if (IS_BYTE_INSIDE(m5, 0x43, 0x4B)) {
--- a/tunnelbridge_cmd.c
+++ b/tunnelbridge_cmd.c
@@ -7,6 +7,7 @@
 
 #include "stdafx.h"
 #include "openttd.h"
+#include "road.h"
 #include "table/sprites.h"
 #include "table/strings.h"
 #include "functions.h"
@@ -352,7 +353,10 @@
 				break;
 
 			case MP_STREET:
-				if (ti.map5 != (direction == 0 ? 5 : 10)) goto not_valid_below;
+				if (GetRoadType(ti.tile) != ROAD_NORMAL ||
+						GetRoadBits(ti.tile) != (direction == 0 ? ROAD_Y : ROAD_X)) {
+					goto not_valid_below;
+				}
 				m5 = 0xE8;
 				break;