# ai_cargo.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_cargo.cpp 18330 2009-11-28 20:35:25Z rubidium $ */
    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 "ai_cargo.hpp"
    00013 #include "../../cargotype.h"
    00014 #include "../../economy_func.h"
    00015 #include "../../core/bitmath_func.hpp"
    00016
    00017 /* static */ bool AICargo::IsValidCargo(CargoID cargo_type)
    00018 {
    00019   return (cargo_type < NUM_CARGO && ::CargoSpec::Get(cargo_type)->IsValid());
    00020 }
    00021
    00022 /* static */ char *AICargo::GetCargoLabel(CargoID cargo_type)
    00023 {
    00024   if (!IsValidCargo(cargo_type)) return NULL;
    00025   const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
    00026
    00027   /* cargo->label is a uint32 packing a 4 character non-terminated string,
    00028    * like "PASS", "COAL", "OIL_". New ones can be defined by NewGRFs */
    00029   char *cargo_label = MallocT<char>(sizeof(cargo->label) + 1);
    00030   for (uint i = 0; i < sizeof(cargo->label); i++) {
    00031     cargo_label[i] = GB(cargo->label, (uint8)(sizeof(cargo->label) - i - 1) * 8, 8);
    00032   }
    00033   cargo_label[sizeof(cargo->label)] = '\0';
    00034   return cargo_label;
    00035 }
    00036
    00037 /* static */ bool AICargo::IsFreight(CargoID cargo_type)
    00038 {
    00039   if (!IsValidCargo(cargo_type)) return false;
    00040   const CargoSpec *cargo = ::CargoSpec::Get(cargo_type);
    00041   return cargo->is_freight;
    00042 }
    00043
    00044 /* static */ bool AICargo::HasCargoClass(CargoID cargo_type, CargoClass cargo_class)
    00045 {
    00046   if (!IsValidCargo(cargo_type)) return false;
    00047   return ::IsCargoInClass(cargo_type, (::CargoClass)cargo_class);
    00048 }
    00049
    00050 /* static */ AICargo::TownEffect AICargo::GetTownEffect(CargoID cargo_type)
    00051 {
    00052   if (!IsValidCargo(cargo_type)) return TE_NONE;
    00053
    00054   return (AICargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
    00055 }
    00056
    00057 /* static */ Money AICargo::GetCargoIncome(CargoID cargo_type, uint32 distance, uint32 days_in_transit)
    00058 {
    00059   if (!IsValidCargo(cargo_type)) return -1;
    00060   return ::GetTransportedGoodsIncome(1, distance, Clamp(days_in_transit * 2 / 5, 0, 255), cargo_type);
    00061 }