waypoint.c
author glx
Mon, 24 Sep 2007 03:08:47 +0000
branch0.5
changeset 5545 f42dc59a45f5
parent 5543 4d8dfa498905
permissions -rw-r--r--
(svn r11153) [0.5] -Fix [FS#1251]: incorrect usage of {G} tag in slovak translation
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1881
diff changeset
     4
#include "openttd.h"
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
     5
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
     6
#include "command.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     7
#include "functions.h"
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
     8
#include "gfx.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
     9
#include "map.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    10
#include "order.h"
3101
e2fdb8802c2f (svn r3696) Add functions to turn a tile into a normal rail tile/depot/waypoint. This is just a tiny step, the rail code needs way more love and caring
tron
parents: 3047
diff changeset
    11
#include "rail_map.h"
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    12
#include "saveload.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    13
#include "station.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    14
#include "tile.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    15
#include "town.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    16
#include "waypoint.h"
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2085
diff changeset
    17
#include "variables.h"
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    18
#include "table/strings.h"
3578
9dec4b636167 (svn r4464) -Codechange: Oops, forgot one file in last commit
celestar
parents: 3574
diff changeset
    19
#include "vehicle.h"
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3792
diff changeset
    20
#include "yapf/yapf.h"
4261
2ec8f5a9747b (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4077
diff changeset
    21
#include "date.h"
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    22
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    23
enum {
4984
b16443d3ab20 (svn r6987) Use the pool macros for the Waypoint pool
tron
parents: 4527
diff changeset
    24
	MAX_WAYPOINTS_PER_TOWN = 64,
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    25
};
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    26
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    27
/**
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    28
 * Called if a new block is added to the waypoint-pool
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    29
 */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    30
static void WaypointPoolNewBlock(uint start_item)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    31
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    32
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    33
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
    34
	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
    35
	 * TODO - This is just a temporary stage, this will be removed. */
4984
b16443d3ab20 (svn r6987) Use the pool macros for the Waypoint pool
tron
parents: 4527
diff changeset
    36
	for (wp = GetWaypoint(start_item); wp != NULL; wp = (wp->index + 1U < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1U) : NULL) wp->index = start_item++;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    37
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    38
5216
d581e4db95b6 (svn r7331) - Codechange: Rename all memory pool macro's and types to "old pool", so the new pool implementation can be committed alongside it.
matthijs
parents: 4984
diff changeset
    39
DEFINE_OLD_POOL(Waypoint, Waypoint, WaypointPoolNewBlock, NULL)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    40
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    41
/* Create a new waypoint */
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2670
diff changeset
    42
static Waypoint* AllocateWaypoint(void)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    43
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    44
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    45
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
    46
	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
    47
	 * TODO - This is just a temporary stage, this will be removed. */
4984
b16443d3ab20 (svn r6987) Use the pool macros for the Waypoint pool
tron
parents: 4527
diff changeset
    48
	for (wp = GetWaypoint(0); wp != NULL; wp = (wp->index + 1U < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1U) : NULL) {
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
    49
		if (!IsValidWaypoint(wp)) {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    50
			uint index = wp->index;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    51
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
    52
			memset(wp, 0, sizeof(*wp));
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    53
			wp->index = index;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    54
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    55
			return wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    56
		}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    57
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    58
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    59
	/* Check if we can add a block to the pool */
4984
b16443d3ab20 (svn r6987) Use the pool macros for the Waypoint pool
tron
parents: 4527
diff changeset
    60
	if (AddBlockToPool(&_Waypoint_pool)) return AllocateWaypoint();
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    61
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    62
	return NULL;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    63
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    64
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    65
/* Update the sign for the waypoint */
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2752
diff changeset
    66
static void UpdateWaypointSign(Waypoint* wp)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    67
{
3422
12cdb13ddb56 (svn r4249) -Codechange: Replace more occurences of 16 by TILE_SIZE and of 8 by TILE_HEIGHT. Reverted one change from the previous commit because it was faulty
celestar
parents: 3349
diff changeset
    68
	Point pt = RemapCoords2(TileX(wp->xy) * TILE_SIZE, TileY(wp->xy) * TILE_SIZE);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    69
	SetDParam(0, wp->index);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    70
	UpdateViewportSignPos(&wp->sign, pt.x, pt.y - 0x20, STR_WAYPOINT_VIEWPORT);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    71
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    72
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    73
/* Redraw the sign of a waypoint */
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2670
diff changeset
    74
static void RedrawWaypointSign(const Waypoint* wp)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    75
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    76
	MarkAllViewportsDirty(
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    77
		wp->sign.left - 6,
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    78
		wp->sign.top,
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    79
		wp->sign.left + (wp->sign.width_1 << 2) + 12,
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    80
		wp->sign.top + 48);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    81
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    82
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    83
/* Update all signs */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    84
void UpdateAllWaypointSigns(void)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    85
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    86
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    87
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    88
	FOR_ALL_WAYPOINTS(wp) {
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
    89
		UpdateWaypointSign(wp);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    90
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    91
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    92
4390
6e00adba300d (svn r6143) -Codechange: DeleteWaypoint removes a waypoint from the pool
truelight
parents: 4389
diff changeset
    93
/* Internal handler to delete a waypoint */
6e00adba300d (svn r6143) -Codechange: DeleteWaypoint removes a waypoint from the pool
truelight
parents: 4389
diff changeset
    94
void DestroyWaypoint(Waypoint *wp)
6e00adba300d (svn r6143) -Codechange: DeleteWaypoint removes a waypoint from the pool
truelight
parents: 4389
diff changeset
    95
{
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4500
diff changeset
    96
	RemoveOrderFromAllVehicles(OT_GOTO_WAYPOINT, wp->index);
4390
6e00adba300d (svn r6143) -Codechange: DeleteWaypoint removes a waypoint from the pool
truelight
parents: 4389
diff changeset
    97
6e00adba300d (svn r6143) -Codechange: DeleteWaypoint removes a waypoint from the pool
truelight
parents: 4389
diff changeset
    98
	if (wp->string != STR_NULL) DeleteName(wp->string);
6e00adba300d (svn r6143) -Codechange: DeleteWaypoint removes a waypoint from the pool
truelight
parents: 4389
diff changeset
    99
6e00adba300d (svn r6143) -Codechange: DeleteWaypoint removes a waypoint from the pool
truelight
parents: 4389
diff changeset
   100
	RedrawWaypointSign(wp);
6e00adba300d (svn r6143) -Codechange: DeleteWaypoint removes a waypoint from the pool
truelight
parents: 4389
diff changeset
   101
}
6e00adba300d (svn r6143) -Codechange: DeleteWaypoint removes a waypoint from the pool
truelight
parents: 4389
diff changeset
   102
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   103
/* Set the default name for a waypoint */
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2670
diff changeset
   104
static void MakeDefaultWaypointName(Waypoint* wp)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   105
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   106
	Waypoint *local_wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   107
	bool used_waypoint[MAX_WAYPOINTS_PER_TOWN];
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   108
	int i;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   109
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   110
	wp->town_index = ClosestTownFromTile(wp->xy, (uint)-1)->index;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   111
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   112
	memset(used_waypoint, 0, sizeof(used_waypoint));
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   113
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   114
	/* Find an unused waypoint number belonging to this town */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   115
	FOR_ALL_WAYPOINTS(local_wp) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   116
		if (wp == local_wp) continue;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   117
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   118
		if (local_wp->xy && local_wp->string == STR_NULL && local_wp->town_index == wp->town_index)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   119
			used_waypoint[local_wp->town_cn] = true;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   120
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   121
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   122
	/* Find an empty spot */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   123
	for (i = 0; used_waypoint[i] && i < MAX_WAYPOINTS_PER_TOWN; i++) {}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   124
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   125
	wp->string = STR_NULL;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   126
	wp->town_cn = i;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   127
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   128
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   129
/* Find a deleted waypoint close to a tile. */
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
   130
static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   131
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   132
	Waypoint *wp, *best = NULL;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   133
	uint thres = 8;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   134
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   135
	FOR_ALL_WAYPOINTS(wp) {
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   136
		if (wp->deleted) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   137
			uint cur_dist = DistanceManhattan(tile, wp->xy);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   138
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   139
			if (cur_dist < thres) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   140
				thres = cur_dist;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   141
				best = wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   142
			}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   143
		}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   144
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   145
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   146
	return best;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   147
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   148
2670
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   149
/**
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   150
 * Update waypoint graphics id against saved GRFID/localidx.
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   151
 * This is to ensure the chosen graphics are correct if GRF files are changed.
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   152
 */
5350
db5f6bf223d7 (svn r7521) -Codechange: Rename UpdateAllWaypointCustomGraphics to AfterLoadWaypoints to be more conforming with other such functions.
Darkvater
parents: 5348
diff changeset
   153
void AfterLoadWaypoints(void)
2670
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   154
{
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   155
	Waypoint *wp;
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   156
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   157
	FOR_ALL_WAYPOINTS(wp) {
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   158
		uint i;
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   159
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   160
		if (wp->grfid == 0) continue;
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   161
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   162
		for (i = 0; i < GetNumCustomStations(STAT_CLASS_WAYP); i++) {
3676
48444d881fbe (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   163
			const StationSpec *statspec = GetCustomStationSpec(STAT_CLASS_WAYP, i);
48444d881fbe (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   164
			if (statspec != NULL && statspec->grfid == wp->grfid && statspec->localidx == wp->localidx) {
2670
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   165
				wp->stat_id = i;
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   166
				break;
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   167
			}
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   168
		}
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   169
	}
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   170
}
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   171
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   172
/** Convert existing rail to waypoint. Eg build a waypoint station over
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   173
 * piece of rail
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3448
diff changeset
   174
 * @param tile tile where waypoint will be built
2625
66b3d632dcd2 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents: 2624
diff changeset
   175
 * @param p1 graphics for waypoint type, 0 indicates standard graphics
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   176
 * @param p2 unused
2085
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   177
 *
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   178
 * @todo When checking for the tile slope,
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   179
 * distingush between "Flat land required" and "land sloped in wrong direction"
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   180
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3448
diff changeset
   181
int32 CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   182
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   183
	Waypoint *wp;
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3585
diff changeset
   184
	Slope tileh;
3101
e2fdb8802c2f (svn r3696) Add functions to turn a tile into a normal rail tile/depot/waypoint. This is just a tiny step, the rail code needs way more love and caring
tron
parents: 3047
diff changeset
   185
	Axis axis;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   186
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   187
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   188
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   189
	/* if custom gfx are used, make sure it is within bounds */
2625
66b3d632dcd2 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents: 2624
diff changeset
   190
	if (p1 >= GetNumCustomStations(STAT_CLASS_WAYP)) return CMD_ERROR;
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   191
3267
591027d10884 (svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents: 3242
diff changeset
   192
	if (!IsTileType(tile, MP_RAILWAY) ||
3792
2eb8388731bf (svn r4788) - Codechange: RAILTYPE_{NORMAL,ELECTRIC,...} and RAIL_TYPE_{NORMAL,SIGNAL,...} have nearly the same name, rename RAIL_TYPE_* to RAIL_TILE_* of extra clarity
rubidium
parents: 3764
diff changeset
   193
			GetRailTileType(tile) != RAIL_TILE_NORMAL || (
3267
591027d10884 (svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents: 3242
diff changeset
   194
				(axis = AXIS_X, GetTrackBits(tile) != TRACK_BIT_X) &&
591027d10884 (svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents: 3242
diff changeset
   195
				(axis = AXIS_Y, GetTrackBits(tile) != TRACK_BIT_Y)
3101
e2fdb8802c2f (svn r3696) Add functions to turn a tile into a normal rail tile/depot/waypoint. This is just a tiny step, the rail code needs way more love and caring
tron
parents: 3047
diff changeset
   196
			)) {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   197
		return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK);
3101
e2fdb8802c2f (svn r3696) Add functions to turn a tile into a normal rail tile/depot/waypoint. This is just a tiny step, the rail code needs way more love and caring
tron
parents: 3047
diff changeset
   198
	}
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   199
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   200
	if (!CheckTileOwnership(tile)) return CMD_ERROR;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   201
	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   202
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   203
	tileh = GetTileSlope(tile, NULL);
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   204
	if (tileh != SLOPE_FLAT &&
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   205
			(!_patches.build_on_slopes || IsSteepSlope(tileh) || !(tileh & (0x3 << axis)) || !(tileh & ~(0x3 << axis)))) {
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   206
		return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   207
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   208
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   209
	/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   210
	wp = FindDeletedWaypointCloseTo(tile);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   211
	if (wp == NULL) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   212
		wp = AllocateWaypoint();
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   213
		if (wp == NULL) return CMD_ERROR;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   214
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   215
		wp->town_index = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   216
		wp->string = STR_NULL;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   217
		wp->town_cn = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   218
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   219
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   220
	if (flags & DC_EXEC) {
4051
566d63f3c5a2 (svn r5325) Don't test something if we already know the result true, because the same test was performed just a few lines before
tron
parents: 4000
diff changeset
   221
		const StationSpec* statspec;
566d63f3c5a2 (svn r5325) Don't test something if we already know the result true, because the same test was performed just a few lines before
tron
parents: 4000
diff changeset
   222
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3157
diff changeset
   223
		MakeRailWaypoint(tile, GetTileOwner(tile), axis, GetRailType(tile), wp->index);
3101
e2fdb8802c2f (svn r3696) Add functions to turn a tile into a normal rail tile/depot/waypoint. This is just a tiny step, the rail code needs way more love and caring
tron
parents: 3047
diff changeset
   224
		MarkTileDirtyByTile(tile);
2670
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   225
4051
566d63f3c5a2 (svn r5325) Don't test something if we already know the result true, because the same test was performed just a few lines before
tron
parents: 4000
diff changeset
   226
		statspec = GetCustomStationSpec(STAT_CLASS_WAYP, p1);
2670
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   227
3676
48444d881fbe (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   228
		if (statspec != NULL) {
4051
566d63f3c5a2 (svn r5325) Don't test something if we already know the result true, because the same test was performed just a few lines before
tron
parents: 4000
diff changeset
   229
			wp->stat_id = p1;
3676
48444d881fbe (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   230
			wp->grfid = statspec->grfid;
48444d881fbe (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   231
			wp->localidx = statspec->localidx;
2670
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   232
		} else {
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   233
			// Specified custom graphics do not exist, so use default.
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   234
			wp->stat_id = 0;
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   235
			wp->grfid = 0;
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   236
			wp->localidx = 0;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   237
		}
2670
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   238
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   239
		wp->deleted = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   240
		wp->xy = tile;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   241
		wp->build_date = _date;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   242
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3977
diff changeset
   243
		if (wp->town_index == 0) MakeDefaultWaypointName(wp);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   244
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   245
		UpdateWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   246
		RedrawWaypointSign(wp);
5348
0f05b1348b5a (svn r7519) -Fix: [YAPF] Cache was not deleted when waypoint was built or removed (frosch)
KUDr
parents: 5216
diff changeset
   247
		YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   248
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   249
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   250
	return _price.build_train_depot;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   251
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   252
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   253
/* Daily loop for waypoints */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   254
void WaypointsDailyLoop(void)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   255
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   256
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   257
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   258
	/* Check if we need to delete a waypoint */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   259
	FOR_ALL_WAYPOINTS(wp) {
4500
426d0baa1be5 (svn r6285) -Fix r6143: of course deleting a waypoint should call the Delete wrapper, not the Destroy function (/me slaps hisself)
truelight
parents: 4395
diff changeset
   260
		if (wp->deleted != 0 && --wp->deleted == 0) DeleteWaypoint(wp);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   261
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   262
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   263
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   264
/* Remove a waypoint */
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
   265
int32 RemoveTrainWaypoint(TileIndex tile, uint32 flags, bool justremove)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   266
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   267
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   268
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   269
	/* Make sure it's a waypoint */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   270
	if (!IsTileType(tile, MP_RAILWAY) ||
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   271
			!IsRailWaypoint(tile) ||
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   272
			(!CheckTileOwnership(tile) && _current_player != OWNER_WATER) ||
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   273
			!EnsureNoVehicle(tile)) {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   274
		return CMD_ERROR;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   275
	}
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   276
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   277
	if (flags & DC_EXEC) {
5348
0f05b1348b5a (svn r7519) -Fix: [YAPF] Cache was not deleted when waypoint was built or removed (frosch)
KUDr
parents: 5216
diff changeset
   278
		Track track = GetRailWaypointTrack(tile);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   279
		wp = GetWaypointByTile(tile);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   280
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   281
		wp->deleted = 30; // let it live for this many days before we do the actual deletion.
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   282
		RedrawWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   283
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   284
		if (justremove) {
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3157
diff changeset
   285
			MakeRailNormal(tile, GetTileOwner(tile), GetRailWaypointBits(tile), GetRailType(tile));
3101
e2fdb8802c2f (svn r3696) Add functions to turn a tile into a normal rail tile/depot/waypoint. This is just a tiny step, the rail code needs way more love and caring
tron
parents: 3047
diff changeset
   286
			MarkTileDirtyByTile(tile);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   287
		} else {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   288
			DoClearSquare(tile);
5348
0f05b1348b5a (svn r7519) -Fix: [YAPF] Cache was not deleted when waypoint was built or removed (frosch)
KUDr
parents: 5216
diff changeset
   289
			SetSignalsOnBothDir(tile, track);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   290
		}
5348
0f05b1348b5a (svn r7519) -Fix: [YAPF] Cache was not deleted when waypoint was built or removed (frosch)
KUDr
parents: 5216
diff changeset
   291
		YapfNotifyTrackLayoutChange(tile, track);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   292
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   293
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   294
	return _price.remove_train_depot;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   295
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   296
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   297
/** Delete a waypoint
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3448
diff changeset
   298
 * @param tile tile where waypoint is to be deleted
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   299
 * @param p1 unused
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   300
 * @param p2 unused
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   301
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3448
diff changeset
   302
int32 CmdRemoveTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   303
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   304
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   305
	return RemoveTrainWaypoint(tile, flags, true);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   306
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   307
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   308
/** Rename a waypoint.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3448
diff changeset
   309
 * @param tile unused
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   310
 * @param p1 id of waypoint
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   311
 * @param p2 unused
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   312
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3448
diff changeset
   313
int32 CmdRenameWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   314
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   315
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   316
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   317
	if (!IsValidWaypointID(p1)) return CMD_ERROR;
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   318
5520
82988aae1562 (svn r10374) [0.5] -Backport from trunk (r10333, r10336, r10337, r10345, 10346, 10368):
rubidium
parents: 5350
diff changeset
   319
	wp = GetWaypoint(p1);
82988aae1562 (svn r10374) [0.5] -Backport from trunk (r10333, r10336, r10337, r10345, 10346, 10368):
rubidium
parents: 5350
diff changeset
   320
	if (!CheckTileOwnership(wp->xy)) return CMD_ERROR;
82988aae1562 (svn r10374) [0.5] -Backport from trunk (r10333, r10336, r10337, r10345, 10346, 10368):
rubidium
parents: 5350
diff changeset
   321
5543
4d8dfa498905 (svn r11077) [0.5] -Fix: Possible NULL pointer dereference.
rubidium
parents: 5520
diff changeset
   322
	if (_cmd_text != NULL && _cmd_text[0] != '\0') {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   323
		StringID str = AllocateNameUnique(_cmd_text, 0);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   324
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   325
		if (str == 0) return CMD_ERROR;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   326
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   327
		if (flags & DC_EXEC) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   328
			if (wp->string != STR_NULL) DeleteName(wp->string);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   329
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   330
			wp->string = str;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   331
			wp->town_cn = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   332
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   333
			UpdateWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   334
			MarkWholeScreenDirty();
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   335
		} else {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   336
			DeleteName(str);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   337
		}
2026
02dfa0aa2c2f (svn r2535) Tabs
tron
parents: 1980
diff changeset
   338
	} else {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   339
		if (flags & DC_EXEC) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   340
			if (wp->string != STR_NULL) DeleteName(wp->string);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   341
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   342
			MakeDefaultWaypointName(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   343
			UpdateWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   344
			MarkWholeScreenDirty();
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   345
		}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   346
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   347
	return 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   348
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   349
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   350
/* This hacks together some dummy one-shot Station structure for a waypoint. */
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
   351
Station *ComposeWaypointStation(TileIndex tile)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   352
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   353
	Waypoint *wp = GetWaypointByTile(tile);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   354
	static Station stat;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   355
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   356
	stat.train_tile = stat.xy = wp->xy;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   357
	stat.town = GetTown(wp->town_index);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   358
	stat.string_id = wp->string == STR_NULL ? /* FIXME? */ 0 : wp->string;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   359
	stat.build_date = wp->build_date;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   360
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   361
	return &stat;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   362
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   363
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   364
/* Draw a waypoint */
2520
75eb66d6e371 (svn r3049) Replace byte/int/uint by RailType where appropriate
tron
parents: 2261
diff changeset
   365
void DrawWaypointSprite(int x, int y, int stat_id, RailType railtype)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   366
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   367
	x += 33;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   368
	y += 17;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   369
3764
ebcaf412fb3a (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3751
diff changeset
   370
	if (!DrawStationTile(x, y, railtype, AXIS_X, STAT_CLASS_WAYP, stat_id)) {
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2254
diff changeset
   371
		DrawDefaultWaypointSprite(x, y, railtype);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   372
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   373
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   374
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   375
/* Fix savegames which stored waypoints in their old format */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   376
void FixOldWaypoints(void)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   377
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   378
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   379
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   380
	/* Convert the old 'town_or_string', to 'string' / 'town' / 'town_cn' */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   381
	FOR_ALL_WAYPOINTS(wp) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   382
		wp->town_index = ClosestTownFromTile(wp->xy, (uint)-1)->index;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   383
		wp->town_cn = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   384
		if (wp->string & 0xC000) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   385
			wp->town_cn = wp->string & 0x3F;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   386
			wp->string = STR_NULL;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   387
		}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   388
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   389
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   390
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   391
void InitializeWaypoints(void)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   392
{
4984
b16443d3ab20 (svn r6987) Use the pool macros for the Waypoint pool
tron
parents: 4527
diff changeset
   393
	CleanPool(&_Waypoint_pool);
b16443d3ab20 (svn r6987) Use the pool macros for the Waypoint pool
tron
parents: 4527
diff changeset
   394
	AddBlockToPool(&_Waypoint_pool);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   395
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   396
1881
023a134a4b12 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1820
diff changeset
   397
static const SaveLoad _waypoint_desc[] = {
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   398
	SLE_CONDVAR(Waypoint, xy,         SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   399
	SLE_CONDVAR(Waypoint, xy,         SLE_UINT32,                  6, SL_MAX_VERSION),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   400
	SLE_CONDVAR(Waypoint, town_index, SLE_UINT16,                 12, SL_MAX_VERSION),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   401
	SLE_CONDVAR(Waypoint, town_cn,    SLE_UINT8,                  12, SL_MAX_VERSION),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   402
	    SLE_VAR(Waypoint, string,     SLE_UINT16),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   403
	    SLE_VAR(Waypoint, deleted,    SLE_UINT8),
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   404
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   405
	SLE_CONDVAR(Waypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32,  3, 30),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   406
	SLE_CONDVAR(Waypoint, build_date, SLE_INT32,                  31, SL_MAX_VERSION),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   407
	SLE_CONDVAR(Waypoint, localidx,   SLE_UINT8,                   3, SL_MAX_VERSION),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   408
	SLE_CONDVAR(Waypoint, grfid,      SLE_UINT32,                 17, SL_MAX_VERSION),
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   409
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   410
	SLE_END()
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   411
};
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   412
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   413
static void Save_WAYP(void)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   414
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   415
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   416
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   417
	FOR_ALL_WAYPOINTS(wp) {
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   418
		SlSetArrayIndex(wp->index);
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   419
		SlObject(wp, _waypoint_desc);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   420
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   421
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   422
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   423
static void Load_WAYP(void)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   424
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   425
	int index;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   426
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   427
	while ((index = SlIterateArray()) != -1) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   428
		Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   429
4984
b16443d3ab20 (svn r6987) Use the pool macros for the Waypoint pool
tron
parents: 4527
diff changeset
   430
		if (!AddBlockIfNeeded(&_Waypoint_pool, index))
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   431
			error("Waypoints: failed loading savegame: too many waypoints");
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   432
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   433
		wp = GetWaypoint(index);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   434
		SlObject(wp, _waypoint_desc);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   435
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   436
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   437
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   438
const ChunkHandler _waypoint_chunk_handlers[] = {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   439
	{ 'CHKP', Save_WAYP, Load_WAYP, CH_ARRAY | CH_LAST},
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   440
};