Cargo type orders: Add support for 64 cargo types

pull/155/head
Jonathan G Rennison 6 years ago
parent 0ae32838ea
commit 00b1fe6246

@ -72,7 +72,7 @@ enum CargoType {
typedef uint64 CargoTypes;
static const CargoTypes ALL_CARGOTYPES = (CargoTypes)UINT32_MAX;
static const CargoTypes ALL_CARGOTYPES = (CargoTypes)UINT64_MAX;
/** Class for storing amounts of cargo */
struct CargoArray {

@ -26,16 +26,16 @@
* @param is_full_loading If the vehicle is full loading.
* @param cargo_mask Mask of cargoes to refresh
*/
/* static */ void LinkRefresher::Run(Vehicle *v, bool allow_merge, bool is_full_loading, uint32 cargo_mask)
/* static */ void LinkRefresher::Run(Vehicle *v, bool allow_merge, bool is_full_loading, CargoTypes cargo_mask)
{
/* If there are no orders we can't predict anything.*/
if (v->orders.list == NULL) return;
uint32 have_cargo_mask = v->GetLastLoadingStationValidCargoMask();
CargoTypes have_cargo_mask = v->GetLastLoadingStationValidCargoMask();
/* Scan orders for cargo-specific load/unload, and run LinkRefresher separately for each set of cargoes where they differ. */
while (cargo_mask != 0) {
uint32 iter_cargo_mask = cargo_mask;
CargoTypes iter_cargo_mask = cargo_mask;
for (const Order *o = v->orders.list->GetFirstOrder(); o != NULL; o = o->next) {
if (o->IsType(OT_GOTO_STATION) || o->IsType(OT_IMPLICIT)) {
if (o->GetUnloadType() == OUFB_CARGO_TYPE_UNLOAD) {
@ -92,7 +92,7 @@ bool LinkRefresher::Hop::operator<(const Hop &other) const
* @param allow_merge If the refresher is allowed to merge or extend link graphs.
* @param is_full_loading If the vehicle is full loading.
*/
LinkRefresher::LinkRefresher(Vehicle *vehicle, HopSet *seen_hops, bool allow_merge, bool is_full_loading, uint32 cargo_mask) :
LinkRefresher::LinkRefresher(Vehicle *vehicle, HopSet *seen_hops, bool allow_merge, bool is_full_loading, CargoTypes cargo_mask) :
vehicle(vehicle), seen_hops(seen_hops), cargo(CT_INVALID), allow_merge(allow_merge),
is_full_loading(is_full_loading), cargo_mask(cargo_mask)
{
@ -199,7 +199,7 @@ const Order *LinkRefresher::PredictNextOrder(const Order *cur, const Order *next
SetBit(flags, USE_NEXT);
if (next->IsType(OT_CONDITIONAL)) {
uint32 this_cargo_mask = this->cargo_mask;
CargoTypes this_cargo_mask = this->cargo_mask;
const Order *skip_to = this->vehicle->orders.list->GetNextDecisionNode(
this->vehicle->orders.list->GetOrderAt(next->GetConditionSkipToOrder()), num_hops, this_cargo_mask);
assert(this_cargo_mask == this->cargo_mask);
@ -215,7 +215,7 @@ const Order *LinkRefresher::PredictNextOrder(const Order *cur, const Order *next
/* Reassign next with the following stop. This can be a station or a
* depot.*/
uint32 this_cargo_mask = this->cargo_mask;
CargoTypes this_cargo_mask = this->cargo_mask;
next = this->vehicle->orders.list->GetNextDecisionNode(
this->vehicle->orders.list->GetNext(next), num_hops++, this_cargo_mask);
assert(this_cargo_mask == this->cargo_mask);

@ -23,7 +23,7 @@
*/
class LinkRefresher {
public:
static void Run(Vehicle *v, bool allow_merge = true, bool is_full_loading = false, uint32 cargo_mask = ~0);
static void Run(Vehicle *v, bool allow_merge = true, bool is_full_loading = false, CargoTypes cargo_mask = ALL_CARGOTYPES);
protected:
/**
@ -89,9 +89,9 @@ protected:
CargoID cargo; ///< Cargo given in last refit order.
bool allow_merge; ///< If the refresher is allowed to merge or extend link graphs.
bool is_full_loading; ///< If the vehicle is full loading.
uint32 cargo_mask; ///< Bit-mask of cargo IDs to refresh.
CargoTypes cargo_mask; ///< Bit-mask of cargo IDs to refresh.
LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading, uint32 cargo_mask);
LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading, CargoTypes cargo_mask);
bool HandleRefit(CargoID refit_cargo);
void ResetRefit();

@ -229,11 +229,11 @@ public:
return ouf;
}
template <typename F> uint32 FilterLoadUnloadTypeCargoMask(F filter_func, uint32 cargo_mask = ~0)
template <typename F> CargoTypes FilterLoadUnloadTypeCargoMask(F filter_func, CargoTypes cargo_mask = ALL_CARGOTYPES)
{
if ((this->GetLoadType() == OLFB_CARGO_TYPE_LOAD) || (this->GetUnloadType() == OUFB_CARGO_TYPE_UNLOAD)) {
CargoID cargo;
uint32 output_mask = cargo_mask;
CargoTypes output_mask = cargo_mask;
FOR_EACH_SET_BIT(cargo, cargo_mask) {
if (!filter_func(this, cargo)) ClrBit(output_mask, cargo);
}
@ -395,10 +395,10 @@ void InsertOrder(Vehicle *v, Order *new_o, VehicleOrderID sel_ord);
void DeleteOrder(Vehicle *v, VehicleOrderID sel_ord);
struct CargoMaskedStationIDStack {
uint32 cargo_mask;
CargoTypes cargo_mask;
StationIDStack station;
CargoMaskedStationIDStack(uint32 cargo_mask, StationIDStack station)
CargoMaskedStationIDStack(CargoTypes cargo_mask, StationIDStack station)
: cargo_mask(cargo_mask), station(station) {}
};
@ -409,7 +409,7 @@ private:
public:
CargoStationIDStackSet()
: first(~0, INVALID_STATION) {}
: first(ALL_CARGOTYPES, INVALID_STATION) {}
const StationIDStack& Get(CargoID cargo) const
{
@ -423,21 +423,21 @@ public:
void FillNextStoppingStation(const Vehicle *v, const OrderList *o, const Order *first = NULL, uint hops = 0);
};
template <typename F> uint32 FilterCargoMask(F filter_func, uint32 cargo_mask = ~0)
template <typename F> CargoTypes FilterCargoMask(F filter_func, CargoTypes cargo_mask = ALL_CARGOTYPES)
{
CargoID cargo;
uint32 output_mask = cargo_mask;
CargoTypes output_mask = cargo_mask;
FOR_EACH_SET_BIT(cargo, cargo_mask) {
if (!filter_func(cargo)) ClrBit(output_mask, cargo);
}
return output_mask;
}
template <typename T, typename F> T CargoMaskValueFilter(uint32 &cargo_mask, F filter_func)
template <typename T, typename F> T CargoMaskValueFilter(CargoTypes &cargo_mask, F filter_func)
{
CargoID first_cargo_id = FindFirstBit(cargo_mask);
T value = filter_func(first_cargo_id);
uint32 other_cargo_mask = cargo_mask;
CargoTypes other_cargo_mask = cargo_mask;
ClrBit(other_cargo_mask, first_cargo_id);
CargoID cargo;
FOR_EACH_SET_BIT(cargo, other_cargo_mask) {
@ -518,8 +518,8 @@ public:
*/
inline VehicleOrderID GetNumManualOrders() const { return this->num_manual_orders; }
CargoMaskedStationIDStack GetNextStoppingStation(const Vehicle *v, uint32 cargo_mask, const Order *first = NULL, uint hops = 0) const;
const Order *GetNextDecisionNode(const Order *next, uint hops, uint32 &cargo_mask) const;
CargoMaskedStationIDStack GetNextStoppingStation(const Vehicle *v, CargoTypes cargo_mask, const Order *first = NULL, uint hops = 0) const;
const Order *GetNextDecisionNode(const Order *next, uint hops, CargoTypes &cargo_mask) const;
void InsertOrderAt(Order *new_order, int index);
void DeleteOrderAt(int index);

@ -309,13 +309,13 @@ void Order::DeAllocExtraInfo()
void CargoStationIDStackSet::FillNextStoppingStation(const Vehicle *v, const OrderList *o, const Order *first, uint hops)
{
this->more.clear();
this->first = o->GetNextStoppingStation(v, ~0, first, hops);
if (this->first.cargo_mask != (uint32) ~0) {
uint32 have_cargoes = this->first.cargo_mask;
this->first = o->GetNextStoppingStation(v, ALL_CARGOTYPES, first, hops);
if (this->first.cargo_mask != ALL_CARGOTYPES) {
CargoTypes have_cargoes = this->first.cargo_mask;
do {
this->more.push_back(o->GetNextStoppingStation(v, ~have_cargoes, first, hops));
have_cargoes |= this->more.back().cargo_mask;
} while (have_cargoes != (uint32) ~0);
} while (have_cargoes != ALL_CARGOTYPES);
}
}
@ -402,7 +402,7 @@ Order *OrderList::GetOrderAt(int index) const
* \li a non-trivial conditional order
* \li NULL if the vehicle won't stop anymore.
*/
const Order *OrderList::GetNextDecisionNode(const Order *next, uint hops, uint32 &cargo_mask) const
const Order *OrderList::GetNextDecisionNode(const Order *next, uint hops, CargoTypes &cargo_mask) const
{
assert(cargo_mask != 0);
@ -448,14 +448,14 @@ const Order *OrderList::GetNextDecisionNode(const Order *next, uint hops, uint32
/**
* Recursively determine the next deterministic station to stop at.
* @param v The vehicle we're looking at.
* @param uint32 cargo_mask Bit-set of the cargo IDs of interest.
* @param CargoTypes cargo_mask Bit-set of the cargo IDs of interest.
* @param first Order to start searching at or NULL to start at cur_implicit_order_index + 1.
* @param hops Number of orders we have already looked at.
* @return A CargoMaskedStationIDStack of the cargo mask the result is valid for, and the next stoppping station or INVALID_STATION.
* @pre The vehicle is currently loading and v->last_station_visited is meaningful.
* @note This function may draw a random number. Don't use it from the GUI.
*/
CargoMaskedStationIDStack OrderList::GetNextStoppingStation(const Vehicle *v, uint32 cargo_mask, const Order *first, uint hops) const
CargoMaskedStationIDStack OrderList::GetNextStoppingStation(const Vehicle *v, CargoTypes cargo_mask, const Order *first, uint hops) const
{
assert(cargo_mask != 0);

@ -45,7 +45,7 @@ std::vector<uint32> _sl_xv_discardable_chunk_ids; ///< list of chunks
static const uint32 _sl_xv_slxi_chunk_version = 0; ///< current version os SLXI chunk
const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = {
{ XSLFI_CARGO_TYPE_ORDERS, XSCF_NULL, 2, 2, "cargo_type_orders", NULL, NULL, "ORDX,VEOX" },
{ XSLFI_CARGO_TYPE_ORDERS, XSCF_NULL, 3, 3, "cargo_type_orders", NULL, NULL, "ORDX,VEOX" },
{ XSLFI_NULL, XSCF_NULL, 0, 0, NULL, NULL, NULL, NULL },// This is the end marker
};

@ -197,7 +197,8 @@ static void Load_ORDR()
const SaveLoad *GetOrderExtraInfoDescription()
{
static const SaveLoad _order_extra_info_desc[] = {
SLE_ARR(OrderExtraInfo, cargo_type_flags, SLE_UINT8, NUM_CARGO),
SLE_CONDARR_X(OrderExtraInfo, cargo_type_flags, SLE_UINT8, 32, 0, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_CARGO_TYPE_ORDERS, 1, 2)),
SLE_CONDARR_X(OrderExtraInfo, cargo_type_flags, SLE_UINT8, NUM_CARGO, 0, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_CARGO_TYPE_ORDERS, 3)),
SLE_END()
};

@ -2168,12 +2168,12 @@ void Vehicle::CancelReservation(StationID next, Station *st)
}
}
uint32 Vehicle::GetLastLoadingStationValidCargoMask() const
CargoTypes Vehicle::GetLastLoadingStationValidCargoMask() const
{
if (!HasBit(this->vehicle_flags, VF_LAST_LOAD_ST_SEP)) {
return (this->last_loading_station != INVALID_STATION) ? ~0 : 0;
return (this->last_loading_station != INVALID_STATION) ? ALL_CARGOTYPES : 0;
} else {
uint32 cargo_mask = 0;
CargoTypes cargo_mask = 0;
for (const Vehicle *u = this; u != NULL; u = u->Next()) {
if (u->cargo_type < NUM_CARGO && u->last_loading_station != INVALID_STATION) {
SetBit(cargo_mask, u->cargo_type);
@ -2197,11 +2197,11 @@ void Vehicle::LeaveStation()
/* Only update the timetable if the vehicle was supposed to stop here. */
if (this->current_order.GetNonStopType() != ONSF_STOP_EVERYWHERE) UpdateVehicleTimetable(this, false);
uint32 cargoes_can_load_unload = this->current_order.FilterLoadUnloadTypeCargoMask([&](const Order *o, CargoID cargo) {
CargoTypes cargoes_can_load_unload = this->current_order.FilterLoadUnloadTypeCargoMask([&](const Order *o, CargoID cargo) {
return ((o->GetCargoLoadType(cargo) & OLFB_NO_LOAD) == 0) || ((o->GetCargoUnloadType(cargo) & OUFB_NO_UNLOAD) == 0);
});
uint32 has_cargo_mask = this->GetLastLoadingStationValidCargoMask();
uint32 cargoes_can_leave_with_cargo = FilterCargoMask([&](CargoID cargo) {
CargoTypes has_cargo_mask = this->GetLastLoadingStationValidCargoMask();
CargoTypes cargoes_can_leave_with_cargo = FilterCargoMask([&](CargoID cargo) {
return this->current_order.CanLeaveWithCargo(HasBit(has_cargo_mask, cargo), cargo);
}, cargoes_can_load_unload);
@ -2214,7 +2214,7 @@ void Vehicle::LeaveStation()
LinkRefresher::Run(this, true, false, cargoes_can_leave_with_cargo);
}
if (cargoes_can_leave_with_cargo == (uint32) ~0) {
if (cargoes_can_leave_with_cargo == ALL_CARGOTYPES) {
/* can leave with all cargoes */
/* if the vehicle could load here or could stop with cargo loaded set the last loading station */

@ -337,7 +337,7 @@ public:
/** We want to 'destruct' the right class. */
virtual ~Vehicle();
uint32 GetLastLoadingStationValidCargoMask() const;
CargoTypes GetLastLoadingStationValidCargoMask() const;
void BeginLoading();
void CancelReservation(StationID next, Station *st);

Loading…
Cancel
Save