src/road_map.h
branchNewGRF_ports
changeset 6719 4cc327ad39d5
parent 6574 e1d1a12faaf7
child 6743 cabfaa4a0295
equal deleted inserted replaced
6718:5a8b295aa345 6719:4cc327ad39d5
     1 /* $Id$ */
     1 /* $Id$ */
       
     2 
       
     3 /** @file road_map.h */
     2 
     4 
     3 #ifndef ROAD_MAP_H
     5 #ifndef ROAD_MAP_H
     4 #define ROAD_MAP_H
     6 #define ROAD_MAP_H
     5 
     7 
     6 #include "macros.h"
     8 #include "macros.h"
    14 	ROAD_TILE_CROSSING,
    16 	ROAD_TILE_CROSSING,
    15 	ROAD_TILE_DEPOT
    17 	ROAD_TILE_DEPOT
    16 };
    18 };
    17 
    19 
    18 static inline RoadTileType GetRoadTileType(TileIndex t)
    20 static inline RoadTileType GetRoadTileType(TileIndex t)
    19 {
    21 	{
    20 	assert(IsTileType(t, MP_STREET));
    22 	assert(IsTileType(t, MP_STREET));
    21 	return (RoadTileType)GB(_m[t].m5, 4, 4);
    23 	return (RoadTileType)GB(_m[t].m5, 6, 2);
    22 }
    24 }
    23 
    25 
    24 static inline bool IsLevelCrossing(TileIndex t)
    26 static inline bool IsLevelCrossing(TileIndex t)
    25 {
    27 {
    26 	return GetRoadTileType(t) == ROAD_TILE_CROSSING;
    28 	return GetRoadTileType(t) == ROAD_TILE_CROSSING;
    29 static inline bool IsLevelCrossingTile(TileIndex t)
    31 static inline bool IsLevelCrossingTile(TileIndex t)
    30 {
    32 {
    31 	return IsTileType(t, MP_STREET) && IsLevelCrossing(t);
    33 	return IsTileType(t, MP_STREET) && IsLevelCrossing(t);
    32 }
    34 }
    33 
    35 
    34 static inline RoadBits GetRoadBits(TileIndex t)
    36 static inline RoadBits GetRoadBits(TileIndex t, RoadType rt)
    35 {
    37 {
    36 	assert(GetRoadTileType(t) == ROAD_TILE_NORMAL);
    38 	assert(GetRoadTileType(t) == ROAD_TILE_NORMAL);
    37 	return (RoadBits)GB(_m[t].m5, 0, 4);
    39 	switch (rt) {
    38 }
    40 		default: NOT_REACHED();
    39 
    41 		case ROADTYPE_ROAD: return (RoadBits)GB(_m[t].m4, 0, 4);
    40 static inline void SetRoadBits(TileIndex t, RoadBits r)
    42 		case ROADTYPE_TRAM: return (RoadBits)GB(_m[t].m4, 4, 4);
       
    43 		case ROADTYPE_HWAY: return (RoadBits)GB(_m[t].m6, 2, 4);
       
    44 	}
       
    45 }
       
    46 
       
    47 static inline RoadBits GetAllRoadBits(TileIndex tile)
       
    48 {
       
    49 	return GetRoadBits(tile, ROADTYPE_ROAD) | GetRoadBits(tile, ROADTYPE_TRAM) | GetRoadBits(tile, ROADTYPE_HWAY);
       
    50 }
       
    51 
       
    52 static inline void SetRoadBits(TileIndex t, RoadBits r, RoadType rt)
    41 {
    53 {
    42 	assert(GetRoadTileType(t) == ROAD_TILE_NORMAL); // XXX incomplete
    54 	assert(GetRoadTileType(t) == ROAD_TILE_NORMAL); // XXX incomplete
    43 	SB(_m[t].m5, 0, 4, r);
    55 	switch (rt) {
    44 }
    56 		default: NOT_REACHED();
    45 
    57 		case ROADTYPE_ROAD: SB(_m[t].m4, 0, 4, r); break;
       
    58 		case ROADTYPE_TRAM: SB(_m[t].m4, 4, 4, r); break;
       
    59 		case ROADTYPE_HWAY: SB(_m[t].m6, 2, 4, r); break;
       
    60 	}
       
    61 }
       
    62 
       
    63 static inline RoadTypes GetRoadTypes(TileIndex t)
       
    64 {
       
    65 	if (IsTileType(t, MP_STREET)) {
       
    66 		return (RoadTypes)GB(_me[t].m7, 5, 3);
       
    67 	} else {
       
    68 		return (RoadTypes)GB(_m[t].m3, 0, 3);
       
    69 	}
       
    70 }
       
    71 
       
    72 static inline void SetRoadTypes(TileIndex t, RoadTypes rt)
       
    73 {
       
    74 	if (IsTileType(t, MP_STREET)) {
       
    75 		SB(_me[t].m7, 5, 3, rt);
       
    76 	} else {
       
    77 		assert(IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE));
       
    78 		SB(_m[t].m3, 0, 2, rt);
       
    79 	}
       
    80 }
       
    81 
       
    82 static inline Owner GetRoadOwner(TileIndex t, RoadType rt)
       
    83 {
       
    84 	if (!IsTileType(t, MP_STREET)) return GetTileOwner(t);
       
    85 
       
    86 	switch (GetRoadTileType(t)) {
       
    87 		default: NOT_REACHED();
       
    88 		case ROAD_TILE_NORMAL:
       
    89 			switch (rt) {
       
    90 				default: NOT_REACHED();
       
    91 				case ROADTYPE_ROAD: return (Owner)GB( _m[t].m1, 0, 5);
       
    92 				case ROADTYPE_TRAM: {
       
    93 					/* Trams don't need OWNER_TOWN, and remapping OWNER_NONE
       
    94 					 * to OWNER_TOWN makes it use one bit less */
       
    95 					Owner o = (Owner)GB( _m[t].m5, 0, 4);
       
    96 					return o == OWNER_TOWN ? OWNER_NONE : o;
       
    97 				}
       
    98 				case ROADTYPE_HWAY: return (Owner)GB(_me[t].m7, 0, 5);
       
    99 			}
       
   100 		case ROAD_TILE_CROSSING:
       
   101 			switch (rt) {
       
   102 				default: NOT_REACHED();
       
   103 				case ROADTYPE_ROAD: return (Owner)GB( _m[t].m4, 0, 5);
       
   104 				case ROADTYPE_TRAM: {
       
   105 					/* Trams don't need OWNER_TOWN, and remapping OWNER_NONE
       
   106 					 * to OWNER_TOWN makes it use one bit less */
       
   107 					Owner o = (Owner)GB( _m[t].m5, 0, 4);
       
   108 					return o == OWNER_TOWN ? OWNER_NONE : o;
       
   109 				}
       
   110 				case ROADTYPE_HWAY: return (Owner)GB(_me[t].m7, 0, 5);
       
   111 			}
       
   112 		case ROAD_TILE_DEPOT: return GetTileOwner(t);
       
   113 	}
       
   114 }
       
   115 
       
   116 static inline void SetRoadOwner(TileIndex t, RoadType rt, Owner o)
       
   117 {
       
   118 	if (!IsTileType(t, MP_STREET)) return SetTileOwner(t, o);
       
   119 
       
   120 	switch (GetRoadTileType(t)) {
       
   121 		default: NOT_REACHED();
       
   122 		case ROAD_TILE_NORMAL:
       
   123 			switch (rt) {
       
   124 				default: NOT_REACHED();
       
   125 				case ROADTYPE_ROAD: SB( _m[t].m1, 0, 5, o); break;
       
   126 				case ROADTYPE_TRAM: SB( _m[t].m5, 0, 4, o == OWNER_NONE ? OWNER_TOWN : o); break;
       
   127 				case ROADTYPE_HWAY: SB(_me[t].m7, 0, 5, o); break;
       
   128 			}
       
   129 			break;
       
   130 		case ROAD_TILE_CROSSING:
       
   131 			switch (rt) {
       
   132 				default: NOT_REACHED();
       
   133 				case ROADTYPE_ROAD: SB( _m[t].m4, 0, 5, o); break;
       
   134 				/* Trams don't need OWNER_TOWN, and remapping OWNER_NONE
       
   135 				 * to OWNER_TOWN makes it use one bit less */
       
   136 				case ROADTYPE_TRAM: SB( _m[t].m5, 0, 4, o == OWNER_NONE ? OWNER_TOWN : o); break;
       
   137 				case ROADTYPE_HWAY: SB(_me[t].m7, 0, 5, o); break;
       
   138 			}
       
   139 			break;
       
   140 		case ROAD_TILE_DEPOT: return SetTileOwner(t, o);
       
   141 	}
       
   142 }
       
   143 
       
   144 /** Which directions are disallowed ? */
       
   145 enum DisallowedRoadDirections {
       
   146 	DRD_NONE,       ///< None of the directions are disallowed
       
   147 	DRD_SOUTHBOUND, ///< All southbound traffic is disallowed
       
   148 	DRD_NORTHBOUND, ///< All northbound traffic is disallowed
       
   149 	DRD_BOTH,       ///< All directions are disallowed
       
   150 	DRD_END
       
   151 };
       
   152 DECLARE_ENUM_AS_BIT_SET(DisallowedRoadDirections);
       
   153 
       
   154 /**
       
   155  * Gets the disallowed directions
       
   156  * @param t the tile to get the directions from
       
   157  * @return the disallowed directions
       
   158  */
       
   159 static inline DisallowedRoadDirections GetDisallowedRoadDirections(TileIndex t)
       
   160 {
       
   161 	assert(GetRoadTileType(t) == ROAD_TILE_NORMAL);
       
   162 	return (DisallowedRoadDirections)GB(_m[t].m5, 4, 2);
       
   163 }
       
   164 
       
   165 /**
       
   166  * Sets the disallowed directions
       
   167  * @param t   the tile to set the directions for
       
   168  * @param drd the disallowed directions
       
   169  */
       
   170 static inline void SetDisallowedRoadDirections(TileIndex t, DisallowedRoadDirections drd)
       
   171 {
       
   172 	assert(GetRoadTileType(t) == ROAD_TILE_NORMAL);
       
   173 	assert(drd < DRD_END);
       
   174 	SB(_m[t].m5, 4, 2, drd);
       
   175 }
    46 
   176 
    47 static inline Axis GetCrossingRoadAxis(TileIndex t)
   177 static inline Axis GetCrossingRoadAxis(TileIndex t)
    48 {
   178 {
    49 	assert(GetRoadTileType(t) == ROAD_TILE_CROSSING);
   179 	assert(GetRoadTileType(t) == ROAD_TILE_CROSSING);
    50 	return (Axis)GB(_m[t].m5, 3, 1);
   180 	return (Axis)GB(_m[t].m4, 6, 1);
    51 }
   181 }
    52 
   182 
    53 static inline RoadBits GetCrossingRoadBits(TileIndex tile)
   183 static inline RoadBits GetCrossingRoadBits(TileIndex tile)
    54 {
   184 {
    55 	return GetCrossingRoadAxis(tile) == AXIS_X ? ROAD_X : ROAD_Y;
   185 	return GetCrossingRoadAxis(tile) == AXIS_X ? ROAD_X : ROAD_Y;
    59 {
   189 {
    60 	return AxisToTrackBits(OtherAxis(GetCrossingRoadAxis(tile)));
   190 	return AxisToTrackBits(OtherAxis(GetCrossingRoadAxis(tile)));
    61 }
   191 }
    62 
   192 
    63 
   193 
    64 // TODO swap owner of road and rail
   194 static inline void UnbarCrossing(TileIndex t)
    65 static inline Owner GetCrossingRoadOwner(TileIndex t)
       
    66 {
   195 {
    67 	assert(GetRoadTileType(t) == ROAD_TILE_CROSSING);
   196 	assert(GetRoadTileType(t) == ROAD_TILE_CROSSING);
    68 	return (Owner)_m[t].m4;
   197 	CLRBIT(_m[t].m4, 5);
    69 }
   198 }
    70 
   199 
    71 static inline void SetCrossingRoadOwner(TileIndex t, Owner o)
   200 static inline void BarCrossing(TileIndex t)
    72 {
   201 {
    73 	assert(GetRoadTileType(t) == ROAD_TILE_CROSSING);
   202 	assert(GetRoadTileType(t) == ROAD_TILE_CROSSING);
    74 	_m[t].m4 = o;
   203 	SETBIT(_m[t].m4, 5);
    75 }
   204 }
    76 
   205 
    77 static inline void UnbarCrossing(TileIndex t)
   206 static inline bool IsCrossingBarred(TileIndex t)
    78 {
   207 {
    79 	assert(GetRoadTileType(t) == ROAD_TILE_CROSSING);
   208 	assert(GetRoadTileType(t) == ROAD_TILE_CROSSING);
    80 	CLRBIT(_m[t].m5, 2);
   209 	return HASBIT(_m[t].m4, 5);
    81 }
       
    82 
       
    83 static inline void BarCrossing(TileIndex t)
       
    84 {
       
    85 	assert(GetRoadTileType(t) == ROAD_TILE_CROSSING);
       
    86 	SETBIT(_m[t].m5, 2);
       
    87 }
       
    88 
       
    89 static inline bool IsCrossingBarred(TileIndex t)
       
    90 {
       
    91 	assert(GetRoadTileType(t) == ROAD_TILE_CROSSING);
       
    92 	return HASBIT(_m[t].m5, 2);
       
    93 }
   210 }
    94 
   211 
    95 #define IsOnDesert IsOnSnow
   212 #define IsOnDesert IsOnSnow
    96 static inline bool IsOnSnow(TileIndex t)
   213 static inline bool IsOnSnow(TileIndex t)
    97 {
   214 {
   164 }
   281 }
   165 
   282 
   166 
   283 
   167 /**
   284 /**
   168  * Returns the RoadBits on an arbitrary tile
   285  * Returns the RoadBits on an arbitrary tile
   169  * Special behavior:
   286  * Special behaviour:
   170  * - road depots: entrance is treated as road piece
   287  * - road depots: entrance is treated as road piece
   171  * - road tunnels: entrance is treated as road piece
   288  * - road tunnels: entrance is treated as road piece
   172  * - bridge ramps: start of the ramp is treated as road piece
   289  * - bridge ramps: start of the ramp is treated as road piece
   173  * - bridge middle parts: bridge itself is ignored
   290  * - bridge middle parts: bridge itself is ignored
       
   291  * @param tile the tile to get the road bits for
       
   292  * @param rt   the road type to get the road bits form
       
   293  * @return the road bits of the given tile
   174  */
   294  */
   175 RoadBits GetAnyRoadBits(TileIndex);
   295 RoadBits GetAnyRoadBits(TileIndex tile, RoadType rt);
   176 
   296 
   177 
   297 /**
   178 TrackBits GetAnyRoadTrackBits(TileIndex tile);
   298  * Get the accessible track bits for the given tile.
   179 
   299  * Special behaviour:
   180 
   300  *  - road depots: no track bits
   181 static inline void MakeRoadNormal(TileIndex t, Owner owner, RoadBits bits, TownID town)
   301  *  - non-drive-through stations: no track bits
       
   302  * @param tile the tile to get the track bits for
       
   303  * @return the track bits for the given tile
       
   304  */
       
   305 TrackBits GetAnyRoadTrackBits(TileIndex tile, RoadType rt);
       
   306 
       
   307 
       
   308 static inline void MakeRoadNormal(TileIndex t, RoadBits bits, RoadTypes rot, TownID town, Owner road, Owner tram, Owner hway)
   182 {
   309 {
   183 	SetTileType(t, MP_STREET);
   310 	SetTileType(t, MP_STREET);
   184 	SetTileOwner(t, owner);
   311 	SetTileOwner(t, road);
   185 	_m[t].m2 = town;
   312 	_m[t].m2 = town;
   186 	_m[t].m3 = 0 << 7 | 0 << 4 | 0;
   313 	_m[t].m3 = 0;
   187 	_m[t].m4 = 0;
   314 	_m[t].m4 = (HASBIT(rot, ROADTYPE_TRAM) ? bits : 0) << 4 | (HASBIT(rot, ROADTYPE_ROAD) ? bits : 0);
   188 	_m[t].m5 = ROAD_TILE_NORMAL << 4 | bits;
   315 	_m[t].m5 = ROAD_TILE_NORMAL << 6;
   189 }
   316 	SetRoadOwner(t, ROADTYPE_TRAM, tram);
   190 
   317 	SB(_m[t].m6, 2, 4, HASBIT(rot, ROADTYPE_HWAY) ? bits : 0);
   191 
   318 	_me[t].m7 = rot << 5 | hway;
   192 static inline void MakeRoadCrossing(TileIndex t, Owner road, Owner rail, Axis roaddir, RailType rt, uint town)
   319 }
       
   320 
       
   321 
       
   322 static inline void MakeRoadCrossing(TileIndex t, Owner road, Owner tram, Owner hway, Owner rail, Axis roaddir, RailType rat, RoadTypes rot, uint town)
   193 {
   323 {
   194 	SetTileType(t, MP_STREET);
   324 	SetTileType(t, MP_STREET);
   195 	SetTileOwner(t, rail);
   325 	SetTileOwner(t, rail);
   196 	_m[t].m2 = town;
   326 	_m[t].m2 = town;
   197 	_m[t].m3 = 0 << 7 | 0 << 4 | rt;
   327 	_m[t].m3 = rat;
   198 	_m[t].m4 = road;
   328 	_m[t].m4 = roaddir << 6 | road;
   199 	_m[t].m5 = ROAD_TILE_CROSSING << 4 | roaddir << 3 | 0 << 2;
   329 	_m[t].m5 = ROAD_TILE_CROSSING << 6;
   200 }
   330 	SetRoadOwner(t, ROADTYPE_TRAM, tram);
   201 
   331 	SB(_m[t].m6, 2, 4, 0);
   202 
   332 	_me[t].m7 = rot << 5 | hway;
   203 static inline void MakeRoadDepot(TileIndex t, Owner owner, DiagDirection dir)
   333 }
       
   334 
       
   335 
       
   336 static inline void MakeRoadDepot(TileIndex t, Owner owner, DiagDirection dir, RoadType rt)
   204 {
   337 {
   205 	SetTileType(t, MP_STREET);
   338 	SetTileType(t, MP_STREET);
   206 	SetTileOwner(t, owner);
   339 	SetTileOwner(t, owner);
   207 	_m[t].m2 = 0;
   340 	_m[t].m2 = 0;
   208 	_m[t].m3 = 0;
   341 	_m[t].m3 = 0;
   209 	_m[t].m4 = 0;
   342 	_m[t].m4 = 0;
   210 	_m[t].m5 = ROAD_TILE_DEPOT << 4 | dir;
   343 	_m[t].m5 = ROAD_TILE_DEPOT << 6 | dir;
       
   344 	SB(_m[t].m6, 2, 4, 0);
       
   345 	_me[t].m7 = RoadTypeToRoadTypes(rt) << 5;
   211 }
   346 }
   212 
   347 
   213 #endif /* ROAD_MAP_H */
   348 #endif /* ROAD_MAP_H */