OpenTTD Source 20261112-master-gbcbec5e63f
cargomonitor.cpp
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#include "stdafx.h"
11#include "cargomonitor.h"
12#include "industry.h"
13#include "station_base.h"
14#include "town.h"
15
16#include "safeguards.h"
17
20
28static void ClearCargoMonitoring(CargoMonitorMap &cargo_monitor_map, CompanyID company = INVALID_OWNER)
29{
30 if (company == INVALID_OWNER) {
31 cargo_monitor_map.clear();
32 return;
33 }
34
35 for (auto it = cargo_monitor_map.begin(); it != cargo_monitor_map.end(); /* nothing */) {
36 if (DecodeMonitorCompany(it->first) == company) {
37 it = cargo_monitor_map.erase(it);
38 } else {
39 ++it;
40 }
41 }
42}
43
49void ClearCargoPickupMonitoring(CompanyID company)
50{
52}
53
59void ClearCargoDeliveryMonitoring(CompanyID company)
60{
62}
63
71static int32_t GetAmount(CargoMonitorMap &monitor_map, CargoMonitorID monitor, bool keep_monitoring)
72{
73 CargoMonitorMap::iterator iter = monitor_map.find(monitor);
74 if (iter == monitor_map.end()) {
75 if (keep_monitoring) {
76 monitor_map.emplace(monitor, 0);
77 }
78 return 0;
79 } else {
80 int32_t result = iter->second;
81 iter->second = 0;
82 if (!keep_monitoring) monitor_map.erase(iter);
83 return result;
84 }
85}
86
93int32_t GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring)
94{
95 return GetAmount(_cargo_deliveries, monitor, keep_monitoring);
96}
97
105int32_t GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring)
106{
107 return GetAmount(_cargo_pickups, monitor, keep_monitoring);
108}
109
119void AddCargoDelivery(CargoType cargo_type, CompanyID company, uint32_t amount, Source src, const Station *st, IndustryID dest)
120{
121 if (amount == 0) return;
122
123 if (src.IsValid()) {
124 /* Handle pickup update. */
125 switch (src.type) {
127 CargoMonitorID num = EncodeCargoIndustryMonitor(company, cargo_type, src.ToIndustryID());
128 CargoMonitorMap::iterator iter = _cargo_pickups.find(num);
129 if (iter != _cargo_pickups.end()) iter->second += amount;
130 break;
131 }
132 case SourceType::Town: {
133 CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, src.ToTownID());
134 CargoMonitorMap::iterator iter = _cargo_pickups.find(num);
135 if (iter != _cargo_pickups.end()) iter->second += amount;
136 break;
137 }
138 default: break;
139 }
140 }
141
142 /* Handle delivery.
143 * Note that delivery in the right area is sufficient to prevent trouble with neighbouring industries or houses. */
144
145 /* Town delivery. */
146 CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, st->town->index);
147 CargoMonitorMap::iterator iter = _cargo_deliveries.find(num);
148 if (iter != _cargo_deliveries.end()) iter->second += amount;
149
150 /* Industry delivery. */
151 for (const auto &i : st->industries_near) {
152 if (i.industry->index != dest) continue;
153 CargoMonitorID num = EncodeCargoIndustryMonitor(company, cargo_type, i.industry->index);
154 CargoMonitorMap::iterator iter = _cargo_deliveries.find(num);
155 if (iter != _cargo_deliveries.end()) iter->second += amount;
156 }
157}
158
CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
void ClearCargoDeliveryMonitoring(CompanyID company)
Clear all delivery cargo monitors.
static void ClearCargoMonitoring(CargoMonitorMap &cargo_monitor_map, CompanyID company=INVALID_OWNER)
Helper method for ClearCargoPickupMonitoring and ClearCargoDeliveryMonitoring.
CargoMonitorMap _cargo_deliveries
Map of monitored deliveries to the amount since last query/activation.
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.
void ClearCargoPickupMonitoring(CompanyID company)
Clear all pick-up cargo monitors.
static int32_t GetAmount(CargoMonitorMap &monitor_map, CargoMonitorID monitor, bool keep_monitoring)
Get and reset the amount associated with a cargo monitor.
void AddCargoDelivery(CargoType cargo_type, CompanyID company, uint32_t amount, Source src, const Station *st, IndustryID dest)
Cargo was delivered to its final destination, update the pickup and delivery maps.
Cargo transport monitoring declarations.
CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoType ctype, IndustryID ind)
Encode a cargo monitor for pickup or delivery at an industry.
uint32_t CargoMonitorID
Unique number for a company / cargo type / (town or industry).
CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoType ctype, TownID town)
Encode a cargo monitoring number for pickup or delivery at a town.
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.
static constexpr Owner INVALID_OWNER
An invalid owner.
Base of all industries.
A number of safeguards to prevent using unsafe methods.
@ Industry
Source/destination is an industry.
Definition source_type.h:21
@ Town
Source/destination is a town.
Definition source_type.h:22
Base classes/functions for stations.
Definition of base types and functions in a cross-platform compatible way.
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
SourceType type
Type of source_id.
Definition source_type.h:37
Station data structure.
IndustryList industries_near
Cached list of industries near the station that can accept cargo,.
Base of the town class.