OpenTTD
cargomonitor.cpp
Go to the documentation of this file.
1 /* $Id$ */
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 
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 
51 {
53 }
54 
61 {
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 
122 void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 amount, SourceType src_type, SourceID src, const Station *st, IndustryID dest)
123 {
124  if (amount == 0) return;
125 
126  if (src != INVALID_SOURCE) {
127  /* Handle pickup update. */
128  switch (src_type) {
129  case ST_INDUSTRY: {
130  CargoMonitorID num = EncodeCargoIndustryMonitor(company, cargo_type, src);
131  CargoMonitorMap::iterator iter = _cargo_pickups.find(num);
132  if (iter != _cargo_pickups.end()) iter->second += amount;
133  break;
134  }
135  case ST_TOWN: {
136  CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, src);
137  CargoMonitorMap::iterator iter = _cargo_pickups.find(num);
138  if (iter != _cargo_pickups.end()) iter->second += amount;
139  break;
140  }
141  default: break;
142  }
143  }
144 
145  /* Handle delivery.
146  * Note that delivery in the right area is sufficient to prevent trouble with neighbouring industries or houses. */
147 
148  /* Town delivery. */
149  CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, st->town->index);
150  CargoMonitorMap::iterator iter = _cargo_deliveries.find(num);
151  if (iter != _cargo_deliveries.end()) iter->second += amount;
152 
153  /* Industry delivery. */
154  for (const Industry * const *ip = st->industries_near.Begin(); ip != st->industries_near.End(); ip++) {
155  if ((*ip)->index != dest) continue;
156  CargoMonitorID num = EncodeCargoIndustryMonitor(company, cargo_type, (*ip)->index);
157  CargoMonitorMap::iterator iter = _cargo_deliveries.find(num);
158  if (iter != _cargo_deliveries.end()) iter->second += amount;
159  }
160 }
161 
Source/destination is a town.
Definition: cargo_type.h:150
IndustryVector industries_near
Cached list of industries near the station that can accept cargo,.
Definition: station_base.h:475
static void ClearCargoMonitoring(CargoMonitorMap &cargo_monitor_map, CompanyID company=INVALID_OWNER)
Helper method for ClearCargoPickupMonitoring and ClearCargoDeliveryMonitoring.
Defines the internal data of a functional industry.
Definition: industry.h:41
const T * Begin() const
Get the pointer to the first item (const)
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Town * town
The town this station is associated with.
const T * End() const
Get the pointer behind the last valid item (const)
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:156
CargoMonitorMap _cargo_deliveries
Map of monitored deliveries to the amount since last query/activation.
static int32 GetAmount(CargoMonitorMap &monitor_map, CargoMonitorID monitor, bool keep_monitoring)
Get and reset the amount associated with a cargo monitor.
std::map< CargoMonitorID, OverflowSafeInt32 > CargoMonitorMap
Map type for storing and updating active cargo monitor numbers and their amounts. ...
Definition: cargomonitor.h:35
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
static CargoMonitorID EncodeCargoIndustryMonitor(CompanyID company, CargoID ctype, IndustryID ind)
Encode a cargo monitor for pickup or delivery at an industry.
Definition: cargomonitor.h:64
void ClearCargoPickupMonitoring(CompanyID company)
Clear all pick-up cargo monitors.
void ClearCargoDeliveryMonitoring(CompanyID company)
Clear all delivery cargo monitors.
uint32 CargoMonitorID
Unique number for a company / cargo type / (town or industry).
Definition: cargomonitor.h:22
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:148
static CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoID ctype, TownID town)
Encode a cargo monitoring number for pickup or delivery at a town.
Definition: cargomonitor.h:84
CargoMonitorMap _cargo_pickups
Map of monitored pick-ups to the amount since last query/activation.
Source/destination is an industry.
Definition: cargo_type.h:149
uint16 SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:155
Cargo transport monitoring declarations.
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.
static CompanyID DecodeMonitorCompany(CargoMonitorID num)
Extract the company from the cargo monitor.
Definition: cargomonitor.h:101
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
Owner
Enum for all companies/owners.
Definition: company_type.h:20
Base classes/functions for stations.
int32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring)
Get the amount of cargo picked up for the given cargo monitor since activation or last query...
int32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring)
Get the amount of cargo delivered for the given cargo monitor since activation or last query...
Station data structure.
Definition: station_base.h:446
An invalid owner.
Definition: company_type.h:31