waypoint.c
author matthijs
Wed, 22 Mar 2006 22:26:16 +0000
branch0.4.5
changeset 9958 bed516c67d61
parent 2916 8f1aa489701f
child 3047 acae37f12efb
permissions -rw-r--r--
(svn r4041) [Debian] Change next version number to 0.4.6 instead of 0.4.5.1.
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"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    11
#include "saveload.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    12
#include "station.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    13
#include "tile.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    14
#include "town.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    15
#include "waypoint.h"
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2085
diff changeset
    16
#include "variables.h"
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    17
#include "table/sprites.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    18
#include "table/strings.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    19
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    20
enum {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    21
	/* Max waypoints: 64000 (8 * 8000) */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    22
	WAYPOINT_POOL_BLOCK_SIZE_BITS = 3,       /* In bits, so (1 << 3) == 8 */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    23
	WAYPOINT_POOL_MAX_BLOCKS      = 8000,
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    24
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    25
	MAX_WAYPOINTS_PER_TOWN        = 64,
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
/**
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    29
 * 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
    30
 */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    31
static void WaypointPoolNewBlock(uint start_item)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    32
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    33
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    34
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    35
	FOR_ALL_WAYPOINTS_FROM(wp, start_item)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    36
		wp->index = start_item++;
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
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    39
/* Initialize the town-pool */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    40
MemoryPool _waypoint_pool = { "Waypoints", WAYPOINT_POOL_MAX_BLOCKS, WAYPOINT_POOL_BLOCK_SIZE_BITS, sizeof(Waypoint), &WaypointPoolNewBlock, 0, 0, NULL };
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    41
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    42
/* Create a new waypoint */
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2670
diff changeset
    43
static Waypoint* AllocateWaypoint(void)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    44
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    45
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    46
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    47
	FOR_ALL_WAYPOINTS(wp) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    48
		if (wp->xy == 0) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    49
			uint index = wp->index;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    50
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    51
			memset(wp, 0, sizeof(Waypoint));
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    52
			wp->index = index;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    53
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    54
			return wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    55
		}
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
	/* Check if we can add a block to the pool */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    59
	if (AddBlockToPool(&_waypoint_pool))
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    60
		return AllocateWaypoint();
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
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    68
	Point pt = RemapCoords2(TileX(wp->xy) * 16, TileY(wp->xy) * 16);
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) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    89
		if (wp->xy)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    90
			UpdateWaypointSign(wp);
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
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    93
2ca6d1624e6d (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
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2670
diff changeset
    95
static void MakeDefaultWaypointName(Waypoint* wp)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    96
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    97
	Waypoint *local_wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    98
	bool used_waypoint[MAX_WAYPOINTS_PER_TOWN];
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    99
	int i;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   100
2ca6d1624e6d (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;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   102
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   103
	memset(used_waypoint, 0, sizeof(used_waypoint));
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   104
2ca6d1624e6d (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 */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   106
	FOR_ALL_WAYPOINTS(local_wp) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   107
		if (wp == local_wp)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   108
			continue;
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
		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
   111
			used_waypoint[local_wp->town_cn] = true;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   112
	}
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 empty spot */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   115
	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
   116
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   117
	wp->string = STR_NULL;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   118
	wp->town_cn = i;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   119
}
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
/* 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
   122
static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   123
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   124
	Waypoint *wp, *best = NULL;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   125
	uint thres = 8, cur_dist;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   126
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   127
	FOR_ALL_WAYPOINTS(wp) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   128
		if (wp->deleted && wp->xy) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   129
			cur_dist = DistanceManhattan(tile, wp->xy);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   130
			if (cur_dist < thres) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   131
				thres = cur_dist;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   132
				best = wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   133
			}
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
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   136
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   137
	return best;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   138
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   139
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
   140
/**
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
   141
 * 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
   142
 * 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
   143
 */
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
   144
void UpdateAllWaypointCustomGraphics(void)
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
   145
{
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
   146
	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
   147
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
   148
	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
   149
		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
   150
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
		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
   152
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
   153
		for (i = 0; i < GetNumCustomStations(STAT_CLASS_WAYP); 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
   154
			const StationSpec *spec = GetCustomStation(STAT_CLASS_WAYP, 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
   155
			if (spec != NULL && spec->grfid == wp->grfid && spec->localidx == wp->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
   156
				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
   157
				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
   158
			}
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
	}
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
1782
0ac16ff36293 (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
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   164
 * piece of rail
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   165
 * @param x,y coordinates where waypoint will be built
2625
66b3d632dcd2 (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
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   167
 * @param p2 unused
2085
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   168
 *
ae9e92ffe168 (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,
ae9e92ffe168 (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
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   171
 */
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   172
int32 CmdBuildTrainWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   173
{
1980
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   174
	TileIndex tile = TileVirtXY(x, y);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   175
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   176
	uint tileh;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   177
	uint dir;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   178
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   179
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   180
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   181
	/* 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
   182
	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
   183
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2026
diff changeset
   184
	if (!IsTileType(tile, MP_RAILWAY) || ((dir = 0, _m[tile].m5 != 1) && (dir = 1, _m[tile].m5 != 2)))
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   185
		return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK);
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
	if (!CheckTileOwnership(tile))
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   188
		return CMD_ERROR;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   189
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   190
	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   191
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   192
	tileh = GetTileSlope(tile, NULL);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   193
	if (tileh != 0) {
2085
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   194
		if (!_patches.build_on_slopes || IsSteepTileh(tileh) || !(tileh & (0x3 << dir)) || !(tileh & ~(0x3 << dir)))
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   195
			return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   196
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   197
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   198
	/* 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
   199
	wp = FindDeletedWaypointCloseTo(tile);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   200
	if (wp == NULL) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   201
		wp = AllocateWaypoint();
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   202
		if (wp == NULL) return CMD_ERROR;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   203
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   204
		wp->town_index = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   205
		wp->string = STR_NULL;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   206
		wp->town_cn = 0;
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
	if (flags & DC_EXEC) {
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
   210
		const StationSpec *spec = NULL;
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
   211
		ModifyTile(tile, MP_MAP2 | MP_MAP5, wp->index, RAIL_TYPE_WAYPOINT | dir);
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
   212
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
   213
		if (GB(p1, 0, 8) < GetNumCustomStations(STAT_CLASS_WAYP))
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
   214
			spec = GetCustomStation(STAT_CLASS_WAYP, GB(p1, 0, 8));
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
   215
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
   216
		if (spec != NULL) {
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
   217
			SETBIT(_m[tile].m3, 4);
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
   218
			wp->stat_id = GB(p1, 0, 8);
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
   219
			wp->grfid = spec->grfid;
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
   220
			wp->localidx = spec->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
   221
		} 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
   222
			// 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
   223
			CLRBIT(_m[tile].m3, 4);
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
   224
			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
   225
			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
   226
			wp->localidx = 0;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   227
		}
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
   228
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   229
		wp->deleted = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   230
		wp->xy = tile;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   231
		wp->build_date = _date;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   232
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   233
		if (wp->town_index == STR_NULL)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   234
			MakeDefaultWaypointName(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   235
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   236
		UpdateWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   237
		RedrawWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   238
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   239
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   240
	return _price.build_train_depot;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   241
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   242
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   243
/* Internal handler to delete a waypoint */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   244
static void DoDeleteWaypoint(Waypoint *wp)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   245
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   246
	Order order;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   247
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   248
	wp->xy = 0;
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
	order.type = OT_GOTO_WAYPOINT;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   251
	order.station = wp->index;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   252
	DeleteDestinationFromVehicleOrder(order);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   253
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   254
	if (wp->string != STR_NULL)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   255
		DeleteName(wp->string);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   256
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   257
	RedrawWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   258
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   259
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   260
/* Daily loop for waypoints */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   261
void WaypointsDailyLoop(void)
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
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   264
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   265
	/* 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
   266
	FOR_ALL_WAYPOINTS(wp) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   267
		if (wp->deleted && !--wp->deleted) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   268
			DoDeleteWaypoint(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   269
		}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   270
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   271
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   272
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   273
/* Remove a waypoint */
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
   274
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
   275
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   276
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   277
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   278
	/* Make sure it's a waypoint */
2668
54d9f9d4dca3 (svn r3210) -Codechange: use IsRailWaypoint where possible (instead of magicnumbers)
truelight
parents: 2625
diff changeset
   279
	if (!IsTileType(tile, MP_RAILWAY) || !IsRailWaypoint(tile))
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   280
		return CMD_ERROR;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   281
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   282
	if (!CheckTileOwnership(tile) && !(_current_player == OWNER_WATER))
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   283
		return CMD_ERROR;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   284
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   285
	if (!EnsureNoVehicle(tile))
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   286
		return CMD_ERROR;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   287
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   288
	if (flags & DC_EXEC) {
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2026
diff changeset
   289
		int direction = _m[tile].m5 & RAIL_WAYPOINT_TRACK_MASK;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   290
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   291
		wp = GetWaypointByTile(tile);
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
		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
   294
		RedrawWaypointSign(wp);
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
		if (justremove) {
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
   297
			ModifyTile(tile, MP_MAP2_CLEAR | MP_MAP5, 1<<direction);
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
   298
			CLRBIT(_m[tile].m3, 4);
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2026
diff changeset
   299
			_m[tile].m4 = 0;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   300
		} else {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   301
			DoClearSquare(tile);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   302
			SetSignalsOnBothDir(tile, direction);
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
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   305
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   306
	return _price.remove_train_depot;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   307
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   308
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   309
/** Delete a waypoint
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   310
 * @param x,y coordinates where waypoint is to be deleted
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   311
 * @param p1 unused
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   312
 * @param p2 unused
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   313
 */
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   314
int32 CmdRemoveTrainWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   315
{
1980
9ea0c89fbb58 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   316
	TileIndex tile = TileVirtXY(x, y);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   317
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   318
	return RemoveTrainWaypoint(tile, flags, true);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   319
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   320
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   321
/** Rename a waypoint.
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   322
 * @param x,y unused
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   323
 * @param p1 id of waypoint
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   324
 * @param p2 unused
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   325
 */
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   326
int32 CmdRenameWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   327
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   328
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   329
	StringID str;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   330
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   331
	if (!IsWaypointIndex(p1)) return CMD_ERROR;
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   332
1820
9b6458526480 (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
   333
	if (_cmd_text[0] != '\0') {
9b6458526480 (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
   334
		str = AllocateNameUnique(_cmd_text, 0);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   335
		if (str == 0)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   336
			return CMD_ERROR;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   337
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   338
		if (flags & DC_EXEC) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   339
			wp = GetWaypoint(p1);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   340
			if (wp->string != STR_NULL)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   341
				DeleteName(wp->string);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   342
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   343
			wp->string = str;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   344
			wp->town_cn = 0;
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
			UpdateWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   347
			MarkWholeScreenDirty();
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   348
		} else {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   349
			DeleteName(str);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   350
		}
2026
02dfa0aa2c2f (svn r2535) Tabs
tron
parents: 1980
diff changeset
   351
	} else {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   352
		if (flags & DC_EXEC) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   353
			wp = GetWaypoint(p1);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   354
			if (wp->string != STR_NULL)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   355
				DeleteName(wp->string);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   356
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   357
			MakeDefaultWaypointName(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   358
			UpdateWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   359
			MarkWholeScreenDirty();
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
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   362
	return 0;
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
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   365
/* 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
   366
Station *ComposeWaypointStation(TileIndex tile)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   367
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   368
	Waypoint *wp = GetWaypointByTile(tile);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   369
	static Station stat;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   370
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   371
	stat.train_tile = stat.xy = wp->xy;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   372
	stat.town = GetTown(wp->town_index);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   373
	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
   374
	stat.build_date = wp->build_date;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   375
	stat.class_id = 6;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   376
	stat.stat_id = wp->stat_id;
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
	return &stat;
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
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   381
extern uint16 _custom_sprites_base;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   382
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2254
diff changeset
   383
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   384
/* Draw a waypoint */
2520
75eb66d6e371 (svn r3049) Replace byte/int/uint by RailType where appropriate
tron
parents: 2261
diff changeset
   385
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
   386
{
2624
47d7768c6dca (svn r3166) Constify read-only accesses of custom stations
peter1138
parents: 2603
diff changeset
   387
	const StationSpec *stat;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   388
	uint32 relocation;
2624
47d7768c6dca (svn r3166) Constify read-only accesses of custom stations
peter1138
parents: 2603
diff changeset
   389
	const DrawTileSprites *cust;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   390
	DrawTileSeqStruct const *seq;
2254
72f8883ff3ac (svn r2774) -Codechange: Removed TRACKTYPE_SPRITE_PITCH globally and replaced it by a member of RailtypeInfo
celestar
parents: 2187
diff changeset
   391
	const RailtypeInfo *rti = GetRailTypeInfo(railtype);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   392
	uint32 ormod, img;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   393
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   394
	ormod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player));
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
	x += 33;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   397
	y += 17;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   398
2625
66b3d632dcd2 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents: 2624
diff changeset
   399
	stat = GetCustomStation(STAT_CLASS_WAYP, stat_id);
66b3d632dcd2 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents: 2624
diff changeset
   400
	if (stat == NULL) {
66b3d632dcd2 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents: 2624
diff changeset
   401
		// stat is NULL for default waypoints and when waypoint graphics are
66b3d632dcd2 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents: 2624
diff changeset
   402
		// not loaded.
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2254
diff changeset
   403
		DrawDefaultWaypointSprite(x, y, railtype);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   404
		return;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   405
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   406
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   407
	relocation = GetCustomStationRelocation(stat, NULL, 1);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   408
	// emulate station tile - open with building
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   409
	// add 1 to get the other direction
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   410
	cust = &stat->renderdata[2];
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
	img = cust->ground_sprite;
2254
72f8883ff3ac (svn r2774) -Codechange: Removed TRACKTYPE_SPRITE_PITCH globally and replaced it by a member of RailtypeInfo
celestar
parents: 2187
diff changeset
   413
	img += (img < _custom_sprites_base) ? rti->total_offset : railtype;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   414
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   415
	if (img & PALETTE_MODIFIER_COLOR) img = (img & SPRITE_MASK);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   416
	DrawSprite(img, x, y);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   417
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   418
	foreach_draw_tile_seq(seq, cust->seq) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   419
		Point pt = RemapCoords(seq->delta_x, seq->delta_y, seq->delta_z);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   420
		uint32 image = seq->image + relocation;
2187
2a51f8925eeb (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
   421
		DrawSprite((image & SPRITE_MASK) | ormod, x + pt.x, y + pt.y);
1542
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
}
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
/* 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
   426
void FixOldWaypoints(void)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   427
{
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
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   430
	/* 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
   431
	FOR_ALL_WAYPOINTS(wp) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   432
		if (wp->xy == 0)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   433
			continue;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   434
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   435
		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
   436
		wp->town_cn = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   437
		if (wp->string & 0xC000) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   438
			wp->town_cn = wp->string & 0x3F;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   439
			wp->string = STR_NULL;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   440
		}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   441
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   442
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   443
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   444
void InitializeWaypoints(void)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   445
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   446
	CleanPool(&_waypoint_pool);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   447
	AddBlockToPool(&_waypoint_pool);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   448
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   449
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
   450
static const SaveLoad _waypoint_desc[] = {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   451
	SLE_CONDVAR(Waypoint, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   452
	SLE_CONDVAR(Waypoint, xy, SLE_UINT32, 6, 255),
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   453
	SLE_CONDVAR(Waypoint, town_index, SLE_UINT16, 12, 255),
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   454
	SLE_CONDVAR(Waypoint, town_cn, SLE_UINT8, 12, 255),
2026
02dfa0aa2c2f (svn r2535) Tabs
tron
parents: 1980
diff changeset
   455
	SLE_VAR(Waypoint, string, SLE_UINT16),
02dfa0aa2c2f (svn r2535) Tabs
tron
parents: 1980
diff changeset
   456
	SLE_VAR(Waypoint, deleted, SLE_UINT8),
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   457
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
   458
	SLE_CONDVAR(Waypoint, build_date, SLE_UINT16,  3, 255),
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
   459
	SLE_CONDVAR(Waypoint, localidx,   SLE_UINT8,   3, 255),
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
   460
	SLE_CONDVAR(Waypoint, grfid,      SLE_UINT32, 17, 255),
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   461
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   462
	SLE_END()
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   463
};
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   464
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   465
static void Save_WAYP(void)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   466
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   467
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   468
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   469
	FOR_ALL_WAYPOINTS(wp) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   470
		if (wp->xy != 0) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   471
			SlSetArrayIndex(wp->index);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   472
			SlObject(wp, _waypoint_desc);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   473
		}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   474
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   475
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   476
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   477
static void Load_WAYP(void)
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   478
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   479
	int index;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   480
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   481
	while ((index = SlIterateArray()) != -1) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   482
		Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   483
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   484
		if (!AddBlockIfNeeded(&_waypoint_pool, index))
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   485
			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
   486
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   487
		wp = GetWaypoint(index);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   488
		SlObject(wp, _waypoint_desc);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   489
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   490
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   491
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   492
const ChunkHandler _waypoint_chunk_handlers[] = {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   493
	{ '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
   494
};