OpenTTD: src/cargomonitor.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

cargomonitor.cpp
Go to the documentation of this file.
1 /* $Id: cargomonitor.cpp 26685 2014-07-12 17:04:14Z alberth $ */
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 #include "stdafx.h"
13 #include "cargomonitor.h"
14 #include "station_base.h"
15
16 #include "safeguards.h"
17
18 CargoMonitorMap _cargo_pickups;
19 CargoMonitorMap _cargo_deliveries;
20
28 static 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 CargoMonitorMap::iterator next;
36 for (CargoMonitorMap::iterator it = cargo_monitor_map.begin(); it != cargo_monitor_map.end(); it = next) {
37 next = it;
38 next++;
39 if (DecodeMonitorCompany(it->first) == company) {
40 cargo_monitor_map.erase(it);
41 }
42 }
43 }
44
50 void ClearCargoPickupMonitoring(CompanyID company)
51 {
52 ClearCargoMonitoring(_cargo_pickups, company);
53 }
54
60 void ClearCargoDeliveryMonitoring(CompanyID company)
61 {
62 ClearCargoMonitoring(_cargo_deliveries, company);
63 }
64
72 static int32 GetAmount(CargoMonitorMap &monitor_map, CargoMonitorID monitor, bool keep_monitoring)
73 {
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);
79 }
80 return 0;
81 } else {
82 int32 result = iter->second;
83 iter->second = 0;
84 if (!keep_monitoring) monitor_map.erase(iter);
85 return result;
86 }
87 }
88
95 int32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring)
96 {
97 return GetAmount(_cargo_deliveries, monitor, keep_monitoring);
98 }
99
107 int32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring)
108 {
109 return GetAmount(_cargo_pickups, monitor, keep_monitoring);
110 }
111
121 void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 amount, SourceType src_type, SourceID src, const Station *st)
122 {
123 if (amount == 0) return;
124
125 if (src != INVALID_SOURCE) {
126 /* Handle pickup update. */
127 switch (src_type) {
128 case ST_INDUSTRY: {
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;
132 break;
133 }
134 case ST_TOWN: {
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;
138 break;
139 }
140 default: break;
141 }
142 }
143
144 /* Handle delivery.
145 * Note that delivery in the right area is sufficient to prevent trouble with neighbouring industries or houses. */
146
147 /* Town delivery. */
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;
151
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;
157 }
158 }
159