diff --git a/bridge_map.h b/bridge_map.h new file mode 100644 index 0000000000..9f07db383d --- /dev/null +++ b/bridge_map.h @@ -0,0 +1,64 @@ +/* $Id$ */ + +#ifndef BRIDGE_MAP_H +#define BRIDGE_MAP_H + +#include "direction.h" +#include "macros.h" +#include "map.h" +#include "rail.h" +#include "tile.h" + + +static inline bool IsBridge(TileIndex t) +{ + assert(IsTileType(t, MP_TUNNELBRIDGE)); + return HASBIT(_m[t].m5, 7); +} + +static inline bool IsBridgeTile(TileIndex t) +{ + return IsTileType(t, MP_TUNNELBRIDGE) && IsBridge(t); +} + + +static inline bool IsBridgeRamp(TileIndex t) +{ + assert(IsBridgeTile(t)); + return !HASBIT(_m[t].m5, 6); +} + +static inline bool IsBridgeMiddle(TileIndex t) +{ + assert(IsBridgeTile(t)); + return HASBIT(_m[t].m5, 6); +} + + +/** + * Get the direction pointing onto the bridge + */ +static inline DiagDirection GetBridgeRampDirection(TileIndex t) +{ + assert(IsBridgeRamp(t)); + /* Heavy wizardry to convert the X/Y (bit 0) + N/S (bit 5) encoding of + * bridges to a DiagDirection + */ + return (DiagDirection)((6 - (_m[t].m5 >> 4 & 2) - (_m[t].m5 & 1)) % 4); +} + + +static inline Axis GetBridgeAxis(TileIndex t) +{ + assert(IsBridgeMiddle(t)); + return (Axis)GB(_m[t].m5, 0, 1); +} + + +static inline bool IsTransportUnderBridge(TileIndex t) +{ + assert(IsBridgeMiddle(t)); + return HASBIT(_m[t].m5, 5); +} + +#endif diff --git a/direction.h b/direction.h new file mode 100644 index 0000000000..e6cf57a9ff --- /dev/null +++ b/direction.h @@ -0,0 +1,30 @@ +/* $Id$ */ + +#ifndef DIRECTION_H +#define DIRECTION_H + + +/* the 2 axis */ +typedef enum Axis { + AXIS_X = 0, + AXIS_Y = 1, + AXIS_END +} Axis; + + +static inline Axis DiagDirToAxis(uint d) +{ + return (Axis)(d & 1); +} + + +/* + * Converts an Axis to a DiagDirection + * Points always in the positive direction, i.e. S[EW] + */ +static inline uint AxisToDiagDir(Axis a) +{ + return (uint)(2 - a); +} + +#endif diff --git a/pathfind.c b/pathfind.c index c6f88720ed..99d50a99ac 100644 --- a/pathfind.c +++ b/pathfind.c @@ -2,10 +2,12 @@ #include "stdafx.h" #include "openttd.h" +#include "bridge_map.h" #include "functions.h" #include "map.h" #include "tile.h" #include "rail.h" +#include "rail_map.h" #include "pathfind.h" #include "debug.h" #include "variables.h" @@ -672,12 +674,6 @@ static const byte _length_of_track[16] = { DIAG_FACTOR,DIAG_FACTOR,STR_FACTOR,STR_FACTOR,STR_FACTOR,STR_FACTOR,0,0 }; -static inline bool IsBridgeMiddle(TileIndex t) {return HASBIT(_m[t].m5, 6);} -static inline uint GetBridgeAxis(TileIndex t) {return GB(_m[t].m5, 0, 1);} -static inline uint DiagDirToAxis(DiagDirection d) {return (d & 1);} -static inline bool IsBridge(TileIndex t) {return HASBIT(_m[t].m5, 7);} -static inline bool IsBridgeTile(TileIndex t) {return IsTileType(t, MP_TUNNELBRIDGE) && IsBridge(t);} -static inline RailType GetRailTypeCrossing(TileIndex t) {return (RailType)GB(_m[t].m4, 0, 4);} // new more optimized pathfinder for trains... // Tile is the tile the train is at. diff --git a/rail_map.h b/rail_map.h new file mode 100644 index 0000000000..438d3ff51b --- /dev/null +++ b/rail_map.h @@ -0,0 +1,14 @@ +/* $Id$ */ + +#ifndef RAIL_MAP_H +#define RAIL_MAP_H + +#include "tile.h" + +// TODO remove this by moving to the same bits as GetRailType() +static inline RailType GetRailTypeCrossing(TileIndex t) +{ + return (RailType)GB(_m[t].m4, 0, 4); +} + +#endif diff --git a/tunnel_map.h b/tunnel_map.h new file mode 100644 index 0000000000..2a7c6e4310 --- /dev/null +++ b/tunnel_map.h @@ -0,0 +1,23 @@ +/* $Id$ */ + +#ifndef TUNNEL_MAP_H +#define TUNNEL_MAP_H + +#include "macros.h" +#include "map.h" + + +static inline bool IsTunnel(TileIndex t) +{ + assert(IsTileType(t, MP_TUNNELBRIDGE)); + return !HASBIT(_m[t].m5, 7); +} + + +static inline uint GetTunnelDirection(TileIndex t) +{ + assert(IsTunnelTile(t)); + return (uint)GB(_m[t].m5, 0, 2); +} + +#endif diff --git a/tunnelbridge_cmd.c b/tunnelbridge_cmd.c index d8ee7d9277..7a3f6abbc7 100644 --- a/tunnelbridge_cmd.c +++ b/tunnelbridge_cmd.c @@ -7,11 +7,13 @@ #include "stdafx.h" #include "openttd.h" +#include "bridge_map.h" #include "table/sprites.h" #include "table/strings.h" #include "functions.h" #include "map.h" #include "tile.h" +#include "tunnel_map.h" #include "vehicle.h" #include "viewport.h" #include "command.h" @@ -1200,13 +1202,6 @@ static void DrawTile_TunnelBridge(TileInfo *ti) } } -static inline bool IsTunnel(TileIndex t) {return !HASBIT(_m[t].m5, 7);} -static inline uint DiagDirToAxis(DiagDirection d) {return (d & 1);} -static inline DiagDirection GetTunnelDirection(TileIndex t) {return (DiagDirection)GB(_m[t].m5, 0, 2);} -static inline bool IsBridgeRamp(TileIndex t) {return !HASBIT(_m[t].m5, 6);} -static inline DiagDirection GetBridgeRampDirection(TileIndex t) {return (DiagDirection)((6 - (_m[t].m5 >> 4 & 2) - (_m[t].m5 & 1)) % 4);} -static inline bool IsTransportUnderBridge(TileIndex t) {return HASBIT(_m[t].m5, 5);} -static inline uint GetBridgeAxis(TileIndex t) {return GB(_m[t].m5, 0, 1);} static uint GetSlopeZ_TunnelBridge(const TileInfo* ti) {