diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index 464113a6c1..35fe61395d 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -1421,15 +1421,8 @@ static void HandleAircraftLoading(Vehicle *v, int mode) if (mode != 0) return; if (--v->load_unload_time_rem != 0) return; - if (CanFillVehicle(v) && ( - v->current_order.flags & OF_FULL_LOAD || - (_patches.gradual_loading && !HASBIT(v->vehicle_flags, VF_LOADING_FINISHED)) - )) { - SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_INC); - if (LoadUnloadVehicle(v, false)) { - InvalidateWindow(WC_AIRCRAFT_LIST, v->owner); - v->MarkDirty(); - } + if (CanFillVehicle(v)) { + LoadUnloadVehicle(v); return; } diff --git a/src/economy.cpp b/src/economy.cpp index 111903163b..159685c48d 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -1381,11 +1381,8 @@ static bool LoadWait(const Vehicle* v, const Vehicle* u) /** * Performs the vehicle payment _and_ marks the vehicle to be unloaded. * @param front_v the vehicle to be unloaded - * @return what windows need to be updated; - * bit 0 set: only vehicle details, - * bit 1 set: vehicle details and station details */ -static int VehiclePayment(Vehicle *front_v) +void VehiclePayment(Vehicle *front_v) { int result = 0; @@ -1401,10 +1398,22 @@ static int VehiclePayment(Vehicle *front_v) StationID last_visited = front_v->last_station_visited; Station *st = GetStation(last_visited); + /* The owner of the train wants to be paid */ + PlayerID old_player = _current_player; + _current_player = front_v->owner; + + /* At this moment loading cannot be finished */ + CLRBIT(front_v->vehicle_flags, VF_LOADING_FINISHED); + + /* Start unloading in at the first possible moment */ + front_v->load_unload_time_rem = 1; + for (Vehicle *v = front_v; v != NULL; v = v->next) { + /* No cargo to unload */ if (v->cargo_cap == 0) continue; SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING); + /* All cargo has already been paid for, no need to pay again */ if (v->cargo_count == v->cargo_paid_for) continue; GoodsEntry *ge = &st->goods[v->cargo_type]; @@ -1465,17 +1474,17 @@ static int VehiclePayment(Vehicle *front_v) ShowCostOrIncomeAnimation(front_v->x_pos, front_v->y_pos, front_v->z_pos, -total_veh_profit); } - return result; + _current_player = old_player; } -int LoadUnloadVehicle(Vehicle *v, bool just_arrived) +int LoadUnloadVehicle(Vehicle *v) { int unloading_time = 20; Vehicle *u = v; int result = 0; int t; uint count, cap; - PlayerID old_player; + bool completely_empty = true; byte load_amount; bool anything_loaded = false; @@ -1491,14 +1500,9 @@ int LoadUnloadVehicle(Vehicle *v, bool just_arrived) * enabling though. */ SETBIT(v->vehicle_flags, VF_LOADING_FINISHED); - old_player = _current_player; - _current_player = v->owner; - StationID last_visited = v->last_station_visited; Station *st = GetStation(last_visited); - if (just_arrived) result |= VehiclePayment(v); - for (; v != NULL; v = v->next) { GoodsEntry* ge; load_amount = EngInfo(v->engine_type)->load_amount; @@ -1596,7 +1600,10 @@ int LoadUnloadVehicle(Vehicle *v, bool just_arrived) /* Skip loading this vehicle if another train/vehicle is already handling * the same cargo type at this station */ - if (_patches.improved_load && (u->current_order.flags & OF_FULL_LOAD) && LoadWait(v,u)) continue; + if (_patches.improved_load && (u->current_order.flags & OF_FULL_LOAD) && LoadWait(v,u)) { + CLRBIT(u->vehicle_flags, VF_LOADING_FINISHED); + continue; + } /* TODO: Regarding this, when we do gradual loading, we * should first unload all vehicles and then start @@ -1673,13 +1680,15 @@ int LoadUnloadVehicle(Vehicle *v, bool just_arrived) } if (result != 0) { + InvalidateWindow(v->GetVehicleListWindowClass(), v->owner); InvalidateWindow(WC_VEHICLE_DETAILS, v->index); + st->MarkTilesDirty(); + v->MarkDirty(); if (result & 2) InvalidateWindow(WC_STATION_VIEW, last_visited); } - _current_player = old_player; return result; } diff --git a/src/economy.h b/src/economy.h index b15f42221e..a61bab7092 100644 --- a/src/economy.h +++ b/src/economy.h @@ -68,4 +68,6 @@ void DeleteSubsidyWithStation(StationID index); int32 GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, CargoID cargo_type); uint MoveGoodsToStation(TileIndex tile, int w, int h, CargoID type, uint amount); +void VehiclePayment(Vehicle *front_v); + #endif /* ECONOMY_H */ diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index a4525d2d14..c11e9c5301 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -761,13 +761,8 @@ static void HandleRoadVehLoading(Vehicle *v) if (--v->load_unload_time_rem != 0) return; - if (CanFillVehicle(v) && (v->current_order.flags & OF_FULL_LOAD || - (_patches.gradual_loading && !HASBIT(v->vehicle_flags, VF_LOADING_FINISHED)))) { - SET_EXPENSES_TYPE(EXPENSES_ROADVEH_INC); - if (LoadUnloadVehicle(v, false)) { - InvalidateWindow(WC_ROADVEH_LIST, v->owner); - v->MarkDirty(); - } + if (CanFillVehicle(v)) { + LoadUnloadVehicle(v); return; } diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 9878e7cd96..0e75f67183 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -306,15 +306,8 @@ static void HandleShipLoading(Vehicle *v) case OT_LOADING: { if (--v->load_unload_time_rem != 0) return; - if (CanFillVehicle(v) && ( - v->current_order.flags & OF_FULL_LOAD || - (_patches.gradual_loading && !HASBIT(v->vehicle_flags, VF_LOADING_FINISHED)) - )) { - SET_EXPENSES_TYPE(EXPENSES_SHIP_INC); - if (LoadUnloadVehicle(v, false)) { - InvalidateWindow(WC_SHIPS_LIST, v->owner); - v->MarkDirty(); - } + if (CanFillVehicle(v)) { + LoadUnloadVehicle(v); return; } PlayShipSound(v); diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 4e1d2064c1..ef2f3178da 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -2544,16 +2544,8 @@ static void HandleTrainLoading(Vehicle *v, bool mode) if (--v->load_unload_time_rem) return; - if (CanFillVehicle(v) && ( - v->current_order.flags & OF_FULL_LOAD || - (_patches.gradual_loading && !HASBIT(v->vehicle_flags, VF_LOADING_FINISHED)) - )) { - v->u.rail.days_since_order_progr = 0; // Prevent a train lost message for full loading trains - SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC); - if (LoadUnloadVehicle(v, false)) { - InvalidateWindow(WC_TRAINS_LIST, v->owner); - v->MarkDirty(); - } + if (CanFillVehicle(v)) { + LoadUnloadVehicle(v); return; } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 20f67c771f..8920c43760 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -40,6 +40,7 @@ #include "newgrf_engine.h" #include "newgrf_sound.h" #include "helpers.hpp" +#include "economy.h" #define INVALID_COORD (-0x8000) #define GEN_HASH(x, y) ((GB((y), 6, 6) << 6) + GB((x), 7, 6)) @@ -741,6 +742,7 @@ static bool CanFillVehicle_FullLoadAny(Vehicle *v) return keep_loading || (not_full && (full & ~not_full) == 0); } + bool CanFillVehicle(Vehicle *v) { TileIndex tile = v->tile; @@ -2971,11 +2973,15 @@ void Vehicle::BeginLoading() GetStation(this->last_station_visited)->loading_vehicles.push_back(this); SET_EXPENSES_TYPE(this->GetExpenseType(true)); - if (LoadUnloadVehicle(this, true) != 0) { - InvalidateWindow(this->GetVehicleListWindowClass(), this->owner); - this->MarkDirty(); - } + VehiclePayment(this); + + InvalidateWindow(this->GetVehicleListWindowClass(), this->owner); InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, STATUS_BAR); + InvalidateWindow(WC_VEHICLE_DETAILS, this->index); + InvalidateWindow(WC_STATION_VIEW, this->last_station_visited); + + GetStation(this->last_station_visited)->MarkTilesDirty(); + this->MarkDirty(); } void Vehicle::LeaveStation() diff --git a/src/vehicle.h b/src/vehicle.h index 904a9f9e5f..810f1ba2d7 100644 --- a/src/vehicle.h +++ b/src/vehicle.h @@ -508,7 +508,7 @@ void ShowAircraftViewWindow(const Vehicle* v); UnitID GetFreeUnitNumber(byte type); -int LoadUnloadVehicle(Vehicle *v, bool just_arrived); +int LoadUnloadVehicle(Vehicle *v); void TrainConsistChanged(Vehicle *v); void TrainPowerChanged(Vehicle *v);