author | peter1138 |
Wed, 19 Apr 2006 07:17:00 +0000 | |
changeset 3587 | c434d5c3dcaa |
parent 3573 | d120a5847915 |
child 3594 | 3c4e4feea80a |
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" |
2967
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
9 |
#include "station.h" |
3587 | 10 |
#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
|
11 |
#include "newgrf_station.h" |
2625
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
12 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
13 |
static StationClass station_classes[STAT_CLASS_MAX]; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
14 |
|
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 |
* Reset station classes to their default state. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
17 |
* 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
|
18 |
* entry, for standard stations and waypoints. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
19 |
*/ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
20 |
void ResetStationClasses(void) |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
21 |
{ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
22 |
StationClassID i; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
23 |
for (i = 0; i < STAT_CLASS_MAX; i++) { |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
24 |
station_classes[i].id = 0; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
25 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
26 |
free(station_classes[i].name); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
27 |
station_classes[i].name = NULL; |
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 |
station_classes[i].stations = 0; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
30 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
31 |
free(station_classes[i].spec); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
32 |
station_classes[i].spec = NULL; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
33 |
} |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
34 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
35 |
// Set up initial data |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
36 |
station_classes[0].id = 'DFLT'; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
37 |
station_classes[0].name = strdup("Default"); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
38 |
station_classes[0].stations = 1; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
39 |
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
|
40 |
station_classes[0].spec[0] = NULL; |
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 |
station_classes[1].id = 'WAYP'; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
43 |
station_classes[1].name = strdup("Waypoints"); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
44 |
station_classes[1].stations = 1; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
45 |
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
|
46 |
station_classes[1].spec[0] = NULL; |
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 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
49 |
/** |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
50 |
* 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
|
51 |
* @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
|
52 |
* @return Index into station_classes of allocated 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 AllocateStationClass(uint32 class) |
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 |
StationClassID i; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
57 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
58 |
for (i = 0; i < STAT_CLASS_MAX; i++) { |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
59 |
if (station_classes[i].id == class) { |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
60 |
// ClassID is already allocated, so reuse it. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
61 |
return i; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
62 |
} else if (station_classes[i].id == 0) { |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
63 |
// 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
|
64 |
station_classes[i].id = class; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
65 |
return i; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
66 |
} |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
67 |
} |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
68 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
69 |
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
|
70 |
return STAT_CLASS_DFLT; |
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 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
73 |
/** |
3587 | 74 |
* Get the number of station classes in use. |
75 |
* @return Number of station classes. |
|
76 |
*/ |
|
77 |
uint GetNumStationClasses(void) |
|
78 |
{ |
|
79 |
uint i; |
|
80 |
for (i = 0; i < STAT_CLASS_MAX && station_classes[i].id != 0; i++); |
|
81 |
return i; |
|
82 |
} |
|
83 |
||
84 |
/** |
|
2625
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
85 |
* 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
|
86 |
* @param sclass Index of the station class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
87 |
* @return Number of stations in the class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
88 |
*/ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
89 |
uint GetNumCustomStations(StationClassID sclass) |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
90 |
{ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
91 |
assert(sclass < STAT_CLASS_MAX); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
92 |
return station_classes[sclass].stations; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
93 |
} |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
94 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
95 |
/** |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
96 |
* Tie a station spec to its station class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
97 |
* @param spec The station spec. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
98 |
*/ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
99 |
void SetCustomStation(StationSpec *spec) |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
100 |
{ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
101 |
StationClass *station_class; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
102 |
int i; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
103 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
104 |
assert(spec->sclass < STAT_CLASS_MAX); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
105 |
station_class = &station_classes[spec->sclass]; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
106 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
107 |
i = station_class->stations++; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
108 |
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
|
109 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
110 |
station_class->spec[i] = spec; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
111 |
} |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
112 |
|
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
113 |
/** |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
114 |
* Retrieve a station spec from a class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
115 |
* @param sclass Index of the station class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
116 |
* @param station The station index with the class. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
117 |
* @return The station spec. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
118 |
*/ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
119 |
const StationSpec *GetCustomStation(StationClassID sclass, uint station) |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
120 |
{ |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
121 |
assert(sclass < STAT_CLASS_MAX); |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
122 |
if (station < station_classes[sclass].stations) |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
123 |
return station_classes[sclass].spec[station]; |
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 |
// 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
|
126 |
// probably was not loaded. |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
127 |
return NULL; |
19bf7f695537
(svn r3167) - NewGRF: Start moving custom station code to separate files.
peter1138
parents:
diff
changeset
|
128 |
} |
2967
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
129 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
130 |
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
|
131 |
{ |
3573
d120a5847915
(svn r4459) - Newstations: check pointer for NULL before using it.
peter1138
parents:
2967
diff
changeset
|
132 |
if (spg == NULL) return NULL; |
2967
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
133 |
switch (spg->type) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
134 |
case SGT_REAL: |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
135 |
return &spg->g.real; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
136 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
137 |
case SGT_DETERMINISTIC: { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
138 |
const DeterministicSpriteGroup *dsg = &spg->g.determ; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
139 |
SpriteGroup *target; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
140 |
int value = -1; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
141 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
142 |
if ((dsg->variable >> 6) == 0) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
143 |
/* General property */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
144 |
value = GetDeterministicSpriteValue(dsg->variable); |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
145 |
} else { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
146 |
if (st == NULL) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
147 |
/* We are in a build dialog of something, |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
148 |
* and we are checking for something undefined. |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
149 |
* That means we should get the first target |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
150 |
* (NOT the default one). */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
151 |
if (dsg->num_ranges > 0) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
152 |
target = dsg->ranges[0].group; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
153 |
} else { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
154 |
target = dsg->default_group; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
155 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
156 |
return ResolveStationSpriteGroup(target, NULL); |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
157 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
158 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
159 |
/* Station-specific property. */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
160 |
if (dsg->var_scope == VSG_SCOPE_PARENT) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
161 |
/* TODO: Town structure. */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
162 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
163 |
} else /* VSG_SELF */ { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
164 |
if (dsg->variable == 0x40 || dsg->variable == 0x41) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
165 |
/* FIXME: This is ad hoc only |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
166 |
* for waypoints. */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
167 |
value = 0x01010000; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
168 |
} else { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
169 |
/* TODO: Only small fraction done. */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
170 |
// TTDPatch runs on little-endian arch; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
171 |
// 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
|
172 |
switch (dsg->variable - 0x70) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
173 |
case 0x80: value = st->facilities; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
174 |
case 0x81: value = st->airport_type; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
175 |
case 0x82: value = st->truck_stops->status; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
176 |
case 0x83: value = st->bus_stops->status; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
177 |
case 0x86: value = st->airport_flags & 0xFFFF; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
178 |
case 0x87: value = st->airport_flags & 0xFF; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
179 |
case 0x8A: value = st->build_date; break; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
180 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
181 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
182 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
183 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
184 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
185 |
target = value != -1 ? EvalDeterministicSpriteGroup(dsg, value) : dsg->default_group; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
186 |
return ResolveStationSpriteGroup(target, st); |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
187 |
} |
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 |
default: |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
190 |
case SGT_RANDOMIZED: |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
191 |
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
|
192 |
return NULL; |
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 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
195 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
196 |
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
|
197 |
{ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
198 |
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
|
199 |
if (rsg == NULL) return 0; |
2967
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
200 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
201 |
if (rsg->sprites_per_set != 0) { |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
202 |
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
|
203 |
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
|
204 |
} |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
205 |
|
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
206 |
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
|
207 |
spec->grfid, spec->localidx); |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
208 |
/* 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
|
209 |
* 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
|
210 |
* emergency measure. */ |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
211 |
return 0; |
77b04531d7a0
(svn r3530) - NewGRF: Move station resolver to newgrf_station
peter1138
parents:
2963
diff
changeset
|
212 |
} |
3587 | 213 |
|
214 |
||
215 |
/** |
|
216 |
* Allocate a StationSpec to a Station. This is called once per build operation. |
|
217 |
* @param spec StationSpec to allocate. |
|
218 |
* @param st Station to allocate it to. |
|
219 |
* @param exec Whether to actually allocate the spec. |
|
220 |
* @return Index within the Station's spec list, or -1 if the allocation failed. |
|
221 |
*/ |
|
222 |
int AllocateSpecToStation(const StationSpec *spec, Station *st, bool exec) |
|
223 |
{ |
|
224 |
uint i; |
|
225 |
||
226 |
if (spec == NULL) return 0; |
|
227 |
||
228 |
/* Check if this spec has already been allocated */ |
|
229 |
for (i = 1; i < st->num_specs && i < 256; i++) { |
|
230 |
if (st->speclist[i].spec == spec) return i; |
|
231 |
} |
|
232 |
||
233 |
for (i = 1; i < st->num_specs && i < 256; i++) { |
|
234 |
if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break; |
|
235 |
} |
|
236 |
||
237 |
if (i < 256) { |
|
238 |
if (exec) { |
|
239 |
if (i >= st->num_specs) { |
|
240 |
st->num_specs = i + 1; |
|
241 |
st->speclist = realloc(st->speclist, st->num_specs * sizeof(*st->speclist)); |
|
242 |
||
243 |
if (st->num_specs == 2) { |
|
244 |
/* Initial allocation */ |
|
245 |
st->speclist[0].spec = NULL; |
|
246 |
st->speclist[0].grfid = 0; |
|
247 |
st->speclist[0].localidx = 0; |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
st->speclist[i].spec = spec; |
|
252 |
st->speclist[i].grfid = spec->grfid; |
|
253 |
st->speclist[i].localidx = spec->localidx; |
|
254 |
} |
|
255 |
return i; |
|
256 |
} |
|
257 |
||
258 |
return -1; |
|
259 |
} |
|
260 |
||
261 |
||
262 |
/** Deallocate a StationSpec from a Station. Called when removing a single station tile. |
|
263 |
* @param st Station to work with. |
|
264 |
* @param specindex Index of the custom station within the Station's spec list. |
|
265 |
* @return Indicates whether the StationSpec was deallocated. |
|
266 |
*/ |
|
267 |
bool DeallocateSpecFromStation(Station *st, byte specindex) |
|
268 |
{ |
|
269 |
bool freeable = true; |
|
270 |
||
271 |
/* specindex of 0 (default) is never freeable */ |
|
272 |
if (specindex == 0) return false; |
|
273 |
||
274 |
/* Check all tiles over the station to check if the specindex is still in use */ |
|
275 |
BEGIN_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile) { |
|
276 |
if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st->index && IsRailwayStation(tile) && GetCustomStationSpecIndex(tile) == specindex) { |
|
277 |
freeable = false; |
|
278 |
break; |
|
279 |
} |
|
280 |
} END_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile) |
|
281 |
||
282 |
if (freeable) { |
|
283 |
/* This specindex is no longer in use, so deallocate it */ |
|
284 |
st->speclist[specindex].spec = NULL; |
|
285 |
st->speclist[specindex].grfid = 0; |
|
286 |
st->speclist[specindex].localidx = 0; |
|
287 |
||
288 |
/* If this was the highest spec index, reallocate */ |
|
289 |
if (specindex == st->num_specs - 1) { |
|
290 |
for (; st->speclist[st->num_specs - 1].grfid == 0 && st->num_specs > 1; st->num_specs--); |
|
291 |
||
292 |
if (st->num_specs > 1) { |
|
293 |
st->speclist = realloc(st->speclist, st->num_specs * sizeof(*st->speclist)); |
|
294 |
} else { |
|
295 |
free(st->speclist); |
|
296 |
st->num_specs = 0; |
|
297 |
st->speclist = NULL; |
|
298 |
} |
|
299 |
} |
|
300 |
} |
|
301 |
||
302 |
return freeable; |
|
303 |
} |