OpenTTD Source 20241224-master-gf74b0cf984
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"
18
19struct Station;
20
29typedef uint32_t CargoMonitorID;
30
32typedef std::map<CargoMonitorID, OverflowSafeInt32> CargoMonitorMap;
33
36
37
38/* Constants for encoding and extracting cargo monitors. */
39constexpr uint8_t CCB_TOWN_IND_NUMBER_START = 0;
40constexpr uint8_t CCB_TOWN_IND_NUMBER_LENGTH = 16;
41constexpr uint8_t CCB_IS_INDUSTRY_BIT = 16;
42constexpr uint8_t CCB_CARGO_TYPE_START = 19;
43constexpr uint8_t CCB_CARGO_TYPE_LENGTH = 6;
44constexpr uint8_t CCB_COMPANY_START = 25;
45constexpr uint8_t CCB_COMPANY_LENGTH = 4;
46
47static_assert(NUM_CARGO <= (1 << CCB_CARGO_TYPE_LENGTH));
48static_assert(MAX_COMPANIES <= (1 << CCB_COMPANY_LENGTH));
49
50
58inline CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
59{
60 assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
61 assert(company < (1 << CCB_COMPANY_LENGTH));
62
63 uint32_t ret = 0;
68 return ret;
69}
70
79{
80 assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
81 assert(company < (1 << CCB_COMPANY_LENGTH));
82
83 uint32_t ret = 0;
87 return ret;
88}
89
99
109
116{
117 return HasBit(num, CCB_IS_INDUSTRY_BIT);
118}
119
126{
127 if (!MonitorMonitorsIndustry(num)) return INVALID_INDUSTRY;
129}
130
137{
138 if (MonitorMonitorsIndustry(num)) return INVALID_TOWN;
140}
141
144int32_t GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring);
145int32_t GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring);
146void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32_t amount, SourceType src_type, SourceID src, const Station *st, IndustryID dest = INVALID_INDUSTRY);
147
148#endif /* CARGOMONITOR_H */
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
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.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Types related to cargoes...
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
uint16_t SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition cargo_type.h:143
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:74
SourceType
Types of cargo source and destination.
Definition cargo_type.h:137
constexpr uint8_t CCB_COMPANY_START
Start bit of the company field.
CargoID DecodeMonitorCargoType(CargoMonitorID num)
Extract the cargo type from the cargo monitor.
void ClearCargoDeliveryMonitoring(CompanyID company=INVALID_OWNER)
Clear all delivery cargo monitors.
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.
CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
Encode a cargo monitoring number for pickup or delivery at a town.
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.
CargoMonitorMap _cargo_deliveries
Map of monitored deliveries to the amount since last query/activation.
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.
int32_t GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring)
Get the amount of cargo delivered for the given cargo monitor since activation or last query.
CargoMonitorMap _cargo_pickups
Map of monitored pick-ups to the amount since last query/activation.
CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
Encode a cargo monitor for pickup or delivery at an industry.
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.
Functions related to companies.
Owner
Enum for all companies/owners.
@ INVALID_OWNER
An invalid owner.
@ MAX_COMPANIES
Maximum number of companies.
Base of all industries.
An overflow safe integer-like type.
Town * town
The town this station is associated with.
Station data structure.
Base of the town class.