cargomonitor.h

Go to the documentation of this file.
00001 /* $Id: cargomonitor.h 26714 2014-08-03 14:03:07Z frosch $ */
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 "core/overflowsafe_type.hpp"
00020 #include <map>
00021 
00022 struct Station;
00023 
00032 typedef uint32 CargoMonitorID; 
00033 
00035 typedef std::map<CargoMonitorID, OverflowSafeInt32> CargoMonitorMap;
00036 
00037 extern CargoMonitorMap _cargo_pickups;
00038 extern CargoMonitorMap _cargo_deliveries;
00039 
00040 
00042 enum CargoCompanyBits {
00043   CCB_TOWN_IND_NUMBER_START  = 0,  
00044   CCB_TOWN_IND_NUMBER_LENGTH = 16, 
00045   CCB_IS_INDUSTRY_BIT        = 16, 
00046   CCB_IS_INDUSTRY_BIT_VALUE  = 1ul << CCB_IS_INDUSTRY_BIT, 
00047   CCB_CARGO_TYPE_START       = 19, 
00048   CCB_CARGO_TYPE_LENGTH      = 5,  
00049   CCB_COMPANY_START          = 24, 
00050   CCB_COMPANY_LENGTH         = 8,  
00051 };
00052 
00053 
00061 static inline CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
00062 {
00063   assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
00064 
00065   uint32 ret = 0;
00066   SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, ind);
00067   SetBit(ret, CCB_IS_INDUSTRY_BIT);
00068   SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
00069   SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company);
00070   return ret;
00071 }
00072 
00080 static inline CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
00081 {
00082   assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
00083 
00084   uint32 ret = 0;
00085   SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, town);
00086   SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
00087   SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company);
00088   return ret;
00089 }
00090 
00096 static inline CompanyID DecodeMonitorCompany(CargoMonitorID num)
00097 {
00098   return static_cast<CompanyID>(GB(num, CCB_COMPANY_START, CCB_COMPANY_LENGTH));
00099 }
00100 
00106 static inline CargoID DecodeMonitorCargoType(CargoMonitorID num)
00107 {
00108   return GB(num, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH);
00109 }
00110 
00116 static inline bool MonitorMonitorsIndustry(CargoMonitorID num)
00117 {
00118   return HasBit(num, CCB_IS_INDUSTRY_BIT);
00119 }
00120 
00126 static inline IndustryID DecodeMonitorIndustry(CargoMonitorID num)
00127 {
00128   if (!MonitorMonitorsIndustry(num)) return INVALID_INDUSTRY;
00129   return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH);
00130 }
00131 
00137 static inline TownID DecodeMonitorTown(CargoMonitorID num)
00138 {
00139   if (MonitorMonitorsIndustry(num)) return INVALID_TOWN;
00140   return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH);
00141 }
00142 
00143 void ClearCargoPickupMonitoring(CompanyID company = INVALID_OWNER);
00144 void ClearCargoDeliveryMonitoring(CompanyID company = INVALID_OWNER);
00145 int32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring);
00146 int32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring);
00147 void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 amount, SourceType src_type, SourceID src, const Station *st);
00148 
00149 #endif /* CARGOMONITOR_H */