cargomonitor.h

Go to the documentation of this file.
00001 /* $Id: cargomonitor.h 24986 2013-02-10 19:49:04Z zuu $ */
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 CARGOMONITOR_H
00013 #define CARGOMONITOR_H
00014 
00015 #include "cargo_type.h"
00016 #include "company_func.h"
00017 #include "industry.h"
00018 #include "town.h"
00019 #include <map>
00020 
00021 struct Station;
00022 
00031 typedef uint32 CargoMonitorID; 
00032 
00034 typedef std::map<CargoMonitorID, uint32> CargoMonitorMap;
00035 
00036 extern CargoMonitorMap _cargo_pickups;
00037 extern CargoMonitorMap _cargo_deliveries;
00038 
00039 
00041 enum CargoCompanyBits {
00042   CCB_TOWN_IND_NUMBER_START  = 0,  
00043   CCB_TOWN_IND_NUMBER_LENGTH = 16, 
00044   CCB_IS_INDUSTRY_BIT        = 16, 
00045   CCB_IS_INDUSTRY_BIT_VALUE  = 1ul << CCB_IS_INDUSTRY_BIT, 
00046   CCB_CARGO_TYPE_START       = 19, 
00047   CCB_CARGO_TYPE_LENGTH      = 5,  
00048   CCB_COMPANY_START          = 24, 
00049   CCB_COMPANY_LENGTH         = 8,  
00050 };
00051 
00052 
00060 static inline CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
00061 {
00062   assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
00063 
00064   uint32 ret = 0;
00065   SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, ind);
00066   SetBit(ret, CCB_IS_INDUSTRY_BIT);
00067   SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
00068   SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company);
00069   return ret;
00070 }
00071 
00079 static inline CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
00080 {
00081   assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
00082 
00083   uint32 ret = 0;
00084   SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, town);
00085   SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
00086   SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company);
00087   return ret;
00088 }
00089 
00095 static inline CompanyID DecodeMonitorCompany(CargoMonitorID num)
00096 {
00097   return static_cast<CompanyID>(GB(num, CCB_COMPANY_START, CCB_COMPANY_LENGTH));
00098 }
00099 
00105 static inline CargoID DecodeMonitorCargoType(CargoMonitorID num)
00106 {
00107   return GB(num, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH);
00108 }
00109 
00115 static inline bool MonitorMonitorsIndustry(CargoMonitorID num)
00116 {
00117   return HasBit(num, CCB_IS_INDUSTRY_BIT);
00118 }
00119 
00125 static inline IndustryID DecodeMonitorIndustry(CargoMonitorID num)
00126 {
00127   if (!MonitorMonitorsIndustry(num)) return INVALID_INDUSTRY;
00128   return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH);
00129 }
00130 
00136 static inline TownID DecodeMonitorTown(CargoMonitorID num)
00137 {
00138   if (MonitorMonitorsIndustry(num)) return INVALID_TOWN;
00139   return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH);
00140 }
00141 
00142 void ClearCargoPickupMonitoring(CompanyID company = INVALID_OWNER);
00143 void ClearCargoDeliveryMonitoring(CompanyID company = INVALID_OWNER);
00144 uint32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring);
00145 uint32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring);
00146 void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 amount, SourceType src_type, SourceID src, const Station *st);
00147 
00148 #endif /* CARGOMONITOR_H */