|
|
@ -18,6 +18,9 @@
|
|
|
|
CargoPacketPool _cargopacket_pool("CargoPacket");
|
|
|
|
CargoPacketPool _cargopacket_pool("CargoPacket");
|
|
|
|
INSTANTIATE_POOL_METHODS(CargoPacket)
|
|
|
|
INSTANTIATE_POOL_METHODS(CargoPacket)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Initialize, i.e. clean, the pool with cargo packets.
|
|
|
|
|
|
|
|
*/
|
|
|
|
void InitializeCargoPackets()
|
|
|
|
void InitializeCargoPackets()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_cargopacket_pool.CleanPool();
|
|
|
|
_cargopacket_pool.CleanPool();
|
|
|
@ -28,7 +31,7 @@ CargoPacket::CargoPacket(StationID source, uint16 count, SourceType source_type,
|
|
|
|
source(source),
|
|
|
|
source(source),
|
|
|
|
source_id(source_id)
|
|
|
|
source_id(source_id)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
this->source_type = source_type;
|
|
|
|
this->source_type = source_type;
|
|
|
|
|
|
|
|
|
|
|
|
if (source != INVALID_STATION) {
|
|
|
|
if (source != INVALID_STATION) {
|
|
|
|
assert(count != 0);
|
|
|
|
assert(count != 0);
|
|
|
@ -67,48 +70,48 @@ CargoPacket::CargoPacket(uint16 count, byte days_in_transit, Money feeder_share,
|
|
|
|
|
|
|
|
|
|
|
|
CargoList::~CargoList()
|
|
|
|
CargoList::~CargoList()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
while (!packets.empty()) {
|
|
|
|
while (!this->packets.empty()) {
|
|
|
|
delete packets.front();
|
|
|
|
delete this->packets.front();
|
|
|
|
packets.pop_front();
|
|
|
|
this->packets.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CargoList::AgeCargo()
|
|
|
|
void CargoList::AgeCargo()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (empty) return;
|
|
|
|
if (this->empty) return;
|
|
|
|
|
|
|
|
|
|
|
|
uint dit = 0;
|
|
|
|
uint dit = 0;
|
|
|
|
for (List::const_iterator it = packets.begin(); it != packets.end(); it++) {
|
|
|
|
for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
|
|
|
if ((*it)->days_in_transit != 0xFF) (*it)->days_in_transit++;
|
|
|
|
if ((*it)->days_in_transit != 0xFF) (*it)->days_in_transit++;
|
|
|
|
dit += (*it)->days_in_transit * (*it)->count;
|
|
|
|
dit += (*it)->days_in_transit * (*it)->count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
days_in_transit = dit / count;
|
|
|
|
this->days_in_transit = dit / count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CargoList::Append(CargoPacket *cp)
|
|
|
|
void CargoList::Append(CargoPacket *cp)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
assert(cp != NULL);
|
|
|
|
assert(cp != NULL);
|
|
|
|
|
|
|
|
|
|
|
|
for (List::iterator it = packets.begin(); it != packets.end(); it++) {
|
|
|
|
for (List::iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
|
|
|
if ((*it)->SameSource(cp) && (*it)->count + cp->count <= CargoPacket::MAX_COUNT) {
|
|
|
|
if ((*it)->SameSource(cp) && (*it)->count + cp->count <= CargoPacket::MAX_COUNT) {
|
|
|
|
(*it)->count += cp->count;
|
|
|
|
(*it)->count += cp->count;
|
|
|
|
(*it)->feeder_share += cp->feeder_share;
|
|
|
|
(*it)->feeder_share += cp->feeder_share;
|
|
|
|
delete cp;
|
|
|
|
delete cp;
|
|
|
|
|
|
|
|
|
|
|
|
InvalidateCache();
|
|
|
|
this->InvalidateCache();
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* The packet could not be merged with another one */
|
|
|
|
/* The packet could not be merged with another one */
|
|
|
|
packets.push_back(cp);
|
|
|
|
this->packets.push_back(cp);
|
|
|
|
InvalidateCache();
|
|
|
|
this->InvalidateCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CargoList::Truncate(uint count)
|
|
|
|
void CargoList::Truncate(uint count)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
for (List::iterator it = packets.begin(); it != packets.end(); it++) {
|
|
|
|
for (List::iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
|
|
|
uint local_count = (*it)->count;
|
|
|
|
uint local_count = (*it)->count;
|
|
|
|
if (local_count <= count) {
|
|
|
|
if (local_count <= count) {
|
|
|
|
count -= local_count;
|
|
|
|
count -= local_count;
|
|
|
@ -119,14 +122,14 @@ void CargoList::Truncate(uint count)
|
|
|
|
count = 0;
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (!packets.empty()) {
|
|
|
|
while (!this->packets.empty()) {
|
|
|
|
CargoPacket *cp = packets.back();
|
|
|
|
CargoPacket *cp = this->packets.back();
|
|
|
|
if (cp->count != 0) break;
|
|
|
|
if (cp->count != 0) break;
|
|
|
|
delete cp;
|
|
|
|
delete cp;
|
|
|
|
packets.pop_back();
|
|
|
|
this->packets.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InvalidateCache();
|
|
|
|
this->InvalidateCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CargoList::MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta, CargoPayment *payment, uint data)
|
|
|
|
bool CargoList::MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta, CargoPayment *payment, uint data)
|
|
|
@ -135,11 +138,11 @@ bool CargoList::MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta,
|
|
|
|
assert(mta == MTA_UNLOAD || mta == MTA_CARGO_LOAD || payment != NULL);
|
|
|
|
assert(mta == MTA_UNLOAD || mta == MTA_CARGO_LOAD || payment != NULL);
|
|
|
|
CargoList tmp;
|
|
|
|
CargoList tmp;
|
|
|
|
|
|
|
|
|
|
|
|
while (!packets.empty() && count > 0) {
|
|
|
|
while (!this->packets.empty() && count > 0) {
|
|
|
|
CargoPacket *cp = *packets.begin();
|
|
|
|
CargoPacket *cp = *packets.begin();
|
|
|
|
if (cp->count <= count) {
|
|
|
|
if (cp->count <= count) {
|
|
|
|
/* Can move the complete packet */
|
|
|
|
/* Can move the complete packet */
|
|
|
|
packets.remove(cp);
|
|
|
|
this->packets.remove(cp);
|
|
|
|
switch (mta) {
|
|
|
|
switch (mta) {
|
|
|
|
case MTA_FINAL_DELIVERY:
|
|
|
|
case MTA_FINAL_DELIVERY:
|
|
|
|
if (cp->source == data) {
|
|
|
|
if (cp->source == data) {
|
|
|
@ -195,7 +198,7 @@ bool CargoList::MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool remaining = !packets.empty();
|
|
|
|
bool remaining = !this->packets.empty();
|
|
|
|
|
|
|
|
|
|
|
|
if (mta == MTA_FINAL_DELIVERY && !tmp.Empty()) {
|
|
|
|
if (mta == MTA_FINAL_DELIVERY && !tmp.Empty()) {
|
|
|
|
/* There are some packets that could not be delivered at the station, put them back */
|
|
|
|
/* There are some packets that could not be delivered at the station, put them back */
|
|
|
@ -204,28 +207,27 @@ bool CargoList::MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (dest != NULL) dest->InvalidateCache();
|
|
|
|
if (dest != NULL) dest->InvalidateCache();
|
|
|
|
InvalidateCache();
|
|
|
|
this->InvalidateCache();
|
|
|
|
|
|
|
|
|
|
|
|
return remaining;
|
|
|
|
return remaining;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CargoList::InvalidateCache()
|
|
|
|
void CargoList::InvalidateCache()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
empty = packets.empty();
|
|
|
|
this->empty = this->packets.empty();
|
|
|
|
count = 0;
|
|
|
|
this->count = 0;
|
|
|
|
feeder_share = 0;
|
|
|
|
this->feeder_share = 0;
|
|
|
|
source = INVALID_STATION;
|
|
|
|
this->source = INVALID_STATION;
|
|
|
|
days_in_transit = 0;
|
|
|
|
this->days_in_transit = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (empty) return;
|
|
|
|
if (this->empty) return;
|
|
|
|
|
|
|
|
|
|
|
|
uint dit = 0;
|
|
|
|
uint dit = 0;
|
|
|
|
for (List::const_iterator it = packets.begin(); it != packets.end(); it++) {
|
|
|
|
for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
|
|
|
count += (*it)->count;
|
|
|
|
this->count += (*it)->count;
|
|
|
|
dit += (*it)->days_in_transit * (*it)->count;
|
|
|
|
dit += (*it)->days_in_transit * (*it)->count;
|
|
|
|
feeder_share += (*it)->feeder_share;
|
|
|
|
this->feeder_share += (*it)->feeder_share;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
days_in_transit = dit / count;
|
|
|
|
this->days_in_transit = dit / count;
|
|
|
|
source = (*packets.begin())->source;
|
|
|
|
this->source = (*packets.begin())->source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|