Index: cargopacket.cpp =================================================================== --- cargopacket.cpp (revision 22851) +++ cargopacket.cpp (working copy) @@ -12,7 +12,10 @@ #include "stdafx.h" #include "core/pool_func.hpp" #include "economy_base.h" +#include "vehicle_base.h" +#include "station_map.h" + /* Initialize the cargopacket-pool */ CargoPacketPool _cargopacket_pool("CargoPacket"); INSTANTIATE_POOL_METHODS(CargoPacket) @@ -249,8 +252,8 @@ * @param data Depending on mta the data of this variable differs: * - MTA_FINAL_DELIVERY - Station ID of packet's origin not to remove. * - MTA_CARGO_LOAD - Station's tile index of load. - * - MTA_TRANSFER - Unused. - * - MTA_UNLOAD - Unused. + * - MTA_TRANSFER - Station ID of packet's origin not to remove. + * - MTA_UNLOAD - Station ID of packet's origin not to remove. * @param payment The payment helper. * * @pre mta == MTA_FINAL_DELIVERY || dest != NULL @@ -259,7 +262,7 @@ */ template template -bool CargoList::MoveTo(Tother_inst *dest, uint max_move, MoveToAction mta, CargoPayment *payment, uint data) +bool CargoList::MoveTo(Tother_inst *dest, uint max_move, MoveToAction mta, CargoPayment *payment, uint data, Vehicle *v) { assert(mta == MTA_FINAL_DELIVERY || dest != NULL); assert(mta == MTA_UNLOAD || mta == MTA_CARGO_LOAD || payment != NULL); @@ -273,6 +276,22 @@ continue; } + if (cp->loaded_at_xy != 0 && GetStationIndex(cp->loaded_at_xy) == data && (mta == MTA_FINAL_DELIVERY || mta == MTA_UNLOAD || mta == MTA_TRANSFER)) { + /* Skip unloading cargo that we just picked up from this station. */ + ++it; + continue; + } + + if (v != NULL && mta == MTA_CARGO_LOAD && cp->SourceStationXY() != data && v->First()->orders.list != NULL && v->First()->orders.list->ContainsStation(GetStationIndex(cp->loaded_at_xy))) { + /* Skip loading cargo that originated from this vehicle station list + * but ofcourse do not skip loading cargo that's originated at this station. + */ + ++it; + continue; + + } + + if (cp->count <= max_move) { /* Can move the complete packet */ max_move -= cp->count; @@ -338,6 +357,33 @@ return it != packets.end(); } +/** Count cargo that is not last pickuped at a station which is in orders list from the vehicle **/ +template +uint CargoList::CountNotInOrdersList(Vehicle *v) +{ + uint count = 0; + if (v != NULL && v->First()->orders.list != NULL ) { + + Iterator it(this->packets.begin()); + while (it != this->packets.end()) { + CargoPacket *cp = *it; + + if ((v->First()->last_station_visited == cp->SourceStation()) || !v->First()->orders.list->ContainsStation(GetStationIndex(cp->loaded_at_xy))) { + /* count only the cargo that is localy generated OR + * cargo that is not lasted picked up at a station which is in the vehicle orders list + */ + count += cp->count; + } + ++it; + } + return count; + } + else { return 0;} +} + + + + /** Invalidates the cached data and rebuilds it. */ template void CargoList::InvalidateCache() @@ -401,8 +447,8 @@ template class CargoList; /** Autoreplace Vehicle -> Vehicle 'transfer'. */ -template bool CargoList::MoveTo(VehicleCargoList *, uint max_move, MoveToAction mta, CargoPayment *payment, uint data); +template bool CargoList::MoveTo(VehicleCargoList *, uint max_move, MoveToAction mta, CargoPayment *payment, uint data, Vehicle *v); /** Cargo unloading at a station. */ -template bool CargoList::MoveTo(StationCargoList *, uint max_move, MoveToAction mta, CargoPayment *payment, uint data); +template bool CargoList::MoveTo(StationCargoList *, uint max_move, MoveToAction mta, CargoPayment *payment, uint data, Vehicle *v); /** Cargo loading at a station. */ -template bool CargoList::MoveTo(VehicleCargoList *, uint max_move, MoveToAction mta, CargoPayment *payment, uint data); +template bool CargoList::MoveTo(VehicleCargoList *, uint max_move, MoveToAction mta, CargoPayment *payment, uint data, Vehicle *v); Index: cargopacket.h =================================================================== --- cargopacket.h (revision 22851) +++ cargopacket.h (working copy) @@ -224,6 +224,7 @@ return this->count; } + /** * Returns source of the first cargo packet in this list. * @return The before mentioned source. @@ -247,8 +248,10 @@ void Truncate(uint max_remaining); template - bool MoveTo(Tother_inst *dest, uint count, MoveToAction mta, CargoPayment *payment, uint data = 0); + bool MoveTo(Tother_inst *dest, uint count, MoveToAction mta, CargoPayment *payment, uint data = 0, Vehicle *v = NULL); + uint CountNotInOrdersList(Vehicle *v); + void InvalidateCache(); }; Index: economy.cpp =================================================================== --- economy.cpp (revision 22851) +++ economy.cpp (working copy) @@ -1205,7 +1205,7 @@ * station is still accepting the cargo in the vehicle. It doesn't * accept cargo that was loaded at the same station. */ if ((u->current_order.GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) && (!accepted || v->cargo.Count() == cargo_count)) { - remaining = v->cargo.MoveTo(&ge->cargo, amount_unloaded, u->current_order.GetUnloadType() & OUFB_TRANSFER ? VehicleCargoList::MTA_TRANSFER : VehicleCargoList::MTA_UNLOAD, payment); + remaining = v->cargo.MoveTo(&ge->cargo, amount_unloaded, u->current_order.GetUnloadType() & OUFB_TRANSFER ? VehicleCargoList::MTA_TRANSFER : VehicleCargoList::MTA_UNLOAD, payment, last_visited); if (!HasBit(ge->acceptance_pickup, GoodsEntry::PICKUP)) { InvalidateWindowData(WC_STATION_LIST, last_visited); SetBit(ge->acceptance_pickup, GoodsEntry::PICKUP); @@ -1268,10 +1268,15 @@ ge->last_age = _cur_year - u->build_year; ge->days_since_pickup = 0; + /* count the cargo that is not sourced from else in the order list from the vehicle */ + + uint count_cargo_not_local = ge->cargo.CountNotInOrdersList(v); + + /* If there's goods waiting at the station, and the vehicle * has capacity for it, load it on the vehicle. */ int cap_left = v->cargo_cap - v->cargo.Count(); - if (!ge->cargo.Empty() && cap_left > 0) { + if (!ge->cargo.Empty() && (cap_left > 0) && (count_cargo_not_local > 0)) { uint cap = cap_left; uint count = ge->cargo.Count(); @@ -1313,7 +1318,7 @@ completely_emptied = false; anything_loaded = true; - ge->cargo.MoveTo(&v->cargo, cap, StationCargoList::MTA_CARGO_LOAD, NULL, st->xy); + ge->cargo.MoveTo(&v->cargo, cap, StationCargoList::MTA_CARGO_LOAD, NULL, st->xy, v); st->time_since_load = 0; st->last_vehicle_type = v->type; Index: order_base.h =================================================================== --- order_base.h (revision 22851) +++ order_base.h (working copy) @@ -263,6 +263,9 @@ */ inline uint GetNumVehicles() const { return this->num_vehicles; } + bool ContainsStation(StationID station) const; + + bool IsVehicleInSharedOrdersList(const Vehicle *v) const; int GetPositionInSharedOrderList(const Vehicle *v) const; Index: order_cmd.cpp =================================================================== --- order_cmd.cpp (revision 22851) +++ order_cmd.cpp (working copy) @@ -462,7 +462,27 @@ return count; } + /** + * Checks if a station is in the vehicle order list + * @param station is the StationID to look for + * @return whether the StatID is in the vehicle order list + */ + +bool OrderList::ContainsStation (StationID station) const +{ + for (Order *o = this->first; o != NULL; o = o->next) { + if (o->GetDestination() == station) return true; + } + return false; +} + + + + + + +/** * Checks whether all orders of the list have a filled timetable. * @return whether all orders have a filled timetable. */