src/waypoint.cpp
author rubidium
Sun, 03 Feb 2008 20:17:54 +0000
branchnoai
changeset 9724 b39bc69bb2f2
parent 9723 eee46cb39750
child 9732 f8eb3e208514
permissions -rw-r--r--
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
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
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
     3
/** @file waypoint.cpp */
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
     4
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1881
diff changeset
     6
#include "openttd.h"
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
     7
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9718
diff changeset
     8
#include "command_func.h"
9599
949374e83b78 (svn r9632) [NoAI] -Sync with trunk r9574:9631.
rubidium
parents: 9574
diff changeset
     9
#include "landscape.h"
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    10
#include "order.h"
3101
e2fdb8802c2f (svn r3696) Add functions to turn a tile into a normal rail tile/depot/waypoint. This is just a tiny step, the rail code needs way more love and caring
tron
parents: 3047
diff changeset
    11
#include "rail_map.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9718
diff changeset
    12
#include "rail.h"
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5350
diff changeset
    13
#include "bridge_map.h"
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    14
#include "saveload.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    15
#include "station.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    16
#include "town.h"
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    17
#include "waypoint.h"
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2085
diff changeset
    18
#include "variables.h"
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3792
diff changeset
    19
#include "yapf/yapf.h"
9599
949374e83b78 (svn r9632) [NoAI] -Sync with trunk r9574:9631.
rubidium
parents: 9574
diff changeset
    20
#include "newgrf.h"
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
    21
#include "misc/autoptr.hpp"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9718
diff changeset
    22
#include "strings_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9718
diff changeset
    23
#include "gfx_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9718
diff changeset
    24
#include "functions.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9718
diff changeset
    25
#include "window_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9718
diff changeset
    26
#include "economy_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9718
diff changeset
    27
#include "date_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9718
diff changeset
    28
#include "vehicle_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9718
diff changeset
    29
#include "vehicle_base.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9718
diff changeset
    30
#include "string_func.h"
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    31
#include "signal_func.h"
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    32
#include "player_func.h"
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    33
#include "settings_type.h"
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    34
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    35
#include "table/strings.h"
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    36
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    37
enum {
4984
b16443d3ab20 (svn r6987) Use the pool macros for the Waypoint pool
tron
parents: 4527
diff changeset
    38
	MAX_WAYPOINTS_PER_TOWN = 64,
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    39
};
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    40
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
    41
DEFINE_OLD_POOL_GENERIC(Waypoint, Waypoint)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    42
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    43
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
    44
/**
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
    45
 * Update the sign for the waypoint
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
    46
 * @param wp Waypoint to update sign */
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2752
diff changeset
    47
static void UpdateWaypointSign(Waypoint* wp)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    48
{
3422
12cdb13ddb56 (svn r4249) -Codechange: Replace more occurences of 16 by TILE_SIZE and of 8 by TILE_HEIGHT. Reverted one change from the previous commit because it was faulty
celestar
parents: 3349
diff changeset
    49
	Point pt = RemapCoords2(TileX(wp->xy) * TILE_SIZE, TileY(wp->xy) * TILE_SIZE);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    50
	SetDParam(0, wp->index);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    51
	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
    52
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    53
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
    54
/**
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
    55
 * Redraw the sign of a waypoint
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
    56
 * @param wp Waypoint to redraw sign */
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2670
diff changeset
    57
static void RedrawWaypointSign(const Waypoint* wp)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    58
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    59
	MarkAllViewportsDirty(
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    60
		wp->sign.left - 6,
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    61
		wp->sign.top,
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    62
		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
    63
		wp->sign.top + 48);
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
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
    66
/**
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
    67
 * Update all signs
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
    68
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
    69
void UpdateAllWaypointSigns()
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    70
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    71
	Waypoint *wp;
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
	FOR_ALL_WAYPOINTS(wp) {
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
    74
		UpdateWaypointSign(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
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    77
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
    78
/**
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
    79
 * Set the default name for a waypoint
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
    80
 * @param wp Waypoint to work on
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
    81
 */
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2670
diff changeset
    82
static void MakeDefaultWaypointName(Waypoint* wp)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    83
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    84
	Waypoint *local_wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    85
	bool used_waypoint[MAX_WAYPOINTS_PER_TOWN];
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    86
	int i;
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
	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
    89
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    90
	memset(used_waypoint, 0, sizeof(used_waypoint));
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
	/* 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
    93
	FOR_ALL_WAYPOINTS(local_wp) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
    94
		if (wp == local_wp) continue;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    95
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    96
		if (local_wp->xy && local_wp->name == NULL && local_wp->town_index == wp->town_index)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    97
			used_waypoint[local_wp->town_cn] = true;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    98
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    99
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   100
	/* Find an empty spot */
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   101
	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
   102
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   103
	wp->name = NULL;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   104
	wp->town_cn = i;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   105
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   106
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   107
/**
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   108
 * Find a deleted waypoint close to a tile.
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   109
 * @param tile to search from
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   110
 */
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
   111
static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile)
1542
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
	Waypoint *wp, *best = NULL;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   114
	uint thres = 8;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   115
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   116
	FOR_ALL_WAYPOINTS(wp) {
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   117
		if (wp->deleted) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   118
			uint cur_dist = DistanceManhattan(tile, wp->xy);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   119
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   120
			if (cur_dist < thres) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   121
				thres = cur_dist;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   122
				best = wp;
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
		}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   125
	}
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
	return best;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   128
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   129
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
   130
/**
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
   131
 * 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
   132
 * 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
   133
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
   134
void AfterLoadWaypoints()
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
   135
{
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
   136
	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
   137
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
   138
	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
   139
		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
   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
		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
   142
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
		for (i = 0; i < GetNumCustomStations(STAT_CLASS_WAYP); i++) {
3676
48444d881fbe (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   144
			const StationSpec *statspec = GetCustomStationSpec(STAT_CLASS_WAYP, i);
9599
949374e83b78 (svn r9632) [NoAI] -Sync with trunk r9574:9631.
rubidium
parents: 9574
diff changeset
   145
			if (statspec != NULL && statspec->grffile->grfid == wp->grfid && statspec->localidx == wp->localidx) {
2670
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   146
				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
   147
				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
   148
			}
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   149
		}
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   150
	}
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
}
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
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   153
/** 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
   154
 * piece of rail
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3448
diff changeset
   155
 * @param tile tile where waypoint will be built
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   156
 * @param flags type of operation
2625
66b3d632dcd2 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents: 2624
diff changeset
   157
 * @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
   158
 * @param p2 unused
2085
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   159
 *
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   160
 * @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
   161
 * 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
   162
 */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9599
diff changeset
   163
CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   164
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   165
	Waypoint *wp;
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   166
	AutoPtrT<Waypoint> wp_auto_delete;
3636
d87b21df2944 (svn r4541) Add a type for slopes and replace many magic numbers by the appropriate enums
tron
parents: 3585
diff changeset
   167
	Slope tileh;
3101
e2fdb8802c2f (svn r3696) Add functions to turn a tile into a normal rail tile/depot/waypoint. This is just a tiny step, the rail code needs way more love and caring
tron
parents: 3047
diff changeset
   168
	Axis axis;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   169
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   170
	/* 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
   171
	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
   172
3267
591027d10884 (svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents: 3242
diff changeset
   173
	if (!IsTileType(tile, MP_RAILWAY) ||
3792
2eb8388731bf (svn r4788) - Codechange: RAILTYPE_{NORMAL,ELECTRIC,...} and RAIL_TYPE_{NORMAL,SIGNAL,...} have nearly the same name, rename RAIL_TYPE_* to RAIL_TILE_* of extra clarity
rubidium
parents: 3764
diff changeset
   174
			GetRailTileType(tile) != RAIL_TILE_NORMAL || (
3267
591027d10884 (svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents: 3242
diff changeset
   175
				(axis = AXIS_X, GetTrackBits(tile) != TRACK_BIT_X) &&
591027d10884 (svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents: 3242
diff changeset
   176
				(axis = AXIS_Y, GetTrackBits(tile) != TRACK_BIT_Y)
3101
e2fdb8802c2f (svn r3696) Add functions to turn a tile into a normal rail tile/depot/waypoint. This is just a tiny step, the rail code needs way more love and caring
tron
parents: 3047
diff changeset
   177
			)) {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   178
		return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK);
3101
e2fdb8802c2f (svn r3696) Add functions to turn a tile into a normal rail tile/depot/waypoint. This is just a tiny step, the rail code needs way more love and caring
tron
parents: 3047
diff changeset
   179
	}
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   180
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   181
	if (!CheckTileOwnership(tile)) return CMD_ERROR;
9718
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   182
	if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   183
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   184
	tileh = GetTileSlope(tile, NULL);
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   185
	if (tileh != SLOPE_FLAT &&
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   186
			(!_patches.build_on_slopes || IsSteepSlope(tileh) || !(tileh & (0x3 << axis)) || !(tileh & ~(0x3 << axis)))) {
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   187
		return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   188
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   189
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5350
diff changeset
   190
	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5350
diff changeset
   191
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   192
	/* 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
   193
	wp = FindDeletedWaypointCloseTo(tile);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   194
	if (wp == NULL) {
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   195
		wp = new Waypoint(tile);
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   196
		if (wp == NULL) return CMD_ERROR;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   197
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   198
		wp_auto_delete = wp;
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   199
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   200
		wp->town_index = 0;
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   201
		wp->name = NULL;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   202
		wp->town_cn = 0;
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   203
	} else if (flags & DC_EXEC) {
9718
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   204
		/* Move existing (recently deleted) waypoint to the new location */
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   205
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   206
		/* First we update the destination for all vehicles that
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   207
		 * have the old waypoint in their orders. */
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   208
		Vehicle *v;
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   209
		FOR_ALL_VEHICLES(v) {
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   210
			if (v->type == VEH_TRAIN &&
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   211
					v->First() == v &&
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   212
					v->current_order.type == OT_GOTO_WAYPOINT &&
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   213
					v->dest_tile == wp->xy) {
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   214
				v->dest_tile = tile;
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   215
			}
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   216
		}
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   217
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   218
		RedrawWaypointSign(wp);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   219
		wp->xy = tile;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   220
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   221
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   222
	if (flags & DC_EXEC) {
4051
566d63f3c5a2 (svn r5325) Don't test something if we already know the result true, because the same test was performed just a few lines before
tron
parents: 4000
diff changeset
   223
		const StationSpec* statspec;
566d63f3c5a2 (svn r5325) Don't test something if we already know the result true, because the same test was performed just a few lines before
tron
parents: 4000
diff changeset
   224
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3157
diff changeset
   225
		MakeRailWaypoint(tile, GetTileOwner(tile), axis, GetRailType(tile), wp->index);
3101
e2fdb8802c2f (svn r3696) Add functions to turn a tile into a normal rail tile/depot/waypoint. This is just a tiny step, the rail code needs way more love and caring
tron
parents: 3047
diff changeset
   226
		MarkTileDirtyByTile(tile);
2670
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   227
4051
566d63f3c5a2 (svn r5325) Don't test something if we already know the result true, because the same test was performed just a few lines before
tron
parents: 4000
diff changeset
   228
		statspec = GetCustomStationSpec(STAT_CLASS_WAYP, p1);
2670
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   229
3676
48444d881fbe (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   230
		if (statspec != NULL) {
4051
566d63f3c5a2 (svn r5325) Don't test something if we already know the result true, because the same test was performed just a few lines before
tron
parents: 4000
diff changeset
   231
			wp->stat_id = p1;
9599
949374e83b78 (svn r9632) [NoAI] -Sync with trunk r9574:9631.
rubidium
parents: 9574
diff changeset
   232
			wp->grfid = statspec->grffile->grfid;
3676
48444d881fbe (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   233
			wp->localidx = statspec->localidx;
2670
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   234
		} else {
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   235
			/* Specified custom graphics do not exist, so use default. */
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
   236
			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
   237
			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
   238
			wp->localidx = 0;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   239
		}
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
   240
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   241
		wp->deleted = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   242
		wp->build_date = _date;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   243
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3977
diff changeset
   244
		if (wp->town_index == 0) MakeDefaultWaypointName(wp);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   245
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   246
		UpdateWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   247
		RedrawWaypointSign(wp);
5348
0f05b1348b5a (svn r7519) -Fix: [YAPF] Cache was not deleted when waypoint was built or removed (frosch)
KUDr
parents: 5216
diff changeset
   248
		YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   249
		wp_auto_delete.Detach();
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   250
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   251
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9718
diff changeset
   252
	return CommandCost(EXPENSES_CONSTRUCTION, _price.build_train_depot);
1542
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
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   255
/**
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   256
 * Daily loop for waypoints
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   257
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
   258
void WaypointsDailyLoop()
1542
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
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   261
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   262
	/* 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
   263
	FOR_ALL_WAYPOINTS(wp) {
4500
426d0baa1be5 (svn r6285) -Fix r6143: of course deleting a waypoint should call the Delete wrapper, not the Destroy function (/me slaps hisself)
truelight
parents: 4395
diff changeset
   264
		if (wp->deleted != 0 && --wp->deleted == 0) DeleteWaypoint(wp);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   265
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   266
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   267
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   268
/**
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   269
 * Remove a waypoint
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   270
 * @param tile from which to remove waypoint
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   271
 * @param flags type of operation
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   272
 * @param justremove will indicate if it is removed from rail or if rails are removed too
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   273
 * @return cost of operation or error
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   274
 */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9599
diff changeset
   275
CommandCost 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
   276
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   277
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   278
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   279
	/* Make sure it's a waypoint */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   280
	if (!IsTileType(tile, MP_RAILWAY) ||
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   281
			!IsRailWaypoint(tile) ||
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   282
			(!CheckTileOwnership(tile) && _current_player != OWNER_WATER) ||
9718
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9701
diff changeset
   283
			!EnsureNoVehicleOnGround(tile)) {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   284
		return CMD_ERROR;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   285
	}
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   286
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   287
	if (flags & DC_EXEC) {
5348
0f05b1348b5a (svn r7519) -Fix: [YAPF] Cache was not deleted when waypoint was built or removed (frosch)
KUDr
parents: 5216
diff changeset
   288
		Track track = GetRailWaypointTrack(tile);
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   289
		Owner owner = GetTileOwner(tile); // cannot use _current_player because of possible floods
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   290
		wp = GetWaypointByTile(tile);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   291
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   292
		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
   293
		RedrawWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   294
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   295
		if (justremove) {
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3157
diff changeset
   296
			MakeRailNormal(tile, GetTileOwner(tile), GetRailWaypointBits(tile), GetRailType(tile));
3101
e2fdb8802c2f (svn r3696) Add functions to turn a tile into a normal rail tile/depot/waypoint. This is just a tiny step, the rail code needs way more love and caring
tron
parents: 3047
diff changeset
   297
			MarkTileDirtyByTile(tile);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   298
		} else {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   299
			DoClearSquare(tile);
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   300
			AddTrackToSignalBuffer(tile, track, owner);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   301
		}
5348
0f05b1348b5a (svn r7519) -Fix: [YAPF] Cache was not deleted when waypoint was built or removed (frosch)
KUDr
parents: 5216
diff changeset
   302
		YapfNotifyTrackLayoutChange(tile, track);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   303
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   304
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9718
diff changeset
   305
	return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_train_depot);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   306
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   307
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   308
/**
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   309
 * Delete a waypoint
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3448
diff changeset
   310
 * @param tile tile where waypoint is to be deleted
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   311
 * @param flags type of operation
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   312
 * @param p1 unused
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   313
 * @param p2 unused
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   314
 * @return cost of operation or error
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   315
 */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9599
diff changeset
   316
CommandCost CmdRemoveTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   317
{
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
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   321
static bool IsUniqueWaypointName(const char *name)
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   322
{
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   323
	const Waypoint *wp;
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   324
	char buf[512];
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   325
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   326
	FOR_ALL_WAYPOINTS(wp) {
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   327
		SetDParam(0, wp->index);
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   328
		GetString(buf, STR_WAYPOINT_RAW, lastof(buf));
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   329
		if (strcmp(buf, name) == 0) return false;
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   330
	}
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   331
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   332
	return true;
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   333
}
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   334
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   335
/**
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   336
 * Rename a waypoint.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3448
diff changeset
   337
 * @param tile unused
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   338
 * @param flags type of operation
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   339
 * @param p1 id of waypoint
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   340
 * @param p2 unused
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   341
 * @return cost of operation or error
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   342
 */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9599
diff changeset
   343
CommandCost CmdRenameWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   344
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   345
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   346
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   347
	if (!IsValidWaypointID(p1)) return CMD_ERROR;
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   348
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   349
	wp = GetWaypoint(p1);
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   350
	if (!CheckTileOwnership(wp->xy)) return CMD_ERROR;
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   351
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   352
	if (!StrEmpty(_cmd_text)) {
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   353
		if (!IsUniqueWaypointName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   354
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   355
		if (flags & DC_EXEC) {
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   356
			free(wp->name);
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   357
			wp->name = strdup(_cmd_text);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   358
			wp->town_cn = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   359
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   360
			UpdateWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   361
			MarkWholeScreenDirty();
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   362
		}
2026
02dfa0aa2c2f (svn r2535) Tabs
tron
parents: 1980
diff changeset
   363
	} else {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   364
		if (flags & DC_EXEC) {
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   365
			free(wp->name);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   366
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   367
			MakeDefaultWaypointName(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   368
			UpdateWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   369
			MarkWholeScreenDirty();
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
	}
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9599
diff changeset
   372
	return CommandCost();
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   373
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   374
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   375
/**
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   376
 * This hacks together some dummy one-shot Station structure for a waypoint.
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   377
 * @param tile on which to work
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   378
 * @return pointer to a Station
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   379
 */
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
   380
Station *ComposeWaypointStation(TileIndex tile)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   381
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   382
	Waypoint *wp = GetWaypointByTile(tile);
5985
e7a55ca9e43d (svn r8277) -Fix (r8038): assert on game exit when waypoints were used. The static variable of type Station (inside ComposeWaypointStation) replaced by byte array so no destructor is called for it on exit.
KUDr
parents: 5838
diff changeset
   383
e7a55ca9e43d (svn r8277) -Fix (r8038): assert on game exit when waypoints were used. The static variable of type Station (inside ComposeWaypointStation) replaced by byte array so no destructor is called for it on exit.
KUDr
parents: 5838
diff changeset
   384
	/* instead of 'static Station stat' use byte array to avoid Station's destructor call upon exit. As
e7a55ca9e43d (svn r8277) -Fix (r8038): assert on game exit when waypoints were used. The static variable of type Station (inside ComposeWaypointStation) replaced by byte array so no destructor is called for it on exit.
KUDr
parents: 5838
diff changeset
   385
	 * a side effect, the station is not constructed now. */
5990
b3c4c4b66249 (svn r8284) -Fix (r8277): g++ compilation error: missing brackets in sizeof
KUDr
parents: 5987
diff changeset
   386
	static byte stat_raw[sizeof(Station)];
5985
e7a55ca9e43d (svn r8277) -Fix (r8038): assert on game exit when waypoints were used. The static variable of type Station (inside ComposeWaypointStation) replaced by byte array so no destructor is called for it on exit.
KUDr
parents: 5838
diff changeset
   387
	static Station &stat = *(Station*)stat_raw;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   388
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   389
	stat.train_tile = stat.xy = wp->xy;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   390
	stat.town = GetTown(wp->town_index);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   391
	stat.build_date = wp->build_date;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   392
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   393
	return &stat;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   394
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   395
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   396
/**
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   397
 * Draw a waypoint
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   398
 * @param x coordinate
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   399
 * @param y coordinate
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   400
 * @param stat_id station id
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   401
 * @param railtype RailType to use for
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   402
 */
2520
75eb66d6e371 (svn r3049) Replace byte/int/uint by RailType where appropriate
tron
parents: 2261
diff changeset
   403
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
   404
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   405
	x += 33;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   406
	y += 17;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   407
3764
ebcaf412fb3a (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3751
diff changeset
   408
	if (!DrawStationTile(x, y, railtype, AXIS_X, STAT_CLASS_WAYP, stat_id)) {
2261
3f78323707bb (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2254
diff changeset
   409
		DrawDefaultWaypointSprite(x, y, railtype);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   410
	}
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
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   413
Waypoint::Waypoint(TileIndex tile)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   414
{
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   415
	this->xy = tile;
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   416
}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   417
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   418
Waypoint::~Waypoint()
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   419
{
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   420
	free(this->name);
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   421
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   422
	if (CleaningPool()) return;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   423
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   424
	RemoveOrderFromAllVehicles(OT_GOTO_WAYPOINT, this->index);
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   425
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   426
	RedrawWaypointSign(this);
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   427
	this->xy = 0;
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   428
}
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   429
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   430
/**
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   431
 * Fix savegames which stored waypoints in their old format
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 6573
diff changeset
   432
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
   433
void FixOldWaypoints()
1542
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
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   436
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   437
	/* 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
   438
	FOR_ALL_WAYPOINTS(wp) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   439
		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
   440
		wp->town_cn = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   441
		if (wp->string & 0xC000) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   442
			wp->town_cn = wp->string & 0x3F;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   443
			wp->string = STR_NULL;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   444
		}
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
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   447
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
   448
void InitializeWaypoints()
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   449
{
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   450
	_Waypoint_pool.CleanPool();
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   451
	_Waypoint_pool.AddBlockToPool();
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   452
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   453
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
   454
static const SaveLoad _waypoint_desc[] = {
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   455
	SLE_CONDVAR(Waypoint, xy,         SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   456
	SLE_CONDVAR(Waypoint, xy,         SLE_UINT32,                  6, SL_MAX_VERSION),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   457
	SLE_CONDVAR(Waypoint, town_index, SLE_UINT16,                 12, SL_MAX_VERSION),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   458
	SLE_CONDVAR(Waypoint, town_cn,    SLE_UINT8,                  12, SL_MAX_VERSION),
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   459
	SLE_CONDVAR(Waypoint, string,     SLE_STRINGID,                0, 83),
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   460
	SLE_CONDSTR(Waypoint, name,       SLE_STR, 0,                 84, SL_MAX_VERSION),
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   461
	    SLE_VAR(Waypoint, deleted,    SLE_UINT8),
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   462
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   463
	SLE_CONDVAR(Waypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32,  3, 30),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   464
	SLE_CONDVAR(Waypoint, build_date, SLE_INT32,                  31, SL_MAX_VERSION),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   465
	SLE_CONDVAR(Waypoint, localidx,   SLE_UINT8,                   3, SL_MAX_VERSION),
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   466
	SLE_CONDVAR(Waypoint, grfid,      SLE_UINT32,                 17, SL_MAX_VERSION),
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   467
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   468
	SLE_END()
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   469
};
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   470
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
   471
static void Save_WAYP()
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   472
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   473
	Waypoint *wp;
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
	FOR_ALL_WAYPOINTS(wp) {
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   476
		SlSetArrayIndex(wp->index);
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   477
		SlObject(wp, _waypoint_desc);
1542
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
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   480
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
   481
static void Load_WAYP()
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   482
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   483
	int index;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   484
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   485
	while ((index = SlIterateArray()) != -1) {
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9631
diff changeset
   486
		Waypoint *wp = new (index) Waypoint();
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   487
		SlObject(wp, _waypoint_desc);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   488
	}
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
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   491
extern const ChunkHandler _waypoint_chunk_handlers[] = {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   492
	{ '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
   493
};