00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "station_base.h"
00014 #include "core/pool_func.hpp"
00015 #include "core/random_func.hpp"
00016 #include "economy_base.h"
00017 #include "cargoaction.h"
00018 #include "order_type.h"
00019
00020
00021 CargoPacketPool _cargopacket_pool("CargoPacket");
00022 INSTANTIATE_POOL_METHODS(CargoPacket)
00023
00024
00027 CargoPacket::CargoPacket()
00028 {
00029 this->source_type = ST_INDUSTRY;
00030 this->source_id = INVALID_SOURCE;
00031 }
00032
00044 CargoPacket::CargoPacket(StationID source, TileIndex source_xy, uint16 count, SourceType source_type, SourceID source_id) :
00045 feeder_share(0),
00046 count(count),
00047 days_in_transit(0),
00048 source_id(source_id),
00049 source(source),
00050 source_xy(source_xy),
00051 loaded_at_xy(0)
00052 {
00053 assert(count != 0);
00054 this->source_type = source_type;
00055 }
00056
00071 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) :
00072 feeder_share(feeder_share),
00073 count(count),
00074 days_in_transit(days_in_transit),
00075 source_id(source_id),
00076 source(source),
00077 source_xy(source_xy),
00078 loaded_at_xy(loaded_at_xy)
00079 {
00080 assert(count != 0);
00081 this->source_type = source_type;
00082 }
00083
00089 CargoPacket *CargoPacket::Split(uint new_size)
00090 {
00091 if (!CargoPacket::CanAllocateItem()) return NULL;
00092
00093 Money fs = this->FeederShare(new_size);
00094 CargoPacket *cp_new = new CargoPacket(new_size, this->days_in_transit, this->source, this->source_xy, this->loaded_at_xy, fs, this->source_type, this->source_id);
00095 this->feeder_share -= fs;
00096 this->count -= new_size;
00097 return cp_new;
00098 }
00099
00104 void CargoPacket::Merge(CargoPacket *cp)
00105 {
00106 this->count += cp->count;
00107 this->feeder_share += cp->feeder_share;
00108 delete cp;
00109 }
00110
00115 void CargoPacket::Reduce(uint count)
00116 {
00117 assert(count < this->count);
00118 this->feeder_share -= this->FeederShare(count);
00119 this->count -= count;
00120 }
00121
00127 void CargoPacket::InvalidateAllFrom(SourceType src_type, SourceID src)
00128 {
00129 CargoPacket *cp;
00130 FOR_ALL_CARGOPACKETS(cp) {
00131 if (cp->source_type == src_type && cp->source_id == src) cp->source_id = INVALID_SOURCE;
00132 }
00133 }
00134
00139 void CargoPacket::InvalidateAllFrom(StationID sid)
00140 {
00141 CargoPacket *cp;
00142 FOR_ALL_CARGOPACKETS(cp) {
00143 if (cp->source == sid) cp->source = INVALID_STATION;
00144 }
00145 }
00146
00147
00148
00149
00150
00151
00152
00156 template <class Tinst, class Tcont>
00157 CargoList<Tinst, Tcont>::~CargoList()
00158 {
00159 for (Iterator it(this->packets.begin()); it != this->packets.end(); ++it) {
00160 delete *it;
00161 }
00162 }
00163
00168 template <class Tinst, class Tcont>
00169 void CargoList<Tinst, Tcont>::OnCleanPool()
00170 {
00171 this->packets.clear();
00172 }
00173
00180 template <class Tinst, class Tcont>
00181 void CargoList<Tinst, Tcont>::RemoveFromCache(const CargoPacket *cp, uint count)
00182 {
00183 assert(count <= cp->count);
00184 this->count -= count;
00185 this->cargo_days_in_transit -= cp->days_in_transit * count;
00186 }
00187
00193 template <class Tinst, class Tcont>
00194 void CargoList<Tinst, Tcont>::AddToCache(const CargoPacket *cp)
00195 {
00196 this->count += cp->count;
00197 this->cargo_days_in_transit += cp->days_in_transit * cp->count;
00198 }
00199
00201 template <class Tinst, class Tcont>
00202 void CargoList<Tinst, Tcont>::InvalidateCache()
00203 {
00204 this->count = 0;
00205 this->cargo_days_in_transit = 0;
00206
00207 for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) {
00208 static_cast<Tinst *>(this)->AddToCache(*it);
00209 }
00210 }
00211
00219 template <class Tinst, class Tcont>
00220 bool CargoList<Tinst, Tcont>::TryMerge(CargoPacket *icp, CargoPacket *cp)
00221 {
00222 if (Tinst::AreMergable(icp, cp) &&
00223 icp->count + cp->count <= CargoPacket::MAX_COUNT) {
00224 icp->Merge(cp);
00225 return true;
00226 } else {
00227 return false;
00228 }
00229 }
00230
00231
00232
00233
00234
00235
00236
00252 void VehicleCargoList::Append(CargoPacket *cp, MoveToAction action)
00253 {
00254 assert(cp != NULL);
00255 assert(action == MTA_LOAD ||
00256 (action == MTA_KEEP && this->action_counts[MTA_LOAD] == 0));
00257 this->AddToMeta(cp, action);
00258
00259 if (this->count == cp->count) {
00260 this->packets.push_back(cp);
00261 return;
00262 }
00263
00264 uint sum = cp->count;
00265 for (ReverseIterator it(this->packets.rbegin()); it != this->packets.rend(); it++) {
00266 CargoPacket *icp = *it;
00267 if (VehicleCargoList::TryMerge(icp, cp)) return;
00268 sum += icp->count;
00269 if (sum >= this->action_counts[action]) {
00270 this->packets.push_back(cp);
00271 return;
00272 }
00273 }
00274
00275 NOT_REACHED();
00276 }
00277
00286 template<class Taction>
00287 void VehicleCargoList::ShiftCargo(Taction action)
00288 {
00289 Iterator it(this->packets.begin());
00290 while (it != this->packets.end() && action.MaxMove() > 0) {
00291 CargoPacket *cp = *it;
00292 if (action(cp)) {
00293 it = this->packets.erase(it);
00294 } else {
00295 break;
00296 }
00297 }
00298 }
00299
00308 template<class Taction>
00309 void VehicleCargoList::PopCargo(Taction action)
00310 {
00311 if (this->packets.empty()) return;
00312 Iterator it(--(this->packets.end()));
00313 Iterator begin(this->packets.begin());
00314 while (action.MaxMove() > 0) {
00315 CargoPacket *cp = *it;
00316 if (action(cp)) {
00317 if (it != begin) {
00318 this->packets.erase(it--);
00319 } else {
00320 this->packets.erase(it);
00321 break;
00322 }
00323 } else {
00324 break;
00325 }
00326 }
00327 }
00328
00335 void VehicleCargoList::RemoveFromCache(const CargoPacket *cp, uint count)
00336 {
00337 this->feeder_share -= cp->FeederShare(count);
00338 this->Parent::RemoveFromCache(cp, count);
00339 }
00340
00346 void VehicleCargoList::AddToCache(const CargoPacket *cp)
00347 {
00348 this->feeder_share += cp->feeder_share;
00349 this->Parent::AddToCache(cp);
00350 }
00351
00358 void VehicleCargoList::RemoveFromMeta(const CargoPacket *cp, MoveToAction action, uint count)
00359 {
00360 this->AssertCountConsistency();
00361 this->RemoveFromCache(cp, count);
00362 this->action_counts[action] -= count;
00363 this->AssertCountConsistency();
00364 }
00365
00371 void VehicleCargoList::AddToMeta(const CargoPacket *cp, MoveToAction action)
00372 {
00373 this->AssertCountConsistency();
00374 this->AddToCache(cp);
00375 this->action_counts[action] += cp->count;
00376 this->AssertCountConsistency();
00377 }
00378
00382 void VehicleCargoList::AgeCargo()
00383 {
00384 for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) {
00385 CargoPacket *cp = *it;
00386
00387 if (cp->days_in_transit == 0xFF) continue;
00388
00389 cp->days_in_transit++;
00390 this->cargo_days_in_transit += cp->count;
00391 }
00392 }
00393
00401 void VehicleCargoList::SetTransferLoadPlace(TileIndex xy)
00402 {
00403 uint sum = 0;
00404 for (Iterator it = this->packets.begin(); sum < this->action_counts[MTA_TRANSFER]; ++it) {
00405 CargoPacket *cp = *it;
00406 cp->loaded_at_xy = xy;
00407 sum += cp->count;
00408 }
00409 }
00410
00424 bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationID next_station, uint8 order_flags, const GoodsEntry *ge, CargoPayment *payment)
00425 {
00426 this->AssertCountConsistency();
00427 assert(this->action_counts[MTA_LOAD] == 0);
00428 this->action_counts[MTA_TRANSFER] = this->action_counts[MTA_DELIVER] = this->action_counts[MTA_KEEP] = 0;
00429 Iterator deliver = this->packets.end();
00430 Iterator it = this->packets.begin();
00431 uint sum = 0;
00432
00433 bool force_keep = (order_flags & OUFB_NO_UNLOAD) != 0;
00434 bool force_unload = (order_flags & OUFB_UNLOAD) != 0;
00435 bool force_transfer = (order_flags & (OUFB_TRANSFER | OUFB_UNLOAD)) != 0;
00436 assert(this->count > 0 || it == this->packets.end());
00437 while (sum < this->count) {
00438 CargoPacket *cp = *it;
00439
00440 this->packets.erase(it++);
00441 StationID cargo_next = INVALID_STATION;
00442 MoveToAction action = MTA_LOAD;
00443 if (force_keep) {
00444 action = MTA_KEEP;
00445 } else if (force_unload && accepted && cp->source != current_station) {
00446 action = MTA_DELIVER;
00447 } else if (force_transfer) {
00448 action = MTA_TRANSFER;
00449 cargo_next = ge->GetVia(cp->source, current_station, next_station);
00450 assert((cargo_next != next_station || cargo_next == INVALID_STATION) &&
00451 cargo_next != current_station);
00452 } else {
00453 cargo_next = ge->GetVia(cp->source);
00454 if (cargo_next == INVALID_STATION) {
00455 action = (accepted && cp->source != current_station) ? MTA_DELIVER : MTA_KEEP;
00456 } else if (cargo_next == current_station) {
00457 action = MTA_DELIVER;
00458 } else if (cargo_next == next_station) {
00459 action = MTA_KEEP;
00460 } else {
00461 action = MTA_TRANSFER;
00462 }
00463 }
00464 Money share;
00465 switch (action) {
00466 case MTA_KEEP:
00467 this->packets.push_back(cp);
00468 if (deliver == this->packets.end()) --deliver;
00469 break;
00470 case MTA_DELIVER:
00471 this->packets.insert(deliver, cp);
00472 break;
00473 case MTA_TRANSFER:
00474 this->packets.push_front(cp);
00475
00476 share = payment->PayTransfer(cp, cp->count);
00477 cp->AddFeederShare(share);
00478 this->feeder_share += share;
00479 cp->next_station = cargo_next;
00480 break;
00481 default:
00482 NOT_REACHED();
00483 }
00484 this->action_counts[action] += cp->count;
00485 sum += cp->count;
00486 }
00487 this->AssertCountConsistency();
00488 return this->action_counts[MTA_DELIVER] > 0 || this->action_counts[MTA_TRANSFER] > 0;
00489 }
00490
00492 void VehicleCargoList::InvalidateCache()
00493 {
00494 this->feeder_share = 0;
00495 this->Parent::InvalidateCache();
00496 }
00497
00505 uint VehicleCargoList::Reassign(uint max_move, MoveToAction from, MoveToAction to)
00506 {
00507 max_move = min(this->action_counts[from], max_move);
00508 assert(Delta((int)from, (int)to) == 1);
00509 this->action_counts[from] -= max_move;
00510 this->action_counts[to] += max_move;
00511 return max_move;
00512 }
00513
00521 uint VehicleCargoList::Return(uint max_move, StationCargoList *dest, StationID next)
00522 {
00523 max_move = min(this->action_counts[MTA_LOAD], max_move);
00524 this->PopCargo(CargoReturn(this, dest, max_move, next));
00525 return max_move;
00526 }
00527
00534 uint VehicleCargoList::Shift(uint max_move, VehicleCargoList *dest)
00535 {
00536 max_move = min(this->count, max_move);
00537 this->PopCargo(CargoShift(this, dest, max_move));
00538 return max_move;
00539 }
00540
00549 uint VehicleCargoList::Unload(uint max_move, StationCargoList *dest, CargoPayment *payment)
00550 {
00551 uint moved = 0;
00552 if (this->action_counts[MTA_TRANSFER] > 0) {
00553 uint move = min(this->action_counts[MTA_TRANSFER], max_move);
00554 this->ShiftCargo(CargoTransfer(this, dest, move));
00555 moved += move;
00556 }
00557 if (this->action_counts[MTA_TRANSFER] == 0 && this->action_counts[MTA_DELIVER] > 0 && moved < max_move) {
00558 uint move = min(this->action_counts[MTA_DELIVER], max_move - moved);
00559 this->ShiftCargo(CargoDelivery(this, move, payment));
00560 moved += move;
00561 }
00562 return moved;
00563 }
00564
00571 uint VehicleCargoList::Truncate(uint max_move)
00572 {
00573 max_move = min(this->count, max_move);
00574 this->PopCargo(CargoRemoval<VehicleCargoList>(this, max_move));
00575 return max_move;
00576 }
00577
00578
00579
00580
00581
00582
00583
00592 void StationCargoList::Append(CargoPacket *cp, StationID next)
00593 {
00594 assert(cp != NULL);
00595 this->AddToCache(cp);
00596
00597 StationCargoPacketMap::List &list = this->packets[next];
00598 for (StationCargoPacketMap::List::reverse_iterator it(list.rbegin());
00599 it != list.rend(); it++) {
00600 if (StationCargoList::TryMerge(*it, cp)) return;
00601 }
00602
00603
00604 list.push_back(cp);
00605 }
00606
00619 template <class Taction>
00620 bool StationCargoList::ShiftCargo(Taction &action, StationID next)
00621 {
00622 std::pair<Iterator, Iterator> range(this->packets.equal_range(next));
00623 for (Iterator it(range.first); it != range.second && it.GetKey() == next;) {
00624 if (action.MaxMove() == 0) return false;
00625 CargoPacket *cp = *it;
00626 if (action(cp)) {
00627 it = this->packets.erase(it);
00628 } else {
00629 return false;
00630 }
00631 }
00632 return true;
00633 }
00634
00649 template <class Taction>
00650 uint StationCargoList::ShiftCargo(Taction action, StationID next, bool include_invalid)
00651 {
00652 uint max_move = action.MaxMove();
00653 if (this->ShiftCargo(action, next) && include_invalid && action.MaxMove() > 0) {
00654 this->ShiftCargo(action, INVALID_STATION);
00655 }
00656 return max_move - action.MaxMove();
00657 }
00658
00667 uint StationCargoList::Truncate(uint max_move, StationCargoAmountMap *cargo_per_source)
00668 {
00669 max_move = min(max_move, this->count);
00670 uint prev_count = this->count;
00671 uint moved = 0;
00672 uint loop = 0;
00673 bool do_count = cargo_per_source != NULL;
00674 while (max_move > moved) {
00675 for (Iterator it(this->packets.begin()); it != this->packets.end();) {
00676 CargoPacket *cp = *it;
00677 if (prev_count > max_move && RandomRange(prev_count) < prev_count - max_move) {
00678 if (do_count && loop == 0) {
00679 (*cargo_per_source)[cp->source] += cp->count;
00680 }
00681 ++it;
00682 continue;
00683 }
00684 uint diff = max_move - moved;
00685 if (cp->count > diff) {
00686 if (diff > 0) {
00687 this->RemoveFromCache(cp, diff);
00688 cp->Reduce(diff);
00689 moved += diff;
00690 }
00691 if (loop > 0) {
00692 if (do_count) (*cargo_per_source)[cp->source] -= diff;
00693 return moved;
00694 } else {
00695 if (do_count) (*cargo_per_source)[cp->source] += cp->count;
00696 ++it;
00697 }
00698 } else {
00699 it = this->packets.erase(it);
00700 if (do_count && loop > 0) {
00701 (*cargo_per_source)[cp->source] -= cp->count;
00702 }
00703 moved += cp->count;
00704 this->RemoveFromCache(cp, cp->count);
00705 delete cp;
00706 }
00707 }
00708 loop++;
00709 }
00710 return moved;
00711 }
00712
00720 uint StationCargoList::Reserve(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationID next)
00721 {
00722 max_move = min(this->count, max_move);
00723 this->ShiftCargo(CargoReservation(this, dest, max_move, load_place), next, true);
00724 return max_move;
00725 }
00726
00734 uint StationCargoList::Load(uint max_move, VehicleCargoList *dest, TileIndex load_place, StationID next_station)
00735 {
00736 uint move = min(dest->ActionCount(VehicleCargoList::MTA_LOAD), max_move);
00737 if (move > 0) {
00738 this->reserved_count -= move;
00739 dest->Reassign(move, VehicleCargoList::MTA_LOAD, VehicleCargoList::MTA_KEEP);
00740 return move;
00741 } else {
00742 move = min(this->count, max_move);
00743 return this->ShiftCargo(CargoLoad(this, dest, move, load_place), next_station, true);
00744 }
00745 }
00746
00755 uint StationCargoList::Reroute(uint max_move, StationCargoList *dest, StationID avoid, StationID avoid2, const GoodsEntry *ge)
00756 {
00757 return this->ShiftCargo(CargoReroute(this, dest, max_move, avoid, avoid2, ge), avoid, false);
00758 }
00759
00760
00761
00762
00763 template class CargoList<VehicleCargoList, CargoPacketList>;
00764 template class CargoList<StationCargoList, StationCargoPacketMap>;