newgrf_station.c
author Darkvater
Tue, 26 Dec 2006 17:36:18 +0000
changeset 5380 8ea58542b6e0
parent 5172 285d1ab82403
child 5409 725ef180e977
permissions -rw-r--r--
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
use in debug.h. grfmsg() is now used as a specific debug-function for grf.
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
     1
/* $Id$ */
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
     2
2963
f28ce0549513 (svn r3525) - Rename station_newgrf.[ch] to newgrf_station.[ch], and update project files.
peter1138
parents: 2625
diff changeset
     3
/** @file newgrf_station.c Functions for dealing with station classes and custom stations. */
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
     4
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
     5
#include "stdafx.h"
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
     6
#include "openttd.h"
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
     7
#include "variables.h"
3764
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
     8
#include "functions.h"
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
     9
#include "debug.h"
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    10
#include "sprite.h"
3764
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
    11
#include "table/sprites.h"
3594
3c4e4feea80a (svn r4484) - Newstations: Use StringIDs instead of char*s to reference our custom names.
peter1138
parents: 3587
diff changeset
    12
#include "table/strings.h"
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
    13
#include "station.h"
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
    14
#include "station_map.h"
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5172
diff changeset
    15
#include "newgrf.h"
3752
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
    16
#include "newgrf_callbacks.h"
2963
f28ce0549513 (svn r3525) - Rename station_newgrf.[ch] to newgrf_station.[ch], and update project files.
peter1138
parents: 2625
diff changeset
    17
#include "newgrf_station.h"
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
    18
#include "newgrf_spritegroup.h"
4261
28670f743746 (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4206
diff changeset
    19
#include "date.h"
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    20
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    21
static StationClass station_classes[STAT_CLASS_MAX];
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    22
3786
ac7ff031ff5f (svn r4782) - Newstations: fix issue with saving/loading games with newstations on big endian architectures.
peter1138
parents: 3780
diff changeset
    23
enum {
ac7ff031ff5f (svn r4782) - Newstations: fix issue with saving/loading games with newstations on big endian architectures.
peter1138
parents: 3780
diff changeset
    24
	MAX_SPECLIST = 255,
ac7ff031ff5f (svn r4782) - Newstations: fix issue with saving/loading games with newstations on big endian architectures.
peter1138
parents: 3780
diff changeset
    25
};
ac7ff031ff5f (svn r4782) - Newstations: fix issue with saving/loading games with newstations on big endian architectures.
peter1138
parents: 3780
diff changeset
    26
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    27
/**
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    28
 * Reset station classes to their default state.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    29
 * This includes initialising the Default and Waypoint classes with an empty
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    30
 * entry, for standard stations and waypoints.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    31
 */
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    32
void ResetStationClasses(void)
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    33
{
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    34
	StationClassID i;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    35
	for (i = 0; i < STAT_CLASS_MAX; i++) {
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    36
		station_classes[i].id = 0;
3594
3c4e4feea80a (svn r4484) - Newstations: Use StringIDs instead of char*s to reference our custom names.
peter1138
parents: 3587
diff changeset
    37
		station_classes[i].name = STR_EMPTY;
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    38
		station_classes[i].stations = 0;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    39
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    40
		free(station_classes[i].spec);
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    41
		station_classes[i].spec = NULL;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    42
	}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    43
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    44
	// Set up initial data
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    45
	station_classes[0].id = 'DFLT';
3594
3c4e4feea80a (svn r4484) - Newstations: Use StringIDs instead of char*s to reference our custom names.
peter1138
parents: 3587
diff changeset
    46
	station_classes[0].name = STR_STAT_CLASS_DFLT;
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    47
	station_classes[0].stations = 1;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    48
	station_classes[0].spec = malloc(sizeof(*station_classes[0].spec));
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    49
	station_classes[0].spec[0] = NULL;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    50
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    51
	station_classes[1].id = 'WAYP';
3594
3c4e4feea80a (svn r4484) - Newstations: Use StringIDs instead of char*s to reference our custom names.
peter1138
parents: 3587
diff changeset
    52
	station_classes[1].name = STR_STAT_CLASS_WAYP;
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    53
	station_classes[1].stations = 1;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    54
	station_classes[1].spec = malloc(sizeof(*station_classes[1].spec));
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    55
	station_classes[1].spec[0] = NULL;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    56
}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    57
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    58
/**
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    59
 * Allocate a station class for the given class id.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    60
 * @param classid A 32 bit value identifying the class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    61
 * @return Index into station_classes of allocated class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    62
 */
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    63
StationClassID AllocateStationClass(uint32 class)
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    64
{
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    65
	StationClassID i;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    66
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    67
	for (i = 0; i < STAT_CLASS_MAX; i++) {
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    68
		if (station_classes[i].id == class) {
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    69
			// ClassID is already allocated, so reuse it.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    70
			return i;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    71
		} else if (station_classes[i].id == 0) {
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    72
			// This class is empty, so allocate it to the ClassID.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    73
			station_classes[i].id = class;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    74
			return i;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    75
		}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    76
	}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    77
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5172
diff changeset
    78
	grfmsg(2, "StationClassAllocate: already allocated %d classes, using default", STAT_CLASS_MAX);
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    79
	return STAT_CLASS_DFLT;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    80
}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    81
3642
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    82
/** Set the name of a custom station class */
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    83
void SetStationClassName(StationClassID sclass, StringID name)
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    84
{
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    85
	assert(sclass < STAT_CLASS_MAX);
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    86
	station_classes[sclass].name = name;
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    87
}
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    88
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    89
/** Retrieve the name of a custom station class */
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    90
StringID GetStationClassName(StationClassID sclass)
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    91
{
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    92
	assert(sclass < STAT_CLASS_MAX);
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    93
	return station_classes[sclass].name;
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    94
}
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    95
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    96
/** Build a list of station class name StringIDs to use in a dropdown list
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    97
 * @return Pointer to a (static) array of StringIDs
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    98
 */
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    99
StringID *BuildStationClassDropdown(void)
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   100
{
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   101
	/* Allow room for all station classes, plus a terminator entry */
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   102
	static StringID names[STAT_CLASS_MAX + 1];
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   103
	uint i;
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   104
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   105
	/* Add each name */
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   106
	for (i = 0; i < STAT_CLASS_MAX && station_classes[i].id != 0; i++) {
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   107
		names[i] = station_classes[i].name;
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   108
	}
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   109
	/* Terminate the list */
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   110
	names[i] = INVALID_STRING_ID;
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   111
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   112
	return names;
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   113
}
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   114
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   115
/**
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   116
 * Get the number of station classes in use.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   117
 * @return Number of station classes.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   118
 */
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   119
uint GetNumStationClasses(void)
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   120
{
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   121
	uint i;
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   122
	for (i = 0; i < STAT_CLASS_MAX && station_classes[i].id != 0; i++);
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   123
	return i;
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   124
}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   125
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   126
/**
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   127
 * Return the number of stations for the given station class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   128
 * @param sclass Index of the station class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   129
 * @return Number of stations in the class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   130
 */
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   131
uint GetNumCustomStations(StationClassID sclass)
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   132
{
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   133
	assert(sclass < STAT_CLASS_MAX);
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   134
	return station_classes[sclass].stations;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   135
}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   136
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   137
/**
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   138
 * Tie a station spec to its station class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   139
 * @param spec The station spec.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   140
 */
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3668
diff changeset
   141
void SetCustomStationSpec(StationSpec *statspec)
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   142
{
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   143
	StationClass *station_class;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   144
	int i;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   145
3780
4c785f28b1dd (svn r4772) - Newstations: when allocating a custom station (via action 0x03) check if the station has already been allocated. (Fixes duplicate waypoints in newstatsw in non-temperate climate)
peter1138
parents: 3775
diff changeset
   146
	/* If the station has already been allocated, don't reallocate it. */
4c785f28b1dd (svn r4772) - Newstations: when allocating a custom station (via action 0x03) check if the station has already been allocated. (Fixes duplicate waypoints in newstatsw in non-temperate climate)
peter1138
parents: 3775
diff changeset
   147
	if (statspec->allocated) return;
4c785f28b1dd (svn r4772) - Newstations: when allocating a custom station (via action 0x03) check if the station has already been allocated. (Fixes duplicate waypoints in newstatsw in non-temperate climate)
peter1138
parents: 3775
diff changeset
   148
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3668
diff changeset
   149
	assert(statspec->sclass < STAT_CLASS_MAX);
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3668
diff changeset
   150
	station_class = &station_classes[statspec->sclass];
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   151
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   152
	i = station_class->stations++;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   153
	station_class->spec = realloc(station_class->spec, station_class->stations * sizeof(*station_class->spec));
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   154
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3668
diff changeset
   155
	station_class->spec[i] = statspec;
3780
4c785f28b1dd (svn r4772) - Newstations: when allocating a custom station (via action 0x03) check if the station has already been allocated. (Fixes duplicate waypoints in newstatsw in non-temperate climate)
peter1138
parents: 3775
diff changeset
   156
	statspec->allocated = true;
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   157
}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   158
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   159
/**
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   160
 * Retrieve a station spec from a class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   161
 * @param sclass Index of the station class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   162
 * @param station The station index with the class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   163
 * @return The station spec.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   164
 */
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3668
diff changeset
   165
const StationSpec *GetCustomStationSpec(StationClassID sclass, uint station)
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   166
{
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   167
	assert(sclass < STAT_CLASS_MAX);
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   168
	if (station < station_classes[sclass].stations)
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   169
		return station_classes[sclass].spec[station];
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   170
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   171
	// If the custom station isn't defined any more, then the GRF file
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   172
	// probably was not loaded.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   173
	return NULL;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   174
}
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   175
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   176
3765
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   177
const StationSpec *GetCustomStationSpecByGrf(uint32 grfid, byte localidx)
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   178
{
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   179
	StationClassID i;
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   180
	uint j;
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   181
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   182
	for (i = STAT_CLASS_DFLT; i < STAT_CLASS_MAX; i++) {
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   183
		for (j = 0; j < station_classes[i].stations; j++) {
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   184
			const StationSpec *statspec = station_classes[i].spec[j];
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   185
			if (statspec == NULL) continue;
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   186
			if (statspec->grfid == grfid && statspec->localidx == localidx) return statspec;
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   187
		}
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   188
	}
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   189
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   190
	return NULL;
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   191
}
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   192
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   193
3756
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   194
/* Evaluate a tile's position within a station, and return the result a bitstuffed format.
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   195
 * if not centred: .TNLcCpP, if centred: .TNL..CP
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   196
 * T = Tile layout number (GetStationGfx), N = Number of platforms, L = Length of platforms
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   197
 * C = Current platform number from start, c = from end
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   198
 * P = Position along platform from start, p = from end
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   199
 * if centred, C/P start from the centre and c/p are not available.
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   200
 */
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   201
uint32 GetPlatformInfo(Axis axis, byte tile, int platforms, int length, int x, int y, bool centred)
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   202
{
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   203
	uint32 retval = 0;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   204
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   205
	if (axis == AXIS_X) {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   206
		intswap(platforms, length);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   207
		intswap(x, y);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   208
	}
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   209
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   210
	/* Limit our sizes to 4 bits */
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   211
	platforms = min(15, platforms);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   212
	length    = min(15, length);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   213
	x = min(15, x);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   214
	y = min(15, y);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   215
	if (centred) {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   216
		x -= platforms / 2;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   217
		y -= length / 2;
3790
aeccfa7ce681 (svn r4786) - Newstations: in GetPlatformInfo() ensure negative values are only four bits
peter1138
parents: 3789
diff changeset
   218
		SB(retval,  0, 4, y & 0xF);
aeccfa7ce681 (svn r4786) - Newstations: in GetPlatformInfo() ensure negative values are only four bits
peter1138
parents: 3789
diff changeset
   219
		SB(retval,  4, 4, x & 0xF);
3756
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   220
	} else {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   221
		SB(retval,  0, 4, y);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   222
		SB(retval,  4, 4, length - y - 1);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   223
		SB(retval,  8, 4, x);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   224
		SB(retval, 12, 4, platforms - x - 1);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   225
	}
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   226
	SB(retval, 16, 4, length);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   227
	SB(retval, 20, 4, platforms);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   228
	SB(retval, 24, 4, tile);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   229
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   230
	return retval;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   231
}
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   232
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   233
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   234
/* Find the end of a railway station, from the tile, in the direction of delta.
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   235
 * If check_type is set, we stop if the custom station type changes.
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   236
 * If check_axis is set, we stop if the station direction changes.
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   237
 */
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   238
static TileIndex FindRailStationEnd(TileIndex tile, TileIndexDiff delta, bool check_type, bool check_axis)
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   239
{
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   240
	bool waypoint;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   241
	byte orig_type = 0;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   242
	Axis orig_axis = AXIS_X;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   243
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   244
	waypoint = IsTileType(tile, MP_RAILWAY);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   245
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   246
	if (waypoint) {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   247
		if (check_axis) orig_axis = GetWaypointAxis(tile);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   248
	} else {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   249
		if (check_type) orig_type = GetCustomStationSpecIndex(tile);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   250
		if (check_axis) orig_axis = GetRailStationAxis(tile);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   251
	}
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   252
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   253
	while (true) {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   254
		TileIndex new_tile = TILE_ADD(tile, delta);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   255
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   256
		if (waypoint) {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   257
			if (!IsTileType(new_tile, MP_RAILWAY)) break;
4182
48dba107ff43 (svn r5624) Use {IsPlainRailTile,IsRailDepot,IsRailWaypoint,HasSignals}() instead of GetRailTile{T,Subt}ype() - this is more concise and a bit more flexible if/when the rail tile encoding changes
tron
parents: 3893
diff changeset
   258
			if (!IsRailWaypoint(new_tile)) break;
3756
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   259
			if (check_axis && GetWaypointAxis(new_tile) != orig_axis) break;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   260
		} else {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   261
			if (!IsRailwayStationTile(new_tile)) break;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   262
			if (check_type && GetCustomStationSpecIndex(new_tile) != orig_type) break;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   263
			if (check_axis && GetRailStationAxis(new_tile) != orig_axis) break;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   264
		}
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   265
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   266
		tile = new_tile;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   267
	}
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   268
	return tile;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   269
}
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   270
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   271
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   272
static uint32 GetPlatformInfoHelper(TileIndex tile, bool check_type, bool check_axis, bool centred)
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   273
{
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   274
	int tx = TileX(tile);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   275
	int ty = TileY(tile);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   276
	int sx = TileX(FindRailStationEnd(tile, TileDiffXY(-1,  0), check_type, check_axis));
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   277
	int sy = TileY(FindRailStationEnd(tile, TileDiffXY( 0, -1), check_type, check_axis));
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   278
	int ex = TileX(FindRailStationEnd(tile, TileDiffXY( 1,  0), check_type, check_axis)) + 1;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   279
	int ey = TileY(FindRailStationEnd(tile, TileDiffXY( 0,  1), check_type, check_axis)) + 1;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   280
	Axis axis = IsTileType(tile, MP_RAILWAY) ? GetWaypointAxis(tile) : GetRailStationAxis(tile);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   281
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   282
	tx -= sx; ex -= sx;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   283
	ty -= sy; ey -= sy;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   284
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   285
	return GetPlatformInfo(axis, IsTileType(tile, MP_RAILWAY) ? 2 : GetStationGfx(tile), ex, ey, tx, ty, centred);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   286
}
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   287
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   288
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   289
/* Station Resolver Functions */
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   290
static uint32 StationGetRandomBits(const ResolverObject *object)
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   291
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   292
	const Station *st = object->u.station.st;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   293
	const TileIndex tile = object->u.station.tile;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   294
	return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16);
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   295
}
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   296
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   297
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   298
static uint32 StationGetTriggers(const ResolverObject *object)
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   299
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   300
	const Station *st = object->u.station.st;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   301
	return st == NULL ? 0 : st->waiting_triggers;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   302
}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   303
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   304
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   305
static void StationSetTriggers(const ResolverObject *object, int triggers)
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   306
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   307
	Station *st = (Station*)object->u.station.st;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   308
	assert(st != NULL);
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   309
	st->waiting_triggers = triggers;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   310
}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   311
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   312
3893
ced26054ee00 (svn r4960) - NewGRF: explicitly handle unsupported variables, instead of returning -1.
peter1138
parents: 3834
diff changeset
   313
static uint32 StationGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   314
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   315
	const Station *st = object->u.station.st;
3756
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   316
	TileIndex tile = object->u.station.tile;
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   317
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   318
	if (st == NULL) {
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   319
		/* Station does not exist, so we're in a purchase list */
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   320
		switch (variable) {
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   321
			case 0x40:
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   322
			case 0x41:
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   323
			case 0x46:
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   324
			case 0x47:
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   325
			case 0x49: return 0x2110000;       /* Platforms, tracks & position */
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   326
			case 0x42: return 0;               /* Rail type (XXX Get current type from GUI?) */
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   327
			case 0x43: return _current_player; /* Station owner */
3748
aeb7aee9ef48 (svn r4736) - Newstations: use the correct value for PBS status (No PBS)
peter1138
parents: 3746
diff changeset
   328
			case 0x44: return 2;               /* PBS status */
4331
ec584824ffb8 (svn r6013) -Fix: some newgrf properties returned the date in the new format where the newgrf's expect the original format. Thanks to michi_cc for finding these and providing the initial patch.
rubidium
parents: 4261
diff changeset
   329
			case 0xFA: return max(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0); /* Build date */
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   330
		}
3893
ced26054ee00 (svn r4960) - NewGRF: explicitly handle unsupported variables, instead of returning -1.
peter1138
parents: 3834
diff changeset
   331
ced26054ee00 (svn r4960) - NewGRF: explicitly handle unsupported variables, instead of returning -1.
peter1138
parents: 3834
diff changeset
   332
		*available = false;
ced26054ee00 (svn r4960) - NewGRF: explicitly handle unsupported variables, instead of returning -1.
peter1138
parents: 3834
diff changeset
   333
		return -1;
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   334
	}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   335
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   336
	switch (variable) {
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   337
		/* Calculated station variables */
3756
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   338
		case 0x40: return GetPlatformInfoHelper(tile, false, false, false);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   339
		case 0x41: return GetPlatformInfoHelper(tile, true,  false, false);
3758
572e7321a53a (svn r4749) - Newstations: add snowline support to variable 42.
peter1138
parents: 3756
diff changeset
   340
		case 0x42: /* Terrain and rail type */
572e7321a53a (svn r4749) - Newstations: add snowline support to variable 42.
peter1138
parents: 3756
diff changeset
   341
			return ((_opt.landscape == LT_HILLY && GetTileZ(tile) > _opt.snow_line) ? 4 : 0) |
572e7321a53a (svn r4749) - Newstations: add snowline support to variable 42.
peter1138
parents: 3756
diff changeset
   342
			       (GetRailType(tile) << 8);
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   343
		case 0x43: return st->owner; /* Station owner */
3748
aeb7aee9ef48 (svn r4736) - Newstations: use the correct value for PBS status (No PBS)
peter1138
parents: 3746
diff changeset
   344
		case 0x44: return 2;         /* PBS status */
3756
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   345
		case 0x46: return GetPlatformInfoHelper(tile, false, false, true);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   346
		case 0x47: return GetPlatformInfoHelper(tile, true,  false, true);
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   347
		case 0x48: { /* Accepted cargo types */
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   348
			CargoID cargo_type;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   349
			uint32 value = 0;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   350
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   351
			for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   352
				if (HASBIT(st->goods[cargo_type].waiting_acceptance, 15)) SETBIT(value, cargo_type);
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   353
			}
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   354
			return value;
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   355
		}
3756
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   356
		case 0x49: return GetPlatformInfoHelper(tile, false, true, false);
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   357
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   358
		/* Variables which use the parameter */
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   359
		case 0x60: return GB(st->goods[parameter].waiting_acceptance, 0, 12);
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   360
		case 0x61: return st->goods[parameter].days_since_pickup;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   361
		case 0x62: return st->goods[parameter].rating;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   362
		case 0x63: return st->goods[parameter].enroute_time;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   363
		case 0x64: return st->goods[parameter].last_speed | (st->goods[parameter].last_age << 8);
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   364
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   365
		/* General station properties */
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   366
		case 0x82: return 50;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   367
		case 0x84: return st->string_id;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   368
		case 0x86: return 0;
3746
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   369
		case 0x8A: return st->had_vehicle_of_type;
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   370
		case 0xF0: return st->facilities;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   371
		case 0xF1: return st->airport_type;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   372
		case 0xF2: return st->truck_stops->status;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   373
		case 0xF3: return st->bus_stops->status;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   374
		case 0xF6: return st->airport_flags;
5172
285d1ab82403 (svn r7282) -Fix (r625): ttdpatch vars are little endian
peter1138
parents: 4817
diff changeset
   375
		case 0xF7: return GB(st->airport_flags, 8, 8);
4331
ec584824ffb8 (svn r6013) -Fix: some newgrf properties returned the date in the new format where the newgrf's expect the original format. Thanks to michi_cc for finding these and providing the initial patch.
rubidium
parents: 4261
diff changeset
   376
		case 0xFA: return max(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0);
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   377
	}
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   378
3746
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   379
	/* Handle cargo variables (deprecated) */
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   380
	if (variable >= 0x8C && variable <= 0xEC) {
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   381
		const GoodsEntry *g = &st->goods[GB(variable - 0x8C, 3, 4)];
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   382
		switch (GB(variable - 0x8C, 0, 3)) {
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   383
			case 0: return g->waiting_acceptance;
5172
285d1ab82403 (svn r7282) -Fix (r625): ttdpatch vars are little endian
peter1138
parents: 4817
diff changeset
   384
			case 1: return GB(g->waiting_acceptance, 8, 8);
3746
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   385
			case 2: return g->days_since_pickup;
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   386
			case 3: return g->rating;
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   387
			case 4: return g->enroute_from;
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   388
			case 5: return g->enroute_time;
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   389
			case 6: return g->last_speed;
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   390
			case 7: return g->last_age;
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   391
		}
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   392
	}
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   393
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5172
diff changeset
   394
	DEBUG(grf, 1, "Unhandled station property 0x%X", variable);
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   395
3893
ced26054ee00 (svn r4960) - NewGRF: explicitly handle unsupported variables, instead of returning -1.
peter1138
parents: 3834
diff changeset
   396
	*available = false;
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   397
	return -1;
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   398
}
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   399
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   400
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   401
static const SpriteGroup *StationResolveReal(const ResolverObject *object, const SpriteGroup *group)
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   402
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   403
	const Station *st = object->u.station.st;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   404
	const StationSpec *statspec = object->u.station.statspec;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   405
	uint set;
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   406
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   407
	uint cargo = 0;
4817
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   408
	CargoID cargo_type = object->u.station.cargo_type;
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   409
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   410
	if (st == NULL || statspec->sclass == STAT_CLASS_WAYP) {
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   411
		return group->g.real.loading[0];
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   412
	}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   413
4817
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   414
	switch (cargo_type) {
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   415
		case GC_INVALID:
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   416
		case GC_DEFAULT_NA:
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   417
		case GC_PURCHASE:
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   418
			cargo = 0;
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   419
			break;
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   420
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   421
		case GC_DEFAULT:
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   422
			for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   423
				cargo += GB(st->goods[cargo_type].waiting_acceptance, 0, 12);
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   424
			}
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   425
			break;
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   426
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   427
		default:
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   428
			cargo = GB(st->goods[_local_cargo_id_ctype[cargo_type]].waiting_acceptance, 0, 12);
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   429
			break;
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   430
	}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   431
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   432
	if (HASBIT(statspec->flags, 1)) cargo /= (st->trainst_w + st->trainst_h);
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   433
	cargo = min(0xfff, cargo);
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   434
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   435
	if (cargo > statspec->cargo_threshold) {
3749
b6c1a611d840 (svn r4737) - Newstations: 'real' groups picked the wrong set of loading or loaded sprites
peter1138
parents: 3748
diff changeset
   436
		if (group->g.real.num_loading > 0) {
3787
3b0c50f6db0e (svn r4783) - Newstations: fix graphical bug that affected some stations when a station has a high amount of cargo waiting.
peter1138
parents: 3786
diff changeset
   437
			set = ((cargo - statspec->cargo_threshold) * group->g.real.num_loading) / (4096 - statspec->cargo_threshold);
3749
b6c1a611d840 (svn r4737) - Newstations: 'real' groups picked the wrong set of loading or loaded sprites
peter1138
parents: 3748
diff changeset
   438
			return group->g.real.loading[set];
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   439
		}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   440
	} else {
3749
b6c1a611d840 (svn r4737) - Newstations: 'real' groups picked the wrong set of loading or loaded sprites
peter1138
parents: 3748
diff changeset
   441
		if (group->g.real.num_loaded > 0) {
b6c1a611d840 (svn r4737) - Newstations: 'real' groups picked the wrong set of loading or loaded sprites
peter1138
parents: 3748
diff changeset
   442
			set = (cargo * group->g.real.num_loaded) / (statspec->cargo_threshold + 1);
b6c1a611d840 (svn r4737) - Newstations: 'real' groups picked the wrong set of loading or loaded sprites
peter1138
parents: 3748
diff changeset
   443
			return group->g.real.loaded[set];
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   444
		}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   445
	}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   446
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   447
	return group->g.real.loading[0];
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   448
}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   449
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   450
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   451
static void NewStationResolver(ResolverObject *res, const StationSpec *statspec, const Station *st, TileIndex tile)
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   452
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   453
	res->GetRandomBits = StationGetRandomBits;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   454
	res->GetTriggers   = StationGetTriggers;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   455
	res->SetTriggers   = StationSetTriggers;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   456
	res->GetVariable   = StationGetVariable;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   457
	res->ResolveReal   = StationResolveReal;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   458
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   459
	res->u.station.st       = st;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   460
	res->u.station.statspec = statspec;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   461
	res->u.station.tile     = tile;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   462
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   463
	res->callback        = 0;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   464
	res->callback_param1 = 0;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   465
	res->callback_param2 = 0;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   466
	res->last_value      = 0;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   467
	res->trigger         = 0;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   468
	res->reseed          = 0;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   469
}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   470
4817
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   471
static const SpriteGroup *ResolveStation(const StationSpec *statspec, const Station *st, ResolverObject *object)
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   472
{
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   473
	const SpriteGroup *group;
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   474
	CargoID ctype = GC_DEFAULT_NA;
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   475
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   476
	if (st == NULL) {
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   477
		/* No station, so we are in a purchase list */
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   478
		ctype = GC_PURCHASE;
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   479
	} else {
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   480
		CargoID cargo;
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   481
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   482
		/* Pick the first cargo that we have waiting */
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   483
		for (cargo = 0; cargo < NUM_GLOBAL_CID; cargo++) {
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   484
			CargoID lcid = _local_cargo_id_ctype[cargo];
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   485
			if (lcid != CT_INVALID && statspec->spritegroup[cargo] != NULL && GB(st->goods[lcid].waiting_acceptance, 0, 12) != 0) {
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   486
				ctype = cargo;
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   487
				break;
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   488
			}
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   489
		}
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   490
	}
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   491
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   492
	group = statspec->spritegroup[ctype];
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   493
	if (group == NULL) {
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   494
		ctype = GC_DEFAULT;
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   495
		group = statspec->spritegroup[ctype];
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   496
	}
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   497
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   498
	if (group == NULL) return NULL;
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   499
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   500
	/* Remember the cargo type we've picked */
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   501
	object->u.station.cargo_type = ctype;
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   502
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   503
	return Resolve(group, object);
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   504
}
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   505
3751
ce6408fdb788 (svn r4739) - Newstations: remove cargo type parameter of GetCustomStationRelocation() as we can determine it internally
peter1138
parents: 3750
diff changeset
   506
SpriteID GetCustomStationRelocation(const StationSpec *statspec, const Station *st, TileIndex tile)
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   507
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   508
	const SpriteGroup *group;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   509
	ResolverObject object;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   510
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   511
	NewStationResolver(&object, statspec, st, tile);
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   512
4817
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   513
	group = ResolveStation(statspec, st, &object);
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   514
	if (group == NULL || group->type != SGT_RESULT) return 0;
3775
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   515
	return group->g.result.sprite - 0x42D;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   516
}
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   517
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   518
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   519
SpriteID GetCustomStationGroundRelocation(const StationSpec *statspec, const Station *st, TileIndex tile)
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   520
{
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   521
	const SpriteGroup *group;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   522
	ResolverObject object;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   523
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   524
	NewStationResolver(&object, statspec, st, tile);
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   525
	object.callback_param1 = 1; /* Indicate we are resolving the ground sprite */
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   526
4817
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   527
	group = ResolveStation(statspec, st, &object);
3775
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   528
	if (group == NULL || group->type != SGT_RESULT) return 0;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   529
	return group->g.result.sprite - 0x42D;
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   530
}
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   531
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   532
3752
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   533
uint16 GetStationCallback(uint16 callback, uint32 param1, uint32 param2, const StationSpec *statspec, const Station *st, TileIndex tile)
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   534
{
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   535
	const SpriteGroup *group;
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   536
	ResolverObject object;
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   537
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   538
	NewStationResolver(&object, statspec, st, tile);
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   539
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   540
	object.callback        = callback;
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   541
	object.callback_param1 = param1;
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   542
	object.callback_param2 = param2;
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   543
4817
116374e1a5f1 (svn r6741) - Codechange: Simplify and unify resolving of station groups, and use the first available cargo type instead of only the default.
peter1138
parents: 4331
diff changeset
   544
	group = ResolveStation(statspec, st, &object);
3752
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   545
	if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   546
	return group->g.callback.result;
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   547
}
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   548
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   549
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   550
/**
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   551
 * Allocate a StationSpec to a Station. This is called once per build operation.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   552
 * @param spec StationSpec to allocate.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   553
 * @param st Station to allocate it to.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   554
 * @param exec Whether to actually allocate the spec.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   555
 * @return Index within the Station's spec list, or -1 if the allocation failed.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   556
 */
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3668
diff changeset
   557
int AllocateSpecToStation(const StationSpec *statspec, Station *st, bool exec)
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   558
{
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   559
	uint i;
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   560
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3668
diff changeset
   561
	if (statspec == NULL) return 0;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   562
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   563
	/* Check if this spec has already been allocated */
3786
ac7ff031ff5f (svn r4782) - Newstations: fix issue with saving/loading games with newstations on big endian architectures.
peter1138
parents: 3780
diff changeset
   564
	for (i = 1; i < st->num_specs && i < MAX_SPECLIST; i++) {
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3668
diff changeset
   565
		if (st->speclist[i].spec == statspec) return i;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   566
	}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   567
3786
ac7ff031ff5f (svn r4782) - Newstations: fix issue with saving/loading games with newstations on big endian architectures.
peter1138
parents: 3780
diff changeset
   568
	for (i = 1; i < st->num_specs && i < MAX_SPECLIST; i++) {
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   569
		if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break;
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   570
	}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   571
3786
ac7ff031ff5f (svn r4782) - Newstations: fix issue with saving/loading games with newstations on big endian architectures.
peter1138
parents: 3780
diff changeset
   572
	if (i == MAX_SPECLIST) return -1;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   573
3730
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   574
	if (exec) {
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   575
		if (i >= st->num_specs) {
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   576
			st->num_specs = i + 1;
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   577
			st->speclist = realloc(st->speclist, st->num_specs * sizeof(*st->speclist));
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   578
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   579
			if (st->num_specs == 2) {
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   580
				/* Initial allocation */
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   581
				st->speclist[0].spec     = NULL;
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   582
				st->speclist[0].grfid    = 0;
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   583
				st->speclist[0].localidx = 0;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   584
			}
3730
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   585
		}
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   586
3730
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   587
		st->speclist[i].spec     = statspec;
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   588
		st->speclist[i].grfid    = statspec->grfid;
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   589
		st->speclist[i].localidx = statspec->localidx;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   590
	}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   591
3730
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   592
	return i;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   593
}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   594
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   595
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   596
/** Deallocate a StationSpec from a Station. Called when removing a single station tile.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   597
 * @param st Station to work with.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   598
 * @param specindex Index of the custom station within the Station's spec list.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   599
 * @return Indicates whether the StationSpec was deallocated.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   600
 */
4190
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   601
void DeallocateSpecFromStation(Station* st, byte specindex)
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   602
{
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   603
	/* specindex of 0 (default) is never freeable */
4190
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   604
	if (specindex == 0) return;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   605
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   606
	/* Check all tiles over the station to check if the specindex is still in use */
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   607
	BEGIN_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile) {
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   608
		if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st->index && IsRailwayStation(tile) && GetCustomStationSpecIndex(tile) == specindex) {
4190
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   609
			return;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   610
		}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   611
	} END_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile)
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   612
4190
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   613
	/* This specindex is no longer in use, so deallocate it */
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   614
	st->speclist[specindex].spec     = NULL;
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   615
	st->speclist[specindex].grfid    = 0;
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   616
	st->speclist[specindex].localidx = 0;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   617
4190
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   618
	/* If this was the highest spec index, reallocate */
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   619
	if (specindex == st->num_specs - 1) {
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   620
		for (; st->speclist[st->num_specs - 1].grfid == 0 && st->num_specs > 1; st->num_specs--);
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   621
4190
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   622
		if (st->num_specs > 1) {
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   623
			st->speclist = realloc(st->speclist, st->num_specs * sizeof(*st->speclist));
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   624
		} else {
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   625
			free(st->speclist);
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   626
			st->num_specs = 0;
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   627
			st->speclist  = NULL;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   628
		}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   629
	}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   630
}
3764
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   631
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   632
/** Draw representation of a station tile for GUI purposes.
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   633
 * @param x, y Position of image.
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   634
 * @param dir Direction.
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   635
 * @param railtype Rail type.
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   636
 * @param sclass, station Type of station.
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   637
 * @return True if the tile was drawn (allows for fallback to default graphic)
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   638
 */
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   639
bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID sclass, uint station)
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   640
{
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   641
	const StationSpec *statspec;
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   642
	const DrawTileSprites *sprites;
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   643
	const DrawTileSeqStruct *seq;
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   644
	const RailtypeInfo *rti = GetRailTypeInfo(railtype);
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   645
	SpriteID relocation;
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   646
	PalSpriteID image;
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   647
	PalSpriteID colourmod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player));
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   648
	uint tile = 2;
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   649
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   650
	statspec = GetCustomStationSpec(sclass, station);
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   651
	if (statspec == NULL) return false;
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   652
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   653
	relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE);
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   654
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   655
	if (HASBIT(statspec->callbackmask, CBM_CUSTOM_LAYOUT)) {
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   656
		uint16 callback = GetStationCallback(CBID_STATION_SPRITE_LAYOUT, 0x2110000, 0, statspec, NULL, INVALID_TILE);
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   657
		if (callback != CALLBACK_FAILED) tile = callback;
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   658
	}
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   659
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   660
	if (statspec->renderdata == NULL) {
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   661
		sprites = GetStationTileLayout(tile + axis);
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   662
	} else {
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   663
		sprites = &statspec->renderdata[(tile < statspec->tiles) ? tile + axis : axis];
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   664
	}
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   665
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   666
	image = sprites->ground_sprite;
3775
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   667
	if (HASBIT(image, 31)) {
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   668
		CLRBIT(image, 31);
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   669
		image += GetCustomStationGroundRelocation(statspec, NULL, INVALID_TILE);
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   670
		image += rti->custom_ground_offset;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   671
	} else {
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   672
		image += rti->total_offset;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   673
	}
3764
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   674
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   675
	if (image & PALETTE_MODIFIER_COLOR) image &= SPRITE_MASK;
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   676
	DrawSprite(image, x, y);
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   677
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   678
	foreach_draw_tile_seq(seq, sprites->seq) {
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   679
		Point pt;
3775
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   680
		image = seq->image;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   681
		if (HASBIT(image, 30)) {
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   682
			CLRBIT(image, 30);
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   683
			image += rti->total_offset;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   684
		} else {
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   685
			image += relocation;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   686
		}
3764
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   687
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   688
		if ((byte)seq->delta_z != 0x80) {
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   689
			pt = RemapCoords(seq->delta_x, seq->delta_y, seq->delta_z);
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   690
			DrawSprite((image & SPRITE_MASK) | colourmod, x + pt.x, y + pt.y);
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   691
		}
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   692
	}
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   693
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   694
	return true;
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   695
}
f017ce73e96b (svn r4755) - Newstations: add a gui station tile drawing routine and use it in place of the existing one for waypoints.
peter1138
parents: 3758
diff changeset
   696
4206
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   697
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   698
static const StationSpec* GetStationSpec(TileIndex t)
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   699
{
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   700
	const Station* st;
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   701
	uint specindex;
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   702
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   703
	if (!IsCustomStationSpecIndex(t)) return NULL;
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   704
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   705
	st = GetStationByTile(t);
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   706
	specindex = GetCustomStationSpecIndex(t);
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   707
	return specindex < st->num_specs ? st->speclist[specindex].spec : NULL;
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   708
}
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   709
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   710
3766
fc2dea6ef369 (svn r4758) - Newstations: add support for 'blocked' station tiles, which no train can pass.
peter1138
parents: 3765
diff changeset
   711
/* Check if a rail station tile is traversable.
fc2dea6ef369 (svn r4758) - Newstations: add support for 'blocked' station tiles, which no train can pass.
peter1138
parents: 3765
diff changeset
   712
 * XXX This could be cached (during build) in the map array to save on all the dereferencing */
fc2dea6ef369 (svn r4758) - Newstations: add support for 'blocked' station tiles, which no train can pass.
peter1138
parents: 3765
diff changeset
   713
bool IsStationTileBlocked(TileIndex tile)
fc2dea6ef369 (svn r4758) - Newstations: add support for 'blocked' station tiles, which no train can pass.
peter1138
parents: 3765
diff changeset
   714
{
4206
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   715
	const StationSpec* statspec = GetStationSpec(tile);
3766
fc2dea6ef369 (svn r4758) - Newstations: add support for 'blocked' station tiles, which no train can pass.
peter1138
parents: 3765
diff changeset
   716
4206
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   717
	return statspec != NULL && HASBIT(statspec->blocked, GetStationGfx(tile));
3766
fc2dea6ef369 (svn r4758) - Newstations: add support for 'blocked' station tiles, which no train can pass.
peter1138
parents: 3765
diff changeset
   718
}
3789
c50647c927e4 (svn r4785) - Newstations: don't draw catenary on non-track tiles
glx
parents: 3787
diff changeset
   719
c50647c927e4 (svn r4785) - Newstations: don't draw catenary on non-track tiles
glx
parents: 3787
diff changeset
   720
/* Check if a rail station tile is electrifiable.
c50647c927e4 (svn r4785) - Newstations: don't draw catenary on non-track tiles
glx
parents: 3787
diff changeset
   721
 * XXX This could be cached (during build) in the map array to save on all the dereferencing */
c50647c927e4 (svn r4785) - Newstations: don't draw catenary on non-track tiles
glx
parents: 3787
diff changeset
   722
bool IsStationTileElectrifiable(TileIndex tile)
c50647c927e4 (svn r4785) - Newstations: don't draw catenary on non-track tiles
glx
parents: 3787
diff changeset
   723
{
4206
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   724
	const StationSpec* statspec = GetStationSpec(tile);
3789
c50647c927e4 (svn r4785) - Newstations: don't draw catenary on non-track tiles
glx
parents: 3787
diff changeset
   725
4206
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   726
	return
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   727
		statspec == NULL ||
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   728
		HASBIT(statspec->pylons, GetStationGfx(tile)) ||
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   729
		!HASBIT(statspec->wires, GetStationGfx(tile));
3789
c50647c927e4 (svn r4785) - Newstations: don't draw catenary on non-track tiles
glx
parents: 3787
diff changeset
   730
}