2007-06-22 11:58:59 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/** @file cargopacket.cpp Implementation of the cargo packets */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2009-05-22 15:13:50 +00:00
|
|
|
#include "core/pool_func.hpp"
|
2009-06-29 19:55:36 +00:00
|
|
|
#include "economy_base.h"
|
2009-07-22 11:35:35 +00:00
|
|
|
#include "station_base.h"
|
2007-06-22 11:58:59 +00:00
|
|
|
|
|
|
|
/* Initialize the cargopacket-pool */
|
2009-05-22 15:13:50 +00:00
|
|
|
CargoPacketPool _cargopacket_pool("CargoPacket");
|
|
|
|
INSTANTIATE_POOL_METHODS(CargoPacket)
|
2007-06-22 11:58:59 +00:00
|
|
|
|
|
|
|
void InitializeCargoPackets()
|
|
|
|
{
|
2009-05-22 15:13:50 +00:00
|
|
|
_cargopacket_pool.CleanPool();
|
2007-06-22 11:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CargoPacket::CargoPacket(StationID source, uint16 count)
|
|
|
|
{
|
|
|
|
if (source != INVALID_STATION) assert(count != 0);
|
|
|
|
|
|
|
|
this->source = source;
|
2009-05-16 23:34:14 +00:00
|
|
|
this->source_xy = (source != INVALID_STATION) ? Station::Get(source)->xy : 0;
|
2007-06-22 11:58:59 +00:00
|
|
|
this->loaded_at_xy = this->source_xy;
|
|
|
|
|
|
|
|
this->count = count;
|
|
|
|
this->days_in_transit = 0;
|
|
|
|
this->feeder_share = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Cargo list implementation
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
CargoList::~CargoList()
|
|
|
|
{
|
|
|
|
while (!packets.empty()) {
|
|
|
|
delete packets.front();
|
|
|
|
packets.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CargoList::AgeCargo()
|
|
|
|
{
|
|
|
|
if (empty) return;
|
|
|
|
|
|
|
|
uint dit = 0;
|
|
|
|
for (List::const_iterator it = packets.begin(); it != packets.end(); it++) {
|
|
|
|
if ((*it)->days_in_transit != 0xFF) (*it)->days_in_transit++;
|
|
|
|
dit += (*it)->days_in_transit * (*it)->count;
|
|
|
|
}
|
|
|
|
days_in_transit = dit / count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CargoList::Append(CargoPacket *cp)
|
|
|
|
{
|
|
|
|
assert(cp != NULL);
|
|
|
|
|
|
|
|
for (List::iterator it = packets.begin(); it != packets.end(); it++) {
|
2007-07-01 12:25:08 +00:00
|
|
|
if ((*it)->SameSource(cp) && (*it)->count + cp->count <= 65535) {
|
2007-06-22 11:58:59 +00:00
|
|
|
(*it)->count += cp->count;
|
|
|
|
(*it)->feeder_share += cp->feeder_share;
|
|
|
|
delete cp;
|
|
|
|
|
|
|
|
InvalidateCache();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The packet could not be merged with another one */
|
|
|
|
packets.push_back(cp);
|
|
|
|
InvalidateCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CargoList::Truncate(uint count)
|
|
|
|
{
|
|
|
|
for (List::iterator it = packets.begin(); it != packets.end(); it++) {
|
|
|
|
uint local_count = (*it)->count;
|
|
|
|
if (local_count <= count) {
|
|
|
|
count -= local_count;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*it)->count = count;
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!packets.empty()) {
|
|
|
|
CargoPacket *cp = packets.back();
|
|
|
|
if (cp->count != 0) break;
|
|
|
|
delete cp;
|
|
|
|
packets.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
InvalidateCache();
|
|
|
|
}
|
|
|
|
|
2009-06-29 19:55:36 +00:00
|
|
|
bool CargoList::MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta, CargoPayment *payment, uint data)
|
2007-06-22 11:58:59 +00:00
|
|
|
{
|
|
|
|
assert(mta == MTA_FINAL_DELIVERY || dest != NULL);
|
2009-06-29 19:55:36 +00:00
|
|
|
assert(mta == MTA_UNLOAD || mta == MTA_CARGO_LOAD || payment != NULL);
|
2007-06-22 11:58:59 +00:00
|
|
|
CargoList tmp;
|
|
|
|
|
|
|
|
while (!packets.empty() && count > 0) {
|
|
|
|
CargoPacket *cp = *packets.begin();
|
|
|
|
if (cp->count <= count) {
|
|
|
|
/* Can move the complete packet */
|
|
|
|
packets.remove(cp);
|
|
|
|
switch (mta) {
|
|
|
|
case MTA_FINAL_DELIVERY:
|
|
|
|
if (cp->source == data) {
|
|
|
|
tmp.Append(cp);
|
|
|
|
} else {
|
2009-06-29 19:55:36 +00:00
|
|
|
payment->PayFinalDelivery(cp, cp->count);
|
2007-06-22 11:58:59 +00:00
|
|
|
count -= cp->count;
|
|
|
|
delete cp;
|
|
|
|
}
|
2009-06-29 19:55:36 +00:00
|
|
|
continue; // of the loop
|
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
case MTA_CARGO_LOAD:
|
|
|
|
cp->loaded_at_xy = data;
|
2009-06-29 19:55:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MTA_TRANSFER:
|
|
|
|
payment->PayTransfer(cp, cp->count);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MTA_UNLOAD:
|
2007-06-22 11:58:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-06-29 19:55:36 +00:00
|
|
|
count -= cp->count;
|
|
|
|
dest->packets.push_back(cp);
|
2007-06-22 11:58:59 +00:00
|
|
|
} else {
|
|
|
|
/* Can move only part of the packet, so split it into two pieces */
|
|
|
|
if (mta != MTA_FINAL_DELIVERY) {
|
|
|
|
CargoPacket *cp_new = new CargoPacket();
|
2008-09-03 10:30:07 +00:00
|
|
|
|
2008-09-03 13:25:56 +00:00
|
|
|
Money fs = cp->feeder_share * count / static_cast<uint>(cp->count);
|
2008-09-03 10:30:07 +00:00
|
|
|
cp->feeder_share -= fs;
|
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
cp_new->source = cp->source;
|
|
|
|
cp_new->source_xy = cp->source_xy;
|
|
|
|
cp_new->loaded_at_xy = (mta == MTA_CARGO_LOAD) ? data : cp->loaded_at_xy;
|
|
|
|
|
|
|
|
cp_new->days_in_transit = cp->days_in_transit;
|
2008-09-03 10:30:07 +00:00
|
|
|
cp_new->feeder_share = fs;
|
2007-06-22 11:58:59 +00:00
|
|
|
|
|
|
|
cp_new->count = count;
|
|
|
|
dest->packets.push_back(cp_new);
|
2009-06-29 19:55:36 +00:00
|
|
|
|
|
|
|
if (mta == MTA_TRANSFER) payment->PayTransfer(cp_new, count);
|
|
|
|
} else {
|
|
|
|
payment->PayFinalDelivery(cp, count);
|
2007-06-22 11:58:59 +00:00
|
|
|
}
|
|
|
|
cp->count -= count;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool remaining = !packets.empty();
|
|
|
|
|
|
|
|
if (mta == MTA_FINAL_DELIVERY && !tmp.Empty()) {
|
|
|
|
/* There are some packets that could not be delivered at the station, put them back */
|
2009-06-29 19:55:36 +00:00
|
|
|
tmp.MoveTo(this, UINT_MAX, MTA_UNLOAD, NULL);
|
2007-06-22 11:58:59 +00:00
|
|
|
tmp.packets.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest != NULL) dest->InvalidateCache();
|
|
|
|
InvalidateCache();
|
|
|
|
|
|
|
|
return remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CargoList::InvalidateCache()
|
|
|
|
{
|
|
|
|
empty = packets.empty();
|
|
|
|
count = 0;
|
|
|
|
feeder_share = 0;
|
|
|
|
source = INVALID_STATION;
|
|
|
|
days_in_transit = 0;
|
|
|
|
|
|
|
|
if (empty) return;
|
|
|
|
|
|
|
|
uint dit = 0;
|
|
|
|
for (List::const_iterator it = packets.begin(); it != packets.end(); it++) {
|
|
|
|
count += (*it)->count;
|
|
|
|
dit += (*it)->days_in_transit * (*it)->count;
|
|
|
|
feeder_share += (*it)->feeder_share;
|
|
|
|
}
|
|
|
|
days_in_transit = dit / count;
|
|
|
|
source = (*packets.begin())->source;
|
|
|
|
}
|
2008-05-04 10:05:35 +00:00
|
|
|
|