mirror of
https://github.com/OpenTTD/OpenTTD
synced 2025-12-10 06:52:05 +01:00
Codechange: Iterate road/tram masks instead of checking each type. (#14716)
This commit is contained in:
@@ -45,8 +45,10 @@ struct CompanyInfrastructure {
|
||||
return std::accumulate(std::begin(this->rail), std::end(this->rail), 0U);
|
||||
}
|
||||
|
||||
uint32_t GetRoadTotal() const;
|
||||
uint32_t GetTramTotal() const;
|
||||
uint32_t GetRoadTramTotal(RoadTramType rtt) const;
|
||||
|
||||
inline uint32_t GetRoadTotal() const { return GetRoadTramTotal(RTT_ROAD); }
|
||||
inline uint32_t GetTramTotal() const { return GetRoadTramTotal(RTT_TRAM); }
|
||||
};
|
||||
|
||||
class FreeUnitIDGenerator {
|
||||
|
||||
@@ -1291,26 +1291,14 @@ int CompanyServiceInterval(const Company *c, VehicleType type)
|
||||
|
||||
/**
|
||||
* Get total sum of all owned road bits.
|
||||
* @param rtt RoadTramType to get total for.
|
||||
* @return Combined total road road bits.
|
||||
*/
|
||||
uint32_t CompanyInfrastructure::GetRoadTotal() const
|
||||
uint32_t CompanyInfrastructure::GetRoadTramTotal(RoadTramType rtt) const
|
||||
{
|
||||
uint32_t total = 0;
|
||||
for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
|
||||
if (RoadTypeIsRoad(rt)) total += this->road[rt];
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total sum of all owned tram bits.
|
||||
* @return Combined total of tram road bits.
|
||||
*/
|
||||
uint32_t CompanyInfrastructure::GetTramTotal() const
|
||||
{
|
||||
uint32_t total = 0;
|
||||
for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
|
||||
if (RoadTypeIsTram(rt)) total += this->road[rt];
|
||||
for (RoadType rt : GetMaskForRoadTramType(rtt)) {
|
||||
total += this->road[rt];
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
14
src/road.h
14
src/road.h
@@ -21,20 +21,6 @@
|
||||
#include "newgrf_badge_type.h"
|
||||
#include "economy_func.h"
|
||||
|
||||
|
||||
enum RoadTramType : bool {
|
||||
RTT_ROAD,
|
||||
RTT_TRAM,
|
||||
};
|
||||
|
||||
enum RoadTramTypes : uint8_t {
|
||||
RTTB_ROAD = 1 << RTT_ROAD,
|
||||
RTTB_TRAM = 1 << RTT_TRAM,
|
||||
};
|
||||
DECLARE_ENUM_AS_BIT_SET(RoadTramTypes)
|
||||
|
||||
static const RoadTramType _roadtramtypes[] = { RTT_ROAD, RTT_TRAM };
|
||||
|
||||
/** Roadtype flag bit numbers. */
|
||||
enum class RoadTypeFlag : uint8_t {
|
||||
Catenary = 0, ///< Bit number for adding catenary
|
||||
|
||||
@@ -31,6 +31,22 @@ DECLARE_INCREMENT_DECREMENT_OPERATORS(RoadType)
|
||||
|
||||
using RoadTypes = EnumBitSet<RoadType, uint64_t>;
|
||||
|
||||
/**
|
||||
* The different types of road type.
|
||||
*/
|
||||
enum RoadTramType : bool {
|
||||
RTT_ROAD, ///< Road road type.
|
||||
RTT_TRAM, ///< Tram road type.
|
||||
};
|
||||
|
||||
enum RoadTramTypes : uint8_t {
|
||||
RTTB_ROAD = 1 << RTT_ROAD, ///< Road road type bit.
|
||||
RTTB_TRAM = 1 << RTT_TRAM, ///< Tram road type bit.
|
||||
};
|
||||
DECLARE_ENUM_AS_BIT_SET(RoadTramTypes)
|
||||
|
||||
static const RoadTramType _roadtramtypes[] = { RTT_ROAD, RTT_TRAM };
|
||||
|
||||
/**
|
||||
* Enumeration for the road parts on a tile.
|
||||
*
|
||||
|
||||
@@ -965,14 +965,9 @@ RoadType GetTownRoadType()
|
||||
const RoadTypeInfo *best = nullptr;
|
||||
const uint16_t assume_max_speed = 50;
|
||||
|
||||
for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
|
||||
if (RoadTypeIsTram(rt)) continue;
|
||||
|
||||
for (RoadType rt : GetMaskForRoadTramType(RTT_ROAD)) {
|
||||
const RoadTypeInfo *rti = GetRoadTypeInfo(rt);
|
||||
|
||||
/* Unused road type. */
|
||||
if (rti->label == 0) continue;
|
||||
|
||||
/* Can town build this road. */
|
||||
if (!rti->flags.Test(RoadTypeFlag::TownBuild)) continue;
|
||||
|
||||
@@ -997,10 +992,9 @@ RoadType GetTownRoadType()
|
||||
static TimerGameCalendar::Date GetTownRoadTypeFirstIntroductionDate()
|
||||
{
|
||||
const RoadTypeInfo *best = nullptr;
|
||||
for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
|
||||
if (RoadTypeIsTram(rt)) continue;
|
||||
for (RoadType rt : GetMaskForRoadTramType(RTT_ROAD)) {
|
||||
const RoadTypeInfo *rti = GetRoadTypeInfo(rt);
|
||||
if (rti->label == 0) continue; // Unused road type.
|
||||
|
||||
if (!rti->flags.Test(RoadTypeFlag::TownBuild)) continue; // Town can't build this road type.
|
||||
|
||||
if (best != nullptr && rti->introduction_date >= best->introduction_date) continue;
|
||||
|
||||
Reference in New Issue
Block a user