(svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
parent
dc82eeb2ae
commit
fc201d4ad8
@ -0,0 +1,351 @@
|
||||
/* $Id$ */
|
||||
|
||||
/** @file cargopacket.cpp Implementation of the cargo packets */
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "openttd.h"
|
||||
#include "station.h"
|
||||
#include "cargopacket.h"
|
||||
#include "saveload.h"
|
||||
|
||||
/**
|
||||
* Called if a new block is added to the station-pool
|
||||
*/
|
||||
static void CargoPacketPoolNewBlock(uint cpart_item)
|
||||
{
|
||||
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
|
||||
* TODO - This is just a temporary stage, this will be removed. */
|
||||
for (CargoPacket *cp = GetCargoPacket(cpart_item); cp != NULL; cp = (cp->index + 1U < GetCargoPacketPoolSize()) ? GetCargoPacket(cp->index + 1U) : NULL) cp->index = cpart_item++;
|
||||
}
|
||||
|
||||
static void CargoPacketPoolCleanBlock(uint cpart_item, uint end_item)
|
||||
{
|
||||
for (uint i = cpart_item; i <= end_item; i++) {
|
||||
CargoPacket *cp = GetCargoPacket(i);
|
||||
if (cp->IsValid()) cp->~CargoPacket();
|
||||
}
|
||||
}
|
||||
|
||||
/* Initialize the cargopacket-pool */
|
||||
DEFINE_OLD_POOL(CargoPacket, CargoPacket, CargoPacketPoolNewBlock, CargoPacketPoolCleanBlock)
|
||||
|
||||
void InitializeCargoPackets()
|
||||
{
|
||||
/* Clean the cargo packet pool and create 1 block in it */
|
||||
CleanPool(&_CargoPacket_pool);
|
||||
AddBlockToPool(&_CargoPacket_pool);
|
||||
|
||||
/* Check whether our &cargolist == &cargolist.packets "hack" works */
|
||||
CargoList::AssertOnWrongPacketOffset();
|
||||
}
|
||||
|
||||
CargoPacket::CargoPacket(StationID source, uint16 count)
|
||||
{
|
||||
if (source != INVALID_STATION) assert(count != 0);
|
||||
|
||||
this->source = source;
|
||||
this->source_xy = (source != INVALID_STATION) ? GetStation(source)->xy : 0;
|
||||
this->loaded_at_xy = this->source_xy;
|
||||
|
||||
this->count = count;
|
||||
this->days_in_transit = 0;
|
||||
this->feeder_share = 0;
|
||||
this->paid_for = false;
|
||||
}
|
||||
|
||||
CargoPacket::~CargoPacket()
|
||||
{
|
||||
this->count = 0;
|
||||
}
|
||||
|
||||
bool CargoPacket::SameSource(CargoPacket *cp)
|
||||
{
|
||||
return this->source_xy == cp->source_xy && this->days_in_transit == cp->days_in_transit && this->paid_for == cp->paid_for;
|
||||
}
|
||||
|
||||
void *CargoPacket::operator new(size_t size)
|
||||
{
|
||||
CargoPacket *cp = AllocateRaw();
|
||||
return cp;
|
||||
}
|
||||
|
||||
void *CargoPacket::operator new(size_t size, CargoPacket::ID cp_idx)
|
||||
{
|
||||
if (!AddBlockIfNeeded(&_CargoPacket_pool, cp_idx))
|
||||
error("CargoPackets: failed loading savegame: too many cargo packets");
|
||||
|
||||
CargoPacket *cp = GetCargoPacket(cp_idx);
|
||||
return cp;
|
||||
}
|
||||
|
||||
void CargoPacket::operator delete(void *p)
|
||||
{
|
||||
}
|
||||
|
||||
void CargoPacket::operator delete(void *p, CargoPacket::ID cp_idx)
|
||||
{
|
||||
}
|
||||
|
||||
/*static*/ CargoPacket *CargoPacket::AllocateRaw()
|
||||
{
|
||||
CargoPacket *cp = NULL;
|
||||
|
||||
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
|
||||
* TODO - This is just a temporary stage, this will be removed. */
|
||||
for (cp = GetCargoPacket(0); cp != NULL; cp = (cp->index + 1U < GetCargoPacketPoolSize()) ? GetCargoPacket(cp->index + 1U) : NULL) {
|
||||
if (!cp->IsValid()) {
|
||||
CargoPacket::ID index = cp->index;
|
||||
|
||||
memset(cp, 0, sizeof(CargoPacket));
|
||||
cp->index = index;
|
||||
return cp;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check if we can add a block to the pool */
|
||||
if (AddBlockToPool(&_CargoPacket_pool)) return AllocateRaw();
|
||||
|
||||
error("CargoPackets: too many cargo packets");
|
||||
}
|
||||
|
||||
static const SaveLoad _cargopacket_desc[] = {
|
||||
SLE_VAR(CargoPacket, source, SLE_UINT16),
|
||||
SLE_VAR(CargoPacket, source_xy, SLE_UINT32),
|
||||
SLE_VAR(CargoPacket, loaded_at_xy, SLE_UINT32),
|
||||
SLE_VAR(CargoPacket, count, SLE_UINT16),
|
||||
SLE_VAR(CargoPacket, days_in_transit, SLE_UINT8),
|
||||
SLE_VAR(CargoPacket, feeder_share, SLE_INT64),
|
||||
SLE_VAR(CargoPacket, paid_for, SLE_BOOL),
|
||||
|
||||
SLE_END()
|
||||
};
|
||||
|
||||
static void Save_CAPA()
|
||||
{
|
||||
CargoPacket *cp;
|
||||
|
||||
FOR_ALL_CARGOPACKETS(cp) {
|
||||
SlSetArrayIndex(cp->index);
|
||||
SlObject(cp, _cargopacket_desc);
|
||||
}
|
||||
}
|
||||
|
||||
static void Load_CAPA()
|
||||
{
|
||||
int index;
|
||||
|
||||
while ((index = SlIterateArray()) != -1) {
|
||||
if (!AddBlockIfNeeded(&_CargoPacket_pool, index)) {
|
||||
error("CargoPackets: failed loading savegame: too many cargo packets");
|
||||
}
|
||||
|
||||
CargoPacket *cp = GetCargoPacket(index);
|
||||
SlObject(cp, _cargopacket_desc);
|
||||
}
|
||||
}
|
||||
|
||||
extern const ChunkHandler _cargopacket_chunk_handlers[] = {
|
||||
{ 'CAPA', Save_CAPA, Load_CAPA, CH_ARRAY | CH_LAST},
|
||||
};
|
||||
|
||||
/*
|
||||
*
|
||||
* Cargo list implementation
|
||||
*
|
||||
*/
|
||||
|
||||
/* static */ void CargoList::AssertOnWrongPacketOffset()
|
||||
{
|
||||
CargoList cl;
|
||||
if ((void*)&cl != (void*)cl.Packets()) NOT_REACHED();
|
||||
}
|
||||
|
||||
|
||||
CargoList::~CargoList()
|
||||
{
|
||||
while (!packets.empty()) {
|
||||
delete packets.front();
|
||||
packets.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
const CargoList::List *CargoList::Packets() const
|
||||
{
|
||||
return &packets;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool CargoList::Empty() const
|
||||
{
|
||||
return empty;
|
||||
}
|
||||
|
||||
uint CargoList::Count() const
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
bool CargoList::UnpaidCargo() const
|
||||
{
|
||||
return unpaid_cargo;
|
||||
}
|
||||
|
||||
Money CargoList::FeederShare() const
|
||||
{
|
||||
return feeder_share;
|
||||
}
|
||||
|
||||
StationID CargoList::Source() const
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
uint CargoList::DaysInTransit() const
|
||||
{
|
||||
return days_in_transit;
|
||||
}
|
||||
|
||||
void CargoList::Append(CargoPacket *cp)
|
||||
{
|
||||
assert(cp != NULL);
|
||||
assert(cp->IsValid());
|
||||
|
||||
for (List::iterator it = packets.begin(); it != packets.end(); it++) {
|
||||
if ((*it)->SameSource(cp)) {
|
||||
(*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();
|
||||
}
|
||||
|
||||
bool CargoList::MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta, uint data)
|
||||
{
|
||||
assert(mta == MTA_FINAL_DELIVERY || dest != NULL);
|
||||
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 {
|
||||
count -= cp->count;
|
||||
delete cp;
|
||||
}
|
||||
break;
|
||||
case MTA_CARGO_LOAD:
|
||||
cp->loaded_at_xy = data;
|
||||
/* When cargo is moved into another vehicle you have *always* paid for it */
|
||||
cp->paid_for = false;
|
||||
/* FALL THROUGH */
|
||||
case MTA_OTHER:
|
||||
count -= cp->count;
|
||||
dest->packets.push_back(cp);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
/* Can move only part of the packet, so split it into two pieces */
|
||||
if (mta != MTA_FINAL_DELIVERY) {
|
||||
CargoPacket *cp_new = new CargoPacket();
|
||||
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;
|
||||
cp_new->feeder_share = cp->feeder_share / count;
|
||||
/* When cargo is moved into another vehicle you have *always* paid for it */
|
||||
cp_new->paid_for = (mta == MTA_CARGO_LOAD) ? false : cp->paid_for;
|
||||
|
||||
cp_new->count = count;
|
||||
dest->packets.push_back(cp_new);
|
||||
|
||||
cp->feeder_share /= cp->count - count;
|
||||
}
|
||||
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 */
|
||||
tmp.MoveTo(this, MAX_UVALUE(uint));
|
||||
tmp.packets.clear();
|
||||
}
|
||||
|
||||
if (dest != NULL) dest->InvalidateCache();
|
||||
InvalidateCache();
|
||||
|
||||
return remaining;
|
||||
}
|
||||
|
||||
void CargoList::InvalidateCache()
|
||||
{
|
||||
empty = packets.empty();
|
||||
count = 0;
|
||||
unpaid_cargo = false;
|
||||
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;
|
||||
unpaid_cargo |= !(*it)->paid_for;
|
||||
dit += (*it)->days_in_transit * (*it)->count;
|
||||
feeder_share += (*it)->feeder_share;
|
||||
}
|
||||
days_in_transit = dit / count;
|
||||
source = (*packets.begin())->source;
|
||||
}
|
@ -0,0 +1,221 @@
|
||||
/* $Id$ */
|
||||
|
||||
/** @file cargotype.h */
|
||||
|
||||
#ifndef CARGOPACKET_H
|
||||
#define CARGOPACKET_H
|
||||
|
||||
#include <list>
|
||||
|
||||
/**
|
||||
* Container for cargo from the same location and time
|
||||
*/
|
||||
struct CargoPacket {
|
||||
bool touched;
|
||||
|
||||
typedef uint32 ID; ///< Type for cargopacket identifiers
|
||||
|
||||
ID index; ///< The unique index of this packet
|
||||
|
||||
StationID source; ///< The station where the cargo came from first
|
||||
TileIndex source_xy; ///< The origin of the cargo (first station in feeder chain)
|
||||
TileIndex loaded_at_xy; ///< Location where this cargo has been loaded into the vehicle
|
||||
|
||||
uint16 count; ///< The amount of cargo in this packet
|
||||
byte days_in_transit; ///< Amount of days this packet has been in transit
|
||||
Money feeder_share; ///< Value of feeder pickup to be paid for on delivery of cargo
|
||||
bool paid_for; ///< Have we been paid for this cargo packet?
|
||||
|
||||
/**
|
||||
* Creates a new cargo packet
|
||||
* @param source the source of the packet
|
||||
* @param count the number of cargo entities to put in this packet
|
||||
* @pre count != 0 || source == INVALID_STATION
|
||||
*/
|
||||
CargoPacket(StationID source = INVALID_STATION, uint16 count = 0);
|
||||
|
||||
/** Destroy the packet */
|
||||
~CargoPacket();
|
||||
|
||||
|
||||
/**
|
||||
* Is this a valid cargo packet ?
|
||||
* @return true if and only it is valid
|
||||
*/
|
||||
bool IsValid() const { return this->count != 0; }
|
||||
|
||||
/**
|
||||
* Checks whether the cargo packet is from (exactly) the same source
|
||||
* in time and location.
|
||||
* @param cp the cargo packet to compare to
|
||||
* @return true if and only if days_in_transit and source_xy are equal
|
||||
*/
|
||||
bool SameSource(CargoPacket *cp);
|
||||
|
||||
|
||||
/* normal new/delete operators. Used when building/removing station */
|
||||
void* operator new (size_t size);
|
||||
void operator delete(void *p);
|
||||
|
||||
/* new/delete operators accepting station index. Used when loading station from savegame. */
|
||||
void* operator new (size_t size, CargoPacket::ID cp_idx);
|
||||
void operator delete(void *p, CargoPacket::ID cp_idx);
|
||||
|
||||
private:
|
||||
/**
|
||||
* Allocate the raw memory for this cargo packet
|
||||
* @return the allocated memory
|
||||
*/
|
||||
static CargoPacket *AllocateRaw();
|
||||
};
|
||||
|
||||
/** We want to use a pool */
|
||||
DECLARE_OLD_POOL(CargoPacket, CargoPacket, 10, 1000)
|
||||
|
||||
/**
|
||||
* Iterate over all _valid_ cargo packets from the given start
|
||||
* @param cp the variable used as "iterator"
|
||||
* @param start the cargo packet ID of the first packet to iterate over
|
||||
*/
|
||||
#define FOR_ALL_CARGOPACKETS_FROM(cp, start) for (cp = GetCargoPacket(start); cp != NULL; cp = (cp->index + 1U < GetCargoPacketPoolSize()) ? GetCargoPacket(cp->index + 1U) : NULL) if (cp->IsValid())
|
||||
|
||||
/**
|
||||
* Iterate over all _valid_ cargo packets from the begin of the pool
|
||||
* @param cp the variable used as "iterator"
|
||||
*/
|
||||
#define FOR_ALL_CARGOPACKETS(cp) FOR_ALL_CARGOPACKETS_FROM(cp, 0)
|
||||
|
||||
/**
|
||||
* Simple collection class for a list of cargo packets
|
||||
*/
|
||||
class CargoList {
|
||||
public:
|
||||
/** List of cargo packets */
|
||||
typedef std::list<CargoPacket *> List;
|
||||
|
||||
/** 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_OTHER ///< "Just" move the packet to another cargo list
|
||||
};
|
||||
|
||||
private:
|
||||
List packets; ///< The cargo packets in this list
|
||||
|
||||
bool empty; ///< Cache for whether this list is empty or not
|
||||
uint count; ///< Cache for the number of cargo entities
|
||||
bool unpaid_cargo; ///< Cache for the unpaid cargo
|
||||
Money feeder_share; ///< Cache for the feeder share
|
||||
StationID source; ///< Cache for the source of the packet
|
||||
uint days_in_transit; ///< Cache for the number of days in transit
|
||||
|
||||
public:
|
||||
/**
|
||||
* Needed for an ugly hack:
|
||||
* - vehicles and stations need to store cargo lists, so they use CargoList as container
|
||||
* - this internals of the container should be protected, e.g. private (or protected) by C++
|
||||
* - for saving/loading we need to pass pointer to objects
|
||||
* -> so *if* the pointer to the cargo list is the same as the pointer to the packet list
|
||||
* encapsulated in the CargoList, we can just pass the CargoList as "offset".
|
||||
* Normally we would then just add the offset of the packets variable within the cargo list
|
||||
* but that is not possible because the variable is private. Furthermore we are not sure
|
||||
* that this works on all platforms, we need to check whether the offset is actually 0.
|
||||
* This cannot be done compile time, because the variable is private. So we need to write
|
||||
* a function that does actually check the offset runtime and call it somewhere where it
|
||||
* is always called but it should not be called often.
|
||||
*/
|
||||
static void AssertOnWrongPacketOffset();
|
||||
|
||||
/** Create the cargo list */
|
||||
CargoList() { this->InvalidateCache(); }
|
||||
/** And destroy it ("frees" all cargo packets) */
|
||||
~CargoList();
|
||||
|
||||
/**
|
||||
* Returns a pointer to the cargo packet list (so you can iterate over it etc).
|
||||
* @return pointer to the packet list
|
||||
*/
|
||||
const CargoList::List *Packets() const;
|
||||
|
||||
/**
|
||||
* Ages the all cargo in this list
|
||||
*/
|
||||
void AgeCargo();
|
||||
|
||||
/**
|
||||
* Checks whether this list is empty
|
||||
* @return true if and only if the list is empty
|
||||
*/
|
||||
bool Empty() const;
|
||||
|
||||
/**
|
||||
* Returns the number of cargo entities in this list
|
||||
* @return the before mentioned number
|
||||
*/
|
||||
uint Count() const;
|
||||
|
||||
/**
|
||||
* Is there some cargo that has not been paid for?
|
||||
* @return true if and only if there is such a cargo
|
||||
*/
|
||||
bool UnpaidCargo() const;
|
||||
|
||||
/**
|
||||
* Returns total sum of the feeder share for all packets
|
||||
* @return the before mentioned number
|
||||
*/
|
||||
Money FeederShare() const;
|
||||
|
||||
/**
|
||||
* Returns source of the first cargo packet in this list
|
||||
* @return the before mentioned source
|
||||
*/
|
||||
StationID Source() const;
|
||||
|
||||
/**
|
||||
* Returns average number of days in transit for a cargo entity
|
||||
* @return the before mentioned number
|
||||
*/
|
||||
uint DaysInTransit() const;
|
||||
|
||||
|
||||
/**
|
||||
* Appends the given cargo packet
|
||||
* @warning After appending this packet may not exist anymore!
|
||||
* @note Do not use the cargo packet anymore after it has been appended to this CargoList!
|
||||
* @param cp the cargo packet to add
|
||||
* @pre cp != NULL
|
||||
*/
|
||||
void Append(CargoPacket *cp);
|
||||
|
||||
/**
|
||||
* Truncates the cargo in this list to the given amount. It leaves the
|
||||
* first count cargo entities and removes the rest.
|
||||
* @param count the maximum amount of entities to be in the list after the command
|
||||
*/
|
||||
void Truncate(uint count);
|
||||
|
||||
/**
|
||||
* Moves the given amount of cargo to another list.
|
||||
* Depending on the value of mta the side effects of this function differ:
|
||||
* - MTA_FINAL_DELIVERY: destroys the packets that do not originate from a specific station
|
||||
* - MTA_CARGO_LOAD: sets the loaded_at_xy value of the moved packets
|
||||
* - MTA_OTHER: just move without side effects
|
||||
* @param dest the destination to move the cargo to
|
||||
* @param count the amount of cargo entities to move
|
||||
* @param mta how to handle the moving (side effects)
|
||||
* @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_OTHER - unused
|
||||
* @param mta == MTA_FINAL_DELIVERY || dest != NULL
|
||||
* @return true if there are still packets that might be moved from this cargo list
|
||||
*/
|
||||
bool MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta = MTA_OTHER, uint data = 0);
|
||||
|
||||
/** Invalidates the cached data and rebuild it */
|
||||
void InvalidateCache();
|
||||
};
|
||||
|
||||
#endif /* CARGOPACKET_H */
|
Loading…
Reference in New Issue