Files
@ r13562:50c5cd7b6316
Branch filter:
Location: cpp/openttd-patchpack/source/src/cargopacket.cpp
r13562:50c5cd7b6316
8.3 KiB
text/x-c
(svn r18094) -Codechange: make the network content GUI work better with RTL
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | /* $Id$ */
/*
* 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/>.
*/
/** @file cargopacket.cpp Implementation of the cargo packets */
#include "stdafx.h"
#include "core/pool_func.hpp"
#include "economy_base.h"
#include "station_base.h"
/* Initialize the cargopacket-pool */
CargoPacketPool _cargopacket_pool("CargoPacket");
INSTANTIATE_POOL_METHODS(CargoPacket)
/**
* Initialize, i.e. clean, the pool with cargo packets.
*/
void InitializeCargoPackets()
{
_cargopacket_pool.CleanPool();
}
CargoPacket::CargoPacket()
{
this->source_type = ST_INDUSTRY;
this->source_id = INVALID_SOURCE;
}
/* NOTE: We have to zero memory ourselves here because we are using a 'new'
* that, in contrary to all other pools, does not memset to 0. */
CargoPacket::CargoPacket(StationID source, TileIndex source_xy, uint16 count, SourceType source_type, SourceID source_id) :
feeder_share(0),
count(count),
days_in_transit(0),
source_id(source_id),
source(source),
source_xy(source_xy),
loaded_at_xy(0)
{
assert(count != 0);
this->source_type = source_type;
}
CargoPacket::CargoPacket(uint16 count, byte days_in_transit, StationID source, TileIndex source_xy, TileIndex loaded_at_xy, 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),
source(source),
source_xy(source_xy),
loaded_at_xy(loaded_at_xy)
{
assert(count != 0);
this->source_type = source_type;
}
/**
* 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;
}
}
/**
* 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 = INVALID_STATION;
}
}
/*
*
* Cargo list implementation
*
*/
template <class Tinst>
CargoList<Tinst>::~CargoList()
{
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);
static_cast<Tinst *>(this)->AddToCache(cp);
for (List::reverse_iterator it(this->packets.rbegin()); it != this->packets.rend(); 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;
delete cp;
return;
}
}
/* The packet could not be merged with another one */
this->packets.push_back(cp);
}
template <class Tinst>
void CargoList<Tinst>::Truncate(uint max_remaining)
{
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. */
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;
} else {
max_remaining -= local_count;
}
++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);
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);
switch(mta) {
case MTA_FINAL_DELIVERY:
payment->PayFinalDelivery(cp, cp->count);
delete cp;
continue; // of the loop
case MTA_CARGO_LOAD:
cp->loaded_at_xy = data;
break;
case MTA_TRANSFER:
cp->feeder_share += payment->PayTransfer(cp, cp->count);
break;
case MTA_UNLOAD:
break;
}
dest->Append(cp);
continue;
}
/* Can move only part of the packet */
if (mta == MTA_FINAL_DELIVERY) {
/* Final delivery doesn't need package splitting. */
payment->PayFinalDelivery(cp, max_move);
/* Remove the delivered data from the cache */
uint left = cp->count - max_move;
cp->count = max_move;
static_cast<Tinst *>(this)->RemoveFromCache(cp);
/* Final delivery payment pays the feeder share, so we have to
* reset that so it is not 'shown' twice for partial unloads. */
cp->feeder_share = 0;
cp->count = left;
} else {
/* But... the rest needs package splitting. */
Money fs = cp->feeder_share * max_move / static_cast<uint>(cp->count);
cp->feeder_share -= fs;
cp->count -= max_move;
CargoPacket *cp_new = new CargoPacket(max_move, cp->days_in_transit, cp->source, cp->source_xy, (mta == MTA_CARGO_LOAD) ? data : cp->loaded_at_xy, fs, cp->source_type, cp->source_id);
static_cast<Tinst *>(this)->RemoveFromCache(cp_new); // this reflects the changes in cp.
if (mta == MTA_TRANSFER) {
/* Add the feeder share before inserting in dest. */
cp_new->feeder_share += payment->PayTransfer(cp_new, max_move);
}
dest->Append(cp_new);
}
max_move = 0;
}
return it != packets.end();
}
template <class Tinst>
void CargoList<Tinst>::InvalidateCache()
{
this->count = 0;
this->cargo_days_in_transit = 0;
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 (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 (cp->days_in_transit == 0xFF) continue;
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.
*/
template class CargoList<VehicleCargoList>;
template class CargoList<StationCargoList>;
/** Autoreplace Vehicle -> Vehicle 'transfer' */
template bool CargoList<VehicleCargoList>::MoveTo(VehicleCargoList *, uint max_move, MoveToAction mta, CargoPayment *payment, uint data);
/** Cargo unloading at a station */
template bool CargoList<VehicleCargoList>::MoveTo(StationCargoList *, uint max_move, MoveToAction mta, CargoPayment *payment, uint data);
/** Cargo loading at a station */
template bool CargoList<StationCargoList>::MoveTo(VehicleCargoList *, uint max_move, MoveToAction mta, CargoPayment *payment, uint data);
|