waypoint.c
author rubidium
Tue, 22 Aug 2006 14:38:37 +0000
changeset 4344 7e123fec5b0b
parent 4326 2e2c9d21ed96
child 4346 66105d4f6e83
permissions -rw-r--r--
(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.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1881
diff changeset
     4
#include "openttd.h"
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
     5
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
     6
#include "command.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     7
#include "functions.h"
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
     8
#include "gfx.h"
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
     9
#include "map.h"
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    10
#include "order.h"
3101
a44fdf9faef1 (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
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    12
#include "saveload.h"
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    13
#include "station.h"
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    14
#include "tile.h"
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    15
#include "town.h"
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    16
#include "waypoint.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2085
diff changeset
    17
#include "variables.h"
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    18
#include "table/strings.h"
3578
c354a34a7c0a (svn r4464) -Codechange: Oops, forgot one file in last commit
celestar
parents: 3574
diff changeset
    19
#include "vehicle.h"
3900
2c84ed52709d (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
28670f743746 (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4077
diff changeset
    21
#include "date.h"
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    22
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    23
enum {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    24
	/* Max waypoints: 64000 (8 * 8000) */
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    25
	WAYPOINT_POOL_BLOCK_SIZE_BITS = 3,       /* In bits, so (1 << 3) == 8 */
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    26
	WAYPOINT_POOL_MAX_BLOCKS      = 8000,
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    27
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    28
	MAX_WAYPOINTS_PER_TOWN        = 64,
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    29
};
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    30
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    31
/**
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    32
 * Called if a new block is added to the waypoint-pool
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    33
 */
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    34
static void WaypointPoolNewBlock(uint start_item)
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    35
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    36
	Waypoint *wp;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    37
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
    38
	FOR_ALL_WAYPOINTS_FROM(wp, start_item) wp->index = start_item++;
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    39
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    40
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    41
/* Initialize the town-pool */
3585
43461f26b729 (svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents: 3578
diff changeset
    42
MemoryPool _waypoint_pool = { "Waypoints", WAYPOINT_POOL_MAX_BLOCKS, WAYPOINT_POOL_BLOCK_SIZE_BITS, sizeof(Waypoint), &WaypointPoolNewBlock, NULL, 0, 0, NULL };
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    43
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    44
/* Create a new waypoint */
2752
55e04dee346d (svn r3297) Staticise
tron
parents: 2670
diff changeset
    45
static Waypoint* AllocateWaypoint(void)
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    46
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    47
	Waypoint *wp;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    48
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    49
	FOR_ALL_WAYPOINTS(wp) {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    50
		if (wp->xy == 0) {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    51
			uint index = wp->index;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    52
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
    53
			memset(wp, 0, sizeof(*wp));
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    54
			wp->index = index;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    55
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    56
			return wp;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    57
		}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    58
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    59
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    60
	/* Check if we can add a block to the pool */
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
    61
	if (AddBlockToPool(&_waypoint_pool)) return AllocateWaypoint();
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    62
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    63
	return NULL;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    64
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    65
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    66
/* Update the sign for the waypoint */
2817
cdf488223c23 (svn r3365) Staticise 36 functions
tron
parents: 2752
diff changeset
    67
static void UpdateWaypointSign(Waypoint* wp)
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    68
{
3422
a6eba3443452 (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
    69
	Point pt = RemapCoords2(TileX(wp->xy) * TILE_SIZE, TileY(wp->xy) * TILE_SIZE);
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    70
	SetDParam(0, wp->index);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    71
	UpdateViewportSignPos(&wp->sign, pt.x, pt.y - 0x20, STR_WAYPOINT_VIEWPORT);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    72
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    73
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    74
/* Redraw the sign of a waypoint */
2752
55e04dee346d (svn r3297) Staticise
tron
parents: 2670
diff changeset
    75
static void RedrawWaypointSign(const Waypoint* wp)
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    76
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    77
	MarkAllViewportsDirty(
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    78
		wp->sign.left - 6,
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    79
		wp->sign.top,
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    80
		wp->sign.left + (wp->sign.width_1 << 2) + 12,
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    81
		wp->sign.top + 48);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    82
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    83
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    84
/* Update all signs */
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    85
void UpdateAllWaypointSigns(void)
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    86
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    87
	Waypoint *wp;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    88
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    89
	FOR_ALL_WAYPOINTS(wp) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
    90
		if (wp->xy != 0) UpdateWaypointSign(wp);
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    91
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    92
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    93
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    94
/* Set the default name for a waypoint */
2752
55e04dee346d (svn r3297) Staticise
tron
parents: 2670
diff changeset
    95
static void MakeDefaultWaypointName(Waypoint* wp)
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    96
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    97
	Waypoint *local_wp;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    98
	bool used_waypoint[MAX_WAYPOINTS_PER_TOWN];
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    99
	int i;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   100
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   101
	wp->town_index = ClosestTownFromTile(wp->xy, (uint)-1)->index;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   102
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   103
	memset(used_waypoint, 0, sizeof(used_waypoint));
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   104
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   105
	/* Find an unused waypoint number belonging to this town */
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   106
	FOR_ALL_WAYPOINTS(local_wp) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   107
		if (wp == local_wp) continue;
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   108
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   109
		if (local_wp->xy && local_wp->string == STR_NULL && local_wp->town_index == wp->town_index)
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   110
			used_waypoint[local_wp->town_cn] = true;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   111
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   112
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   113
	/* Find an empty spot */
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   114
	for (i = 0; used_waypoint[i] && i < MAX_WAYPOINTS_PER_TOWN; i++) {}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   115
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   116
	wp->string = STR_NULL;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   117
	wp->town_cn = i;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   118
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   119
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   120
/* Find a deleted waypoint close to a tile. */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
   121
static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile)
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   122
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   123
	Waypoint *wp, *best = NULL;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   124
	uint thres = 8;
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   125
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   126
	FOR_ALL_WAYPOINTS(wp) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   127
		if (wp->deleted && wp->xy != 0) {
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   128
			uint cur_dist = DistanceManhattan(tile, wp->xy);
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   129
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   130
			if (cur_dist < thres) {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   131
				thres = cur_dist;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   132
				best = wp;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   133
			}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   134
		}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   135
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   136
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   137
	return best;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   138
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   139
2670
f2d6a8424e3e (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
   140
/**
f2d6a8424e3e (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
   141
 * Update waypoint graphics id against saved GRFID/localidx.
f2d6a8424e3e (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
   142
 * This is to ensure the chosen graphics are correct if GRF files are changed.
f2d6a8424e3e (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
   143
 */
f2d6a8424e3e (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
   144
void UpdateAllWaypointCustomGraphics(void)
f2d6a8424e3e (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
   145
{
f2d6a8424e3e (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
   146
	Waypoint *wp;
f2d6a8424e3e (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
   147
f2d6a8424e3e (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
   148
	FOR_ALL_WAYPOINTS(wp) {
f2d6a8424e3e (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
		uint i;
f2d6a8424e3e (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
f2d6a8424e3e (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
		if (wp->grfid == 0) continue;
f2d6a8424e3e (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
f2d6a8424e3e (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
   153
		for (i = 0; i < GetNumCustomStations(STAT_CLASS_WAYP); i++) {
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   154
			const StationSpec *statspec = GetCustomStationSpec(STAT_CLASS_WAYP, i);
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   155
			if (statspec != NULL && statspec->grfid == wp->grfid && statspec->localidx == wp->localidx) {
2670
f2d6a8424e3e (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
				wp->stat_id = i;
f2d6a8424e3e (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
				break;
f2d6a8424e3e (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
			}
f2d6a8424e3e (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
		}
f2d6a8424e3e (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
	}
f2d6a8424e3e (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
}
f2d6a8424e3e (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
1782
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   163
/** Convert existing rail to waypoint. Eg build a waypoint station over
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   164
 * piece of rail
3491
35d747bb5e82 (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
   165
 * @param tile tile where waypoint will be built
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents: 2624
diff changeset
   166
 * @param p1 graphics for waypoint type, 0 indicates standard graphics
1782
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   167
 * @param p2 unused
2085
876f20a0e843 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   168
 *
876f20a0e843 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   169
 * @todo When checking for the tile slope,
876f20a0e843 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   170
 * distingush between "Flat land required" and "land sloped in wrong direction"
1782
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   171
 */
3491
35d747bb5e82 (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
   172
int32 CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   173
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   174
	Waypoint *wp;
3636
a36cc46e754d (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3585
diff changeset
   175
	Slope tileh;
3101
a44fdf9faef1 (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
   176
	Axis axis;
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   177
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   178
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   179
1782
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   180
	/* if custom gfx are used, make sure it is within bounds */
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents: 2624
diff changeset
   181
	if (p1 >= GetNumCustomStations(STAT_CLASS_WAYP)) return CMD_ERROR;
1782
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   182
3267
feff95208a9f (svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents: 3242
diff changeset
   183
	if (!IsTileType(tile, MP_RAILWAY) ||
3792
67c865c9315c (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
   184
			GetRailTileType(tile) != RAIL_TILE_NORMAL || (
3267
feff95208a9f (svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents: 3242
diff changeset
   185
				(axis = AXIS_X, GetTrackBits(tile) != TRACK_BIT_X) &&
feff95208a9f (svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents: 3242
diff changeset
   186
				(axis = AXIS_Y, GetTrackBits(tile) != TRACK_BIT_Y)
3101
a44fdf9faef1 (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
   187
			)) {
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   188
		return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK);
3101
a44fdf9faef1 (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
   189
	}
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   190
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   191
	if (!CheckTileOwnership(tile)) return CMD_ERROR;
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   192
	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   193
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   194
	tileh = GetTileSlope(tile, NULL);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   195
	if (tileh != SLOPE_FLAT &&
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   196
			(!_patches.build_on_slopes || IsSteepSlope(tileh) || !(tileh & (0x3 << axis)) || !(tileh & ~(0x3 << axis)))) {
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   197
		return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   198
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   199
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   200
	/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   201
	wp = FindDeletedWaypointCloseTo(tile);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   202
	if (wp == NULL) {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   203
		wp = AllocateWaypoint();
1782
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   204
		if (wp == NULL) return CMD_ERROR;
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   205
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   206
		wp->town_index = 0;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   207
		wp->string = STR_NULL;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   208
		wp->town_cn = 0;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   209
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   210
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   211
	if (flags & DC_EXEC) {
4051
22af4bcffbbb (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
   212
		const StationSpec* statspec;
22af4bcffbbb (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
   213
3242
4c32bf21153b (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3157
diff changeset
   214
		MakeRailWaypoint(tile, GetTileOwner(tile), axis, GetRailType(tile), wp->index);
3101
a44fdf9faef1 (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
   215
		MarkTileDirtyByTile(tile);
2670
f2d6a8424e3e (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
   216
4051
22af4bcffbbb (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
   217
		statspec = GetCustomStationSpec(STAT_CLASS_WAYP, p1);
2670
f2d6a8424e3e (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
   218
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   219
		if (statspec != NULL) {
4051
22af4bcffbbb (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
   220
			wp->stat_id = p1;
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   221
			wp->grfid = statspec->grfid;
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   222
			wp->localidx = statspec->localidx;
2670
f2d6a8424e3e (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
   223
		} else {
f2d6a8424e3e (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
   224
			// Specified custom graphics do not exist, so use default.
f2d6a8424e3e (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
			wp->stat_id = 0;
f2d6a8424e3e (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
   226
			wp->grfid = 0;
f2d6a8424e3e (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
			wp->localidx = 0;
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   228
		}
2670
f2d6a8424e3e (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
   229
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   230
		wp->deleted = 0;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   231
		wp->xy = tile;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   232
		wp->build_date = _date;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   233
4000
4009d092b306 (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
   234
		if (wp->town_index == 0) MakeDefaultWaypointName(wp);
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   235
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   236
		UpdateWaypointSign(wp);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   237
		RedrawWaypointSign(wp);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   238
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   239
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   240
	return _price.build_train_depot;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   241
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   242
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   243
/* Internal handler to delete a waypoint */
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   244
static void DoDeleteWaypoint(Waypoint *wp)
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   245
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   246
	Order order;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   247
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   248
	wp->xy = 0;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   249
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   250
	order.type = OT_GOTO_WAYPOINT;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   251
	order.station = wp->index;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   252
	DeleteDestinationFromVehicleOrder(order);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   253
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   254
	if (wp->string != STR_NULL) DeleteName(wp->string);
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   255
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   256
	RedrawWaypointSign(wp);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   257
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   258
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   259
/* Daily loop for waypoints */
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   260
void WaypointsDailyLoop(void)
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   261
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   262
	Waypoint *wp;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   263
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   264
	/* Check if we need to delete a waypoint */
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   265
	FOR_ALL_WAYPOINTS(wp) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   266
		if (wp->deleted != 0 && --wp->deleted == 0) DoDeleteWaypoint(wp);
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   267
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   268
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   269
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   270
/* Remove a waypoint */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
   271
int32 RemoveTrainWaypoint(TileIndex tile, uint32 flags, bool justremove)
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   272
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   273
	Waypoint *wp;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   274
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   275
	/* Make sure it's a waypoint */
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   276
	if (!IsTileType(tile, MP_RAILWAY) ||
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   277
			!IsRailWaypoint(tile) ||
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   278
			(!CheckTileOwnership(tile) && _current_player != OWNER_WATER) ||
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   279
			!EnsureNoVehicle(tile)) {
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   280
		return CMD_ERROR;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   281
	}
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   282
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   283
	if (flags & DC_EXEC) {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   284
		wp = GetWaypointByTile(tile);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   285
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   286
		wp->deleted = 30; // let it live for this many days before we do the actual deletion.
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   287
		RedrawWaypointSign(wp);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   288
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   289
		if (justremove) {
3242
4c32bf21153b (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3157
diff changeset
   290
			MakeRailNormal(tile, GetTileOwner(tile), GetRailWaypointBits(tile), GetRailType(tile));
3101
a44fdf9faef1 (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
   291
			MarkTileDirtyByTile(tile);
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   292
		} else {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   293
			DoClearSquare(tile);
3448
bc96e486627d (svn r4280) -Codechange: Add and make use of map accessors concerning railway waypoints
celestar
parents: 3422
diff changeset
   294
			SetSignalsOnBothDir(tile, GetRailWaypointTrack(tile));
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3792
diff changeset
   295
			YapfNotifyTrackLayoutChange(tile, GetRailWaypointTrack(tile));
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   296
		}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   297
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   298
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   299
	return _price.remove_train_depot;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   300
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   301
1782
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   302
/** Delete a waypoint
3491
35d747bb5e82 (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
   303
 * @param tile tile where waypoint is to be deleted
1782
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   304
 * @param p1 unused
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   305
 * @param p2 unused
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   306
 */
3491
35d747bb5e82 (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
   307
int32 CmdRemoveTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   308
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   309
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   310
	return RemoveTrainWaypoint(tile, flags, true);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   311
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   312
1782
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   313
/** Rename a waypoint.
3491
35d747bb5e82 (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
   314
 * @param tile unused
1782
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   315
 * @param p1 id of waypoint
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   316
 * @param p2 unused
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   317
 */
3491
35d747bb5e82 (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
   318
int32 CmdRenameWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   319
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   320
	Waypoint *wp;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   321
1782
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   322
	if (!IsWaypointIndex(p1)) return CMD_ERROR;
48749e4c2dc6 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   323
1820
d03c56850dc2 (svn r2324) Introduce _cmd_text for passing strings with a command instead of abusing _decode_parameters as text buffer. This should prevent several possible buffer overruns and is a bit cleaner to use. As bonus it reduces the size of most command packets by 79 bytes.
tron
parents: 1783
diff changeset
   324
	if (_cmd_text[0] != '\0') {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   325
		StringID str = AllocateNameUnique(_cmd_text, 0);
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   326
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   327
		if (str == 0) return CMD_ERROR;
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   328
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   329
		if (flags & DC_EXEC) {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   330
			wp = GetWaypoint(p1);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   331
			if (wp->string != STR_NULL) DeleteName(wp->string);
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   332
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   333
			wp->string = str;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   334
			wp->town_cn = 0;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   335
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   336
			UpdateWaypointSign(wp);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   337
			MarkWholeScreenDirty();
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   338
		} else {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   339
			DeleteName(str);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   340
		}
2026
567e3bc9af72 (svn r2535) Tabs
tron
parents: 1980
diff changeset
   341
	} else {
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   342
		if (flags & DC_EXEC) {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   343
			wp = GetWaypoint(p1);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   344
			if (wp->string != STR_NULL) DeleteName(wp->string);
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   345
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   346
			MakeDefaultWaypointName(wp);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   347
			UpdateWaypointSign(wp);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   348
			MarkWholeScreenDirty();
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   349
		}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   350
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   351
	return 0;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   352
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   353
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   354
/* This hacks together some dummy one-shot Station structure for a waypoint. */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
   355
Station *ComposeWaypointStation(TileIndex tile)
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   356
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   357
	Waypoint *wp = GetWaypointByTile(tile);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   358
	static Station stat;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   359
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   360
	stat.train_tile = stat.xy = wp->xy;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   361
	stat.town = GetTown(wp->town_index);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   362
	stat.string_id = wp->string == STR_NULL ? /* FIXME? */ 0 : wp->string;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   363
	stat.build_date = wp->build_date;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   364
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   365
	return &stat;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   366
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   367
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   368
/* Draw a waypoint */
2520
8a52362c4ada (svn r3049) Replace byte/int/uint by RailType where appropriate
tron
parents: 2261
diff changeset
   369
void DrawWaypointSprite(int x, int y, int stat_id, RailType railtype)
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   370
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   371
	x += 33;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   372
	y += 17;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   373
3764
f017ce73e96b (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
   374
	if (!DrawStationTile(x, y, railtype, AXIS_X, STAT_CLASS_WAYP, stat_id)) {
2261
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2254
diff changeset
   375
		DrawDefaultWaypointSprite(x, y, railtype);
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   376
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   377
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   378
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   379
/* Fix savegames which stored waypoints in their old format */
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   380
void FixOldWaypoints(void)
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   381
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   382
	Waypoint *wp;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   383
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   384
	/* Convert the old 'town_or_string', to 'string' / 'town' / 'town_cn' */
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   385
	FOR_ALL_WAYPOINTS(wp) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   386
		if (wp->xy == 0) continue;
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   387
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   388
		wp->town_index = ClosestTownFromTile(wp->xy, (uint)-1)->index;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   389
		wp->town_cn = 0;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   390
		if (wp->string & 0xC000) {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   391
			wp->town_cn = wp->string & 0x3F;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   392
			wp->string = STR_NULL;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   393
		}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   394
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   395
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   396
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   397
void InitializeWaypoints(void)
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   398
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   399
	CleanPool(&_waypoint_pool);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   400
	AddBlockToPool(&_waypoint_pool);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   401
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   402
1881
435d39bd6ee0 (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
   403
static const SaveLoad _waypoint_desc[] = {
4344
7e123fec5b0b (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
   404
	SLE_CONDVAR(Waypoint, xy,         SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
7e123fec5b0b (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, xy,         SLE_UINT32,                  6, SL_MAX_VERSION),
7e123fec5b0b (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, town_index, SLE_UINT16,                 12, SL_MAX_VERSION),
7e123fec5b0b (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, town_cn,    SLE_UINT8,                  12, SL_MAX_VERSION),
7e123fec5b0b (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_VAR(Waypoint, string,     SLE_UINT16),
7e123fec5b0b (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
   409
	    SLE_VAR(Waypoint, deleted,    SLE_UINT8),
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   410
4344
7e123fec5b0b (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
   411
	SLE_CONDVAR(Waypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32,  3, 30),
7e123fec5b0b (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
   412
	SLE_CONDVAR(Waypoint, build_date, SLE_INT32,                  31, SL_MAX_VERSION),
7e123fec5b0b (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
   413
	SLE_CONDVAR(Waypoint, localidx,   SLE_UINT8,                   3, SL_MAX_VERSION),
7e123fec5b0b (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
   414
	SLE_CONDVAR(Waypoint, grfid,      SLE_UINT32,                 17, SL_MAX_VERSION),
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   415
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   416
	SLE_END()
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   417
};
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   418
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   419
static void Save_WAYP(void)
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   420
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   421
	Waypoint *wp;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   422
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   423
	FOR_ALL_WAYPOINTS(wp) {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   424
		if (wp->xy != 0) {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   425
			SlSetArrayIndex(wp->index);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   426
			SlObject(wp, _waypoint_desc);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   427
		}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   428
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   429
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   430
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   431
static void Load_WAYP(void)
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   432
{
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   433
	int index;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   434
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   435
	while ((index = SlIterateArray()) != -1) {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   436
		Waypoint *wp;
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   437
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   438
		if (!AddBlockIfNeeded(&_waypoint_pool, index))
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   439
			error("Waypoints: failed loading savegame: too many waypoints");
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   440
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   441
		wp = GetWaypoint(index);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   442
		SlObject(wp, _waypoint_desc);
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   443
	}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   444
}
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   445
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   446
const ChunkHandler _waypoint_chunk_handlers[] = {
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   447
	{ 'CHKP', Save_WAYP, Load_WAYP, CH_ARRAY | CH_LAST},
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   448
};