OpenTTD Source 20261112-master-gbcbec5e63f
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef CARGOMONITOR_H
11#define CARGOMONITOR_H
12
13#include "cargo_type.h"
14#include "source_type.h"
16
17struct Station;
18
27typedef uint32_t CargoMonitorID;
28
30typedef std::map<CargoMonitorID, OverflowSafeInt32> CargoMonitorMap;
31
34
35
36/* Constants for encoding and extracting cargo monitors. */
37constexpr uint8_t CCB_TOWN_IND_NUMBER_START = 0;
38constexpr uint8_t CCB_TOWN_IND_NUMBER_LENGTH = 16;
39constexpr uint8_t CCB_IS_INDUSTRY_BIT = 16;
40constexpr uint8_t CCB_CARGO_TYPE_START = 19;
41constexpr uint8_t CCB_CARGO_TYPE_LENGTH = 6;
42constexpr uint8_t CCB_COMPANY_START = 25;
43constexpr uint8_t CCB_COMPANY_LENGTH = 4;
44
45static_assert(NUM_CARGO <= (1 << CCB_CARGO_TYPE_LENGTH));
46static_assert(MAX_COMPANIES <= (1 << CCB_COMPANY_LENGTH));
47
48
56inline CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoType ctype, IndustryID ind)
57{
58 assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
59 assert(company < (1 << CCB_COMPANY_LENGTH));
60
61 uint32_t ret = 0;
65 SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company.base());
66 return ret;
67}
68
76inline CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoType ctype, TownID town)
77{
78 assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
79 assert(company < (1 << CCB_COMPANY_LENGTH));
80
81 uint32_t ret = 0;
84 SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company.base());
85 return ret;
86}
87
94{
95 return static_cast<CompanyID>(GB(num, CCB_COMPANY_START, CCB_COMPANY_LENGTH));
96}
97
107
114{
115 return HasBit(num, CCB_IS_INDUSTRY_BIT);
116}
117
124{
125 if (!MonitorMonitorsIndustry(num)) return IndustryID::Invalid();
126 return static_cast<IndustryID>(GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH));
127}
128
135{
136 if (MonitorMonitorsIndustry(num)) return TownID::Invalid();
137 return static_cast<TownID>(GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH));
138}
139
140void ClearCargoPickupMonitoring(CompanyID company = INVALID_OWNER);
141void ClearCargoDeliveryMonitoring(CompanyID company = INVALID_OWNER);
142int32_t GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring);
143int32_t GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring);
144void AddCargoDelivery(CargoType cargo_type, CompanyID company, uint32_t amount, Source src, const Station *st, IndustryID dest = IndustryID::Invalid());
145
146#endif /* CARGOMONITOR_H */
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.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Types related to cargoes...
static constexpr CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:77
CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
CargoMonitorMap _cargo_deliveries
Map of monitored deliveries to the amount since last query/activation.
CargoMonitorMap _cargo_pickups
Map of monitored pick-ups to the amount since last query/activation.
constexpr uint8_t CCB_COMPANY_START
Start bit of the company field.
void ClearCargoDeliveryMonitoring(CompanyID company=INVALID_OWNER)
Clear all delivery cargo monitors.
CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoType ctype, IndustryID ind)
Encode a cargo monitor for pickup or delivery at an industry.
constexpr uint8_t CCB_TOWN_IND_NUMBER_START
Start bit of the town or industry number.
constexpr uint8_t CCB_IS_INDUSTRY_BIT
Bit indicating the town/industry number is an industry.
void ClearCargoPickupMonitoring(CompanyID company=INVALID_OWNER)
Clear all pick-up cargo monitors.
constexpr uint8_t CCB_CARGO_TYPE_LENGTH
Number of bits of the cargo type field.
IndustryID DecodeMonitorIndustry(CargoMonitorID num)
Extract the industry number from the cargo monitor.
bool MonitorMonitorsIndustry(CargoMonitorID num)
Does the cargo number monitor an industry or a town?
uint32_t CargoMonitorID
Unique number for a company / cargo type / (town or industry).
constexpr uint8_t CCB_TOWN_IND_NUMBER_LENGTH
Number of bits of the town or industry number.
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.
CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoType ctype, TownID town)
Encode a cargo monitoring number for pickup or delivery at a town.
void AddCargoDelivery(CargoType cargo_type, CompanyID company, uint32_t amount, Source src, const Station *st, IndustryID dest=IndustryID::Invalid())
Cargo was delivered to its final destination, update the pickup and delivery maps.
CargoType DecodeMonitorCargoType(CargoMonitorID num)
Extract the cargo type from the cargo monitor.
int32_t GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring)
Get the amount of cargo delivered for the given cargo monitor since activation or last query.
constexpr uint8_t CCB_CARGO_TYPE_START
Start bit of the cargo type field.
std::map< CargoMonitorID, OverflowSafeInt32 > CargoMonitorMap
Map type for storing and updating active cargo monitor numbers and their amounts.
CompanyID DecodeMonitorCompany(CargoMonitorID num)
Extract the company from the cargo monitor.
TownID DecodeMonitorTown(CargoMonitorID num)
Extract the town number from the cargo monitor.
constexpr uint8_t CCB_COMPANY_LENGTH
Number of bits of the company field.
static constexpr Owner INVALID_OWNER
An invalid owner.
An overflow safe integer-like type.
Type for the source of cargo.
Town * town
The town this station is associated with.
A location from where cargo can come from (or go to).
Definition source_type.h:32
Station data structure.