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/>.
10 /** @file cargomonitor.h Cargo transport monitoring declarations. */
12 #ifndef CARGOMONITOR_H
13 #define CARGOMONITOR_H
15 #include "cargo_type.h"
16 #include "company_func.h"
19 #include "core/overflowsafe_type.hpp"
20 #include "3rdparty/cpp-btree/btree_map.h"
25 * Unique number for a company / cargo type / (town or industry).
26 * Encoding is as follows:
27 * - bits 0-15 town or industry number
28 * - bit 16 is set if it is an industry number (else it is a town number).
29 * - bits 19-23 Cargo type.
30 * - bits 24-31 %Company number.
32 typedef uint32 CargoMonitorID
; ///< Type of the cargo monitor number.
34 /** Map type for storing and updating active cargo monitor numbers and their amounts. */
35 typedef btree::btree_map
<CargoMonitorID
, OverflowSafeInt32
> CargoMonitorMap
;
37 extern CargoMonitorMap _cargo_pickups
;
38 extern CargoMonitorMap _cargo_deliveries
;
41 /** Constants for encoding and extracting cargo monitors. */
42 enum CargoCompanyBits
{
43 CCB_TOWN_IND_NUMBER_START
= 0, ///< Start bit of the town or industry number.
44 CCB_TOWN_IND_NUMBER_LENGTH
= 16, ///< Number of bits of the town or industry number.
45 CCB_IS_INDUSTRY_BIT
= 16, ///< Bit indicating the town/industry number is an industry.
46 CCB_IS_INDUSTRY_BIT_VALUE
= 1ul << CCB_IS_INDUSTRY_BIT
, ///< Value of the #CCB_IS_INDUSTRY_BIT bit.
47 CCB_CARGO_TYPE_START
= 19, ///< Start bit of the cargo type field.
48 CCB_CARGO_TYPE_LENGTH
= 5, ///< Number of bits of the cargo type field.
49 CCB_COMPANY_START
= 24, ///< Start bit of the company field.
50 CCB_COMPANY_LENGTH
= 8, ///< Number of bits of the company field.
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 static inline CargoMonitorID
EncodeCargoIndustryMonitor(CompanyID company
, CargoID ctype
, IndustryID ind
)
63 assert(ctype
< (1 << CCB_CARGO_TYPE_LENGTH
));
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
);
74 * Encode a cargo monitoring number for pickup or delivery at a town.
75 * @param company %Company performing the transport.
76 * @param ctype Cargo type being transported.
77 * @param town %Town providing or accepting the cargo.
78 * @return The encoded cargo/company/town number.
80 static inline CargoMonitorID
EncodeCargoTownMonitor(CompanyID company
, CargoID ctype
, TownID town
)
82 assert(ctype
< (1 << CCB_CARGO_TYPE_LENGTH
));
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
);
92 * Extract the company from the cargo monitor.
93 * @param num Cargo monitoring number to decode.
94 * @return The extracted company id.
96 static inline CompanyID
DecodeMonitorCompany(CargoMonitorID num
)
98 return static_cast<CompanyID
>(GB(num
, CCB_COMPANY_START
, CCB_COMPANY_LENGTH
));
102 * Extract the cargo type from the cargo monitor.
103 * @param num Cargo monitoring number to decode.
104 * @return The extracted cargo type.
106 static inline CargoID
DecodeMonitorCargoType(CargoMonitorID num
)
108 return GB(num
, CCB_CARGO_TYPE_START
, CCB_CARGO_TYPE_LENGTH
);
112 * Does the cargo number monitor an industry or a town?
113 * @param num Cargo monitoring number to decode.
114 * @return true if monitoring an industry, false if monitoring a town.
116 static inline bool MonitorMonitorsIndustry(CargoMonitorID num
)
118 return HasBit(num
, CCB_IS_INDUSTRY_BIT
);
122 * Extract the industry number from the cargo monitor.
123 * @param num Cargo monitoring number to decode.
124 * @return The extracted industry id, or #INVALID_INDUSTRY if the number does not monitor an industry.
126 static inline IndustryID
DecodeMonitorIndustry(CargoMonitorID num
)
128 if (!MonitorMonitorsIndustry(num
)) return INVALID_INDUSTRY
;
129 return GB(num
, CCB_TOWN_IND_NUMBER_START
, CCB_TOWN_IND_NUMBER_LENGTH
);
133 * Extract the town number from the cargo monitor.
134 * @param num Cargo monitoring number to decode.
135 * @return The extracted town id, or #INVALID_TOWN if the number does not monitor a town.
137 static inline TownID
DecodeMonitorTown(CargoMonitorID num
)
139 if (MonitorMonitorsIndustry(num
)) return INVALID_TOWN
;
140 return GB(num
, CCB_TOWN_IND_NUMBER_START
, CCB_TOWN_IND_NUMBER_LENGTH
);
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
);
149 #endif /* CARGOMONITOR_H */