MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / LoadUnloadVehicle

Function LoadUnloadVehicle

src/economy.cpp:1615–1922  ·  view source on GitHub ↗

* Loads/unload the vehicle if possible. * @param front the vehicle to be (un)loaded */

Source from the content-addressed store, hash-verified

1613 * @param front the vehicle to be (un)loaded
1614 */
1615static void LoadUnloadVehicle(Vehicle *front)
1616{
1617 assert(front->current_order.IsType(OT_LOADING));
1618
1619 StationID last_visited = front->last_station_visited;
1620 Station *st = Station::Get(last_visited);
1621
1622 std::vector<StationID> next_station;
1623 front->GetNextStoppingStation(next_station);
1624 bool use_autorefit = front->current_order.IsRefit() && front->current_order.GetRefitCargo() == CARGO_AUTO_REFIT;
1625 CargoArray consist_capleft{};
1626 if (_settings_game.order.improved_load && use_autorefit ?
1627 front->cargo_payment == nullptr : (front->current_order.IsFullLoadOrder())) {
1628 ReserveConsist(st, front,
1629 (use_autorefit && front->load_unload_ticks != 0) ? &consist_capleft : nullptr,
1630 next_station);
1631 }
1632
1633 /* We have not waited enough time till the next round of loading/unloading */
1634 if (front->load_unload_ticks != 0) return;
1635
1636 if (front->type == VEH_TRAIN && (!IsTileType(front->tile, MP_STATION) || GetStationIndex(front->tile) != st->index)) {
1637 /* The train reversed in the station. Take the "easy" way
1638 * out and let the train just leave as it always did. */
1639 front->vehicle_flags.Set(VehicleFlag::LoadingFinished);
1640 front->load_unload_ticks = 1;
1641 return;
1642 }
1643
1644 int new_load_unload_ticks = 0;
1645 bool dirty_vehicle = false;
1646 bool dirty_station = false;
1647
1648 bool completely_emptied = true;
1649 bool anything_unloaded = false;
1650 bool anything_loaded = false;
1651 CargoTypes full_load_amount = 0;
1652 CargoTypes cargo_not_full = 0;
1653 CargoTypes cargo_full = 0;
1654 CargoTypes reservation_left = 0;
1655
1656 front->cur_speed = 0;
1657
1658 CargoPayment *payment = front->cargo_payment;
1659
1660 uint artic_part = 0; // Articulated part we are currently trying to load. (not counting parts without capacity)
1661 for (Vehicle *v = front; v != nullptr; v = v->Next()) {
1662 if (v == front || !v->Previous()->HasArticulatedPart()) artic_part = 0;
1663 if (v->cargo_cap == 0) continue;
1664 artic_part++;
1665
1666 GoodsEntry *ge = &st->goods[v->cargo_type];
1667
1668 if (v->vehicle_flags.Test(VehicleFlag::CargoUnloading) && front->current_order.GetUnloadType() != OrderUnloadType::NoUnload) {
1669 uint cargo_count = v->cargo.UnloadCount();
1670 uint amount_unloaded = _settings_game.order.gradual_loading ? std::min(cargo_count, GetLoadAmount(v)) : cargo_count;
1671 bool remaining = false; // Are there cargo entities in this vehicle that can still be unloaded here?
1672

Callers 1

LoadUnloadStationFunction · 0.85

Calls 15

ReserveConsistFunction · 0.85
IsTileTypeFunction · 0.85
GetStationIndexFunction · 0.85
GetLoadAmountFunction · 0.85
InvalidFunction · 0.85
HandleStationRefitFunction · 0.85
NOT_REACHEDFunction · 0.85
SetBitFunction · 0.85
ClrBitFunction · 0.85

Tested by

no test coverage detected