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"
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.
30 typedef uint32 CargoMonitorID
; ///< Type of the cargo monitor number.
32 /** Map type for storing and updating active cargo monitor numbers and their amounts. */
33 typedef std::map
<CargoMonitorID
, OverflowSafeInt32
> CargoMonitorMap
;
35 extern CargoMonitorMap _cargo_pickups
;
36 extern CargoMonitorMap _cargo_deliveries
;
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.
51 assert_compile(NUM_CARGO
<= (1 << CCB_CARGO_TYPE_LENGTH
));
52 assert_compile(MAX_COMPANIES
<= (1 << CCB_COMPANY_LENGTH
));
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.
62 static inline CargoMonitorID
EncodeCargoIndustryMonitor(CompanyID company
, CargoID ctype
, IndustryID ind
)
64 assert(ctype
< (1 << CCB_CARGO_TYPE_LENGTH
));
65 assert(company
< (1 << CCB_COMPANY_LENGTH
));
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
);
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.
82 static inline CargoMonitorID
EncodeCargoTownMonitor(CompanyID company
, CargoID ctype
, TownID town
)
84 assert(ctype
< (1 << CCB_CARGO_TYPE_LENGTH
));
85 assert(company
< (1 << CCB_COMPANY_LENGTH
));
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
);
95 * Extract the company from the cargo monitor.
96 * @param num Cargo monitoring number to decode.
97 * @return The extracted company id.
99 static inline CompanyID
DecodeMonitorCompany(CargoMonitorID num
)
101 return static_cast<CompanyID
>(GB(num
, CCB_COMPANY_START
, CCB_COMPANY_LENGTH
));
105 * Extract the cargo type from the cargo monitor.
106 * @param num Cargo monitoring number to decode.
107 * @return The extracted cargo type.
109 static inline CargoID
DecodeMonitorCargoType(CargoMonitorID num
)
111 return GB(num
, CCB_CARGO_TYPE_START
, CCB_CARGO_TYPE_LENGTH
);
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.
119 static inline bool MonitorMonitorsIndustry(CargoMonitorID num
)
121 return HasBit(num
, CCB_IS_INDUSTRY_BIT
);
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.
129 static inline IndustryID
DecodeMonitorIndustry(CargoMonitorID num
)
131 if (!MonitorMonitorsIndustry(num
)) return INVALID_INDUSTRY
;
132 return GB(num
, CCB_TOWN_IND_NUMBER_START
, CCB_TOWN_IND_NUMBER_LENGTH
);
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.
140 static inline TownID
DecodeMonitorTown(CargoMonitorID num
)
142 if (MonitorMonitorsIndustry(num
)) return INVALID_TOWN
;
143 return GB(num
, CCB_TOWN_IND_NUMBER_START
, CCB_TOWN_IND_NUMBER_LENGTH
);
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
);
152 #endif /* CARGOMONITOR_H */