diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index db49b5fb5f..56fb1e650c 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -997,7 +997,7 @@ static bool AircraftController(Aircraft *v) DirDiff dirdiff = DirDifference(amd.direction, v->direction); /* if distance is 0, and plane points in right direction, no point in calling * UpdateAircraftSpeed(). So do it only afterwards */ - if (dirdiff == DIRDIFF_SAME) { + if (dirdiff == DirDiff::Same) { v->cur_speed = 0; return true; } @@ -1207,7 +1207,7 @@ static bool HandleCrashedAircraft(Aircraft *v) uint32_t r; if (Chance16R(1, 32, r)) { static const DirDiff delta[] = { - DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT + DirDiff::Left45, DirDiff::Same, DirDiff::Same, DirDiff::Right45 }; v->direction = ChangeDir(v->direction, delta[GB(r, 16, 2)]); diff --git a/src/airport.cpp b/src/airport.cpp index 83e5543e81..7c73c4b66a 100644 --- a/src/airport.cpp +++ b/src/airport.cpp @@ -82,7 +82,7 @@ AirportMovingData RotateAirportMovingData(const AirportMovingData *orig, Directi { AirportMovingData amd; amd.flags = orig->flags; - amd.direction = ChangeDir(orig->direction, (DirDiff)rotation); + amd.direction = ChangeDir(orig->direction, static_cast(rotation)); switch (rotation) { case DIR_N: amd.x = orig->x; diff --git a/src/direction_func.h b/src/direction_func.h index b1f2ffc5ac..477bda7276 100644 --- a/src/direction_func.h +++ b/src/direction_func.h @@ -71,15 +71,15 @@ inline DirDiff DirDifference(Direction d0, Direction d1) assert(IsValidDirection(d1)); /* Cast to uint so compiler can use bitmask. If the difference is negative * and we used int instead of uint, further "+ 8" would have to be added. */ - return static_cast((static_cast(d0) - static_cast(d1)) % 8); + return static_cast((to_underlying(d0) - to_underlying(d1)) % 8); } /** * Applies two differences together * * This function adds two differences together and returns the resulting - * difference. So adding two DIRDIFF_REVERSE together results in the - * DIRDIFF_SAME difference. + * difference. So adding two DirDiff::Reverse together results in the + * DirDiff::Same difference. * * @param d The first difference * @param delta The second difference to add on @@ -88,7 +88,7 @@ inline DirDiff DirDifference(Direction d0, Direction d1) inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta) { /* Cast to uint so compiler can use bitmask. Result can never be negative. */ - return static_cast((static_cast(d) + static_cast(delta)) % 8); + return static_cast((to_underlying(d) + to_underlying(delta)) % 8); } /** @@ -98,7 +98,7 @@ inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta) */ inline DirDiff LimitDirDiff(DirDiff d) { - return d > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT; + return d > DirDiff::Reverse ? DirDiff::Left45 : DirDiff::Right45; } /** @@ -115,7 +115,7 @@ inline Direction ChangeDir(Direction d, DirDiff delta) { assert(IsValidDirection(d)); /* Cast to uint so compiler can use bitmask. Result can never be negative. */ - return static_cast((static_cast(d) + static_cast(delta)) % 8); + return static_cast((to_underlying(d) + to_underlying(delta)) % 8); } diff --git a/src/direction_type.h b/src/direction_type.h index fb075efab5..57cbeb3621 100644 --- a/src/direction_type.h +++ b/src/direction_type.h @@ -60,13 +60,13 @@ static constexpr Directions DIRECTIONS_ALL{DIR_N, DIR_NE, DIR_E, DIR_SE, DIR_S, * modulo DIR_END or use the #ChangeDirDiff(DirDiff, DirDiff) function. * @see ChangeDirDiff(DirDiff, DirDiff) */ -enum DirDiff : uint8_t { - DIRDIFF_SAME = 0, ///< Both directions faces to the same direction - DIRDIFF_45RIGHT = 1, ///< Angle of 45 degrees right - DIRDIFF_90RIGHT = 2, ///< Angle of 90 degrees right - DIRDIFF_REVERSE = 4, ///< One direction is the opposite of the other one - DIRDIFF_90LEFT = 6, ///< Angle of 90 degrees left - DIRDIFF_45LEFT = 7, ///< Angle of 45 degrees left +enum class DirDiff : uint8_t { + Same = 0, ///< Both directions faces to the same direction + Right45 = 1, ///< Angle of 45 degrees right + Right90 = 2, ///< Angle of 90 degrees right + Reverse = 4, ///< One direction is the opposite of the other one + Left90 = 6, ///< Angle of 90 degrees left + Left45 = 7, ///< Angle of 45 degrees left }; diff --git a/src/disaster_vehicle.cpp b/src/disaster_vehicle.cpp index e9c6b483a2..17889058b9 100644 --- a/src/disaster_vehicle.cpp +++ b/src/disaster_vehicle.cpp @@ -696,7 +696,7 @@ static bool DisasterTick_Submarine(DisasterVehicle *v) } } - v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DIRDIFF_90RIGHT : DIRDIFF_90LEFT); + v->direction = ChangeDir(v->direction, GB(Random(), 0, 1) ? DirDiff::Right90 : DirDiff::Left90); return true; } diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 1583d12262..ff2e65550d 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -531,13 +531,13 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec const Vehicle *u_p = v->Previous(); const Vehicle *u_n = v->Next(); - DirDiff f = (u_p == nullptr) ? DIRDIFF_SAME : DirDifference(u_p->direction, v->direction); - DirDiff b = (u_n == nullptr) ? DIRDIFF_SAME : DirDifference(v->direction, u_n->direction); + DirDiff f = (u_p == nullptr) ? DirDiff::Same : DirDifference(u_p->direction, v->direction); + DirDiff b = (u_n == nullptr) ? DirDiff::Same : DirDifference(v->direction, u_n->direction); DirDiff t = ChangeDirDiff(f, b); - return ((t > DIRDIFF_REVERSE ? t | 8 : t) << 16) | - ((b > DIRDIFF_REVERSE ? b | 8 : b) << 8) | - ( f > DIRDIFF_REVERSE ? f | 8 : f); + return ((t > DirDiff::Reverse ? to_underlying(t) | 8 : to_underlying(t)) << 16) | + ((b > DirDiff::Reverse ? to_underlying(b) | 8 : to_underlying(b)) << 8) | + ( f > DirDiff::Reverse ? to_underlying(f) | 8 : to_underlying(f)); } case 0x46: // Motion counter @@ -648,13 +648,14 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec */ if (!v->IsGroundVehicle()) return 0; - const Vehicle *u = v->Move((int8_t)parameter); + const Vehicle *u = v->Move(static_cast(parameter)); if (u == nullptr) return 0; /* Get direction difference. */ - bool prev = (int8_t)parameter < 0; - uint32_t ret = prev ? DirDifference(u->direction, v->direction) : DirDifference(v->direction, u->direction); - if (ret > DIRDIFF_REVERSE) ret |= 0x08; + bool prev = static_cast(parameter) < 0; + DirDiff dirdiff = prev ? DirDifference(u->direction, v->direction) : DirDifference(v->direction, u->direction); + uint32_t ret = to_underlying(dirdiff); + if (dirdiff > DirDiff::Reverse) ret |= 0x08; if (u->vehstatus.Test(VehState::Hidden)) ret |= 0x80; diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 630d36404d..0f15950d00 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -507,7 +507,7 @@ static void DeleteLastRoadVeh(RoadVehicle *v) static void RoadVehSetRandomDirection(RoadVehicle *v) { static const DirDiff delta[] = { - DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT + DirDiff::Left45, DirDiff::Same, DirDiff::Same, DirDiff::Right45 }; do { diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 7ea9b7878c..ed9e00178c 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -796,9 +796,9 @@ static void ShipController(Ship *v) const Direction new_direction = b.dir; const DirDiff diff = DirDifference(new_direction, v->direction); switch (diff) { - case DIRDIFF_SAME: - case DIRDIFF_45RIGHT: - case DIRDIFF_45LEFT: + case DirDiff::Same: + case DirDiff::Right45: + case DirDiff::Left45: /* Continue at speed */ v->rotation = v->direction = new_direction; break; diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 8dc5efaebf..d7ccd426ee 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -328,11 +328,11 @@ uint16_t Train::GetCurveSpeedLimit() const Direction next_dir = u->Next()->direction; DirDiff dirdiff = DirDifference(this_dir, next_dir); - if (dirdiff == DIRDIFF_SAME) continue; + if (dirdiff == DirDiff::Same) continue; - if (dirdiff == DIRDIFF_45LEFT) curvecount[0]++; - if (dirdiff == DIRDIFF_45RIGHT) curvecount[1]++; - if (dirdiff == DIRDIFF_45LEFT || dirdiff == DIRDIFF_45RIGHT) { + if (dirdiff == DirDiff::Left45) curvecount[0]++; + if (dirdiff == DirDiff::Right45) curvecount[1]++; + if (dirdiff == DirDiff::Left45 || dirdiff == DirDiff::Right45) { if (lastpos != -1) { numcurve++; sum += pos - lastpos; @@ -344,7 +344,7 @@ uint16_t Train::GetCurveSpeedLimit() const } /* if we have a 90 degree turn, fix the speed limit to 60 */ - if (dirdiff == DIRDIFF_90LEFT || dirdiff == DIRDIFF_90RIGHT) { + if (dirdiff == DirDiff::Left90 || dirdiff == DirDiff::Right90) { max_speed = 61; } } @@ -3574,7 +3574,7 @@ bool TrainController(Train *v, Vehicle *nomove, bool reverse) if (prev == nullptr && _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) { const AccelerationSlowdownParams *asp = &_accel_slowdown[static_cast(v->GetAccelerationType())]; DirDiff diff = DirDifference(v->direction, chosen_dir); - v->cur_speed -= (diff == DIRDIFF_45RIGHT || diff == DIRDIFF_45LEFT ? asp->small_turn : asp->large_turn) * v->cur_speed >> 8; + v->cur_speed -= (diff == DirDiff::Right45 || diff == DirDiff::Left45 ? asp->small_turn : asp->large_turn) * v->cur_speed >> 8; } direction_changed = true; v->SetMovingDirection(chosen_dir); @@ -3782,7 +3782,7 @@ static void DeleteLastWagon(Train *v) static void ChangeTrainDirRandomly(Train *v) { static const DirDiff delta[] = { - DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT + DirDiff::Left45, DirDiff::Same, DirDiff::Same, DirDiff::Right45 }; do { diff --git a/src/vehicle.cpp b/src/vehicle.cpp index f98b8a6af1..70d93a0964 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1840,7 +1840,7 @@ Direction GetDirectionTowards(const Vehicle *v, int x, int y) Direction dir = v->GetMovingDirection(); DirDiff dirdiff = DirDifference(_new_direction_table[i], dir); - if (dirdiff == DIRDIFF_SAME) return dir; + if (dirdiff == DirDiff::Same) return dir; return ChangeDir(dir, LimitDirDiff(dirdiff)); } @@ -2763,7 +2763,7 @@ static void SpawnAdvancedVisualEffect(const Vehicle *v) Direction l_dir = v->direction; if (v->type == VehicleType::Train && Train::From(v)->flags.Test(VehicleRailFlag::Flipped)) l_dir = ReverseDir(l_dir); - Direction t_dir = ChangeDir(l_dir, DIRDIFF_90RIGHT); + Direction t_dir = ChangeDir(l_dir, DirDiff::Right90); int8_t x_center = _vehicle_smoke_pos[l_dir] * l_center; int8_t y_center = _vehicle_smoke_pos[t_dir] * l_center; @@ -2947,7 +2947,7 @@ void Vehicle::ShowVisualEffect() const if (v->type == VehicleType::Train) effect_offset += (VEHICLE_LENGTH - Train::From(v)->gcache.cached_veh_length) / 2; int x = _vehicle_smoke_pos[v->direction] * effect_offset; - int y = _vehicle_smoke_pos[ChangeDir(v->direction, DIRDIFF_90RIGHT)] * effect_offset; + int y = _vehicle_smoke_pos[ChangeDir(v->direction, DirDiff::Right90)] * effect_offset; if (v->type == VehicleType::Train && Train::From(v)->flags.Test(VehicleRailFlag::Flipped)) { x = -x;