(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
authortron
Fri, 24 Mar 2006 08:55:08 +0000
changeset 3315 1f65f8260092
parent 3314 b8b234d4584f
child 3316 5d172314aa4e
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
ai/default/default.c
ai/trolly/trolly.c
aircraft_cmd.c
aircraft_gui.c
disaster_cmd.c
npf.c
order_gui.c
roadveh_cmd.c
station_cmd.c
station_map.h
train_cmd.c
--- a/ai/default/default.c	Fri Mar 24 08:00:45 2006 +0000
+++ b/ai/default/default.c	Fri Mar 24 08:55:08 2006 +0000
@@ -7,6 +7,7 @@
 #include "../../map.h"
 #include "../../rail_map.h"
 #include "../../road_map.h"
+#include "../../station_map.h"
 #include "../../tile.h"
 #include "../../player.h"
 #include "../../tunnel_map.h"
@@ -2313,11 +2314,11 @@
 	p->ai.banned_tile_count = 0;
 }
 
-static int AiGetStationIdByDef(TileIndex tile, int id)
+static StationID AiGetStationIdByDef(TileIndex tile, int id)
 {
 	const AiDefaultBlockData *p = _default_rail_track_data[id]->data;
 	while (p->mode != 1) p++;
-	return _m[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))].m2;
+	return GetStationIndex(TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)));
 }
 
 static void AiStateBuildRailVeh(Player *p)
@@ -3096,11 +3097,11 @@
 	p->ai.banned_tile_count = 0;
 }
 
-static int AiGetStationIdFromRoadBlock(TileIndex tile, int id)
+static StationID AiGetStationIdFromRoadBlock(TileIndex tile, int id)
 {
 	const AiDefaultBlockData *p = _road_default_block_data[id]->data;
 	while (p->mode != 1) p++;
-	return _m[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))].m2;
+	return GetStationIndex(TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)));
 }
 
 static void AiStateBuildRoadVehicles(Player *p)
@@ -3406,11 +3407,11 @@
 	p->ai.state = AIS_BUILD_AIRCRAFT_VEHICLES;
 }
 
-static int AiGetStationIdFromAircraftBlock(TileIndex tile, int id)
+static StationID AiGetStationIdFromAircraftBlock(TileIndex tile, int id)
 {
 	const AiDefaultBlockData *p = _airport_default_block_data[id];
 	while (p->mode != 1) p++;
-	return _m[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))].m2;
+	return GetStationIndex(TILE_ADD(tile, ToTileIndexDiff(p->tileoffs)));
 }
 
 static void AiStateBuildAircraftVehicles(Player *p)
@@ -3431,7 +3432,7 @@
 
 	/* XXX - Have the AI pick the hangar terminal in an airport. Eg get airport-type
 	 * and offset to the FIRST depot because the AI picks the st->xy tile */
-	tile += ToTileIndexDiff(GetAirport(GetStation(_m[tile].m2)->airport_type)->airport_depots[0]);
+	tile += ToTileIndexDiff(GetAirport(GetStationByTile(tile)->airport_type)->airport_depots[0]);
 	if (CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT))) return;
 	loco_id = _new_aircraft_id;
 
--- a/ai/trolly/trolly.c	Fri Mar 24 08:00:45 2006 +0000
+++ b/ai/trolly/trolly.c	Fri Mar 24 08:55:08 2006 +0000
@@ -22,6 +22,7 @@
 #include "../../debug.h"
 #include "../../functions.h"
 #include "../../road_map.h"
+#include "../../station_map.h"
 #include "../../table/strings.h"
 #include "../../map.h"
 #include "../../tile.h"
@@ -1202,7 +1203,7 @@
 	idx = 0;
 	order.type = OT_GOTO_STATION;
 	order.flags = 0;
-	order.station = _m[p->ainew.to_tile].m2;
+	order.station = GetStationIndex(p->ainew.to_tile);
 	if (p->ainew.tbt == AI_TRUCK && p->ainew.to_deliver)
 		order.flags |= OF_FULL_LOAD;
 	AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
@@ -1210,7 +1211,7 @@
 	idx = 0;
 	order.type = OT_GOTO_STATION;
 	order.flags = 0;
-	order.station = _m[p->ainew.from_tile].m2;
+	order.station = GetStationIndex(p->ainew.from_tile);
 	if (p->ainew.tbt == AI_TRUCK && p->ainew.from_deliver)
 		order.flags |= OF_FULL_LOAD;
 	AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
--- a/aircraft_cmd.c	Fri Mar 24 08:00:45 2006 +0000
+++ b/aircraft_cmd.c	Fri Mar 24 08:55:08 2006 +0000
@@ -4,6 +4,7 @@
 #include "openttd.h"
 #include "debug.h"
 #include "functions.h"
+#include "station_map.h"
 #include "table/strings.h"
 #include "map.h"
 #include "tile.h"
@@ -239,7 +240,7 @@
 		 * layout for #th position of depot. Since layout must start with a listing
 		 * of all depots, it is simple */
 		{
-			const Station* st = GetStation(_m[tile].m2);
+			const Station* st = GetStationByTile(tile);
 			const AirportFTAClass* apc = GetAirport(st->airport_type);
 			uint i;
 
@@ -256,7 +257,7 @@
 
 		v->u.air.state = HANGAR;
 		v->u.air.previous_pos = v->u.air.pos;
-		v->u.air.targetairport = _m[tile].m2;
+		v->u.air.targetairport = GetStationIndex(tile);
 		v->next = u;
 
 		v->service_interval = _patches.servint_aircraft;
--- a/aircraft_gui.c	Fri Mar 24 08:00:45 2006 +0000
+++ b/aircraft_gui.c	Fri Mar 24 08:55:08 2006 +0000
@@ -4,6 +4,7 @@
 #include "openttd.h"
 #include "debug.h"
 #include "functions.h"
+#include "station_map.h"
 #include "table/sprites.h"
 #include "table/strings.h"
 #include "map.h"
@@ -650,7 +651,7 @@
 	}
 	SetVScrollCount(w, (num + w->hscroll.cap - 1) / w->hscroll.cap);
 
-	SetDParam(0, _m[tile].m2);
+	SetDParam(0, GetStationIndex(tile));
 	DrawWindowWidgets(w);
 
 	x = 2;
--- a/disaster_cmd.c	Fri Mar 24 08:00:45 2006 +0000
+++ b/disaster_cmd.c	Fri Mar 24 08:55:08 2006 +0000
@@ -3,6 +3,7 @@
 #include "stdafx.h"
 #include "openttd.h"
 #include "industry_map.h"
+#include "station_map.h"
 #include "table/strings.h"
 #include "functions.h"
 #include "map.h"
@@ -183,7 +184,7 @@
 				v->current_order.station = 1;
 				v->age = 0;
 
-				SetDParam(0, _m[tile].m2);
+				SetDParam(0, GetStationIndex(tile));
 				AddNewsItem(STR_B000_ZEPPELIN_DISASTER_AT,
 					NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0),
 					v->index,
@@ -205,7 +206,7 @@
 				IsTileType(tile, MP_STATION) &&
 				IS_BYTE_INSIDE(_m[tile].m5, 8, 0x43) &&
 				IS_HUMAN_PLAYER(GetTileOwner(tile))) {
-			st = GetStation(_m[tile].m2);
+			st = GetStationByTile(tile);
 			CLRBITS(st->airport_flags, RUNWAY_IN_block);
 		}
 
@@ -247,7 +248,7 @@
 			IsTileType(tile, MP_STATION) &&
 			IS_BYTE_INSIDE(_m[tile].m5, 8, 0x43) &&
 			IS_HUMAN_PLAYER(GetTileOwner(tile))) {
-		st = GetStation(_m[tile].m2);
+		st = GetStationByTile(tile);
 		SETBITS(st->airport_flags, RUNWAY_IN_block);
 	}
 }
--- a/npf.c	Fri Mar 24 08:00:45 2006 +0000
+++ b/npf.c	Fri Mar 24 08:55:08 2006 +0000
@@ -10,6 +10,7 @@
 #include "macros.h"
 #include "pathfind.h"
 #include "station.h"
+#include "station_map.h"
 #include "tile.h"
 #include "depot.h"
 #include "tunnel_map.h"
@@ -423,7 +424,7 @@
 	 * is correct */
 	if (
 		(fstd->station_index == INVALID_STATION && tile == fstd->dest_coords) || /* We've found the tile, or */
-		(IsTileType(tile, MP_STATION) && _m[tile].m2 == fstd->station_index) /* the station */
+		(IsTileType(tile, MP_STATION) && GetStationIndex(tile) == fstd->station_index) /* the station */
 	) {
 		return AYSTAR_FOUND_END_NODE;
 	} else {
--- a/order_gui.c	Fri Mar 24 08:00:45 2006 +0000
+++ b/order_gui.c	Fri Mar 24 08:55:08 2006 +0000
@@ -3,6 +3,7 @@
 #include "stdafx.h"
 #include "openttd.h"
 #include "road_map.h"
+#include "station_map.h"
 #include "table/sprites.h"
 #include "table/strings.h"
 #include "functions.h"
@@ -190,7 +191,6 @@
 static Order GetOrderCmdFromTile(const Vehicle *v, TileIndex tile)
 {
 	Order order;
-	int st_index;
 
 	// check depot first
 	if (_patches.gotodepot) {
@@ -221,7 +221,7 @@
 			if (IsAircraftHangarTile(tile) && IsTileOwner(tile, _local_player)) {
 				order.type = OT_GOTO_DEPOT;
 				order.flags = OF_PART_OF_ORDERS;
-				order.station = _m[tile].m2;
+				order.station = GetStationIndex(tile);
 				return order;
 			}
 			break;
@@ -257,7 +257,8 @@
 	}
 
 	if (IsTileType(tile, MP_STATION)) {
-		const Station* st = GetStation(st_index = _m[tile].m2);
+		StationID st_index = GetStationIndex(tile);
+		const Station* st = GetStation(st_index);
 
 		if (st->owner == _current_player || st->owner == OWNER_NONE) {
 			byte facil;
--- a/roadveh_cmd.c	Fri Mar 24 08:00:45 2006 +0000
+++ b/roadveh_cmd.c	Fri Mar 24 08:55:08 2006 +0000
@@ -5,6 +5,7 @@
 #include "debug.h"
 #include "functions.h"
 #include "road_map.h"
+#include "station_map.h"
 #include "table/strings.h"
 #include "map.h"
 #include "tile.h"
@@ -1415,7 +1416,7 @@
 	if (v->u.road.state >= 0x20 &&
 			_road_veh_data_1[v->u.road.state - 0x20 + (_opt.road_side<<4)] == v->u.road.frame) {
 		RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
-		Station* st = GetStation(_m[v->tile].m2);
+		Station* st = GetStationByTile(v->tile);
 
 		if (v->current_order.type != OT_LEAVESTATION &&
 				v->current_order.type != OT_GOTO_DEPOT) {
@@ -1423,7 +1424,7 @@
 
 			CLRBIT(rs->status, 7);
 
-			v->last_station_visited = _m[v->tile].m2;
+			v->last_station_visited = GetStationIndex(v->tile);
 
 			RoadVehArrivesAt(v, st);
 
--- a/station_cmd.c	Fri Mar 24 08:00:45 2006 +0000
+++ b/station_cmd.c	Fri Mar 24 08:55:08 2006 +0000
@@ -7,6 +7,7 @@
 #include "openttd.h"
 #include "debug.h"
 #include "functions.h"
+#include "station_map.h"
 #include "table/sprites.h"
 #include "table/strings.h"
 #include "map.h"
@@ -110,7 +111,7 @@
 
 RoadStop* GetRoadStopByTile(TileIndex tile, RoadStopType type)
 {
-	const Station* st = GetStation(_m[tile].m2);
+	const Station* st = GetStationByTile(tile);
 	RoadStop* rs;
 
 	for (rs = GetPrimaryRoadStop(st, type); rs->xy != tile; rs = rs->next) {
@@ -184,7 +185,7 @@
 	// check around to see if there's any stations there
 	BEGIN_TILE_LOOP(tile_cur, w + 2, h + 2, tile - TileDiffXY(1, 1))
 		if (IsTileType(tile_cur, MP_STATION)) {
-			StationID t = _m[tile_cur].m2;
+			StationID t = GetStationIndex(tile_cur);
 			{
 				Station *st = GetStation(t);
 				// you cannot take control of an oilrig!!
@@ -232,7 +233,7 @@
 	uint t;
 
 	for (i = 0; i != MapSize(); i++) {
-		if (IsTileType(i, MP_STATION) && _m[i].m2 == station_index) {
+		if (IsTileType(i, MP_STATION) && GetStationIndex(i) == station_index) {
 			t = TileX(i);
 			if (t < x1) x1 = t;
 			if (t > x2) x2 = t;
@@ -797,7 +798,7 @@
 			if (_m[tile_cur].m5 >= 8) {
 				return ClearTile_Station(tile_cur, DC_AUTO); // get error message
 			} else {
-				StationID st = _m[tile_cur].m2;
+				StationID st = GetStationIndex(tile_cur);
 				if (*station == INVALID_STATION) {
 					*station = st;
 				} else if (*station != st) {
@@ -1069,7 +1070,7 @@
 
 static bool TileBelongsToRailStation(const Station *st, TileIndex tile)
 {
-	return IsTileType(tile, MP_STATION) && _m[tile].m2 == st->index && _m[tile].m5 < 8;
+	return IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st->index && _m[tile].m5 < 8;
 }
 
 static void MakeRailwayStationAreaSmaller(Station *st)
@@ -1144,7 +1145,7 @@
 
 	// make sure the specified tile belongs to the current player, and that it is a railroad station.
 	if (!IsTileType(tile, MP_STATION) || _m[tile].m5 >= 8 || !_patches.nonuniform_stations) return CMD_ERROR;
-	st = GetStation(_m[tile].m2);
+	st = GetStationByTile(tile);
 	if (_current_player != OWNER_WATER && (!CheckOwnership(st->owner) || !EnsureNoVehicle(tile))) return CMD_ERROR;
 
 	// if we reached here, it means we can actually delete it. do that.
@@ -1252,7 +1253,8 @@
 
 int32 DoConvertStationRail(TileIndex tile, uint totype, bool exec)
 {
-	const Station *st = GetStation(_m[tile].m2);
+	const Station* st = GetStationByTile(tile);
+
 	if (!CheckOwnership(st->owner) || !EnsureNoVehicle(tile)) return CMD_ERROR;
 
 	// tile is not a railroad station?
@@ -1971,7 +1973,7 @@
 		//debug("Cust-o-mized %p", statspec);
 
 		if (statspec != NULL) {
-			const Station* st = GetStation(_m[ti->tile].m2);
+			const Station* st = GetStationByTile(ti->tile);
 
 			relocation = GetCustomStationRelocation(statspec, st, 0);
 			//debug("Relocation %d", relocation);
@@ -2051,7 +2053,7 @@
 	StringID str;
 
 	td->owner = GetTileOwner(tile);
-	td->build_date = GetStation(_m[tile].m2)->build_date;
+	td->build_date = GetStationByTile(tile)->build_date;
 
 	m5 = _m[tile].m5;
 	(str=STR_305E_RAILROAD_STATION, m5 < 8) ||
@@ -2166,7 +2168,7 @@
 	if (_m[tile].m5 == 32 || _m[tile].m5 == 65) {
 		ShowAircraftDepotWindow(tile);
 	} else {
-		ShowStationViewWindow(_m[tile].m2);
+		ShowStationViewWindow(GetStationIndex(tile));
 	}
 }
 
@@ -2179,7 +2181,7 @@
 	if (v->type == VEH_Train) {
 		if (IS_BYTE_INSIDE(_m[tile].m5, 0, 8) && IsFrontEngine(v) &&
 				!IsCompatibleTrainStationTile(tile + TileOffsByDir(DirToDiagDir(v->direction)), tile)) {
-			StationID station_id = _m[tile].m2;
+			StationID station_id = GetStationIndex(tile);
 
 			if ((!(v->current_order.flags & OF_NON_STOP) && !_patches.new_nonstop) ||
 					(v->current_order.type == OT_GOTO_STATION && v->current_order.station == station_id)) {
@@ -2563,7 +2565,7 @@
 		cur_tile = TILE_MASK(cur_tile);
 		if (!IsTileType(cur_tile, MP_STATION)) continue;
 
-		st = GetStation(_m[cur_tile].m2);
+		st = GetStationByTile(cur_tile);
 
 		for (i = 0; i != lengthof(around); i++) {
 			if (around[i] == NULL) {
@@ -2721,7 +2723,7 @@
 
 void DeleteOilRig(TileIndex tile)
 {
-	Station *st = GetStation(_m[tile].m2);
+	Station* st = GetStationByTile(tile);
 
 	DoClearSquare(tile);
 
@@ -2738,7 +2740,8 @@
 	if (!IsTileOwner(tile, old_player)) return;
 
 	if (new_player != OWNER_SPECTATOR) {
-		Station *st = GetStation(_m[tile].m2);
+		Station* st = GetStationByTile(tile);
+
 		SetTileOwner(tile, new_player);
 		st->owner = new_player;
 		_global_station_sort_dirty = true; // transfer ownership of station to another player
@@ -2764,7 +2767,7 @@
 		return_cmd_error(STR_4800_IN_THE_WAY);
 	}
 
-	st = GetStation(_m[tile].m2);
+	st = GetStationByTile(tile);
 
 	if (m5 < 8) return RemoveRailroadStation(st, tile, flags);
 	// original airports < 67, new airports between 83 - 114
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/station_map.h	Fri Mar 24 08:55:08 2006 +0000
@@ -0,0 +1,14 @@
+/* $Id$ */
+
+#include "station.h"
+
+
+static inline StationID GetStationIndex(TileIndex t)
+{
+	return (StationID)_m[t].m2;
+}
+
+static inline Station* GetStationByTile(TileIndex t)
+{
+	return GetStation(GetStationIndex(t));
+}
--- a/train_cmd.c	Fri Mar 24 08:00:45 2006 +0000
+++ b/train_cmd.c	Fri Mar 24 08:55:08 2006 +0000
@@ -6,6 +6,7 @@
 #include "debug.h"
 #include "functions.h"
 #include "gui.h"
+#include "station_map.h"
 #include "table/strings.h"
 #include "map.h"
 #include "tile.h"
@@ -196,21 +197,20 @@
 static bool TrainShouldStop(const Vehicle* v, TileIndex tile)
 {
 	const Order* o = &v->current_order;
+	StationID sid = GetStationIndex(tile);
 
 	assert(v->type == VEH_Train);
-	assert(IsTileType(v->tile, MP_STATION));
 	//When does a train drive through a station
 	//first we deal with the "new nonstop handling"
-	if (_patches.new_nonstop && o->flags & OF_NON_STOP &&
-			_m[tile].m2 == o->station )
+	if (_patches.new_nonstop && o->flags & OF_NON_STOP && sid == o->station) {
 		return false;
-
-	if (v->last_station_visited == _m[tile].m2)
+	}
+
+	if (v->last_station_visited == sid) return false;
+
+	if (sid != o->station && (o->flags & OF_NON_STOP || _patches.new_nonstop)) {
 		return false;
-
-	if (_m[tile].m2 != o->station &&
-			(o->flags & OF_NON_STOP || _patches.new_nonstop))
-		return false;
+	}
 
 	return true;
 }
@@ -2027,8 +2027,11 @@
 	if (ttfd->dest_coords == 0) return false;
 
 	// did we reach the final station?
-	if ((ttfd->station_index == INVALID_STATION && tile == ttfd->dest_coords) ||
-			(IsTileType(tile, MP_STATION) && IS_BYTE_INSIDE(_m[tile].m5, 0, 8) && _m[tile].m2 == ttfd->station_index)) {
+	if ((ttfd->station_index == INVALID_STATION && tile == ttfd->dest_coords) || (
+				IsTileType(tile, MP_STATION) &&
+				IS_BYTE_INSIDE(_m[tile].m5, 0, 8) &&
+				GetStationIndex(tile) == ttfd->station_index
+			)) {
 		/* We do not check for dest_coords if we have a station_index,
 		 * because in that case the dest_coords are just an
 		 * approximation of where the station is */
@@ -2301,7 +2304,7 @@
 	if (_patches.new_nonstop &&
 			v->current_order.flags & OF_NON_STOP &&
 			IsTileType(v->tile, MP_STATION) &&
-			v->current_order.station == _m[v->tile].m2) {
+			v->current_order.station == GetStationIndex(v->tile)) {
 		v->cur_order_index++;
 	}