diff options
author | fonsinchen <fonsinchen@openttd.org> | 2013-06-09 13:04:32 +0000 |
---|---|---|
committer | fonsinchen <fonsinchen@openttd.org> | 2013-06-09 13:04:32 +0000 |
commit | 741c431caa59670bbe53740cc8a864992ce1bbd2 (patch) | |
tree | 42e4ab0bb88bb7afa969a268dc03dbe4b611a41b /src/cargopacket.cpp | |
parent | 04e3eb6fabc0e4aff04c189368356b8af15e9655 (diff) | |
download | openttd-741c431caa59670bbe53740cc8a864992ce1bbd2.tar.xz |
(svn r25362) -Feature: consider cargo waiting at other stations for rating at the origin station
Diffstat (limited to 'src/cargopacket.cpp')
-rw-r--r-- | src/cargopacket.cpp | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/src/cargopacket.cpp b/src/cargopacket.cpp index 16a5e61b1..2a4c96e82 100644 --- a/src/cargopacket.cpp +++ b/src/cargopacket.cpp @@ -659,33 +659,53 @@ uint StationCargoList::ShiftCargo(Taction action, StationID next, bool include_i /** * Truncates where each destination loses roughly the same percentage of its * cargo. This is done by randomizing the selection of packets to be removed. + * Optionally count the cargo by origin station. * @param max_move Maximum amount of cargo to remove. + * @param cargo_per_source Container for counting the cargo by origin. * @return Amount of cargo actually moved. */ -uint StationCargoList::Truncate(uint max_move) +uint StationCargoList::Truncate(uint max_move, StationCargoAmountMap *cargo_per_source) { max_move = min(max_move, this->count); uint prev_count = this->count; uint moved = 0; + uint loop = 0; + bool do_count = cargo_per_source != NULL; while (max_move > moved) { for (Iterator it(this->packets.begin()); it != this->packets.end();) { + CargoPacket *cp = *it; if (prev_count > max_move && RandomRange(prev_count) < prev_count - max_move) { + if (do_count && loop == 0) { + (*cargo_per_source)[cp->source] += cp->count; + } ++it; continue; } - CargoPacket *cp = *it; uint diff = max_move - moved; if (cp->count > diff) { - this->RemoveFromCache(cp, diff); - cp->Reduce(diff); - return moved + diff; + if (diff > 0) { + this->RemoveFromCache(cp, diff); + cp->Reduce(diff); + moved += diff; + } + if (loop > 0) { + if (do_count) (*cargo_per_source)[cp->source] -= diff; + return moved; + } else { + if (do_count) (*cargo_per_source)[cp->source] += cp->count; + ++it; + } } else { it = this->packets.erase(it); + if (do_count && loop > 0) { + (*cargo_per_source)[cp->source] -= cp->count; + } moved += cp->count; this->RemoveFromCache(cp, cp->count); delete cp; } } + loop++; } return moved; } |