OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

rail_map.h
Go to the documentation of this file.
1 /* $Id: rail_map.h 26878 2014-09-21 11:23:33Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #ifndef RAIL_MAP_H
13 #define RAIL_MAP_H
14
15 #include "rail_type.h"
16 #include "depot_type.h"
17 #include "signal_func.h"
18 #include "track_func.h"
19 #include "tile_map.h"
20 #include "signal_type.h"
21
22
24 enum RailTileType {
25 RAIL_TILE_NORMAL = 0,
26 RAIL_TILE_SIGNALS = 1,
27 RAIL_TILE_DEPOT = 3,
28 };
29
37 static inline RailTileType GetRailTileType(TileIndex t)
38 {
39 assert(IsTileType(t, MP_RAILWAY));
40 return (RailTileType)GB(_m[t].m5, 6, 2);
41 }
42
50 static inline bool IsPlainRail(TileIndex t)
51 {
52 RailTileType rtt = GetRailTileType(t);
53 return rtt == RAIL_TILE_NORMAL || rtt == RAIL_TILE_SIGNALS;
54 }
55
61 static inline bool IsPlainRailTile(TileIndex t)
62 {
63 return IsTileType(t, MP_RAILWAY) && IsPlainRail(t);
64 }
65
66
73 static inline bool HasSignals(TileIndex t)
74 {
75 return GetRailTileType(t) == RAIL_TILE_SIGNALS;
76 }
77
84 static inline void SetHasSignals(TileIndex tile, bool signals)
85 {
86 assert(IsPlainRailTile(tile));
87 SB(_m[tile].m5, 6, 1, signals);
88 }
89
96 static inline bool IsRailDepot(TileIndex t)
97 {
98 return GetRailTileType(t) == RAIL_TILE_DEPOT;
99 }
100
106 static inline bool IsRailDepotTile(TileIndex t)
107 {
108 return IsTileType(t, MP_RAILWAY) && IsRailDepot(t);
109 }
110
116 static inline RailType GetRailType(TileIndex t)
117 {
118 return (RailType)GB(_m[t].m3, 0, 4);
119 }
120
126 static inline void SetRailType(TileIndex t, RailType r)
127 {
128 SB(_m[t].m3, 0, 4, r);
129 }
130
131
137 static inline TrackBits GetTrackBits(TileIndex tile)
138 {
139 assert(IsPlainRailTile(tile));
140 return (TrackBits)GB(_m[tile].m5, 0, 6);
141 }
142
148 static inline void SetTrackBits(TileIndex t, TrackBits b)
149 {
150 assert(IsPlainRailTile(t));
151 SB(_m[t].m5, 0, 6, b);
152 }
153
161 static inline bool HasTrack(TileIndex tile, Track track)
162 {
163 return HasBit(GetTrackBits(tile), track);
164 }
165
172 static inline DiagDirection GetRailDepotDirection(TileIndex t)
173 {
174 return (DiagDirection)GB(_m[t].m5, 0, 2);
175 }
176
183 static inline Track GetRailDepotTrack(TileIndex t)
184 {
185 return DiagDirToDiagTrack(GetRailDepotDirection(t));
186 }
187
188
195 static inline TrackBits GetRailReservationTrackBits(TileIndex t)
196 {
197 assert(IsPlainRailTile(t));
198 byte track_b = GB(_m[t].m2, 8, 3);
199 Track track = (Track)(track_b - 1); // map array saves Track+1
200 if (track_b == 0) return TRACK_BIT_NONE;
201 return (TrackBits)(TrackToTrackBits(track) | (HasBit(_m[t].m2, 11) ? TrackToTrackBits(TrackToOppositeTrack(track)) : 0));
202 }
203
210 static inline void SetTrackReservation(TileIndex t, TrackBits b)
211 {
212 assert(IsPlainRailTile(t));
213 assert(b != INVALID_TRACK_BIT);
214 assert(!TracksOverlap(b));
215 Track track = RemoveFirstTrack(&b);
216 SB(_m[t].m2, 8, 3, track == INVALID_TRACK ? 0 : track + 1);
217 SB(_m[t].m2, 11, 1, (byte)(b != TRACK_BIT_NONE));
218 }
219
227 static inline bool TryReserveTrack(TileIndex tile, Track t)
228 {
229 assert(HasTrack(tile, t));
230 TrackBits bits = TrackToTrackBits(t);
231 TrackBits res = GetRailReservationTrackBits(tile);
232 if ((res & bits) != TRACK_BIT_NONE) return false; // already reserved
233 res |= bits;
234 if (TracksOverlap(res)) return false; // crossing reservation present
235 SetTrackReservation(tile, res);
236 return true;
237 }
238
245 static inline void UnreserveTrack(TileIndex tile, Track t)
246 {
247 assert(HasTrack(tile, t));
248 TrackBits res = GetRailReservationTrackBits(tile);
249 res &= ~TrackToTrackBits(t);
250 SetTrackReservation(tile, res);
251 }
252
259 static inline bool HasDepotReservation(TileIndex t)
260 {
261 assert(IsRailDepot(t));
262 return HasBit(_m[t].m5, 4);
263 }
264
271 static inline void SetDepotReservation(TileIndex t, bool b)
272 {
273 assert(IsRailDepot(t));
274 SB(_m[t].m5, 4, 1, (byte)b);
275 }
276
283 static inline TrackBits GetDepotReservationTrackBits(TileIndex t)
284 {
285 return HasDepotReservation(t) ? TrackToTrackBits(GetRailDepotTrack(t)) : TRACK_BIT_NONE;
286 }
287
288
289 static inline bool IsPbsSignal(SignalType s)
290 {
291 return s == SIGTYPE_PBS || s == SIGTYPE_PBS_ONEWAY;
292 }
293
294 static inline SignalType GetSignalType(TileIndex t, Track track)
295 {
296 assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
297 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
298 return (SignalType)GB(_m[t].m2, pos, 3);
299 }
300
301 static inline void SetSignalType(TileIndex t, Track track, SignalType s)
302 {
303 assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
304 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0;
305 SB(_m[t].m2, pos, 3, s);
306 if (track == INVALID_TRACK) SB(_m[t].m2, 4, 3, s);
307 }
308
309 static inline bool IsPresignalEntry(TileIndex t, Track track)
310 {
311 return GetSignalType(t, track) == SIGTYPE_ENTRY || GetSignalType(t, track) == SIGTYPE_COMBO;
312 }
313
314 static inline bool IsPresignalExit(TileIndex t, Track track)
315 {
316 return GetSignalType(t, track) == SIGTYPE_EXIT || GetSignalType(t, track) == SIGTYPE_COMBO;
317 }
318
320 static inline bool IsOnewaySignal(TileIndex t, Track track)
321 {
322 return GetSignalType(t, track) != SIGTYPE_PBS;
323 }
324
325 static inline void CycleSignalSide(TileIndex t, Track track)
326 {
327 byte sig;
328 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 6;
329
330 sig = GB(_m[t].m3, pos, 2);
331 if (--sig == 0) sig = IsPbsSignal(GetSignalType(t, track)) ? 2 : 3;
332 SB(_m[t].m3, pos, 2, sig);
333 }
334
335 static inline SignalVariant GetSignalVariant(TileIndex t, Track track)
336 {
337 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
338 return (SignalVariant)GB(_m[t].m2, pos, 1);
339 }
340
341 static inline void SetSignalVariant(TileIndex t, Track track, SignalVariant v)
342 {
343 byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 7 : 3;
344 SB(_m[t].m2, pos, 1, v);
345 if (track == INVALID_TRACK) SB(_m[t].m2, 7, 1, v);
346 }
347
353 static inline void SetSignalStates(TileIndex tile, uint state)
354 {
355 SB(_m[tile].m4, 4, 4, state);
356 }
357
363 static inline uint GetSignalStates(TileIndex tile)
364 {
365 return GB(_m[tile].m4, 4, 4);
366 }
367
374 static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit)
375 {
376 return (SignalState)HasBit(GetSignalStates(t), signalbit);
377 }
378
384 static inline void SetPresentSignals(TileIndex tile, uint signals)
385 {
386 SB(_m[tile].m3, 4, 4, signals);
387 }
388
394 static inline uint GetPresentSignals(TileIndex tile)
395 {
396 return GB(_m[tile].m3, 4, 4);
397 }
398
405 static inline bool IsSignalPresent(TileIndex t, byte signalbit)
406 {
407 return HasBit(GetPresentSignals(t), signalbit);
408 }
409
414 static inline bool HasSignalOnTrack(TileIndex tile, Track track)
415 {
416 assert(IsValidTrack(track));
417 return GetRailTileType(tile) == RAIL_TILE_SIGNALS && (GetPresentSignals(tile) & SignalOnTrack(track)) != 0;
418 }
419
427 static inline bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
428 {
429 assert (IsValidTrackdir(trackdir));
430 return GetRailTileType(tile) == RAIL_TILE_SIGNALS && GetPresentSignals(tile) & SignalAlongTrackdir(trackdir);
431 }
432
439 static inline SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir)
440 {
441 assert(IsValidTrackdir(trackdir));
442 assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir)));
443 return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ?
444 SIGNAL_STATE_GREEN : SIGNAL_STATE_RED;
445 }
446
450 static inline void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state)
451 {
452 if (state == SIGNAL_STATE_GREEN) { // set 1
453 SetSignalStates(tile, GetSignalStates(tile) | SignalAlongTrackdir(trackdir));
454 } else {
455 SetSignalStates(tile, GetSignalStates(tile) & ~SignalAlongTrackdir(trackdir));
456 }
457 }
458
464 static inline bool HasPbsSignalOnTrackdir(TileIndex tile, Trackdir td)
465 {
466 return IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, td) &&
467 IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)));
468 }
469
476 static inline bool HasOnewaySignalBlockingTrackdir(TileIndex tile, Trackdir td)
477 {
478 return IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, ReverseTrackdir(td)) &&
479 !HasSignalOnTrackdir(tile, td) && IsOnewaySignal(tile, TrackdirToTrack(td));
480 }
481
482
483 RailType GetTileRailType(TileIndex tile);
484
486 enum RailGroundType {
487 RAIL_GROUND_BARREN = 0,
488 RAIL_GROUND_GRASS = 1,
489 RAIL_GROUND_FENCE_NW = 2,
490 RAIL_GROUND_FENCE_SE = 3,
491 RAIL_GROUND_FENCE_SENW = 4,
492 RAIL_GROUND_FENCE_NE = 5,
493 RAIL_GROUND_FENCE_SW = 6,
494 RAIL_GROUND_FENCE_NESW = 7,
495 RAIL_GROUND_FENCE_VERT1 = 8,
496 RAIL_GROUND_FENCE_VERT2 = 9,
497 RAIL_GROUND_FENCE_HORIZ1 = 10,
498 RAIL_GROUND_FENCE_HORIZ2 = 11,
499 RAIL_GROUND_ICE_DESERT = 12,
500 RAIL_GROUND_WATER = 13,
501 RAIL_GROUND_HALF_SNOW = 14,
502 };
503
504 static inline void SetRailGroundType(TileIndex t, RailGroundType rgt)
505 {
506 SB(_m[t].m4, 0, 4, rgt);
507 }
508
509 static inline RailGroundType GetRailGroundType(TileIndex t)
510 {
511 return (RailGroundType)GB(_m[t].m4, 0, 4);
512 }
513
514 static inline bool IsSnowRailGround(TileIndex t)
515 {
516 return GetRailGroundType(t) == RAIL_GROUND_ICE_DESERT;
517 }
518
519
520 static inline void MakeRailNormal(TileIndex t, Owner o, TrackBits b, RailType r)
521 {
522 SetTileType(t, MP_RAILWAY);
523 SetTileOwner(t, o);
524 _m[t].m2 = 0;
525 _m[t].m3 = r;
526 _m[t].m4 = 0;
527 _m[t].m5 = RAIL_TILE_NORMAL << 6 | b;
528 SB(_me[t].m6, 2, 4, 0);
529 _me[t].m7 = 0;
530 }
531
532
533 static inline void MakeRailDepot(TileIndex t, Owner o, DepotID did, DiagDirection d, RailType r)
534 {
535 SetTileType(t, MP_RAILWAY);
536 SetTileOwner(t, o);
537 _m[t].m2 = did;
538 _m[t].m3 = r;
539 _m[t].m4 = 0;
540 _m[t].m5 = RAIL_TILE_DEPOT << 6 | d;
541 SB(_me[t].m6, 2, 4, 0);
542 _me[t].m7 = 0;
543 }
544
545 #endif /* RAIL_MAP_H */