OpenTTD Source: src/cargomonitor.h Source File

OpenTTD Source 12.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 #include <map>
19
20 struct Station;
21
30 typedef uint32 CargoMonitorID;
31
33 typedef std::map<CargoMonitorID, OverflowSafeInt32> CargoMonitorMap;
34
35 extern CargoMonitorMap _cargo_pickups;
36 extern CargoMonitorMap _cargo_deliveries;
37
38
40 enum CargoCompanyBits {
41 CCB_TOWN_IND_NUMBER_START = 0,
42 CCB_TOWN_IND_NUMBER_LENGTH = 16,
43 CCB_IS_INDUSTRY_BIT = 16,
44 CCB_IS_INDUSTRY_BIT_VALUE = 1ul << CCB_IS_INDUSTRY_BIT,
45 CCB_CARGO_TYPE_START = 19,
46 CCB_CARGO_TYPE_LENGTH = 6,
47 CCB_COMPANY_START = 25,
48 CCB_COMPANY_LENGTH = 4,
49 };
50
51 static_assert(NUM_CARGO <= (1 << CCB_CARGO_TYPE_LENGTH));
52 static_assert(MAX_COMPANIES <= (1 << CCB_COMPANY_LENGTH));
53
54
62 static inline CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
63 {
64 assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
65 assert(company < (1 << CCB_COMPANY_LENGTH));
66
67 uint32 ret = 0;
68 SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, ind);
69 SetBit(ret, CCB_IS_INDUSTRY_BIT);
70 SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
71 SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company);
72 return ret;
73 }
74
82 static inline CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
83 {
84 assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
85 assert(company < (1 << CCB_COMPANY_LENGTH));
86
87 uint32 ret = 0;
88 SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, town);
89 SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
90 SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company);
91 return ret;
92 }
93
99 static inline CompanyID DecodeMonitorCompany(CargoMonitorID num)
100 {
101 return static_cast<CompanyID>(GB(num, CCB_COMPANY_START, CCB_COMPANY_LENGTH));
102 }
103
109 static inline CargoID DecodeMonitorCargoType(CargoMonitorID num)
110 {
111 return GB(num, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH);
112 }
113
119 static inline bool MonitorMonitorsIndustry(CargoMonitorID num)
120 {
121 return HasBit(num, CCB_IS_INDUSTRY_BIT);
122 }
123
129 static inline IndustryID DecodeMonitorIndustry(CargoMonitorID num)
130 {
131 if (!MonitorMonitorsIndustry(num)) return INVALID_INDUSTRY;
132 return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH);
133 }
134
140 static inline TownID DecodeMonitorTown(CargoMonitorID num)
141 {
142 if (MonitorMonitorsIndustry(num)) return INVALID_TOWN;
143 return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH);
144 }
145
146 void ClearCargoPickupMonitoring(CompanyID company = INVALID_OWNER);
147 void ClearCargoDeliveryMonitoring(CompanyID company = INVALID_OWNER);
148 int32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring);
149 int32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring);
150 void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 amount, SourceType src_type, SourceID src, const Station *st, IndustryID dest = INVALID_INDUSTRY);
151
152 #endif /* CARGOMONITOR_H */
EncodeCargoTownMonitor
static CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
Encode a cargo monitoring number for pickup or delivery at a town.
Definition: cargomonitor.h:82
EncodeCargoIndustryMonitor
static CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
Encode a cargo monitor for pickup or delivery at an industry.
Definition: cargomonitor.h:62
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
CargoMonitorMap
std::map< CargoMonitorID, OverflowSafeInt32 > CargoMonitorMap
Map type for storing and updating active cargo monitor numbers and their amounts.
Definition: cargomonitor.h:33
BaseStation::town
Town * town
The town this station is associated with.
Definition: base_station_base.h:61
Station
Station data structure.
Definition: station_base.h:447
CCB_COMPANY_LENGTH
@ CCB_COMPANY_LENGTH
Number of bits of the company field.
Definition: cargomonitor.h:48
_cargo_deliveries
CargoMonitorMap _cargo_deliveries
Map of monitored deliveries to the amount since last query/activation.
Definition: cargomonitor.cpp:17
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
overflowsafe_type.hpp
town.h
DecodeMonitorIndustry
static IndustryID DecodeMonitorIndustry(CargoMonitorID num)
Extract the industry number from the cargo monitor.
Definition: cargomonitor.h:129
CargoMonitorID
uint32 CargoMonitorID
Unique number for a company / cargo type / (town or industry).
Definition: cargomonitor.h:20
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:42
GetDeliveryAmount
int32 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:93
SourceID
uint16 SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:153
CCB_CARGO_TYPE_LENGTH
@ CCB_CARGO_TYPE_LENGTH
Number of bits of the cargo type field.
Definition: cargomonitor.h:46
SB
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
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:48
DecodeMonitorCompany
static CompanyID DecodeMonitorCompany(CargoMonitorID num)
Extract the company from the cargo monitor.
Definition: cargomonitor.h:99
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:43
CCB_TOWN_IND_NUMBER_START
@ CCB_TOWN_IND_NUMBER_START
Start bit of the town or industry number.
Definition: cargomonitor.h:41
CCB_CARGO_TYPE_START
@ CCB_CARGO_TYPE_START
Start bit of the cargo type field.
Definition: cargomonitor.h:45
DecodeMonitorTown
static TownID DecodeMonitorTown(CargoMonitorID num)
Extract the town number from the cargo monitor.
Definition: cargomonitor.h:140
_cargo_pickups
CargoMonitorMap _cargo_pickups
Map of monitored pick-ups to the amount since last query/activation.
Definition: cargomonitor.cpp:16
SourceType
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:147
MonitorMonitorsIndustry
static bool MonitorMonitorsIndustry(CargoMonitorID num)
Does the cargo number monitor an industry or a town?
Definition: cargomonitor.h:119
CCB_IS_INDUSTRY_BIT_VALUE
@ CCB_IS_INDUSTRY_BIT_VALUE
Value of the CCB_IS_INDUSTRY_BIT bit.
Definition: cargomonitor.h:44
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:65
company_func.h
DecodeMonitorCargoType
static CargoID DecodeMonitorCargoType(CargoMonitorID num)
Extract the cargo type from the cargo monitor.
Definition: cargomonitor.h:109
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
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:47
AddCargoDelivery
void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 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:120
ClearCargoDeliveryMonitoring
void ClearCargoDeliveryMonitoring(CompanyID company=INVALID_OWNER)
Clear all delivery cargo monitors.
Definition: cargomonitor.cpp:58
GetPickupAmount
int32 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:105
CargoCompanyBits
CargoCompanyBits
Constants for encoding and extracting cargo monitors.
Definition: cargomonitor.h:40