Add a vehicle type mask front only iteration filter

This commit is contained in:
Jonathan G Rennison 2024-09-05 18:19:46 +01:00
parent 4842010b7d
commit bf7750321a
2 changed files with 29 additions and 0 deletions

View File

@ -1344,6 +1344,21 @@ public:
}
};
struct VehicleFrontOnlyTypeMaskFilter {
VehicleTypeMask vt_mask;
bool operator() (size_t index)
{
#if OTTD_UPPER_TAGGED_PTR
uintptr_t vptr = _vehicle_pool.GetRaw(index);
return !VehiclePoolOps::IsNonFrontVehiclePtr(vptr) && HasBit(this->vt_mask, VehiclePoolOps::GetVehicleType(vptr));
#else
const Vehicle *v = Vehicle::Get(index);
return HasBit(this->vt_mask, v->type) && v->Previous() == nullptr;
#endif
}
};
/**
* Returns an iterable ensemble of all valid vehicles of the given type
* @param vt the VehicleType to filter
@ -1375,6 +1390,17 @@ public:
{
return Pool::IterateWrapperFiltered<Vehicle, VehicleFrontOnlyTypeFilter>(from, VehicleFrontOnlyTypeFilter{ vt });
}
/**
* Returns an iterable ensemble of all valid front vehicles of the given type
* @param VehicleTypeMask the set of VehicleType to filter, as a VehicleTypeMask
* @param from index of the first vehicle to consider
* @return an iterable ensemble of all valid front vehicles of the given type
*/
static Pool::IterateWrapperFiltered<Vehicle, VehicleFrontOnlyTypeMaskFilter> IterateTypeMaskFrontOnly(VehicleTypeMask vt_mask, size_t from = 0)
{
return Pool::IterateWrapperFiltered<Vehicle, VehicleFrontOnlyTypeMaskFilter>(from, VehicleFrontOnlyTypeMaskFilter{ vt_mask });
}
};
inline bool IsPointInViewportVehicleRedrawArea(const std::vector<Rect> &viewport_redraw_rects, const Point &pt)

View File

@ -39,6 +39,9 @@ DECLARE_POSTFIX_INCREMENT(VehicleType)
template <> struct EnumPropsT<VehicleType> : MakeEnumPropsT<VehicleType, uint8_t, VEH_TRAIN, VEH_END, VEH_INVALID, 3> {};
DECLARE_ENUM_AS_ADDABLE(VehicleType)
using VehicleTypeMask = uint8_t;
static_assert(VEH_END <= 8);
struct Vehicle;
struct Train;
struct RoadVehicle;