diff --git a/src/economy.cpp b/src/economy.cpp index 8711011..02f121d 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -1097,6 +1097,7 @@ void PrepareUnload(Vehicle *front_v) if ((front_v->current_order.GetUnloadType() & OUFB_NO_UNLOAD) == 0) { for (Vehicle *v = front_v; v != NULL; v = v->Next()) { + ClrBit(v->vehicle_flags, VF_LOADING_ALMOST_FINISHED); if (v->cargo_cap > 0 && !v->cargo.Empty()) { SetBit(v->vehicle_flags, VF_CARGO_UNLOADING); } @@ -1162,6 +1163,10 @@ static void LoadUnloadVehicle(Vehicle *v, int *cargo_left) for (; v != NULL; v = v->Next()) { if (v->cargo_cap == 0) continue; + /* Ignore any newly available cargo when we already got + * everything that was available on the previous check */ + if (HasBit(v->vehicle_flags, VF_LOADING_ALMOST_FINISHED)) continue; + const Engine *e = Engine::Get(v->engine_type); byte load_amount = e->info.load_amount; @@ -1255,6 +1260,17 @@ static void LoadUnloadVehicle(Vehicle *v, int *cargo_left) uint cap = cap_left; uint count = ge->cargo.Count(); + /* Make this vehicle leave when we got all available cargo and/or when it is + * time to leave, ignoring new cargo arriving at the station during loading */ + if (!anything_unloaded && + ((uint) cap_left > count) && + (!_settings_game.order.gradual_loading || + (load_amount > count)) && + (!_settings_game.order.timetabling || + (v->current_order_time >= (uint) max(v->current_order.wait_time - v->lateness_counter, 0)))) { + SetBit(v->vehicle_flags, VF_LOADING_ALMOST_FINISHED); + } + /* Skip loading this vehicle if another train/vehicle is already handling * the same cargo type at this station */ if (_settings_game.order.improved_load && cargo_left[v->cargo_type] <= 0) { diff --git a/src/vehicle_base.h b/src/vehicle_base.h index bea2485..9a36344 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -40,6 +40,7 @@ enum VehicleFlags { VF_TIMETABLE_STARTED, ///< Whether the vehicle has started running on the timetable yet. VF_AUTOFILL_TIMETABLE, ///< Whether the vehicle should fill in the timetable automatically. VF_AUTOFILL_PRES_WAIT_TIME, ///< Whether non-destructive auto-fill should preserve waiting times + VF_LOADING_ALMOST_FINISHED, ///< Whether we should leave when the currently loading cargo has loaded. }; /** Cached oftenly queried (NewGRF) values */