cargopacket.h

Go to the documentation of this file.
00001 /* $Id: cargopacket.h 21849 2011-01-19 16:25:00Z rubidium $ */
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, 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   FORCEINLINE uint16 Count() const
00073   {
00074     return this->count;
00075   }
00076 
00082   FORCEINLINE Money FeederShare() const
00083   {
00084     return this->feeder_share;
00085   }
00086 
00093   FORCEINLINE byte DaysInTransit() const
00094   {
00095     return this->days_in_transit;
00096   }
00097 
00102   FORCEINLINE SourceType SourceSubsidyType() const
00103   {
00104     return this->source_type;
00105   }
00106 
00111   FORCEINLINE SourceID SourceSubsidyID() const
00112   {
00113     return this->source_id;
00114   }
00115 
00120   FORCEINLINE SourceID SourceStation() const
00121   {
00122     return this->source;
00123   }
00124 
00129   FORCEINLINE TileIndex SourceStationXY() const
00130   {
00131     return this->source_xy;
00132   }
00133 
00138   FORCEINLINE 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 
00204   FORCEINLINE const List *Packets() const
00205   {
00206     return &this->packets;
00207   }
00208 
00213   FORCEINLINE bool Empty() const
00214   {
00215     return this->count == 0;
00216   }
00217 
00222   FORCEINLINE uint Count() const
00223   {
00224     return this->count;
00225   }
00226 
00231   FORCEINLINE StationID Source() const
00232   {
00233     return this->Empty() ? INVALID_STATION : this->packets.front()->source;
00234   }
00235 
00240   FORCEINLINE uint DaysInTransit() const
00241   {
00242     return this->count == 0 ? 0 : this->cargo_days_in_transit / this->count;
00243   }
00244 
00245 
00246   void Append(CargoPacket *cp);
00247   void Truncate(uint max_remaining);
00248 
00249   template <class Tother_inst>
00250   bool MoveTo(Tother_inst *dest, uint count, MoveToAction mta, CargoPayment *payment, uint data = 0);
00251 
00252   void InvalidateCache();
00253 };
00254 
00258 class VehicleCargoList : public CargoList<VehicleCargoList> {
00259 protected:
00261   typedef CargoList<VehicleCargoList> Parent;
00262 
00263   Money feeder_share; 
00264 
00265   void AddToCache(const CargoPacket *cp);
00266   void RemoveFromCache(const CargoPacket *cp);
00267 
00268 public:
00270   friend class CargoList<VehicleCargoList>;
00272   friend const struct SaveLoad *GetVehicleDescription(VehicleType vt);
00273 
00278   FORCEINLINE Money FeederShare() const
00279   {
00280     return this->feeder_share;
00281   }
00282 
00283   void AgeCargo();
00284 
00285   void InvalidateCache();
00286 
00294   static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
00295   {
00296     return cp1->source_xy    == cp2->source_xy &&
00297         cp1->days_in_transit == cp2->days_in_transit &&
00298         cp1->source_type     == cp2->source_type &&
00299         cp1->source_id       == cp2->source_id &&
00300         cp1->loaded_at_xy    == cp2->loaded_at_xy;
00301   }
00302 };
00303 
00307 class StationCargoList : public CargoList<StationCargoList> {
00308 public:
00310   friend class CargoList<StationCargoList>;
00312   friend const struct SaveLoad *GetGoodsDesc();
00313 
00321   static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
00322   {
00323     return cp1->source_xy    == cp2->source_xy &&
00324         cp1->days_in_transit == cp2->days_in_transit &&
00325         cp1->source_type     == cp2->source_type &&
00326         cp1->source_id       == cp2->source_id;
00327   }
00328 };
00329 
00330 #endif /* CARGOPACKET_H */

Generated on Fri Mar 4 21:36:57 2011 for OpenTTD by  doxygen 1.6.1