cargopacket.h

Go to the documentation of this file.
00001 /* $Id: cargopacket.h 23640 2011-12-20 17:57:56Z truebrain $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #ifndef CARGOPACKET_H
00013 #define CARGOPACKET_H
00014 
00015 #include "core/pool_type.hpp"
00016 #include "economy_type.h"
00017 #include "station_type.h"
00018 #include "cargo_type.h"
00019 #include "vehicle_type.h"
00020 #include <list>
00021 
00023 typedef uint32 CargoPacketID;
00024 struct CargoPacket;
00025 
00027 typedef Pool<CargoPacket, CargoPacketID, 1024, 0xFFF000, PT_NORMAL, true, false> CargoPacketPool;
00029 extern CargoPacketPool _cargopacket_pool;
00030 
00031 template <class Tinst> class CargoList;
00032 extern const struct SaveLoad *GetCargoPacketDesc();
00033 
00037 struct CargoPacket : CargoPacketPool::PoolItem<&_cargopacket_pool> {
00038 private:
00039   Money feeder_share;         
00040   uint16 count;               
00041   byte days_in_transit;       
00042   SourceTypeByte source_type; 
00043   SourceID source_id;         
00044   StationID source;           
00045   TileIndex source_xy;        
00046   TileIndex loaded_at_xy;     
00047 
00049   template <class Tinst> friend class CargoList;
00050   friend class VehicleCargoList;
00051   friend class StationCargoList;
00053   friend const struct SaveLoad *GetCargoPacketDesc();
00054 public:
00056   static const uint16 MAX_COUNT = UINT16_MAX;
00057 
00058   CargoPacket();
00059   CargoPacket(StationID source, TileIndex source_xy, uint16 count, SourceType source_type, SourceID source_id);
00060   CargoPacket(uint16 count, byte days_in_transit, StationID source, TileIndex source_xy, TileIndex loaded_at_xy, Money feeder_share = 0, SourceType source_type = ST_INDUSTRY, SourceID source_id = INVALID_SOURCE);
00061 
00063   ~CargoPacket() { }
00064 
00065   CargoPacket *Split(uint new_size);
00066   void Merge(CargoPacket *cp);
00067 
00072   inline uint16 Count() const
00073   {
00074     return this->count;
00075   }
00076 
00082   inline Money FeederShare() const
00083   {
00084     return this->feeder_share;
00085   }
00086 
00093   inline byte DaysInTransit() const
00094   {
00095     return this->days_in_transit;
00096   }
00097 
00102   inline SourceType SourceSubsidyType() const
00103   {
00104     return this->source_type;
00105   }
00106 
00111   inline SourceID SourceSubsidyID() const
00112   {
00113     return this->source_id;
00114   }
00115 
00120   inline SourceID SourceStation() const
00121   {
00122     return this->source;
00123   }
00124 
00129   inline TileIndex SourceStationXY() const
00130   {
00131     return this->source_xy;
00132   }
00133 
00138   inline TileIndex LoadedAtXY() const
00139   {
00140     return this->loaded_at_xy;
00141   }
00142 
00143 
00144   static void InvalidateAllFrom(SourceType src_type, SourceID src);
00145   static void InvalidateAllFrom(StationID sid);
00146   static void AfterLoad();
00147 };
00148 
00154 #define FOR_ALL_CARGOPACKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(CargoPacket, cargopacket_index, var, start)
00155 
00160 #define FOR_ALL_CARGOPACKETS(var) FOR_ALL_CARGOPACKETS_FROM(var, 0)
00161 
00166 template <class Tinst>
00167 class CargoList {
00168 public:
00170   typedef std::list<CargoPacket *> List;
00172   typedef List::iterator Iterator;
00174   typedef List::const_iterator ConstIterator;
00175 
00177   enum MoveToAction {
00178     MTA_FINAL_DELIVERY, 
00179     MTA_CARGO_LOAD,     
00180     MTA_TRANSFER,       
00181     MTA_UNLOAD,         
00182   };
00183 
00184 protected:
00185   uint count;                 
00186   uint cargo_days_in_transit; 
00187 
00188   List packets;               
00189 
00190   void AddToCache(const CargoPacket *cp);
00191 
00192   void RemoveFromCache(const CargoPacket *cp);
00193 
00194 public:
00196   CargoList() {}
00197 
00198   ~CargoList();
00199 
00200   void OnCleanPool();
00201 
00206   inline const List *Packets() const
00207   {
00208     return &this->packets;
00209   }
00210 
00215   inline bool Empty() const
00216   {
00217     return this->count == 0;
00218   }
00219 
00224   inline uint Count() const
00225   {
00226     return this->count;
00227   }
00228 
00233   inline StationID Source() const
00234   {
00235     return this->Empty() ? INVALID_STATION : this->packets.front()->source;
00236   }
00237 
00242   inline uint DaysInTransit() const
00243   {
00244     return this->count == 0 ? 0 : this->cargo_days_in_transit / this->count;
00245   }
00246 
00247 
00248   void Append(CargoPacket *cp);
00249   void Truncate(uint max_remaining);
00250 
00251   template <class Tother_inst>
00252   bool MoveTo(Tother_inst *dest, uint count, MoveToAction mta, CargoPayment *payment, uint data = 0);
00253 
00254   void InvalidateCache();
00255 };
00256 
00260 class VehicleCargoList : public CargoList<VehicleCargoList> {
00261 protected:
00263   typedef CargoList<VehicleCargoList> Parent;
00264 
00265   Money feeder_share; 
00266 
00267   void AddToCache(const CargoPacket *cp);
00268   void RemoveFromCache(const CargoPacket *cp);
00269 
00270 public:
00272   friend class CargoList<VehicleCargoList>;
00274   friend const struct SaveLoad *GetVehicleDescription(VehicleType vt);
00275 
00280   inline Money FeederShare() const
00281   {
00282     return this->feeder_share;
00283   }
00284 
00285   void AgeCargo();
00286 
00287   void InvalidateCache();
00288 
00296   static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
00297   {
00298     return cp1->source_xy    == cp2->source_xy &&
00299         cp1->days_in_transit == cp2->days_in_transit &&
00300         cp1->source_type     == cp2->source_type &&
00301         cp1->source_id       == cp2->source_id &&
00302         cp1->loaded_at_xy    == cp2->loaded_at_xy;
00303   }
00304 };
00305 
00309 class StationCargoList : public CargoList<StationCargoList> {
00310 public:
00312   friend class CargoList<StationCargoList>;
00314   friend const struct SaveLoad *GetGoodsDesc();
00315 
00323   static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
00324   {
00325     return cp1->source_xy    == cp2->source_xy &&
00326         cp1->days_in_transit == cp2->days_in_transit &&
00327         cp1->source_type     == cp2->source_type &&
00328         cp1->source_id       == cp2->source_id;
00329   }
00330 };
00331 
00332 #endif /* CARGOPACKET_H */