Files
@ r12269:f94731e7dc53
Branch filter:
Location: cpp/openttd-patchpack/source/src/cargopacket.cpp
r12269:f94731e7dc53
4.4 KiB
text/x-c
(svn r16694) -Fix [FS#2995] (rgradual loading, rnewindustries): only pay for whatever has been actually unloaded and perform the payment when unloading has finished. This fixes, amongst others:
* cheating by starting to unload and after getting paid rushing to the depot to get sold (or unloading, loading and getting paid again for the remainder)
* cargo being dropped onto a station at the moment a stockpiling industry doesn't accept it anymore
* industries getting cargo that has not been unloaded yet and subsequently dumping it back on the station in one go
Note: you will now get paid after the unloading has finished, so you'll have to wait a bit longer for 'your' money.
* cheating by starting to unload and after getting paid rushing to the depot to get sold (or unloading, loading and getting paid again for the remainder)
* cargo being dropped onto a station at the moment a stockpiling industry doesn't accept it anymore
* industries getting cargo that has not been unloaded yet and subsequently dumping it back on the station in one go
Note: you will now get paid after the unloading has finished, so you'll have to wait a bit longer for 'your' money.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | /* $Id$ */
/** @file cargopacket.cpp Implementation of the cargo packets */
#include "stdafx.h"
#include "station_base.h"
#include "core/pool_func.hpp"
#include "economy_base.h"
/* Initialize the cargopacket-pool */
CargoPacketPool _cargopacket_pool("CargoPacket");
INSTANTIATE_POOL_METHODS(CargoPacket)
void InitializeCargoPackets()
{
_cargopacket_pool.CleanPool();
}
CargoPacket::CargoPacket(StationID source, uint16 count)
{
if (source != INVALID_STATION) assert(count != 0);
this->source = source;
this->source_xy = (source != INVALID_STATION) ? Station::Get(source)->xy : 0;
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++) {
if ((*it)->SameSource(cp) && (*it)->count + cp->count <= 65535) {
(*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, CargoPayment *payment, uint data)
{
assert(mta == MTA_FINAL_DELIVERY || dest != NULL);
assert(mta == MTA_UNLOAD || mta == MTA_CARGO_LOAD || payment != 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 {
payment->PayFinalDelivery(cp, cp->count);
count -= cp->count;
delete cp;
}
continue; // of the loop
case MTA_CARGO_LOAD:
cp->loaded_at_xy = data;
break;
case MTA_TRANSFER:
payment->PayTransfer(cp, cp->count);
break;
case MTA_UNLOAD:
break;
}
count -= cp->count;
dest->packets.push_back(cp);
} else {
/* Can move only part of the packet, so split it into two pieces */
if (mta != MTA_FINAL_DELIVERY) {
CargoPacket *cp_new = new CargoPacket();
Money fs = cp->feeder_share * count / static_cast<uint>(cp->count);
cp->feeder_share -= fs;
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 = fs;
cp_new->count = count;
dest->packets.push_back(cp_new);
if (mta == MTA_TRANSFER) payment->PayTransfer(cp_new, count);
} else {
payment->PayFinalDelivery(cp, 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, UINT_MAX, MTA_UNLOAD, NULL);
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;
}
|