waypoint.c
changeset 2670 34970983ef08
parent 2668 54d9f9d4dca3
child 2752 b5fe5a7e6282
--- a/waypoint.c	Wed Nov 16 16:16:18 2005 +0000
+++ b/waypoint.c	Wed Nov 16 22:20:15 2005 +0000
@@ -63,19 +63,6 @@
 	return NULL;
 }
 
-/* Fetch a waypoint by tile */
-Waypoint *GetWaypointByTile(TileIndex tile)
-{
-	Waypoint *wp;
-
-	FOR_ALL_WAYPOINTS(wp) {
-		if (wp->xy == tile)
-			return wp;
-	}
-
-	return NULL;
-}
-
 /* Update the sign for the waypoint */
 void UpdateWaypointSign(Waypoint *wp)
 {
@@ -151,6 +138,29 @@
 	return best;
 }
 
+/**
+ * Update waypoint graphics id against saved GRFID/localidx.
+ * This is to ensure the chosen graphics are correct if GRF files are changed.
+ */
+void UpdateAllWaypointCustomGraphics(void)
+{
+	Waypoint *wp;
+
+	FOR_ALL_WAYPOINTS(wp) {
+		uint i;
+
+		if (wp->grfid == 0) continue;
+
+		for (i = 0; i < GetNumCustomStations(STAT_CLASS_WAYP); i++) {
+			const StationSpec *spec = GetCustomStation(STAT_CLASS_WAYP, i);
+			if (spec != NULL && spec->grfid == wp->grfid && spec->localidx == wp->localidx) {
+				wp->stat_id = i;
+				break;
+			}
+		}
+	}
+}
+
 /** Convert existing rail to waypoint. Eg build a waypoint station over
  * piece of rail
  * @param x,y coordinates where waypoint will be built
@@ -198,13 +208,26 @@
 	}
 
 	if (flags & DC_EXEC) {
+		const StationSpec *spec = NULL;
 		bool reserved = PBSTileReserved(tile) != 0;
-		ModifyTile(tile, MP_MAP5, RAIL_TYPE_WAYPOINT | dir);
-		if (p1 > 0) { // waypoint type 0 uses default graphics
-			// custom graphics
-			_m[tile].m3 |= 16;
-			_m[tile].m4 = (p1 - 1) & 0xff;
+		ModifyTile(tile, MP_MAP2 | MP_MAP5, wp->index, RAIL_TYPE_WAYPOINT | dir);
+
+		if (GB(p1, 0, 8) < GetNumCustomStations(STAT_CLASS_WAYP))
+			spec = GetCustomStation(STAT_CLASS_WAYP, GB(p1, 0, 8));
+
+		if (spec != NULL) {
+			SETBIT(_m[tile].m3, 4);
+			wp->stat_id = GB(p1, 0, 8);
+			wp->grfid = spec->grfid;
+			wp->localidx = spec->localidx;
+		} else {
+			// Specified custom graphics do not exist, so use default.
+			CLRBIT(_m[tile].m3, 4);
+			wp->stat_id = 0;
+			wp->grfid = 0;
+			wp->localidx = 0;
 		}
+
 		if (reserved) {
 			PBSReserveTrack(tile, dir);
 		} else {
@@ -280,8 +303,8 @@
 
 		if (justremove) {
 			bool reserved = PBSTileReserved(tile) != 0;
-			ModifyTile(tile, MP_MAP5, 1<<direction);
-			_m[tile].m3 &= ~16;
+			ModifyTile(tile, MP_MAP2_CLEAR | MP_MAP5, 1<<direction);
+			CLRBIT(_m[tile].m3, 4);
 			_m[tile].m4 = 0;
 			if (reserved) {
 				PBSReserveTrack(tile, direction);
@@ -446,8 +469,9 @@
 	SLE_VAR(Waypoint, string, SLE_UINT16),
 	SLE_VAR(Waypoint, deleted, SLE_UINT8),
 
-	SLE_CONDVAR(Waypoint, build_date, SLE_UINT16, 3, 255),
-	SLE_CONDVAR(Waypoint, stat_id, SLE_UINT8, 3, 255),
+	SLE_CONDVAR(Waypoint, build_date, SLE_UINT16,  3, 255),
+	SLE_CONDVAR(Waypoint, localidx,   SLE_UINT8,   3, 255),
+	SLE_CONDVAR(Waypoint, grfid,      SLE_UINT32, 17, 255),
 
 	SLE_END()
 };