OpenTTD
cargomonitor.h
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef CARGOMONITOR_H
13 #define CARGOMONITOR_H
14 
15 #include "cargo_type.h"
16 #include "company_func.h"
17 #include "industry.h"
18 #include "town.h"
20 #include <map>
21 
22 struct Station;
23 
32 typedef uint32 CargoMonitorID;
33 
35 typedef std::map<CargoMonitorID, OverflowSafeInt32> CargoMonitorMap;
36 
39 
40 
51 };
52 
53 assert_compile(NUM_CARGO <= (1 << CCB_CARGO_TYPE_LENGTH));
54 assert_compile(MAX_COMPANIES <= (1 << CCB_COMPANY_LENGTH));
55 
56 
64 static inline CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
65 {
66  assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
67  assert(company < (1 << CCB_COMPANY_LENGTH));
68 
69  uint32 ret = 0;
73  SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company);
74  return ret;
75 }
76 
84 static inline CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
85 {
86  assert(ctype < (1 << CCB_CARGO_TYPE_LENGTH));
87  assert(company < (1 << CCB_COMPANY_LENGTH));
88 
89  uint32 ret = 0;
92  SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company);
93  return ret;
94 }
95 
102 {
103  return static_cast<CompanyID>(GB(num, CCB_COMPANY_START, CCB_COMPANY_LENGTH));
104 }
105 
112 {
114 }
115 
122 {
123  return HasBit(num, CCB_IS_INDUSTRY_BIT);
124 }
125 
131 static inline IndustryID DecodeMonitorIndustry(CargoMonitorID num)
132 {
133  if (!MonitorMonitorsIndustry(num)) return INVALID_INDUSTRY;
135 }
136 
142 static inline TownID DecodeMonitorTown(CargoMonitorID num)
143 {
144  if (MonitorMonitorsIndustry(num)) return INVALID_TOWN;
146 }
147 
150 int32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring);
151 int32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring);
152 void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 amount, SourceType src_type, SourceID src, const Station *st, IndustryID dest = INVALID_INDUSTRY);
153 
154 #endif /* CARGOMONITOR_H */
Start bit of the cargo type field.
Definition: cargomonitor.h:47
CargoMonitorMap _cargo_pickups
Map of monitored pick-ups to the amount since last query/activation.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
static bool MonitorMonitorsIndustry(CargoMonitorID num)
Does the cargo number monitor an industry or a town?
Definition: cargomonitor.h:121
Start bit of the company field.
Definition: cargomonitor.h:49
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
An overflow safe integer-like type.
static CargoID DecodeMonitorCargoType(CargoMonitorID num)
Extract the cargo type from the cargo monitor.
Definition: cargomonitor.h:111
Town * town
The town this station is associated with.
Types related to cargoes...
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.
Bit indicating the town/industry number is an industry.
Definition: cargomonitor.h:45
Value of the CCB_IS_INDUSTRY_BIT bit.
Definition: cargomonitor.h:46
CargoCompanyBits
Constants for encoding and extracting cargo monitors.
Definition: cargomonitor.h:42
std::map< CargoMonitorID, OverflowSafeInt32 > CargoMonitorMap
Map type for storing and updating active cargo monitor numbers and their amounts. ...
Definition: cargomonitor.h:35
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.
static CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
Encode a cargo monitor for pickup or delivery at an industry.
Definition: cargomonitor.h:64
CargoMonitorMap _cargo_deliveries
Map of monitored deliveries to the amount since last query/activation.
Number of bits of the cargo type field.
Definition: cargomonitor.h:48
void ClearCargoDeliveryMonitoring(CompanyID company=INVALID_OWNER)
Clear all delivery cargo monitors.
int32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring)
Get the amount of cargo picked up for the given cargo monitor since activation or last query...
Maximum number of companies.
Definition: company_type.h:25
uint32 CargoMonitorID
Unique number for a company / cargo type / (town or industry).
Definition: cargomonitor.h:22
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:148
static CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
Encode a cargo monitoring number for pickup or delivery at a town.
Definition: cargomonitor.h:84
Start bit of the town or industry number.
Definition: cargomonitor.h:43
Functions related to companies.
static IndustryID DecodeMonitorIndustry(CargoMonitorID num)
Extract the industry number from the cargo monitor.
Definition: cargomonitor.h:131
uint16 SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:155
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Number of bits of the town or industry number.
Definition: cargomonitor.h:44
static CompanyID DecodeMonitorCompany(CargoMonitorID num)
Extract the company from the cargo monitor.
Definition: cargomonitor.h:101
Number of bits of the company field.
Definition: cargomonitor.h:50
Base of all industries.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Base of the town class.
int32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring)
Get the amount of cargo delivered for the given cargo monitor since activation or last query...
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
Owner
Enum for all companies/owners.
Definition: company_type.h:20
static TownID DecodeMonitorTown(CargoMonitorID num)
Extract the town number from the cargo monitor.
Definition: cargomonitor.h:142
Station data structure.
Definition: station_base.h:446
An invalid owner.
Definition: company_type.h:31
void ClearCargoPickupMonitoring(CompanyID company=INVALID_OWNER)
Clear all pick-up cargo monitors.