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 constexpr uint8_t CCB_TOWN_IND_NUMBER_START
= 0; ///< Start bit of the town or industry number.
40 constexpr uint8_t CCB_TOWN_IND_NUMBER_LENGTH
= 16; ///< Number of bits of the town or industry number.
41 constexpr uint8_t CCB_IS_INDUSTRY_BIT
= 16; ///< Bit indicating the town/industry number is an industry.
42 constexpr uint8_t CCB_CARGO_TYPE_START
= 19; ///< Start bit of the cargo type field.
43 constexpr uint8_t CCB_CARGO_TYPE_LENGTH
= 6; ///< Number of bits of the cargo type field.
44 constexpr uint8_t CCB_COMPANY_START
= 25; ///< Start bit of the company field.
45 constexpr uint8_t CCB_COMPANY_LENGTH
= 4; ///< Number of bits of the company field.
47 static_assert(NUM_CARGO
<= (1 << CCB_CARGO_TYPE_LENGTH
));
48 static_assert(MAX_COMPANIES
<= (1 << CCB_COMPANY_LENGTH
));
52 * Encode a cargo monitor for pickup or delivery at an industry.
53 * @param company Company performing the transport.
54 * @param ctype Cargo type being transported.
55 * @param ind %Industry providing or accepting the cargo.
56 * @return The encoded cargo/company/industry number.
58 inline CargoMonitorID
EncodeCargoIndustryMonitor(CompanyID company
, CargoID ctype
, IndustryID ind
)
60 assert(ctype
< (1 << CCB_CARGO_TYPE_LENGTH
));
61 assert(company
< (1 << CCB_COMPANY_LENGTH
));
64 SB(ret
, CCB_TOWN_IND_NUMBER_START
, CCB_TOWN_IND_NUMBER_LENGTH
, ind
);
65 SetBit(ret
, CCB_IS_INDUSTRY_BIT
);
66 SB(ret
, CCB_CARGO_TYPE_START
, CCB_CARGO_TYPE_LENGTH
, ctype
);
67 SB(ret
, CCB_COMPANY_START
, CCB_COMPANY_LENGTH
, company
);
72 * Encode a cargo monitoring number for pickup or delivery at a town.
73 * @param company %Company performing the transport.
74 * @param ctype Cargo type being transported.
75 * @param town %Town providing or accepting the cargo.
76 * @return The encoded cargo/company/town number.
78 inline CargoMonitorID
EncodeCargoTownMonitor(CompanyID company
, CargoID ctype
, TownID town
)
80 assert(ctype
< (1 << CCB_CARGO_TYPE_LENGTH
));
81 assert(company
< (1 << CCB_COMPANY_LENGTH
));
84 SB(ret
, CCB_TOWN_IND_NUMBER_START
, CCB_TOWN_IND_NUMBER_LENGTH
, town
);
85 SB(ret
, CCB_CARGO_TYPE_START
, CCB_CARGO_TYPE_LENGTH
, ctype
);
86 SB(ret
, CCB_COMPANY_START
, CCB_COMPANY_LENGTH
, company
);
91 * Extract the company from the cargo monitor.
92 * @param num Cargo monitoring number to decode.
93 * @return The extracted company id.
95 inline CompanyID
DecodeMonitorCompany(CargoMonitorID num
)
97 return static_cast<CompanyID
>(GB(num
, CCB_COMPANY_START
, CCB_COMPANY_LENGTH
));
101 * Extract the cargo type from the cargo monitor.
102 * @param num Cargo monitoring number to decode.
103 * @return The extracted cargo type.
105 inline CargoID
DecodeMonitorCargoType(CargoMonitorID num
)
107 return GB(num
, CCB_CARGO_TYPE_START
, CCB_CARGO_TYPE_LENGTH
);
111 * Does the cargo number monitor an industry or a town?
112 * @param num Cargo monitoring number to decode.
113 * @return true if monitoring an industry, false if monitoring a town.
115 inline bool MonitorMonitorsIndustry(CargoMonitorID num
)
117 return HasBit(num
, CCB_IS_INDUSTRY_BIT
);
121 * Extract the industry number from the cargo monitor.
122 * @param num Cargo monitoring number to decode.
123 * @return The extracted industry id, or #INVALID_INDUSTRY if the number does not monitor an industry.
125 inline IndustryID
DecodeMonitorIndustry(CargoMonitorID num
)
127 if (!MonitorMonitorsIndustry(num
)) return INVALID_INDUSTRY
;
128 return GB(num
, CCB_TOWN_IND_NUMBER_START
, CCB_TOWN_IND_NUMBER_LENGTH
);
132 * Extract the town number from the cargo monitor.
133 * @param num Cargo monitoring number to decode.
134 * @return The extracted town id, or #INVALID_TOWN if the number does not monitor a town.
136 inline TownID
DecodeMonitorTown(CargoMonitorID num
)
138 if (MonitorMonitorsIndustry(num
)) return INVALID_TOWN
;
139 return GB(num
, CCB_TOWN_IND_NUMBER_START
, CCB_TOWN_IND_NUMBER_LENGTH
);
142 void ClearCargoPickupMonitoring(CompanyID company
= INVALID_OWNER
);
143 void ClearCargoDeliveryMonitoring(CompanyID company
= INVALID_OWNER
);
144 int32_t GetDeliveryAmount(CargoMonitorID monitor
, bool keep_monitoring
);
145 int32_t GetPickupAmount(CargoMonitorID monitor
, bool keep_monitoring
);
146 void AddCargoDelivery(CargoID cargo_type
, CompanyID company
, uint32_t amount
, SourceType src_type
, SourceID src
, const Station
*st
, IndustryID dest
= INVALID_INDUSTRY
);
148 #endif /* CARGOMONITOR_H */