cargopacket.h

Go to the documentation of this file.
00001 /* $Id: cargopacket.h 17840 2009-10-21 19:42:49Z 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 "tile_type.h"
00018 #include "station_type.h"
00019 #include "cargo_type.h"
00020 #include "vehicle_type.h"
00021 #include <list>
00022 
00024 typedef uint32 CargoPacketID;
00025 struct CargoPacket;
00026 
00028 typedef Pool<CargoPacket, CargoPacketID, 1024, 1048576, true, false> CargoPacketPool;
00030 extern CargoPacketPool _cargopacket_pool;
00031 
00032 template <class Tinst> class CargoList;
00033 extern const struct SaveLoad *GetCargoPacketDesc();
00034 
00038 struct CargoPacket : CargoPacketPool::PoolItem<&_cargopacket_pool> {
00039 private:
00040   Money feeder_share;         
00041   uint16 count;               
00042   byte days_in_transit;       
00043   SourceTypeByte source_type; 
00044   SourceID source_id;         
00045   StationID source;           
00046   TileIndex source_xy;        
00047   TileIndex loaded_at_xy;     
00048 
00050   template <class Tinst> friend class CargoList;
00051   friend class VehicleCargoList;
00052   friend class StationCargoList;
00054   friend const struct SaveLoad *GetCargoPacketDesc();
00055 public:
00057   static const uint16 MAX_COUNT = UINT16_MAX;
00058 
00062   CargoPacket();
00063 
00073   CargoPacket(StationID source, TileIndex source_xy, uint16 count, SourceType source_type, SourceID source_id);
00074 
00087   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);
00088 
00090   ~CargoPacket() { }
00091 
00092 
00097   FORCEINLINE uint16 Count() const
00098   {
00099     return this->count;
00100   }
00101 
00107   FORCEINLINE Money FeederShare() const
00108   {
00109     return this->feeder_share;
00110   }
00111 
00118   FORCEINLINE byte DaysInTransit() const
00119   {
00120     return this->days_in_transit;
00121   }
00122 
00127   FORCEINLINE SourceType SourceSubsidyType() const
00128   {
00129     return this->source_type;
00130   }
00131 
00136   FORCEINLINE SourceID SourceSubsidyID() const
00137   {
00138     return this->source_id;
00139   }
00140 
00145   FORCEINLINE SourceID SourceStation() const
00146   {
00147     return this->source;
00148   }
00149 
00154   FORCEINLINE TileIndex SourceStationXY() const
00155   {
00156     return this->source_xy;
00157   }
00158 
00163   FORCEINLINE TileIndex LoadedAtXY() const
00164   {
00165     return this->loaded_at_xy;
00166   }
00167 
00168 
00169   static void InvalidateAllFrom(SourceType src_type, SourceID src);
00170   static void InvalidateAllFrom(StationID sid);
00171   static void AfterLoad();
00172 };
00173 
00179 #define FOR_ALL_CARGOPACKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(CargoPacket, cargopacket_index, var, start)
00180 
00185 #define FOR_ALL_CARGOPACKETS(var) FOR_ALL_CARGOPACKETS_FROM(var, 0)
00186 
00191 template <class Tinst>
00192 class CargoList {
00193 public:
00195   typedef std::list<CargoPacket *> List;
00197   typedef List::iterator Iterator;
00199   typedef List::const_iterator ConstIterator;
00200 
00202   enum MoveToAction {
00203     MTA_FINAL_DELIVERY, 
00204     MTA_CARGO_LOAD,     
00205     MTA_TRANSFER,       
00206     MTA_UNLOAD,         
00207   };
00208 
00209 protected:
00210   uint count;                 
00211   uint cargo_days_in_transit; 
00212 
00213   List packets;               
00214 
00220   void AddToCache(const CargoPacket *cp);
00221 
00227   void RemoveFromCache(const CargoPacket *cp);
00228 
00229 public:
00231   CargoList() {}
00233   ~CargoList();
00234 
00239   FORCEINLINE const List *Packets() const
00240   {
00241     return &this->packets;
00242   }
00243 
00248   FORCEINLINE bool Empty() const
00249   {
00250     return this->count == 0;
00251   }
00252 
00257   FORCEINLINE uint Count() const
00258   {
00259     return this->count;
00260   }
00261 
00266   FORCEINLINE StationID Source() const
00267   {
00268     return this->Empty() ? INVALID_STATION : this->packets.front()->source;
00269   }
00270 
00275   FORCEINLINE uint DaysInTransit() const
00276   {
00277     return this->count == 0 ? 0 : this->cargo_days_in_transit / this->count;
00278   }
00279 
00280 
00288   void Append(CargoPacket *cp);
00289 
00295   void Truncate(uint max_remaining);
00296 
00318   template <class Tother_inst>
00319   bool MoveTo(Tother_inst *dest, uint count, MoveToAction mta, CargoPayment *payment, uint data = 0);
00320 
00322   void InvalidateCache();
00323 };
00324 
00328 class VehicleCargoList : public CargoList<VehicleCargoList> {
00329 protected:
00331   typedef CargoList<VehicleCargoList> Parent;
00332 
00333   Money feeder_share; 
00334 
00340   void AddToCache(const CargoPacket *cp);
00341 
00347   void RemoveFromCache(const CargoPacket *cp);
00348 
00349 public:
00351   friend class CargoList<VehicleCargoList>;
00353   friend const struct SaveLoad *GetVehicleDescription(VehicleType vt);
00354 
00359   FORCEINLINE Money FeederShare() const
00360   {
00361     return this->feeder_share;
00362   }
00363 
00367   void AgeCargo();
00368 
00370   void InvalidateCache();
00371 
00379   static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
00380   {
00381     return cp1->source_xy    == cp2->source_xy &&
00382         cp1->days_in_transit == cp2->days_in_transit &&
00383         cp1->source_type     == cp2->source_type &&
00384         cp1->source_id       == cp2->source_id &&
00385         cp1->loaded_at_xy    == cp2->loaded_at_xy;
00386   }
00387 };
00388 
00392 class StationCargoList : public CargoList<StationCargoList> {
00393 public:
00395   friend class CargoList<StationCargoList>;
00397   friend const struct SaveLoad *GetGoodsDesc();
00398 
00406   static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
00407   {
00408     return cp1->source_xy    == cp2->source_xy &&
00409         cp1->days_in_transit == cp2->days_in_transit &&
00410         cp1->source_type     == cp2->source_type &&
00411         cp1->source_id       == cp2->source_id;
00412   }
00413 };
00414 
00415 #endif /* CARGOPACKET_H */

Generated on Wed Dec 23 23:27:49 2009 for OpenTTD by  doxygen 1.5.6