7 |
7 |
8 #include "direction.h" |
8 #include "direction.h" |
9 #include "tile.h" |
9 #include "tile.h" |
10 |
10 |
11 typedef enum RailTypes { |
11 typedef enum RailTypes { |
|
12 RAILTYPE_BEGIN = 0, |
12 RAILTYPE_RAIL = 0, |
13 RAILTYPE_RAIL = 0, |
13 RAILTYPE_ELECTRIC = 1, |
14 RAILTYPE_ELECTRIC = 1, |
14 RAILTYPE_MONO = 2, |
15 RAILTYPE_MONO = 2, |
15 RAILTYPE_MAGLEV = 3, |
16 RAILTYPE_MAGLEV = 3, |
16 RAILTYPE_END, |
17 RAILTYPE_END, |
17 INVALID_RAILTYPE = 0xFF |
18 INVALID_RAILTYPE = 0xFF |
18 } RailType; |
19 } RailType; |
19 |
20 |
20 typedef byte RailTypeMask; |
21 typedef byte RailTypeMask; |
21 |
22 |
|
23 /** Allow incrementing of Track variables */ |
|
24 DECLARE_POSTFIX_INCREMENT(RailType); |
|
25 /** Define basic enum properties */ |
|
26 template <> struct EnumPropsT<RailType> : MakeEnumPropsT<RailType, byte, RAILTYPE_BEGIN, RAILTYPE_END, INVALID_RAILTYPE> {}; |
|
27 typedef TinyEnumT<RailType> RailTypeByte; |
|
28 |
22 |
29 |
23 /** These are used to specify a single track. |
30 /** These are used to specify a single track. |
24 * Can be translated to a trackbit with TrackToTrackbit */ |
31 * Can be translated to a trackbit with TrackToTrackbit */ |
25 typedef enum Track { |
32 typedef enum Track { |
|
33 TRACK_BEGIN = 0, |
26 TRACK_X = 0, |
34 TRACK_X = 0, |
27 TRACK_Y = 1, |
35 TRACK_Y = 1, |
28 TRACK_UPPER = 2, |
36 TRACK_UPPER = 2, |
29 TRACK_LOWER = 3, |
37 TRACK_LOWER = 3, |
30 TRACK_LEFT = 4, |
38 TRACK_LEFT = 4, |
31 TRACK_RIGHT = 5, |
39 TRACK_RIGHT = 5, |
32 TRACK_END, |
40 TRACK_END, |
33 INVALID_TRACK = 0xFF |
41 INVALID_TRACK = 0xFF |
34 } Track; |
42 } Track; |
|
43 |
|
44 /** Allow incrementing of Track variables */ |
|
45 DECLARE_POSTFIX_INCREMENT(Track); |
|
46 /** Define basic enum properties */ |
|
47 template <> struct EnumPropsT<Track> : MakeEnumPropsT<Track, byte, TRACK_BEGIN, TRACK_END, INVALID_TRACK> {}; |
|
48 typedef TinyEnumT<Track> TrackByte; |
35 |
49 |
36 |
50 |
37 /** Convert an Axis to the corresponding Track |
51 /** Convert an Axis to the corresponding Track |
38 * AXIS_X -> TRACK_X |
52 * AXIS_X -> TRACK_X |
39 * AXIS_Y -> TRACK_Y |
53 * AXIS_Y -> TRACK_Y |
60 TRACK_BIT_3WAY_NE = TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_RIGHT, |
74 TRACK_BIT_3WAY_NE = TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_RIGHT, |
61 TRACK_BIT_3WAY_SE = TRACK_BIT_Y | TRACK_BIT_LOWER | TRACK_BIT_RIGHT, |
75 TRACK_BIT_3WAY_SE = TRACK_BIT_Y | TRACK_BIT_LOWER | TRACK_BIT_RIGHT, |
62 TRACK_BIT_3WAY_SW = TRACK_BIT_X | TRACK_BIT_LOWER | TRACK_BIT_LEFT, |
76 TRACK_BIT_3WAY_SW = TRACK_BIT_X | TRACK_BIT_LOWER | TRACK_BIT_LEFT, |
63 TRACK_BIT_3WAY_NW = TRACK_BIT_Y | TRACK_BIT_UPPER | TRACK_BIT_LEFT, |
77 TRACK_BIT_3WAY_NW = TRACK_BIT_Y | TRACK_BIT_UPPER | TRACK_BIT_LEFT, |
64 TRACK_BIT_ALL = TRACK_BIT_CROSS | TRACK_BIT_HORZ | TRACK_BIT_VERT, |
78 TRACK_BIT_ALL = TRACK_BIT_CROSS | TRACK_BIT_HORZ | TRACK_BIT_VERT, |
65 TRACK_BIT_MASK = 0x3FU |
79 TRACK_BIT_MASK = 0x3FU, |
|
80 TRACK_BIT_WORMHOLE = 0x40U, |
|
81 TRACK_BIT_SPECIAL = 0x80U, |
|
82 INVALID_TRACK_BIT = 0xFF |
66 } TrackBits; |
83 } TrackBits; |
67 |
84 |
|
85 /** Define basic enum properties */ |
|
86 template <> struct EnumPropsT<TrackBits> : MakeEnumPropsT<TrackBits, byte, TRACK_BIT_NONE, TRACK_BIT_ALL, INVALID_TRACK_BIT> {}; |
|
87 typedef TinyEnumT<TrackBits> TrackBitsByte; |
|
88 |
|
89 DECLARE_ENUM_AS_BIT_SET(TrackBits); |
|
90 DECLARE_ENUM_AS_BIT_INDEX(Track, TrackBits); |
68 |
91 |
69 /** |
92 /** |
70 * Maps a Track to the corresponding TrackBits value |
93 * Maps a Track to the corresponding TrackBits value |
71 */ |
94 */ |
72 static inline TrackBits TrackToTrackBits(Track track) |
95 static inline TrackBits TrackToTrackBits(Track track) |
82 |
105 |
83 |
106 |
84 /** These are a combination of tracks and directions. Values are 0-5 in one |
107 /** These are a combination of tracks and directions. Values are 0-5 in one |
85 * direction (corresponding to the Track enum) and 8-13 in the other direction. */ |
108 * direction (corresponding to the Track enum) and 8-13 in the other direction. */ |
86 typedef enum Trackdirs { |
109 typedef enum Trackdirs { |
|
110 TRACKDIR_BEGIN = 0, |
87 TRACKDIR_X_NE = 0, |
111 TRACKDIR_X_NE = 0, |
88 TRACKDIR_Y_SE = 1, |
112 TRACKDIR_Y_SE = 1, |
89 TRACKDIR_UPPER_E = 2, |
113 TRACKDIR_UPPER_E = 2, |
90 TRACKDIR_LOWER_E = 3, |
114 TRACKDIR_LOWER_E = 3, |
91 TRACKDIR_LEFT_S = 4, |
115 TRACKDIR_LEFT_S = 4, |
99 TRACKDIR_LEFT_N = 12, |
123 TRACKDIR_LEFT_N = 12, |
100 TRACKDIR_RIGHT_N = 13, |
124 TRACKDIR_RIGHT_N = 13, |
101 TRACKDIR_END, |
125 TRACKDIR_END, |
102 INVALID_TRACKDIR = 0xFF, |
126 INVALID_TRACKDIR = 0xFF, |
103 } Trackdir; |
127 } Trackdir; |
|
128 |
|
129 /** Define basic enum properties */ |
|
130 template <> struct EnumPropsT<Trackdir> : MakeEnumPropsT<Trackdir, byte, TRACKDIR_BEGIN, TRACKDIR_END, INVALID_TRACKDIR> {}; |
|
131 typedef TinyEnumT<Trackdir> TrackdirByte; |
104 |
132 |
105 /** These are a combination of tracks and directions. Values are 0-5 in one |
133 /** These are a combination of tracks and directions. Values are 0-5 in one |
106 * direction (corresponding to the Track enum) and 8-13 in the other direction. */ |
134 * direction (corresponding to the Track enum) and 8-13 in the other direction. */ |
107 typedef enum TrackdirBits { |
135 typedef enum TrackdirBits { |
108 TRACKDIR_BIT_NONE = 0x0000, |
136 TRACKDIR_BIT_NONE = 0x0000, |
121 TRACKDIR_BIT_RIGHT_N = 0x2000, |
149 TRACKDIR_BIT_RIGHT_N = 0x2000, |
122 TRACKDIR_BIT_MASK = 0x3F3F, |
150 TRACKDIR_BIT_MASK = 0x3F3F, |
123 INVALID_TRACKDIR_BIT = 0xFFFF, |
151 INVALID_TRACKDIR_BIT = 0xFFFF, |
124 } TrackdirBits; |
152 } TrackdirBits; |
125 |
153 |
|
154 /** Define basic enum properties */ |
|
155 template <> struct EnumPropsT<TrackdirBits> : MakeEnumPropsT<TrackdirBits, uint16, TRACKDIR_BIT_NONE, TRACKDIR_BIT_MASK, INVALID_TRACKDIR_BIT> {}; |
|
156 typedef TinyEnumT<TrackdirBits> TrackdirBitsShort; |
|
157 DECLARE_ENUM_AS_BIT_SET(TrackdirBits); |
|
158 DECLARE_ENUM_AS_BIT_INDEX(Trackdir, TrackdirBits); |
|
159 |
126 /** This struct contains all the info that is needed to draw and construct tracks. |
160 /** This struct contains all the info that is needed to draw and construct tracks. |
127 */ |
161 */ |
128 typedef struct RailtypeInfo { |
162 typedef struct RailtypeInfo { |
129 /** Struct containing the main sprites. @note not all sprites are listed, but only |
163 /** Struct containing the main sprites. @note not all sprites are listed, but only |
130 * the ones used directly in the code */ |
164 * the ones used directly in the code */ |
212 * Maps a Trackdir to the corresponding TrackdirBits value |
246 * Maps a Trackdir to the corresponding TrackdirBits value |
213 */ |
247 */ |
214 static inline TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir) { return (TrackdirBits)(1 << trackdir); } |
248 static inline TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir) { return (TrackdirBits)(1 << trackdir); } |
215 |
249 |
216 /** |
250 /** |
|
251 * Removes first Track from TrackBits and returns it |
|
252 */ |
|
253 static inline Track RemoveFirstTrack(TrackBits &tracks) |
|
254 { |
|
255 if (tracks != TRACK_BIT_NONE && tracks != INVALID_TRACK_BIT) { |
|
256 Track first = (Track)FIND_FIRST_BIT(tracks); |
|
257 tracks = ClrBitT(tracks, first); |
|
258 return first; |
|
259 } |
|
260 return INVALID_TRACK; |
|
261 } |
|
262 |
|
263 /** |
|
264 * Removes first Trackdir from TrackdirBits and returns it |
|
265 */ |
|
266 static inline Trackdir RemoveFirstTrackdir(TrackdirBits &trackdirs) |
|
267 { |
|
268 if (trackdirs != TRACKDIR_BIT_NONE && trackdirs != INVALID_TRACKDIR_BIT) { |
|
269 Trackdir first = (Trackdir)FindFirstBit2x64(trackdirs); |
|
270 trackdirs = ClrBitT(trackdirs, first); |
|
271 return first; |
|
272 } |
|
273 return INVALID_TRACKDIR; |
|
274 } |
|
275 |
|
276 /** |
|
277 * Returns first Track from TrackBits or INVALID_TRACK |
|
278 */ |
|
279 static inline Track FindFirstTrack(TrackBits tracks) |
|
280 { |
|
281 return (tracks != TRACK_BIT_NONE && tracks != INVALID_TRACK_BIT) ? (Track)FIND_FIRST_BIT(tracks) : INVALID_TRACK; |
|
282 } |
|
283 |
|
284 /** |
|
285 * Converts TrackBits to Track. TrackBits must contain just one Track or INVALID_TRACK_BIT! |
|
286 */ |
|
287 static inline Track TrackBitsToTrack(TrackBits tracks) |
|
288 { |
|
289 assert (tracks == INVALID_TRACK_BIT || (tracks != TRACK_BIT_NONE && KILL_FIRST_BIT(tracks) == 0)); |
|
290 return tracks != INVALID_TRACK_BIT ? (Track)FIND_FIRST_BIT(tracks) : INVALID_TRACK; |
|
291 } |
|
292 |
|
293 /** |
|
294 * Returns first Trackdir from TrackdirBits or INVALID_TRACKDIR |
|
295 */ |
|
296 static inline Trackdir FindFirstTrackdir(TrackdirBits trackdirs) |
|
297 { |
|
298 assert((trackdirs & ~TRACKDIR_BIT_MASK) == TRACKDIR_BIT_NONE); |
|
299 return (trackdirs != TRACKDIR_BIT_NONE) ? (Trackdir)FindFirstBit2x64(trackdirs) : INVALID_TRACKDIR; |
|
300 } |
|
301 |
|
302 /** |
217 * These functions check the validity of Tracks and Trackdirs. assert against |
303 * These functions check the validity of Tracks and Trackdirs. assert against |
218 * them when convenient. |
304 * them when convenient. |
219 */ |
305 */ |
220 static inline bool IsValidTrack(Track track) { return track < TRACK_END; } |
306 static inline bool IsValidTrack(Track track) { return track < TRACK_END; } |
221 static inline bool IsValidTrackdir(Trackdir trackdir) { return (TrackdirToTrackdirBits(trackdir) & TRACKDIR_BIT_MASK) != 0; } |
307 static inline bool IsValidTrackdir(Trackdir trackdir) { return (TrackdirToTrackdirBits(trackdir) & TRACKDIR_BIT_MASK) != 0; } |