Update: Translations from eints
[openttd-github.git] / src / cargomonitor.cpp
blobab8dc4a06f0ce25cb2bf10ad1cce8e02a54a656a
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 */
8 /** @file cargomonitor.cpp Implementation of the cargo transport monitoring. */
10 #include "stdafx.h"
11 #include "cargomonitor.h"
12 #include "station_base.h"
14 #include "safeguards.h"
16 CargoMonitorMap _cargo_pickups; ///< Map of monitored pick-ups to the amount since last query/activation.
17 CargoMonitorMap _cargo_deliveries; ///< Map of monitored deliveries to the amount since last query/activation.
19 /**
20 * Helper method for #ClearCargoPickupMonitoring and #ClearCargoDeliveryMonitoring.
21 * Clears all monitors that belong to the specified company or all if #INVALID_OWNER
22 * is specified as company.
23 * @param cargo_monitor_map reference to the cargo monitor map to operate on.
24 * @param company company to clear cargo monitors for or #INVALID_OWNER if all cargo monitors should be cleared.
26 static void ClearCargoMonitoring(CargoMonitorMap &cargo_monitor_map, CompanyID company = INVALID_OWNER)
28 if (company == INVALID_OWNER) {
29 cargo_monitor_map.clear();
30 return;
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;
42 /**
43 * Clear all pick-up cargo monitors.
44 * @param company clear all pick-up monitors for this company or if #INVALID_OWNER
45 * is passed, all pick-up monitors are cleared regardless of company.
47 void ClearCargoPickupMonitoring(CompanyID company)
49 ClearCargoMonitoring(_cargo_pickups, company);
52 /**
53 * Clear all delivery cargo monitors.
54 * @param company clear all delivery monitors for this company or if #INVALID_OWNER
55 * is passed, all delivery monitors are cleared regardless of company.
57 void ClearCargoDeliveryMonitoring(CompanyID company)
59 ClearCargoMonitoring(_cargo_deliveries, company);
62 /**
63 * Get and reset the amount associated with a cargo monitor.
64 * @param[in,out] monitor_map Monitoring map to search (and reset for the queried entry).
65 * @param monitor Cargo monitor to query/reset.
66 * @param keep_monitoring After returning from this call, continue monitoring.
67 * @return Amount collected since last query/activation for the monitored combination.
69 static int32_t GetAmount(CargoMonitorMap &monitor_map, CargoMonitorID monitor, bool keep_monitoring)
71 CargoMonitorMap::iterator iter = monitor_map.find(monitor);
72 if (iter == monitor_map.end()) {
73 if (keep_monitoring) {
74 monitor_map.emplace(monitor, 0);
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;
85 /**
86 * Get the amount of cargo delivered for the given cargo monitor since activation or last query.
87 * @param monitor Cargo monitor to query.
88 * @param keep_monitoring After returning from this call, continue monitoring.
89 * @return Amount of delivered cargo for the monitored combination.
91 int32_t GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring)
93 return GetAmount(_cargo_deliveries, monitor, keep_monitoring);
96 /**
97 * Get the amount of cargo picked up for the given cargo monitor since activation or last query.
98 * @param monitor Monitoring number to query.
99 * @param keep_monitoring After returning from this call, continue monitoring.
100 * @return Amount of picked up cargo for the monitored combination.
101 * @note Cargo pick up is counted on final delivery, to prevent users getting credit for picking up cargo without delivering it.
103 int32_t GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring)
105 return GetAmount(_cargo_pickups, monitor, keep_monitoring);
109 * Cargo was delivered to its final destination, update the pickup and delivery maps.
110 * @param cargo_type type of cargo.
111 * @param company company delivering the cargo.
112 * @param amount Amount of cargo delivered.
113 * @param src_type type of \a src.
114 * @param src index of source.
115 * @param st station where the cargo is delivered to.
116 * @param dest industry index where the cargo is delivered to.
118 void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32_t amount, SourceType src_type, SourceID src, const Station *st, IndustryID dest)
120 if (amount == 0) return;
122 if (src != INVALID_SOURCE) {
123 /* Handle pickup update. */
124 switch (src_type) {
125 case SourceType::Industry: {
126 CargoMonitorID num = EncodeCargoIndustryMonitor(company, cargo_type, src);
127 CargoMonitorMap::iterator iter = _cargo_pickups.find(num);
128 if (iter != _cargo_pickups.end()) iter->second += amount;
129 break;
131 case SourceType::Town: {
132 CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, src);
133 CargoMonitorMap::iterator iter = _cargo_pickups.find(num);
134 if (iter != _cargo_pickups.end()) iter->second += amount;
135 break;
137 default: break;
141 /* Handle delivery.
142 * Note that delivery in the right area is sufficient to prevent trouble with neighbouring industries or houses. */
144 /* Town delivery. */
145 CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, st->town->index);
146 CargoMonitorMap::iterator iter = _cargo_deliveries.find(num);
147 if (iter != _cargo_deliveries.end()) iter->second += amount;
149 /* Industry delivery. */
150 for (const auto &i : st->industries_near) {
151 if (i.industry->index != dest) continue;
152 CargoMonitorID num = EncodeCargoIndustryMonitor(company, cargo_type, i.industry->index);
153 CargoMonitorMap::iterator iter = _cargo_deliveries.find(num);
154 if (iter != _cargo_deliveries.end()) iter->second += amount;