From fbe939b31f8c7ea4e6e47d6d903625dd25c5e856 Mon Sep 17 00:00:00 2001 From: tron Date: Wed, 8 Mar 2006 06:55:33 +0000 Subject: (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends --- ai/trolly/build.c | 6 ++-- ai/trolly/pathfinder.c | 2 +- ai/trolly/trolly.c | 7 ++--- ai/trolly/trolly.h | 2 +- aircraft_cmd.c | 2 +- aircraft_gui.c | 4 +-- direction.h | 5 ++++ disaster_cmd.c | 56 ++++++++++++++++++++++---------------- dock_gui.c | 6 ++-- industry_cmd.c | 18 ++++++------ newgrf_engine.c | 2 +- newgrf_engine.h | 4 ++- pathfind.c | 4 +-- rail_cmd.c | 14 ++++------ rail_gui.c | 6 ++-- roadveh_cmd.c | 32 ++++++++++++---------- roadveh_gui.c | 4 +-- ship_cmd.c | 27 ++++++++++-------- ship_gui.c | 4 +-- station_cmd.c | 51 +++++++++++++++++----------------- train_cmd.c | 34 +++++++++++------------ train_gui.c | 4 +-- tunnelbridge_cmd.c | 74 ++++++++++++++++++++++++-------------------------- vehicle.c | 13 +++++---- vehicle.h | 10 +++---- water_cmd.c | 17 ++++++------ waypoint.c | 4 +-- 27 files changed, 215 insertions(+), 197 deletions(-) diff --git a/ai/trolly/build.c b/ai/trolly/build.c index 235166cab..2d0b25ec5 100644 --- a/ai/trolly/build.c +++ b/ai/trolly/build.c @@ -5,6 +5,7 @@ #include "../../debug.h" #include "../../functions.h" #include "../../map.h" +#include "../../road_map.h" #include "../../tile.h" #include "../../command.h" #include "trolly.h" @@ -255,9 +256,8 @@ int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag) return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH); } -int AiNew_Build_Depot(Player *p, TileIndex tile, byte direction, byte flag) +int AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag) { - static const byte _roadbits_by_dir[4] = {2,1,8,4}; int ret, ret2; if (p->ainew.tbt == AI_TRAIN) return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT); @@ -265,7 +265,7 @@ int AiNew_Build_Depot(Player *p, TileIndex tile, byte direction, byte flag) ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT); if (CmdFailed(ret)) return ret; // Try to build the road from the depot - ret2 = AI_DoCommand(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD); + ret2 = AI_DoCommand(tile + TileOffsByDir(direction), DiagDirToRoadBits(ReverseDiagDir(direction)), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD); // If it fails, ignore it.. if (CmdFailed(ret2)) return ret; return ret + ret2; diff --git a/ai/trolly/pathfinder.c b/ai/trolly/pathfinder.c index 1f77e611f..d5244a1da 100644 --- a/ai/trolly/pathfinder.c +++ b/ai/trolly/pathfinder.c @@ -372,7 +372,7 @@ static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *curr extern uint GetRailFoundation(uint tileh, uint bits); extern uint GetRoadFoundation(uint tileh, uint bits); -extern uint GetBridgeFoundation(uint tileh, byte direction); +extern uint GetBridgeFoundation(uint tileh, Axis); // XXX function declaration in .c enum { BRIDGE_NO_FOUNDATION = 1 << 0 | 1 << 3 | 1 << 6 | 1 << 9 | 1 << 12, }; diff --git a/ai/trolly/trolly.c b/ai/trolly/trolly.c index 6e8d68d8c..2ccee6fb1 100644 --- a/ai/trolly/trolly.c +++ b/ai/trolly/trolly.c @@ -1024,7 +1024,6 @@ static void AiNew_State_BuildPath(Player *p) // This means we are done building! if (p->ainew.tbt == AI_TRUCK && !_patches.roadveh_queue) { - static const byte _roadbits_by_dir[4] = {2,1,8,4}; // If they not queue, they have to go up and down to try again at a station... // We don't want that, so try building some road left or right of the station int dir1, dir2, dir3; @@ -1047,7 +1046,7 @@ static void AiNew_State_BuildPath(Player *p) dir3 = p->ainew.to_direction; } - ret = AI_DoCommand(tile, _roadbits_by_dir[dir1], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); + ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir1)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); if (!CmdFailed(ret)) { dir1 = TileOffsByDir(dir1); if (IsTileType(tile + dir1, MP_CLEAR) || IsTileType(tile + dir1, MP_TREES)) { @@ -1059,7 +1058,7 @@ static void AiNew_State_BuildPath(Player *p) } } - ret = AI_DoCommand(tile, _roadbits_by_dir[dir2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); + ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir2)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); if (!CmdFailed(ret)) { dir2 = TileOffsByDir(dir2); if (IsTileType(tile + dir2, MP_CLEAR) || IsTileType(tile + dir2, MP_TREES)) { @@ -1071,7 +1070,7 @@ static void AiNew_State_BuildPath(Player *p) } } - ret = AI_DoCommand(tile, _roadbits_by_dir[dir3^2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); + ret = AI_DoCommand(tile, DiagDirToRoadBits(dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); if (!CmdFailed(ret)) { dir3 = TileOffsByDir(dir3); if (IsTileType(tile + dir3, MP_CLEAR) || IsTileType(tile + dir3, MP_TREES)) { diff --git a/ai/trolly/trolly.h b/ai/trolly/trolly.h index b1a6ecce9..acf371896 100644 --- a/ai/trolly/trolly.h +++ b/ai/trolly/trolly.h @@ -256,6 +256,6 @@ int AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag) int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag); int AiNew_PickVehicle(Player *p); int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag); -int AiNew_Build_Depot(Player *p, TileIndex tile, byte direction, byte flag); +int AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag); #endif /* AI_TROLLY_H */ diff --git a/aircraft_cmd.c b/aircraft_cmd.c index 4b104f114..f74d92ec2 100644 --- a/aircraft_cmd.c +++ b/aircraft_cmd.c @@ -91,7 +91,7 @@ static bool HaveHangarInOrderList(Vehicle *v) } #endif -int GetAircraftImage(const Vehicle *v, byte direction) +int GetAircraftImage(const Vehicle* v, Direction direction) { int spritenum = v->spritenum; diff --git a/aircraft_gui.c b/aircraft_gui.c index 3a78c2134..398bb1fc9 100644 --- a/aircraft_gui.c +++ b/aircraft_gui.c @@ -65,7 +65,7 @@ void DrawAircraftPurchaseInfo(int x, int y, EngineID engine_number) static void DrawAircraftImage(const Vehicle *v, int x, int y, VehicleID selection) { PalSpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v); - DrawSprite(GetAircraftImage(v, 6) | pal, x + 25, y + 10); + DrawSprite(GetAircraftImage(v, DIR_W) | pal, x + 25, y + 10); if (v->subtype == 0) DrawSprite(SPR_ROTOR_STOPPED, x + 25, y + 5); if (v->index == selection) { DrawFrameRect(x - 1, y - 1, x + 58, y + 21, 0xF, FR_BORDERONLY); @@ -730,7 +730,7 @@ static void AircraftDepotClickAircraft(Window *w, int x, int y) if (v != NULL) { WP(w,traindepot_d).sel = v->index; SetWindowDirty(w); - SetObjectToPlaceWnd(GetVehiclePalette(v) | GetAircraftImage(v, 6), 4, w); + SetObjectToPlaceWnd(GetVehiclePalette(v) | GetAircraftImage(v, DIR_W), 4, w); } break; diff --git a/direction.h b/direction.h index fb99071f3..e29fb3c49 100644 --- a/direction.h +++ b/direction.h @@ -17,6 +17,11 @@ typedef enum Direction { INVALID_DIR = 0xFF, } Direction; +static inline Direction ReverseDir(Direction d) +{ + return (Direction)(4 ^ d); +} + /* Direction commonly used as the direction of entering and leaving tiles, 4-way */ typedef enum DiagDirection { diff --git a/disaster_cmd.c b/disaster_cmd.c index 2c8c6e73c..61590c47a 100644 --- a/disaster_cmd.c +++ b/disaster_cmd.c @@ -74,7 +74,7 @@ static void DisasterVehicleUpdateImage(Vehicle *v) v->cur_image = img; } -static void InitializeDisasterVehicle(Vehicle *v, int x, int y, byte z, byte direction, byte subtype) +static void InitializeDisasterVehicle(Vehicle* v, int x, int y, byte z, Direction direction, byte subtype) { v->type = VEH_Disaster; v->x_pos = x; @@ -552,7 +552,7 @@ static void DisasterTick_4(Vehicle *v) return; } - InitializeDisasterVehicle(u, -6*16, v->y_pos, 135, 5, 11); + InitializeDisasterVehicle(u, -6 * 16, v->y_pos, 135, DIR_SW, 11); u->u.disaster.unk2 = v->index; w = ForceAllocateSpecialVehicle(); @@ -560,7 +560,7 @@ static void DisasterTick_4(Vehicle *v) return; u->next = w; - InitializeDisasterVehicle(w, -6*16, v->y_pos, 0, 5, 12); + InitializeDisasterVehicle(w, -6 * 16, v->y_pos, 0, DIR_SW, 12); w->vehstatus |= VS_DISASTER; } else if (v->current_order.station < 1) { @@ -720,13 +720,13 @@ static void Disaster0_Init(void) } } - InitializeDisasterVehicle(v, x, 0, 135, 3, 0); + InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, 0); // Allocate shadow too? u = ForceAllocateSpecialVehicle(); if (u != NULL) { v->next = u; - InitializeDisasterVehicle(u, x, 0, 0, 3, 1); + InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, 1); u->vehstatus |= VS_DISASTER; } } @@ -741,7 +741,7 @@ static void Disaster1_Init(void) x = TileX(Random()) * 16 + 8; - InitializeDisasterVehicle(v, x, 0, 135, 3, 2); + InitializeDisasterVehicle(v, x, 0, 135, DIR_SE, 2); v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2); v->age = 0; @@ -749,7 +749,7 @@ static void Disaster1_Init(void) u = ForceAllocateSpecialVehicle(); if (u != NULL) { v->next = u; - InitializeDisasterVehicle(u,x,0,0,3,3); + InitializeDisasterVehicle(u, x, 0, 0, DIR_SE, 3); u->vehstatus |= VS_DISASTER; } } @@ -780,12 +780,12 @@ static void Disaster2_Init(void) x = (MapSizeX() + 9) * 16 - 1; y = TileY(found->xy) * 16 + 37; - InitializeDisasterVehicle(v,x,y, 135,1,4); + InitializeDisasterVehicle(v, x, y, 135, DIR_NE, 4); u = ForceAllocateSpecialVehicle(); if (u != NULL) { v->next = u; - InitializeDisasterVehicle(u,x,y,0,3,5); + InitializeDisasterVehicle(u, x, y, 0, DIR_SE, 5); u->vehstatus |= VS_DISASTER; } } @@ -816,18 +816,18 @@ static void Disaster3_Init(void) x = -16 * 16; y = TileY(found->xy) * 16 + 37; - InitializeDisasterVehicle(v,x,y, 135,5,6); + InitializeDisasterVehicle(v, x, y, 135, DIR_SW, 6); u = ForceAllocateSpecialVehicle(); if (u != NULL) { v->next = u; - InitializeDisasterVehicle(u,x,y,0,5,7); + InitializeDisasterVehicle(u, x, y, 0, DIR_SW, 7); u->vehstatus |= VS_DISASTER; w = ForceAllocateSpecialVehicle(); if (w != NULL) { u->next = w; - InitializeDisasterVehicle(w,x,y,140,5,8); + InitializeDisasterVehicle(w, x, y, 140, DIR_SW, 8); } } } @@ -842,7 +842,7 @@ static void Disaster4_Init(void) x = TileX(Random()) * 16 + 8; y = MapMaxX() * 16 - 1; - InitializeDisasterVehicle(v, x, y, 135, 7, 9); + InitializeDisasterVehicle(v, x, y, 135, DIR_NW, 9); v->dest_tile = TileXY(MapSizeX() / 2, MapSizeY() / 2); v->age = 0; @@ -850,7 +850,7 @@ static void Disaster4_Init(void) u = ForceAllocateSpecialVehicle(); if (u != NULL) { v->next = u; - InitializeDisasterVehicle(u,x,y,0,7,10); + InitializeDisasterVehicle(u, x, y, 0, DIR_NW, 10); u->vehstatus |= VS_DISASTER; } } @@ -860,7 +860,7 @@ static void Disaster5_Init(void) { Vehicle *v = ForceAllocateSpecialVehicle(); int x,y; - byte dir; + Direction dir; uint32 r; if (v == NULL) return; @@ -868,10 +868,14 @@ static void Disaster5_Init(void) r = Random(); x = TileX(r) * 16 + 8; - y = 8; - dir = 3; - if (r & 0x80000000) { y = MapMaxX() * 16 - 8 - 1; dir = 7; } - InitializeDisasterVehicle(v, x, y, 0, dir,13); + if (r & 0x80000000) { + y = MapMaxX() * 16 - 8 - 1; + dir = DIR_NW; + } else { + y = 8; + dir = DIR_SE; + } + InitializeDisasterVehicle(v, x, y, 0, dir, 13); v->age = 0; } @@ -880,7 +884,7 @@ static void Disaster6_Init(void) { Vehicle *v = ForceAllocateSpecialVehicle(); int x,y; - byte dir; + Direction dir; uint32 r; if (v == NULL) return; @@ -888,10 +892,14 @@ static void Disaster6_Init(void) r = Random(); x = TileX(r) * 16 + 8; - y = 8; - dir = 3; - if (r & 0x80000000) { y = MapMaxX() * 16 - 8 - 1; dir = 7; } - InitializeDisasterVehicle(v, x, y, 0, dir,14); + if (r & 0x80000000) { + y = MapMaxX() * 16 - 8 - 1; + dir = DIR_NW; + } else { + y = 8; + dir = DIR_SE; + } + InitializeDisasterVehicle(v, x, y, 0, dir, 14); v->age = 0; } diff --git a/dock_gui.c b/dock_gui.c index 4f5e47d43..da664345d 100644 --- a/dock_gui.c +++ b/dock_gui.c @@ -18,7 +18,7 @@ static void ShowBuildDockStationPicker(void); static void ShowBuildDocksDepotPicker(void); -static byte _ship_depot_direction; +static Axis _ship_depot_direction; void CcBuildDocks(bool success, TileIndex tile, uint32 p1, uint32 p2) { @@ -293,7 +293,7 @@ static void ShowBuildDockStationPicker(void) static void UpdateDocksDirection(void) { - if (_ship_depot_direction != 0) { + if (_ship_depot_direction != AXIS_X) { SetTileSelectSize(1, 2); } else { SetTileSelectSize(2, 1); @@ -362,5 +362,5 @@ static void ShowBuildDocksDepotPicker(void) void InitializeDockGui(void) { - _ship_depot_direction = 0; + _ship_depot_direction = AXIS_X; } diff --git a/industry_cmd.c b/industry_cmd.c index c8100e304..c949d2360 100644 --- a/industry_cmd.c +++ b/industry_cmd.c @@ -907,7 +907,7 @@ static bool IsBadFarmFieldTile2(TileIndex tile) } } -static void SetupFarmFieldFence(TileIndex tile, int size, byte type, int direction) +static void SetupFarmFieldFence(TileIndex tile, int size, byte type, Axis direction) { do { tile = TILE_MASK(tile); @@ -917,14 +917,14 @@ static void SetupFarmFieldFence(TileIndex tile, int size, byte type, int directi if (or == 1 && CHANCE16(1, 7)) or = 2; - if (direction) { - SetFenceSW(tile, or); - } else { + if (direction == AXIS_X) { SetFenceSE(tile, or); + } else { + SetFenceSW(tile, or); } } - tile += direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0); + tile += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); } while (--size); } @@ -984,10 +984,10 @@ static void PlantFarmField(TileIndex tile) type = _plantfarmfield_type[Random() & 0xF]; } - SetupFarmFieldFence(tile - TileDiffXY(1, 0), size_y, type, 1); - SetupFarmFieldFence(tile - TileDiffXY(0, 1), size_x, type, 0); - SetupFarmFieldFence(tile + TileDiffXY(size_x - 1, 0), size_y, type, 1); - SetupFarmFieldFence(tile + TileDiffXY(0, size_y - 1), size_x, type, 0); + SetupFarmFieldFence(tile - TileDiffXY(1, 0), size_y, type, AXIS_Y); + SetupFarmFieldFence(tile - TileDiffXY(0, 1), size_x, type, AXIS_X); + SetupFarmFieldFence(tile + TileDiffXY(size_x - 1, 0), size_y, type, AXIS_Y); + SetupFarmFieldFence(tile + TileDiffXY(0, size_y - 1), size_x, type, AXIS_X); } static void MaybePlantFarmField(const Industry* i) diff --git a/newgrf_engine.c b/newgrf_engine.c index 72f705771..1fd9e00f3 100644 --- a/newgrf_engine.c +++ b/newgrf_engine.c @@ -388,7 +388,7 @@ static const SpriteGroup *GetVehicleSpriteGroup(EngineID engine, const Vehicle * return group; } -int GetCustomEngineSprite(EngineID engine, const Vehicle *v, byte direction) +int GetCustomEngineSprite(EngineID engine, const Vehicle* v, Direction direction) { const SpriteGroup *group; const RealSpriteGroup *rsg; diff --git a/newgrf_engine.h b/newgrf_engine.h index 94b787c33..acbdfc5aa 100644 --- a/newgrf_engine.h +++ b/newgrf_engine.h @@ -3,6 +3,8 @@ #ifndef NEWGRF_ENGINE_H #define NEWGRF_ENGINE_H +#include "direction.h" + /** @file newgrf_engine.h */ @@ -15,7 +17,7 @@ VARDEF const uint32 cargo_classes[16]; void SetWagonOverrideSprites(EngineID engine, struct SpriteGroup *group, byte *train_id, int trains); void SetCustomEngineSprites(EngineID engine, byte cargo, struct SpriteGroup *group); // loaded is in percents, overriding_engine 0xffff is none -int GetCustomEngineSprite(EngineID engine, const Vehicle *v, byte direction); +int GetCustomEngineSprite(EngineID engine, const Vehicle* v, Direction direction); uint16 GetCallBackResult(uint16 callback_info, EngineID engine, const Vehicle *v); bool UsesWagonOverride(const Vehicle *v); #define GetCustomVehicleSprite(v, direction) GetCustomEngineSprite(v->engine_type, v, direction) diff --git a/pathfind.c b/pathfind.c index b0518f6fd..2cdf1884a 100644 --- a/pathfind.c +++ b/pathfind.c @@ -210,7 +210,7 @@ static const int8 _get_tunlen_inc[5] = { -16, 0, 16, 0, -16 }; /* Returns the end tile and the length of a tunnel. The length does not * include the starting tile (entry), it does include the end tile (exit). */ -FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, uint direction) +FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, DiagDirection direction) { FindLengthOfTunnelResult flotr; int x,y; @@ -246,7 +246,7 @@ FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, uint direction) static const uint16 _tpfmode1_and[4] = { 0x1009, 0x16, 0x520, 0x2A00 }; -static uint SkipToEndOfTunnel(TrackPathFinder *tpf, TileIndex tile, int direction) +static uint SkipToEndOfTunnel(TrackPathFinder* tpf, TileIndex tile, DiagDirection direction) { FindLengthOfTunnelResult flotr; TPFSetTileBit(tpf, tile, 14); diff --git a/rail_cmd.c b/rail_cmd.c index 3aad4431e..5f19dec00 100644 --- a/rail_cmd.c +++ b/rail_cmd.c @@ -1788,8 +1788,8 @@ bool UpdateSignalsOnSegment(TileIndex tile, Direction dir) void SetSignalsOnBothDir(TileIndex tile, byte track) { - static const byte _search_dir_1[6] = {1, 3, 1, 3, 5, 3}; - static const byte _search_dir_2[6] = {5, 7, 7, 5, 7, 1}; + static const Direction _search_dir_1[] = { DIR_NE, DIR_SE, DIR_NE, DIR_SE, DIR_SW, DIR_SE }; + static const Direction _search_dir_2[] = { DIR_SW, DIR_NW, DIR_NW, DIR_SW, DIR_NW, DIR_NE }; UpdateSignalsOnSegment(tile, _search_dir_1[track]); UpdateSignalsOnSegment(tile, _search_dir_2[track]); @@ -2069,15 +2069,13 @@ static const byte _deltacoord_leaveoffset[8] = { -1, 0, 1, 0, /* x */ 0, 1, 0, -1 /* y */ }; -static const byte _enter_directions[4] = {5, 7, 1, 3}; -static const byte _leave_directions[4] = {1, 3, 5, 7}; static const byte _depot_track_mask[4] = {1, 2, 1, 2}; static uint32 VehicleEnter_Track(Vehicle *v, TileIndex tile, int x, int y) { byte fract_coord; byte fract_coord_leave; - int dir; + DiagDirection dir; int length; // this routine applies only to trains in depot tiles @@ -2102,11 +2100,11 @@ static uint32 VehicleEnter_Track(Vehicle *v, TileIndex tile, int x, int y) /* make sure a train is not entering the tile from behind */ return 8; } else if (_fractcoords_enter[dir] == fract_coord) { - if (_enter_directions[dir] == v->direction) { + if (DiagDirToDir(ReverseDiagDir(dir)) == v->direction) { /* enter the depot */ v->u.rail.track = 0x80, v->vehstatus |= VS_HIDDEN; /* hide it */ - v->direction ^= 4; + v->direction = ReverseDir(v->direction); if (v->next == NULL) TrainEnterDepot(v, tile); v->tile = tile; @@ -2114,7 +2112,7 @@ static uint32 VehicleEnter_Track(Vehicle *v, TileIndex tile, int x, int y) return 4; } } else if (fract_coord_leave == fract_coord) { - if (_leave_directions[dir] == v->direction) { + if (DiagDirToDir(dir) == v->direction) { /* leave the depot? */ if ((v = v->next) != NULL) { v->vehstatus &= ~VS_HIDDEN; diff --git a/rail_gui.c b/rail_gui.c index 61d2375e9..d2fcb1920 100644 --- a/rail_gui.c +++ b/rail_gui.c @@ -23,7 +23,7 @@ static RailType _cur_railtype; static bool _remove_button_clicked; -static byte _build_depot_direction; +static DiagDirection _build_depot_direction; static byte _waypoint_count = 1; static byte _cur_waypoint_type; @@ -101,7 +101,7 @@ static const uint16 _place_depot_extra[12] = { void CcRailDepot(bool success, TileIndex tile, uint32 p1, uint32 p2) { if (success) { - int dir = p2; + DiagDirection dir = p2; SndPlayTileFx(SND_20_SPLAT_2, tile); ResetObjectToPlace(); @@ -901,7 +901,7 @@ static void ShowBuildWaypointPicker(void) void InitializeRailGui(void) { - _build_depot_direction = 3; + _build_depot_direction = DIAGDIR_NW; _railstation.numtracks = 1; _railstation.platlength = 1; _railstation.dragdrop = true; diff --git a/roadveh_cmd.c b/roadveh_cmd.c index 17decca9c..64be9610a 100644 --- a/roadveh_cmd.c +++ b/roadveh_cmd.c @@ -60,7 +60,7 @@ static const uint16 _road_pf_table_3[4] = { 0x910, 0x1600, 0x2005, 0x2A }; -int GetRoadVehImage(const Vehicle *v, byte direction) +int GetRoadVehImage(const Vehicle* v, Direction direction) { int img = v->spritenum; int image; @@ -718,7 +718,7 @@ typedef struct RoadVehFindData { int x; int y; const Vehicle* veh; - byte dir; + Direction dir; } RoadVehFindData; static void* EnumCheckRoadVehClose(Vehicle *v, void* data) @@ -744,7 +744,7 @@ static void* EnumCheckRoadVehClose(Vehicle *v, void* data) v : NULL; } -static Vehicle *RoadVehFindCloseTo(Vehicle *v, int x, int y, byte dir) +static Vehicle* RoadVehFindCloseTo(Vehicle* v, int x, int y, Direction dir) { RoadVehFindData rvf; Vehicle *u; @@ -833,12 +833,12 @@ static bool RoadVehAccelerate(Vehicle *v) return (t < v->progress); } -static byte RoadVehGetNewDirection(Vehicle *v, int x, int y) +static Direction RoadVehGetNewDirection(const Vehicle* v, int x, int y) { - static const byte _roadveh_new_dir[11] = { - 0, 7, 6, 0, - 1, 0, 5, 0, - 2, 3, 4 + static const Direction _roadveh_new_dir[] = { + DIR_N , DIR_NW, DIR_W , 0, + DIR_NE, DIR_N , DIR_SW, 0, + DIR_E , DIR_SE, DIR_S }; x = x - v->x_pos + 1; @@ -848,10 +848,11 @@ static byte RoadVehGetNewDirection(Vehicle *v, int x, int y) return _roadveh_new_dir[y * 4 + x]; } -static byte RoadVehGetSlidingDirection(Vehicle *v, int x, int y) +static Direction RoadVehGetSlidingDirection(const Vehicle* v, int x, int y) { - byte b = RoadVehGetNewDirection(v, x, y); - byte d = v->direction; + Direction b = RoadVehGetNewDirection(v, x, y); + Direction d = v->direction; + if (b == d) return d; d = (d + 1) & 7; if (b == d) return d; @@ -980,7 +981,7 @@ static bool EnumRoadTrackFindDist(TileIndex tile, void* data, int track, uint le // Returns direction to choose // or -1 if the direction is currently blocked -static int RoadFindPathToDest(Vehicle *v, TileIndex tile, int enterdir) +static int RoadFindPathToDest(Vehicle* v, TileIndex tile, DiagDirection enterdir) { #define return_track(x) {best_track = x; goto found_best_track; } @@ -1154,7 +1155,8 @@ static const byte _roadveh_data_2[4] = { 0,1,8,9 }; static void RoadVehController(Vehicle *v) { - byte new_dir, old_dir; + Direction new_dir; + Direction old_dir; RoadDriveEntry rd; int x,y; uint32 r; @@ -1273,7 +1275,7 @@ static void RoadVehController(Vehicle *v) TileIndex tile = v->tile + TileOffsByDir(rd.x & 3); int dir = RoadFindPathToDest(v, tile, rd.x & 3); uint32 r; - byte newdir; + Direction newdir; const RoadDriveEntry *rdp; if (dir == -1) { @@ -1339,7 +1341,7 @@ again: int dir = RoadFindPathToDest(v, v->tile, rd.x & 3); uint32 r; int tmp; - byte newdir; + Direction newdir; const RoadDriveEntry *rdp; if (dir == -1) { diff --git a/roadveh_gui.c b/roadveh_gui.c index 2d8a1826c..0d5344495 100644 --- a/roadveh_gui.c +++ b/roadveh_gui.c @@ -65,7 +65,7 @@ void DrawRoadVehPurchaseInfo(int x, int y, EngineID engine_number) static void DrawRoadVehImage(const Vehicle *v, int x, int y, VehicleID selection) { PalSpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v); - DrawSprite(GetRoadVehImage(v, 6) | pal, x + 14, y + 6); + DrawSprite(GetRoadVehImage(v, DIR_W) | pal, x + 14, y + 6); if (v->index == selection) { DrawFrameRect(x - 1, y - 1, x + 28, y + 12, 15, FR_BORDERONLY); @@ -636,7 +636,7 @@ static void RoadDepotClickVeh(Window *w, int x, int y) if (v != NULL) { WP(w,traindepot_d).sel = v->index; SetWindowDirty(w); - SetObjectToPlaceWnd(GetVehiclePalette(v) | GetRoadVehImage(v, 6), 4, w); + SetObjectToPlaceWnd(GetVehiclePalette(v) | GetRoadVehImage(v, DIR_W), 4, w); } break; diff --git a/ship_cmd.c b/ship_cmd.c index d12d36ae7..607dc152c 100644 --- a/ship_cmd.c +++ b/ship_cmd.c @@ -44,7 +44,7 @@ void DrawShipEngine(int x, int y, EngineID engine, uint32 image_ormod) DrawSprite((6 + _ship_sprites[spritenum]) | image_ormod, x, y); } -int GetShipImage(const Vehicle *v, byte direction) +int GetShipImage(const Vehicle* v, Direction direction) { int spritenum = v->spritenum; @@ -579,10 +579,10 @@ static int ChooseShipTrack(Vehicle *v, TileIndex tile, int enterdir, uint tracks } } -static const byte _new_vehicle_direction_table[11] = { - 0, 7, 6, 0, - 1, 0, 5, 0, - 2, 3, 4, +static const Direction _new_vehicle_direction_table[] = { + DIR_N , DIR_NW, DIR_W , 0, + DIR_NE, DIR_N , DIR_SW, 0, + DIR_E , DIR_SE, DIR_S }; static int ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile) @@ -646,7 +646,9 @@ static void ShipController(Vehicle *v) GetNewVehiclePosResult gp; uint32 r; const byte *b; - int dir,track,tracks; + Direction dir; + int track; + int tracks; v->tick_counter++; @@ -736,23 +738,24 @@ static void ShipController(Vehicle *v) } } } else { + DiagDirection diagdir; // new tile if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) goto reverse_direction; dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile); - assert(dir == 1 || dir == 3 || dir == 5 || dir == 7); - dir>>=1; - tracks = GetAvailShipTracks(gp.new_tile, dir); + assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW); + diagdir = DirToDiagDir(dir); + tracks = GetAvailShipTracks(gp.new_tile, diagdir); if (tracks == 0) goto reverse_direction; // Choose a direction, and continue if we find one - track = ChooseShipTrack(v, gp.new_tile, dir, tracks); + track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks); if (track < 0) goto reverse_direction; - b = _ship_subcoord[dir][track]; + b = _ship_subcoord[diagdir][track]; gp.x = (gp.x&~0xF) | b[0]; gp.y = (gp.y&~0xF) | b[1]; @@ -783,7 +786,7 @@ getout: return; reverse_direction: - dir = v->direction ^ 4; + dir = ReverseDir(v->direction); v->direction = dir; goto getout; } diff --git a/ship_gui.c b/ship_gui.c index b46dfa62d..d4b0c9dee 100644 --- a/ship_gui.c +++ b/ship_gui.c @@ -65,7 +65,7 @@ void DrawShipPurchaseInfo(int x, int y, EngineID engine_number) static void DrawShipImage(const Vehicle *v, int x, int y, VehicleID selection) { - DrawSprite(GetShipImage(v, 6) | GetVehiclePalette(v), x + 32, y + 10); + DrawSprite(GetShipImage(v, DIR_W) | GetVehiclePalette(v), x + 32, y + 10); if (v->index == selection) { DrawFrameRect(x - 5, y - 1, x + 67, y + 21, 15, FR_BORDERONLY); @@ -713,7 +713,7 @@ static void ShipDepotClick(Window *w, int x, int y) if (v != NULL) { WP(w,traindepot_d).sel = v->index; SetWindowDirty(w); - SetObjectToPlaceWnd(GetVehiclePalette(v) | GetShipImage(v, 6), 4, w); + SetObjectToPlaceWnd(GetVehiclePalette(v) | GetShipImage(v, DIR_W), 4, w); } break; diff --git a/station_cmd.c b/station_cmd.c index 07d7c5cc4..90601855a 100644 --- a/station_cmd.c +++ b/station_cmd.c @@ -817,7 +817,7 @@ int32 CheckFlatLandBelow(TileIndex tile, uint w, uint h, uint flags, uint invali return cost; } -static bool CanExpandRailroadStation(Station *st, uint *fin, int direction) +static bool CanExpandRailroadStation(Station* st, uint* fin, Axis axis) { uint curw = st->trainst_w, curh = st->trainst_h; TileIndex tile = fin[0]; @@ -832,8 +832,8 @@ static bool CanExpandRailroadStation(Station *st, uint *fin, int direction) curh = max(TileY(st->train_tile) + curh, TileY(tile) + h) - y; tile = TileXY(x, y); } else { - // check so the direction is the same - if ((_m[st->train_tile].m5 & 1) != direction) { + // check so the orientation is the same + if ((_m[st->train_tile].m5 & 1U) != axis) { _error_message = STR_306D_NONUNIFORM_STATIONS_DISALLOWED; return false; } @@ -934,7 +934,7 @@ int32 CmdBuildRailroadStation(int x, int y, uint32 flags, uint32 p1, uint32 p2) int32 cost, ret; StationID est; int plat_len, numtracks; - int direction; + Axis axis; uint finalvalues[3]; SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); @@ -946,16 +946,16 @@ int32 CmdBuildRailroadStation(int x, int y, uint32 flags, uint32 p1, uint32 p2) if (!ValParamRailtype(p2 & 0xF)) return CMD_ERROR; /* unpack parameters */ - direction = p1 & 1; + axis = p1 & 1; numtracks = GB(p1, 8, 8); plat_len = GB(p1, 16, 8); /* w = length, h = num_tracks */ - if (direction) { - h_org = plat_len; - w_org = numtracks; - } else { + if (axis == AXIS_X) { w_org = plat_len; h_org = numtracks; + } else { + h_org = plat_len; + w_org = numtracks; } if (h_org > _patches.station_spread || w_org > _patches.station_spread) return CMD_ERROR; @@ -969,7 +969,7 @@ int32 CmdBuildRailroadStation(int x, int y, uint32 flags, uint32 p1, uint32 p2) est = INVALID_STATION; // If DC_EXEC is in flag, do not want to pass it to CheckFlatLandBelow, because of a nice bug // for detail info, see: https://sourceforge.net/tracker/index.php?func=detail&aid=1029064&group_id=103924&atid=636365 - if (CmdFailed(ret = CheckFlatLandBelow(tile_org, w_org, h_org, flags&~DC_EXEC, 5 << direction, _patches.nonuniform_stations ? &est : NULL))) return CMD_ERROR; + if (CmdFailed(ret = CheckFlatLandBelow(tile_org, w_org, h_org, flags&~DC_EXEC, 5 << axis, _patches.nonuniform_stations ? &est : NULL))) return CMD_ERROR; cost = ret + (numtracks * _price.train_station_track + _price.train_station_length) * plat_len; // Make sure there are no similar stations around us. @@ -991,7 +991,7 @@ int32 CmdBuildRailroadStation(int x, int y, uint32 flags, uint32 p1, uint32 p2) // check if we want to expanding an already existing station? if (_is_old_ai_player || !_patches.join_stations) return_cmd_error(STR_3005_TOO_CLOSE_TO_ANOTHER_RAILROAD); - if (!CanExpandRailroadStation(st, finalvalues, direction)) + if (!CanExpandRailroadStation(st, finalvalues, axis)) return CMD_ERROR; } @@ -1021,7 +1021,7 @@ int32 CmdBuildRailroadStation(int x, int y, uint32 flags, uint32 p1, uint32 p2) // Now really clear the land below the station // It should never return CMD_ERROR.. but you never know ;) // (a bit strange function name for it, but it really does clear the land, when DC_EXEC is in flags) - if (CmdFailed(CheckFlatLandBelow(tile_org, w_org, h_org, flags, 5 << direction, _patches.nonuniform_stations ? &est : NULL))) return CMD_ERROR; + if (CmdFailed(CheckFlatLandBelow(tile_org, w_org, h_org, flags, 5 << axis, _patches.nonuniform_stations ? &est : NULL))) return CMD_ERROR; st->train_tile = finalvalues[0]; if (!st->facilities) st->xy = finalvalues[0]; @@ -1033,8 +1033,8 @@ int32 CmdBuildRailroadStation(int x, int y, uint32 flags, uint32 p1, uint32 p2) st->build_date = _date; - tile_delta = direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0); - track = direction ? TRACK_Y : TRACK_X; + tile_delta = (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); + track = (axis == AXIS_X ? TRACK_X : TRACK_Y); statspec = (p2 & 0x10) != 0 ? GetCustomStation(STAT_CLASS_DFLT, p2 >> 8) : NULL; layout_ptr = alloca(numtracks * plat_len); @@ -1051,7 +1051,7 @@ int32 CmdBuildRailroadStation(int x, int y, uint32 flags, uint32 p1, uint32 p2) station_index, /* map2 parameter */ p2 & 0xFF, /* map3lo parameter */ p2 >> 8, /* map3hi parameter */ - (*layout_ptr++) + direction /* map5 parameter */ + (*layout_ptr++) + axis /* map5 parameter */ ); tile += tile_delta; @@ -1172,27 +1172,27 @@ uint GetStationPlatforms(const Station *st, TileIndex tile) { TileIndex t; TileIndexDiff delta; - int dir; + Axis dir; uint len; assert(TileBelongsToRailStation(st, tile)); len = 0; dir = _m[tile].m5 & 1; - delta = dir ? TileDiffXY(0, 1) : TileDiffXY(1, 0); + delta = (dir == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); // find starting tile.. t = tile; do { t -= delta; len++; - } while (TileBelongsToRailStation(st, t) && (_m[t].m5 & 1) == dir); + } while (TileBelongsToRailStation(st, t) && (_m[t].m5 & 1U) == dir); // find ending tile t = tile; do { t += delta; len++; - } while (TileBelongsToRailStation(st, t) && (_m[t].m5 & 1) == dir); + } while (TileBelongsToRailStation(st, t) && (_m[t].m5 & 1U) == dir); return len - 1; } @@ -1806,17 +1806,17 @@ int32 CmdBuildDock(int x, int y, uint32 flags, uint32 p1, uint32 p2) { TileIndex tile = TileVirtXY(x, y); TileIndex tile_cur; - int direction; + DiagDirection direction; int32 cost; Station *st; SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); switch (GetTileSlope(tile, NULL)) { - case 3: direction = 0; break; - case 6: direction = 3; break; - case 9: direction = 1; break; - case 12: direction = 2; break; + case 3: direction = DIAGDIR_NE; break; + case 6: direction = DIAGDIR_NW; break; + case 9: direction = DIAGDIR_SE; break; + case 12: direction = DIAGDIR_SW; break; default: return_cmd_error(STR_304B_SITE_UNSUITABLE); } @@ -1898,7 +1898,8 @@ int32 CmdBuildDock(int x, int y, uint32 flags, uint32 p1, uint32 p2) MP_MAP2 | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR | MP_MAP5, st->index, - (direction&1) + 0x50); + DiagDirToAxis(direction) + 0x50 + ); UpdateStationVirtCoordDirty(st); UpdateStationAcceptance(st, false); diff --git a/train_cmd.c b/train_cmd.c index 4d0f434ae..49c186e8f 100644 --- a/train_cmd.c +++ b/train_cmd.c @@ -239,8 +239,8 @@ static int GetTrainAcceleration(Vehicle *v, bool mode) //first find the curve speed limit for (u = v; u->next != NULL; u = u->next, pos++) { - int dir = u->direction; - int ndir = u->next->direction; + Direction dir = u->direction; + Direction ndir = u->next->direction; int i; for (i = 0; i < 2; i++) { @@ -373,7 +373,7 @@ static void UpdateTrainAcceleration(Vehicle* v) v->acceleration = clamp(power / weight * 4, 1, 255); } -int GetTrainImage(const Vehicle *v, byte direction) +int GetTrainImage(const Vehicle* v, Direction direction) { int img = v->spritenum; int base; @@ -1444,8 +1444,8 @@ static void ReverseTrainSwapVeh(Vehicle *v, int l, int r) swap_byte(&a->direction, &b->direction); /* toggle direction */ - if (!(a->u.rail.track & 0x80)) a->direction ^= 4; - if (!(b->u.rail.track & 0x80)) b->direction ^= 4; + if (!(a->u.rail.track & 0x80)) a->direction = ReverseDir(a->direction); + if (!(b->u.rail.track & 0x80)) b->direction = ReverseDir(b->direction); /* swap more variables */ swap_int32(&a->x_pos, &b->x_pos); @@ -1462,7 +1462,7 @@ static void ReverseTrainSwapVeh(Vehicle *v, int l, int r) VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos); VehicleEnterTile(b, b->tile, b->x_pos, b->y_pos); } else { - if (!(a->u.rail.track & 0x80)) a->direction ^= 4; + if (!(a->u.rail.track & 0x80)) a->direction = ReverseDir(a->direction); UpdateVarsAfterSwap(a); VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos); @@ -2492,10 +2492,10 @@ static byte AfterSetTrainPos(Vehicle *v, bool new_tile) return old_z; } -static const byte _new_vehicle_direction_table[11] = { - 0, 7, 6, 0, - 1, 0, 5, 0, - 2, 3, 4, +static const Direction _new_vehicle_direction_table[11] = { + DIR_N , DIR_NW, DIR_W , 0, + DIR_NE, DIR_N , DIR_SW, 0, + DIR_E , DIR_SE, DIR_S }; static Direction GetNewVehicleDirectionByTile(TileIndex new_tile, TileIndex old_tile) @@ -2589,7 +2589,7 @@ static const RailtypeSlowdownParams _railtype_slowdown[3] = { }; /* Modify the speed of the vehicle due to a turn */ -static void AffectSpeedByDirChange(Vehicle *v, byte new_dir) +static void AffectSpeedByDirChange(Vehicle* v, Direction new_dir) { byte diff; const RailtypeSlowdownParams *rsp; @@ -2617,9 +2617,9 @@ static void AffectSpeedByZChange(Vehicle *v, byte old_z) } } -static const byte _otherside_signal_directions[14] = { - 1, 3, 1, 3, 5, 3, 0, 0, - 5, 7, 7, 5, 7, 1, +static const Direction _otherside_signal_directions[] = { + DIR_NE, DIR_SE, DIR_NE, DIR_SE, DIR_SW, DIR_SE, 0, 0, + DIR_SW, DIR_NW, DIR_NW, DIR_SW, DIR_NW, DIR_NE, }; static void TrainMovedChangeSignals(TileIndex tile, DiagDirection dir) @@ -2732,7 +2732,7 @@ static void CheckTrainCollision(Vehicle *v) typedef struct VehicleAtSignalData { TileIndex tile; - byte direction; + Direction direction; } VehicleAtSignalData; static void *CheckVehicleAtSignal(Vehicle *v, void *data) @@ -2757,7 +2757,7 @@ static void TrainController(Vehicle *v) DiagDirection enterdir; Direction dir; Direction newdir; - byte chosen_dir; + Direction chosen_dir; byte chosen_track; byte old_z; @@ -2938,7 +2938,7 @@ red_light: { TileIndex o_tile = gp.new_tile + TileOffsByDir(enterdir); VehicleAtSignalData vasd; vasd.tile = o_tile; - vasd.direction = dir ^ 4; + vasd.direction = ReverseDir(dir); /* check if a train is waiting on the other side */ if (VehicleFromPos(o_tile, &vasd, CheckVehicleAtSignal) == NULL) return; diff --git a/train_gui.c b/train_gui.c index e7f1ace5b..d8be51d8b 100644 --- a/train_gui.c +++ b/train_gui.c @@ -362,7 +362,7 @@ static void DrawTrainImage(const Vehicle *v, int x, int y, int count, int skip, if (dx + width <= count) { PalSpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v); - DrawSprite(GetTrainImage(v, 6) | pal, x + 14 + WagonLengthToPixels(dx), y + 6 + (is_custom_sprite(RailVehInfo(v->engine_type)->image_index) ? _traininfo_vehicle_pitch : 0)); + DrawSprite(GetTrainImage(v, DIR_W) | pal, x + 14 + WagonLengthToPixels(dx), y + 6 + (is_custom_sprite(RailVehInfo(v->engine_type)->image_index) ? _traininfo_vehicle_pitch : 0)); if (v->index == selection) DrawFrameRect(x - 1 + WagonLengthToPixels(dx), y - 1, x + WagonLengthToPixels(dx + width) - 1, y + 12, 15, FR_BORDERONLY); } @@ -577,7 +577,7 @@ static void TrainDepotClickTrain(Window *w, int x, int y) TrainDepotMoveVehicle(v, sel, gdvp.head); } else if (v != NULL) { WP(w,traindepot_d).sel = v->index; - SetObjectToPlaceWnd(GetVehiclePalette(v) | GetTrainImage(v, 6), 4, w); + SetObjectToPlaceWnd(GetVehiclePalette(v) | GetTrainImage(v, DIR_W), 4, w); SetWindowDirty(w); } break; diff --git a/tunnelbridge_cmd.c b/tunnelbridge_cmd.c index 42c54a621..b770379e7 100644 --- a/tunnelbridge_cmd.c +++ b/tunnelbridge_cmd.c @@ -119,7 +119,7 @@ static inline int GetBridgeType(TileIndex tile) * is_start_tile = false <-- end tile * is_start_tile = true <-- start tile */ -static uint32 CheckBridgeSlope(uint direction, uint tileh, bool is_start_tile) +static uint32 CheckBridgeSlope(Axis direction, uint tileh, bool is_start_tile) { if (IsSteepTileh(tileh)) return CMD_ERROR; @@ -129,7 +129,7 @@ static uint32 CheckBridgeSlope(uint direction, uint tileh, bool is_start_tile) - direction X: tiles 0, 12 - direction Y: tiles 0, 9 */ - if ((direction ? 0x201 : 0x1001) & (1 << tileh)) return 0; + if ((direction == AXIS_X ? 0x1001 : 0x201) & (1 << tileh)) return 0; // disallow certain start tiles to avoid certain crooked bridges if (tileh == 2) return CMD_ERROR; @@ -139,7 +139,7 @@ static uint32 CheckBridgeSlope(uint direction, uint tileh, bool is_start_tile) - direction X: tiles 0, 3 - direction Y: tiles 0, 6 */ - if ((direction? 0x41 : 0x9) & (1 << tileh)) return 0; + if ((direction == AXIS_X ? 0x9 : 0x41) & (1 << tileh)) return 0; // disallow certain end tiles to avoid certain crooked bridges if (tileh == 8) return CMD_ERROR; @@ -149,8 +149,8 @@ static uint32 CheckBridgeSlope(uint direction, uint tileh, bool is_start_tile) * start-tile: X 2,1 Y 2,4 (2 was disabled before) * end-tile: X 8,4 Y 8,1 (8 was disabled before) */ - if ((tileh == 1 && is_start_tile != (bool)direction) || - (tileh == 4 && is_start_tile == (bool)direction)) { + if ((tileh == 1 && is_start_tile != (direction != AXIS_X)) || + (tileh == 4 && is_start_tile == (direction != AXIS_X))) { return CMD_ERROR; } @@ -200,7 +200,7 @@ int32 CmdBuildBridge(int x, int y, uint32 flags, uint32 p1, uint32 p2) TileInfo ti_start, ti_end, ti; /* OPT: only 2 of those are ever used */ uint bridge_len; uint odd_middle_part; - uint direction; + Axis direction; uint i; int32 cost, terraformcost, ret; bool allow_on_slopes; @@ -225,17 +225,16 @@ int32 CmdBuildBridge(int x, int y, uint32 flags, uint32 p1, uint32 p2) sx = TileX(p1) * 16; sy = TileY(p1) * 16; - direction = 0; - /* check if valid, and make sure that (x,y) are smaller than (sx,sy) */ if (x == sx) { if (y == sy) return_cmd_error(STR_5008_CANNOT_START_AND_END_ON); - direction = 1; + direction = AXIS_Y; if (y > sy) { intswap(y,sy); intswap(x,sx); } } else if (y == sy) { + direction = AXIS_X; if (x > sx) { intswap(y,sy); intswap(x,sx); @@ -327,10 +326,10 @@ int32 CmdBuildBridge(int x, int y, uint32 flags, uint32 p1, uint32 p2) odd_middle_part = (bridge_len % 2) ? (bridge_len / 2) : bridge_len; for (i = 0; i != bridge_len; i++) { - if (direction != 0) { - y += 16; - } else { + if (direction == AXIS_X) { x += 16; + } else { + y += 16; } FindLandscapeHeight(&ti, x, y); @@ -349,13 +348,15 @@ int32 CmdBuildBridge(int x, int y, uint32 flags, uint32 p1, uint32 p2) break; case MP_RAILWAY: - if (ti.map5 != (direction == 0 ? 2 : 1)) goto not_valid_below; + if (ti.map5 != (direction == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X)) { + goto not_valid_below; + } m5 = 0xE0; break; case MP_STREET: if (GetRoadType(ti.tile) != ROAD_NORMAL || - GetRoadBits(ti.tile) != (direction == 0 ? ROAD_Y : ROAD_X)) { + GetRoadBits(ti.tile) != (direction == AXIS_X ? ROAD_Y : ROAD_X)) { goto not_valid_below; } m5 = 0xE8; @@ -408,7 +409,7 @@ not_valid_below:; } } - SetSignalsOnBothDir(ti_start.tile, (direction & 1) ? 1 : 0); + SetSignalsOnBothDir(ti_start.tile, direction == AXIS_X ? TRACK_X : TRACK_Y); /* for human player that builds the bridge he gets a selection to choose from bridges (DC_QUERY_COST) It's unnecessary to execute this command every time for every bridge. So it is done only @@ -546,7 +547,6 @@ static int32 DoClearTunnel(TileIndex tile, uint32 flags) Town *t; TileIndex endtile; uint length; - static const byte _updsignals_tunnel_dir[4] = { 5, 7, 1, 3}; SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); @@ -574,8 +574,7 @@ static int32 DoClearTunnel(TileIndex tile, uint32 flags) if (flags & DC_EXEC) { // We first need to request the direction before calling DoClearSquare // else the direction is always 0.. dah!! ;) - DiagDirection tile_dir = GetTunnelDirection(tile); - DiagDirection endtile_dir = GetTunnelDirection(endtile); + DiagDirection dir = GetTunnelDirection(tile); // Adjust the town's player rating. Do this before removing the tile owner info. if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) @@ -583,22 +582,22 @@ static int32 DoClearTunnel(TileIndex tile, uint32 flags) DoClearSquare(tile); DoClearSquare(endtile); - UpdateSignalsOnSegment(tile, _updsignals_tunnel_dir[tile_dir]); - UpdateSignalsOnSegment(endtile, _updsignals_tunnel_dir[endtile_dir]); + UpdateSignalsOnSegment(tile, DiagDirToDir(ReverseDiagDir(dir))); + UpdateSignalsOnSegment(endtile, DiagDirToDir(dir)); } return _price.clear_tunnel * (length + 1); } static TileIndex FindEdgesOfBridge(TileIndex tile, TileIndex *endtile) { - int direction = GB(_m[tile].m5, 0, 1); + Axis direction = GB(_m[tile].m5, 0, 1); TileIndex start; // find start of bridge for (;;) { if (IsTileType(tile, MP_TUNNELBRIDGE) && (_m[tile].m5 & 0xE0) == 0x80) break; - tile += direction ? TileDiffXY(0, -1) : TileDiffXY(-1, 0); + tile += (direction == AXIS_X ? TileDiffXY(-1, 0) : TileDiffXY(0, -1)); } start = tile; @@ -607,7 +606,7 @@ static TileIndex FindEdgesOfBridge(TileIndex tile, TileIndex *endtile) for (;;) { if (IsTileType(tile, MP_TUNNELBRIDGE) && (_m[tile].m5 & 0xE0) == 0xA0) break; - tile += direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0); + tile += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); } *endtile = tile; @@ -620,7 +619,7 @@ static int32 DoClearBridge(TileIndex tile, uint32 flags) TileIndex endtile; Vehicle *v; Town *t; - int direction; + Axis direction; SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); @@ -672,8 +671,8 @@ static int32 DoClearBridge(TileIndex tile, uint32 flags) Omit tile and endtile, since these are already checked, thus solving the problem of bridges over water, or higher bridges, where z is not increased, eg level bridge */ - tile += direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0); - endtile -= direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0); + tile += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); + endtile -= (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); /* Bridges on slopes might have their Z-value offset..correct this */ v = FindVehicleBetween(tile, endtile, TilePixelHeight(tile) + 8 + GetCorrectTileHeight(tile)); if (v != NULL) { @@ -682,9 +681,8 @@ static int32 DoClearBridge(TileIndex tile, uint32 flags) } /* Put the tiles back to start/end position */ - tile -= direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0); - endtile += direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0); - + tile -= (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); + endtile += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); t = ClosestTownFromTile(tile, (uint)-1); //needed for town rating penalty // check if you're allowed to remove the bridge owned by a town. @@ -726,17 +724,17 @@ static int32 DoClearBridge(TileIndex tile, uint32 flags) clear_it:; DoClearSquare(c); } - c += direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0); + c += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); } while (c <= endtile); - SetSignalsOnBothDir(tile, direction); - SetSignalsOnBothDir(endtile, direction); + SetSignalsOnBothDir(tile, direction == AXIS_X ? TRACK_X : TRACK_Y); + SetSignalsOnBothDir(endtile, direction == AXIS_X ? TRACK_X : TRACK_Y); } - if (direction) { - return (TileY(endtile) - TileY(tile) + 1) * _price.clear_bridge; - } else { + if (direction == AXIS_X) { return (TileX(endtile) - TileX(tile) + 1) * _price.clear_bridge; + } else { + return (TileY(endtile) - TileY(tile) + 1) * _price.clear_bridge; } } @@ -918,7 +916,7 @@ static void DrawBridgePillars(const TileInfo *ti, int x, int y, int z) } } -uint GetBridgeFoundation(uint tileh, byte direction) +uint GetBridgeFoundation(uint tileh, Axis axis) { int i; // normal level sloped building (7, 11, 13, 14) @@ -931,8 +929,8 @@ uint GetBridgeFoundation(uint tileh, byte direction) (i += 2, tileh == 4) || (i += 2, tileh == 8) ) && ( - direction == 0 || - (i++, direction == 1) + axis == AXIS_X || + (i++, axis == AXIS_Y) )) { return i + 15; } diff --git a/vehicle.c b/vehicle.c index a368c5d4b..e4dd53774 100644 --- a/vehicle.c +++ b/vehicle.c @@ -1921,15 +1921,16 @@ bool GetNewVehiclePos(const Vehicle *v, GetNewVehiclePosResult *gp) return gp->old_tile == gp->new_tile; } -static const byte _new_direction_table[9] = { - 0, 7, 6, - 1, 3, 5, - 2, 3, 4, +static const Direction _new_direction_table[] = { + DIR_N , DIR_NW, DIR_W , + DIR_NE, DIR_SE, DIR_SW, + DIR_E , DIR_SE, DIR_S }; -byte GetDirectionTowards(const Vehicle *v, int x, int y) +Direction GetDirectionTowards(const Vehicle* v, int x, int y) { - byte dirdiff, dir; + Direction dir; + byte dirdiff; int i = 0; if (y >= v->y_pos) { diff --git a/vehicle.h b/vehicle.h index 0c13e98d8..4713cdca4 100644 --- a/vehicle.h +++ b/vehicle.h @@ -269,10 +269,10 @@ void ViewportAddVehicles(DrawPixelInfo *dpi); void TrainEnterDepot(Vehicle *v, TileIndex tile); /* train_cmd.h */ -int GetTrainImage(const Vehicle *v, byte direction); -int GetAircraftImage(const Vehicle *v, byte direction); -int GetRoadVehImage(const Vehicle *v, byte direction); -int GetShipImage(const Vehicle *v, byte direction); +int GetTrainImage(const Vehicle* v, Direction direction); +int GetAircraftImage(const Vehicle* v, Direction direction); +int GetRoadVehImage(const Vehicle* v, Direction direction); +int GetShipImage(const Vehicle* v, Direction direction); Vehicle *CreateEffectVehicle(int x, int y, int z, EffectVehicle type); Vehicle *CreateEffectVehicleAbove(int x, int y, int z, EffectVehicle type); @@ -332,7 +332,7 @@ Trackdir GetVehicleTrackdir(const Vehicle* v); /* returns true if staying in the same tile */ bool GetNewVehiclePos(const Vehicle *v, GetNewVehiclePosResult *gp); -byte GetDirectionTowards(const Vehicle *v, int x, int y); +Direction GetDirectionTowards(const Vehicle* v, int x, int y); #define BEGIN_ENUM_WAGONS(v) do { #define END_ENUM_WAGONS(v) } while ( (v=v->next) != NULL); diff --git a/water_cmd.c b/water_cmd.c index 2df124595..c4745b763 100644 --- a/water_cmd.c +++ b/water_cmd.c @@ -127,7 +127,7 @@ static int32 RemoveShipDepot(TileIndex tile, uint32 flags) } // build a shiplift -static int32 DoBuildShiplift(TileIndex tile, int dir, uint32 flags) +static int32 DoBuildShiplift(TileIndex tile, DiagDirection dir, uint32 flags) { int32 ret; int delta; @@ -189,17 +189,18 @@ static void MarkTilesAroundDirty(TileIndex tile) int32 CmdBuildLock(int x, int y, uint32 flags, uint32 p1, uint32 p2) { TileIndex tile = TileVirtXY(x, y); - uint tileh; + DiagDirection dir; SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); - tileh = GetTileSlope(tile, NULL); - if (tileh == 3 || tileh == 6 || tileh == 9 || tileh == 12) { - static const byte _shiplift_dirs[16] = {0, 0, 0, 2, 0, 0, 1, 0, 0, 3, 0, 0, 0}; - return DoBuildShiplift(tile, _shiplift_dirs[tileh], flags); + switch (GetTileSlope(tile, NULL)) { + case 3: dir = DIAGDIR_SW; break; + case 6: dir = DIAGDIR_SE; break; + case 9: dir = DIAGDIR_NW; break; + case 12: dir = DIAGDIR_NE; break; + default: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); } - - return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); + return DoBuildShiplift(tile, dir, flags); } /** Build a piece of canal. diff --git a/waypoint.c b/waypoint.c index a999ef2c3..8ff180875 100644 --- a/waypoint.c +++ b/waypoint.c @@ -292,7 +292,7 @@ int32 RemoveTrainWaypoint(TileIndex tile, uint32 flags, bool justremove) return CMD_ERROR; if (flags & DC_EXEC) { - int direction = _m[tile].m5 & RAIL_WAYPOINT_TRACK_MASK; + Axis direction = _m[tile].m5 & RAIL_WAYPOINT_TRACK_MASK; wp = GetWaypointByTile(tile); @@ -304,7 +304,7 @@ int32 RemoveTrainWaypoint(TileIndex tile, uint32 flags, bool justremove) MarkTileDirtyByTile(tile); } else { DoClearSquare(tile); - SetSignalsOnBothDir(tile, direction); + SetSignalsOnBothDir(tile, direction == AXIS_X ? TRACK_X : TRACK_Y); } } -- cgit v1.2.3-70-g09d2