src/waypoint.cpp
author convert-repo
Mon, 07 Apr 2008 16:21:55 +0000
changeset 10076 dfd70e42c4ae
parent 9343 c30fd350dd8c
child 10230 bf81856a1101
permissions -rw-r--r--
update tags
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
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
     3
/** @file waypoint.cpp */
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
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
8612
6414fc21c2f3 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8610
diff changeset
     8
#include "command_func.h"
6949
72d11a1e1e60 (svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents: 6947
diff changeset
     9
#include "landscape.h"
9280
9c03416d26b1 (svn r12488) -Codechange: split order.h into order_base.h and order_func.h.
rubidium
parents: 9075
diff changeset
    10
#include "order_func.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"
8634
5ffca02f9115 (svn r11700) -Codechange: reduce the amount of unnecessary includes.
rubidium
parents: 8627
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"
9281
d8cd9ac52a68 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 9280
diff changeset
    15
#include "station_base.h"
1542
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"
6947
c21859ab4493 (svn r9601) -Codechange: Store grf file reference in station spec, not just GRF ID
peter1138
parents: 6928
diff changeset
    20
#include "newgrf.h"
7877
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
    21
#include "misc/autoptr.hpp"
8610
17cc343a23dd (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8604
diff changeset
    22
#include "strings_func.h"
8721
b7971ad9bdd9 (svn r11788) -Fix (11787): makedepend doesn't mark delete files as changed...
rubidium
parents: 8710
diff changeset
    23
#include "gfx_func.h"
8627
448ebf3a8291 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8617
diff changeset
    24
#include "functions.h"
448ebf3a8291 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8617
diff changeset
    25
#include "window_func.h"
8634
5ffca02f9115 (svn r11700) -Codechange: reduce the amount of unnecessary includes.
rubidium
parents: 8627
diff changeset
    26
#include "economy_func.h"
8636
2b158acb649c (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8635
diff changeset
    27
#include "date_func.h"
8640
1e93b81e96d2 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8636
diff changeset
    28
#include "vehicle_func.h"
1e93b81e96d2 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8636
diff changeset
    29
#include "vehicle_base.h"
8710
52015340050c (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents: 8640
diff changeset
    30
#include "string_func.h"
8734
b7fc8c6fd379 (svn r11802) -Fix [FS#716]: do not crash trains when leaving depot to a very long track
smatz
parents: 8733
diff changeset
    31
#include "signal_func.h"
8750
fdd6054e7bae (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8734
diff changeset
    32
#include "player_func.h"
8766
c86cfa3a7580 (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8760
diff changeset
    33
#include "settings_type.h"
9283
624272490f5a (svn r12495) -Codechange: reduce the dependency on newgrf_station.h (especially because newgrf_station.h includes a lot of stuff).
rubidium
parents: 9281
diff changeset
    34
#include "newgrf_station.h"
9343
c30fd350dd8c (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents: 9332
diff changeset
    35
#include "oldpool_func.h"
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    36
8760
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8754
diff changeset
    37
#include "table/strings.h"
8734
b7fc8c6fd379 (svn r11802) -Fix [FS#716]: do not crash trains when leaving depot to a very long track
smatz
parents: 8733
diff changeset
    38
7877
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
    39
DEFINE_OLD_POOL_GENERIC(Waypoint, Waypoint)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    40
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    41
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    42
/**
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    43
 * Update the sign for the waypoint
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    44
 * @param wp Waypoint to update sign */
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2752
diff changeset
    45
static void UpdateWaypointSign(Waypoint* wp)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    46
{
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
    47
	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
    48
	SetDParam(0, wp->index);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    49
	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
    50
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    51
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    52
/**
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    53
 * Redraw the sign of a waypoint
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    54
 * @param wp Waypoint to redraw sign */
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2670
diff changeset
    55
static void RedrawWaypointSign(const Waypoint* wp)
1542
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
	MarkAllViewportsDirty(
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    58
		wp->sign.left - 6,
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    59
		wp->sign.top,
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    60
		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
    61
		wp->sign.top + 48);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    62
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    63
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    64
/**
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    65
 * Update all signs
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    66
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
    67
void UpdateAllWaypointSigns()
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    68
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    69
	Waypoint *wp;
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
	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
    72
		UpdateWaypointSign(wp);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    73
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    74
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    75
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    76
/**
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    77
 * Set the default name for a waypoint
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    78
 * @param wp Waypoint to work on
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
    79
 */
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2670
diff changeset
    80
static void MakeDefaultWaypointName(Waypoint* wp)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    81
{
9075
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    82
	uint32 used = 0; // bitmap of used waypoint numbers, sliding window with 'next' as base
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    83
	uint32 next = 0; // first waypoint number in the bitmap
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    84
	WaypointID idx = 0; // index where we will stop
1542
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
	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
    87
9075
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    88
	/* Find first unused waypoint number belonging to this town. This can never fail,
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    89
	 * as long as there can be at most 65535 waypoints in total.
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    90
	 *
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    91
	 * This does 'n * m' search, but with 32bit 'used' bitmap, it needs at most 'n * (1 + ceil(m / 32))'
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    92
	 * steps (n - number of waypoints in pool, m - number of waypoints near this town).
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    93
	 * Usually, it needs only 'n' steps.
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    94
	 *
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    95
	 * If it wasn't using 'used' and 'idx', it would just search for increasing 'next',
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    96
	 * but this way it is faster */
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
    97
9075
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    98
	WaypointID cid = 0; // current index, goes to GetWaypointPoolSize()-1, then wraps to 0
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
    99
	do {
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   100
		Waypoint *lwp = GetWaypoint(cid);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   101
9075
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   102
		/* check only valid waypoints... */
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   103
 		if (lwp->IsValid() && wp != lwp) {
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   104
			/* only waypoints with 'generic' name within the same city */
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   105
			if (lwp->name == NULL && lwp->town_index == wp->town_index) {
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   106
				/* if lwp->town_cn < next, uint will overflow to '+inf' */
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   107
				uint i = (uint)lwp->town_cn - next;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   108
9075
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   109
				if (i < 32) {
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   110
					SetBit(used, i); // update bitmap
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   111
					if (i == 0) {
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   112
						/* shift bitmap while the lowest bit is '1';
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   113
						 * increase the base of the bitmap too */
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   114
						do {
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   115
							used >>= 1;
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   116
							next++;
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   117
						} while (HasBit(used, 0));
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   118
						/* when we are at 'idx' again at end of the loop and
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   119
						 * 'next' hasn't changed, then no waypoint had town_cn == next,
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   120
						 * so we can safely use it */
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   121
						idx = cid;
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   122
					}
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   123
				}
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   124
			}
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   125
		}
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   126
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   127
		cid++;
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   128
		if (cid == GetWaypointPoolSize()) cid = 0; // wrap to zero...
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   129
	} while (cid != idx);
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   130
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   131
	wp->town_cn = (uint16)next; // set index...
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   132
	wp->name = NULL; // ... and use generic name
1542
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
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   135
/**
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   136
 * Find a deleted waypoint close to a tile.
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   137
 * @param tile to search from
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   138
 */
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
   139
static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   140
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   141
	Waypoint *wp, *best = NULL;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   142
	uint thres = 8;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   143
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   144
	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
   145
		if (wp->deleted) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   146
			uint cur_dist = DistanceManhattan(tile, wp->xy);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   147
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   148
			if (cur_dist < thres) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   149
				thres = cur_dist;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   150
				best = wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   151
			}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   152
		}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   153
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   154
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   155
	return best;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   156
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   157
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
   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
 * 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
   160
 * 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
   161
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
   162
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
   163
{
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
   164
	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
   165
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   166
	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
   167
		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
   168
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   169
		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
   170
34970983ef08 (svn r3212) - Waypoints/Depots: Store waypoint index in m2 for waypoints. This moves the ground type bits from m2 to m4 for waypoints and depots (leaving room for depot index in m2 in future), and moves the custom graphics ID to the waypoint struct.
peter1138
parents: 2668
diff changeset
   171
		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
   172
			const StationSpec *statspec = GetCustomStationSpec(STAT_CLASS_WAYP, i);
6947
c21859ab4493 (svn r9601) -Codechange: Store grf file reference in station spec, not just GRF ID
peter1138
parents: 6928
diff changeset
   173
			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
   174
				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
   175
				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
   176
			}
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
   177
		}
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
   178
	}
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
   179
}
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
   180
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   181
/** 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
   182
 * 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
   183
 * @param tile tile where waypoint will be built
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   184
 * @param flags type of operation
2625
66b3d632dcd2 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents: 2624
diff changeset
   185
 * @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
   186
 * @param p2 unused
2085
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   187
 *
ae9e92ffe168 (svn r2595) -Codechange: Introduced "IsSteepTileh" to find whether a tile is steep
celestar
parents: 2049
diff changeset
   188
 * @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
   189
 * 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
   190
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6949
diff changeset
   191
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
   192
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   193
	Waypoint *wp;
7877
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   194
	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
   195
	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
   196
	Axis axis;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   197
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   198
	/* 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
   199
	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
   200
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
   201
	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
   202
			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
   203
				(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
   204
				(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
   205
			)) {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   206
		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
   207
	}
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   208
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   209
	if (!CheckTileOwnership(tile)) return CMD_ERROR;
8254
5de2bbd26163 (svn r11303) -Fix: EnsureNoVehicle and EnsureNoVehicleOnGround were both used to check whether there was no vehicle on the ground, except that the former didn't take care of aircraft shadows. So now we only use EnsureNoVehicleOnGround.
rubidium
parents: 8200
diff changeset
   210
	if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   211
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   212
	tileh = GetTileSlope(tile, NULL);
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   213
	if (tileh != SLOPE_FLAT &&
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   214
			(!_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
   215
		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
   216
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   217
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5350
diff changeset
   218
	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
   219
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   220
	/* 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
   221
	wp = FindDeletedWaypointCloseTo(tile);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   222
	if (wp == NULL) {
7877
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   223
		wp = new Waypoint(tile);
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   224
		if (wp == NULL) return CMD_ERROR;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   225
7877
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   226
		wp_auto_delete = wp;
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   227
8997
85ddd26ec9c6 (svn r12076) -Fix: when reusing a renamed deleted waypoint, keep the new name
glx
parents: 8802
diff changeset
   228
		wp->town_index = INVALID_TOWN;
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
   229
		wp->name = NULL;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   230
		wp->town_cn = 0;
7964
46224d0b4752 (svn r10973) -Fix [FS#1154]: update wp->xy when waypoint is moved (Catalan)
KUDr
parents: 7909
diff changeset
   231
	} else if (flags & DC_EXEC) {
8200
74d44b461346 (svn r11238) -Fix [FS#1316]: the orders of trains going to a waypoint where not updated when the waypoint would be moved.
rubidium
parents: 7992
diff changeset
   232
		/* Move existing (recently deleted) waypoint to the new location */
74d44b461346 (svn r11238) -Fix [FS#1316]: the orders of trains going to a waypoint where not updated when the waypoint would be moved.
rubidium
parents: 7992
diff changeset
   233
74d44b461346 (svn r11238) -Fix [FS#1316]: the orders of trains going to a waypoint where not updated when the waypoint would be moved.
rubidium
parents: 7992
diff changeset
   234
		/* First we update the destination for all vehicles that
74d44b461346 (svn r11238) -Fix [FS#1316]: the orders of trains going to a waypoint where not updated when the waypoint would be moved.
rubidium
parents: 7992
diff changeset
   235
		 * have the old waypoint in their orders. */
74d44b461346 (svn r11238) -Fix [FS#1316]: the orders of trains going to a waypoint where not updated when the waypoint would be moved.
rubidium
parents: 7992
diff changeset
   236
		Vehicle *v;
74d44b461346 (svn r11238) -Fix [FS#1316]: the orders of trains going to a waypoint where not updated when the waypoint would be moved.
rubidium
parents: 7992
diff changeset
   237
		FOR_ALL_VEHICLES(v) {
74d44b461346 (svn r11238) -Fix [FS#1316]: the orders of trains going to a waypoint where not updated when the waypoint would be moved.
rubidium
parents: 7992
diff changeset
   238
			if (v->type == VEH_TRAIN &&
74d44b461346 (svn r11238) -Fix [FS#1316]: the orders of trains going to a waypoint where not updated when the waypoint would be moved.
rubidium
parents: 7992
diff changeset
   239
					v->First() == v &&
9332
2e120d0bd632 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 9283
diff changeset
   240
					v->current_order.IsType(OT_GOTO_WAYPOINT) &&
8200
74d44b461346 (svn r11238) -Fix [FS#1316]: the orders of trains going to a waypoint where not updated when the waypoint would be moved.
rubidium
parents: 7992
diff changeset
   241
					v->dest_tile == wp->xy) {
74d44b461346 (svn r11238) -Fix [FS#1316]: the orders of trains going to a waypoint where not updated when the waypoint would be moved.
rubidium
parents: 7992
diff changeset
   242
				v->dest_tile = tile;
74d44b461346 (svn r11238) -Fix [FS#1316]: the orders of trains going to a waypoint where not updated when the waypoint would be moved.
rubidium
parents: 7992
diff changeset
   243
			}
74d44b461346 (svn r11238) -Fix [FS#1316]: the orders of trains going to a waypoint where not updated when the waypoint would be moved.
rubidium
parents: 7992
diff changeset
   244
		}
74d44b461346 (svn r11238) -Fix [FS#1316]: the orders of trains going to a waypoint where not updated when the waypoint would be moved.
rubidium
parents: 7992
diff changeset
   245
7964
46224d0b4752 (svn r10973) -Fix [FS#1154]: update wp->xy when waypoint is moved (Catalan)
KUDr
parents: 7909
diff changeset
   246
		RedrawWaypointSign(wp);
46224d0b4752 (svn r10973) -Fix [FS#1154]: update wp->xy when waypoint is moved (Catalan)
KUDr
parents: 7909
diff changeset
   247
		wp->xy = tile;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   248
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   249
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   250
	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
   251
		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
   252
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3157
diff changeset
   253
		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
   254
		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
   255
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
   256
		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
   257
3676
48444d881fbe (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   258
		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
   259
			wp->stat_id = p1;
6947
c21859ab4493 (svn r9601) -Codechange: Store grf file reference in station spec, not just GRF ID
peter1138
parents: 6928
diff changeset
   260
			wp->grfid = statspec->grffile->grfid;
3676
48444d881fbe (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3636
diff changeset
   261
			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
   262
		} else {
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   263
			/* 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
   264
			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
   265
			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
   266
			wp->localidx = 0;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   267
		}
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
   268
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   269
		wp->deleted = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   270
		wp->build_date = _date;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   271
8997
85ddd26ec9c6 (svn r12076) -Fix: when reusing a renamed deleted waypoint, keep the new name
glx
parents: 8802
diff changeset
   272
		if (wp->town_index == INVALID_TOWN) MakeDefaultWaypointName(wp);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   273
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   274
		UpdateWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   275
		RedrawWaypointSign(wp);
5348
0f05b1348b5a (svn r7519) -Fix: [YAPF] Cache was not deleted when waypoint was built or removed (frosch)
KUDr
parents: 5216
diff changeset
   276
		YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
7877
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   277
		wp_auto_delete.Detach();
1542
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
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8721
diff changeset
   280
	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
   281
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   282
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   283
/**
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   284
 * Daily loop for waypoints
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   285
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
   286
void WaypointsDailyLoop()
1542
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
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   289
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   290
	/* 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
   291
	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
   292
		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
   293
	}
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
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   296
/**
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   297
 * Remove a waypoint
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   298
 * @param tile from which to remove waypoint
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   299
 * @param flags type of operation
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   300
 * @param justremove will indicate if it is removed from rail or if rails are removed too
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   301
 * @return cost of operation or error
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   302
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6949
diff changeset
   303
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
   304
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   305
	Waypoint *wp;
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
	/* Make sure it's a waypoint */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   308
	if (!IsTileType(tile, MP_RAILWAY) ||
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   309
			!IsRailWaypoint(tile) ||
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   310
			(!CheckTileOwnership(tile) && _current_player != OWNER_WATER) ||
8254
5de2bbd26163 (svn r11303) -Fix: EnsureNoVehicle and EnsureNoVehicleOnGround were both used to check whether there was no vehicle on the ground, except that the former didn't take care of aircraft shadows. So now we only use EnsureNoVehicleOnGround.
rubidium
parents: 8200
diff changeset
   311
			!EnsureNoVehicleOnGround(tile)) {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   312
		return CMD_ERROR;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4052
diff changeset
   313
	}
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   314
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   315
	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
   316
		Track track = GetRailWaypointTrack(tile);
8796
81b0d6b54a1f (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz
parents: 8766
diff changeset
   317
		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
   318
		wp = GetWaypointByTile(tile);
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
		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
   321
		RedrawWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   322
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   323
		if (justremove) {
3242
1cefa03f0d5e (svn r3916) Get/Set the rail type by [GS]etRailType{Crossing,OnBridge,}()
tron
parents: 3157
diff changeset
   324
			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
   325
			MarkTileDirtyByTile(tile);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   326
		} else {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   327
			DoClearSquare(tile);
8802
e08824cdab46 (svn r11871) -Fix [FS#1074]: do not update signals after each tile when building/removing a large block of track/signals/station
smatz
parents: 8796
diff changeset
   328
			AddTrackToSignalBuffer(tile, track, owner);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   329
		}
5348
0f05b1348b5a (svn r7519) -Fix: [YAPF] Cache was not deleted when waypoint was built or removed (frosch)
KUDr
parents: 5216
diff changeset
   330
		YapfNotifyTrackLayoutChange(tile, track);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   331
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   332
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8721
diff changeset
   333
	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
   334
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   335
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   336
/**
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   337
 * 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
   338
 * @param tile tile where waypoint is to be deleted
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   339
 * @param flags type of operation
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   340
 * @param p1 unused
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   341
 * @param p2 unused
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   342
 * @return cost of operation or error
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   343
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6949
diff changeset
   344
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
   345
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   346
	return RemoveTrainWaypoint(tile, flags, true);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   347
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   348
7593
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   349
static bool IsUniqueWaypointName(const char *name)
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   350
{
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   351
	const Waypoint *wp;
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   352
	char buf[512];
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   353
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   354
	FOR_ALL_WAYPOINTS(wp) {
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   355
		SetDParam(0, wp->index);
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   356
		GetString(buf, STR_WAYPOINT_RAW, lastof(buf));
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   357
		if (strcmp(buf, name) == 0) return false;
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   358
	}
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   359
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   360
	return true;
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   361
}
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   362
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   363
/**
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   364
 * 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
   365
 * @param tile unused
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   366
 * @param flags type of operation
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   367
 * @param p1 id of waypoint
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   368
 * @param p2 unused
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   369
 * @return cost of operation or error
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   370
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6949
diff changeset
   371
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
   372
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   373
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   374
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   375
	if (!IsValidWaypointID(p1)) return CMD_ERROR;
1782
0ac16ff36293 (svn r2286) - CodeChange: paramcheck the next batch of commands.
Darkvater
parents: 1542
diff changeset
   376
7597
73cfa9943246 (svn r10368) -Fix: when renaming waypoints, ownership was only checked client-side, not in the command.
peter1138
parents: 7593
diff changeset
   377
	wp = GetWaypoint(p1);
73cfa9943246 (svn r10368) -Fix: when renaming waypoints, ownership was only checked client-side, not in the command.
peter1138
parents: 7593
diff changeset
   378
	if (!CheckTileOwnership(wp->xy)) return CMD_ERROR;
73cfa9943246 (svn r10368) -Fix: when renaming waypoints, ownership was only checked client-side, not in the command.
peter1138
parents: 7593
diff changeset
   379
7593
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   380
	if (!StrEmpty(_cmd_text)) {
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   381
		if (!IsUniqueWaypointName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
cfce84f8995c (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7446
diff changeset
   382
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   383
		if (flags & DC_EXEC) {
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
   384
			free(wp->name);
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
   385
			wp->name = strdup(_cmd_text);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   386
			wp->town_cn = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   387
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   388
			UpdateWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   389
			MarkWholeScreenDirty();
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   390
		}
2026
02dfa0aa2c2f (svn r2535) Tabs
tron
parents: 1980
diff changeset
   391
	} else {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   392
		if (flags & DC_EXEC) {
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
   393
			free(wp->name);
1542
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
			MakeDefaultWaypointName(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   396
			UpdateWaypointSign(wp);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   397
			MarkWholeScreenDirty();
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   398
		}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   399
	}
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   400
	return CommandCost();
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   401
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   402
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   403
/**
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   404
 * This hacks together some dummy one-shot Station structure for a waypoint.
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   405
 * @param tile on which to work
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   406
 * @return pointer to a Station
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   407
 */
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
   408
Station *ComposeWaypointStation(TileIndex tile)
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   409
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   410
	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
   411
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
   412
	/* 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
   413
	 * 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
   414
	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
   415
	static Station &stat = *(Station*)stat_raw;
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   416
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   417
	stat.train_tile = stat.xy = wp->xy;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   418
	stat.town = GetTown(wp->town_index);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   419
	stat.build_date = wp->build_date;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   420
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   421
	return &stat;
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
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   424
/**
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   425
 * Draw a waypoint
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   426
 * @param x coordinate
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   427
 * @param y coordinate
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   428
 * @param stat_id station id
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   429
 * @param railtype RailType to use for
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   430
 */
2520
75eb66d6e371 (svn r3049) Replace byte/int/uint by RailType where appropriate
tron
parents: 2261
diff changeset
   431
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
   432
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   433
	x += 33;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   434
	y += 17;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   435
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
   436
	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
   437
		DrawDefaultWaypointSprite(x, y, railtype);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   438
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   439
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   440
7877
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   441
Waypoint::Waypoint(TileIndex tile)
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   442
{
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   443
	this->xy = tile;
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   444
}
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   445
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   446
Waypoint::~Waypoint()
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   447
{
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
   448
	free(this->name);
7909
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7877
diff changeset
   449
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7877
diff changeset
   450
	if (CleaningPool()) return;
8df54a2839a1 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7877
diff changeset
   451
7877
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   452
	RemoveOrderFromAllVehicles(OT_GOTO_WAYPOINT, this->index);
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   453
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   454
	RedrawWaypointSign(this);
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   455
	this->xy = 0;
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   456
}
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   457
6928
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   458
/**
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   459
 * Fix savegames which stored waypoints in their old format
44797333bcbf (svn r9568) -Documentation: doxygen and comment changes: Root of src is finally done. Now, time to start clearing as much as possible
belugas
parents: 6573
diff changeset
   460
 */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
   461
void FixOldWaypoints()
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   462
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   463
	Waypoint *wp;
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
	/* 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
   466
	FOR_ALL_WAYPOINTS(wp) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   467
		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
   468
		wp->town_cn = 0;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   469
		if (wp->string & 0xC000) {
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   470
			wp->town_cn = wp->string & 0x3F;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   471
			wp->string = STR_NULL;
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
	}
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
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
   476
void InitializeWaypoints()
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   477
{
7877
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   478
	_Waypoint_pool.CleanPool();
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   479
	_Waypoint_pool.AddBlockToPool();
1542
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
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
   482
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
   483
	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
   484
	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
   485
	SLE_CONDVAR(Waypoint, town_index, SLE_UINT16,                 12, SL_MAX_VERSION),
9075
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   486
	SLE_CONDVAR(Waypoint, town_cn,    SLE_FILE_U8 | SLE_VAR_U16,  12, 88),
ce14a76881b1 (svn r12160) -Fix [FS#1744]: remove the arbitrary limit of 64 waypoints per town, so weird things won't happen anymore
smatz
parents: 8997
diff changeset
   487
	SLE_CONDVAR(Waypoint, town_cn,    SLE_UINT16,                 89, SL_MAX_VERSION),
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
   488
	SLE_CONDVAR(Waypoint, string,     SLE_STRINGID,                0, 83),
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
   489
	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
   490
	    SLE_VAR(Waypoint, deleted,    SLE_UINT8),
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   491
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
   492
	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
   493
	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
   494
	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
   495
	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
   496
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   497
	SLE_END()
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   498
};
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   499
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
   500
static void Save_WAYP()
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   501
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   502
	Waypoint *wp;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   503
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   504
	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
   505
		SlSetArrayIndex(wp->index);
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   506
		SlObject(wp, _waypoint_desc);
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   507
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   508
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   509
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5990
diff changeset
   510
static void Load_WAYP()
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   511
{
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   512
	int index;
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   513
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   514
	while ((index = SlIterateArray()) != -1) {
7877
eca84d5e568a (svn r10750) -Codechange: make the waypoint struct use the new poolitem class as super class.
rubidium
parents: 7597
diff changeset
   515
		Waypoint *wp = new (index) Waypoint();
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   516
		SlObject(wp, _waypoint_desc);
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   517
	}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   518
}
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   519
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   520
extern const ChunkHandler _waypoint_chunk_handlers[] = {
1542
2ca6d1624e6d (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
diff changeset
   521
	{ '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
   522
};