OpenTTD Source 20250312-master-gcdcc6b491d
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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "stdafx.h"
11#include "cargomonitor.h"
12#include "station_base.h"
13
14#include "safeguards.h"
15
18
26static void ClearCargoMonitoring(CargoMonitorMap &cargo_monitor_map, CompanyID company = INVALID_OWNER)
27{
28 if (company == INVALID_OWNER) {
29 cargo_monitor_map.clear();
30 return;
31 }
32
33 for (auto it = cargo_monitor_map.begin(); it != cargo_monitor_map.end(); /* nothing */) {
34 if (DecodeMonitorCompany(it->first) == company) {
35 it = cargo_monitor_map.erase(it);
36 } else {
37 ++it;
38 }
39 }
40}
41
51
61
69static int32_t GetAmount(CargoMonitorMap &monitor_map, CargoMonitorID monitor, bool keep_monitoring)
70{
71 CargoMonitorMap::iterator iter = monitor_map.find(monitor);
72 if (iter == monitor_map.end()) {
73 if (keep_monitoring) {
74 monitor_map.emplace(monitor, 0);
75 }
76 return 0;
77 } else {
78 int32_t result = iter->second;
79 iter->second = 0;
80 if (!keep_monitoring) monitor_map.erase(iter);
81 return result;
82 }
83}
84
91int32_t GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring)
92{
93 return GetAmount(_cargo_deliveries, monitor, keep_monitoring);
94}
95
103int32_t GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring)
104{
105 return GetAmount(_cargo_pickups, monitor, keep_monitoring);
106}
107
117void AddCargoDelivery(CargoType cargo_type, CompanyID company, uint32_t amount, Source src, const Station *st, IndustryID dest)
118{
119 if (amount == 0) return;
120
121 if (src.IsValid()) {
122 /* Handle pickup update. */
123 switch (src.type) {
125 CargoMonitorID num = EncodeCargoIndustryMonitor(company, cargo_type, src.ToIndustryID());
126 CargoMonitorMap::iterator iter = _cargo_pickups.find(num);
127 if (iter != _cargo_pickups.end()) iter->second += amount;
128 break;
129 }
130 case SourceType::Town: {
131 CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, src.ToTownID());
132 CargoMonitorMap::iterator iter = _cargo_pickups.find(num);
133 if (iter != _cargo_pickups.end()) iter->second += amount;
134 break;
135 }
136 default: break;
137 }
138 }
139
140 /* Handle delivery.
141 * Note that delivery in the right area is sufficient to prevent trouble with neighbouring industries or houses. */
142
143 /* Town delivery. */
144 CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, st->town->index);
145 CargoMonitorMap::iterator iter = _cargo_deliveries.find(num);
146 if (iter != _cargo_deliveries.end()) iter->second += amount;
147
148 /* Industry delivery. */
149 for (const auto &i : st->industries_near) {
150 if (i.industry->index != dest) continue;
151 CargoMonitorID num = EncodeCargoIndustryMonitor(company, cargo_type, i.industry->index);
152 CargoMonitorMap::iterator iter = _cargo_deliveries.find(num);
153 if (iter != _cargo_deliveries.end()) iter->second += amount;
154 }
155}
156
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:23
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.
A number of safeguards to prevent using unsafe methods.
@ Industry
Source/destination is an industry.
@ Town
Source/destination is a town.
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.
Tindex index
Index of this pool item.
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,.