cargodest_base.h

Go to the documentation of this file.
00001 /* $Id$ */
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 CARGODEST_BASE_H
00013 #define CARGODEST_BASE_H
00014 
00015 #include "cargodest_type.h"
00016 #include "cargo_type.h"
00017 #include "town_type.h"
00018 #include "core/smallvec_type.hpp"
00019 #include "core/pool_type.hpp"
00020 #include "order_type.h"
00021 #include "station_type.h"
00022 #include "company_type.h"
00023 #include "vehicle_type.h"
00024 
00025 struct CargoSourceSink;
00026 
00028 struct CargoLink {
00029   CargoSourceSink      *dest;      
00030   TransportedCargoStat amount;     
00031   uint                 weight;     
00032   byte                 weight_mod; 
00033 
00034   CargoLink(CargoSourceSink *d, byte mod) : dest(d), weight(1), weight_mod(mod) {}
00035 
00036   /* Compare two cargo links for inequality. */
00037   bool operator !=(const CargoLink &other) const
00038   {
00039     return other.dest != dest;
00040   }
00041 };
00042 
00044 struct CargoSourceSink {
00046   SmallVector<CargoLink, 8> cargo_links[NUM_CARGO];
00048   uint cargo_links_weight[NUM_CARGO];
00049 
00051   uint16 num_links_expected[NUM_CARGO];
00052 
00054   uint num_incoming_links[NUM_CARGO];
00055 
00056   virtual ~CargoSourceSink();
00057 
00059   virtual SourceType GetType() const = 0;
00061   virtual SourceID GetID() const = 0;
00062 
00069   bool HasLinkTo(CargoID cid, const CargoSourceSink *dest) const
00070   {
00071     return this->cargo_links[cid].Contains(CargoLink(const_cast<CargoSourceSink *>(dest), 1));
00072   }
00073 
00075   virtual bool AcceptsCargo(CargoID cid) const = 0;
00077   virtual bool SuppliesCargo(CargoID cid) const = 0;
00078 
00080   virtual uint GetDestinationWeight(CargoID cid, byte weight_mod) const = 0;
00081 
00082   CargoLink *GetRandomLink(CargoID cid, bool allow_self);
00083 
00085   virtual void CreateSpecialLinks(CargoID cid);
00086 
00088   virtual TileArea GetTileForDestination(CargoID cid) = 0;
00089 
00090   void SaveCargoSourceSink();
00091   void LoadCargoSourceSink();
00092   void PtrsCargoSourceSink();
00093 };
00094 
00095 
00097 typedef Pool<RouteLink, RouteLinkID, 512, 262144> RouteLinkPool;
00098 extern RouteLinkPool _routelink_pool;
00099 
00101 struct RouteLink : public RouteLinkPool::PoolItem<&_routelink_pool> {
00102 private:
00103   friend const struct SaveLoad *GetRouteLinkDescription(); 
00104   friend void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner);
00105   friend void AgeRouteLinks(Station *st);
00106 
00107   StationID       dest;            
00108   OrderID         prev_order;      
00109   OrderID         next_order;      
00110   OwnerByte       owner;           
00111   VehicleTypeByte vtype;           
00112   uint32          travel_time;     
00113   uint16          wait_time;       
00114 
00115 public:
00117   RouteLink(StationID dest = INVALID_STATION, OrderID prev_order = INVALID_ORDER, OrderID next_order = INVALID_ORDER, Owner owner = INVALID_OWNER, uint32 travel_time = 0, VehicleType vtype = VEH_INVALID)
00118     : dest(dest), prev_order(prev_order), next_order(next_order), travel_time(travel_time), wait_time(0)
00119   {
00120     this->owner = owner;
00121     this->vtype = vtype;
00122   }
00123 
00124   ~RouteLink();
00125 
00127   inline StationID GetDestination() const { return this->dest; }
00128 
00130   inline OrderID GetOriginOrderId() const { return this->prev_order; }
00131 
00133   inline OrderID GetDestOrderId() const { return this->next_order; }
00134 
00136   inline Owner GetOwner() const { return this->owner; }
00137 
00139   inline VehicleType GetVehicleType() const { return this->vtype; }
00140 
00142   inline uint32 GetTravelTime() const { return this->travel_time; }
00143 
00145   inline uint16 GetWaitTime() const { return this->wait_time; }
00146 
00148   inline void SetDestination(StationID dest_id, OrderID dest_order_id)
00149   {
00150     this->dest = dest_id;
00151     this->next_order = dest_order_id;
00152   }
00153 
00155   void UpdateTravelTime(uint32 new_time)
00156   {
00157     /* Weighted average so that a single late vehicle will not skew the time. */
00158     this->travel_time = (3 * this->travel_time + new_time) / 4;
00159   }
00160 
00162   void VehicleArrived()
00163   {
00164     this->wait_time = 0;
00165   }
00166 };
00167 
00168 
00174 #define FOR_ALL_ROUTELINKS_FROM(var, start) FOR_ALL_ITEMS_FROM(RouteLink, routelink_index, var, start)
00175 
00180 #define FOR_ALL_ROUTELINKS(var) FOR_ALL_ROUTELINKS_FROM(var, 0)
00181 
00182 #endif /* CARGODEST_BASE_H */

Generated on Fri Jun 3 05:18:49 2011 for OpenTTD by  doxygen 1.6.1