2007-06-22 11:58:59 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
/** @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
|
|
|
|
2009-10-06 19:52:27 +00:00
|
|
|
/**
|
|
|
|
* Initialize, i.e. clean, the pool with cargo packets.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2009-10-06 17:33:01 +00:00
|
|
|
CargoPacket::CargoPacket(StationID source, uint16 count, SourceType source_type, SourceID source_id) :
|
|
|
|
count(count),
|
2009-10-06 21:19:20 +00:00
|
|
|
source_id(source_id),
|
|
|
|
source(source)
|
2007-06-22 11:58:59 +00:00
|
|
|
{
|
2009-10-06 19:52:27 +00:00
|
|
|
this->source_type = source_type;
|
2009-10-06 17:33:01 +00:00
|
|
|
|
|
|
|
if (source != INVALID_STATION) {
|
|
|
|
assert(count != 0);
|
|
|
|
this->source_xy = Station::Get(source)->xy;
|
|
|
|
this->loaded_at_xy = this->source_xy;
|
|
|
|
}
|
2009-08-08 16:42:55 +00:00
|
|
|
}
|
|
|
|
|
2009-10-06 17:23:15 +00:00
|
|
|
CargoPacket::CargoPacket(uint16 count, byte days_in_transit, Money feeder_share, SourceType source_type, SourceID source_id) :
|
|
|
|
feeder_share(feeder_share),
|
|
|
|
count(count),
|
|
|
|
days_in_transit(days_in_transit),
|
|
|
|
source_id(source_id)
|
|
|
|
{
|
|
|
|
this->source_type = source_type;
|
|
|
|
}
|
|
|
|
|
2009-08-08 16:42:55 +00:00
|
|
|
/**
|
|
|
|
* Invalidates (sets source_id to INVALID_SOURCE) all cargo packets from given source
|
|
|
|
* @param src_type type of source
|
|
|
|
* @param src index of source
|
|
|
|
*/
|
|
|
|
/* static */ void CargoPacket::InvalidateAllFrom(SourceType src_type, SourceID src)
|
|
|
|
{
|
|
|
|
CargoPacket *cp;
|
|
|
|
FOR_ALL_CARGOPACKETS(cp) {
|
|
|
|
if (cp->source_type == src_type && cp->source_id == src) cp->source_id = INVALID_SOURCE;
|
|
|
|
}
|
2007-06-22 11:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Cargo list implementation
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
CargoList::~CargoList()
|
|
|
|
{
|
2009-10-06 19:52:27 +00:00
|
|
|
while (!this->packets.empty()) {
|
|
|
|
delete this->packets.front();
|
|
|
|
this->packets.pop_front();
|
2007-06-22 11:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-07 08:25:12 +00:00
|
|
|
void CargoList::RemoveFromCache(const CargoPacket *cp)
|
|
|
|
{
|
|
|
|
this->count -= cp->count;
|
|
|
|
this->feeder_share -= cp->feeder_share;
|
|
|
|
this->cargo_days_in_transit -= cp->days_in_transit * cp->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CargoList::AddToCache(const CargoPacket *cp)
|
|
|
|
{
|
|
|
|
this->count += cp->count;
|
|
|
|
this->feeder_share += cp->feeder_share;
|
|
|
|
this->cargo_days_in_transit += cp->days_in_transit * cp->count;
|
|
|
|
}
|
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
void CargoList::AgeCargo()
|
|
|
|
{
|
2009-10-06 19:52:27 +00:00
|
|
|
for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
2009-10-06 21:24:03 +00:00
|
|
|
/* If we're at the maximum, then we can't increase no more. */
|
|
|
|
if ((*it)->days_in_transit == 0xFF) continue;
|
|
|
|
|
|
|
|
(*it)->days_in_transit++;
|
|
|
|
this->cargo_days_in_transit += (*it)->count;
|
2007-06-22 11:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CargoList::Append(CargoPacket *cp)
|
|
|
|
{
|
|
|
|
assert(cp != NULL);
|
|
|
|
|
2009-10-06 19:52:27 +00:00
|
|
|
for (List::iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
2009-10-07 08:25:12 +00:00
|
|
|
CargoPacket *icp = *it;
|
|
|
|
if (icp->SameSource(cp) && icp->count + cp->count <= CargoPacket::MAX_COUNT) {
|
|
|
|
icp->count += cp->count;
|
|
|
|
icp->feeder_share += cp->feeder_share;
|
2007-06-22 11:58:59 +00:00
|
|
|
|
2009-10-07 08:25:12 +00:00
|
|
|
this->AddToCache(cp);
|
|
|
|
delete cp;
|
2007-06-22 11:58:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The packet could not be merged with another one */
|
2009-10-06 19:52:27 +00:00
|
|
|
this->packets.push_back(cp);
|
2009-10-07 08:25:12 +00:00
|
|
|
this->AddToCache(cp);
|
2007-06-22 11:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-07 08:25:12 +00:00
|
|
|
void CargoList::Truncate(uint max_remaining)
|
2007-06-22 11:58:59 +00:00
|
|
|
{
|
2009-10-07 08:25:12 +00:00
|
|
|
for (List::iterator it = packets.begin(); it != packets.end(); /* done during loop*/) {
|
|
|
|
CargoPacket *cp = *it;
|
|
|
|
if (max_remaining == 0) {
|
|
|
|
/* Nothing should remain, just remove the packets. */
|
|
|
|
packets.erase(it++);
|
|
|
|
this->RemoveFromCache(cp);
|
|
|
|
delete cp;
|
2007-06-22 11:58:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-10-07 08:25:12 +00:00
|
|
|
uint local_count = cp->count;
|
|
|
|
if (local_count > max_remaining) {
|
|
|
|
uint diff = local_count - max_remaining;
|
|
|
|
this->count -= diff;
|
|
|
|
this->cargo_days_in_transit -= cp->days_in_transit * diff;
|
|
|
|
cp->count = max_remaining;
|
|
|
|
max_remaining = 0;
|
|
|
|
} else {
|
|
|
|
max_remaining -= local_count;
|
|
|
|
}
|
|
|
|
++it;
|
2007-06-22 11:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2009-10-06 19:52:27 +00:00
|
|
|
while (!this->packets.empty() && count > 0) {
|
2007-06-22 11:58:59 +00:00
|
|
|
CargoPacket *cp = *packets.begin();
|
|
|
|
if (cp->count <= count) {
|
|
|
|
/* Can move the complete packet */
|
2009-10-06 19:52:27 +00:00
|
|
|
this->packets.remove(cp);
|
2007-06-22 11:58:59 +00:00
|
|
|
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:
|
2009-10-06 17:23:15 +00:00
|
|
|
cp->feeder_share += payment->PayTransfer(cp, cp->count);
|
2009-06-29 19:55:36 +00:00
|
|
|
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
|
|
|
|
2009-08-08 16:42:55 +00:00
|
|
|
cp_new->source_type = cp->source_type;
|
|
|
|
cp_new->source_id = cp->source_id;
|
|
|
|
|
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
|
|
|
|
2009-10-06 17:23:15 +00:00
|
|
|
if (mta == MTA_TRANSFER) cp_new->feeder_share += payment->PayTransfer(cp_new, count);
|
2009-06-29 19:55:36 +00:00
|
|
|
} else {
|
|
|
|
payment->PayFinalDelivery(cp, count);
|
2007-06-22 11:58:59 +00:00
|
|
|
}
|
|
|
|
cp->count -= count;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-06 19:52:27 +00:00
|
|
|
bool remaining = !this->packets.empty();
|
2007-06-22 11:58:59 +00:00
|
|
|
|
|
|
|
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();
|
2009-10-06 19:52:27 +00:00
|
|
|
this->InvalidateCache();
|
2007-06-22 11:58:59 +00:00
|
|
|
|
|
|
|
return remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CargoList::InvalidateCache()
|
|
|
|
{
|
2009-10-06 19:52:27 +00:00
|
|
|
this->count = 0;
|
|
|
|
this->feeder_share = 0;
|
2009-10-06 21:24:03 +00:00
|
|
|
this->cargo_days_in_transit = 0;
|
2007-06-22 11:58:59 +00:00
|
|
|
|
2009-10-06 19:52:27 +00:00
|
|
|
for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
2009-10-07 08:25:12 +00:00
|
|
|
this->AddToCache(*it);
|
2007-06-22 11:58:59 +00:00
|
|
|
}
|
|
|
|
}
|