Merge branch 'cargo_type_order' into jgrpp

# Conflicts:
#	src/order_base.h
#	src/order_gui.cpp
#	src/order_type.h
#	src/saveload/extended_ver_sl.cpp
#	src/saveload/extended_ver_sl.h
#	src/vehicle_base.h
This commit is contained in:
Jonathan G Rennison
2016-09-08 00:12:48 +01:00
19 changed files with 1079 additions and 159 deletions

View File

@@ -1251,6 +1251,30 @@ Money CargoPayment::PayTransfer(const CargoPacket *cp, uint count)
return profit; // account for the (virtual) profit already made for the cargo packet
}
/**
* Returns the load type of a vehicle.
* In case of cargo type order, the load type returned depends on the cargo carriable by the vehicle.
* @pre v != NULL
* @param v A pointer to a vehicle.
* @return the load type of this vehicle.
*/
static OrderLoadFlags GetLoadType(const Vehicle *v)
{
return v->First()->current_order.GetCargoLoadType(v->cargo_type);
}
/**
* Returns the unload type of a vehicle.
* In case of cargo type order, the unload type returned depends on the cargo carriable by the vehicle.
* @pre v != NULL
* @param v A pointer to a vehicle.
* @return The unload type of this vehicle.
*/
static OrderUnloadFlags GetUnloadType(const Vehicle *v)
{
return v->First()->current_order.GetCargoUnloadType(v->cargo_type);
}
/**
* Prepare the vehicle to be unloaded.
* @param curr_station the station where the consist is at the moment
@@ -1274,16 +1298,17 @@ void PrepareUnload(Vehicle *front_v)
assert(CargoPayment::CanAllocateItem());
front_v->cargo_payment = new CargoPayment(front_v);
StationIDStack next_station = front_v->GetNextStoppingStation();
CargoStationIDStackSet next_station = front_v->GetNextStoppingStation();
if (front_v->orders.list == NULL || (front_v->current_order.GetUnloadType() & OUFB_NO_UNLOAD) == 0) {
Station *st = Station::Get(front_v->last_station_visited);
for (Vehicle *v = front_v; v != NULL; v = v->Next()) {
if (GetUnloadType(v) & OUFB_NO_UNLOAD) continue;
const GoodsEntry *ge = &st->goods[v->cargo_type];
if (v->cargo_cap > 0 && v->cargo.TotalCount() > 0) {
v->cargo.Stage(
HasBit(ge->status, GoodsEntry::GES_ACCEPTANCE),
front_v->last_station_visited, next_station,
front_v->current_order.GetUnloadType(), ge,
front_v->last_station_visited, next_station.Get(v->cargo_type),
GetUnloadType(v), ge,
front_v->cargo_payment);
if (v->cargo.UnloadCount() > 0) SetBit(v->vehicle_flags, VF_CARGO_UNLOADING);
}
@@ -1436,8 +1461,9 @@ struct FinalizeRefitAction
{
CargoArray &consist_capleft; ///< Capacities left in the consist.
Station *st; ///< Station to reserve cargo from.
StationIDStack &next_station; ///< Next hops to reserve cargo for.
const CargoStationIDStackSet &next_station; ///< Next hops to reserve cargo for.
bool do_reserve; ///< If the vehicle should reserve.
Vehicle *cargo_type_loading; ///< Non-null if vehicle should reserve if the cargo type of the vehicle is a cargo-specific full-load order using this pointer
/**
* Create a finalizing action.
@@ -1445,9 +1471,10 @@ struct FinalizeRefitAction
* @param st Station to reserve cargo from.
* @param next_station Next hops to reserve cargo for.
* @param do_reserve If we should reserve cargo or just add up the capacities.
* @param cargo_type_loading Non-null if vehicle should reserve if the cargo type of the vehicle is a cargo-specific full-load order using this pointer
*/
FinalizeRefitAction(CargoArray &consist_capleft, Station *st, StationIDStack &next_station, bool do_reserve) :
consist_capleft(consist_capleft), st(st), next_station(next_station), do_reserve(do_reserve) {}
FinalizeRefitAction(CargoArray &consist_capleft, Station *st, const CargoStationIDStackSet &next_station, bool do_reserve, Vehicle *cargo_type_loading) :
consist_capleft(consist_capleft), st(st), next_station(next_station), do_reserve(do_reserve), cargo_type_loading(cargo_type_loading) {}
/**
* Reserve cargo from the station and update the remaining consist capacities with the
@@ -1457,9 +1484,9 @@ struct FinalizeRefitAction
*/
bool operator()(Vehicle *v)
{
if (this->do_reserve) {
if (this->do_reserve || (cargo_type_loading == NULL || (cargo_type_loading->current_order.GetCargoLoadTypeRaw(v->cargo_type) & OLFB_FULL_LOAD))) {
this->st->goods[v->cargo_type].cargo.Reserve(v->cargo_cap - v->cargo.RemainingCount(),
&v->cargo, st->xy, this->next_station);
&v->cargo, st->xy, this->next_station.Get(v->cargo_type));
}
this->consist_capleft[v->cargo_type] += v->cargo_cap - v->cargo.RemainingCount();
return true;
@@ -1474,7 +1501,7 @@ struct FinalizeRefitAction
* @param next_station Possible next stations the vehicle can travel to.
* @param new_cid Target cargo for refit.
*/
static void HandleStationRefit(Vehicle *v, CargoArray &consist_capleft, Station *st, StationIDStack next_station, CargoID new_cid)
static void HandleStationRefit(Vehicle *v, CargoArray &consist_capleft, Station *st, CargoStationIDStackSet next_station, CargoID new_cid)
{
Vehicle *v_start = v->GetFirstEnginePart();
if (!IterateVehicleParts(v_start, IsEmptyAction())) return;
@@ -1492,7 +1519,7 @@ static void HandleStationRefit(Vehicle *v, CargoArray &consist_capleft, Station
CargoID cid;
new_cid = v_start->cargo_type;
FOR_EACH_SET_CARGO_ID(cid, refit_mask) {
if (st->goods[cid].cargo.HasCargoFor(next_station)) {
if (st->goods[cid].cargo.HasCargoFor(next_station.Get(cid))) {
/* Try to find out if auto-refitting would succeed. In case the refit is allowed,
* the returned refit capacity will be greater than zero. */
DoCommand(v_start->tile, v_start->index, cid | 1U << 6 | 0xFF << 8 | 1U << 16, DC_QUERY_COST, GetCmdRefitVeh(v_start)); // Auto-refit and only this vehicle including artic parts.
@@ -1524,23 +1551,26 @@ static void HandleStationRefit(Vehicle *v, CargoArray &consist_capleft, Station
/* Add new capacity to consist capacity and reserve cargo */
IterateVehicleParts(v_start, FinalizeRefitAction(consist_capleft, st, next_station,
is_auto_refit || (v->First()->current_order.GetLoadType() & OLFB_FULL_LOAD) != 0));
is_auto_refit || (v->First()->current_order.GetLoadType() & OLFB_FULL_LOAD) != 0,
(v->First()->current_order.GetLoadType() == OLFB_CARGO_TYPE_LOAD) ? v->First() : NULL));
cur_company.Restore();
}
struct ReserveCargoAction {
Station *st;
StationIDStack *next_station;
const CargoStationIDStackSet &next_station;
Vehicle *cargo_type_loading;
ReserveCargoAction(Station *st, StationIDStack *next_station) :
st(st), next_station(next_station) {}
ReserveCargoAction(Station *st, const CargoStationIDStackSet &next_station, Vehicle *cargo_type_loading) :
st(st), next_station(next_station), cargo_type_loading(cargo_type_loading) {}
bool operator()(Vehicle *v)
{
if (cargo_type_loading != NULL && !(cargo_type_loading->current_order.GetCargoLoadTypeRaw(v->cargo_type) & OLFB_FULL_LOAD)) return true;
if (v->cargo_cap > v->cargo.RemainingCount()) {
st->goods[v->cargo_type].cargo.Reserve(v->cargo_cap - v->cargo.RemainingCount(),
&v->cargo, st->xy, *next_station);
&v->cargo, st->xy, next_station.Get(v->cargo_type));
}
return true;
@@ -1555,8 +1585,9 @@ struct ReserveCargoAction {
* @param u Front of the loading vehicle consist.
* @param consist_capleft If given, save free capacities after reserving there.
* @param next_station Station(s) the vehicle will stop at next.
* @param cargo_type_loading check cargo-specific loading type
*/
static void ReserveConsist(Station *st, Vehicle *u, CargoArray *consist_capleft, StationIDStack *next_station)
static void ReserveConsist(Station *st, Vehicle *u, CargoArray *consist_capleft, const CargoStationIDStackSet &next_station, bool cargo_type_loading)
{
/* If there is a cargo payment not all vehicles of the consist have tried to do the refit.
* In that case, only reserve if it's a fixed refit and the equivalent of "articulated chain"
@@ -1572,9 +1603,10 @@ static void ReserveConsist(Station *st, Vehicle *u, CargoArray *consist_capleft,
(v->type != VEH_TRAIN || !Train::From(v)->IsRearDualheaded()) &&
(v->type != VEH_AIRCRAFT || Aircraft::From(v)->IsNormalAircraft()) &&
(must_reserve || u->current_order.GetRefitCargo() == v->cargo_type)) {
IterateVehicleParts(v, ReserveCargoAction(st, next_station));
IterateVehicleParts(v, ReserveCargoAction(st, next_station, cargo_type_loading ? u : NULL));
}
if (consist_capleft == NULL || v->cargo_cap == 0) continue;
if (cargo_type_loading && !(u->current_order.GetCargoLoadTypeRaw(v->cargo_type) & OLFB_FULL_LOAD)) continue;
(*consist_capleft)[v->cargo_type] += v->cargo_cap - v->cargo.RemainingCount();
}
}
@@ -1611,14 +1643,25 @@ static void LoadUnloadVehicle(Vehicle *front)
StationID last_visited = front->last_station_visited;
Station *st = Station::Get(last_visited);
StationIDStack next_station = front->GetNextStoppingStation();
CargoStationIDStackSet next_station = front->GetNextStoppingStation();
bool use_autorefit = front->current_order.IsRefit() && front->current_order.GetRefitCargo() == CT_AUTO_REFIT;
CargoArray consist_capleft;
if (_settings_game.order.improved_load && use_autorefit ?
front->cargo_payment == NULL : (front->current_order.GetLoadType() & OLFB_FULL_LOAD) != 0) {
bool should_reserve_consist = false;
bool reserve_consist_cargo_type_loading = false;
if (_settings_game.order.improved_load && use_autorefit) {
if (front->cargo_payment == NULL) should_reserve_consist = true;
} else {
if ((front->current_order.GetLoadType() & OLFB_FULL_LOAD) || (front->current_order.GetLoadType() == OLFB_CARGO_TYPE_LOAD)) {
should_reserve_consist = true;
reserve_consist_cargo_type_loading = (front->current_order.GetLoadType() == OLFB_CARGO_TYPE_LOAD);
}
}
if (should_reserve_consist) {
ReserveConsist(st, front,
(use_autorefit && front->load_unload_ticks != 0) ? &consist_capleft : NULL,
&next_station);
next_station,
reserve_consist_cargo_type_loading);
}
/* We have not waited enough time till the next round of loading/unloading */
@@ -1658,7 +1701,7 @@ static void LoadUnloadVehicle(Vehicle *front)
GoodsEntry *ge = &st->goods[v->cargo_type];
if (HasBit(v->vehicle_flags, VF_CARGO_UNLOADING) && (front->current_order.GetUnloadType() & OUFB_NO_UNLOAD) == 0) {
if (HasBit(v->vehicle_flags, VF_CARGO_UNLOADING) && (GetUnloadType(v) & OUFB_NO_UNLOAD) == 0) {
uint cargo_count = v->cargo.UnloadCount();
uint amount_unloaded = _settings_game.order.gradual_loading ? min(cargo_count, load_amount) : cargo_count;
bool remaining = false; // Are there cargo entities in this vehicle that can still be unloaded here?
@@ -1668,7 +1711,7 @@ static void LoadUnloadVehicle(Vehicle *front)
if (!HasBit(ge->status, GoodsEntry::GES_ACCEPTANCE) && v->cargo.ActionCount(VehicleCargoList::MTA_DELIVER) > 0) {
/* The station does not accept our goods anymore. */
if (front->current_order.GetUnloadType() & (OUFB_TRANSFER | OUFB_UNLOAD)) {
if (GetUnloadType(v) & (OUFB_TRANSFER | OUFB_UNLOAD)) {
/* Transfer instead of delivering. */
v->cargo.Reassign<VehicleCargoList::MTA_DELIVER, VehicleCargoList::MTA_TRANSFER>(
v->cargo.ActionCount(VehicleCargoList::MTA_DELIVER), INVALID_STATION);
@@ -1725,7 +1768,7 @@ static void LoadUnloadVehicle(Vehicle *front)
}
/* Do not pick up goods when we have no-load set or loading is stopped. */
if (front->current_order.GetLoadType() & OLFB_NO_LOAD || HasBit(front->vehicle_flags, VF_STOP_LOADING)) continue;
if (GetLoadType(v) & OLFB_NO_LOAD || HasBit(front->vehicle_flags, VF_STOP_LOADING)) continue;
/* This order has a refit, if this is the first vehicle part carrying cargo and the whole vehicle is empty, try refitting. */
if (front->current_order.IsRefit() && artic_part == 1) {
@@ -1768,7 +1811,7 @@ static void LoadUnloadVehicle(Vehicle *front)
if (_settings_game.order.gradual_loading) cap_left = min(cap_left, load_amount);
if (v->cargo.StoredCount() == 0) TriggerVehicle(v, VEHICLE_TRIGGER_NEW_CARGO);
uint loaded = ge->cargo.Load(cap_left, &v->cargo, st->xy, next_station);
uint loaded = ge->cargo.Load(cap_left, &v->cargo, st->xy, next_station.Get(v->cargo_type));
if (v->cargo.ActionCount(VehicleCargoList::MTA_LOAD) > 0) {
/* Remember if there are reservations left so that we don't stop
* loading before they're loaded. */
@@ -1828,6 +1871,16 @@ static void LoadUnloadVehicle(Vehicle *front)
if (!anything_unloaded) delete payment;
ClrBit(front->vehicle_flags, VF_STOP_LOADING);
bool has_full_load_order = front->current_order.GetLoadType() & OLFB_FULL_LOAD;
if (front->current_order.GetLoadType() == OLFB_CARGO_TYPE_LOAD) {
for (Vehicle *v = front; v != NULL; v = v->Next()) {
if (front->current_order.GetCargoLoadTypeRaw(v->cargo_type) & OLFB_FULL_LOAD) {
has_full_load_order = true;
break;
}
}
}
if (anything_loaded || anything_unloaded) {
if (_settings_game.order.gradual_loading) {
/* The time it takes to load one 'slice' of cargo or passengers depends
@@ -1838,7 +1891,7 @@ static void LoadUnloadVehicle(Vehicle *front)
}
/* We loaded less cargo than possible for all cargo types and it's not full
* load and we're not supposed to wait any longer: stop loading. */
if (!anything_unloaded && full_load_amount == 0 && reservation_left == 0 && !(front->current_order.GetLoadType() & OLFB_FULL_LOAD) &&
if (!anything_unloaded && full_load_amount == 0 && reservation_left == 0 && !has_full_load_order &&
front->current_order_time >= (uint)max(front->current_order.GetTimetabledWait() - front->lateness_counter, 0)) {
SetBit(front->vehicle_flags, VF_STOP_LOADING);
}
@@ -1847,7 +1900,7 @@ static void LoadUnloadVehicle(Vehicle *front)
} else {
UpdateLoadUnloadTicks(front, st, 20); // We need the ticks for link refreshing.
bool finished_loading = true;
if (front->current_order.GetLoadType() & OLFB_FULL_LOAD) {
if (has_full_load_order) {
if (front->current_order.GetLoadType() == OLF_FULL_LOAD_ANY) {
/* if the aircraft carries passengers and is NOT full, then
* continue loading, no matter how much mail is in */