OpenTTD Source: src/cargomonitor.h Source File

OpenTTD Source 15.0-RC2

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
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
19struct Station;
20
29 typedef uint32_t CargoMonitorID;
30
32 typedef std::map<CargoMonitorID, OverflowSafeInt32> CargoMonitorMap;
33
34extern CargoMonitorMap _cargo_pickups;
35extern CargoMonitorMap _cargo_deliveries;
36
37
38/* Constants for encoding and extracting cargo monitors. */
39 constexpr uint8_t CCB_TOWN_IND_NUMBER_START = 0;
40 constexpr uint8_t CCB_TOWN_IND_NUMBER_LENGTH = 16;
41 constexpr uint8_t CCB_IS_INDUSTRY_BIT = 16;
42 constexpr uint8_t CCB_CARGO_TYPE_START = 19;
43 constexpr uint8_t CCB_CARGO_TYPE_LENGTH = 6;
44 constexpr uint8_t CCB_COMPANY_START = 25;
45 constexpr 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
58 inline CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoType 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;
64 SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, ind.base());
65 SetBit(ret, CCB_IS_INDUSTRY_BIT);
66 SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
67 SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company.base());
68 return ret;
69}
70
78 inline CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoType ctype, TownID town)
79{
80 assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
81 assert(company < (1 << CCB_COMPANY_LENGTH));
82
83 uint32_t ret = 0;
84 SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, town.base());
85 SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
86 SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company.base());
87 return ret;
88}
89
95 inline CompanyID DecodeMonitorCompany(CargoMonitorID num)
96{
97 return static_cast<CompanyID>(GB(num, CCB_COMPANY_START, CCB_COMPANY_LENGTH));
98}
99
105 inline CargoType DecodeMonitorCargoType(CargoMonitorID num)
106{
107 return GB(num, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH);
108}
109
115 inline bool MonitorMonitorsIndustry(CargoMonitorID num)
116{
117 return HasBit(num, CCB_IS_INDUSTRY_BIT);
118}
119
125 inline IndustryID DecodeMonitorIndustry(CargoMonitorID num)
126{
127 if (!MonitorMonitorsIndustry(num)) return IndustryID::Invalid();
128 return static_cast<IndustryID>(GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH));
129}
130
136 inline TownID DecodeMonitorTown(CargoMonitorID num)
137{
138 if (MonitorMonitorsIndustry(num)) return TownID::Invalid();
139 return static_cast<TownID>(GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH));
140}
141
142void ClearCargoPickupMonitoring(CompanyID company = INVALID_OWNER);
143void ClearCargoDeliveryMonitoring(CompanyID company = INVALID_OWNER);
144int32_t GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring);
145int32_t GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring);
146void AddCargoDelivery(CargoType cargo_type, CompanyID company, uint32_t amount, Source src, const Station *st, IndustryID dest = IndustryID::Invalid());
147
148#endif /* CARGOMONITOR_H */
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
GB
static constexpr 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
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition bitmath_func.hpp:121
HasBit
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
cargo_type.h
Types related to cargoes...
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
NUM_CARGO
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:73
CCB_COMPANY_START
constexpr uint8_t CCB_COMPANY_START
Start bit of the company field.
Definition cargomonitor.h:44
ClearCargoDeliveryMonitoring
void ClearCargoDeliveryMonitoring(CompanyID company=INVALID_OWNER)
Clear all delivery cargo monitors.
Definition cargomonitor.cpp:57
EncodeCargoIndustryMonitor
CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoType ctype, IndustryID ind)
Encode a cargo monitor for pickup or delivery at an industry.
Definition cargomonitor.h:58
CCB_TOWN_IND_NUMBER_START
constexpr uint8_t CCB_TOWN_IND_NUMBER_START
Start bit of the town or industry number.
Definition cargomonitor.h:39
CCB_IS_INDUSTRY_BIT
constexpr uint8_t CCB_IS_INDUSTRY_BIT
Bit indicating the town/industry number is an industry.
Definition cargomonitor.h:41
_cargo_deliveries
CargoMonitorMap _cargo_deliveries
Map of monitored deliveries to the amount since last query/activation.
Definition cargomonitor.cpp:17
ClearCargoPickupMonitoring
void ClearCargoPickupMonitoring(CompanyID company=INVALID_OWNER)
Clear all pick-up cargo monitors.
Definition cargomonitor.cpp:47
CCB_CARGO_TYPE_LENGTH
constexpr uint8_t CCB_CARGO_TYPE_LENGTH
Number of bits of the cargo type field.
Definition cargomonitor.h:43
DecodeMonitorIndustry
IndustryID DecodeMonitorIndustry(CargoMonitorID num)
Extract the industry number from the cargo monitor.
Definition cargomonitor.h:125
MonitorMonitorsIndustry
bool MonitorMonitorsIndustry(CargoMonitorID num)
Does the cargo number monitor an industry or a town?
Definition cargomonitor.h:115
CargoMonitorID
uint32_t CargoMonitorID
Unique number for a company / cargo type / (town or industry).
Definition cargomonitor.h:29
CCB_TOWN_IND_NUMBER_LENGTH
constexpr uint8_t CCB_TOWN_IND_NUMBER_LENGTH
Number of bits of the town or industry number.
Definition cargomonitor.h:40
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:103
EncodeCargoTownMonitor
CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoType ctype, TownID town)
Encode a cargo monitoring number for pickup or delivery at a town.
Definition cargomonitor.h:78
AddCargoDelivery
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.
Definition cargomonitor.cpp:117
DecodeMonitorCargoType
CargoType DecodeMonitorCargoType(CargoMonitorID num)
Extract the cargo type from the cargo monitor.
Definition cargomonitor.h:105
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:91
_cargo_pickups
CargoMonitorMap _cargo_pickups
Map of monitored pick-ups to the amount since last query/activation.
Definition cargomonitor.cpp:16
CCB_CARGO_TYPE_START
constexpr uint8_t CCB_CARGO_TYPE_START
Start bit of the cargo type field.
Definition cargomonitor.h:42
CargoMonitorMap
std::map< CargoMonitorID, OverflowSafeInt32 > CargoMonitorMap
Map type for storing and updating active cargo monitor numbers and their amounts.
Definition cargomonitor.h:32
DecodeMonitorCompany
CompanyID DecodeMonitorCompany(CargoMonitorID num)
Extract the company from the cargo monitor.
Definition cargomonitor.h:95
DecodeMonitorTown
TownID DecodeMonitorTown(CargoMonitorID num)
Extract the town number from the cargo monitor.
Definition cargomonitor.h:136
CCB_COMPANY_LENGTH
constexpr uint8_t CCB_COMPANY_LENGTH
Number of bits of the company field.
Definition cargomonitor.h:45
company_func.h
Functions related to companies.
INVALID_OWNER
static constexpr Owner INVALID_OWNER
An invalid owner.
Definition company_type.h:30
industry.h
Base of all industries.
overflowsafe_type.hpp
An overflow safe integer-like type.
BaseStation::town
Town * town
The town this station is associated with.
Definition base_station_base.h:67
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
Source
A location from where cargo can come from (or go to).
Definition source_type.h:32
Station
Station data structure.
Definition station_base.h:500
town.h
Base of the town class.