Rearrange conditionals in FreeTrainTrackReservation
[openttd/fttd.git] / src / cargomonitor.h
blobce9a8e5d6d5bb66574ae9b60e1cfd22ce3200bbe
1 /* $Id$ */
3 /*
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/>.
8 */
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"
17 #include "industry.h"
18 #include "town.h"
19 #include <map>
21 struct Station;
23 /**
24 * Unique number for a company / cargo type / (town or industry).
25 * Encoding is as follows:
26 * - bits 0-15 town or industry number
27 * - bit 16 is set if it is an industry number (else it is a town number).
28 * - bits 19-23 Cargo type.
29 * - bits 24-31 %Company number.
31 typedef uint32 CargoMonitorID; ///< Type of the cargo monitor number.
33 /** Map type for storing and updating active cargo monitor numbers and their amounts. */
34 typedef std::map<CargoMonitorID, uint32> CargoMonitorMap;
36 extern CargoMonitorMap _cargo_pickups;
37 extern CargoMonitorMap _cargo_deliveries;
40 /** Constants for encoding and extracting cargo monitors. */
41 enum CargoCompanyBits {
42 CCB_TOWN_IND_NUMBER_START = 0, ///< Start bit of the town or industry number.
43 CCB_TOWN_IND_NUMBER_LENGTH = 16, ///< Number of bits of the town or industry number.
44 CCB_IS_INDUSTRY_BIT = 16, ///< Bit indicating the town/industry number is an industry.
45 CCB_IS_INDUSTRY_BIT_VALUE = 1ul << CCB_IS_INDUSTRY_BIT, ///< Value of the #CCB_IS_INDUSTRY_BIT bit.
46 CCB_CARGO_TYPE_START = 19, ///< Start bit of the cargo type field.
47 CCB_CARGO_TYPE_LENGTH = 5, ///< Number of bits of the cargo type field.
48 CCB_COMPANY_START = 24, ///< Start bit of the company field.
49 CCB_COMPANY_LENGTH = 8, ///< Number of bits of the company field.
53 /**
54 * Encode a cargo monitor for pickup or delivery at an industry.
55 * @param company Company performing the transport.
56 * @param ctype Cargo type being transported.
57 * @param ind %Industry providing or accepting the cargo.
58 * @return The encoded cargo/company/industry number.
60 static inline CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
62 assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
64 uint32 ret = 0;
65 SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, ind);
66 SetBit(ret, CCB_IS_INDUSTRY_BIT);
67 SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
68 SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company);
69 return ret;
72 /**
73 * Encode a cargo monitoring number for pickup or delivery at a town.
74 * @param company %Company performing the transport.
75 * @param ctype Cargo type being transported.
76 * @param town %Town providing or accepting the cargo.
77 * @return The encoded cargo/company/town number.
79 static inline CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
81 assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
83 uint32 ret = 0;
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);
87 return ret;
90 /**
91 * Extract the company from the cargo monitor.
92 * @param num Cargo monitoring number to decode.
93 * @return The extracted company id.
95 static 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 static 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 static 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 static 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 static 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 uint32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring);
145 uint32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring);
146 void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 amount, SourceType src_type, SourceID src, const Station *st);
148 #endif /* CARGOMONITOR_H */