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/>.
10 /** @file cargomonitor.cpp Implementation of the cargo transport monitoring. */
13 #include "cargomonitor.h"
14 #include "station_base.h"
16 #include "safeguards.h"
18 CargoMonitorMap _cargo_pickups
; ///< Map of monitored pick-ups to the amount since last query/activation.
19 CargoMonitorMap _cargo_deliveries
; ///< Map of monitored deliveries to the amount since last query/activation.
22 * Helper method for #ClearCargoPickupMonitoring and #ClearCargoDeliveryMonitoring.
23 * Clears all monitors that belong to the specified company or all if #INVALID_OWNER
24 * is specified as company.
25 * @param cargo_monitor_map reference to the cargo monitor map to operate on.
26 * @param company company to clear cargo monitors for or #INVALID_OWNER if all cargo monitors should be cleared.
28 static void ClearCargoMonitoring(CargoMonitorMap
&cargo_monitor_map
, CompanyID company
= INVALID_OWNER
)
30 if (company
== INVALID_OWNER
) {
31 cargo_monitor_map
.clear();
35 CargoMonitorMap::iterator next
;
36 for (CargoMonitorMap::iterator it
= cargo_monitor_map
.begin(); it
!= cargo_monitor_map
.end(); it
= next
) {
39 if (DecodeMonitorCompany(it
->first
) == company
) {
40 cargo_monitor_map
.erase(it
);
46 * Clear all pick-up cargo monitors.
47 * @param company clear all pick-up monitors for this company or if #INVALID_OWNER
48 * is passed, all pick-up monitors are cleared regardless of company.
50 void ClearCargoPickupMonitoring(CompanyID company
)
52 ClearCargoMonitoring(_cargo_pickups
, company
);
56 * Clear all delivery cargo monitors.
57 * @param company clear all delivery monitors for this company or if #INVALID_OWNER
58 * is passed, all delivery monitors are cleared regardless of company.
60 void ClearCargoDeliveryMonitoring(CompanyID company
)
62 ClearCargoMonitoring(_cargo_deliveries
, company
);
66 * Get and reset the amount associated with a cargo monitor.
67 * @param[in,out] monitor_map Monitoring map to search (and reset for the queried entry).
68 * @param monitor Cargo monitor to query/reset.
69 * @param keep_monitoring After returning from this call, continue monitoring.
70 * @return Amount collected since last query/activation for the monitored combination.
72 static int32
GetAmount(CargoMonitorMap
&monitor_map
, CargoMonitorID monitor
, bool keep_monitoring
)
74 CargoMonitorMap::iterator iter
= monitor_map
.find(monitor
);
75 if (iter
== monitor_map
.end()) {
76 if (keep_monitoring
) {
77 std::pair
<CargoMonitorID
, uint32
> p(monitor
, 0);
78 monitor_map
.insert(p
);
82 int32 result
= iter
->second
;
84 if (!keep_monitoring
) monitor_map
.erase(iter
);
90 * Get the amount of cargo delivered for the given cargo monitor since activation or last query.
91 * @param monitor Cargo monitor to query.
92 * @param keep_monitoring After returning from this call, continue monitoring.
93 * @return Amount of delivered cargo for the monitored combination.
95 int32
GetDeliveryAmount(CargoMonitorID monitor
, bool keep_monitoring
)
97 return GetAmount(_cargo_deliveries
, monitor
, keep_monitoring
);
101 * Get the amount of cargo picked up for the given cargo monitor since activation or last query.
102 * @param monitor Monitoring number to query.
103 * @param keep_monitoring After returning from this call, continue monitoring.
104 * @return Amount of picked up cargo for the monitored combination.
105 * @note Cargo pick up is counted on final delivery, to prevent users getting credit for picking up cargo without delivering it.
107 int32
GetPickupAmount(CargoMonitorID monitor
, bool keep_monitoring
)
109 return GetAmount(_cargo_pickups
, monitor
, keep_monitoring
);
113 * Cargo was delivered to its final destination, update the pickup and delivery maps.
114 * @param cargo_type type of cargo.
115 * @param company company delivering the cargo.
116 * @param amount Amount of cargo delivered.
117 * @param src_type type of \a src.
118 * @param src index of source.
119 * @param st station where the cargo is delivered to.
121 void AddCargoDelivery(CargoID cargo_type
, CompanyID company
, uint32 amount
, SourceType src_type
, SourceID src
, const Station
*st
)
123 if (amount
== 0) return;
125 if (src
!= INVALID_SOURCE
) {
126 /* Handle pickup update. */
129 CargoMonitorID num
= EncodeCargoIndustryMonitor(company
, cargo_type
, src
);
130 CargoMonitorMap::iterator iter
= _cargo_pickups
.find(num
);
131 if (iter
!= _cargo_pickups
.end()) iter
->second
+= amount
;
135 CargoMonitorID num
= EncodeCargoTownMonitor(company
, cargo_type
, src
);
136 CargoMonitorMap::iterator iter
= _cargo_pickups
.find(num
);
137 if (iter
!= _cargo_pickups
.end()) iter
->second
+= amount
;
145 * Note that delivery in the right area is sufficient to prevent trouble with neighbouring industries or houses. */
148 CargoMonitorID num
= EncodeCargoTownMonitor(company
, cargo_type
, st
->town
->index
);
149 CargoMonitorMap::iterator iter
= _cargo_deliveries
.find(num
);
150 if (iter
!= _cargo_deliveries
.end()) iter
->second
+= amount
;
152 /* Industry delivery. */
153 for (const Industry
* const *ip
= st
->industries_near
.Begin(); ip
!= st
->industries_near
.End(); ip
++) {
154 CargoMonitorID num
= EncodeCargoIndustryMonitor(company
, cargo_type
, (*ip
)->index
);
155 CargoMonitorMap::iterator iter
= _cargo_deliveries
.find(num
);
156 if (iter
!= _cargo_deliveries
.end()) iter
->second
+= amount
;