author | peter1138 |
Sun, 23 Apr 2006 18:37:06 +0000 | |
changeset 3642 | 8da309b5fd2b |
parent 3594 | 3c4e4feea80a |
child 3668 | 42325f12e7d8 |
permissions | -rw-r--r-- |
2625
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
1 |
/* $Id$ */ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
2 |
|
2963
f28ce0549513
(svn r3525) - Rename station_newgrf.[ch] to newgrf_station.[ch], and update project files.
peter1138
parents:
2625
diff
changeset
|
3 |
/** @file newgrf_station.c Functions for dealing with station classes and custom stations. */ |
2625
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
4 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
5 |
#include "stdafx.h" |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
6 |
#include "openttd.h" |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
7 |
#include "debug.h" |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
8 |
#include "sprite.h" |
3594
3c4e4feea80a
(svn r4484) - Newstations: Use StringIDs instead of char*s to reference our custom names.
peter1138
parents:
3587
diff
changeset
|
9 |
#include "table/strings.h" |
2967
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
10 |
#include "station.h" |
3587 | 11 |
#include "station_map.h" |
2963
f28ce0549513
(svn r3525) - Rename station_newgrf.[ch] to newgrf_station.[ch], and update project files.
peter1138
parents:
2625
diff
changeset
|
12 |
#include "newgrf_station.h" |
2625
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
13 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
14 |
static StationClass station_classes[STAT_CLASS_MAX]; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
15 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
16 |
/** |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
17 |
* Reset station classes to their default state. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
18 |
* 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
|
19 |
* entry, for standard stations and waypoints. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
20 |
*/ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
21 |
void ResetStationClasses(void) |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
22 |
{ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
23 |
StationClassID i; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
24 |
for (i = 0; i < STAT_CLASS_MAX; i++) { |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
25 |
station_classes[i].id = 0; |
3594
3c4e4feea80a
(svn r4484) - Newstations: Use StringIDs instead of char*s to reference our custom names.
peter1138
parents:
3587
diff
changeset
|
26 |
station_classes[i].name = STR_EMPTY; |
2625
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
27 |
station_classes[i].stations = 0; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
28 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
29 |
free(station_classes[i].spec); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
30 |
station_classes[i].spec = NULL; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
31 |
} |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
32 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
33 |
// Set up initial data |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
34 |
station_classes[0].id = 'DFLT'; |
3594
3c4e4feea80a
(svn r4484) - Newstations: Use StringIDs instead of char*s to reference our custom names.
peter1138
parents:
3587
diff
changeset
|
35 |
station_classes[0].name = STR_STAT_CLASS_DFLT; |
2625
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
36 |
station_classes[0].stations = 1; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
37 |
station_classes[0].spec = malloc(sizeof(*station_classes[0].spec)); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
38 |
station_classes[0].spec[0] = NULL; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
39 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
40 |
station_classes[1].id = 'WAYP'; |
3594
3c4e4feea80a
(svn r4484) - Newstations: Use StringIDs instead of char*s to reference our custom names.
peter1138
parents:
3587
diff
changeset
|
41 |
station_classes[1].name = STR_STAT_CLASS_WAYP; |
2625
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
42 |
station_classes[1].stations = 1; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
43 |
station_classes[1].spec = malloc(sizeof(*station_classes[1].spec)); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
44 |
station_classes[1].spec[0] = NULL; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
45 |
} |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
46 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
47 |
/** |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
48 |
* Allocate a station class for the given class id. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
49 |
* @param classid A 32 bit value identifying the class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
50 |
* @return Index into station_classes of allocated class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
51 |
*/ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
52 |
StationClassID AllocateStationClass(uint32 class) |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
53 |
{ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
54 |
StationClassID i; |
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 |
for (i = 0; i < STAT_CLASS_MAX; i++) { |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
57 |
if (station_classes[i].id == class) { |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
58 |
// ClassID is already allocated, so reuse it. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
59 |
return i; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
60 |
} else if (station_classes[i].id == 0) { |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
61 |
// This class is empty, so allocate it to the ClassID. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
62 |
station_classes[i].id = class; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
63 |
return i; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
64 |
} |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
65 |
} |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
66 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
67 |
DEBUG(grf, 2)("StationClassAllocate: Already allocated %d classes, using default.", STAT_CLASS_MAX); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
68 |
return STAT_CLASS_DFLT; |
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 |
|
3642
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
71 |
/** Set the name of a custom station class */ |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
72 |
void SetStationClassName(StationClassID sclass, StringID name) |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
73 |
{ |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
74 |
assert(sclass < STAT_CLASS_MAX); |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
75 |
station_classes[sclass].name = name; |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
76 |
} |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
77 |
|
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
78 |
/** Retrieve the name of a custom station class */ |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
79 |
StringID GetStationClassName(StationClassID sclass) |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
80 |
{ |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
81 |
assert(sclass < STAT_CLASS_MAX); |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
82 |
return station_classes[sclass].name; |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
83 |
} |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
84 |
|
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
85 |
/** Build a list of station class name StringIDs to use in a dropdown list |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
86 |
* @return Pointer to a (static) array of StringIDs |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
87 |
*/ |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
88 |
StringID *BuildStationClassDropdown(void) |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
89 |
{ |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
90 |
/* Allow room for all station classes, plus a terminator entry */ |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
91 |
static StringID names[STAT_CLASS_MAX + 1]; |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
92 |
uint i; |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
93 |
|
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
94 |
/* Add each name */ |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
95 |
for (i = 0; i < STAT_CLASS_MAX && station_classes[i].id != 0; i++) { |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
96 |
names[i] = station_classes[i].name; |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
97 |
} |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
98 |
/* Terminate the list */ |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
99 |
names[i] = INVALID_STRING_ID; |
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
100 |
|
8da309b5fd2b
(svn r4551) - NewGRF: add string handling for newstations.
peter1138
parents:
3594
diff
changeset
|
101 |
return names; |
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 |
|
2625
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
104 |
/** |
3587 | 105 |
* Get the number of station classes in use. |
106 |
* @return Number of station classes. |
|
107 |
*/ |
|
108 |
uint GetNumStationClasses(void) |
|
109 |
{ |
|
110 |
uint i; |
|
111 |
for (i = 0; i < STAT_CLASS_MAX && station_classes[i].id != 0; i++); |
|
112 |
return i; |
|
113 |
} |
|
114 |
||
115 |
/** |
|
2625
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
116 |
* 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
|
117 |
* @param sclass Index of the station class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
118 |
* @return Number of stations in the class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
119 |
*/ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
120 |
uint GetNumCustomStations(StationClassID sclass) |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
121 |
{ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
122 |
assert(sclass < STAT_CLASS_MAX); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
123 |
return station_classes[sclass].stations; |
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 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
126 |
/** |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
127 |
* Tie a station spec to its station class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
128 |
* @param spec The station spec. |
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 |
void SetCustomStation(StationSpec *spec) |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
131 |
{ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
132 |
StationClass *station_class; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
133 |
int i; |
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 |
assert(spec->sclass < STAT_CLASS_MAX); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
136 |
station_class = &station_classes[spec->sclass]; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
137 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
138 |
i = station_class->stations++; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
139 |
station_class->spec = realloc(station_class->spec, station_class->stations * sizeof(*station_class->spec)); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
140 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
141 |
station_class->spec[i] = spec; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
142 |
} |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
143 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
144 |
/** |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
145 |
* Retrieve a station spec from a class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
146 |
* @param sclass Index of the station class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
147 |
* @param station The station index with the class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
148 |
* @return The station spec. |
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 |
const StationSpec *GetCustomStation(StationClassID sclass, uint station) |
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 |
assert(sclass < STAT_CLASS_MAX); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
153 |
if (station < station_classes[sclass].stations) |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
154 |
return station_classes[sclass].spec[station]; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
155 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
156 |
// If the custom station isn't defined any more, then the GRF file |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
157 |
// probably was not loaded. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
158 |
return NULL; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
159 |
} |
2967
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
160 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
161 |
static const RealSpriteGroup *ResolveStationSpriteGroup(const SpriteGroup *spg, const Station *st) |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
162 |
{ |
3573
d120a5847915
(svn r4459) - Newstations: check pointer for NULL before using it.
peter1138
parents:
2967
diff
changeset
|
163 |
if (spg == NULL) return NULL; |
2967
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
164 |
switch (spg->type) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
165 |
case SGT_REAL: |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
166 |
return &spg->g.real; |
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 |
case SGT_DETERMINISTIC: { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
169 |
const DeterministicSpriteGroup *dsg = &spg->g.determ; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
170 |
SpriteGroup *target; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
171 |
int value = -1; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
172 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
173 |
if ((dsg->variable >> 6) == 0) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
174 |
/* General property */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
175 |
value = GetDeterministicSpriteValue(dsg->variable); |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
176 |
} else { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
177 |
if (st == NULL) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
178 |
/* We are in a build dialog of something, |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
179 |
* and we are checking for something undefined. |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
180 |
* That means we should get the first target |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
181 |
* (NOT the default one). */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
182 |
if (dsg->num_ranges > 0) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
183 |
target = dsg->ranges[0].group; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
184 |
} else { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
185 |
target = dsg->default_group; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
186 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
187 |
return ResolveStationSpriteGroup(target, NULL); |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
188 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
189 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
190 |
/* Station-specific property. */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
191 |
if (dsg->var_scope == VSG_SCOPE_PARENT) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
192 |
/* TODO: Town structure. */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
193 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
194 |
} else /* VSG_SELF */ { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
195 |
if (dsg->variable == 0x40 || dsg->variable == 0x41) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
196 |
/* FIXME: This is ad hoc only |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
197 |
* for waypoints. */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
198 |
value = 0x01010000; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
199 |
} else { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
200 |
/* TODO: Only small fraction done. */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
201 |
// TTDPatch runs on little-endian arch; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
202 |
// Variable is 0x70 + offset in the TTD's station structure |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
203 |
switch (dsg->variable - 0x70) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
204 |
case 0x80: value = st->facilities; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
205 |
case 0x81: value = st->airport_type; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
206 |
case 0x82: value = st->truck_stops->status; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
207 |
case 0x83: value = st->bus_stops->status; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
208 |
case 0x86: value = st->airport_flags & 0xFFFF; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
209 |
case 0x87: value = st->airport_flags & 0xFF; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
210 |
case 0x8A: value = st->build_date; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
211 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
212 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
213 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
214 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
215 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
216 |
target = value != -1 ? EvalDeterministicSpriteGroup(dsg, value) : dsg->default_group; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
217 |
return ResolveStationSpriteGroup(target, st); |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
218 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
219 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
220 |
default: |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
221 |
case SGT_RANDOMIZED: |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
222 |
DEBUG(grf, 6)("I don't know how to handle random spritegroups yet!"); |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
223 |
return NULL; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
224 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
225 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
226 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
227 |
uint32 GetCustomStationRelocation(const StationSpec *spec, const Station *st, byte ctype) |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
228 |
{ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
229 |
const RealSpriteGroup *rsg = ResolveStationSpriteGroup(spec->spritegroup[ctype], st); |
3573
d120a5847915
(svn r4459) - Newstations: check pointer for NULL before using it.
peter1138
parents:
2967
diff
changeset
|
230 |
if (rsg == NULL) return 0; |
2967
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
231 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
232 |
if (rsg->sprites_per_set != 0) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
233 |
if (rsg->loading_count != 0) return rsg->loading[0]->g.result.result; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
234 |
if (rsg->loaded_count != 0) return rsg->loaded[0]->g.result.result; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
235 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
236 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
237 |
DEBUG(grf, 6)("Custom station 0x%08x::0x%02x has no sprites associated.", |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
238 |
spec->grfid, spec->localidx); |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
239 |
/* This is what gets subscribed of dtss->image in newgrf.c, |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
240 |
* so it's probably kinda "default offset". Try to use it as |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
241 |
* emergency measure. */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
242 |
return 0; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
243 |
} |
3587 | 244 |
|
245 |
||
246 |
/** |
|
247 |
* Allocate a StationSpec to a Station. This is called once per build operation. |
|
248 |
* @param spec StationSpec to allocate. |
|
249 |
* @param st Station to allocate it to. |
|
250 |
* @param exec Whether to actually allocate the spec. |
|
251 |
* @return Index within the Station's spec list, or -1 if the allocation failed. |
|
252 |
*/ |
|
253 |
int AllocateSpecToStation(const StationSpec *spec, Station *st, bool exec) |
|
254 |
{ |
|
255 |
uint i; |
|
256 |
||
257 |
if (spec == NULL) return 0; |
|
258 |
||
259 |
/* Check if this spec has already been allocated */ |
|
260 |
for (i = 1; i < st->num_specs && i < 256; i++) { |
|
261 |
if (st->speclist[i].spec == spec) return i; |
|
262 |
} |
|
263 |
||
264 |
for (i = 1; i < st->num_specs && i < 256; i++) { |
|
265 |
if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break; |
|
266 |
} |
|
267 |
||
268 |
if (i < 256) { |
|
269 |
if (exec) { |
|
270 |
if (i >= st->num_specs) { |
|
271 |
st->num_specs = i + 1; |
|
272 |
st->speclist = realloc(st->speclist, st->num_specs * sizeof(*st->speclist)); |
|
273 |
||
274 |
if (st->num_specs == 2) { |
|
275 |
/* Initial allocation */ |
|
276 |
st->speclist[0].spec = NULL; |
|
277 |
st->speclist[0].grfid = 0; |
|
278 |
st->speclist[0].localidx = 0; |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
st->speclist[i].spec = spec; |
|
283 |
st->speclist[i].grfid = spec->grfid; |
|
284 |
st->speclist[i].localidx = spec->localidx; |
|
285 |
} |
|
286 |
return i; |
|
287 |
} |
|
288 |
||
289 |
return -1; |
|
290 |
} |
|
291 |
||
292 |
||
293 |
/** Deallocate a StationSpec from a Station. Called when removing a single station tile. |
|
294 |
* @param st Station to work with. |
|
295 |
* @param specindex Index of the custom station within the Station's spec list. |
|
296 |
* @return Indicates whether the StationSpec was deallocated. |
|
297 |
*/ |
|
298 |
bool DeallocateSpecFromStation(Station *st, byte specindex) |
|
299 |
{ |
|
300 |
bool freeable = true; |
|
301 |
||
302 |
/* specindex of 0 (default) is never freeable */ |
|
303 |
if (specindex == 0) return false; |
|
304 |
||
305 |
/* Check all tiles over the station to check if the specindex is still in use */ |
|
306 |
BEGIN_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile) { |
|
307 |
if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st->index && IsRailwayStation(tile) && GetCustomStationSpecIndex(tile) == specindex) { |
|
308 |
freeable = false; |
|
309 |
break; |
|
310 |
} |
|
311 |
} END_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile) |
|
312 |
||
313 |
if (freeable) { |
|
314 |
/* This specindex is no longer in use, so deallocate it */ |
|
315 |
st->speclist[specindex].spec = NULL; |
|
316 |
st->speclist[specindex].grfid = 0; |
|
317 |
st->speclist[specindex].localidx = 0; |
|
318 |
||
319 |
/* If this was the highest spec index, reallocate */ |
|
320 |
if (specindex == st->num_specs - 1) { |
|
321 |
for (; st->speclist[st->num_specs - 1].grfid == 0 && st->num_specs > 1; st->num_specs--); |
|
322 |
||
323 |
if (st->num_specs > 1) { |
|
324 |
st->speclist = realloc(st->speclist, st->num_specs * sizeof(*st->speclist)); |
|
325 |
} else { |
|
326 |
free(st->speclist); |
|
327 |
st->num_specs = 0; |
|
328 |
st->speclist = NULL; |
|
329 |
} |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
return freeable; |
|
334 |
} |