OpenTTD Source: src/cargomonitor.h Source File

OpenTTD Source 14.0-beta1

cargomonitor.h
Go to the documentation of this file.
1 /*
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/>.
6 */
7
10 #ifndef CARGOMONITOR_H
11 #define CARGOMONITOR_H
12
13 #include "cargo_type.h"
14 #include "company_func.h"
15 #include "industry.h"
16 #include "town.h"
17 #include "core/overflowsafe_type.hpp"
18
19 struct Station;
20
29 typedef uint32_t CargoMonitorID;
30
32 typedef std::map<CargoMonitorID, OverflowSafeInt32> CargoMonitorMap;
33
34 extern CargoMonitorMap _cargo_pickups;
35 extern CargoMonitorMap _cargo_deliveries;
36
37
39 enum CargoCompanyBits {
40 CCB_TOWN_IND_NUMBER_START = 0,
41 CCB_TOWN_IND_NUMBER_LENGTH = 16,
42 CCB_IS_INDUSTRY_BIT = 16,
43 CCB_IS_INDUSTRY_BIT_VALUE = 1ul << CCB_IS_INDUSTRY_BIT,
44 CCB_CARGO_TYPE_START = 19,
45 CCB_CARGO_TYPE_LENGTH = 6,
46 CCB_COMPANY_START = 25,
47 CCB_COMPANY_LENGTH = 4,
48 };
49
50 static_assert(NUM_CARGO <= (1 << CCB_CARGO_TYPE_LENGTH));
51 static_assert(MAX_COMPANIES <= (1 << CCB_COMPANY_LENGTH));
52
53
61 inline CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
62 {
63 assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
64 assert(company < (1 << CCB_COMPANY_LENGTH));
65
66 uint32_t ret = 0;
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);
71 return ret;
72 }
73
81 inline CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
82 {
83 assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
84 assert(company < (1 << CCB_COMPANY_LENGTH));
85
86 uint32_t ret = 0;
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);
90 return ret;
91 }
92
98 inline CompanyID DecodeMonitorCompany(CargoMonitorID num)
99 {
100 return static_cast<CompanyID>(GB(num, CCB_COMPANY_START, CCB_COMPANY_LENGTH));
101 }
102
108 inline CargoID DecodeMonitorCargoType(CargoMonitorID num)
109 {
110 return GB(num, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH);
111 }
112
118 inline bool MonitorMonitorsIndustry(CargoMonitorID num)
119 {
120 return HasBit(num, CCB_IS_INDUSTRY_BIT);
121 }
122
128 inline IndustryID DecodeMonitorIndustry(CargoMonitorID num)
129 {
130 if (!MonitorMonitorsIndustry(num)) return INVALID_INDUSTRY;
131 return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH);
132 }
133
139 inline TownID DecodeMonitorTown(CargoMonitorID num)
140 {
141 if (MonitorMonitorsIndustry(num)) return INVALID_TOWN;
142 return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH);
143 }
144
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);
150
151 #endif /* CARGOMONITOR_H */
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
CargoMonitorMap
std::map< CargoMonitorID, OverflowSafeInt32 > CargoMonitorMap
Map type for storing and updating active cargo monitor numbers and their amounts.
Definition: cargomonitor.h:32
BaseStation::town
Town * town
The town this station is associated with.
Definition: base_station_base.h:73
Station
Station data structure.
Definition: station_base.h:442
CCB_COMPANY_LENGTH
@ CCB_COMPANY_LENGTH
Number of bits of the company field.
Definition: cargomonitor.h:47
GB
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
_cargo_deliveries
CargoMonitorMap _cargo_deliveries
Map of monitored deliveries to the amount since last query/activation.
Definition: cargomonitor.cpp:17
EncodeCargoTownMonitor
CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
Encode a cargo monitoring number for pickup or delivery at a town.
Definition: cargomonitor.h:81
overflowsafe_type.hpp
AddCargoDelivery
void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32_t amount, SourceType src_type, SourceID src, const Station *st, IndustryID dest=INVALID_INDUSTRY)
Cargo was delivered to its final destination, update the pickup and delivery maps.
Definition: cargomonitor.cpp:119
town.h
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
CCB_TOWN_IND_NUMBER_LENGTH
@ CCB_TOWN_IND_NUMBER_LENGTH
Number of bits of the town or industry number.
Definition: cargomonitor.h:41
EncodeCargoIndustryMonitor
CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
Encode a cargo monitor for pickup or delivery at an industry.
Definition: cargomonitor.h:61
CCB_CARGO_TYPE_LENGTH
@ CCB_CARGO_TYPE_LENGTH
Number of bits of the cargo type field.
Definition: cargomonitor.h:45
cargo_type.h
INVALID_OWNER
@ INVALID_OWNER
An invalid owner.
Definition: company_type.h:29
ClearCargoPickupMonitoring
void ClearCargoPickupMonitoring(CompanyID company=INVALID_OWNER)
Clear all pick-up cargo monitors.
Definition: cargomonitor.cpp:47
SourceID
uint16_t SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:126
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
industry.h
CCB_IS_INDUSTRY_BIT
@ CCB_IS_INDUSTRY_BIT
Bit indicating the town/industry number is an industry.
Definition: cargomonitor.h:42
CCB_TOWN_IND_NUMBER_START
@ CCB_TOWN_IND_NUMBER_START
Start bit of the town or industry number.
Definition: cargomonitor.h:40
CCB_CARGO_TYPE_START
@ CCB_CARGO_TYPE_START
Start bit of the cargo type field.
Definition: cargomonitor.h:44
DecodeMonitorIndustry
IndustryID DecodeMonitorIndustry(CargoMonitorID num)
Extract the industry number from the cargo monitor.
Definition: cargomonitor.h:128
_cargo_pickups
CargoMonitorMap _cargo_pickups
Map of monitored pick-ups to the amount since last query/activation.
Definition: cargomonitor.cpp:16
DecodeMonitorCargoType
CargoID DecodeMonitorCargoType(CargoMonitorID num)
Extract the cargo type from the cargo monitor.
Definition: cargomonitor.h:108
GetPickupAmount
int32_t GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring)
Get the amount of cargo picked up for the given cargo monitor since activation or last query.
Definition: cargomonitor.cpp:104
SourceType
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:120
CCB_IS_INDUSTRY_BIT_VALUE
@ CCB_IS_INDUSTRY_BIT_VALUE
Value of the CCB_IS_INDUSTRY_BIT bit.
Definition: cargomonitor.h:43
company_func.h
GetDeliveryAmount
int32_t GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring)
Get the amount of cargo delivered for the given cargo monitor since activation or last query.
Definition: cargomonitor.cpp:92
DecodeMonitorCompany
CompanyID DecodeMonitorCompany(CargoMonitorID num)
Extract the company from the cargo monitor.
Definition: cargomonitor.h:98
DecodeMonitorTown
TownID DecodeMonitorTown(CargoMonitorID num)
Extract the town number from the cargo monitor.
Definition: cargomonitor.h:139
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
CCB_COMPANY_START
@ CCB_COMPANY_START
Start bit of the company field.
Definition: cargomonitor.h:46
SB
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
NUM_CARGO
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
Definition: cargo_type.h:68
ClearCargoDeliveryMonitoring
void ClearCargoDeliveryMonitoring(CompanyID company=INVALID_OWNER)
Clear all delivery cargo monitors.
Definition: cargomonitor.cpp:57
CargoCompanyBits
CargoCompanyBits
Constants for encoding and extracting cargo monitors.
Definition: cargomonitor.h:39
CargoMonitorID
uint32_t CargoMonitorID
Unique number for a company / cargo type / (town or industry).
Definition: cargomonitor.h:19
MonitorMonitorsIndustry
bool MonitorMonitorsIndustry(CargoMonitorID num)
Does the cargo number monitor an industry or a town?
Definition: cargomonitor.h:118
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103