cargopacket.h

Go to the documentation of this file.
00001 /* $Id: cargopacket.h 20574 2010-08-19 20:58:30Z 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 
00061   CargoPacket();
00062 
00072   CargoPacket(StationID source, TileIndex source_xy, uint16 count, SourceType source_type, SourceID source_id);
00073 
00086   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);
00087 
00089   ~CargoPacket() { }
00090 
00091 
00096   FORCEINLINE uint16 Count() const
00097   {
00098     return this->count;
00099   }
00100 
00106   FORCEINLINE Money FeederShare() const
00107   {
00108     return this->feeder_share;
00109   }
00110 
00117   FORCEINLINE byte DaysInTransit() const
00118   {
00119     return this->days_in_transit;
00120   }
00121 
00126   FORCEINLINE SourceType SourceSubsidyType() const
00127   {
00128     return this->source_type;
00129   }
00130 
00135   FORCEINLINE SourceID SourceSubsidyID() const
00136   {
00137     return this->source_id;
00138   }
00139 
00144   FORCEINLINE SourceID SourceStation() const
00145   {
00146     return this->source;
00147   }
00148 
00153   FORCEINLINE TileIndex SourceStationXY() const
00154   {
00155     return this->source_xy;
00156   }
00157 
00162   FORCEINLINE TileIndex LoadedAtXY() const
00163   {
00164     return this->loaded_at_xy;
00165   }
00166 
00167 
00168   static void InvalidateAllFrom(SourceType src_type, SourceID src);
00169   static void InvalidateAllFrom(StationID sid);
00170   static void AfterLoad();
00171 };
00172 
00178 #define FOR_ALL_CARGOPACKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(CargoPacket, cargopacket_index, var, start)
00179 
00184 #define FOR_ALL_CARGOPACKETS(var) FOR_ALL_CARGOPACKETS_FROM(var, 0)
00185 
00190 template <class Tinst>
00191 class CargoList {
00192 public:
00194   typedef std::list<CargoPacket *> List;
00196   typedef List::iterator Iterator;
00198   typedef List::const_iterator ConstIterator;
00199 
00201   enum MoveToAction {
00202     MTA_FINAL_DELIVERY, 
00203     MTA_CARGO_LOAD,     
00204     MTA_TRANSFER,       
00205     MTA_UNLOAD,         
00206   };
00207 
00208 protected:
00209   uint count;                 
00210   uint cargo_days_in_transit; 
00211 
00212   List packets;               
00213 
00219   void AddToCache(const CargoPacket *cp);
00220 
00226   void RemoveFromCache(const CargoPacket *cp);
00227 
00228 public:
00230   CargoList() {}
00232   ~CargoList();
00233 
00238   FORCEINLINE const List *Packets() const
00239   {
00240     return &this->packets;
00241   }
00242 
00247   FORCEINLINE bool Empty() const
00248   {
00249     return this->count == 0;
00250   }
00251 
00256   FORCEINLINE uint Count() const
00257   {
00258     return this->count;
00259   }
00260 
00265   FORCEINLINE StationID Source() const
00266   {
00267     return this->Empty() ? INVALID_STATION : this->packets.front()->source;
00268   }
00269 
00274   FORCEINLINE uint DaysInTransit() const
00275   {
00276     return this->count == 0 ? 0 : this->cargo_days_in_transit / this->count;
00277   }
00278 
00279 
00287   void Append(CargoPacket *cp);
00288 
00294   void Truncate(uint max_remaining);
00295 
00317   template <class Tother_inst>
00318   bool MoveTo(Tother_inst *dest, uint count, MoveToAction mta, CargoPayment *payment, uint data = 0);
00319 
00321   void InvalidateCache();
00322 };
00323 
00327 class VehicleCargoList : public CargoList<VehicleCargoList> {
00328 protected:
00330   typedef CargoList<VehicleCargoList> Parent;
00331 
00332   Money feeder_share; 
00333 
00339   void AddToCache(const CargoPacket *cp);
00340 
00346   void RemoveFromCache(const CargoPacket *cp);
00347 
00348 public:
00350   friend class CargoList<VehicleCargoList>;
00352   friend const struct SaveLoad *GetVehicleDescription(VehicleType vt);
00353 
00358   FORCEINLINE Money FeederShare() const
00359   {
00360     return this->feeder_share;
00361   }
00362 
00366   void AgeCargo();
00367 
00369   void InvalidateCache();
00370 
00378   static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
00379   {
00380     return cp1->source_xy    == cp2->source_xy &&
00381         cp1->days_in_transit == cp2->days_in_transit &&
00382         cp1->source_type     == cp2->source_type &&
00383         cp1->source_id       == cp2->source_id &&
00384         cp1->loaded_at_xy    == cp2->loaded_at_xy;
00385   }
00386 };
00387 
00391 class StationCargoList : public CargoList<StationCargoList> {
00392 public:
00394   friend class CargoList<StationCargoList>;
00396   friend const struct SaveLoad *GetGoodsDesc();
00397 
00405   static bool AreMergable(const CargoPacket *cp1, const CargoPacket *cp2)
00406   {
00407     return cp1->source_xy    == cp2->source_xy &&
00408         cp1->days_in_transit == cp2->days_in_transit &&
00409         cp1->source_type     == cp2->source_type &&
00410         cp1->source_id       == cp2->source_id;
00411   }
00412 };
00413 
00414 #endif /* CARGOPACKET_H */

Generated on Fri Dec 31 17:15:29 2010 for OpenTTD by  doxygen 1.6.1