OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

cargomonitor.h
Go to the documentation of this file.
1 /* $Id: cargomonitor.h 26685 2014-07-12 17:04:14Z alberth $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #ifndef CARGOMONITOR_H
13 #define CARGOMONITOR_H
14
15 #include "cargo_type.h"
16 #include "company_func.h"
17 #include "industry.h"
18 #include "town.h"
19 #include "core/overflowsafe_type.hpp"
20 #include <map>
21
22 struct Station;
23
32 typedef uint32 CargoMonitorID;
33
35 typedef std::map<CargoMonitorID, OverflowSafeInt32> CargoMonitorMap;
36
37 extern CargoMonitorMap _cargo_pickups;
38 extern CargoMonitorMap _cargo_deliveries;
39
40
42 enum CargoCompanyBits {
43 CCB_TOWN_IND_NUMBER_START = 0,
44 CCB_TOWN_IND_NUMBER_LENGTH = 16,
45 CCB_IS_INDUSTRY_BIT = 16,
46 CCB_IS_INDUSTRY_BIT_VALUE = 1ul << CCB_IS_INDUSTRY_BIT,
47 CCB_CARGO_TYPE_START = 19,
48 CCB_CARGO_TYPE_LENGTH = 5,
49 CCB_COMPANY_START = 24,
50 CCB_COMPANY_LENGTH = 8,
51 };
52
53
61 static inline CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
62 {
63 assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
64
65 uint32 ret = 0;
66 SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, ind);
67 SetBit(ret, CCB_IS_INDUSTRY_BIT);
68 SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
69 SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company);
70 return ret;
71 }
72
80 static inline CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
81 {
82 assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
83
84 uint32 ret = 0;
85 SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, town);
86 SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
87 SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company);
88 return ret;
89 }
90
96 static inline CompanyID DecodeMonitorCompany(CargoMonitorID num)
97 {
98 return static_cast<CompanyID>(GB(num, CCB_COMPANY_START, CCB_COMPANY_LENGTH));
99 }
100
106 static inline CargoID DecodeMonitorCargoType(CargoMonitorID num)
107 {
108 return GB(num, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH);
109 }
110
116 static inline bool MonitorMonitorsIndustry(CargoMonitorID num)
117 {
118 return HasBit(num, CCB_IS_INDUSTRY_BIT);
119 }
120
126 static inline IndustryID DecodeMonitorIndustry(CargoMonitorID num)
127 {
128 if (!MonitorMonitorsIndustry(num)) return INVALID_INDUSTRY;
129 return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH);
130 }
131
137 static inline TownID DecodeMonitorTown(CargoMonitorID num)
138 {
139 if (MonitorMonitorsIndustry(num)) return INVALID_TOWN;
140 return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH);
141 }
142
143 void ClearCargoPickupMonitoring(CompanyID company = INVALID_OWNER);
144 void ClearCargoDeliveryMonitoring(CompanyID company = INVALID_OWNER);
145 int32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring);
146 int32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring);
147 void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 amount, SourceType src_type, SourceID src, const Station *st);
148
149 #endif /* CARGOMONITOR_H */