Changeset - r13294:1c7a8cd8e77d
[Not reviewed]
master
0 4 0
rubidium - 15 years ago 2009-10-19 09:15:47
rubidium@openttd.org
(svn r17813) -Codechange: unify the CargoPacket related coding style
4 files changed with 23 insertions and 19 deletions:
0 comments (0 inline, 0 general)
src/cargopacket.cpp
Show inline comments
 
@@ -64,88 +64,87 @@ CargoPacket::CargoPacket(uint16 count, b
 
		if (cp->source_type == src_type && cp->source_id == src) cp->source_id = INVALID_SOURCE;
 
	}
 
}
 

	
 
/**
 
 * Invalidates (sets source to INVALID_STATION) all cargo packets from given station
 
 * @param sid the station that gets removed
 
 */
 
/* static */ void CargoPacket::InvalidateAllFrom(StationID sid)
 
{
 
	CargoPacket *cp;
 
	FOR_ALL_CARGOPACKETS(cp) {
 
		if (cp->source == sid) cp->source_id = INVALID_SOURCE;
 
		if (cp->source == sid) cp->source = INVALID_STATION;
 
	}
 
}
 

	
 
/*
 
 *
 
 * Cargo list implementation
 
 *
 
 */
 

	
 
template <class Tinst>
 
CargoList<Tinst>::~CargoList()
 
{
 
	while (!this->packets.empty()) {
 
		delete this->packets.front();
 
		this->packets.pop_front();
 
	for (Iterator it(this->packets.begin()); it != this->packets.end(); ++it) {
 
		delete *it;
 
	}
 
}
 

	
 
template <class Tinst>
 
void CargoList<Tinst>::RemoveFromCache(const CargoPacket *cp)
 
{
 
	this->count                 -= cp->count;
 
	this->cargo_days_in_transit -= cp->days_in_transit * cp->count;
 
}
 

	
 
template <class Tinst>
 
void CargoList<Tinst>::AddToCache(const CargoPacket *cp)
 
{
 
	this->count                 += cp->count;
 
	this->cargo_days_in_transit += cp->days_in_transit * cp->count;
 
}
 

	
 
template <class Tinst>
 
void CargoList<Tinst>::Append(CargoPacket *cp)
 
{
 
	assert(cp != NULL);
 

	
 
	for (List::iterator it = this->packets.begin(); it != this->packets.end(); it++) {
 
	for (Iterator it(this->packets.begin()); it != this->packets.end(); it++) {
 
		CargoPacket *icp = *it;
 
		if (Tinst::AreMergable(icp, cp) && icp->count + cp->count <= CargoPacket::MAX_COUNT) {
 
			icp->count        += cp->count;
 
			icp->feeder_share += cp->feeder_share;
 

	
 
			static_cast<Tinst *>(this)->AddToCache(cp);
 
			delete cp;
 
			return;
 
		}
 
	}
 

	
 
	/* The packet could not be merged with another one */
 
	this->packets.push_back(cp);
 
	static_cast<Tinst *>(this)->AddToCache(cp);
 
}
 

	
 

	
 
template <class Tinst>
 
void CargoList<Tinst>::Truncate(uint max_remaining)
 
{
 
	for (List::iterator it = packets.begin(); it != packets.end(); /* done during loop*/) {
 
	for (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->packets.erase(it++);
 
			static_cast<Tinst *>(this)->RemoveFromCache(cp);
 
			delete cp;
 
			continue;
 
		}
 

	
 
		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;
 
@@ -154,26 +153,26 @@ void CargoList<Tinst>::Truncate(uint max
 
		}
 
		++it;
 
	}
 
}
 

	
 
template <class Tinst>
 
template <class Tother_inst>
 
bool CargoList<Tinst>::MoveTo(Tother_inst *dest, uint max_move, MoveToAction mta, CargoPayment *payment, uint data)
 
{
 
	assert(mta == MTA_FINAL_DELIVERY || dest != NULL);
 
	assert(mta == MTA_UNLOAD || mta == MTA_CARGO_LOAD || payment != NULL);
 

	
 
	List::iterator it = packets.begin();
 
	while (it != packets.end() && max_move > 0) {
 
	Iterator it(this->packets.begin());
 
	while (it != this->packets.end() && max_move > 0) {
 
		CargoPacket *cp = *it;
 
		if (cp->source == data && mta == MTA_FINAL_DELIVERY) {
 
			/* Skip cargo that originated from this station. */
 
			++it;
 
			continue;
 
		}
 

	
 
		if (cp->count <= max_move) {
 
			/* Can move the complete packet */
 
			max_move -= cp->count;
 
			this->packets.erase(it++);
 
			static_cast<Tinst *>(this)->RemoveFromCache(cp);
 
@@ -232,50 +231,51 @@ bool CargoList<Tinst>::MoveTo(Tother_ins
 
		max_move = 0;
 
	}
 

	
 
	return it != packets.end();
 
}
 

	
 
template <class Tinst>
 
void CargoList<Tinst>::InvalidateCache()
 
{
 
	this->count = 0;
 
	this->cargo_days_in_transit = 0;
 

	
 
	for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
 
	for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) {
 
		static_cast<Tinst *>(this)->AddToCache(*it);
 
	}
 
}
 

	
 

	
 
void VehicleCargoList::RemoveFromCache(const CargoPacket *cp)
 
{
 
	this->feeder_share -= cp->feeder_share;
 
	this->Parent::RemoveFromCache(cp);
 
}
 

	
 
void VehicleCargoList::AddToCache(const CargoPacket *cp)
 
{
 
	this->feeder_share += cp->feeder_share;
 
	this->Parent::AddToCache(cp);
 
}
 

	
 
void VehicleCargoList::AgeCargo()
 
{
 
	for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
 
	for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) {
 
		CargoPacket *cp = *it;
 
		/* If we're at the maximum, then we can't increase no more. */
 
		if ((*it)->days_in_transit == 0xFF) continue;
 
		if (cp->days_in_transit == 0xFF) continue;
 

	
 
		(*it)->days_in_transit++;
 
		this->cargo_days_in_transit += (*it)->count;
 
		cp->days_in_transit++;
 
		this->cargo_days_in_transit += cp->count;
 
	}
 
}
 

	
 
void VehicleCargoList::InvalidateCache()
 
{
 
	this->feeder_share = 0;
 
	this->Parent::InvalidateCache();
 
}
 

	
 
/*
 
 * We have to instantiate everything we want to be usable.
 
 */
src/cargopacket.h
Show inline comments
 
@@ -176,26 +176,30 @@ public:
 
 * Iterate over all _valid_ cargo packets from the begin of the pool
 
 * @param var   the variable used as "iterator"
 
 */
 
#define FOR_ALL_CARGOPACKETS(var) FOR_ALL_CARGOPACKETS_FROM(var, 0)
 

	
 
/**
 
 * Simple collection class for a list of cargo packets
 
 * @tparam Tinst The actual instantation of this cargo list
 
 */
 
template <class Tinst>
 
class CargoList {
 
public:
 
	/** List of cargo packets */
 
	/** Container with cargo packets */
 
	typedef std::list<CargoPacket *> List;
 
	/** The iterator for our container */
 
	typedef List::iterator Iterator;
 
	/** The const iterator for our container */
 
	typedef List::const_iterator ConstIterator;
 

	
 
	/** Kind of actions that could be done with packets on move */
 
	enum MoveToAction {
 
		MTA_FINAL_DELIVERY, ///< "Deliver" the packet to the final destination, i.e. destroy the packet
 
		MTA_CARGO_LOAD,     ///< Load the packet onto a vehicle, i.e. set the last loaded station ID
 
		MTA_TRANSFER,       ///< The cargo is moved as part of a transfer
 
		MTA_UNLOAD,         ///< The cargo is moved as part of a forced unload
 
	};
 

	
 
protected:
 
	uint count;                 ///< Cache for the number of cargo entities
 
	uint cargo_days_in_transit; ///< Cache for the sum of number of days in transit of each entity; comparable to man-hours
src/saveload/cargopacket_sl.cpp
Show inline comments
 
@@ -19,44 +19,44 @@
 
/* static */ void CargoPacket::AfterLoad()
 
{
 
	if (CheckSavegameVersion(44)) {
 
		Vehicle *v;
 
		/* If we remove a station while cargo from it is still enroute, payment calculation will assume
 
			* 0, 0 to be the source of the cargo, resulting in very high payments usually. v->source_xy
 
			* stores the coordinates, preserving them even if the station is removed. However, if a game is loaded
 
			* where this situation exists, the cargo-source information is lost. in this case, we set the source
 
			* to the current tile of the vehicle to prevent excessive profits
 
			*/
 
		FOR_ALL_VEHICLES(v) {
 
			const VehicleCargoList::List *packets = v->cargo.Packets();
 
			for (VehicleCargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
 
			for (VehicleCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
 
				CargoPacket *cp = *it;
 
				cp->source_xy = Station::IsValidID(cp->source) ? Station::Get(cp->source)->xy : v->tile;
 
				cp->loaded_at_xy = cp->source_xy;
 
			}
 
			v->cargo.InvalidateCache();
 
		}
 

	
 
		/* Store position of the station where the goods come from, so there
 
			* are no very high payments when stations get removed. However, if the
 
			* station where the goods came from is already removed, the source
 
			* information is lost. In that case we set it to the position of this
 
			* station */
 
		Station *st;
 
		FOR_ALL_STATIONS(st) {
 
			for (CargoID c = 0; c < NUM_CARGO; c++) {
 
				GoodsEntry *ge = &st->goods[c];
 

	
 
				const StationCargoList::List *packets = ge->cargo.Packets();
 
				for (StationCargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
 
				for (StationCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
 
					CargoPacket *cp = *it;
 
					cp->source_xy = Station::IsValidID(cp->source) ? Station::Get(cp->source)->xy : st->xy;
 
					cp->loaded_at_xy = cp->source_xy;
 
				}
 
			}
 
		}
 
	}
 

	
 
	if (CheckSavegameVersion(120)) {
 
		/* CargoPacket's source should be either INVALID_STATION or a valid station */
 
		CargoPacket *cp;
 
		FOR_ALL_CARGOPACKETS(cp) {
src/station_gui.cpp
Show inline comments
 
@@ -816,37 +816,37 @@ struct StationViewWindow : public Window
 
		for (CargoID i = 0; i < NUM_CARGO; i++) {
 
			if (st->goods[i].cargo.Empty()) {
 
				this->cargo_rows[i] = 0;
 
			} else {
 
				/* Add an entry for total amount of cargo of this type waiting. */
 
				cargolist.push_back(CargoData(i, INVALID_STATION, st->goods[i].cargo.Count()));
 

	
 
				/* Set the row for this cargo entry for the expand/hide button */
 
				this->cargo_rows[i] = (uint16)cargolist.size();
 

	
 
				/* Add an entry for each distinct cargo source. */
 
				const StationCargoList::List *packets = st->goods[i].cargo.Packets();
 
				for (StationCargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
 
				for (StationCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
 
					const CargoPacket *cp = *it;
 
					if (cp->SourceStation() != station_id) {
 
						bool added = false;
 

	
 
						/* Enable the expand/hide button for this cargo type */
 
						SetBit(transfers, i);
 

	
 
						/* Don't add cargo lines if not expanded */
 
						if (!HasBit(this->cargo, i)) break;
 

	
 
						/* Check if we already have this source in the list */
 
						for (CargoDataList::iterator jt = cargolist.begin(); jt != cargolist.end(); jt++) {
 
						for (CargoDataList::iterator jt(cargolist.begin()); jt != cargolist.end(); jt++) {
 
							CargoData *cd = &(*jt);
 
							if (cd->cargo == i && cd->source == cp->SourceStation()) {
 
								cd->count += cp->Count();
 
								added = true;
 
								break;
 
							}
 
						}
 

	
 
						if (!added) cargolist.push_back(CargoData(i, cp->SourceStation(), cp->Count()));
 
					}
 
				}
 
			}
0 comments (0 inline, 0 general)