summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--dedicated.c2
-rw-r--r--functions.h2
-rw-r--r--main_gui.c9
-rw-r--r--misc.c2
-rw-r--r--newgrf.c2
-rw-r--r--order_gui.c5
-rw-r--r--rail_cmd.c279
-rw-r--r--rail_gui.c1
-rw-r--r--saveload.c4
-rw-r--r--strings.c11
-rw-r--r--table/track_land.h12
-rw-r--r--train_cmd.c3
-rw-r--r--ttd.c8
-rw-r--r--tunnelbridge_cmd.c2
-rw-r--r--vehicle.c78
-rw-r--r--vehicle.h18
-rw-r--r--viewport.c105
-rw-r--r--waypoint.c459
-rw-r--r--waypoint.h60
20 files changed, 619 insertions, 444 deletions
diff --git a/Makefile b/Makefile
index c8a38190a..213b2bbd9 100644
--- a/Makefile
+++ b/Makefile
@@ -658,6 +658,7 @@ C_SOURCES += vehicle.c
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
diff --git a/dedicated.c b/dedicated.c
index 0c2de4a0b..b1c63c0e9 100644
--- a/dedicated.c
+++ b/dedicated.c
@@ -302,7 +302,7 @@ static int DedicatedVideoMainLoop(void)
#endif
if (cur_ticks >= next_tick) {
- next_tick += 30;
+// next_tick += 30;
GameLoop();
_screen.dst_ptr = _dedicated_video_mem;
diff --git a/functions.h b/functions.h
index 92c03392c..7f2577fa6 100644
--- a/functions.h
+++ b/functions.h
@@ -58,7 +58,6 @@ void StationPickerDrawSprite(int x, int y, int railtype, int image);
/* 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 ShowNetworkChatQueryWindow(byte desttype, byte dest);
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);
diff --git a/main_gui.c b/main_gui.c
index 3c411122e..22f0afd15 100644
--- a/main_gui.c
+++ b/main_gui.c
@@ -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 @@ void ShowRenameSignWindow(SignStruct *ss)
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;
}
diff --git a/misc.c b/misc.c
index 155c21db8..306921731 100644
--- a/misc.c
+++ b/misc.c
@@ -157,6 +157,7 @@ void CSleep(int milliseconds)
}
void InitializeVehicles(void);
+void InitializeWaypoints(void);
void InitializeDepot(void);
void InitializeOrders(void);
void InitializeClearLand(void);
@@ -218,6 +219,7 @@ void InitializeGame(uint log_x, uint log_y)
}
InitializeVehicles();
+ InitializeWaypoints();
InitializeDepot();
InitializeOrders();
diff --git a/newgrf.c b/newgrf.c
index 8d13de865..f8deb9433 100644
--- a/newgrf.c
+++ b/newgrf.c
@@ -31,7 +31,7 @@ GRFFile *_first_grffile;
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;
diff --git a/order_gui.c b/order_gui.c
index a681a7c3c..0a92caa84 100644
--- a/order_gui.c
+++ b/order_gui.c
@@ -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 @@ static Order GetOrderCmdFromTile(Vehicle *v, uint tile)
&& (_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 @@ static void OrdersWndProc(Window *w, WindowEvent *e)
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)
diff --git a/rail_cmd.c b/rail_cmd.c
index 8b37a00b6..878fa0296 100644
--- a/rail_cmd.c
+++ b/rail_cmd.c
@@ -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 @@ enum { /* These values are bitmasks for the map5 byte */
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 @@ static inline bool IsRailDepot(byte m5)
(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 @@ int32 CmdBuildTrainDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2)
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 @@ static int32 ClearTile_Track(TileIndex tile, byte flags)
-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 @@ static void DrawSpecialBuilding(uint32 image, uint32 tracktype_offs,
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 DrawTrainDepotSprite(int x, int y, int image, int railtype)
}
}
-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 @@ static void ClickTile_Track(uint tile)
if (IsRailDepot(_map5[tile]))
ShowTrainDepotWindow(tile);
else if (IsRailWaypoint(_map5[tile]))
- ShowRenameWaypointWindow(&_waypoints[GetWaypointByTile(tile)]);
+ ShowRenameWaypointWindow(GetWaypointByTile(tile));
}
diff --git a/rail_gui.c b/rail_gui.c
index 8ad66440d..f1b522958 100644
--- a/rail_gui.c
+++ b/rail_gui.c
@@ -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;
diff --git a/saveload.c b/saveload.c
index 947019dac..d436e97f3 100644
--- a/saveload.c
+++ b/saveload.c
@@ -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 @@ static void UninitWriteZlib(void)
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 @@ extern const ChunkHandler _animated_tile_chunk_handlers[];
static const ChunkHandler * const _chunk_handlers[] = {
_misc_chunk_handlers,
_veh_chunk_handlers,
+ _waypoint_chunk_handlers,
_depot_chunk_handlers,
_order_chunk_handlers,
_industry_chunk_handlers,
diff --git a/strings.c b/strings.c
index 75c5401e5..52404aaf3 100644
--- a/strings.c
+++ b/strings.c
@@ -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 @@ static char *DecodeString(char *buff, const char *str)
}
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 @@ static char *DecodeString(char *buff, const char *str)
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);
diff --git a/table/track_land.h b/table/track_land.h
index 41df8b21d..21f24eae0 100644
--- a/table/track_land.h
+++ b/table/track_land.h
@@ -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_depot_layout_table_3[] = {
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 @@ static const DrawTrackSeqStruct* const _track_depot_layout_table[] = {
_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,
diff --git a/train_cmd.c b/train_cmd.c
index 09b2ce019..39a247fe3 100644
--- a/train_cmd.c
+++ b/train_cmd.c
@@ -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 @@ static bool ProcessTrainOrder(Vehicle *v)
break;
case OT_GOTO_WAYPOINT:
- v->dest_tile = _waypoints[order->station].xy;
+ v->dest_tile = GetWaypoint(order->station)->xy;
result = CheckReverseTrain(v);
break;
}
diff --git a/ttd.c b/ttd.c
index 159306c18..c424a7971 100644
--- a/ttd.c
+++ b/ttd.c
@@ -34,6 +34,7 @@
#include "network.h"
#include "signs.h"
#include "depot.h"
+#include "waypoint.h"
#include <stdarg.h>
@@ -1318,6 +1319,13 @@ bool AfterLoadGame(uint version)
// 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();
diff --git a/tunnelbridge_cmd.c b/tunnelbridge_cmd.c
index 92c59b6b3..4b98be103 100644
--- a/tunnelbridge_cmd.c
+++ b/tunnelbridge_cmd.c
@@ -930,9 +930,9 @@ static const byte _bridge_foundations[2][16] = {
{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"
diff --git a/vehicle.c b/vehicle.c
index 82127512c..8636ceb00 100644
--- a/vehicle.c
+++ b/vehicle.c
@@ -159,27 +159,10 @@ void VehiclePositionChanged(Vehicle *v)
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 @@ void AfterLoadVehicles(void)
}
}
}
-
- // 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 @@ void InitializeVehicles(void)
AddBlockToPool(&_vehicle_pool);
// clear it...
- memset(&_waypoints, 0, sizeof(_waypoints));
-
memset(_vehicle_position_hash, -1, sizeof(_vehicle_position_hash));
}
@@ -396,26 +373,6 @@ int CountVehiclesInChain(Vehicle *v)
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 @@ static void Load_VEHS(void)
_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},
};
diff --git a/vehicle.h b/vehicle.h
index d7c3aafac..b7344f35f 100644
--- a/vehicle.h
+++ b/vehicle.h
@@ -198,16 +198,6 @@ struct Vehicle {
#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 DeleteVehicleChain(Vehicle *v);
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 @@ void ShowAircraftViewWindow(Vehicle *v);
UnitID GetFreeUnitNumber(byte type);
int LoadUnloadVehicle(Vehicle *v);
-uint GetWaypointByTile(uint tile);
void UpdateTrainAcceleration(Vehicle *v);
int32 GetTrainRunningCost(Vehicle *v);
@@ -423,9 +408,6 @@ static inline Vehicle *GetFirstVehicleFromSharedList(Vehicle *v)
return u;
}
-// 128 waypoints
-VARDEF Waypoint _waypoints[128];
-
// NOSAVE: Can be regenerated by inspecting the vehicles.
VARDEF VehicleID _vehicle_position_hash[0x1000];
diff --git a/viewport.c b/viewport.c
index 6bcdf4dc1..a9e699d4f 100644
--- a/viewport.c
+++ b/viewport.c
@@ -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 ViewportAddSigns(DrawPixelInfo *dpi)
static void ViewportAddWaypoints(DrawPixelInfo *dpi)
{
- Waypoint *cp;
+ Waypoint *wp;
int left, top, right, bottom;
StringSpriteToDraw *sstd;
@@ -992,34 +993,34 @@ static void ViewportAddWaypoints(DrawPixelInfo *dpi)
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) {
-
- sstd=AddStringToDraw(cp->sign.left + 1, cp->sign.top + 1, STR_WAYPOINT_VIEWPORT, cp - _waypoints, 0, 0);
+ 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(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) {
-
- sstd=AddStringToDraw(cp->sign.left + 1, cp->sign.top + 1, STR_WAYPOINT_VIEWPORT, cp - _waypoints, 0, 0);
+ 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(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 @@ static void ViewportAddWaypoints(DrawPixelInfo *dpi)
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 CheckClickOnSign(ViewPort *vp, int x, int y)
static bool CheckClickOnWaypoint(ViewPort *vp, int x, int y)
{
- Waypoint *cp;
+ Waypoint *wp;
if (!(_display_opt & DO_WAYPOINTS))
return false;
@@ -1633,39 +1634,39 @@ static bool CheckClickOnWaypoint(ViewPort *vp, int x, int y)
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;
}
}
diff --git a/waypoint.c b/waypoint.c
new file mode 100644
index 000000000..47170df65
--- /dev/null
+++ b/waypoint.c
@@ -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},
+};
diff --git a/waypoint.h b/waypoint.h
new file mode 100644
index 000000000..186b8c154
--- /dev/null
+++ b/waypoint.h
@@ -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 */