(svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
authortruelight
Thu, 24 Mar 2005 17:03:37 +0000
changeset 1542 2ca6d1624e6d
parent 1541 5c98ae4cf541
child 1543 d502c1c2ea80
(svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
-Codechange: rewrote some functions while moving waypoint-stuff
-Add: added support for 64k waypoints
-Fix: made the waypoint struct a bit more logic (no bit-fucking)
Makefile
dedicated.c
functions.h
main_gui.c
misc.c
newgrf.c
order_gui.c
rail_cmd.c
rail_gui.c
saveload.c
strings.c
table/track_land.h
train_cmd.c
ttd.c
tunnelbridge_cmd.c
vehicle.c
vehicle.h
viewport.c
waypoint.c
waypoint.h
--- a/Makefile	Thu Mar 24 15:15:06 2005 +0000
+++ b/Makefile	Thu Mar 24 17:03:37 2005 +0000
@@ -658,6 +658,7 @@
 C_SOURCES += vehicle_gui.c
 C_SOURCES += viewport.c
 C_SOURCES += water_cmd.c
+C_SOURCES += waypoint.c
 C_SOURCES += widget.c
 C_SOURCES += window.c
 
--- a/dedicated.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/dedicated.c	Thu Mar 24 17:03:37 2005 +0000
@@ -302,7 +302,7 @@
 #endif
 
 		if (cur_ticks >= next_tick) {
-			next_tick += 30;
+//			next_tick += 30;
 
 			GameLoop();
 			_screen.dst_ptr = _dedicated_video_mem;
--- a/functions.h	Thu Mar 24 15:15:06 2005 +0000
+++ b/functions.h	Thu Mar 24 17:03:37 2005 +0000
@@ -58,7 +58,6 @@
 
 /* track_land.c */
 void DrawTrainDepotSprite(int x, int y, int image, int railtype);
-void DrawWaypointSprite(int x, int y, int image, int railtype);
 
 /* road_land.c */
 void DrawRoadDepotSprite(int x, int y, int image);
@@ -209,7 +208,6 @@
 void ShowNetworkGiveMoneyWindow(byte player);
 void ShowNetworkNeedGamePassword(void);
 void ShowNetworkNeedCompanyPassword(void);
-void ShowRenameWaypointWindow(Waypoint *cp);
 int FindFirstBit(uint32 x);
 void ShowHighscoreTable(int difficulty, int8 rank);
 void ShowEndGameChart(void);
--- a/main_gui.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/main_gui.c	Thu Mar 24 17:03:37 2005 +0000
@@ -19,6 +19,7 @@
 #include "sound.h"
 #include "network.h"
 #include "signs.h"
+#include "waypoint.h"
 
 #ifdef ENABLE_NETWORK
 #include "network_data.h"
@@ -390,14 +391,14 @@
 	ShowQueryString(ss->str, STR_280B_EDIT_SIGN_TEXT, 30, 180, 1, 0);
 }
 
-void ShowRenameWaypointWindow(Waypoint *cp)
+void ShowRenameWaypointWindow(Waypoint *wp)
 {
-	int id = cp - _waypoints;
+	int id = wp->index;
 
 	/* Are we allowed to change the name of the waypoint? */
-	if (!CheckTileOwnership(cp->xy)) {
+	if (!CheckTileOwnership(wp->xy)) {
 		ShowErrorMessage(_error_message, STR_CANT_CHANGE_WAYPOINT_NAME,
-			TileX(cp->xy) * 16, TileY(cp->xy) * 16);
+			TileX(wp->xy) * 16, TileY(wp->xy) * 16);
 		return;
 	}
 
--- a/misc.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/misc.c	Thu Mar 24 17:03:37 2005 +0000
@@ -157,6 +157,7 @@
 }
 
 void InitializeVehicles(void);
+void InitializeWaypoints(void);
 void InitializeDepot(void);
 void InitializeOrders(void);
 void InitializeClearLand(void);
@@ -218,6 +219,7 @@
 	}
 
 	InitializeVehicles();
+	InitializeWaypoints();
 	InitializeDepot();
 	InitializeOrders();
 
--- a/newgrf.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/newgrf.c	Thu Mar 24 17:03:37 2005 +0000
@@ -31,7 +31,7 @@
 int _grffile_count;
 static int _cur_spriteid;
 static int _cur_stage;
-extern int _custom_sprites_base;
+extern uint16 _custom_sprites_base;
 
 static int32 _paramlist[0x7f];
 static int _param_max;
--- a/order_gui.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/order_gui.c	Thu Mar 24 17:03:37 2005 +0000
@@ -13,6 +13,7 @@
 #include "command.h"
 #include "viewport.h"
 #include "depot.h"
+#include "waypoint.h"
 
 static int OrderGetSel(Window *w)
 {
@@ -219,7 +220,7 @@
 	&& (_map5[tile]&0xFE)==0xC4) {
 		order.type = OT_GOTO_WAYPOINT;
 		order.flags = 0;
-		order.station = GetWaypointByTile(tile);
+		order.station = GetWaypointByTile(tile)->index;
 		return order;
 	}
 
@@ -379,7 +380,7 @@
 					xy = GetDepot(ord->station)->xy;
 					break;
 				case OT_GOTO_WAYPOINT:	/* goto waypoint order */
-					xy = _waypoints[ord->station].xy;
+					xy = GetWaypoint(ord->station)->xy;
 				}
 
 				if (xy)
--- a/rail_cmd.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/rail_cmd.c	Thu Mar 24 17:03:37 2005 +0000
@@ -14,6 +14,7 @@
 #include "station.h"
 #include "sprite.h"
 #include "depot.h"
+#include "waypoint.h"
 
 extern uint16 _custom_sprites_base;
 
@@ -37,9 +38,6 @@
 	RAIL_DEPOT_TRACK_MASK = 1,
 	RAIL_DEPOT_DIR = 3,
 
-	RAIL_TYPE_WAYPOINT = 0xC4,
-	RAIL_WAYPOINT_TRACK_MASK = 1,
-
 	RAIL_SUBTYPE_MASK     = 0x3C,
 	RAIL_SUBTYPE_DEPOT    = 0x00,
 	RAIL_SUBTYPE_WAYPOINT = 0x04
@@ -52,13 +50,6 @@
 		(m5 & RAIL_SUBTYPE_MASK) == RAIL_SUBTYPE_DEPOT;
 }
 
-static inline bool IsRailWaypoint(byte m5)
-{
-	return
-		(m5 & RAIL_TYPE_MASK) == RAIL_TYPE_DEPOT &&
-		(m5 & RAIL_SUBTYPE_MASK) == RAIL_SUBTYPE_WAYPOINT;
-}
-
 /* Format of rail map5 byte.
  * 00 abcdef  => Normal rail
  * 01 abcdef  => Rail with signals
@@ -689,196 +680,6 @@
 	return cost + _price.build_train_depot;
 }
 
-static void MakeDefaultWaypointName(Waypoint *cp)
-{
-	int townidx = ClosestTownFromTile(cp->xy, (uint)-1)->index;
-	Waypoint *cc;
-	bool used_waypoint[64];
-	int i;
-
-	memset(used_waypoint, 0, sizeof(used_waypoint));
-
-	// find an unused waypoint number belonging to this town
-	for(cc = _waypoints; cc != endof(_waypoints); cc++) {
-		if (cc->xy && cc->town_or_string & 0xC000 && (cc->town_or_string & 0xFF) == townidx)
-			used_waypoint[(cc->town_or_string >> 8) & 0x3F] = true;
-	}
-
-	for(i=0; used_waypoint[i] && i!=lengthof(used_waypoint)-1; i++) {}
-	cp->town_or_string = 0xC000 + (i << 8) + townidx;
-}
-
-// find a deleted waypoint close to a tile.
-static Waypoint *FindDeletedWaypointCloseTo(uint tile)
-{
-	Waypoint *cp,*best = NULL;
-	uint thres = 8, cur_dist;
-
-	for(cp = _waypoints; cp != endof(_waypoints); cp++) {
-		if (cp->deleted && cp->xy) {
-			cur_dist = DistanceManhattan(tile, cp->xy);
-			if (cur_dist < thres) {
-				thres = cur_dist;
-				best = cp;
-			}
-		}
-	}
-	return best;
-}
-
-/* Convert existing rail to waypoint */
-
-int32 CmdBuildTrainWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
-{
-	uint tile = TILE_FROM_XY(x,y);
-	Waypoint *cp;
-	uint tileh;
-	uint dir;
-
-	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
-
-	if (!IsTileType(tile, MP_RAILWAY) || ((dir = 0, _map5[tile] != 1) && (dir = 1, _map5[tile] != 2)))
-		return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK);
-
-	if (!CheckTileOwnership(tile))
-		return CMD_ERROR;
-
-	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
-
-	tileh = GetTileSlope(tile, NULL);
-	if (tileh != 0) {
-		if (!_patches.build_on_slopes || tileh & 0x10 || !(tileh & (0x3 << dir)) || !(tileh & ~(0x3 << dir)))
-			return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
-	}
-
-	// check if there is an already existing, deleted, waypoint close to us that we can reuse.
-	cp = FindDeletedWaypointCloseTo(tile);
-	if (cp == NULL) {
-		cp = AllocateWaypoint();
-		if (cp == NULL) return CMD_ERROR;
-		cp->town_or_string = 0;
-	}
-
-	if (flags & DC_EXEC) {
-		ModifyTile(tile, MP_MAP5, RAIL_TYPE_WAYPOINT | dir);
-		if (--p1 & 0x100) { // waypoint type 0 uses default graphics
-			// custom graphics
-			_map3_lo[tile] |= 16;
-			_map3_hi[tile] = p1 & 0xff;
-		}
-
-		cp->deleted = 0;
-		cp->xy = tile;
-		cp->build_date = _date;
-
-		if (cp->town_or_string == 0) MakeDefaultWaypointName(cp); else RedrawWaypointSign(cp);
-		UpdateWaypointSign(cp);
-		RedrawWaypointSign(cp);
-		SetSignalsOnBothDir(tile, dir ? 2 : 1);
-	}
-
-	return _price.build_train_depot;
-}
-
-static void DoDeleteWaypoint(Waypoint *cp)
-{
-	Order order;
-	cp->xy = 0;
-
-	order.type = OT_GOTO_WAYPOINT;
-	order.station = cp - _waypoints;
-	DeleteDestinationFromVehicleOrder(order);
-
-	if (~cp->town_or_string & 0xC000) DeleteName(cp->town_or_string);
-	RedrawWaypointSign(cp);
-}
-
-// delete waypoints after a while
-void WaypointsDailyLoop(void)
-{
-	Waypoint *cp;
-	for(cp = _waypoints; cp != endof(_waypoints); cp++) {
-		if (cp->deleted && !--cp->deleted) {
-			DoDeleteWaypoint(cp);
-		}
-	}
-}
-
-static int32 RemoveTrainWaypoint(uint tile, uint32 flags, bool justremove)
-{
-	Waypoint *cp;
-
-	// make sure it's a waypoint
-	if (!IsTileType(tile, MP_RAILWAY) || !IsRailWaypoint(_map5[tile]))
-		return CMD_ERROR;
-
-	if (!CheckTileOwnership(tile) && !(_current_player==17))
-		return CMD_ERROR;
-
-	if (!EnsureNoVehicle(tile))
-		return CMD_ERROR;
-
-	if (flags & DC_EXEC) {
-		int direction = _map5[tile] & RAIL_WAYPOINT_TRACK_MASK;
-
-		// mark the waypoint deleted
-		for(cp=_waypoints; cp->xy != (TileIndex)tile; cp++) {}
-		cp->deleted = 30; // let it live for this many days before we do the actual deletion.
-		RedrawWaypointSign(cp);
-
-		if (justremove) {
-			ModifyTile(tile, MP_MAP5, 1<<direction);
-			_map3_lo[tile] &= ~16;
-			_map3_hi[tile] = 0;
-		} else {
-			DoClearSquare(tile);
-			SetSignalsOnBothDir(tile, direction);
-		}
-	}
-
-	return _price.remove_train_depot;
-}
-
-int32 CmdRemoveTrainWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
-{
-	uint tile = TILE_FROM_XY(x,y);
-	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
-	return RemoveTrainWaypoint(tile, flags, true);
-}
-
-
-// p1 = id of waypoint
-int32 CmdRenameWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
-{
-	Waypoint *cp;
-	StringID str;
-
-	if (_decode_parameters[0] != 0) {
-		str = AllocateNameUnique((const char*)_decode_parameters, 0);
-		if (str == 0) return CMD_ERROR;
-
-		if (flags & DC_EXEC) {
-			cp = &_waypoints[p1];
-			if (~cp->town_or_string & 0xC000) DeleteName(cp->town_or_string);
-			cp->town_or_string = str;
-			UpdateWaypointSign(cp);
-			MarkWholeScreenDirty();
-		} else {
-			DeleteName(str);
-		}
-	}	else {
-		if (flags & DC_EXEC) {
-			cp = &_waypoints[p1];
-			if (~cp->town_or_string & 0xC000) DeleteName(cp->town_or_string);
-			MakeDefaultWaypointName(cp);
-			UpdateWaypointSign(cp);
-			MarkWholeScreenDirty();
-		}
-	}
-	return 0;
-}
-
-
 /* build signals, alternate between double/single, signal/semaphore,
  * pre/exit/combo-signals
  * p1 bits 0-2 - track-orientation, valid values: 0-5
@@ -1300,14 +1101,6 @@
 
 
 
-typedef struct DrawTrackSeqStruct {
-	uint16 image;
-	byte subcoord_x;
-	byte subcoord_y;
-	byte width;
-	byte height;
-} DrawTrackSeqStruct;
-
 #include "table/track_land.h"
 
 // used for presignals
@@ -1500,22 +1293,6 @@
 	AddSortableSpriteToDraw(image, ti->x + x, ti->y + y, xsize, ysize, zsize, ti->z + z);
 }
 
-/* This hacks together some dummy one-shot Station structure for a waypoint. */
-static Station *ComposeWaypointStation(uint tile)
-{
-	Waypoint *waypt = &_waypoints[GetWaypointByTile(tile)];
-	static Station stat;
-
-	stat.train_tile = stat.xy = waypt->xy;
-	/* FIXME - We should always keep town. */
-	stat.town = waypt->town_or_string & 0xC000 ? GetTown(waypt->town_or_string & 0xFF) : NULL;
-	stat.string_id = waypt->town_or_string & 0xC000 ? /* FIXME? */ 0 : waypt->town_or_string;
-	stat.build_date = waypt->build_date;
-	stat.class_id = 6; stat.stat_id = waypt->stat_id;
-
-	return &stat;
-}
-
 static void DrawTile_Track(TileInfo *ti)
 {
 	uint32 tracktype_offs, image;
@@ -1719,58 +1496,6 @@
 	}
 }
 
-void DrawWaypointSprite(int x, int y, int stat_id, int railtype)
-{
-	StationSpec *stat;
-	uint32 relocation;
-	DrawTileSprites *cust;
-	DrawTileSeqStruct const *seq;
-	uint32 ormod, img;
-
-	ormod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player));
-
-	x += 33;
-	y += 17;
-
-	// draw default waypoint graphics of ID 0
-	if (stat_id == 0) {
-		const DrawTrackSeqStruct *dtss = _track_depot_layout_table[4];
-
-		img = dtss++->image;
-		if (img & 0x8000) img = (img & 0x7FFF) + railtype*TRACKTYPE_SPRITE_PITCH;
-		DrawSprite(img, x, y);
-
-		for (; dtss->image != 0; dtss++) {
-			Point pt = RemapCoords(dtss->subcoord_x, dtss->subcoord_y, 0);
-			img = dtss->image;
-			if (img & 0x8000) img |= ormod;
-			DrawSprite(img, x + pt.x, y + pt.y);
-		}
-		return;
-	}
-
-	stat = GetCustomStation(STAT_CLASS_WAYP, stat_id - 1);
-	assert(stat);
-	relocation = GetCustomStationRelocation(stat, NULL, 1);
-	// emulate station tile - open with building
-	// add 1 to get the other direction
-	cust = &stat->renderdata[2];
-
-	img = cust->ground_sprite;
-	img += railtype*((img<_custom_sprites_base)?TRACKTYPE_SPRITE_PITCH:1);
-
-	if (img & 0x8000) img = (img & 0x7FFF);
-	DrawSprite(img, x, y);
-
-	foreach_draw_tile_seq(seq, cust->seq) {
-		Point pt = RemapCoords(seq->delta_x, seq->delta_y, seq->delta_z);
-		uint32 image = seq->image + relocation;
-
-		DrawSprite((image&0x3FFF) | ormod, x + pt.x, y + pt.y);
-	}
-}
-
-
 #define NUM_SSD_ENTRY 256
 #define NUM_SSD_STACK 32
 
@@ -2176,7 +1901,7 @@
 	if (IsRailDepot(_map5[tile]))
 		ShowTrainDepotWindow(tile);
 	else if (IsRailWaypoint(_map5[tile]))
-		ShowRenameWaypointWindow(&_waypoints[GetWaypointByTile(tile)]);
+		ShowRenameWaypointWindow(GetWaypointByTile(tile));
 
 }
 
--- a/rail_gui.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/rail_gui.c	Thu Mar 24 17:03:37 2005 +0000
@@ -12,6 +12,7 @@
 #include "command.h"
 #include "vehicle.h"
 #include "station.h"
+#include "waypoint.h"
 
 static uint _cur_railtype;
 static bool _remove_button_clicked;
--- a/saveload.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/saveload.c	Thu Mar 24 17:03:37 2005 +0000
@@ -8,7 +8,7 @@
 #include "saveload.h"
 
 enum {
-	SAVEGAME_MAJOR_VERSION = 0xB,
+	SAVEGAME_MAJOR_VERSION = 0xC,
 	SAVEGAME_MINOR_VERSION = 0x1,
 
 	SAVEGAME_LOADABLE_VERSION = (SAVEGAME_MAJOR_VERSION << 8) + SAVEGAME_MINOR_VERSION
@@ -871,6 +871,7 @@
 extern const ChunkHandler _misc_chunk_handlers[];
 extern const ChunkHandler _player_chunk_handlers[];
 extern const ChunkHandler _veh_chunk_handlers[];
+extern const ChunkHandler _waypoint_chunk_handlers[];
 extern const ChunkHandler _depot_chunk_handlers[];
 extern const ChunkHandler _order_chunk_handlers[];
 extern const ChunkHandler _town_chunk_handlers[];
@@ -884,6 +885,7 @@
 static const ChunkHandler * const _chunk_handlers[] = {
 	_misc_chunk_handlers,
 	_veh_chunk_handlers,
+	_waypoint_chunk_handlers,
 	_depot_chunk_handlers,
 	_order_chunk_handlers,
 	_industry_chunk_handlers,
--- a/strings.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/strings.c	Thu Mar 24 17:03:37 2005 +0000
@@ -9,6 +9,7 @@
 #include "vehicle.h"
 #include "news.h"
 #include "screenshot.h"
+#include "waypoint.h"
 
 static char *StationGetSpecialString(char *buff);
 static char *GetSpecialTownNameString(char *buff, int ind);
@@ -545,14 +546,14 @@
 		}
 
 		case 0x9D: { // {WAYPOINT}
-			Waypoint *cp = &_waypoints[GetDParam(0)];
+			Waypoint *wp = GetWaypoint(GetDParam(0));
 			StringID str;
 			int idx;
-			if (~cp->town_or_string & 0xC000) {
+			if (wp->string != STR_NULL) {
 				GetParamInt32(); // skip it
-				str = cp->town_or_string;
+				str = wp->string;
 			} else {
-				idx = (cp->town_or_string >> 8) & 0x3F;
+				idx = wp->town_cn;
 				if (idx == 0) {
 					str = STR_WAYPOINTNAME_CITY;
 				} else {
@@ -560,7 +561,7 @@
 					SetDParam(1, idx + 1);
 					str = STR_WAYPOINTNAME_CITY_SERIAL;
 				}
-				SetDParam(0, cp->town_or_string & 0xFF);
+				SetDParam(0, wp->town_index);
 			}
 
 			buff = GetString(buff, str);
--- a/table/track_land.h	Thu Mar 24 15:15:06 2005 +0000
+++ b/table/track_land.h	Thu Mar 24 17:03:37 2005 +0000
@@ -1,3 +1,11 @@
+typedef struct DrawTrackSeqStruct {
+	uint16 image;
+	byte subcoord_x;
+	byte subcoord_y;
+	byte width;
+	byte height;
+} DrawTrackSeqStruct;
+
 #define TILE_SEQ_BEGIN(x) { x, 0, 0, 0, 0 },
 #define TILE_SEQ_LINE(a, b, c, d, e) { a, b, c, d, e },
 #define TILE_SEQ_END() { 0, 0, 0, 0, 0 }
@@ -31,7 +39,7 @@
 static const DrawTrackSeqStruct _track_waypoint_table_0[] = {
 	TILE_SEQ_BEGIN(0x83F4)
 	TILE_SEQ_LINE(0x8000 + SPR_OPENTTD_BASE+18,  0,  0,  16,  5)
-	TILE_SEQ_LINE(0x8000 + SPR_OPENTTD_BASE+19,  0, 11,  16,  5)
+	TILE_SEQ_LINE(0x8000 + +19,  0, 11,  16,  5)
 	TILE_SEQ_END()
 };
 
@@ -53,7 +61,7 @@
 	_track_waypoint_table_1,
 };
 
-const byte _track_sloped_sprites[14] = {
+static const byte _track_sloped_sprites[14] = {
 	14, 15, 22, 13,
 	 0, 21, 17, 12,
 	23,  0, 18, 20,
--- a/train_cmd.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/train_cmd.c	Thu Mar 24 17:03:37 2005 +0000
@@ -15,6 +15,7 @@
 #include "player.h"
 #include "sound.h"
 #include "depot.h"
+#include "waypoint.h"
 
 #define is_firsthead_sprite(spritenum) \
 	(is_custom_sprite(spritenum) \
@@ -1916,7 +1917,7 @@
 			break;
 
 		case OT_GOTO_WAYPOINT:
-			v->dest_tile = _waypoints[order->station].xy;
+			v->dest_tile = GetWaypoint(order->station)->xy;
 			result = CheckReverseTrain(v);
 			break;
 	}
--- a/ttd.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/ttd.c	Thu Mar 24 17:03:37 2005 +0000
@@ -34,6 +34,7 @@
 #include "network.h"
 #include "signs.h"
 #include "depot.h"
+#include "waypoint.h"
 
 #include <stdarg.h>
 
@@ -1318,6 +1319,13 @@
 
 	// Update all vehicles
 	AfterLoadVehicles();
+
+	// Update all waypoints
+	if (version < 0x0C00)
+		FixOldWaypoints();
+
+	UpdateAllWaypointSigns();
+
 	// in version 2.2 of the savegame, we have new airports
 	if (version <= 0x201) {
 		UpdateOldAircraft();
--- a/tunnelbridge_cmd.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/tunnelbridge_cmd.c	Thu Mar 24 17:03:37 2005 +0000
@@ -930,9 +930,9 @@
 	{1,15,17,0,19,5,6,7,21,9,10,11, 0,13,14},
 };
 
-extern const byte _track_sloped_sprites[14];
 extern const byte _road_sloped_sprites[14];
 
+#include "table/track_land.h"
 #include "table/bridge_land.h"
 #include "table/tunnel_land.h"
 #include "table/water_land.h"
--- a/vehicle.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/vehicle.c	Thu Mar 24 17:03:37 2005 +0000
@@ -159,27 +159,10 @@
 	v->bottom_coord = pt.y + sd->ysize + 2;
 }
 
-void UpdateWaypointSign(Waypoint *cp)
-{
-	Point pt = RemapCoords2(TileX(cp->xy) * 16, TileY(cp->xy) * 16);
-	SetDParam(0, cp - _waypoints);
-	UpdateViewportSignPos(&cp->sign, pt.x, pt.y - 0x20, STR_WAYPOINT_VIEWPORT);
-}
-
-void RedrawWaypointSign(Waypoint *cp)
-{
-	MarkAllViewportsDirty(
-		cp->sign.left - 6,
-		cp->sign.top,
-		cp->sign.left + (cp->sign.width_1 << 2) + 12,
-		cp->sign.top + 48);
-}
-
 // Called after load to update coordinates
 void AfterLoadVehicles(void)
 {
 	Vehicle *v;
-	Waypoint *cp;
 
 	FOR_ALL_VEHICLES(v) {
 		if (v->type != 0) {
@@ -192,12 +175,8 @@
 			}
 		}
 	}
-
-	// update waypoint signs
-	for(cp=_waypoints; cp != endof(_waypoints); cp++) if (cp->xy) UpdateWaypointSign(cp);
 }
 
-
 static Vehicle *InitializeVehicle(Vehicle *v)
 {
 	VehicleID index = v->index;
@@ -353,8 +332,6 @@
 		AddBlockToPool(&_vehicle_pool);
 
 	// clear it...
-	memset(&_waypoints, 0, sizeof(_waypoints));
-
 	memset(_vehicle_position_hash, -1, sizeof(_vehicle_position_hash));
 }
 
@@ -396,26 +373,6 @@
 	return count;
 }
 
-Waypoint *AllocateWaypoint(void)
-{
-	Waypoint *cp;
-
-	for(cp = _waypoints; cp != endof(_waypoints); cp++) {
-		if (cp->xy == 0)
-			return cp;
-	}
-
-	return NULL;
-}
-
-uint GetWaypointByTile(uint tile)
-{
-	Waypoint *cp;
-	int i=0;
-	for(cp=_waypoints; cp->xy != (TileIndex)tile; cp++) { i++; }
-	return i;
-}
-
 void DeleteVehicle(Vehicle *v)
 {
 	DeleteName(v->string_id);
@@ -2097,41 +2054,8 @@
 		_vehicle_id_ctr_day = 0;
 }
 
-static const byte _waypoint_desc[] = {
-	SLE_CONDVAR(Waypoint, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
-	SLE_CONDVAR(Waypoint, xy, SLE_UINT32, 6, 255),
-	SLE_VAR(Waypoint,town_or_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_END()
-};
-
-static void Save_CHKP(void)
-{
-	Waypoint *cp;
-	int i;
-	for(i=0,cp=_waypoints; i!=lengthof(_waypoints); i++,cp++) {
-		if (cp->xy != 0) {
-			SlSetArrayIndex(i);
-			SlObject(cp, _waypoint_desc);
-		}
-	}
-}
-
-static void Load_CHKP(void)
-{
-	int index;
-	while ((index = SlIterateArray()) != -1) {
-		SlObject(&_waypoints[index], _waypoint_desc);
-	}
-}
-
 const ChunkHandler _veh_chunk_handlers[] = {
-	{ 'VEHS', Save_VEHS, Load_VEHS, CH_SPARSE_ARRAY},
-	{ 'CHKP', Save_CHKP, Load_CHKP, CH_ARRAY | CH_LAST},
+	{ 'VEHS', Save_VEHS, Load_VEHS, CH_SPARSE_ARRAY | CH_LAST},
 };
 
 
--- a/vehicle.h	Thu Mar 24 15:15:06 2005 +0000
+++ b/vehicle.h	Thu Mar 24 17:03:37 2005 +0000
@@ -198,16 +198,6 @@
 #define is_custom_firsthead_sprite(x) (x == 0xfd)
 #define is_custom_secondhead_sprite(x) (x == 0xfe)
 
-// train waypoint
-struct Waypoint {
-	TileIndex xy;
-	uint16 town_or_string; // if this is 0xC000, it's a string id, otherwise a town.
-	ViewportSign sign;
-	uint16 build_date;
-	byte stat_id;
-	byte deleted;					 // this is a delete counter. when it reaches 0, the waypoint struct is deleted.
-};
-
 enum {
 	VEH_Train = 0x10,
 	VEH_Road = 0x11,
@@ -268,10 +258,6 @@
 void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc);
 void CallVehicleTicks(void);
 
-Waypoint *AllocateWaypoint(void);
-void UpdateWaypointSign(Waypoint *cp);
-void RedrawWaypointSign(Waypoint *cp);
-
 void InitializeTrains(void);
 
 bool CanFillVehicle(Vehicle *v);
@@ -318,7 +304,6 @@
 UnitID GetFreeUnitNumber(byte type);
 
 int LoadUnloadVehicle(Vehicle *v);
-uint GetWaypointByTile(uint tile);
 
 void UpdateTrainAcceleration(Vehicle *v);
 int32 GetTrainRunningCost(Vehicle *v);
@@ -423,9 +408,6 @@
 	return u;
 }
 
-// 128 waypoints
-VARDEF Waypoint _waypoints[128];
-
 // NOSAVE: Can be regenerated by inspecting the vehicles.
 VARDEF VehicleID _vehicle_position_hash[0x1000];
 
--- a/viewport.c	Thu Mar 24 15:15:06 2005 +0000
+++ b/viewport.c	Thu Mar 24 17:03:37 2005 +0000
@@ -13,6 +13,7 @@
 #include "gfx.h"
 #include "town.h"
 #include "signs.h"
+#include "waypoint.h"
 
 #define VIEWPORT_DRAW_MEM (65536 * 2)
 
@@ -978,7 +979,7 @@
 
 static void ViewportAddWaypoints(DrawPixelInfo *dpi)
 {
-	Waypoint *cp;
+	Waypoint *wp;
 
 	int left, top, right, bottom;
 	StringSpriteToDraw *sstd;
@@ -992,34 +993,34 @@
 	bottom = top + dpi->height;
 
 	if (dpi->zoom < 1) {
-		for(cp=_waypoints; cp != endof(_waypoints); cp++) {
-			if (cp->xy &&
-					bottom > cp->sign.top &&
-					top < cp->sign.top + 12 &&
-					right > cp->sign.left &&
-					left < cp->sign.left + cp->sign.width_1) {
+		FOR_ALL_WAYPOINTS(wp) {
+			if (wp->xy &&
+					bottom > wp->sign.top &&
+					top < wp->sign.top + 12 &&
+					right > wp->sign.left &&
+					left < wp->sign.left + wp->sign.width_1) {
 
-				sstd=AddStringToDraw(cp->sign.left + 1, cp->sign.top + 1, STR_WAYPOINT_VIEWPORT, cp - _waypoints, 0, 0);
+				sstd = AddStringToDraw(wp->sign.left + 1, wp->sign.top + 1, STR_WAYPOINT_VIEWPORT, wp->index, 0, 0);
 				if (sstd != NULL) {
-					sstd->width = cp->sign.width_1;
-					sstd->color = (cp->deleted ? 0xE : 11);
+					sstd->width = wp->sign.width_1;
+					sstd->color = (wp->deleted ? 0xE : 11);
 				}
 			}
 		}
 	} else if (dpi->zoom == 1) {
 		right += 2;
 		bottom += 2;
-		for(cp=_waypoints; cp != endof(_waypoints); cp++) {
-			if (cp->xy &&
-					bottom > cp->sign.top &&
-					top < cp->sign.top + 24 &&
-					right > cp->sign.left &&
-					left < cp->sign.left + cp->sign.width_1*2) {
+		FOR_ALL_WAYPOINTS(wp) {
+			if (wp->xy &&
+					bottom > wp->sign.top &&
+					top < wp->sign.top + 24 &&
+					right > wp->sign.left &&
+					left < wp->sign.left + wp->sign.width_1*2) {
 
-				sstd=AddStringToDraw(cp->sign.left + 1, cp->sign.top + 1, STR_WAYPOINT_VIEWPORT, cp - _waypoints, 0, 0);
+				sstd = AddStringToDraw(wp->sign.left + 1, wp->sign.top + 1, STR_WAYPOINT_VIEWPORT, wp->index, 0, 0);
 				if (sstd != NULL) {
-					sstd->width = cp->sign.width_1;
-					sstd->color = (cp->deleted ? 0xE : 11);
+					sstd->width = wp->sign.width_1;
+					sstd->color = (wp->deleted ? 0xE : 11);
 				}
 			}
 		}
@@ -1027,17 +1028,17 @@
 		right += 4;
 		bottom += 5;
 
-		for(cp=_waypoints; cp != endof(_waypoints); cp++) {
-			if (cp->xy &&
-					bottom > cp->sign.top &&
-					top < cp->sign.top + 24 &&
-					right > cp->sign.left &&
-					left < cp->sign.left + cp->sign.width_2*4) {
+		FOR_ALL_WAYPOINTS(wp) {
+			if (wp->xy &&
+					bottom > wp->sign.top &&
+					top < wp->sign.top + 24 &&
+					right > wp->sign.left &&
+					left < wp->sign.left + wp->sign.width_2*4) {
 
-				sstd=AddStringToDraw(cp->sign.left + 1, cp->sign.top + 1, STR_WAYPOINT_VIEWPORT_TINY, cp - _waypoints, 0, 0);
+				sstd = AddStringToDraw(wp->sign.left + 1, wp->sign.top + 1, STR_WAYPOINT_VIEWPORT_TINY, wp->index, 0, 0);
 				if (sstd != NULL) {
-					sstd->width = cp->sign.width_2 | 0x8000;
-					sstd->color = (cp->deleted ? 0xE : 11);
+					sstd->width = wp->sign.width_2 | 0x8000;
+					sstd->color = (wp->deleted ? 0xE : 11);
 				}
 			}
 		}
@@ -1624,7 +1625,7 @@
 
 static bool CheckClickOnWaypoint(ViewPort *vp, int x, int y)
 {
-	Waypoint *cp;
+	Waypoint *wp;
 
 	if (!(_display_opt & DO_WAYPOINTS))
 		return false;
@@ -1633,39 +1634,39 @@
 		x = x - vp->left + vp->virtual_left;
 		y = y - vp->top + vp->virtual_top;
 
-		for(cp = _waypoints; cp != endof(_waypoints); cp++) {
-			if (cp->xy &&
-			    y >= cp->sign.top &&
-					y < cp->sign.top + 12 &&
-					x >= cp->sign.left &&
-					x < cp->sign.left + cp->sign.width_1) {
-				ShowRenameWaypointWindow(cp);
+		FOR_ALL_WAYPOINTS(wp) {
+			if (wp->xy &&
+			    y >= wp->sign.top &&
+					y < wp->sign.top + 12 &&
+					x >= wp->sign.left &&
+					x < wp->sign.left + wp->sign.width_1) {
+				ShowRenameWaypointWindow(wp);
 				return true;
 			}
 		}
 	} else if (vp->zoom == 1) {
 		x = (x - vp->left + 1) * 2 + vp->virtual_left;
 		y = (y - vp->top + 1) * 2 + vp->virtual_top;
-		for(cp = _waypoints; cp != endof(_waypoints); cp++) {
-			if (cp->xy &&
-			    y >= cp->sign.top &&
-					y < cp->sign.top + 24 &&
-					x >= cp->sign.left &&
-					x < cp->sign.left + cp->sign.width_1 * 2) {
-				ShowRenameWaypointWindow(cp);
+		FOR_ALL_WAYPOINTS(wp) {
+			if (wp->xy &&
+			    y >= wp->sign.top &&
+					y < wp->sign.top + 24 &&
+					x >= wp->sign.left &&
+					x < wp->sign.left + wp->sign.width_1 * 2) {
+				ShowRenameWaypointWindow(wp);
 				return true;
 			}
 		}
 	} else {
 		x = (x - vp->left + 3) * 4 + vp->virtual_left;
 		y = (y - vp->top + 3) * 4 + vp->virtual_top;
-		for(cp = _waypoints; cp != endof(_waypoints); cp++) {
-			if (cp->xy &&
-			    y >= cp->sign.top &&
-					y < cp->sign.top + 24 &&
-					x >= cp->sign.left &&
-					x < cp->sign.left + cp->sign.width_2 * 4) {
-				ShowRenameWaypointWindow(cp);
+		FOR_ALL_WAYPOINTS(wp) {
+			if (wp->xy &&
+			    y >= wp->sign.top &&
+					y < wp->sign.top + 24 &&
+					x >= wp->sign.left &&
+					x < wp->sign.left + wp->sign.width_2 * 4) {
+				ShowRenameWaypointWindow(wp);
 				return true;
 			}
 		}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/waypoint.c	Thu Mar 24 17:03:37 2005 +0000
@@ -0,0 +1,459 @@
+#include "stdafx.h"
+#include "ttd.h"
+
+#include "command.h"
+#include "gfx.h"
+#include "map.h"
+#include "order.h"
+#include "saveload.h"
+#include "station.h"
+#include "tile.h"
+#include "town.h"
+#include "waypoint.h"
+#include "table/sprites.h"
+#include "table/strings.h"
+#include "table/track_land.h"
+
+enum {
+	/* Max waypoints: 64000 (8 * 8000) */
+	WAYPOINT_POOL_BLOCK_SIZE_BITS = 3,       /* In bits, so (1 << 3) == 8 */
+	WAYPOINT_POOL_MAX_BLOCKS      = 8000,
+
+	MAX_WAYPOINTS_PER_TOWN        = 64,
+};
+
+/**
+ * Called if a new block is added to the waypoint-pool
+ */
+static void WaypointPoolNewBlock(uint start_item)
+{
+	Waypoint *wp;
+
+	FOR_ALL_WAYPOINTS_FROM(wp, start_item)
+		wp->index = start_item++;
+}
+
+/* Initialize the town-pool */
+MemoryPool _waypoint_pool = { "Waypoints", WAYPOINT_POOL_MAX_BLOCKS, WAYPOINT_POOL_BLOCK_SIZE_BITS, sizeof(Waypoint), &WaypointPoolNewBlock, 0, 0, NULL };
+
+/* Create a new waypoint */
+Waypoint *AllocateWaypoint(void)
+{
+	Waypoint *wp;
+
+	FOR_ALL_WAYPOINTS(wp) {
+		if (wp->xy == 0) {
+			uint index = wp->index;
+
+			memset(wp, 0, sizeof(Waypoint));
+			wp->index = index;
+
+			return wp;
+		}
+	}
+
+	/* Check if we can add a block to the pool */
+	if (AddBlockToPool(&_waypoint_pool))
+		return AllocateWaypoint();
+
+	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)
+{
+	Point pt = RemapCoords2(TileX(wp->xy) * 16, TileY(wp->xy) * 16);
+	SetDParam(0, wp->index);
+	UpdateViewportSignPos(&wp->sign, pt.x, pt.y - 0x20, STR_WAYPOINT_VIEWPORT);
+}
+
+/* Redraw the sign of a waypoint */
+void RedrawWaypointSign(Waypoint *wp)
+{
+	MarkAllViewportsDirty(
+		wp->sign.left - 6,
+		wp->sign.top,
+		wp->sign.left + (wp->sign.width_1 << 2) + 12,
+		wp->sign.top + 48);
+}
+
+/* Update all signs */
+void UpdateAllWaypointSigns(void)
+{
+	Waypoint *wp;
+
+	FOR_ALL_WAYPOINTS(wp) {
+		if (wp->xy)
+			UpdateWaypointSign(wp);
+	}
+}
+
+/* Set the default name for a waypoint */
+void MakeDefaultWaypointName(Waypoint *wp)
+{
+	Waypoint *local_wp;
+	bool used_waypoint[MAX_WAYPOINTS_PER_TOWN];
+	int i;
+
+	wp->town_index = ClosestTownFromTile(wp->xy, (uint)-1)->index;
+
+	memset(used_waypoint, 0, sizeof(used_waypoint));
+
+	/* Find an unused waypoint number belonging to this town */
+	FOR_ALL_WAYPOINTS(local_wp) {
+		if (wp == local_wp)
+			continue;
+
+		if (local_wp->xy && local_wp->string == STR_NULL && local_wp->town_index == wp->town_index)
+			used_waypoint[local_wp->town_cn] = true;
+	}
+
+	/* Find an empty spot */
+	for (i = 0; used_waypoint[i] && i < MAX_WAYPOINTS_PER_TOWN; i++) {}
+
+	wp->string = STR_NULL;
+	wp->town_cn = i;
+}
+
+/* Find a deleted waypoint close to a tile. */
+static Waypoint *FindDeletedWaypointCloseTo(uint tile)
+{
+	Waypoint *wp, *best = NULL;
+	uint thres = 8, cur_dist;
+
+	FOR_ALL_WAYPOINTS(wp) {
+		if (wp->deleted && wp->xy) {
+			cur_dist = DistanceManhattan(tile, wp->xy);
+			if (cur_dist < thres) {
+				thres = cur_dist;
+				best = wp;
+			}
+		}
+	}
+
+	return best;
+}
+
+/* Convert existing rail to waypoint */
+int32 CmdBuildTrainWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
+{
+	TileIndex tile = TILE_FROM_XY(x,y);
+	Waypoint *wp;
+	uint tileh;
+	uint dir;
+
+	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
+
+	if (!IsTileType(tile, MP_RAILWAY) || ((dir = 0, _map5[tile] != 1) && (dir = 1, _map5[tile] != 2)))
+		return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK);
+
+	if (!CheckTileOwnership(tile))
+		return CMD_ERROR;
+
+	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
+
+	tileh = GetTileSlope(tile, NULL);
+	if (tileh != 0) {
+		if (!_patches.build_on_slopes || tileh & 0x10 || !(tileh & (0x3 << dir)) || !(tileh & ~(0x3 << dir)))
+			return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
+	}
+
+	/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
+	wp = FindDeletedWaypointCloseTo(tile);
+	if (wp == NULL) {
+		wp = AllocateWaypoint();
+		if (wp == NULL)
+			return CMD_ERROR;
+
+		wp->town_index = 0;
+		wp->string = STR_NULL;
+		wp->town_cn = 0;
+	}
+
+	if (flags & DC_EXEC) {
+		ModifyTile(tile, MP_MAP5, RAIL_TYPE_WAYPOINT | dir);
+		if (--p1 & 0x100) { // waypoint type 0 uses default graphics
+			// custom graphics
+			_map3_lo[tile] |= 16;
+			_map3_hi[tile] = p1 & 0xff;
+		}
+
+		wp->deleted = 0;
+		wp->xy = tile;
+		wp->build_date = _date;
+
+		if (wp->town_index == STR_NULL)
+			MakeDefaultWaypointName(wp);
+
+		UpdateWaypointSign(wp);
+		RedrawWaypointSign(wp);
+	}
+
+	return _price.build_train_depot;
+}
+
+/* Internal handler to delete a waypoint */
+static void DoDeleteWaypoint(Waypoint *wp)
+{
+	Order order;
+
+	wp->xy = 0;
+
+	order.type = OT_GOTO_WAYPOINT;
+	order.station = wp->index;
+	DeleteDestinationFromVehicleOrder(order);
+
+	if (wp->string != STR_NULL)
+		DeleteName(wp->string);
+
+	RedrawWaypointSign(wp);
+}
+
+/* Daily loop for waypoints */
+void WaypointsDailyLoop(void)
+{
+	Waypoint *wp;
+
+	/* Check if we need to delete a waypoint */
+	FOR_ALL_WAYPOINTS(wp) {
+		if (wp->deleted && !--wp->deleted) {
+			DoDeleteWaypoint(wp);
+		}
+	}
+}
+
+/* Remove a waypoint */
+int32 RemoveTrainWaypoint(uint tile, uint32 flags, bool justremove)
+{
+	Waypoint *wp;
+
+	/* Make sure it's a waypoint */
+	if (!IsTileType(tile, MP_RAILWAY) || !IsRailWaypoint(_map5[tile]))
+		return CMD_ERROR;
+
+	if (!CheckTileOwnership(tile) && !(_current_player == OWNER_WATER))
+		return CMD_ERROR;
+
+	if (!EnsureNoVehicle(tile))
+		return CMD_ERROR;
+
+	if (flags & DC_EXEC) {
+		int direction = _map5[tile] & RAIL_WAYPOINT_TRACK_MASK;
+
+		wp = GetWaypointByTile(tile);
+
+		wp->deleted = 30; // let it live for this many days before we do the actual deletion.
+		RedrawWaypointSign(wp);
+
+		if (justremove) {
+			ModifyTile(tile, MP_MAP5, 1<<direction);
+			_map3_lo[tile] &= ~16;
+			_map3_hi[tile] = 0;
+		} else {
+			DoClearSquare(tile);
+			SetSignalsOnBothDir(tile, direction);
+		}
+	}
+
+	return _price.remove_train_depot;
+}
+
+/* Command call to remove a waypoint */
+int32 CmdRemoveTrainWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
+{
+	uint tile = TILE_FROM_XY(x,y);
+	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
+	return RemoveTrainWaypoint(tile, flags, true);
+}
+
+/* Rename a waypoint
+ * p1 = id of waypoint */
+int32 CmdRenameWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
+{
+	Waypoint *wp;
+	StringID str;
+
+	if (_decode_parameters[0] != 0) {
+		str = AllocateNameUnique((const char*)_decode_parameters, 0);
+		if (str == 0)
+			return CMD_ERROR;
+
+		if (flags & DC_EXEC) {
+			wp = GetWaypoint(p1);
+			if (wp->string != STR_NULL)
+				DeleteName(wp->string);
+
+			wp->string = str;
+			wp->town_cn = 0;
+
+			UpdateWaypointSign(wp);
+			MarkWholeScreenDirty();
+		} else {
+			DeleteName(str);
+		}
+	}	else {
+		if (flags & DC_EXEC) {
+			wp = GetWaypoint(p1);
+			if (wp->string != STR_NULL)
+				DeleteName(wp->string);
+
+			MakeDefaultWaypointName(wp);
+			UpdateWaypointSign(wp);
+			MarkWholeScreenDirty();
+		}
+	}
+	return 0;
+}
+
+/* This hacks together some dummy one-shot Station structure for a waypoint. */
+Station *ComposeWaypointStation(uint tile)
+{
+	Waypoint *wp = GetWaypointByTile(tile);
+	static Station stat;
+
+	stat.train_tile = stat.xy = wp->xy;
+	stat.town = GetTown(wp->town_index);
+	stat.string_id = wp->string == STR_NULL ? /* FIXME? */ 0 : wp->string;
+	stat.build_date = wp->build_date;
+	stat.class_id = 6;
+	stat.stat_id = wp->stat_id;
+
+	return &stat;
+}
+
+extern uint16 _custom_sprites_base;
+
+/* Draw a waypoint */
+void DrawWaypointSprite(int x, int y, int stat_id, int railtype)
+{
+	StationSpec *stat;
+	uint32 relocation;
+	DrawTileSprites *cust;
+	DrawTileSeqStruct const *seq;
+	uint32 ormod, img;
+
+	ormod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player));
+
+	x += 33;
+	y += 17;
+
+	/* draw default waypoint graphics of ID 0 */
+	if (stat_id == 0) {
+		const DrawTrackSeqStruct *dtss = _track_depot_layout_table[4];
+
+		img = dtss++->image;
+		if (img & 0x8000) img = (img & 0x7FFF) + railtype*TRACKTYPE_SPRITE_PITCH;
+		DrawSprite(img, x, y);
+
+		for (; dtss->image != 0; dtss++) {
+			Point pt = RemapCoords(dtss->subcoord_x, dtss->subcoord_y, 0);
+			img = dtss->image;
+			if (img & 0x8000) img |= ormod;
+			DrawSprite(img, x + pt.x, y + pt.y);
+		}
+		return;
+	}
+
+	stat = GetCustomStation(STAT_CLASS_WAYP, stat_id - 1);
+	assert(stat);
+	relocation = GetCustomStationRelocation(stat, NULL, 1);
+	// emulate station tile - open with building
+	// add 1 to get the other direction
+	cust = &stat->renderdata[2];
+
+	img = cust->ground_sprite;
+	img += railtype * ((img < _custom_sprites_base) ? TRACKTYPE_SPRITE_PITCH : 1);
+
+	if (img & 0x8000) img = (img & 0x7FFF);
+	DrawSprite(img, x, y);
+
+	foreach_draw_tile_seq(seq, cust->seq) {
+		Point pt = RemapCoords(seq->delta_x, seq->delta_y, seq->delta_z);
+		uint32 image = seq->image + relocation;
+
+		DrawSprite((image&0x3FFF) | ormod, x + pt.x, y + pt.y);
+	}
+}
+
+/* Fix savegames which stored waypoints in their old format */
+void FixOldWaypoints(void)
+{
+	Waypoint *wp;
+
+	/* Convert the old 'town_or_string', to 'string' / 'town' / 'town_cn' */
+	FOR_ALL_WAYPOINTS(wp) {
+		if (wp->xy == 0)
+			continue;
+
+		wp->town_index = ClosestTownFromTile(wp->xy, (uint)-1)->index;
+		wp->town_cn = 0;
+		if (wp->string & 0xC000) {
+			wp->town_cn = wp->string & 0x3F;
+			wp->string = STR_NULL;
+		}
+	}
+}
+
+void InitializeWaypoints(void)
+{
+	CleanPool(&_waypoint_pool);
+	AddBlockToPool(&_waypoint_pool);
+}
+
+static const byte _waypoint_desc[] = {
+	SLE_CONDVAR(Waypoint, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
+	SLE_CONDVAR(Waypoint, xy, SLE_UINT32, 6, 255),
+	SLE_CONDVAR(Waypoint, town_index, SLE_UINT16, 12, 255),
+	SLE_CONDVAR(Waypoint, town_cn, SLE_UINT8, 12, 255),
+	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_END()
+};
+
+static void Save_WAYP(void)
+{
+	Waypoint *wp;
+
+	FOR_ALL_WAYPOINTS(wp) {
+		if (wp->xy != 0) {
+			SlSetArrayIndex(wp->index);
+			SlObject(wp, _waypoint_desc);
+		}
+	}
+}
+
+static void Load_WAYP(void)
+{
+	int index;
+
+	while ((index = SlIterateArray()) != -1) {
+		Waypoint *wp;
+
+		if (!AddBlockIfNeeded(&_waypoint_pool, index))
+			error("Waypoints: failed loading savegame: too many waypoints");
+
+		wp = GetWaypoint(index);
+		SlObject(wp, _waypoint_desc);
+	}
+}
+
+const ChunkHandler _waypoint_chunk_handlers[] = {
+	{ 'CHKP', Save_WAYP, Load_WAYP, CH_ARRAY | CH_LAST},
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/waypoint.h	Thu Mar 24 17:03:37 2005 +0000
@@ -0,0 +1,60 @@
+#ifndef WAYPOINT_H
+#define WAYPOINT_H
+
+#include "pool.h"
+
+struct Waypoint {
+	TileIndex xy;
+	uint16 index;
+
+	uint16 town_index;
+	byte town_cn;          // The Nth waypoint for this town (consecutive number)
+	StringID string;       // If this is zero, town + town_cn is used for naming
+
+	ViewportSign sign;
+	uint16 build_date;
+	byte stat_id;
+	byte deleted;          // this is a delete counter. when it reaches 0, the waypoint struct is deleted.
+};
+
+enum {
+	RAIL_TYPE_WAYPOINT = 0xC4,
+	RAIL_WAYPOINT_TRACK_MASK = 1,
+};
+
+extern MemoryPool _waypoint_pool;
+
+/**
+ * Get the pointer to the waypoint with index 'index'
+ */
+static inline Waypoint *GetWaypoint(uint index)
+{
+	return (Waypoint*)GetItemFromPool(&_waypoint_pool, index);
+}
+
+/**
+ * Get the current size of the WaypointPool
+ */
+static inline uint16 GetWaypointPoolSize(void)
+{
+	return _waypoint_pool.total_items;
+}
+
+#define FOR_ALL_WAYPOINTS_FROM(wp, start) for (wp = GetWaypoint(start); wp != NULL; wp = (wp->index + 1 < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1) : NULL)
+#define FOR_ALL_WAYPOINTS(wp) FOR_ALL_WAYPOINTS_FROM(wp, 0)
+
+static inline bool IsRailWaypoint(byte m5)
+{
+	return (m5 & 0xFC) == 0xC4;
+}
+
+int32 RemoveTrainWaypoint(uint tile, uint32 flags, bool justremove);
+Station *ComposeWaypointStation(uint tile);
+Waypoint *GetWaypointByTile(TileIndex tile);
+void ShowRenameWaypointWindow(Waypoint *cp);
+void DrawWaypointSprite(int x, int y, int image, int railtype);
+void UpdateWaypointSign(Waypoint *cp);
+void FixOldWaypoints(void);
+void UpdateAllWaypointSigns(void);
+
+#endif /* WAYPOINT_H */