# cargomonitor.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "cargomonitor.h"
    00014 #include "station_base.h"
    00015
    00016 CargoMonitorMap _cargo_pickups;
    00017 CargoMonitorMap _cargo_deliveries;
    00018
    00020 void ClearCargoPickupMonitoring()
    00021 {
    00022   _cargo_pickups.clear();
    00023 }
    00024
    00026 void ClearCargoDeliveryMonitoring()
    00027 {
    00028   _cargo_deliveries.clear();
    00029 }
    00030
    00038 static uint32 GetAmount(CargoMonitorMap &monitor_map, CargoMonitorID monitor, bool keep_monitoring)
    00039 {
    00040   CargoMonitorMap::iterator iter = monitor_map.find(monitor);
    00041   if (iter == monitor_map.end()) {
    00042     if (keep_monitoring) {
    00043       std::pair<CargoMonitorID, uint32> p(monitor, 0);
    00044       monitor_map.insert(p);
    00045     }
    00046     return 0;
    00047   } else {
    00048     uint32 result = iter->second;
    00049     iter->second = 0;
    00050     if (!keep_monitoring) monitor_map.erase(iter);
    00051     return result;
    00052   }
    00053 }
    00054
    00061 uint32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring)
    00062 {
    00063   return GetAmount(_cargo_deliveries, monitor, keep_monitoring);
    00064 }
    00065
    00073 uint32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring)
    00074 {
    00075   return GetAmount(_cargo_pickups, monitor, keep_monitoring);
    00076 }
    00077
    00087 void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 amount, SourceType src_type, SourceID src, const Station *st)
    00088 {
    00089   if (amount == 0) return;
    00090
    00091   if (src != INVALID_SOURCE) {
    00092     /* Handle pickup update. */
    00093     switch (src_type) {
    00094       case ST_INDUSTRY: {
    00095         CargoMonitorID num = EncodeCargoIndustryMonitor(company, cargo_type, src);
    00096         CargoMonitorMap::iterator iter = _cargo_pickups.find(num);
    00097         if (iter != _cargo_pickups.end()) iter->second += amount;
    00098         break;
    00099       }
    00100       case ST_TOWN: {
    00101         CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, src);
    00102         CargoMonitorMap::iterator iter = _cargo_pickups.find(num);
    00103         if (iter != _cargo_pickups.end()) iter->second += amount;
    00104         break;
    00105       }
    00106       default: break;
    00107     }
    00108   }
    00109
    00110   /* Handle delivery.
    00111    * Note that delivery in the right area is sufficient to prevent trouble with neighbouring industries or houses. */
    00112
    00113   /* Town delivery. */
    00114   CargoMonitorID num = EncodeCargoTownMonitor(company, cargo_type, st->town->index);
    00115   CargoMonitorMap::iterator iter = _cargo_deliveries.find(num);
    00116   if (iter != _cargo_deliveries.end()) iter->second += amount;
    00117
    00118   /* Industry delivery. */
    00119   for (const Industry * const *ip = st->industries_near.Begin(); ip != st->industries_near.End(); ip++) {
    00120     CargoMonitorID num = EncodeCargoIndustryMonitor(company, cargo_type, (*ip)->index);
    00121     CargoMonitorMap::iterator iter = _cargo_deliveries.find(num);
    00122     if (iter != _cargo_deliveries.end()) iter->second += amount;
    00123   }
    00124 }
    00125