OpenTTD Source: src/cargomonitor.cpp Source File

OpenTTD Source 13.2.1

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
16 CargoMonitorMap _cargo_pickups;
17 CargoMonitorMap _cargo_deliveries;
18
26 static 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 CargoMonitorMap::iterator next;
34 for (CargoMonitorMap::iterator it = cargo_monitor_map.begin(); it != cargo_monitor_map.end(); it = next) {
35 next = it;
36 next++;
37 if (DecodeMonitorCompany(it->first) == company) {
38 cargo_monitor_map.erase(it);
39 }
40 }
41 }
42
48 void ClearCargoPickupMonitoring(CompanyID company)
49 {
50 ClearCargoMonitoring(_cargo_pickups, company);
51 }
52
58 void ClearCargoDeliveryMonitoring(CompanyID company)
59 {
60 ClearCargoMonitoring(_cargo_deliveries, company);
61 }
62
70 static int32 GetAmount(CargoMonitorMap &monitor_map, CargoMonitorID monitor, bool keep_monitoring)
71 {
72 CargoMonitorMap::iterator iter = monitor_map.find(monitor);
73 if (iter == monitor_map.end()) {
74 if (keep_monitoring) {
75 std::pair<CargoMonitorID, uint32> p(monitor, 0);
76 monitor_map.insert(p);
77 }
78 return 0;
79 } else {
80 int32 result = iter->second;
81 iter->second = 0;
82 if (!keep_monitoring) monitor_map.erase(iter);
83 return result;
84 }
85 }
86
93 int32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring)
94 {
95 return GetAmount(_cargo_deliveries, monitor, keep_monitoring);
96 }
97
105 int32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring)
106 {
107 return GetAmount(_cargo_pickups, monitor, keep_monitoring);
108 }
109
120 void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 amount, SourceType src_type, SourceID src, const Station *st, IndustryID dest)
121 {
122 if (amount == 0) return;
123
124 if (src != INVALID_SOURCE) {
125 /* Handle pickup update. */
126 switch (src_type) {
127 case ST_INDUSTRY: {
128 CargoMonitorID num = EncodeCargoIndustryMonitor(company, cargo_type, src);
129 CargoMonitorMap::iterator iter = _cargo_pickups.find(num);
130 if (iter != _cargo_pickups.end()) iter->second += amount;
131 break;
132 }
133 case ST_TOWN: {
134 CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, src);
135 CargoMonitorMap::iterator iter = _cargo_pickups.find(num);
136 if (iter != _cargo_pickups.end()) iter->second += amount;
137 break;
138 }
139 default: break;
140 }
141 }
142
143 /* Handle delivery.
144 * Note that delivery in the right area is sufficient to prevent trouble with neighbouring industries or houses. */
145
146 /* Town delivery. */
147 CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, st->town->index);
148 CargoMonitorMap::iterator iter = _cargo_deliveries.find(num);
149 if (iter != _cargo_deliveries.end()) iter->second += amount;
150
151 /* Industry delivery. */
152 for (const auto &i : st->industries_near) {
153 if (i.industry->index != dest) continue;
154 CargoMonitorID num = EncodeCargoIndustryMonitor(company, cargo_type, i.industry->index);
155 CargoMonitorMap::iterator iter = _cargo_deliveries.find(num);
156 if (iter != _cargo_deliveries.end()) iter->second += amount;
157 }
158 }
159
EncodeCargoTownMonitor
static CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
Encode a cargo monitoring number for pickup or delivery at a town.
Definition: cargomonitor.h:82
EncodeCargoIndustryMonitor
static CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
Encode a cargo monitor for pickup or delivery at an industry.
Definition: cargomonitor.h:62
CargoMonitorMap
std::map< CargoMonitorID, OverflowSafeInt32 > CargoMonitorMap
Map type for storing and updating active cargo monitor numbers and their amounts.
Definition: cargomonitor.h:33
BaseStation::town
Town * town
The town this station is associated with.
Definition: base_station_base.h:61
Station
Station data structure.
Definition: station_base.h:454
ClearCargoDeliveryMonitoring
void ClearCargoDeliveryMonitoring(CompanyID company)
Clear all delivery cargo monitors.
Definition: cargomonitor.cpp:58
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
CargoMonitorID
uint32 CargoMonitorID
Unique number for a company / cargo type / (town or industry).
Definition: cargomonitor.h:20
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
ST_TOWN
@ ST_TOWN
Source/destination is a town.
Definition: cargo_type.h:149
SourceID
uint16 SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:153
GetPickupAmount
int32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring)
Get the amount of cargo picked up for the given cargo monitor since activation or last query.
Definition: cargomonitor.cpp:105
INVALID_OWNER
@ INVALID_OWNER
An invalid owner.
Definition: company_type.h:29
DecodeMonitorCompany
static CompanyID DecodeMonitorCompany(CargoMonitorID num)
Extract the company from the cargo monitor.
Definition: cargomonitor.h:99
ST_INDUSTRY
@ ST_INDUSTRY
Source/destination is an industry.
Definition: cargo_type.h:148
_cargo_pickups
CargoMonitorMap _cargo_pickups
Map of monitored pick-ups to the amount since last query/activation.
Definition: cargomonitor.cpp:16
safeguards.h
stdafx.h
_cargo_deliveries
CargoMonitorMap _cargo_deliveries
Map of monitored deliveries to the amount since last query/activation.
Definition: cargomonitor.cpp:17
Station::industries_near
IndustryList industries_near
Cached list of industries near the station that can accept cargo,.
Definition: station_base.h:486
station_base.h
SourceType
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:147
AddCargoDelivery
void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 amount, SourceType src_type, SourceID src, const Station *st, IndustryID dest)
Cargo was delivered to its final destination, update the pickup and delivery maps.
Definition: cargomonitor.cpp:120
ClearCargoMonitoring
static void ClearCargoMonitoring(CargoMonitorMap &cargo_monitor_map, CompanyID company=INVALID_OWNER)
Helper method for ClearCargoPickupMonitoring and ClearCargoDeliveryMonitoring.
Definition: cargomonitor.cpp:26
ClearCargoPickupMonitoring
void ClearCargoPickupMonitoring(CompanyID company)
Clear all pick-up cargo monitors.
Definition: cargomonitor.cpp:48
INVALID_SOURCE
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:154
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
GetAmount
static int32 GetAmount(CargoMonitorMap &monitor_map, CargoMonitorID monitor, bool keep_monitoring)
Get and reset the amount associated with a cargo monitor.
Definition: cargomonitor.cpp:70
GetDeliveryAmount
int32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring)
Get the amount of cargo delivered for the given cargo monitor since activation or last query.
Definition: cargomonitor.cpp:93
cargomonitor.h