src/newgrf_station.cpp
author rubidium
Wed, 03 Dec 2008 00:09:26 +0000
changeset 10398 bb046bbab3a3
parent 10355 8a930759b457
permissions -rw-r--r--
(svn r14649) -Change: consistently use can't instead of primarily can't and a bit of cannot in english.txt.
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
6117
6507b2a7e71d (svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents: 6113
diff changeset
     3
/** @file newgrf_station.cpp 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"
8119
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8118
diff changeset
     8
#include "tile_cmd.h"
6343
76d17f784c13 (svn r9371) -Feature: Add support for variable snow lines in the arctic climate, supplied
maedhros
parents: 6247
diff changeset
     9
#include "landscape.h"
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    10
#include "debug.h"
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    11
#include "sprite.h"
8785
871586967963 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 8756
diff changeset
    12
#include "station_base.h"
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
    13
#include "station_map.h"
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5172
diff changeset
    14
#include "newgrf.h"
3752
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
    15
#include "newgrf_callbacks.h"
6849
e1b7913352f8 (svn r10089) -Fix (r10040): Use GetTileZ instead of GetClearGround in GetTerrainType as
maedhros
parents: 6683
diff changeset
    16
#include "newgrf_commons.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"
9018
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
    19
#include "newgrf_sound.h"
6113
a10ede03ac58 (svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents: 6106
diff changeset
    20
#include "cargotype.h"
9726
7cc08b5b0787 (svn r13851) -Fix (r9393): GetTownByTile() is only valid for houses and roads.
frosch
parents: 9714
diff changeset
    21
#include "town.h"
6354
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
    22
#include "newgrf_town.h"
8123
ce31d2843a95 (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 8121
diff changeset
    23
#include "gfx_func.h"
8140
0d0d8c94f84b (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8130
diff changeset
    24
#include "date_func.h"
10208
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    25
#include "company_func.h"
9005
d6b0e0a54ef2 (svn r12800) -Codechange: move the animated tile related functions out of texteff.cpp (it isn't a text effect after all). Also remove a few more functions from functions.
rubidium
parents: 9004
diff changeset
    26
#include "animated_tile_func.h"
9006
7666e7b47088 (svn r12801) -Codechange: remove the dependency of function.h in town_map.h
rubidium
parents: 9005
diff changeset
    27
#include "functions.h"
9662
d21460a10105 (svn r13734) -Fix: NewGRF rail continuation would always mark a tunnel on the same axis as connected, even when the tunnel faces the wrong direction.
rubidium
parents: 9038
diff changeset
    28
#include "tunnelbridge_map.h"
9823
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9750
diff changeset
    29
#include "rail_map.h"
10256
d69ddb1a8422 (svn r14487) -Fix: The station picker preview did not draw child sprites.
frosch
parents: 10208
diff changeset
    30
#include "spritecache.h"
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    31
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8254
diff changeset
    32
#include "table/sprites.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8254
diff changeset
    33
#include "table/strings.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8254
diff changeset
    34
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    35
static StationClass _station_classes[STAT_CLASS_MAX];
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    36
3786
ac7ff031ff5f (svn r4782) - Newstations: fix issue with saving/loading games with newstations on big endian architectures.
peter1138
parents: 3780
diff changeset
    37
enum {
ac7ff031ff5f (svn r4782) - Newstations: fix issue with saving/loading games with newstations on big endian architectures.
peter1138
parents: 3780
diff changeset
    38
	MAX_SPECLIST = 255,
ac7ff031ff5f (svn r4782) - Newstations: fix issue with saving/loading games with newstations on big endian architectures.
peter1138
parents: 3780
diff changeset
    39
};
ac7ff031ff5f (svn r4782) - Newstations: fix issue with saving/loading games with newstations on big endian architectures.
peter1138
parents: 3780
diff changeset
    40
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    41
/**
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    42
 * Reset station classes to their default state.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    43
 * 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
    44
 * entry, for standard stations and waypoints.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    45
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6148
diff changeset
    46
void ResetStationClasses()
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    47
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    48
	for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) {
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    49
		_station_classes[i].id = 0;
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    50
		_station_classes[i].name = STR_EMPTY;
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    51
		_station_classes[i].stations = 0;
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    52
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    53
		free(_station_classes[i].spec);
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    54
		_station_classes[i].spec = NULL;
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    55
	}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    56
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6343
diff changeset
    57
	/* Set up initial data */
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    58
	_station_classes[0].id = 'DFLT';
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    59
	_station_classes[0].name = STR_STAT_CLASS_DFLT;
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    60
	_station_classes[0].stations = 1;
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    61
	_station_classes[0].spec = MallocT<StationSpec*>(1);
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    62
	_station_classes[0].spec[0] = NULL;
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    63
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    64
	_station_classes[1].id = 'WAYP';
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    65
	_station_classes[1].name = STR_STAT_CLASS_WAYP;
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    66
	_station_classes[1].stations = 1;
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    67
	_station_classes[1].spec = MallocT<StationSpec*>(1);
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    68
	_station_classes[1].spec[0] = NULL;
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    69
}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    70
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    71
/**
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    72
 * Allocate a station class for the given class id.
6481
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6460
diff changeset
    73
 * @param cls A 32 bit value identifying the class.
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    74
 * @return Index into _station_classes of allocated class.
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    75
 */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    76
StationClassID AllocateStationClass(uint32 cls)
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    77
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    78
	for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) {
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    79
		if (_station_classes[i].id == cls) {
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6343
diff changeset
    80
			/* ClassID is already allocated, so reuse it. */
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    81
			return i;
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    82
		} else if (_station_classes[i].id == 0) {
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6343
diff changeset
    83
			/* This class is empty, so allocate it to the ClassID. */
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    84
			_station_classes[i].id = cls;
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    85
			return i;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    86
		}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    87
	}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    88
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5172
diff changeset
    89
	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
    90
	return STAT_CLASS_DFLT;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    91
}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
    92
3642
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    93
/** Set the name of a custom station class */
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
    94
void SetStationClassName(StationClassID sclass, StringID name)
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
	assert(sclass < STAT_CLASS_MAX);
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
    97
	_station_classes[sclass].name = name;
3642
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
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   100
/** Retrieve the name of a custom station class */
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   101
StringID GetStationClassName(StationClassID sclass)
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   102
{
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   103
	assert(sclass < STAT_CLASS_MAX);
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
   104
	return _station_classes[sclass].name;
3642
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   105
}
8da309b5fd2b (svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents: 3594
diff changeset
   106
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   107
/**
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   108
 * Get the number of station classes in use.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   109
 * @return Number of station classes.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   110
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6148
diff changeset
   111
uint GetNumStationClasses()
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   112
{
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   113
	uint i;
8695
6607e9b9ffe2 (svn r12368) -Codechange: use explicit body for loops and conditions and remove -Wno-empty-body from the configure script
smatz
parents: 8616
diff changeset
   114
	for (i = 0; i < STAT_CLASS_MAX && _station_classes[i].id != 0; i++) {}
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   115
	return i;
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   116
}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   117
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   118
/**
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   119
 * 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
   120
 * @param sclass Index of the station class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   121
 * @return Number of stations in the class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   122
 */
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   123
uint GetNumCustomStations(StationClassID sclass)
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   124
{
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   125
	assert(sclass < STAT_CLASS_MAX);
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
   126
	return _station_classes[sclass].stations;
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   127
}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   128
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   129
/**
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   130
 * Tie a station spec to its station class.
6481
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6460
diff changeset
   131
 * @param statspec The station spec.
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   132
 */
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3668
diff changeset
   133
void SetCustomStationSpec(StationSpec *statspec)
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   134
{
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   135
	StationClass *station_class;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   136
	int i;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   137
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
   138
	/* 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
   139
	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
   140
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3668
diff changeset
   141
	assert(statspec->sclass < STAT_CLASS_MAX);
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
   142
	station_class = &_station_classes[statspec->sclass];
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   143
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   144
	i = station_class->stations++;
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
   145
	station_class->spec = ReallocT(station_class->spec, station_class->stations);
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   146
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3668
diff changeset
   147
	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
   148
	statspec->allocated = true;
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   149
}
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   150
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
 * Retrieve a station spec from a class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   153
 * @param sclass Index of the station class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   154
 * @param station The station index with the class.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   155
 * @return The station spec.
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   156
 */
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3668
diff changeset
   157
const StationSpec *GetCustomStationSpec(StationClassID sclass, uint station)
2625
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
	assert(sclass < STAT_CLASS_MAX);
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
   160
	if (station < _station_classes[sclass].stations)
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
   161
		return _station_classes[sclass].spec[station];
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   162
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6343
diff changeset
   163
	/* If the custom station isn't defined any more, then the GRF file
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6343
diff changeset
   164
	 * probably was not loaded. */
2625
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   165
	return NULL;
19bf7f695537 (svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff changeset
   166
}
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   167
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   168
3765
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   169
const StationSpec *GetCustomStationSpecByGrf(uint32 grfid, byte localidx)
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   170
{
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   171
	uint j;
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   172
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   173
	for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) {
8316
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
   174
		for (j = 0; j < _station_classes[i].stations; j++) {
1afa7e96ee1a (svn r11881) -Cleanup: Code style for global variables.
peter1138
parents: 8295
diff changeset
   175
			const StationSpec *statspec = _station_classes[i].spec[j];
3765
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   176
			if (statspec == NULL) continue;
6451
e576c71bfc09 (svn r9601) -Codechange: Store grf file reference in station spec, not just GRF ID
peter1138
parents: 6357
diff changeset
   177
			if (statspec->grffile->grfid == grfid && statspec->localidx == localidx) return statspec;
3765
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
	}
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   180
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   181
	return NULL;
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   182
}
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   183
a4ddbff73f9f (svn r4757) - Newstations: add saveload support for custom station speclists
peter1138
parents: 3764
diff changeset
   184
3756
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   185
/* 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
   186
 * 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
   187
 * 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
   188
 * 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
   189
 * 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
   190
 * 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
   191
 */
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   192
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
   193
{
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   194
	uint32 retval = 0;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   195
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   196
	if (axis == AXIS_X) {
6106
2898cd9417fd (svn r8841) -Fix
tron
parents: 5849
diff changeset
   197
		Swap(platforms, length);
2898cd9417fd (svn r8841) -Fix
tron
parents: 5849
diff changeset
   198
		Swap(x, y);
3756
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   199
	}
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
	/* 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
   202
	platforms = min(15, platforms);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   203
	length    = min(15, length);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   204
	x = min(15, x);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   205
	y = min(15, y);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   206
	if (centred) {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   207
		x -= platforms / 2;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   208
		y -= length / 2;
3790
aeccfa7ce681 (svn r4786) - Newstations: in GetPlatformInfo() ensure negative values are only four bits
peter1138
parents: 3789
diff changeset
   209
		SB(retval,  0, 4, y & 0xF);
aeccfa7ce681 (svn r4786) - Newstations: in GetPlatformInfo() ensure negative values are only four bits
peter1138
parents: 3789
diff changeset
   210
		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
   211
	} else {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   212
		SB(retval,  0, 4, y);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   213
		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
   214
		SB(retval,  8, 4, x);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   215
		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
   216
	}
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   217
	SB(retval, 16, 4, length);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   218
	SB(retval, 20, 4, platforms);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   219
	SB(retval, 24, 4, tile);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   220
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   221
	return retval;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   222
}
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   223
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   224
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   225
/* 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
   226
 * 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
   227
 * 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
   228
 */
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   229
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
   230
{
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   231
	bool waypoint;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   232
	byte orig_type = 0;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   233
	Axis orig_axis = AXIS_X;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   234
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   235
	waypoint = IsTileType(tile, MP_RAILWAY);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   236
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   237
	if (waypoint) {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   238
		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
   239
	} else {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   240
		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
   241
		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
   242
	}
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
	while (true) {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   245
		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
   246
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   247
		if (waypoint) {
10099
a42cafcba550 (svn r14280) -Codechange: use IsRailWaypointTile() instead of IsTileType() and IsRailWaypoint() checks at several places
smatz
parents: 9823
diff changeset
   248
			if (!IsRailWaypointTile(new_tile)) break;
3756
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   249
			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
   250
		} else {
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   251
			if (!IsRailwayStationTile(new_tile)) break;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   252
			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
   253
			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
   254
		}
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
		tile = new_tile;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   257
	}
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   258
	return tile;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   259
}
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   260
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   261
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   262
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
   263
{
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   264
	int tx = TileX(tile);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   265
	int ty = TileY(tile);
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   266
	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
   267
	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
   268
	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
   269
	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
   270
	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
   271
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   272
	tx -= sx; ex -= sx;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   273
	ty -= sy; ey -= sy;
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   274
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   275
	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
   276
}
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   277
c295b025ac56 (svn r4747) - Newstations: implement support for variables 40, 41, 46, 47 and 49.
peter1138
parents: 3755
diff changeset
   278
5409
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   279
static uint32 GetRailContinuationInfo(TileIndex tile)
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   280
{
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   281
	/* Tile offsets and exit dirs for X axis */
7157
ff42326d2fde (svn r10431) -Codechange: constness on static array
peter1138
parents: 7010
diff changeset
   282
	static const Direction x_dir[8] = { DIR_SW, DIR_NE, DIR_SE, DIR_NW, DIR_S, DIR_E, DIR_W, DIR_N };
ff42326d2fde (svn r10431) -Codechange: constness on static array
peter1138
parents: 7010
diff changeset
   283
	static const DiagDirection x_exits[8] = { DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SW, DIAGDIR_NE };
5409
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   284
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   285
	/* Tile offsets and exit dirs for Y axis */
7157
ff42326d2fde (svn r10431) -Codechange: constness on static array
peter1138
parents: 7010
diff changeset
   286
	static const Direction y_dir[8] = { DIR_SE, DIR_NW, DIR_SW, DIR_NE, DIR_S, DIR_W, DIR_E, DIR_N };
ff42326d2fde (svn r10431) -Codechange: constness on static array
peter1138
parents: 7010
diff changeset
   287
	static const DiagDirection y_exits[8] = { DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_NW };
5409
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   288
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   289
	Axis axis = IsTileType(tile, MP_RAILWAY) ? GetWaypointAxis(tile) : GetRailStationAxis(tile);
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   290
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   291
	/* Choose appropriate lookup table to use */
7157
ff42326d2fde (svn r10431) -Codechange: constness on static array
peter1138
parents: 7010
diff changeset
   292
	const Direction *dir = axis == AXIS_X ? x_dir : y_dir;
ff42326d2fde (svn r10431) -Codechange: constness on static array
peter1138
parents: 7010
diff changeset
   293
	const DiagDirection *diagdir = axis == AXIS_X ? x_exits : y_exits;
5409
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   294
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   295
	uint32 res = 0;
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   296
	uint i;
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   297
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   298
	for (i = 0; i < lengthof(x_dir); i++, dir++, diagdir++) {
9662
d21460a10105 (svn r13734) -Fix: NewGRF rail continuation would always mark a tunnel on the same axis as connected, even when the tunnel faces the wrong direction.
rubidium
parents: 9038
diff changeset
   299
		TileIndex neighbour_tile = tile + TileOffsByDir(*dir);
d21460a10105 (svn r13734) -Fix: NewGRF rail continuation would always mark a tunnel on the same axis as connected, even when the tunnel faces the wrong direction.
rubidium
parents: 9038
diff changeset
   300
		TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(neighbour_tile, TRANSPORT_RAIL, 0));
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8571
diff changeset
   301
		if (trackbits != TRACK_BIT_NONE) {
5409
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   302
			/* If there is any track on the tile, set the bit in the second byte */
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   303
			SetBit(res, i + 8);
5409
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   304
9662
d21460a10105 (svn r13734) -Fix: NewGRF rail continuation would always mark a tunnel on the same axis as connected, even when the tunnel faces the wrong direction.
rubidium
parents: 9038
diff changeset
   305
			/* With tunnels and bridges the tile has tracks, but they are not necessarily connected
d21460a10105 (svn r13734) -Fix: NewGRF rail continuation would always mark a tunnel on the same axis as connected, even when the tunnel faces the wrong direction.
rubidium
parents: 9038
diff changeset
   306
			 * with the next tile because the ramp is not going in the right direction. */
d21460a10105 (svn r13734) -Fix: NewGRF rail continuation would always mark a tunnel on the same axis as connected, even when the tunnel faces the wrong direction.
rubidium
parents: 9038
diff changeset
   307
			if (IsTileType(neighbour_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(neighbour_tile) != *diagdir) {
d21460a10105 (svn r13734) -Fix: NewGRF rail continuation would always mark a tunnel on the same axis as connected, even when the tunnel faces the wrong direction.
rubidium
parents: 9038
diff changeset
   308
				continue;
d21460a10105 (svn r13734) -Fix: NewGRF rail continuation would always mark a tunnel on the same axis as connected, even when the tunnel faces the wrong direction.
rubidium
parents: 9038
diff changeset
   309
			}
d21460a10105 (svn r13734) -Fix: NewGRF rail continuation would always mark a tunnel on the same axis as connected, even when the tunnel faces the wrong direction.
rubidium
parents: 9038
diff changeset
   310
5409
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   311
			/* If any track reaches our exit direction, set the bit in the lower byte */
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8571
diff changeset
   312
			if (trackbits & DiagdirReachesTracks(*diagdir)) SetBit(res, i);
5409
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   313
		}
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   314
	}
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   315
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   316
	return res;
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   317
}
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   318
725ef180e977 (svn r7606) -Codechange: [NewStations] Add support for variables 45 and 65.
peter1138
parents: 5380
diff changeset
   319
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   320
/* 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
   321
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
   322
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   323
	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
   324
	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
   325
	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
   326
}
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   327
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   328
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   329
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
   330
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   331
	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
   332
	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
   333
}
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
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
   337
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   338
	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
   339
	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
   340
	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
   341
}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   342
7230
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   343
/**
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   344
 * Station variable cache
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   345
 * This caches 'expensive' station variable lookups which iterate over
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   346
 * several tiles that may be called multiple times per Resolve().
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   347
 */
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   348
static struct {
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   349
	uint32 v40;
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   350
	uint32 v41;
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   351
	uint32 v45;
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   352
	uint32 v46;
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   353
	uint32 v47;
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   354
	uint32 v49;
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   355
	uint8 valid;
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   356
} _svc;
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   357
3893
ced26054ee00 (svn r4960) - NewGRF: explicitly handle unsupported variables, instead of returning -1.
peter1138
parents: 3834
diff changeset
   358
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
   359
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   360
	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
   361
	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
   362
6354
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   363
	if (object->scope == VSG_SCOPE_PARENT) {
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   364
		/* Pass the request on to the town of the station */
9726
7cc08b5b0787 (svn r13851) -Fix (r9393): GetTownByTile() is only valid for houses and roads.
frosch
parents: 9714
diff changeset
   365
		const Town *t;
6354
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   366
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   367
		if (st != NULL) {
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   368
			t = st->town;
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   369
		} else if (tile != INVALID_TILE) {
9726
7cc08b5b0787 (svn r13851) -Fix (r9393): GetTownByTile() is only valid for houses and roads.
frosch
parents: 9714
diff changeset
   370
			t = ClosestTownFromTile(tile, UINT_MAX);
6354
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   371
		} else {
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   372
			*available = false;
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   373
			return UINT_MAX;
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   374
		}
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   375
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   376
		return TownGetVariable(variable, parameter, available, t);
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   377
	}
584c63c77226 (svn r9393) -Codechange: Use newgrf town resolver as station parent scope handler
peter1138
parents: 6348
diff changeset
   378
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   379
	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
   380
		/* 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
   381
		switch (variable) {
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   382
			case 0x40:
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   383
			case 0x41:
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   384
			case 0x46:
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   385
			case 0x47:
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10099
diff changeset
   386
			case 0x49: return 0x2110000;        // Platforms, tracks & position
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10099
diff changeset
   387
			case 0x42: return 0;                // Rail type (XXX Get current type from GUI?)
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10099
diff changeset
   388
			case 0x43: return _current_company; // Station owner
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10099
diff changeset
   389
			case 0x44: return 2;                // PBS status
7922
a7e266f966d9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 7642
diff changeset
   390
			case 0xFA: return Clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Build date, clamped to a 16 bit value
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   391
		}
3893
ced26054ee00 (svn r4960) - NewGRF: explicitly handle unsupported variables, instead of returning -1.
peter1138
parents: 3834
diff changeset
   392
ced26054ee00 (svn r4960) - NewGRF: explicitly handle unsupported variables, instead of returning -1.
peter1138
parents: 3834
diff changeset
   393
		*available = false;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   394
		return UINT_MAX;
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   395
	}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   396
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   397
	switch (variable) {
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   398
		/* Calculated station variables */
7230
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   399
		case 0x40:
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   400
			if (!HasBit(_svc.valid, 0)) { _svc.v40 = GetPlatformInfoHelper(tile, false, false, false); SetBit(_svc.valid, 0); }
7230
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   401
			return _svc.v40;
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   402
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   403
		case 0x41:
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   404
			if (!HasBit(_svc.valid, 1)) { _svc.v41 = GetPlatformInfoHelper(tile, true,  false, false); SetBit(_svc.valid, 1); }
7230
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   405
			return _svc.v41;
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   406
6849
e1b7913352f8 (svn r10089) -Fix (r10040): Use GetTileZ instead of GetClearGround in GetTerrainType as
maedhros
parents: 6683
diff changeset
   407
		case 0x42: return GetTerrainType(tile) | (GetRailType(tile) << 8);
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6343
diff changeset
   408
		case 0x43: return st->owner; // Station owner
9823
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9750
diff changeset
   409
		case 0x44:
10099
a42cafcba550 (svn r14280) -Codechange: use IsRailWaypointTile() instead of IsTileType() and IsRailWaypoint() checks at several places
smatz
parents: 9823
diff changeset
   410
			if (IsRailWaypointTile(tile)) {
9823
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9750
diff changeset
   411
				return GetDepotWaypointReservation(tile) ? 7 : 4;
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9750
diff changeset
   412
			} else {
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9750
diff changeset
   413
				return GetRailwayStationReservation(tile) ? 7 : 4; // PBS status
352b11cff200 (svn r13965) -Add [YAPP]: Implement newgrf var 0x44 for stations (PBS reservation state). (michi_cc)
rubidium
parents: 9750
diff changeset
   414
			}
7230
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   415
		case 0x45:
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   416
			if (!HasBit(_svc.valid, 2)) { _svc.v45 = GetRailContinuationInfo(tile); SetBit(_svc.valid, 2); }
7230
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   417
			return _svc.v45;
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   418
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   419
		case 0x46:
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   420
			if (!HasBit(_svc.valid, 3)) { _svc.v46 = GetPlatformInfoHelper(tile, false, false, true); SetBit(_svc.valid, 3); }
7230
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   421
			return _svc.v46;
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   422
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   423
		case 0x47:
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   424
			if (!HasBit(_svc.valid, 4)) { _svc.v47 = GetPlatformInfoHelper(tile, true,  false, true); SetBit(_svc.valid, 4); }
7230
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   425
			return _svc.v47;
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   426
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6343
diff changeset
   427
		case 0x48: { // Accepted cargo types
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   428
			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
   429
			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
   430
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   431
			for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   432
				if (HasBit(st->goods[cargo_type].acceptance_pickup, GoodsEntry::PICKUP)) SetBit(value, cargo_type);
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   433
			}
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   434
			return value;
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   435
		}
7230
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   436
		case 0x49:
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   437
			if (!HasBit(_svc.valid, 5)) { _svc.v49 = GetPlatformInfoHelper(tile, false, true, false); SetBit(_svc.valid, 5); }
7230
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   438
			return _svc.v49;
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   439
8956
359bcdf14389 (svn r12747) -Codechange: Add varaction retrieval of station animation frame
peter1138
parents: 8906
diff changeset
   440
		case 0x4A: // Animation frame of tile
359bcdf14389 (svn r12747) -Codechange: Add varaction retrieval of station animation frame
peter1138
parents: 8906
diff changeset
   441
			return GetStationAnimationFrame(tile);
359bcdf14389 (svn r12747) -Codechange: Add varaction retrieval of station animation frame
peter1138
parents: 8906
diff changeset
   442
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   443
		/* Variables which use the parameter */
6460
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   444
		/* Variables 0x60 to 0x65 are handled separately below */
8956
359bcdf14389 (svn r12747) -Codechange: Add varaction retrieval of station animation frame
peter1138
parents: 8906
diff changeset
   445
		case 0x66: // Animation frame of nearby tile
359bcdf14389 (svn r12747) -Codechange: Add varaction retrieval of station animation frame
peter1138
parents: 8906
diff changeset
   446
			if (parameter != 0) tile = GetNearbyTile(parameter, tile);
359bcdf14389 (svn r12747) -Codechange: Add varaction retrieval of station animation frame
peter1138
parents: 8906
diff changeset
   447
			return st->TileBelongsToRailStation(tile) ? GetStationAnimationFrame(tile) : UINT_MAX;
359bcdf14389 (svn r12747) -Codechange: Add varaction retrieval of station animation frame
peter1138
parents: 8906
diff changeset
   448
359bcdf14389 (svn r12747) -Codechange: Add varaction retrieval of station animation frame
peter1138
parents: 8906
diff changeset
   449
		case 0x67: { // Land info of nearby tile
8118
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   450
			Axis axis = GetRailStationAxis(tile);
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   451
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   452
			if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   453
8458
38fe72ff1402 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8316
diff changeset
   454
			Slope tileh = GetTileSlope(tile, NULL);
38fe72ff1402 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8316
diff changeset
   455
			bool swap = (axis == AXIS_Y && HasBit(tileh, SLOPE_W) != HasBit(tileh, SLOPE_E));
38fe72ff1402 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8316
diff changeset
   456
38fe72ff1402 (svn r12028) -Codechange: Split common part of station var 0x67, house var 0x62, indtile var 0x60 and industry var 0x62 to 'newgrf_commons.cpp'.
frosch
parents: 8316
diff changeset
   457
			return GetNearbyTileInformation(tile) ^ (swap ? SLOPE_EW : 0);
8118
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   458
		}
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   459
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   460
		case 0x68: { // Station info of nearby tiles
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   461
			TileIndex nearby_tile = GetNearbyTile(parameter, tile);
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   462
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   463
			if (!IsRailwayStationTile(nearby_tile)) return 0xFFFFFFFF;
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   464
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   465
			uint32 grfid = st->speclist[GetCustomStationSpecIndex(tile)].grfid;
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   466
			bool perpendicular = GetRailStationAxis(tile) != GetRailStationAxis(nearby_tile);
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   467
			bool same_station = st->TileBelongsToRailStation(nearby_tile);
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   468
			uint32 res = GB(GetStationGfx(nearby_tile), 1, 2) << 12 | !!perpendicular << 11 | !!same_station << 10;
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   469
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   470
			if (IsCustomStationSpecIndex(nearby_tile)) {
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   471
				const StationSpecList ssl = GetStationByTile(nearby_tile)->speclist[GetCustomStationSpecIndex(nearby_tile)];
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   472
				res |= 1 << (ssl.grfid != grfid ? 9 : 8) | ssl.localidx;
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   473
			}
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   474
			return res;
fa5fe5491657 (svn r11679) -Add: [newgrf] support for station vars 67 and 68
glx
parents: 7931
diff changeset
   475
		}
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   476
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   477
		/* 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
   478
		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
   479
		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
   480
		case 0x86: return 0;
3746
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   481
		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
   482
		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
   483
		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
   484
		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
   485
		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
   486
		case 0xF6: return st->airport_flags;
5172
285d1ab82403 (svn r7282) -Fix (r625): ttdpatch vars are little endian
peter1138
parents: 4817
diff changeset
   487
		case 0xF7: return GB(st->airport_flags, 8, 8);
7922
a7e266f966d9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 7642
diff changeset
   488
		case 0xFA: return Clamp(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   489
	}
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   490
6460
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   491
	/* Handle cargo variables with parameter, 0x60 to 0x65 */
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   492
	if (variable >= 0x60 && variable <= 0x65) {
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   493
		CargoID c = GetCargoTranslation(parameter, object->u.station.statspec->grffile);
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   494
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   495
		if (c == CT_INVALID) return 0;
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   496
		const GoodsEntry *ge = &st->goods[c];
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   497
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   498
		switch (variable) {
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6849
diff changeset
   499
			case 0x60: return min(ge->cargo.Count(), 4095);
6460
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   500
			case 0x61: return ge->days_since_pickup;
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   501
			case 0x62: return ge->rating;
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6849
diff changeset
   502
			case 0x63: return ge->cargo.DaysInTransit();
6460
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   503
			case 0x64: return ge->last_speed | (ge->last_age << 8);
7474
1daa825ba893 (svn r10981) -Fix [FS#1156] (r10970): stations gave ratings for accepted cargo too.
rubidium
parents: 7327
diff changeset
   504
			case 0x65: return GB(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1) << 3;
6460
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   505
		}
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   506
	}
705fbddbf3dc (svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
peter1138
parents: 6451
diff changeset
   507
3746
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   508
	/* Handle cargo variables (deprecated) */
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   509
	if (variable >= 0x8C && variable <= 0xEC) {
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   510
		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
   511
		switch (GB(variable - 0x8C, 0, 3)) {
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6849
diff changeset
   512
			case 0: return g->cargo.Count();
7474
1daa825ba893 (svn r10981) -Fix [FS#1156] (r10970): stations gave ratings for accepted cargo too.
rubidium
parents: 7327
diff changeset
   513
			case 1: return GB(min(g->cargo.Count(), 4095), 0, 4) | (GB(g->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1) << 7);
3746
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   514
			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
   515
			case 3: return g->rating;
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6849
diff changeset
   516
			case 4: return g->cargo.Source();
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6849
diff changeset
   517
			case 5: return g->cargo.DaysInTransit();
3746
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   518
			case 6: return g->last_speed;
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   519
			case 7: return g->last_age;
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   520
		}
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   521
	}
17a40e29c579 (svn r4734) - Newstations: add more variables and fix a division by zero.
peter1138
parents: 3743
diff changeset
   522
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5172
diff changeset
   523
	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
   524
3893
ced26054ee00 (svn r4960) - NewGRF: explicitly handle unsupported variables, instead of returning -1.
peter1138
parents: 3834
diff changeset
   525
	*available = false;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   526
	return UINT_MAX;
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   527
}
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   528
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   529
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   530
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
   531
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   532
	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
   533
	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
   534
	uint set;
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   535
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   536
	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
   537
	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
   538
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   539
	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
   540
		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
   541
	}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   542
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
   543
	switch (cargo_type) {
6148
5247b66aad19 (svn r8891) -Codechange: Remove remains of global cargo scheme. All cargo mapping is now dealt with only in NewGRF code, on load where possible.
peter1138
parents: 6122
diff changeset
   544
		case CT_INVALID:
5247b66aad19 (svn r8891) -Codechange: Remove remains of global cargo scheme. All cargo mapping is now dealt with only in NewGRF code, on load where possible.
peter1138
parents: 6122
diff changeset
   545
		case CT_DEFAULT_NA:
5247b66aad19 (svn r8891) -Codechange: Remove remains of global cargo scheme. All cargo mapping is now dealt with only in NewGRF code, on load where possible.
peter1138
parents: 6122
diff changeset
   546
		case CT_PURCHASE:
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
   547
			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
   548
			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
   549
6148
5247b66aad19 (svn r8891) -Codechange: Remove remains of global cargo scheme. All cargo mapping is now dealt with only in NewGRF code, on load where possible.
peter1138
parents: 6122
diff changeset
   550
		case CT_DEFAULT:
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
   551
			for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6849
diff changeset
   552
				cargo += st->goods[cargo_type].cargo.Count();
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
   553
			}
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
   554
			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
   555
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
   556
		default:
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6849
diff changeset
   557
			cargo = st->goods[cargo_type].cargo.Count();
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
   558
			break;
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   559
	}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   560
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   561
	if (HasBit(statspec->flags, 1)) cargo /= (st->trainst_w + st->trainst_h);
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   562
	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
   563
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   564
	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
   565
		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
   566
			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
   567
			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
   568
		}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   569
	} else {
3749
b6c1a611d840 (svn r4737) - Newstations: 'real' groups picked the wrong set of loading or loaded sprites
peter1138
parents: 3748
diff changeset
   570
		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
   571
			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
   572
			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
   573
		}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   574
	}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   575
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   576
	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
   577
}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   578
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   579
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   580
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
   581
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   582
	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
   583
	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
   584
	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
   585
	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
   586
	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
   587
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   588
	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
   589
	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
   590
	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
   591
7327
d810677fb909 (svn r10690) -Codechange: use the enum that describes all callback IDs in favor of "just" using an untyped integer.
rubidium
parents: 7272
diff changeset
   592
	res->callback        = CBID_NO_CALLBACK;
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   593
	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
   594
	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
   595
	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
   596
	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
   597
	res->reseed          = 0;
8756
b6f8dc6a246f (svn r12452) -Feature: [NewGRF] Add random action 2 type 84. For vehicles only.
glx
parents: 8695
diff changeset
   598
	res->count           = 0;
9750
7114408dd1fa (svn r13885) -Fix [FS#2168]: Var 0x7F is not feature-specific.
frosch
parents: 9726
diff changeset
   599
	res->grffile         = (statspec != NULL ? statspec->grffile : NULL);
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   600
}
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   601
5849
58a3d90e7ee1 (svn r8423) -Fix
tron
parents: 5825
diff changeset
   602
static const SpriteGroup *ResolveStation(ResolverObject *object)
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
   603
{
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
   604
	const SpriteGroup *group;
6148
5247b66aad19 (svn r8891) -Codechange: Remove remains of global cargo scheme. All cargo mapping is now dealt with only in NewGRF code, on load where possible.
peter1138
parents: 6122
diff changeset
   605
	CargoID ctype = CT_DEFAULT_NA;
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
   606
5849
58a3d90e7ee1 (svn r8423) -Fix
tron
parents: 5825
diff changeset
   607
	if (object->u.station.st == NULL) {
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
   608
		/* No station, so we are in a purchase list */
6148
5247b66aad19 (svn r8891) -Codechange: Remove remains of global cargo scheme. All cargo mapping is now dealt with only in NewGRF code, on load where possible.
peter1138
parents: 6122
diff changeset
   609
		ctype = CT_PURCHASE;
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
   610
	} 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
   611
		/* Pick the first cargo that we have waiting */
6113
a10ede03ac58 (svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents: 6106
diff changeset
   612
		for (CargoID cargo = 0; cargo < NUM_CARGO; cargo++) {
a10ede03ac58 (svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents: 6106
diff changeset
   613
			const CargoSpec *cs = GetCargo(cargo);
6148
5247b66aad19 (svn r8891) -Codechange: Remove remains of global cargo scheme. All cargo mapping is now dealt with only in NewGRF code, on load where possible.
peter1138
parents: 6122
diff changeset
   614
			if (cs->IsValid() && object->u.station.statspec->spritegroup[cargo] != NULL &&
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6849
diff changeset
   615
					!object->u.station.st->goods[cargo].cargo.Empty()) {
6148
5247b66aad19 (svn r8891) -Codechange: Remove remains of global cargo scheme. All cargo mapping is now dealt with only in NewGRF code, on load where possible.
peter1138
parents: 6122
diff changeset
   616
				ctype = cargo;
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
   617
				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
   618
			}
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
   619
		}
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
   620
	}
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
   621
5849
58a3d90e7ee1 (svn r8423) -Fix
tron
parents: 5825
diff changeset
   622
	group = object->u.station.statspec->spritegroup[ctype];
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
   623
	if (group == NULL) {
6148
5247b66aad19 (svn r8891) -Codechange: Remove remains of global cargo scheme. All cargo mapping is now dealt with only in NewGRF code, on load where possible.
peter1138
parents: 6122
diff changeset
   624
		ctype = CT_DEFAULT;
5849
58a3d90e7ee1 (svn r8423) -Fix
tron
parents: 5825
diff changeset
   625
		group = object->u.station.statspec->spritegroup[ctype];
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
   626
	}
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
   627
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
   628
	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
   629
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
   630
	/* 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
   631
	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
   632
7230
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   633
	/* Invalidate all cached vars */
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   634
	_svc.valid = 0;
1da1567909fc (svn r10509) -Codechange: cache expensive newgrf station variables during
peter1138
parents: 7192
diff changeset
   635
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
   636
	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
   637
}
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   638
3751
ce6408fdb788 (svn r4739) - Newstations: remove cargo type parameter of GetCustomStationRelocation() as we can determine it internally
peter1138
parents: 3750
diff changeset
   639
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
   640
{
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   641
	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
   642
	ResolverObject object;
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   643
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   644
	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
   645
5849
58a3d90e7ee1 (svn r8423) -Fix
tron
parents: 5825
diff changeset
   646
	group = ResolveStation(&object);
3743
9eb8bdd90532 (svn r4729) - Newstations: replace old station resolver with new resolver code (as has happened for vehicles)
peter1138
parents: 3741
diff changeset
   647
	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
   648
	return group->g.result.sprite - 0x42D;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   649
}
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   650
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   651
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   652
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
   653
{
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   654
	const SpriteGroup *group;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   655
	ResolverObject object;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   656
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   657
	NewStationResolver(&object, statspec, st, tile);
6348
6dd01da7a02b (svn r9385) -Cleanup: doxygen changes. Today, we are exploring the letter N.
belugas
parents: 6343
diff changeset
   658
	object.callback_param1 = 1; // Indicate we are resolving the ground sprite
3775
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   659
5849
58a3d90e7ee1 (svn r8423) -Fix
tron
parents: 5825
diff changeset
   660
	group = ResolveStation(&object);
3775
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   661
	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
   662
	return group->g.result.sprite - 0x42D;
2967
77b04531d7a0 (svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents: 2963
diff changeset
   663
}
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   664
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   665
7327
d810677fb909 (svn r10690) -Codechange: use the enum that describes all callback IDs in favor of "just" using an untyped integer.
rubidium
parents: 7272
diff changeset
   666
uint16 GetStationCallback(CallbackID callback, uint32 param1, uint32 param2, const StationSpec *statspec, const Station *st, TileIndex tile)
3752
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   667
{
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   668
	const SpriteGroup *group;
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   669
	ResolverObject object;
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   670
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   671
	NewStationResolver(&object, statspec, st, tile);
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   672
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   673
	object.callback        = callback;
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   674
	object.callback_param1 = param1;
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   675
	object.callback_param2 = param2;
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   676
5849
58a3d90e7ee1 (svn r8423) -Fix
tron
parents: 5825
diff changeset
   677
	group = ResolveStation(&object);
3752
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   678
	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
   679
	return group->g.callback.result;
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   680
}
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   681
7c28dc5a1698 (svn r4740) - Newstations: add function to evaluate station callbacks
peter1138
parents: 3751
diff changeset
   682
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   683
/**
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   684
 * Allocate a StationSpec to a Station. This is called once per build operation.
6481
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6460
diff changeset
   685
 * @param statspec StationSpec to allocate.
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   686
 * @param st Station to allocate it to.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   687
 * @param exec Whether to actually allocate the spec.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   688
 * @return Index within the Station's spec list, or -1 if the allocation failed.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   689
 */
3676
680dfa6336a1 (svn r4593) CodeChange : Renamed spec (when using StationSpec) to statspec.
belugas
parents: 3668
diff changeset
   690
int AllocateSpecToStation(const StationSpec *statspec, Station *st, bool exec)
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   691
{
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   692
	uint i;
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   693
9038
38f0f10f8cca (svn r12857) -Fix [FS#1948]: remove the last uses of AutoPtr in the station code.
rubidium
parents: 9018
diff changeset
   694
	if (statspec == NULL || st == NULL) return 0;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   695
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   696
	/* 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
   697
	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
   698
		if (st->speclist[i].spec == statspec) return i;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   699
	}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   700
3786
ac7ff031ff5f (svn r4782) - Newstations: fix issue with saving/loading games with newstations on big endian architectures.
peter1138
parents: 3780
diff changeset
   701
	for (i = 1; i < st->num_specs && i < MAX_SPECLIST; i++) {
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   702
		if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break;
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   703
	}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   704
3786
ac7ff031ff5f (svn r4782) - Newstations: fix issue with saving/loading games with newstations on big endian architectures.
peter1138
parents: 3780
diff changeset
   705
	if (i == MAX_SPECLIST) return -1;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   706
3730
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   707
	if (exec) {
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   708
		if (i >= st->num_specs) {
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   709
			st->num_specs = i + 1;
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
   710
			st->speclist = ReallocT(st->speclist, st->num_specs);
3730
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   711
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   712
			if (st->num_specs == 2) {
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   713
				/* Initial allocation */
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   714
				st->speclist[0].spec     = NULL;
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   715
				st->speclist[0].grfid    = 0;
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   716
				st->speclist[0].localidx = 0;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   717
			}
3730
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   718
		}
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   719
3730
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   720
		st->speclist[i].spec     = statspec;
6451
e576c71bfc09 (svn r9601) -Codechange: Store grf file reference in station spec, not just GRF ID
peter1138
parents: 6357
diff changeset
   721
		st->speclist[i].grfid    = statspec->grffile->grfid;
3730
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   722
		st->speclist[i].localidx = statspec->localidx;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   723
	}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   724
3730
cb7f3617bc4a (svn r4707) - NewGRF: minor code duffage; return early and less indentation.
peter1138
parents: 3676
diff changeset
   725
	return i;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   726
}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   727
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   728
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   729
/** Deallocate a StationSpec from a Station. Called when removing a single station tile.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   730
 * @param st Station to work with.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   731
 * @param specindex Index of the custom station within the Station's spec list.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   732
 * @return Indicates whether the StationSpec was deallocated.
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   733
 */
4190
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   734
void DeallocateSpecFromStation(Station* st, byte specindex)
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   735
{
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   736
	/* 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
   737
	if (specindex == 0) return;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   738
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   739
	/* Check all tiles over the station to check if the specindex is still in use */
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   740
	BEGIN_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile) {
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   741
		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
   742
			return;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   743
		}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   744
	} END_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile)
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   745
4190
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   746
	/* 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
   747
	st->speclist[specindex].spec     = NULL;
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   748
	st->speclist[specindex].grfid    = 0;
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   749
	st->speclist[specindex].localidx = 0;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   750
4190
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   751
	/* 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
   752
	if (specindex == st->num_specs - 1) {
8695
6607e9b9ffe2 (svn r12368) -Codechange: use explicit body for loops and conditions and remove -Wno-empty-body from the configure script
smatz
parents: 8616
diff changeset
   753
		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
   754
4190
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   755
		if (st->num_specs > 1) {
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
   756
			st->speclist = ReallocT(st->speclist, st->num_specs);
4190
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   757
		} else {
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   758
			free(st->speclist);
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   759
			st->num_specs = 0;
2acef650361f (svn r5632) Replace a boolean variable in DeallocateSpecFromStation() by a return
tron
parents: 4182
diff changeset
   760
			st->speclist  = NULL;
9003
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   761
			st->cached_anim_triggers = 0;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   762
			return;
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   763
		}
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   764
	}
9003
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   765
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   766
	StationUpdateAnimTriggers(st);
3587
c434d5c3dcaa (svn r4473) - Newstations:
peter1138
parents: 3573
diff changeset
   767
}
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
   768
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
   769
/** Draw representation of a station tile for GUI purposes.
6481
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6460
diff changeset
   770
 * @param x Position x of image.
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6460
diff changeset
   771
 * @param y Position y of image.
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6460
diff changeset
   772
 * @param axis Axis.
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
   773
 * @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
   774
 * @param sclass, station Type of station.
6481
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6460
diff changeset
   775
 * @param station station ID
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
   776
 * @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
   777
 */
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
   778
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
   779
{
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
   780
	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
   781
	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
   782
	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
   783
	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
   784
	SpriteID relocation;
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5609
diff changeset
   785
	SpriteID image;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10099
diff changeset
   786
	SpriteID palette = COMPANY_SPRITE_COLOR(_local_company);
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
   787
	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
   788
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
   789
	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
   790
	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
   791
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
   792
	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
   793
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   794
	if (HasBit(statspec->callbackmask, CBM_STATION_SPRITE_LAYOUT)) {
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
   795
		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
   796
		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
   797
	}
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
   798
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
   799
	if (statspec->renderdata == NULL) {
7272
d47fc9e22d1c (svn r10601) -Codechange: store (and use) the type of stations instead of hardcoding station types by graphics IDs.
rubidium
parents: 7230
diff changeset
   800
		sprites = GetStationTileLayout(STATION_RAIL, tile + axis);
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
   801
	} else {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   802
		sprites = &statspec->renderdata[(tile < statspec->tiles) ? tile + axis : (uint)axis];
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
   803
	}
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
   804
8571
53c94d717e99 (svn r12149) -Codechange: Merge 'ground_sprite' and 'ground_pal' of DrawTileSprites into one PalSpriteID
frosch
parents: 8570
diff changeset
   805
	image = sprites->ground.sprite;
10355
8a930759b457 (svn r14606) -Codechange: Unify usage of PALETTE_MODIFIER_TRANSPARENT and PALETTE_MODIFIER_COLOR in spritelayout drawing.
frosch
parents: 10256
diff changeset
   806
	SpriteID pal = sprites->ground.pal;
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   807
	if (HasBit(image, SPRITE_MODIFIER_USE_OFFSET)) {
3775
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   808
		image += GetCustomStationGroundRelocation(statspec, NULL, INVALID_TILE);
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   809
		image += rti->custom_ground_offset;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   810
	} else {
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   811
		image += rti->total_offset;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   812
	}
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
   813
10355
8a930759b457 (svn r14606) -Codechange: Unify usage of PALETTE_MODIFIER_TRANSPARENT and PALETTE_MODIFIER_COLOR in spritelayout drawing.
frosch
parents: 10256
diff changeset
   814
	DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
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
   815
10256
d69ddb1a8422 (svn r14487) -Fix: The station picker preview did not draw child sprites.
frosch
parents: 10208
diff changeset
   816
	Point child_offset = {0, 0};
d69ddb1a8422 (svn r14487) -Fix: The station picker preview did not draw child sprites.
frosch
parents: 10208
diff changeset
   817
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
   818
	foreach_draw_tile_seq(seq, sprites->seq) {
8570
f50de4804ff1 (svn r12148) -Codechange: Merge 'image' and 'pal' of DrawTileSeqStruct into one PalSpriteID
frosch
parents: 8458
diff changeset
   819
		image = seq->image.sprite;
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   820
		if (HasBit(image, SPRITE_MODIFIER_USE_OFFSET)) {
3775
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   821
			image += rti->total_offset;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   822
		} else {
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   823
			image += relocation;
0fb5118536c3 (svn r4767) - Newstations: fix loading / use of custom ground sprites
peter1138
parents: 3766
diff changeset
   824
		}
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
   825
10355
8a930759b457 (svn r14606) -Codechange: Unify usage of PALETTE_MODIFIER_TRANSPARENT and PALETTE_MODIFIER_COLOR in spritelayout drawing.
frosch
parents: 10256
diff changeset
   826
		pal = SpriteLayoutPaletteTransform(image, seq->image.pal, palette);
8906
325b028994e9 (svn r12674) -Fix [FS#1902]: Colour remaps on station sprites only worked for company colours.
peter1138
parents: 8785
diff changeset
   827
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
   828
		if ((byte)seq->delta_z != 0x80) {
10256
d69ddb1a8422 (svn r14487) -Fix: The station picker preview did not draw child sprites.
frosch
parents: 10208
diff changeset
   829
			Point pt = RemapCoords(seq->delta_x, seq->delta_y, seq->delta_z);
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5609
diff changeset
   830
			DrawSprite(image, pal, x + pt.x, y + pt.y);
10256
d69ddb1a8422 (svn r14487) -Fix: The station picker preview did not draw child sprites.
frosch
parents: 10208
diff changeset
   831
d69ddb1a8422 (svn r14487) -Fix: The station picker preview did not draw child sprites.
frosch
parents: 10208
diff changeset
   832
			const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
d69ddb1a8422 (svn r14487) -Fix: The station picker preview did not draw child sprites.
frosch
parents: 10208
diff changeset
   833
			child_offset.x = pt.x + spr->x_offs;
d69ddb1a8422 (svn r14487) -Fix: The station picker preview did not draw child sprites.
frosch
parents: 10208
diff changeset
   834
			child_offset.y = pt.y + spr->y_offs;
d69ddb1a8422 (svn r14487) -Fix: The station picker preview did not draw child sprites.
frosch
parents: 10208
diff changeset
   835
		} else {
d69ddb1a8422 (svn r14487) -Fix: The station picker preview did not draw child sprites.
frosch
parents: 10208
diff changeset
   836
			/* For stations and original spritelayouts delta_x and delta_y are signed */
d69ddb1a8422 (svn r14487) -Fix: The station picker preview did not draw child sprites.
frosch
parents: 10208
diff changeset
   837
			DrawSprite(image, pal, x + child_offset.x + seq->delta_x, y + child_offset.y + seq->delta_y);
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
   838
		}
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
   839
	}
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
   840
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
   841
	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
   842
}
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
   843
4206
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   844
9714
5d785e184f2d (svn r13830) -Feature: show additional NewGRF info in the Tile Info window for stations, houses and industries
smatz
parents: 9662
diff changeset
   845
const StationSpec *GetStationSpec(TileIndex t)
4206
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   846
{
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   847
	const Station* st;
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   848
	uint specindex;
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   849
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   850
	if (!IsCustomStationSpecIndex(t)) return NULL;
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   851
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   852
	st = GetStationByTile(t);
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   853
	specindex = GetCustomStationSpecIndex(t);
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   854
	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
   855
}
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   856
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   857
3766
fc2dea6ef369 (svn r4758) - Newstations: add support for 'blocked' station tiles, which no train can pass.
peter1138
parents: 3765
diff changeset
   858
/* 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
   859
 * 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
   860
bool IsStationTileBlocked(TileIndex tile)
fc2dea6ef369 (svn r4758) - Newstations: add support for 'blocked' station tiles, which no train can pass.
peter1138
parents: 3765
diff changeset
   861
{
4206
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   862
	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
   863
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   864
	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
   865
}
3789
c50647c927e4 (svn r4785) - Newstations: don't draw catenary on non-track tiles
glx
parents: 3787
diff changeset
   866
c50647c927e4 (svn r4785) - Newstations: don't draw catenary on non-track tiles
glx
parents: 3787
diff changeset
   867
/* 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
   868
 * 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
   869
bool IsStationTileElectrifiable(TileIndex tile)
c50647c927e4 (svn r4785) - Newstations: don't draw catenary on non-track tiles
glx
parents: 3787
diff changeset
   870
{
4206
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   871
	const StationSpec* statspec = GetStationSpec(tile);
3789
c50647c927e4 (svn r4785) - Newstations: don't draw catenary on non-track tiles
glx
parents: 3787
diff changeset
   872
4206
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   873
	return
8f5e602cd2b1 (svn r5690) Factor common code to reduce code duplication
tron
parents: 4205
diff changeset
   874
		statspec == NULL ||
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   875
		HasBit(statspec->pylons, GetStationGfx(tile)) ||
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7922
diff changeset
   876
		!HasBit(statspec->wires, GetStationGfx(tile));
3789
c50647c927e4 (svn r4785) - Newstations: don't draw catenary on non-track tiles
glx
parents: 3787
diff changeset
   877
}
9003
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   878
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   879
void AnimateStationTile(TileIndex tile)
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   880
{
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   881
	const StationSpec *ss = GetStationSpec(tile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   882
	if (ss == NULL) return;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   883
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   884
	const Station *st = GetStationByTile(tile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   885
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   886
	uint8 animation_speed = ss->anim_speed;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   887
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   888
	if (HasBit(ss->callbackmask, CBM_STATION_ANIMATION_SPEED)) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   889
		uint16 callback = GetStationCallback(CBID_STATION_ANIMATION_SPEED, 0, 0, ss, st, tile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   890
		if (callback != CALLBACK_FAILED) animation_speed = Clamp(callback & 0xFF, 0, 16);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   891
	}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   892
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   893
	if (_tick_counter % (1 << animation_speed) != 0) return;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   894
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   895
	uint8 frame      = GetStationAnimationFrame(tile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   896
	uint8 num_frames = ss->anim_frames;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   897
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   898
	bool frame_set_by_callback = false;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   899
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   900
	if (HasBit(ss->callbackmask, CBM_STATION_ANIMATION_NEXT_FRAME)) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   901
		uint32 param = HasBit(ss->flags, 2) ? Random() : 0;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   902
		uint16 callback = GetStationCallback(CBID_STATION_ANIM_NEXT_FRAME, param, 0, ss, st, tile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   903
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   904
		if (callback != CALLBACK_FAILED) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   905
			frame_set_by_callback = true;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   906
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   907
			switch (callback & 0xFF) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   908
				case 0xFF:
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   909
					DeleteAnimatedTile(tile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   910
					break;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   911
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   912
				case 0xFE:
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   913
					frame_set_by_callback = false;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   914
					break;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   915
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   916
				default:
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   917
					frame = callback & 0xFF;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   918
					break;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   919
			}
9018
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
   920
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
   921
			/* If the lower 7 bits of the upper byte of the callback
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
   922
			 * result are not empty, it is a sound effect. */
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
   923
			if (GB(callback, 8, 7) != 0) PlayTileSound(ss->grffile, GB(callback, 8, 7), tile);
9003
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   924
		}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   925
	}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   926
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   927
	if (!frame_set_by_callback) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   928
		if (frame < num_frames) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   929
			frame++;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   930
		} else if (frame == num_frames && HasBit(ss->anim_status, 0)) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   931
			/* This animation loops, so start again from the beginning */
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   932
			frame = 0;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   933
		} else {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   934
			/* This animation doesn't loop, so stay here */
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   935
			DeleteAnimatedTile(tile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   936
		}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   937
	}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   938
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   939
	SetStationAnimationFrame(tile, frame);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   940
	MarkTileDirtyByTile(tile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   941
}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   942
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   943
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   944
static void ChangeStationAnimationFrame(const StationSpec *ss, const Station *st, TileIndex tile, uint16 random_bits, StatAnimTrigger trigger, CargoID cargo_type)
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   945
{
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   946
	uint16 callback = GetStationCallback(CBID_STATION_ANIM_START_STOP, (random_bits << 16) | Random(), (uint8)trigger | (cargo_type << 8), ss, st, tile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   947
	if (callback == CALLBACK_FAILED) return;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   948
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   949
	switch (callback & 0xFF) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   950
		case 0xFD: /* Do nothing. */         break;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   951
		case 0xFE: AddAnimatedTile(tile);    break;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   952
		case 0xFF: DeleteAnimatedTile(tile); break;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   953
		default:
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   954
			SetStationAnimationFrame(tile, callback);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   955
			AddAnimatedTile(tile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   956
			break;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   957
	}
9018
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
   958
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
   959
	/* If the lower 7 bits of the upper byte of the callback
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
   960
	 * result are not empty, it is a sound effect. */
403898e0b54e (svn r12817) -Feature: the ability to play NewGRF sounds for industries and stations.
rubidium
parents: 9006
diff changeset
   961
	if (GB(callback, 8, 7) != 0) PlayTileSound(ss->grffile, GB(callback, 8, 7), tile);
9003
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   962
}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   963
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   964
enum TriggerArea {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   965
	TA_TILE,
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   966
	TA_PLATFORM,
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   967
	TA_WHOLE,
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   968
};
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   969
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   970
struct TileArea {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   971
	TileIndex tile;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   972
	uint8 w;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   973
	uint8 h;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   974
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   975
	TileArea(const Station *st, TileIndex tile, TriggerArea ta)
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   976
	{
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   977
		switch (ta) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   978
			default: NOT_REACHED();
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   979
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   980
			case TA_TILE:
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   981
				this->tile = tile;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   982
				this->w    = 1;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   983
				this->h    = 1;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   984
				break;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   985
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   986
			case TA_PLATFORM: {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   987
				TileIndex start, end;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   988
				Axis axis = GetRailStationAxis(tile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   989
				TileIndexDiff delta = TileOffsByDiagDir(AxisToDiagDir(axis));
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   990
9004
0f9d87665433 (svn r12799) -Fix (r12798): Empty for-loop warnings from gcc 4.3+
peter1138
parents: 9003
diff changeset
   991
				for (end = tile; IsRailwayStationTile(end + delta) && IsCompatibleTrainStationTile(tile, end + delta); end += delta) { /* Nothing */ }
0f9d87665433 (svn r12799) -Fix (r12798): Empty for-loop warnings from gcc 4.3+
peter1138
parents: 9003
diff changeset
   992
				for (start = tile; IsRailwayStationTile(start - delta) && IsCompatibleTrainStationTile(tile, start - delta); start -= delta) { /* Nothing */ }
9003
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   993
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   994
				this->tile = start;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   995
				this->w = TileX(end) - TileX(start) + 1;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   996
				this->h = TileY(end) - TileY(start) + 1;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   997
				break;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   998
			}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
   999
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1000
			case TA_WHOLE:
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1001
				this->tile = st->train_tile;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1002
				this->w    = st->trainst_w + 1;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1003
				this->h    = st->trainst_h + 1;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1004
				break;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1005
		}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1006
	}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1007
};
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1008
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1009
void StationAnimationTrigger(const Station *st, TileIndex tile, StatAnimTrigger trigger, CargoID cargo_type)
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1010
{
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1011
	/* List of coverage areas for each animation trigger */
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1012
	static const TriggerArea tas[] = {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1013
		TA_TILE, TA_WHOLE, TA_WHOLE, TA_PLATFORM, TA_PLATFORM, TA_PLATFORM, TA_WHOLE
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1014
	};
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1015
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1016
	/* Get Station if it wasn't supplied */
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1017
	if (st == NULL) st = GetStationByTile(tile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1018
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1019
	/* Check the cached animation trigger bitmask to see if we need
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1020
	 * to bother with any further processing. */
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1021
	if (!HasBit(st->cached_anim_triggers, trigger)) return;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1022
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1023
	uint16 random_bits = Random();
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1024
	TileArea area = TileArea(st, tile, tas[trigger]);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1025
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1026
	for (uint y = 0; y < area.h; y++) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1027
		for (uint x = 0; x < area.w; x++) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1028
			if (st->TileBelongsToRailStation(area.tile)) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1029
				const StationSpec *ss = GetStationSpec(area.tile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1030
				if (ss != NULL && HasBit(ss->anim_triggers, trigger)) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1031
					CargoID cargo;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1032
					if (cargo_type == CT_INVALID) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1033
						cargo = CT_INVALID;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1034
					} else {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1035
						cargo = GetReverseCargoTranslation(cargo_type, ss->grffile);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1036
					}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1037
					ChangeStationAnimationFrame(ss, st, area.tile, random_bits, trigger, cargo);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1038
				}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1039
			}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1040
			area.tile += TileDiffXY(1, 0);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1041
		}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1042
		area.tile += TileDiffXY(-area.w, 1);
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1043
	}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1044
}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1045
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1046
/**
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1047
 * Update the cached animation trigger bitmask for a station.
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1048
 * @param st Station to update.
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1049
 */
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1050
void StationUpdateAnimTriggers(Station *st)
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1051
{
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1052
	st->cached_anim_triggers = 0;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1053
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1054
	/* Combine animation trigger bitmask for all station specs
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1055
	 * of this station. */
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1056
	for (uint i = 0; i < st->num_specs; i++) {
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1057
		const StationSpec *ss = st->speclist[i].spec;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1058
		if (ss != NULL) st->cached_anim_triggers |= ss->anim_triggers;
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1059
	}
7972fa66d9c9 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138
parents: 8956
diff changeset
  1060
}