(svn r26917) -Revert (r26857): It broke improved loading.

This commit is contained in:
fonsinchen 2014-09-24 20:55:47 +00:00
parent 10a1166465
commit d6fc217136
1 changed files with 29 additions and 94 deletions

View File

@ -1347,93 +1347,36 @@ static void ReserveConsist(Station *st, Vehicle *u, CargoArray *consist_capleft,
* @tparam Taction Class of action to be applied. Must implement bool operator()([const] Vehicle *). * @tparam Taction Class of action to be applied. Must implement bool operator()([const] Vehicle *).
* @param v First articulated part. * @param v First articulated part.
* @param action Instance of Taction. * @param action Instance of Taction.
* @param abort_on_false If set, don't iterate other parts if one has returned false.
* @return false if any of the action invocations returned false, true otherwise. * @return false if any of the action invocations returned false, true otherwise.
*/ */
template<class Taction> template<class Taction>
bool IterateVehicleParts(Vehicle *v, Taction action, bool abort_on_false = false) bool IterateVehicleParts(Vehicle *v, Taction action)
{ {
bool result = true;
for (Vehicle *w = v; w != NULL; for (Vehicle *w = v; w != NULL;
w = w->HasArticulatedPart() ? w->GetNextArticulatedPart() : NULL) { w = w->HasArticulatedPart() ? w->GetNextArticulatedPart() : NULL) {
if (!action(w)) { if (!action(w)) return false;
if (abort_on_false) return false;
result = false;
}
if (w->type == VEH_TRAIN) { if (w->type == VEH_TRAIN) {
Train *train = Train::From(w); Train *train = Train::From(w);
if (train->IsMultiheaded() && !action(train->other_multiheaded_part)) { if (train->IsMultiheaded() && !action(train->other_multiheaded_part)) return false;
if (abort_on_false) return false;
result = false;
} }
} }
} if (v->type == VEH_AIRCRAFT && Aircraft::From(v)->IsNormalAircraft()) return action(v->Next());
if (v->type == VEH_AIRCRAFT && Aircraft::From(v)->IsNormalAircraft() && return true;
!action(v->Next())) {
return false;
}
return result;
} }
/** /**
* Action to reserve cargo. * Action to check if a vehicle has no stored cargo.
*/ */
struct ReserveAction { struct IsEmptyAction
CargoArray &consist_capleft; ///< Capacities left in the consist.
Station *st; ///< Station to reserve cargo from.
StationIDStack &next_station; ///< Next hops to reserve cargo for.
bool do_reserve; ///< If we want cargo to be reserved at all.
/**
* Create a reserve action.
* @param consist_capleft Capacities left in the consist.
* @param st Station to reserve cargo from.
* @param next_station Next hops to reserve cargo for.
*/
ReserveAction(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)
{}
uint operator()(Vehicle *v)
{
if (!do_reserve) return 0;
return this->st->goods[v->cargo_type].cargo.Reserve(
v->cargo_cap - v->cargo.RemainingCount(),
&v->cargo, this->st->xy, this->next_station);
}
};
/**
* Action to check if a vehicle has no stored cargo or otherwise reserve for it if Treserve is set.
* @tparam Treserve If true do reserve if vehicle has cargo, otherwise don't.
*/
struct CheckOrReserveAction : public ReserveAction
{ {
/** /**
* Create a check/reserve action. * Checks if the vehicle has stored cargo.
* @param consist_capleft Capacities left in the consist.
* @param st Station to reserve cargo from.
* @param next_station Next hops to reserve cargo for.
*/
CheckOrReserveAction(CargoArray &consist_capleft, Station *st, StationIDStack &next_station,
bool do_reserve) :
ReserveAction(consist_capleft, st, next_station, do_reserve) {}
/**
* Checks if the vehicle has stored cargo and if yes, reserves more cargo for it.
* @param v Vehicle to be checked. * @param v Vehicle to be checked.
* @return true if v is either empty or has only reserved cargo, false otherwise. * @return true if v is either empty or has only reserved cargo, false otherwise.
*/ */
bool operator()(Vehicle *v) bool operator()(const Vehicle *v)
{ {
if (v->cargo.StoredCount() == 0) { return v->cargo.StoredCount() == 0;
return true;
} else {
this->consist_capleft[v->cargo_type] -= ReserveAction::operator()(v);
return false;
}
} }
}; };
@ -1497,17 +1440,20 @@ struct ReturnCargoAction
/** /**
* Action for finalizing a refit. * Action for finalizing a refit.
*/ */
struct FinalizeRefitAction : public ReserveAction 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.
/** /**
* Create a finalizing action. * Create a finalizing action.
* @param consist_capleft Capacities left in the consist. * @param consist_capleft Capacities left in the consist.
* @param st Station to reserve cargo from. * @param st Station to reserve cargo from.
* @param next_station Next hops to reserve cargo for. * @param next_station Next hops to reserve cargo for.
*/ */
FinalizeRefitAction(CargoArray &consist_capleft, Station *st, StationIDStack &next_station, FinalizeRefitAction(CargoArray &consist_capleft, Station *st, StationIDStack &next_station) :
bool do_reserve) : consist_capleft(consist_capleft), st(st), next_station(next_station) {}
ReserveAction(consist_capleft, st, next_station, do_reserve) {}
/** /**
* Reserve cargo from the station and update the remaining consist capacities with the * Reserve cargo from the station and update the remaining consist capacities with the
@ -1517,7 +1463,8 @@ struct FinalizeRefitAction : public ReserveAction
*/ */
bool operator()(Vehicle *v) bool operator()(Vehicle *v)
{ {
ReserveAction::operator()(v); this->st->goods[v->cargo_type].cargo.Reserve(v->cargo_cap - v->cargo.RemainingCount(),
&v->cargo, st->xy, next_station);
this->consist_capleft[v->cargo_type] += v->cargo_cap - v->cargo.RemainingCount(); this->consist_capleft[v->cargo_type] += v->cargo_cap - v->cargo.RemainingCount();
return true; return true;
} }
@ -1530,17 +1477,11 @@ struct FinalizeRefitAction : public ReserveAction
* @param st Station the vehicle is loading at. * @param st Station the vehicle is loading at.
* @param next_station Possible next stations the vehicle can travel to. * @param next_station Possible next stations the vehicle can travel to.
* @param new_cid Target cargo for refit. * @param new_cid Target cargo for refit.
* @param full_load If the order we're currently following has a full load modifier.
*/ */
static void HandleStationRefit(Vehicle *v, CargoArray &consist_capleft, Station *st, static void HandleStationRefit(Vehicle *v, CargoArray &consist_capleft, Station *st, StationIDStack next_station, CargoID new_cid)
StationIDStack next_station, CargoID new_cid, bool full_load)
{ {
bool reserve = full_load || new_cid == CT_AUTO_REFIT;
Vehicle *v_start = v->GetFirstEnginePart(); Vehicle *v_start = v->GetFirstEnginePart();
if (!IterateVehicleParts(v_start, if (!IterateVehicleParts(v_start, IsEmptyAction())) return;
CheckOrReserveAction(consist_capleft, st, next_station, reserve), reserve)) {
return;
}
Backup<CompanyByte> cur_company(_current_company, v->owner, FILE_LINE); Backup<CompanyByte> cur_company(_current_company, v->owner, FILE_LINE);
@ -1585,7 +1526,7 @@ static void HandleStationRefit(Vehicle *v, CargoArray &consist_capleft, Station
} }
/* Add new capacity to consist capacity and reserve cargo */ /* Add new capacity to consist capacity and reserve cargo */
IterateVehicleParts(v_start, FinalizeRefitAction(consist_capleft, st, next_station, reserve)); IterateVehicleParts(v_start, FinalizeRefitAction(consist_capleft, st, next_station));
cur_company.Restore(); cur_company.Restore();
} }
@ -1623,17 +1564,12 @@ static void LoadUnloadVehicle(Vehicle *front)
Station *st = Station::Get(last_visited); Station *st = Station::Get(last_visited);
StationIDStack next_station = front->GetNextStoppingStation(); StationIDStack next_station = front->GetNextStoppingStation();
bool use_stationrefit = front->current_order.IsRefit(); bool use_autorefit = front->current_order.IsRefit() && front->current_order.GetRefitCargo() == CT_AUTO_REFIT;
CargoArray consist_capleft; CargoArray consist_capleft;
bool do_reserve = _settings_game.order.improved_load && if (_settings_game.order.improved_load &&
(front->current_order.GetLoadType() & OLFB_FULL_LOAD) != 0; ((front->current_order.GetLoadType() & OLFB_FULL_LOAD) != 0 || use_autorefit)) {
ReserveConsist(st, front,
/* Refitting to a fixed cargo will most likely invalidate any reservations, so we (use_autorefit && front->load_unload_ticks != 0) ? &consist_capleft : NULL,
* shouldn't do them here. HandleStationRefit reserves after refitting, or instead of
* refitting if the vehicle isn't empty. */
if (do_reserve && !use_stationrefit) {
ReserveConsist(st, front, (use_stationrefit && front->load_unload_ticks != 0) ?
&consist_capleft : NULL,
next_station); next_station);
} }
@ -1743,9 +1679,8 @@ static void LoadUnloadVehicle(Vehicle *front)
if (front->current_order.GetLoadType() & OLFB_NO_LOAD || HasBit(front->vehicle_flags, VF_STOP_LOADING)) continue; if (front->current_order.GetLoadType() & 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. */ /* This order has a refit, if this is the first vehicle part carrying cargo and the whole vehicle is empty, try refitting. */
if (use_stationrefit && artic_part == 1) { if (front->current_order.IsRefit() && artic_part == 1) {
HandleStationRefit(v, consist_capleft, st, next_station, HandleStationRefit(v, consist_capleft, st, next_station, front->current_order.GetRefitCargo());
front->current_order.GetRefitCargo(), do_reserve);
ge = &st->goods[v->cargo_type]; ge = &st->goods[v->cargo_type];
} }