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/>.
8 /** @file cargomonitor.h Cargo transport monitoring declarations. */
10 #ifndef CARGOMONITOR_H
11 #define CARGOMONITOR_H
13 #include "cargo_type.h"
14 #include "company_func.h"
17 #include "core/overflowsafe_type.hpp"
22 * Unique number for a company / cargo type / (town or industry).
23 * Encoding is as follows:
24 * - bits 0-15 town or industry number
25 * - bit 16 is set if it is an industry number (else it is a town number).
26 * - bits 19-23 Cargo type.
27 * - bits 24-31 %Company number.
29 typedef uint32_t CargoMonitorID
; ///< Type of the cargo monitor number.
31 /** Map type for storing and updating active cargo monitor numbers and their amounts. */
32 typedef std::map
<CargoMonitorID
, OverflowSafeInt32
> CargoMonitorMap
;
34 extern CargoMonitorMap _cargo_pickups
;
35 extern CargoMonitorMap _cargo_deliveries
;
38 /** Constants for encoding and extracting cargo monitors. */
39 enum CargoCompanyBits
{
40 CCB_TOWN_IND_NUMBER_START
= 0, ///< Start bit of the town or industry number.
41 CCB_TOWN_IND_NUMBER_LENGTH
= 16, ///< Number of bits of the town or industry number.
42 CCB_IS_INDUSTRY_BIT
= 16, ///< Bit indicating the town/industry number is an industry.
43 CCB_IS_INDUSTRY_BIT_VALUE
= 1ul << CCB_IS_INDUSTRY_BIT
, ///< Value of the #CCB_IS_INDUSTRY_BIT bit.
44 CCB_CARGO_TYPE_START
= 19, ///< Start bit of the cargo type field.
45 CCB_CARGO_TYPE_LENGTH
= 6, ///< Number of bits of the cargo type field.
46 CCB_COMPANY_START
= 25, ///< Start bit of the company field.
47 CCB_COMPANY_LENGTH
= 4, ///< Number of bits of the company field.
50 static_assert(NUM_CARGO
<= (1 << CCB_CARGO_TYPE_LENGTH
));
51 static_assert(MAX_COMPANIES
<= (1 << CCB_COMPANY_LENGTH
));
55 * Encode a cargo monitor for pickup or delivery at an industry.
56 * @param company Company performing the transport.
57 * @param ctype Cargo type being transported.
58 * @param ind %Industry providing or accepting the cargo.
59 * @return The encoded cargo/company/industry number.
61 inline CargoMonitorID
EncodeCargoIndustryMonitor(CompanyID company
, CargoID ctype
, IndustryID ind
)
63 assert(ctype
< (1 << CCB_CARGO_TYPE_LENGTH
));
64 assert(company
< (1 << CCB_COMPANY_LENGTH
));
67 SB(ret
, CCB_TOWN_IND_NUMBER_START
, CCB_TOWN_IND_NUMBER_LENGTH
, ind
);
68 SetBit(ret
, CCB_IS_INDUSTRY_BIT
);
69 SB(ret
, CCB_CARGO_TYPE_START
, CCB_CARGO_TYPE_LENGTH
, ctype
);
70 SB(ret
, CCB_COMPANY_START
, CCB_COMPANY_LENGTH
, company
);
75 * Encode a cargo monitoring number for pickup or delivery at a town.
76 * @param company %Company performing the transport.
77 * @param ctype Cargo type being transported.
78 * @param town %Town providing or accepting the cargo.
79 * @return The encoded cargo/company/town number.
81 inline CargoMonitorID
EncodeCargoTownMonitor(CompanyID company
, CargoID ctype
, TownID town
)
83 assert(ctype
< (1 << CCB_CARGO_TYPE_LENGTH
));
84 assert(company
< (1 << CCB_COMPANY_LENGTH
));
87 SB(ret
, CCB_TOWN_IND_NUMBER_START
, CCB_TOWN_IND_NUMBER_LENGTH
, town
);
88 SB(ret
, CCB_CARGO_TYPE_START
, CCB_CARGO_TYPE_LENGTH
, ctype
);
89 SB(ret
, CCB_COMPANY_START
, CCB_COMPANY_LENGTH
, company
);
94 * Extract the company from the cargo monitor.
95 * @param num Cargo monitoring number to decode.
96 * @return The extracted company id.
98 inline CompanyID
DecodeMonitorCompany(CargoMonitorID num
)
100 return static_cast<CompanyID
>(GB(num
, CCB_COMPANY_START
, CCB_COMPANY_LENGTH
));
104 * Extract the cargo type from the cargo monitor.
105 * @param num Cargo monitoring number to decode.
106 * @return The extracted cargo type.
108 inline CargoID
DecodeMonitorCargoType(CargoMonitorID num
)
110 return GB(num
, CCB_CARGO_TYPE_START
, CCB_CARGO_TYPE_LENGTH
);
114 * Does the cargo number monitor an industry or a town?
115 * @param num Cargo monitoring number to decode.
116 * @return true if monitoring an industry, false if monitoring a town.
118 inline bool MonitorMonitorsIndustry(CargoMonitorID num
)
120 return HasBit(num
, CCB_IS_INDUSTRY_BIT
);
124 * Extract the industry number from the cargo monitor.
125 * @param num Cargo monitoring number to decode.
126 * @return The extracted industry id, or #INVALID_INDUSTRY if the number does not monitor an industry.
128 inline IndustryID
DecodeMonitorIndustry(CargoMonitorID num
)
130 if (!MonitorMonitorsIndustry(num
)) return INVALID_INDUSTRY
;
131 return GB(num
, CCB_TOWN_IND_NUMBER_START
, CCB_TOWN_IND_NUMBER_LENGTH
);
135 * Extract the town number from the cargo monitor.
136 * @param num Cargo monitoring number to decode.
137 * @return The extracted town id, or #INVALID_TOWN if the number does not monitor a town.
139 inline TownID
DecodeMonitorTown(CargoMonitorID num
)
141 if (MonitorMonitorsIndustry(num
)) return INVALID_TOWN
;
142 return GB(num
, CCB_TOWN_IND_NUMBER_START
, CCB_TOWN_IND_NUMBER_LENGTH
);
145 void ClearCargoPickupMonitoring(CompanyID company
= INVALID_OWNER
);
146 void ClearCargoDeliveryMonitoring(CompanyID company
= INVALID_OWNER
);
147 int32_t GetDeliveryAmount(CargoMonitorID monitor
, bool keep_monitoring
);
148 int32_t GetPickupAmount(CargoMonitorID monitor
, bool keep_monitoring
);
149 void AddCargoDelivery(CargoID cargo_type
, CompanyID company
, uint32_t amount
, SourceType src_type
, SourceID src
, const Station
*st
, IndustryID dest
= INVALID_INDUSTRY
);
151 #endif /* CARGOMONITOR_H */