Cargo type orders: Add support for 64 cargo types

This commit is contained in:
Jonathan G Rennison
2018-07-02 19:20:59 +01:00
parent 0ae32838ea
commit 00b1fe6246
9 changed files with 39 additions and 38 deletions

View File

@@ -72,7 +72,7 @@ enum CargoType {
typedef uint64 CargoTypes; 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 */ /** Class for storing amounts of cargo */
struct CargoArray { struct CargoArray {

View File

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

View File

@@ -23,7 +23,7 @@
*/ */
class LinkRefresher { class LinkRefresher {
public: 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: protected:
/** /**
@@ -89,9 +89,9 @@ protected:
CargoID cargo; ///< Cargo given in last refit order. CargoID cargo; ///< Cargo given in last refit order.
bool allow_merge; ///< If the refresher is allowed to merge or extend link graphs. bool allow_merge; ///< If the refresher is allowed to merge or extend link graphs.
bool is_full_loading; ///< If the vehicle is full loading. 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); bool HandleRefit(CargoID refit_cargo);
void ResetRefit(); void ResetRefit();

View File

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

View File

@@ -309,13 +309,13 @@ void Order::DeAllocExtraInfo()
void CargoStationIDStackSet::FillNextStoppingStation(const Vehicle *v, const OrderList *o, const Order *first, uint hops) void CargoStationIDStackSet::FillNextStoppingStation(const Vehicle *v, const OrderList *o, const Order *first, uint hops)
{ {
this->more.clear(); this->more.clear();
this->first = o->GetNextStoppingStation(v, ~0, first, hops); this->first = o->GetNextStoppingStation(v, ALL_CARGOTYPES, first, hops);
if (this->first.cargo_mask != (uint32) ~0) { if (this->first.cargo_mask != ALL_CARGOTYPES) {
uint32 have_cargoes = this->first.cargo_mask; CargoTypes have_cargoes = this->first.cargo_mask;
do { do {
this->more.push_back(o->GetNextStoppingStation(v, ~have_cargoes, first, hops)); this->more.push_back(o->GetNextStoppingStation(v, ~have_cargoes, first, hops));
have_cargoes |= this->more.back().cargo_mask; 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 a non-trivial conditional order
* \li NULL if the vehicle won't stop anymore. * \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); 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. * Recursively determine the next deterministic station to stop at.
* @param v The vehicle we're looking 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 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. * @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. * @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. * @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. * @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); assert(cargo_mask != 0);

View File

@@ -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 static const uint32 _sl_xv_slxi_chunk_version = 0; ///< current version os SLXI chunk
const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = { 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 { XSLFI_NULL, XSCF_NULL, 0, 0, NULL, NULL, NULL, NULL },// This is the end marker
}; };

View File

@@ -197,7 +197,8 @@ static void Load_ORDR()
const SaveLoad *GetOrderExtraInfoDescription() const SaveLoad *GetOrderExtraInfoDescription()
{ {
static const SaveLoad _order_extra_info_desc[] = { 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() SLE_END()
}; };

View File

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

View File

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