1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
8 /** @file cargomonitor.h Cargo transport monitoring declarations. */
9 
10 #ifndef CARGOMONITOR_H
11 #define CARGOMONITOR_H
12 
13 #include "cargo_type.h"
14 #include "company_func.h"
15 #include "industry.h"
16 #include "town.h"
17 #include "core/overflowsafe_type.hpp"
18 #include <map>
19 
20 struct Station;
21 
22 /**
23  * Unique number for a company / cargo type / (town or industry).
24  * Encoding is as follows:
25  * - bits 0-15 town or industry number
26  * - bit 16 is set if it is an industry number (else it is a town number).
27  * - bits 19-23 Cargo type.
28  * - bits 24-31 %Company number.
29  */
30 typedef uint32 CargoMonitorID; ///< Type of the cargo monitor number.
31 
32 /** Map type for storing and updating active cargo monitor numbers and their amounts. */
33 typedef std::map<CargoMonitorID, OverflowSafeInt32> CargoMonitorMap;
34 
35 extern CargoMonitorMap _cargo_pickups;
36 extern CargoMonitorMap _cargo_deliveries;
37 
38 
39 /** Constants for encoding and extracting cargo monitors. */
40 enum CargoCompanyBits {
41 	CCB_TOWN_IND_NUMBER_START  = 0,  ///< Start bit of the town or industry number.
42 	CCB_TOWN_IND_NUMBER_LENGTH = 16, ///< Number of bits of the town or industry number.
43 	CCB_IS_INDUSTRY_BIT        = 16, ///< Bit indicating the town/industry number is an industry.
44 	CCB_IS_INDUSTRY_BIT_VALUE  = 1ul << CCB_IS_INDUSTRY_BIT, ///< Value of the #CCB_IS_INDUSTRY_BIT bit.
45 	CCB_CARGO_TYPE_START       = 19, ///< Start bit of the cargo type field.
46 	CCB_CARGO_TYPE_LENGTH      = 6,  ///< Number of bits of the cargo type field.
47 	CCB_COMPANY_START          = 25, ///< Start bit of the company field.
48 	CCB_COMPANY_LENGTH         = 4,  ///< Number of bits of the company field.
49 };
50 
51 static_assert(NUM_CARGO     <= (1 << CCB_CARGO_TYPE_LENGTH));
52 static_assert(MAX_COMPANIES <= (1 << CCB_COMPANY_LENGTH));
53 
54 
55 /**
56  * Encode a cargo monitor for pickup or delivery at an industry.
57  * @param company Company performing the transport.
58  * @param ctype Cargo type being transported.
59  * @param ind %Industry providing or accepting the cargo.
60  * @return The encoded cargo/company/industry number.
61  */
EncodeCargoIndustryMonitor(CompanyID company,CargoID ctype,IndustryID ind)62 static inline CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
63 {
64 	assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
65 	assert(company < (1 << CCB_COMPANY_LENGTH));
66 
67 	uint32 ret = 0;
68 	SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, ind);
69 	SetBit(ret, CCB_IS_INDUSTRY_BIT);
70 	SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
71 	SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company);
72 	return ret;
73 }
74 
75 /**
76  * Encode a cargo monitoring number for pickup or delivery at a town.
77  * @param company %Company performing the transport.
78  * @param ctype Cargo type being transported.
79  * @param town %Town providing or accepting the cargo.
80  * @return The encoded cargo/company/town number.
81  */
EncodeCargoTownMonitor(CompanyID company,CargoID ctype,TownID town)82 static inline CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
83 {
84 	assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
85 	assert(company < (1 << CCB_COMPANY_LENGTH));
86 
87 	uint32 ret = 0;
88 	SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, town);
89 	SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
90 	SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company);
91 	return ret;
92 }
93 
94 /**
95  * Extract the company from the cargo monitor.
96  * @param num Cargo monitoring number to decode.
97  * @return The extracted company id.
98  */
DecodeMonitorCompany(CargoMonitorID num)99 static inline CompanyID DecodeMonitorCompany(CargoMonitorID num)
100 {
101 	return static_cast<CompanyID>(GB(num, CCB_COMPANY_START, CCB_COMPANY_LENGTH));
102 }
103 
104 /**
105  * Extract the cargo type from the cargo monitor.
106  * @param num Cargo monitoring number to decode.
107  * @return The extracted cargo type.
108  */
DecodeMonitorCargoType(CargoMonitorID num)109 static inline CargoID DecodeMonitorCargoType(CargoMonitorID num)
110 {
111 	return GB(num, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH);
112 }
113 
114 /**
115  * Does the cargo number monitor an industry or a town?
116  * @param num Cargo monitoring number to decode.
117  * @return true if monitoring an industry, false if monitoring a town.
118  */
MonitorMonitorsIndustry(CargoMonitorID num)119 static inline bool MonitorMonitorsIndustry(CargoMonitorID num)
120 {
121 	return HasBit(num, CCB_IS_INDUSTRY_BIT);
122 }
123 
124 /**
125  * Extract the industry number from the cargo monitor.
126  * @param num Cargo monitoring number to decode.
127  * @return The extracted industry id, or #INVALID_INDUSTRY if the number does not monitor an industry.
128  */
DecodeMonitorIndustry(CargoMonitorID num)129 static inline IndustryID DecodeMonitorIndustry(CargoMonitorID num)
130 {
131 	if (!MonitorMonitorsIndustry(num)) return INVALID_INDUSTRY;
132 	return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH);
133 }
134 
135 /**
136  * Extract the town number from the cargo monitor.
137  * @param num Cargo monitoring number to decode.
138  * @return The extracted town id, or #INVALID_TOWN if the number does not monitor a town.
139  */
DecodeMonitorTown(CargoMonitorID num)140 static inline TownID DecodeMonitorTown(CargoMonitorID num)
141 {
142 	if (MonitorMonitorsIndustry(num)) return INVALID_TOWN;
143 	return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH);
144 }
145 
146 void ClearCargoPickupMonitoring(CompanyID company = INVALID_OWNER);
147 void ClearCargoDeliveryMonitoring(CompanyID company = INVALID_OWNER);
148 int32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring);
149 int32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring);
150 void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 amount, SourceType src_type, SourceID src, const Station *st, IndustryID dest = INVALID_INDUSTRY);
151 
152 #endif /* CARGOMONITOR_H */
153